var httpReqQueue = new Array(); 

//=======================================
// DESCRIPTION:
// Instantiate an httpReqQueue variable
//=======================================
// USAGE:
// Queue several AJAX requests with first-in first-out (FIFO) processing order
//=======================================
function httpQueuedReq(freed, functionHandlerName)
{
	this.freed = freed;
	this.functionHandlerName = functionHandlerName;
	this.xmlhttp = false;
	try
	{
		if (window.XMLHttpRequest)
		{ this.xmlhttp = new XMLHttpRequest(); }
		else if (window.ActiveXObject)
		{ this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	catch(e)
	{
		alert("httpQueuedReq()\n" + e);
	}
}

//================================
// DESCRIPTION:
// Add an httpRequest to the queue
//================================
// USAGE:
// Every time we need to send an httpRequest, call this method, passing in the URL and the functionHandlerName
//================================
function httpReqQUEUE(url, functionHandlerName) 
{	
	// Create a date/time stamp to append to the end of the URL string
	// 	(this is needed to fool IE into realizing we are requesting a new
	// 	page each time, not a cached version of the last one IE retrieved)
	// This URL would look like this:    response-page.aspx?z=20071005092545
	var date = new Date();
	var newDate = date.getDate();
	var newTime = date.getTime();	
	
	// If a ? is found, we need to just append another Query String variable with a & sign
	if (url.indexOf("?") > -1)
	{	url = url + "&z=" + newDate + newTime; }
	// If no ? is found, we have a URL without any Query String variables, so we'll use a ? sign to add one
	else
	{	url = url + "?z=" + newDate + newTime;	}
	
	var pos = -1; 
	for (var i=0; i<httpReqQueue.length; i++) 
	{ 
		if (httpReqQueue[i].freed == 1) 
		{
			pos = i;
			break;
		} 
	}
	if (pos == -1)
	{
		pos = httpReqQueue.length;
		httpReqQueue[pos] = new httpQueuedReq(1, functionHandlerName);
	}
	if (httpReqQueue[pos].xmlhttp)
	{
		httpReqQueue[pos].freed = 0;
		httpReqQueue[pos].functionHandlerName = functionHandlerName;
		httpReqQueue[pos].xmlhttp.open("GET",url,true); 
		httpReqQueue[pos].xmlhttp.onreadystatechange = function() { HandleQueuedRequests(pos);  } 
		if (window.XMLHttpRequest) 
		{ httpReqQueue[pos].xmlhttp.send(null); } 
		else if (window.ActiveXObject) 
		{ httpReqQueue[pos].xmlhttp.send(); } 
	}
} 

// Method will wait for all queued httpReqs to be completed before doing anything with the results
function HandleQueuedRequests(pos)
{
	// If the server has processed the request and sent back a HTTP response
	if (typeof(httpReqQueue[pos]) != 'undefined' && httpReqQueue[pos].freed == 0)
	{
		// If readyState is 4, server has returned a response
		if (httpReqQueue[pos].xmlhttp.readyState == 4)
		{	
			// If the HTTP response status code is 200 or 304, the request succeeded
			if (httpReqQueue[pos].xmlhttp.status == 200 || httpReqQueue[pos].xmlhttp.status == 304)
			{
				httpReqQueue[pos].freed = 1;
			}
			else
			{ alert("The server returned status code: "+httpReqQueue[pos].xmlhttp.status+"\n"+httpReqQueue[pos].xmlhttp.statusText); }
					
			// Loop through httpReqQueue array, checking if all items have been processed and we 
			// have recieved responses for each request in the array
			var allItemsProcessed = true;
			var outputTEXT = "";
			for (var i = 0; i < httpReqQueue.length; i++)
			{
				// If item is awaiting a response from the server still
				if (httpReqQueue[i].freed == 0)
				{
					allItemsProcessed = false;
				}
			}
			
			// We need to wait until all httpReq items in our queue are processed
			if (allItemsProcessed == true)
			{
				// Loop through all httpReqQueue that are finished processing
				for (var pos = 0; pos < httpReqQueue.length; pos++)
				{
					// Evaluate the functionHandlerName property into an actual JS function 
					// and pass in the pos of the httpReqQueue to be used
					eval(httpReqQueue[pos].functionHandlerName + "(" + pos + ")");
				}			
			}
		}
	}
}



