/* LiquiTech API - Ajax Functions **********************************
 *	
 *	This API contains all the functions needed to manipulate Dojo
 *
 *********************************************************************/
 
/* API Variables * *******************************************************
 *
 *	NOTES:		These variables are used as global references for all
 *				API functions that are included after this API file.
 *				Consequently this creates a dependency that this API file
 *				must be included before any other  API. 
 *
 *************************************************************************/
	
	var glassElement 			= getElement("glassPane");
	
/******************************************************************************/

 /* Set Ajax Url * *******************************************************
 *
 *	TAKES:		Element Name, Server Url, Download Start Callback Function,
 *				Download End Callback Function
 * 	RETURNS:	NOTHING
 *	NOTE:		Uses XML Http Request to get data from the server based on
 *				the Server Url provided, and posts the resulting data to
 *				the DOM object associated with the Element Name id. Also,
 *				optional callback functions for onDownloadStart and onDownloadEnd
 *				are defined. 
 *	
 *************************************************************************/
 
	function setAjaxUrl(elementName, serverUrl, onDownloadStart, onDownloadEnd)
	{
		if (serverUrl.match("\\?"))
		{
			serverUrl = serverUrl + "&opid=" + new Date().getTime();	
		}
		else
		{
			serverUrl = serverUrl + "?opid=" + new Date().getTime();	
		}
		
		if (getElement(elementName))
		{
			var xmlHttpResponseElement = getElement(elementName);
			xmlHttpResponseElement.innerHTML = "";
			showElement(xmlHttpResponseElement.id);
			
			submitXmlHttpRequest(serverUrl, xmlHttpResponseElement, onDownloadStart, onDownloadEnd);
		}
	}

 /* Submit Xml Http Reuqest * *******************************************************
 *
 *	TAKES:		Server Url, Xml Http Response Element, Download Start Callback Function,
 *				Download End Callback Function
 * 	RETURNS:	NOTHING
 *	NOTE:		Opens the connection to the server and requests the data at the
 *				Server Url provided. The resulting data from the server is set to the 
 *				Xml Http Response Element provided.  Any callback functions that are provided
 *				are also fired in this function.
 *	
 *********************************************************************************/
 
	function submitXmlHttpRequest(serverUrl, xmlHttpResponseElement, onDownloadStart, onDownloadEnd)
	{
		var xmlhttp;
		xmlhttp = null;
		
		if (window.XMLHttpRequest)
		{
			// code for all new browsers
		  	xmlhttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			// code for IE5 and IE6
		 	xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
		}
		
		if (xmlhttp!=null)
		{
			xmlhttp.open("GET", serverUrl, true);
		  	
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4) // 4 = loaded
				{
					//getElement("serverRequestStatus").innerHTML = xmlhttp.status;
					
					if (xmlhttp.status == 200) // 200 = OK
					{
						// Call OnRequestComplete function
						if (xmlHttpResponseElement != null)
						{
							xmlHttpResponseElement.innerHTML = xmlhttp.responseText;
							
							// OnDownloadEnd Callback
							if ((onDownloadEnd != false) & (onDownloadEnd != "") & (onDownloadEnd != null)) 
							{
								eval(onDownloadEnd);
							}
					
						}
						else
						{
							//getElement("serverRequestStatus").innerHTML = "Error: xmlHttpResponseElement doesn't exist!";
						}
					}
				}
				else if (xmlhttp.readyState == 1) // 1 = loading
				{
					// OnDownloadStart Callback
					if ((onDownloadStart != false) & (onDownloadStart != "") & (onDownloadStart != null)) 
					{
						eval(onDownloadStart);
					}
				}
				else if (xmlhttp.readyState == 2) // 2 = request sent
				{
					// Call onRequestStart
				}
				else if (xmlhttp.readyState == 3) // 3 = request in process
				{
					// In Process
					
				}		
			};
			
			xmlhttp.send(null);
		}
		else
		{
		  alert("Your browser does not support XMLHTTP.");
		}

	}
	
	
 /* Set Loading Graphic * *******************************************************
 *
 *	TAKES:		Element Id
 * 	RETURNS:	NOTHING
 *	NOTE:		Used with onDonwloadStart - Ajax call
 *
 *********************************************************************************/
 
 	function setLoadingGraphic(elementId)
	{
		var element = getElement(elementId);
		
		if (element)
		{
			showElement(elementId);
			element.innerHTML = "<table width=\"100%\" height=\"100%\"><tr><td align=\"center\" valign=\"center\"><img src=\"/images/loadingGraphicIndicatorLite.gif\"></td></tr></table>";
		}
	}
 