// 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 == "LoadHomepageLogos")
			{
				
				// Connect asynchonously to the php to update database
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "homepagelogos.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 + "&id1=" + id1 + "&id2=" + id2; // 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.
			}
			
			if(action == "LoadPackages")
			{
				
				// 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 = "LoadPackages.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 product = "product";
				var product = document.getElementById("product").value;
				var params = "action=" + globalAction + "&product=" + product; // 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="LoadPackages";
				document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message"
			}
			
			if(action == "LoadBrochureImage")
			{
				var ddajaxtabssettings={}
				// Connect asynchonously to the php to update database
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "brochureimages.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 + "&id1=" + 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="LoadBrochureImage";
				document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message"
			}
			
			if(action == "LoadStationaryImage")
			{
				var ddajaxtabssettings={}
				// Connect asynchonously to the php to update database
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "stationaryimages.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 + "&id1=" + 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="LoadStationaryImage";
				document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message"
				
			}
			
			if(action == "LoadLogos")
			{
				var ddajaxtabssettings={}
				// Connect asynchonously to the php to update database
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "logos.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_logos; // reference the form being used
				//var selStatus = "ident_eType";
				//var id = document.getElementById("category_id").value;
				var params = "action=" + globalAction + "&id1=" + id1 + "&id2=" + id2; // 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="<center><img src='images/loading.gif' /></center>" 
				divId="LoadLogos";
				document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message"
			}
			
			
			
			if(action == "LoadLogosHome")
			{
				var ddajaxtabssettings={}
				// Connect asynchonously to the php to update database
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "logos_home.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_logos; // reference the form being used
				//var selStatus = "ident_eType";
				//var id = document.getElementById("category_id").value;
				var params = "action=" + globalAction + "&id1=" + id1 + "&id2=" + id2; // 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="<center><img src='images/loading.gif' /></center>" 
				divId="LoadLogosHome";
				document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message"
			}
			
			if(action == "LoadConsultationResponse")
			{
				var ddajaxtabssettings={}
				// Connect asynchonously to the php to update database
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "LoadConsultationResponse.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_logos; // reference the form being used
				//var selStatus = "ident_eType";
				var name = document.getElementById("name").value;
				var email = document.getElementById("email").value;
				var phone = document.getElementById("phone").value;
				var message = document.getElementById("message").value;				
				
				var params = "action=" + globalAction + "&name=" + name + "&email=" + email + "&phone=" + phone + "&message=" + message; // 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="<center><img src='images/loading.gif' /></center>" 
				divId="LoadConsultationResponse";
				document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message"
			}
			
			if(action == "LoadMeta")
			{				
				// Connect asynchonously to the php to update database
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "loadmeta.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_logos; // reference the form being used
				//var selStatus = "ident_eType";
				
				
				var params = "action=" + globalAction; // 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.
				
			}
			
			if(action == "LoadAmount")
			{
				var ddajaxtabssettings={}
				// Connect asynchonously to the php to update database
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "LoadAmount.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.order; // reference the form being used
				//var selStatus = "ident_eType";
				var product = document.getElementById("product").value;			
				//var package = document.getElementById("package").value;			
				
				var params = "action=" + globalAction + "&product=" + product  + "&package=" + 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="LoadAmount";
				document.getElementById(divId).innerHTML=ddajaxtabssettings.loadstatustext //Display "fetching page message"
				
			}
			
			
			if(action == "LoadAmountPackage")
			{
				var ddajaxtabssettings={}
				// Connect asynchonously to the php to update database
				var method = "POST";		// Set to the method ("GET" or "POST") used to pass variables to the server-side page
				var url = "LoadAmountPackage.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.order; // reference the form being used
				//var selStatus = "ident_eType";
				var product = document.getElementById("product").value;			
				//var package = document.getElementById("package").value;			
				
				var params = "action=" + globalAction + "&product=" + product  + "&package=" + 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="LoadAmount";
				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 == "LoadHomepageLogos")
				{
					var divName = document.getElementById("LoadHomepageLogos");					
					divName.innerHTML = '<span id="LoadHomepageLogos">' + xmlHttp.responseText + '</span>';
				}
				
				if(globalAction == "LoadPackages")
				{
					var divName = document.getElementById("LoadPackages");					
					divName.innerHTML = '<span id="LoadPackages">' + xmlHttp.responseText + '</span>';					
					setTimeout("process('LoadAmount')",250);
					
				}
				
				if(globalAction == "LoadAmount")
				{
					var divName = document.getElementById("LoadAmount");					
					divName.innerHTML = '<span id="LoadAmount">' + xmlHttp.responseText + '</span>';															
				}
				
				
				
				if(globalAction == "LoadBrochureImage")
				{
					var divName = document.getElementById("LoadBrochureImage");					
					divName.innerHTML = '<span id="LoadBrochureImage">' + xmlHttp.responseText + '</span>';
				}
				
				if(globalAction == "LoadStationaryImage")
				{
					var divName = document.getElementById("LoadStationaryImage");					
					divName.innerHTML = '<span id="LoadStationaryImage">' + xmlHttp.responseText + '</span>';
				}
				
				if(globalAction == "LoadLogos")
				{					
					var divName = document.getElementById("LoadLogos");					
					divName.innerHTML = '<span id="LoadLogos">' + xmlHttp.responseText + '</span>';					
					
				}
				
				if(globalAction == "LoadLogosHome")
				{					
					var divName = document.getElementById("LoadLogosHome");					
					divName.innerHTML = '<span id="LoadLogosHome">' + xmlHttp.responseText + '</span>';					
					
				}
				
				if(globalAction == "LoadConsultationResponse")
				{
					var divName = document.getElementById("LoadConsultationResponse");					
					divName.innerHTML = '<span id="LoadConsultationResponse">' + xmlHttp.responseText + '</span>';
				}
				
				if(globalAction == "LoadMeta")
				{
					var divName = document.getElementById("LoadMeta");					
					divName.innerHTML = '<span id="LoadMeta">' + 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);
		}
	}
}