
var g_dispatcher=null;

function getdisp()
{
	if (top.g_dispatcher == null)
		top.g_dispatcher = new Dispatcher();
	return top.g_dispatcher;
}

function ResponseMgr(respText)
{
	this.respText = respText;
}

ResponseMgr.prototype.getText = function ()
{
	return this.respText;
}

ResponseMgr.prototype.parse = function()
{
	var paramArray=this.respText.split("&");
	this.nameValue = new Array();
	for (var i=0; i < paramArray.length; i++) {
		var nvArray = paramArray[i].split("=");
		this.nameValue[nvArray[0]] = nvArray[1];
	}
}

ResponseMgr.prototype.getParam =  function(name)
{
	return this.nameValue[name];
}

ResponseMgr.prototype.respText=null;
ResponseMgr.prototype.nameValue=null;


function Dispatcher(m){
	this.mode = m;
}
Dispatcher.prototype.mode = null;
Dispatcher.prototype.mostRecentQueryParams = null;
Dispatcher.prototype.mostRecentQueryAPI = null;
Dispatcher.prototype.ptrFn = null;

Dispatcher.prototype.objXmlHttp=null;	

Dispatcher.prototype.deleteInstance=function()
{
	delete this.objXmlHttp;
	this.objXmlHttp = null;
	if(this.mostRecentQueryAPI != null)
	{
		this.Invoke(this.mostRecentQueryAPI,this.mostRecentQueryParams,this.ptrFn);
	}
	this.mostRecentQueryAPI=null;
}

Dispatcher.prototype.Invoke = function ( urlAPI, arrParams, ptrFn ,override)
{
	if (this.objXmlHttp != null) 
	{
		if(override)
			var check=this.objXmlHttp.readyState!=0?1:0;
		else
			var check=this.mode==0?1:0;
		if(check==1)
		{
			try	{
				this.objXmlHttp.abort() ;
			}
			catch(e) {
			}
			this.mostRecentQueryAPI=null;
			delete this.objXmlHttp;
		}
		else
		{
			this.mostRecentQueryParams=arrParams;
			this.mostRecentQueryAPI=urlAPI;
			this.ptrFn = ptrFn;
			return;		
		}
			
	}
	
	this.objXmlHttp = GetXmlHttp() ;
	

	this.objXmlHttp.open( "post", urlAPI, true ) ;

	var objXHttp = this.objXmlHttp;
	var currDisp = this;
	var t1 = null;
	var win = window;
	this.objXmlHttp.onreadystatechange = function ()
	{
		if ( objXHttp.readyState == 4 )
		{
			var t;
			var objResponse = new ResponseMgr((t=objXHttp.responseText));
			var t2 = getTimeStamp();
			//win.alert("Time For Result data to Return: " + (t2-t1));
			if(ptrFn)
				ptrFn( objResponse ) ;
			currDisp.deleteInstance();

		}
	}
	
	var strParams = "" ;
	var strPrefix = "" ;
	for ( strParamName in arrParams )
	{
		strParams += ( strPrefix + escape( strParamName ) + "=" + escape( arrParams[ strParamName ] ) ) ;
		strPrefix = "&" ;
	}
	t1=getTimeStamp();
	this.objXmlHttp.send( strParams ) ;
}
