// holds an instance of XMLHttpRequest

var xmlHttp = createXmlHttpRequestObject();
var globalAction = ""; 	// keeps track of which operation is currently being done
var globalIDNum = 0; 	// keeps track of which record ID is being operated on

// creates an XMLHttpRequest instance

function createXmlHttpRequestObject()
{
	var xmlHttp;	// reference to the XMLHttpRequest object
	
	// Create the XMLHttpRequest Object for all versions of IE up to and including IE6
	try
	{
		// try to create XMLHttpRequest object
		
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
		
		// try every ID until on works
		
		for (var i=0; i < XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create the XMLHttpRequest object
				
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
				
			}
			catch(e){}
		}
	}
		
	// return the created object or display an error message
		
	if(!xmlHttp)
	{
		alert("Error creating the XMLHttpRequest object.");
	}
	else
	{
		return xmlHttp;
	}
}

// called to read a file from the server



function process(action,IDNum, IDNum2)
{
	// only continue if xmlHttp isn't void
	
	if(xmlHttp)
	{
		globalAction = action;				// Set the globalAction
		id1 = IDNum;				// Set the globalIDNum
		id2 = IDNum2;				// Set the globalIDNum
		// try to connect to the server
		
		try
		{
			
			// Change the div contents from plain text to a Select Box
			
			
			if(action == "load_photo")
			{
				
				// Connect asynchonously to the php to update database
				var ddajaxtabssettings={}
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "load_photo.php";	// Set to the url of the server-side page being connected to
				var async = true;			// Setting this argument to true is the backbone of AJAX. It allows for connecting to 
											// a server-side page for processing asynchronously.

				//var doc = document.form_event; // reference the form being used
				//var selStatus = "ident_eType";
				//var id = document.getElementById(selStatus).value;
				var params = "action=" + globalAction + "&id=" + id1; // Parameters to be passed.
			
				xmlHttp.open(method, url, async);
				xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	// Set the Content-Type Header
				xmlHttp.onreadystatechange = handleStateChange;	// Set to the function that will handle the state change
				xmlHttp.send(params);	// Set to the parameter list (as a string) being passed to the server-side script
										// If using the "GET" method, this should be set to null.
										
				ddajaxtabssettings.bustcachevar=1  //bust potential caching of external pages after initial request? (1=yes, 0=no)
				ddajaxtabssettings.loadstatustext="<img src='images/loading.gif' />" 
				divId="load_photo";
				document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message"
			}
			
			
			
			if(action == "load_models")
			{
				
				// Connect asynchonously to the php to update database
				var ddajaxtabssettings={}
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "load_models.php";	// Set to the url of the server-side page being connected to
				var async = true;			// Setting this argument to true is the backbone of AJAX. It allows for connecting to 
											// a server-side page for processing asynchronously.

				//var doc = document.form_event; // reference the form being used
				var selStatus = "make";
				var id = document.getElementById(selStatus).value;
				var params = "action=" + globalAction + "&id=" + id; // Parameters to be passed.
			
				xmlHttp.open(method, url, async);
				xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	// Set the Content-Type Header
				xmlHttp.onreadystatechange = handleStateChange;	// Set to the function that will handle the state change
				xmlHttp.send(params);	// Set to the parameter list (as a string) being passed to the server-side script
										// If using the "GET" method, this should be set to null.
										
				ddajaxtabssettings.bustcachevar=1  //bust potential caching of external pages after initial request? (1=yes, 0=no)
				ddajaxtabssettings.loadstatustext="<img src='images/loading.gif' />" 
				divId="load_models";
				document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message"
										
			
				
			}
			
			
			
			
			
		}
		
		// display the error in case of failure
		catch(e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}



// function that handles the response from the HttpRequestObject
function handleStateChange()
{
	// We wait until the server is ready to respond
	if(xmlHttp.readyState == 4)
	{
		// only continue if status is 200 or OK
		if(xmlHttp.status == 200)
		{
			try
			{
				// Handle the server's response
				
				// Change the div Text to a Select Box
				
				
				if(globalAction == "load_photo")
				{
					var divName = document.getElementById("load_photo");					
					divName.innerHTML = '<span id="load_photo">' + xmlHttp.responseText + '</span>';
				}
				
				if(globalAction == "load_models")
				{
					var divName = document.getElementById("load_models");					
					divName.innerHTML = '<span id="load_models">' + xmlHttp.responseText + '</span>';
				}
				
			
			}
			catch(e)
			{
				// display error message
				alert("Error reading the response: " + e.toString());
			}
		}
		else
		{
			// display status message
			alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}