// Note that this is the same as the FSO Ajax script and should be kept in synch
var objXMLHTTP;
var arrRequestQueue = new Array();

// Attempt to create the XMLHTTP object
function CreateXMLHTTPObj()
{

	// Pretend IE less then 6 dosn't support Ajax
	var version = 10
	if( navigator.appVersion.indexOf("MSIE") != -1 ) {
		var temp = navigator.appVersion.split("MSIE")
		version = parseFloat(temp[1])
	}
	if( version < 6 ) {
		return null;
	}
	
	var objReturn = null;
	try
	{
		objReturn = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			objReturn = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e2)
		{
			objReturn = null;
		}
	}
	if( !objReturn && typeof XMLHttpRequest != "undefined" )
	{
		objReturn = new XMLHttpRequest();
	}
	return objReturn;
}

function ClearRequestQueue()
{
    while( arrRequestQueue.shift() != null ) {}
}

// Used to process all the requests in the queue
function GetRequestFromQueue()
{
	var strRequest, strErrorCall, arrRequest;
	
	// If the request object is ready, get the oldest request from the queue and send it
	if( arrRequestQueue.length > 0 && (!objXMLHTTP || objXMLHTTP.readyState == 0) )
	{
		arrRequest = arrRequestQueue.shift();
		strRequest = arrRequest[0];
		strErrorCall = arrRequest[1];
		SendXMLHTTP( strRequest, strErrorCall );
	}
	
	// Wait half a second and try again
    setTimeout( GetRequestFromQueue, 500 );

}
setTimeout( GetRequestFromQueue, 500 );

// Send the XMLHTTP request
function SendXMLHTTP( strRequest, strErrorCall )
{
	var intTimeoutID;

	// A request is busy, queue the next request
	if( objXMLHTTP && objXMLHTTP.readyState != 0 ){
		var arrRequest = new Array();
		arrRequest[0] = strRequest;
		arrRequest[1] = strErrorCall;
		arrRequestQueue.push( arrRequest );
		return false;
	}
	
	objXMLHTTP = CreateXMLHTTPObj();
	
	if( objXMLHTTP )
	{
		//Prepare the request
		objXMLHTTP.open( "GET", strRequest, true );
		
		// Give the call a time out
		var tempFunction = function()
		{ 
			// If the state is not complete or uninitialized
			if( objXMLHTTP && objXMLHTTP.readyState != 4 && objXMLHTTP.readyState != 0 )
			{
				objXMLHTTP.abort();
				objXMLHTTP = null;
			}
			
			if( strErrorCall )
			{
			    eval( strErrorCall );
			}
		}
		intTimeoutID = setTimeout( tempFunction, 10000 ); 
		
		//Set the ready state change function
		objXMLHTTP.onreadystatechange=function()
		{	
			//If the change is a failed one and there is an error call
			if( objXMLHTTP && objXMLHTTP.readyState == 0 && strErrorCall )
			{
			    clearTimeout( intTimeoutID );
				
				eval( strErrorCall );
			}
			
			//If the change was to complete, and there is some response text
			if( objXMLHTTP && objXMLHTTP.readyState == 4 && objXMLHTTP.responseText )
			{
			    clearTimeout( intTimeoutID );
				
				try
				{
					eval( objXMLHTTP.responseText );
				}
				catch( e )
				{
					eval( strErrorCall );
				}
				objXMLHTTP.abort();
				objXMLHTTP = null;
			}
		};
		
		//Actuall send the request
		objXMLHTTP.send(null);
		
		return false;
	}
	else
	{
		return true;
	}
}
