var requestObjects = new Array();			// Array of all the AjaxRequestObjects for the page.

function AjaxRequestObject()
{
	// Properties
	this.xmlhttp = false;					// The XML HTTP object
	this.requestType = "GET";				// Default to "GET"
	this.postParameters;					// POST url parameters
	this.url;								// URL of the request
	this.customHandler;						// Custom function used to process the returned XML
	this.retries = 3;						// Default to 3
	this.randomizeURL = false;				// Option to randomize URL to stop caching possible issues
	
	// Custom properties.
	this.targetID;							// The ID of the target object used in the customHandler
	this.selectedIndexValue;				// The selectedIndex of a <select> object used in the customHandler
		
	// Methods
	this.startRequest = MakeXmlHttpRequest;	// Function that initiates the XML HTTP request.
}

function MakeXmlHttpRequest()
{
	this.xmlhttp = false;
	
	if(window.XMLHttpRequest) // Default request object, compatible with Mozilla, IE7...
	{
    	try
    	{
			this.xmlhttp = new XMLHttpRequest();
			try
			{
				this.xmlhttp.overrideMimeType('text/xml'); // Mozilla only...
			}
			catch(e)
			{
				// Do nothing since this is most likely an incompatable browser.
			}
		}
		catch(e)
		{
			this.xmlhttp = false;
		}
	}
	else if(window.ActiveXObject) // Default request object for IE (!IE7)...
	{
       	try
       	{
        	this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	}
      	catch(e)
      	{
        	try
        	{
          		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	}
        	catch(e)
        	{
          		this.xmlhttp = false;
        	}
		}
	}
	
	requestObjects.push(this);	// Add our object to the Array of objects...
	
	if(this.xmlhttp)
	{
		// Generates a random string appended to the request to stop browsers from caching results
		if(this.randomizeURL == true)
		{
			var charString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
			var randomString = "";
			for(var i = 0; i < 12; i++) {
				var randNum = Math.floor(Math.random() * charString.length);
				randomString += charString.substring(randNum, randNum + 1);
			}
			var urlRandomization = "&randomization=" + randomString;
			if(this.requestType == "GET")
				this.url += urlRandomization;
			else if(this.requestType == "POST")
				this.postParameters += urlRandomization;
		} 
		// Called each time the state changes.		
		// 0 – Uninitialized
		// 1 – Loading
		// 2 – Loaded
		// 3 – Interactive
		// 4 – Completed
		this.xmlhttp.onreadystatechange = ProcessRequest;
		if(this.requestType == "GET")
		{
			this.xmlhttp.open(this.requestType, this.url, true);
			this.xmlhttp.send(null);
		}
		else if(this.requestType == "POST")
		{			
			this.xmlhttp.open(this.requestType, this.url, true);
			this.xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			this.xmlhttp.send(this.postParameters);
		}
	}	
}

function ProcessRequest()
{
	if(typeof(window['requestObjects']) == "undefined")
		return;	
	for(var i = 0; i < requestObjects.length; i++)
	{
		if(requestObjects[i].xmlhttp.readyState == 4)
		{
			try
			{
				if(requestObjects[i].xmlhttp.status == 200 || requestObjects[i].xmlhttp.status == 304)
				{
					var node;
					if(requestObjects[i].xmlhttp.responseXML)
						node = requestObjects[i].xmlhttp.responseXML.documentElement;
					else	// ResponseXML is bad, try fixing for Mozilla...
					{					
						var parser = new DOMParser();
						var xmlDoc = parser.parseFromString(requestObjects[i].xmlhttp.responseText, "text/xml");
						node = xmlDoc.documentElement;
					}
					requestObjects[i].customHandler(node, requestObjects[i]);
					requestObjects.splice(i, 1); // Done, remove the AJAX object from the array
				}
				else
				{
					// If status is an error or timeout, retry X number of times
					if(requestObjects[i].retries > 1)
					{
						requestObjects[i].xmlhttp.onreadystatechange = ProcessRequest;
						requestObjects[i].xmlhttp.open(requestObjects[i].requestType, requestObjects[i].url, true);
						requestObjects[i].xmlhttp.send(null);
						if(this.requestType == "GET")
						{
							requestObjects[i].xmlhttp.send(null);
						}
						else if(this.requestType == "POST")
						{
							requestObjects[i].xmlhttp.send(this.postParameters);
						}
						requestObjects[i].retries--;
					}
				}
			}
			catch(e)
			{
			
			}			
		}
	}
}