/* Blue River Digital 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 BRD
 *				API functions that are included after this API file.
 *				Consequently this creates a dependency that this API file
 *				must be included before any other BRD API. 
 *
 *************************************************************************/
	
	var glassElement 			= getElement("glassPane");
	var loadingElement			= getElement("loadingGraphic");
 	
/******************************************************************************/

 /* 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 Positon * *******************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTE:		Sets the position of the GLOBAL loading element object defined
 *				in the "API Variables" section above, to the center of the current
 *				webpage both vertically and horizontally.
 *
 *************************************************************************/
 
	function setLoadingPosition()
	{
		var posX;
		posX = (getWindowWidth() / 2);  		// Center with webpage
		
		if (IE)
		{
			loadingElement.style.left = posX - 75;	
			loadingElement.style.top = ((getWindowHeight() - 75) / 2) + getPageYOffset();
		}
		else
		{
			loadingElement.style.left = (posX - 75) + "px";	
			loadingElement.style.top = ((getWindowHeight() - 75) / 2) + getPageYOffset() + "px";
		}
	}
	
	
/*  Show Loading Graphic * *******************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTE:		Loads the loading graphic into the loading object, centers the
 *				loading object on the page and displays the grpahic.
 *
 *************************************************************************/
	
	function showLoadingGraphic()
	{
		loadingElement.style.display = "block";
		loadingElement.innerHTML = "<div style=\"border: solid 1px #666666; background-color: #FFFFFF; padding: 10px; width: 150px;\" align=\"\center\"><img src=\"/images/loadingGraphic1.gif\"></div>";
		setLoadingPosition();
	}

/*  Set Loading Graphic * *******************************************************
 *
 *	TAKES:		Element Id
 * 	RETURNS:	NOTHING
 *	NOTE:		Loads the loading graphic into the element object, taking on the
 *				size attributes of the element and graying out the background
 *
 *************************************************************************/
	
	function setLoadingGraphic(elementId)
	{
		if (elementId != "")
		{
			var element = getElement(elementId);
			
			element.innerHTML = "<table width=\"" + getWidth(element) + "\" height=\"" + (.75 *getHeight(element)) + "\"><tr valign=\"center\"><td align=\"center\"><img src=\"/images/loadingGraphic1.gif\"></td></tr></table>";
		}
	}
	

/*  Set Custom Loading Graphic * *******************************************************
 *
 *	TAKES:		Element Id, Loading Image Graphic
 * 	RETURNS:	NOTHING
 *	NOTE:		Loads the loading graphic into the element object, taking on the
 *				size attributes of the element and graying out the background
 *
 *************************************************************************/
	
	function setCustomLoadingGraphic(elementId, loadingImage)
	{
		if (elementId != "")
		{
			var element = getElement(elementId);
			
			element.innerHTML = "<table width=\"" + getWidth(element) + "\" height=\"" + (.75 *getHeight(element)) + "\"><tr valign=\"center\"><td align=\"center\"><img src=\"" + loadingImage + "\"></td></tr></table>";
		}
	}


/*  Hide Loading Graphic * *******************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *	NOTE:		Clears out the loading object and then hides the loading object.
 *
 *************************************************************************/
 
	function hideLoadingGraphic()
	{
		loadingElement.style.display = "none";
		loadingElement.innerHTML = "";
		setLoadingPosition();
	}
