/* Blue River Digital API - Dojo 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");
 	var xmlhttp					= null;
	var xmlHttpResponseElement 	= null;
	var callOnDownloadStart		= null;
	var callOnDownloadEnd		= null;

/*************************************************************************/
 
	function setAjaxUrl(elementName, ajaxUrl)
	{
		if (ajaxUrl.match("\\?"))
		{
			ajaxUrl = ajaxUrl + "&opid=" + new Date().getTime();	
		}
		else
		{
			ajaxUrl = ajaxUrl + "?opid=" + new Date().getTime();	
		}
		
		showElement(elementName);
		dojo.widget.byId(elementName).setUrl(ajaxUrl);
				
	}
	
/******************************************************************************/

	function setAjaxUrlTest(elementName, serverUrl, onDownloadStart, onDownloadEnd)
	{
		if (serverUrl.match("\\?"))
		{
			serverUrl = serverUrl + "&opid=" + new Date().getTime();	
		}
		else
		{
			serverUrl = serverUrl + "?opid=" + new Date().getTime();	
		}
		
		if (getElement(elementName))
		{
			xmlHttpResponseElement = getElement(elementName);
			showElement(elementName);
			
			callOnDownloadStart 	= onDownloadStart;
			callOnDownloadEnd		= onDownloadEnd;
			
			submitXmlHttpRequest(serverUrl);
		}
	}
	
	function submitXmlHttpRequest(serverUrl)
	{
		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 = GetXmlHttpResult;
			xmlhttp.send(null);
		}
		else
		{
		  alert("Your browser does not support XMLHTTP.");
		}

	}
	
	function GetXmlHttpResult()
	{	
		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 ((callOnDownloadEnd != false) & (callOnDownloadEnd != "") & (callOnDownloadEnd != null)) 
					{
						eval(callOnDownloadEnd);
					}
			
				}
				else
				{
					getElement("serverRequestStatus").innerHTML = "Error: xmlHttpResponseElement doesn't exist!";
				}
			}
		}
		else if (xmlhttp.readyState == 1) // 1 = loading
		{
			// OnDownloadStart Callback
			if ((callOnDownloadStart != false) & (callOnDownloadStart != "") & (callOnDownloadStart != null)) 
			{
				eval(callOnDownloadStart);
			}
		}
		else if (xmlhttp.readyState == 2) // 2 = request sent
		{
			// Call onRequestStart
		}
		else if (xmlhttp.readyState == 3) // 3 = request in process
		{
			// In Process
			
		}
		
	}

/*  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() - 818) / 2;  		// Center with webpage
		loadingElement.style.left = posX + ((818-230)/2);	
		loadingElement.style.top = getPageYOffset()+300;
	}
	
	
/*  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 = "<img src=\"http://www.blueriverdigital.com/images/loadingGraphic1.gif\">";
		setLoadingPosition();
	}
	

/*  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();
	}