//////////////////////////////////////////////
// Function: GetAjaxObject		   			//
// Arguments: None			    			//
// Return: Proper Ajax object.		    	//
// Purpose: To return the browser specfic   //
//			Ajax object.	    			//
//////////////////////////////////////////////
function GetAjaxObject()
{
	// Object to hold the request object //
	var AjaxObject;
	
	// Figure out which browser we are in //
	if(window.XMLHttpRequest)
	{
		// Not IE //
		AjaxObject = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		// IE //
		// loops through the various versions of XMLHTTP to ensure we're using the latest
	    var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
                        "Microsoft.XMLHTTP"];

        for (var i = 0; i < versions.length ; i++)
		{
		    try
		    {
			    // try to create the object
			    // if it doesn't work, we'll try again
			    // if it does work, we'll save a reference to the proper one to speed up future instantiations
		        AjaxObject = new ActiveXObject(versions[i]);
		
		        if (AjaxObject) {
		            _ms_XMLHttpRequest_ActiveX = versions[i];
		            break;
		        }
		    }
		    catch (objException)
		    {
		    	// trap; try next one
		    };
		}
	}
	return(AjaxObject);
}

//////////////////////////////////////////////
// Function: AjaxRequest		    		//
// Arguments: Method: HTTP Method           //
//	      URL: URL of the target page.  	//
//	      Data: Data to send.	    		//
//	      Process: Callback function.   	//
// Return: Filled out ajax object.	    	//
// Purpose: Sends an request to a remote    //
//	using ajax then process the 	    	//
//	data using the process function.    	//
//////////////////////////////////////////////
function AjaxRequest(Method, URL, Data, Process)
{
	// Save the this var //
	var Self = this;
	
	// Grab the ajax object //
	Self.AjaxObject = GetAjaxObject();
	
	// Check to make sure it worked out //
	if(typeof Self.AjaxObject == 'undefined' || Self.AjaxObject == null)
		return(-1);
		
	// Check to make sure we have a real callback function //
	// If not we set a custom call back function to handle the request //
	if(typeof Process != 'undefined' && Process != null)
		Self.Process = Process;
	else
		Self.Process = DefaultProcess;
	
	var NewArguments = Array();
		
	for(i=4; i<arguments.length; i++)
		NewArguments[i-4] = arguments[i];
		
	// Set the callback function up //
	Self.AjaxObject.onreadystatechange = function()
	{
		var LoadingCheck = 0;
	
		if(Self.AjaxObject.readyState == 4)
		{
			
			HideLoadingDiv();
			
			if(Self.AjaxObject.status == 200)
			{
				// Set the Process function up //
				Self.Process(Self.AjaxObject, NewArguments);
			}
		}
		else
		{
			if(!LoadingCheck)
			{
				LoadingCheck = 1;
				ShowLoadingDiv();
			}
		}
	}
	
	// Snif out the method //
	if(!Method)
		Method = "POST";
		
	// Set the Method string to uppercase //
	Method = Method.toUpperCase();
	
	// Open up the request //
	Self.AjaxObject.open(Method, URL, true);

	// Send some headers //
	if(Method == "POST")
	{
		Self.AjaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		Self.AjaxObject.setRequestHeader("Content-length", Data.length);
		Self.AjaxObject.setRequestHeader("Connection", "close");
	}

	// Check the data //
	if(!Data)
		Data = "";

	// Send the data //
	Self.AjaxObject.send(Data);
	
	// Return the ajax object //
	return(Self.AjaxObject);
}

//////////////////////////////////////////////
// Function: DefaultProcess
// Arguments: AjaxObject: Ajax Object
// Return: None		
// Purpose: Default handler for ajax
//	request. Should be overwritten.	
//////////////////////////////////////////////
function DefaultProcess(AjaxObject)
{
	
}