/* 
Author: Aaron Murray
email : a a r o n m u r r a y @ m s n . c o m 
*/


//page that will handle the Ajax requests
var AjaxServerPageName = "AjaxServer.aspx";	//this is the only line that needs to be modified in this file

/*Global XMLHTTP Request object
The trouble with this is if multiple AJAX requests are coming back at once...
then we may need more objects (or an Array?) and some way of telling the EventHandlers which object contains the return data
Or should we make a new object for each call?
*/
var XmlHttp;


//Creating and setting the instance of appropriate XMLHTTP Request object to a global “XmlHttp” variable  
function CreateXmlHttp()
{

    if (XmlHttp != null) {
        XmlHttp = null;
        if (XmlHttp != null) {
            alert("wacky, I couldn't make the XmlHttp object null...");
        }
    }    

	//Creating object of XMLHTTP in IE
	try	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(oc) {
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") {
		XmlHttp = new XMLHttpRequest();
	}
}


//generic method for submitting & regesitering an AJAX request
//Example: SubmitAjaxRequest("changepicture", "&imgid=" + pId, myEventHandlerName);
function SubmitAjaxRequest(pServerFunctionName, pUrlParams, pEventhandler) {
	//prepare the correct params for the processing page
	var requestUrl = AjaxServerPageName + "?function=" + pServerFunctionName + pUrlParams + "&hacktostopcaching=" + encodeURIComponent(new Date());	
	//alert(requestUrl);
	CreateXmlHttp();	//set up the global XmlHttp object to send the request
	
	if(XmlHttp)	// then the browser supports XMLHTTPRequest object
	{
		XmlHttp.onreadystatechange = pEventhandler;	//Setting the event handler for the response. This shouldn't be a string
		
		//Initializes the request object with GET (METHOD of posting), Request URL and sets the request as ASYNCHRONOUS.
		XmlHttp.open("GET", requestUrl,  true);	
		XmlHttp.send(null);		//Sends the request to server
	} 
}

//generic method for submitting & regesitering an AJAX request
//Example: SubmitAjaxRequest("changepicture", "&imgid=" + pId, myEventHandlerName);
function SubmitAjaxRequestSynchronous(pServerFunctionName, pUrlParams, pEventhandler) {
	//prepare the correct params for the processing page
	var requestUrl = AjaxServerPageName + "?function=" + pServerFunctionName + pUrlParams + "&hacktostopcaching=" + encodeURIComponent(new Date());	

	CreateXmlHttp();	//set up the global XmlHttp object to send the request
	
	if(XmlHttp)	// then the browser supports XMLHTTPRequest object
	{
		XmlHttp.onreadystatechange = pEventhandler;	//Setting the event handler for the response. This shouldn't be a string
		
		//Initializes the request object with GET (METHOD of posting), Request URL and sets the request as SYNCHRONOUS.
		XmlHttp.open("GET", requestUrl,  false);	
		XmlHttp.send(null);		//Sends the request to server
	} 
}