// JavaScript Document

function createRequestObject() {
    
        var req;
    
        if(window.XMLHttpRequest){
            // Firefox, Safari, Opera...
            req = new XMLHttpRequest();
        } else if(window.ActiveXObject) {
            // Internet Explorer 5+
            req = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            // There is an error creating the object,
            // just as an old browser is being used.
            alert('There was a problem creating the XMLHttpRequest object');
        }
    
        return req;
    
    }
    
    // Make the XMLHttpRequest object
    var http = createRequestObject();
	var http1 = createRequestObject();
    var http2 = createRequestObject();
	
    function sendRequest(act) {
        
		// test input for an integer
		if (parseInt(act)==act-0) {
				// Open PHP script for requests
				http.open('get', 'celebtext.php?act='+act);
				http.onreadystatechange = handleResponse;
				http.send(null);
				
				//set focus on inline frame
				document.getElementById('celebrity_favorites_inline_frame_id').focus();
				
				// Open PHP script for picture requests
				//http1.open('get', 'celebpict.php?act='+act);
				//http1.onreadystatechange = handlePictResponse;
				//http1.send(null);
				
		}
    }
    
    function changeRequest() {
        
		// test input for an integer
				// Open PHP script for requests
				http2.open('get', 'confirmation.php');
				http2.onreadystatechange = handleChange;
				http2.send(null);
				
		
    }	
	
	    function handlePictResponse() {
    
			/* NOTE: If the placeholder image is not the same size as the image you retrieve, the original image height and width
			will not change. I added two lines of code above to correct that problem.
		
			Clearly we are not preloading the images but are retrieving them remotely after the page has completely loaded.
			Similar to AJAX, the call to retrieve the data requires a function which acts on the data once it has been retrieved.
			In the case of the XMLHTTP object the function is attached to the XMLHTTP object's "onreadystatechange" event. Here we
			attach the function to an image object's "onload" event.
		
			Also like the AJAX, once we know the data has been returned we need to insert the data into an existing document. Since
			it is not text or markup, the innerHTML method is not appropriate and we can either attach the new image to the existing
			document using the appendChild DOM method or assign its source to an existing image. 
	
			Rod Divilbiss (original coder and article writer)
			5/18/2006
			Updated 3/23/2007 */
	
	
	
			if(http1.readyState == 4 && http1.status == 200){
		
				// Text returned FROM PHP script
					var pImageURL =  http1.responseText;
					var img=frames['celebrity_inline_frame'].document.createElement('img');	
					
					//alert( pImageURL );
					img.onload = function (evt) {
							frames['celebrity_inline_frame'].document.getElementById('celebpict').src=this.src; 
							frames['celebrity_inline_frame'].document.getElementById('celebpict').width=this.width;
							frames['celebrity_inline_frame'].document.getElementById('celebpict').height=this.height;
						}

					img.src=pImageURL;
			}

		}
	
	
	
     function gotoRequest() {
      		/* This function reteives requests from the drop down menu on the "celebrity page" and passes them to 
			"sendRequest" */
			
		//alert(document.getElementById("menu1").value);  
		
		// send selected value to sendRequest
		sendRequest(document.getElementById("menu1").value);

    }
    function handleResponse() {
    

	
        if(http.readyState == 4 && http.status == 200){
 		  
            // Text returned FROM PHP script
            var response = http.responseText;
    				
            if(response) {


					frames['celebrity_inline_frame'].document.getElementById('celebtext').innerHTML=response;
					//alert(frames['celebrity_inline_frame'].document.getElementById('celebtext').innerHTML); 

            }
    
        }
    }
   
    function handleChange() {
    

	
        if(http2.readyState == 4 && http2.status == 200){
 		  
            // Text returned FROM PHP script
            var response = http2.responseText;
    				
            if(response) {


					document.getElementById('test').innerHTML=response;

            }
    
        }
    }

    function celebtext() {
        sendRequest('celebtext');
    }
