var blnSupportsXMLHTTP = false;
var objXMLHTTP;
var arrRequestQueue = new Array();


// Initialize by checking to see if XMLHTTP is supported
function Initialize()
{
	if( CreateXMLHTTPObj() )
		blnSupportsXMLHTTP = true;
	else
		blnSupportsXMLHTTP = false;
}

// Attempt to create the XMLHTTP object
function CreateXMLHTTPObj()
{

	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;
}

// Used to process all the requests in the queue
function GetRequestFromQue()
{
	var strRequest, strErrorCall, arrRequest;
	
	// If the request object is ready, get the oldest request from the que and send it
	if( !objXMLHTTP || objXMLHTTP.readyState == 0 )
	{
		arrRequest = arrRequestQueue.shift();
		strRequest = arrRequest[0];
		strErrorCall = arrRequest[1];
		SendXMLHTTP( strRequest, strErrorCall );
	}
	
	// If there's more requests, wait half a second and try again
	if( arrRequestQueue.length > 0 )
		setTimeout( GetRequestFromQue, 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 );
		setTimeout( GetRequestFromQue, 500 );
		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.readyState != 4 && objXMLHTTP.readyState != 0 )
			{
				objXMLHTTP.abort();
				// Force a call to make sure that strErrorCall gets run
				objXMLHTTP.onreadystatechange();					
				objXMLHTTP = null;
			}
		}
		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.readyState == 0 && strErrorCall )
			{
				clearTimeout( intTimeoutID );
				eval( strErrorCall );
			}
			//If the change was to complete, and there is some response text
			if( 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;
	}
}
