
// Acquire XmlHttpObject for browser
function GetXmlHttpObject() {
	var objXMLHttp = null;
	
	if (window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		try
		{
			objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(e)
			{}
		}
	}
	
	return objXMLHttp;
}

// XmlHttp Request Global Variables 
var xmlHttp = [];
var xmlHttpQueue = [];
var xmlHttpQueueProcessing = false;

// Function to process the queued requests
function proXmlHttpQueue() {
	
	// Lock queue for processing
	xmlHttpQueueProcessing = true;
	
	// Loop all pending requests
	while(xmlHttpQueue.length > 0) {
		if(sendXmlHttpRequest(xmlHttpQueue[0])) xmlHttpQueue.splice(0,1); // remove request from queue
		else break; // if request was not sent end loop
	}
	
	// Unlock queue processing
	xmlHttpQueueProcessing = false;
}

// Function to handle XmlHttp request sending
function sendXmlHttpRequest(request) {
	
	var i = null; // local variable for holding a loop instance
	var httpObjToUse = null; // the XmlHttpObject to use for the current request
	var maxConcurrency = 5; // maximum number of concurrent request at any one time
	var httpQueueElem = null; //
	
	// Get the first available http object
	for(i=0; i < maxConcurrency; i++) {
		if(!xmlHttp[i]) xmlHttp[i] = GetXmlHttpObject(); // if the current http object does not exist, create it
		if(xmlHttp[i].readyState == 0 || xmlHttp[i].readyState == 4 || xmlHttp[i].readyState == "complete") { // if the current http object is available, use it
			if(xmlHttp[i].readyState != 0) xmlHttp[i] = GetXmlHttpObject(); // create a handle to a new xmlHttpObject if the current has been previously used (fix for IE(7) as it can only use an xmlHttpObject once successfully)
			httpObjToUse = i;
			break;
		}
	}
	
	// If no http object has been initialised, alert user and return request failed
	if(!xmlHttp[0]) {
		alert("Browser does not support AJAX Requests");
		return false;
	}
	// If no http object is available, return request failed
	else if(httpObjToUse == null) return false;
	
	// Function definition for state changes
	xmlHttp[httpObjToUse].onreadystatechange = function() {
		
		if (xmlHttp[httpObjToUse].readyState == 4 || xmlHttp[httpObjToUse].readyState == "complete")
		{
			/*try
			{*/
				var HttpResponse = eval('(' + xmlHttp[httpObjToUse].responseText + ')');
				if(HttpResponse.eid.length > 0) document.getElementById(HttpResponse.eid).innerHTML = HttpResponse.html;
				if(HttpResponse.returnfunc.length > 0) eval(HttpResponse.returnfunc + '(HttpResponse)');
				if(HttpResponse.extended == 1) HttpReturnLevelOne(HttpResponse);
				else if(HttpResponse.extended == 2) HttpReturnLevelTwo(HttpResponse);
			/*}
			catch(e)
			{
				alert("Request Failed.\nEither your session has timed out or the returned content is invalid.");
			}*/
			
			// If not currently processing requests, process the queue
			if(!xmlHttpQueueProcessing) proXmlHttpQueue();
		}
		
	}
	
	// Action the http request
	xmlHttp[httpObjToUse].open(request.method,request.url,true);
	if(request.method == "POST")
	{
		xmlHttp[httpObjToUse].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp[httpObjToUse].setRequestHeader("Content-length", request.postvars.length);
		xmlHttp[httpObjToUse].setRequestHeader("Connection", "close");
	}
	xmlHttp[httpObjToUse].send(request.postvars);
	
	// Return request sent successfully
	return true;
} 

// Function to handle XmlHttp request concurrency and queuing
function getcontent(method,url,urlvars,postvars,eid,returnfunc) {
	// Prep supplied content for transmission
	if(method == "POST") { 
		postvars = postvars + "&eid=" + eid;
		if (returnfunc.length > 0) postvars = postvars + "&rf=" + returnfunc;
	}
	else {
		urlvars = urlvars + "&eid=" + eid;
		if (returnfunc.length > 0) urlvars = urlvars + "&rf=" + returnfunc;
	}
		
	url = url + "?";
	if(urlvars.length > 0) url = url + urlvars + "&";
	url = url + "sid=" + Math.random();
	
	if(eid.length > 0) {
		var xPos = document.getElementById(eid).offsetWidth;
		var yPos = document.getElementById(eid).offsetHeight;
		var containerSource = "<div style='width: " + xPos + "; height: " + yPos + "; text-align: center;'><img src='/images/ajax-loader-mini.gif' width='19' height='19' border='0'></div>";
		document.getElementById(eid).innerHTML = containerSource;
	}
	// END prep supplied content
	
	// Queue request for processing
	httpQueueElem = {
						"method" : method,
						"url" : url,
						"urlvars" : urlvars,
						"postvars" : postvars,
						"eid" : eid,
						"returnfunc" : returnfunc
	} 
	xmlHttpQueue[xmlHttpQueue.length] = httpQueueElem;
	
	// If not currently processing requests, process the queue
	if(!xmlHttpQueueProcessing) proXmlHttpQueue();
}
