
/**********************************
*	     GLOBAL VARIABLES
***********************************/

//Global Array to hold instances of the XMLHttpRequest Objects that we will use
var AjaxArray = new Array(0);


/****************************************
*   "PRIVATE" UTILITY FUNCTION 
* don't call these functions explicitly
******************************************/

/*
	<summary>Stock function to get an XMLHttpRequest Object in all browsers</summary>
	<returns>An XMLHttpRequest object</returns>
*/
function getHTTPObject() 
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	{
		try 
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E) 
			{
				xmlhttp = false;
			}
		}
	}
	@else
	{
		xmlhttp = false;
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		try 
		{
			xmlhttp = new XMLHttpRequest();
		} 
		catch (e)
		{
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

/*
	<summary>Dynamically generate an OnReadyStateChange handler.</summary>
	<param name="p_ArrayIndex">the index of the array that contains the XMLHttpRequest object associated with this callback</param>
	<param name="p_StateCompleteCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is complete</parm>
	
*/
function MakeShortHTTPRequestHandler(p_ArrayIndex, p_StateCompleteCallbackFunction)
{
	var FunctionString= "var objHttpRequest = AjaxArray[" + p_ArrayIndex + "];\
						 if (objHttpRequest.readyState == 4) \
						 { \
							" + p_StateCompleteCallbackFunction + "; \
						 }"
	return new Function(FunctionString);

}	

/*
	<summary>Dynamically generate an OnReadyStateChange handler.</summary>
	<param name="p_ArrayIndex">the index of the array that contains the XMLHttpRequest object associated with this callback</param>
	<param name="p_StateUninitializedCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Uninitialized</parm>
	<param name="p_StateLoadingCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Loading</parm>
	<param name="p_StateLoadedCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Loaded</parm>
	<param name="p_StateInteractiveCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Interactive</parm>
	<param name="p_StateCompleteCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Complete</parm>
	
*/
function MakeCompleteHTTPRequestHandler(p_ArrayIndex, 
								p_StateUninitializedCallbackFunction, 
								p_StateLoadingCallbackFunction,
								p_StateLoadedCallbackFunction,
								p_StateInteractiveCallbackFunction,
								p_StateCompleteCallbackFunction
								)
{
	return new Function(
		"var objHttpRequest = AjaxArray[" + p_ArrayIndex + "];\
		if (objHttpRequest.readyState == 0) \
		{ \
		" + p_StateUninitializedCallbackFunction + "; \
		} \
		if (objHttpRequest.readyState == 1) \
		{ \
		" + p_StateLoadingCallbackFunction + "; \
		} \
		if (objHttpRequest.readyState == 2) \
		{ \
		" + p_StateLoadedCallbackFunction + "; \
		} \
		if (objHttpRequest.readyState == 3) \
		{ \
		" + p_StateInteractiveCallbackFunction + "; \
		} \
		if (objHttpRequest.readyState == 4) \
		{ \
		" + p_StateCompleteCallbackFunction + "; \
		} "
	)
}




/*******************************
*   "PUBLIC" FUNCTIONS
* call these from your code.
*******************************/

/*
	<summary>Use this function to produce an ajax call.  Only offers callback for Complete state</summary>
	<param name="p_urlToOpen">The url to make a request of</param>
	<param name="p_StateCompleteCallbackFunction">the function that will be called when the url is opened</param>
	<returns>An Ajax object</returns>

*/
function RunShortAjax(p_urlToOpen, p_StateCompleteCallbackFunction)
{
	//Make an XMLHttpRequest object and push it onto the end of our AjaxArray
	AjaxArray.push(getHTTPObject());
	//Grab the element that we just pushed on and open the requested url
	AjaxArray[AjaxArray.length - 1].open("GET", p_urlToOpen, true);
	
	//make a handler function that will handle this response
	var myFunction = MakeShortHTTPRequestHandler(AjaxArray.length - 1, p_StateCompleteCallbackFunction)

	//assign our dynamically created function to the 
	AjaxArray[AjaxArray.length - 1].onreadystatechange = myFunction
	//Run the request
	AjaxArray[AjaxArray.length - 1].send(null);
}

/*
	<summary>Use this function to produce an ajax call.  Offers all callbacks for all states</summary>
	<param name="p_urlToOpen">The url to make a request of</param>
	<param name="p_StateUninitializedCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Uninitialized</parm>
	<param name="p_StateLoadingCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Loading</parm>
	<param name="p_StateLoadedCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Loaded</parm>
	<param name="p_StateInteractiveCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Interactive</parm>
	<param name="p_StateCompleteCallbackFunction">the name of the function to call along with any parameters that will be calld when readystate is Complete</parm>
	<returns>An Ajax object</returns>
*/
function RunCompleteAjax(p_urlToOpen,
						p_StateUninitializedCallbackFunction, 
						p_StateLoadingCallbackFunction,
						p_StateLoadedCallbackFunction,
						p_StateInteractiveCallbackFunction,
						p_StateCompleteCallbackFunction
						)
{
	//Make an XMLHttpRequest object and push it onto the end of our AjaxArray
	AjaxArray.push(getHTTPObject());
	//Grab the element that we just pushed on and open the requested url
	AjaxArray[AjaxArray.length - 1].open("GET", p_urlToOpen, true);
	
	//make a handler function that will handle this response
	var myFunction = MakeCompleteHTTPRequestHandler(AjaxArray.length - 1, 
													p_StateUninitializedCallbackFunction, 
													p_StateLoadingCallbackFunction,
													p_StateLoadedCallbackFunction,
													p_StateInteractiveCallbackFunction,
													p_StateCompleteCallbackFunction
													)

	//assign our dynamically created function to the 
	AjaxArray[AjaxArray.length - 1].onreadystatechange = myFunction
	//Run the request
	AjaxArray[AjaxArray.length - 1].send(null);
}