// Universal transferable AJAX functions

// ************* XML HTTP Object Functions *********************

var XMLHttpFactories = 
[
	function () 
	{
		return new XMLHttpRequest() // Firefox, Opera 8.0+, Safari => create standard object, if fails
	},
	function () 
	{
		return new ActiveXObject("Msxml2.XMLHTTP") // Internet Explorer => create latest M$ object, if fails
	},
	function () 
	{
		return new ActiveXObject("Msxml3.XMLHTTP") // Internet Explorer => create latest M$ object, if fails
	},
	function () 
	{
		return new ActiveXObject("Microsoft.XMLHTTP") //old IE => create older M$ object, if fails
	} 
]; // variable containing instantiations of each, increasingly older xmlHTTP object

function createXMLHTTPObject() //this function creates the xmlHTTP request object, in different browsers
{
	var xmlHttp = false;
	
	for ( var i=0 ; i<XMLHttpFactories.length ; i++ ) // go from 0 to length of the xmlHTTP array
	{
		try 
		{
			xmlHttp = XMLHttpFactories[i](); // try an XML object according to index
		}
		catch (e) 
		{
			continue; // catch any errors and continue along the loop until done
		}
		break; // if fails completely then break
	}
	return xmlHttp;
}

// make actual request to page using ajax call. Passed arguments are utilised. This function is made as generic as possible
function sendXMLRequest(requestPage,postData,requestHandler)
{
	// NB - postData must be a formatted string that contains "exampleVariable=value&exampleVariable=value&...."
	var request = createXMLHTTPObject(); // create the XML object for every request call
	if ( !request ) // check to see if the creation was succesful, otherwise dismiss the user
	{
		alert("Your cannot use this site, please upgrade your browser or turn on JavaScript");
		return;
	}
	
	var requestMethod = ( postData ) ? "POST" : "GET" ; // if no post data is supplied then make it a get
	request.open(requestMethod,requestPage,true); // open the ajax request to the server at specified page with specified method, asynchronously
	
	request.setRequestHeader('User-Agent','XMLHTTP/1.1'); // make sure the request header is the correct XHTML version */
	
	if ( postData ) // check to see if you are using postData and assign the right HTTP headers
	{
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length", postData.length);
		request.setRequestHeader("Connection", "close");
	}

	/* Explanation of ready states
	
	0 = uninitialized – open() has not yet been called.
	1 = open – send() has not yet been called.
	2 = sent – send() has been called, headers and status are available.
	3 = receiving – Downloading, responseText holds partial data (although this functionality is not available in IE)
	4 = loaded – Finished.
	
	*/ 

	request.onreadystatechange = 	function() // event handler that fires every time the ready state changes
									{	
										if ( request.readyState == 4 && request.status == 200 ) // if the ready state is 4 and the HTTP is clear, process the response
										{
											requestHandler(request);
										}
										else if ( request.readyState != 4 ) // if the ready state doesn't get to 4, fail
										{
											return;
										}
										else if ( request.status != 200 && request.status != 304 ) // if the HTTP status is in error display and fail
										{
											alert("HTTP Error:" + request.status);
											return;
										}
									}
	if ( request.readyState == 4 ) // make sure before sending the request a previous request has not been left undealt with
	{
		return;
	}
	
	request.send(postData); // send the request using the post data. parameter will be already be null if it is a get
}

// ****************** End XML HTTP Object Functions *********************

// ****************** GET URL OBJECT **********************

// Constructor for the 'get' parameter object. Helps form the get parameters to be passed to Ajax function
function getParamWrapper(name,value)
{
	this.name = name;
	this.value = value;
	this.getParam = createGetParam;
}

// Function that will create the finished get parameter.
function createGetParam()
{
	getURL = this.name + "=" + this.value;
	return getURL;
}

// ****************** End Get URL Object **********************
