var xhttp = null;
var objInfoRetriever = null;
var busy=false;
var lastRequestTime = null;
var nextURL = null;
var nextCallBack = null;
var hashcache = new Array();

var isie = ( navigator.userAgent.toLowerCase() == "msie" );



function RetrieveInfo(objEntityID, objEntityItemID, objCallback)
{
	var url = "./JCInline.aspx?entityID=" + objEntityID + "&itemID=" + objEntityItemID;
	objInfoRetriever = new InfoRetriever(url, objCallback);
}

function RetrieveDocuments(position, objCallback)
{
	var url = "./inlineInformation.aspx?relpos=" + position
	objInfoRetriever = new InfoRetriever(url, objCallback)
}

/*function InfoRetriever(url, objCallback) 
{

	this.Callback = objCallback;
	this.Completed = false;
	this.Url = url;
	this.init();
	
	if (xhttp)
	{
		this.xhttpLookup(url + "&cb=" + Math.random()); //cb added to interfere with potentially conflicting caches
	}
	
}*/

function InfoRetriever(url, objCallback, method, param) 
{
	this.Callback = objCallback;
	this.Completed = false;
	this.Url = url;
	this.Param = param;
	this.init();

	if (xhttp)
	{
		if (method=="post")
		{
			this.xhttpPost(url + "&cb=" + Math.random());
		}
		else
		{
			this.xhttpLookup(url + "&cb=" + Math.random()); //cb added to interfere with potentially conflicting caches
		}
	}
}

function RetrievePrintResults(url, callback, method, param)
{
	objInfoRetriever = new InfoRetriever(url,callback,method,param);
	return true;
}

function UpdateSelection(changes, callback)
{
	var url = "./resultsinline.aspx?Mode=Check&Changes=" + changes;
	objInfoRetriever = new InfoRetriever(url,callback);
	return true;
}

function RetrieveResults(filter, datasources, offset, sortby, reverse, callback)
{
	var url = "./resultsinline.aspx?filter=" + filter + "&offset=" + offset + "&sortby=" + sortby + "&reverse=" + reverse + "&datasources=" +datasources;

	if (!busy)
	{
		busy = true;
		nextURL = null;
		nextCallBack = null;
		if (hashcache[url] != null)
		{
			callback(hashcache[url]);
			objInfoRetriever = new InfoRetriever(url+"&cached=true", CompleteSyncUpdate);
			return false;
		}
		objInfoRetriever = new InfoRetriever(url, callback);
		return true;
	}
	else
	{
		nextURL = url;
		nextCallBack = callback;
		window.setTimeout("QueueNextAction()", 50);
		return true;
	}
}

function CompleteSyncUpdate(chunk)
{
	busy=false;
}

function QueueNextAction()
{
	if (nextURL == null || nextCallBack == null)
		return;
		
	if (!busy)
	{
		busy = true;
		objInfoRetriever = new InfoRetriever(nextURL, nextCallBack);
		nextURL = null;
		nextCallBack = null;
	}
	else
	{
		//still busy
		window.setTimeout("QueueNextAction()", 50);
	}
}


InfoRetriever.prototype.init = function ()
{
	if (window.ActiveXObject) // branch for IE/Windows ActiveX version
	{
		if (xhttp == null)
		{
			xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
	}
	else if 	(window.XMLHttpRequest) {
		if (xhttp == null)
		{
			xhttp = new XMLHttpRequest();
		}
		
	} 			
}


InfoRetriever.prototype.callInProgress = function () 
{
    switch ( xhttp.readyState ) {
        case 1, 2, 3:
            return true;
        break;
	
        // Case 4 and 0
        default:
            return false;
        break;
    }
}

InfoRetriever.prototype.xhttpLookup = function (url)
{
	
	if (this.callInProgress)
		xhttp.abort();


	if (window.ActiveXObject) // branch for IE/Windows ActiveX version
	{
		if (xhttp == null)
		{
			xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
	
		if (xhttp) 
		{
			xhttp.open("GET", url, true);
			xhttp.onreadystatechange = ReadyStateChange;
			xhttp.send();
		}
	}	 
	else if (window.XMLHttpRequest) 
	{
		if (xhttp == null)
		{
			xhttp = new XMLHttpRequest();
		}

		if (xhttp) 
		{
			xhttp.onreadystatechange = ReadyStateChange;
			xhttp.open("GET", url, true);
			xhttp.send(null);
		}					
	}

}
InfoRetriever.prototype.xhttpPost = function (url)
{	
	if (this.callInProgress)
		xhttp.abort();

	if (window.ActiveXObject)  // branch for IE/Windows ActiveX version
	{
		if (xhttp == null)
			xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
/*	
		if (xhttp) 
		{
			xhttp.open("GET", url, true);
			xhttp.onreadystatechange = ReadyStateChange;
			xhttp.send();
		}
*/		
	}	
	else if (window.XMLHttpRequest) 
	{
		if (xhttp == null)
			xhttp = new XMLHttpRequest();

	}
	
	if (xhttp) 
	{
		xhttp.onreadystatechange = ReadyStateChange;
		xhttp.open("POST", url, true);
		xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhttp.setRequestHeader("Content-length", this.Param.length);
		xhttp.setRequestHeader("Connection", "close");
		xhttp.send(this.Param);			
	}					
}


ReadyStateChange = function ()
{

	//alert('Ready State Changed');
	if ( xhttp.readyState == 4 && xhttp.responseText)
	{								
		try
		{
			objInfoRetriever.Info = xhttp.responseText;
			//hashcache[objInfoRetriever.Url] = objInfoRetriever.Info; //CACHE DISABLED FOR BETA1 : CONFLICTS WITH RESULTS SELECTION
			
			
			
			objInfoRetriever.Callback(objInfoRetriever.Info);
		}
		catch (exception) { ; }
		finally { busy = false; }
	}
	else if (objInfoRetriever && objInfoRetriever.Url.match(/option=ResultsSelectedDocuments/) && 
	    xhttp.readyState == 4 && xhttp.responseText == "" )
	{
		objInfoRetriever.Info = xhttp.responseText;
		// For "Results Selected Documents print" option, show error message if no documents was selected
		objInfoRetriever.Callback(objInfoRetriever.Info);
	}	
	
}


 