if (typeof(__ess_m_aNamespaces) == 'undefined')	//include in each ESS ClientAPI namespace file for dependency loading
	var __ess_m_aNamespaces = new Array();

function __ess_getParser()
{
	if (ess_xmlhttp.JsXmlHttpRequest != null)
		return 'JS';
	if (ess.dom.browser.isType(ess.dom.browser.InternetExplorer))
		return 'ActiveX'; //'ActiveX';
	else if (typeof(XMLHttpRequest) != "undefined") //(ess.dom.browser.isType(ess.dom.browser.Netscape) || ess.dom.browser.isType(ess.dom.browser.Mozilla)) //(typeof XMLHttpRequest != "undefined");
		return 'Native'; //'Native';
	else
		return 'JS';
	
}

function __ess_cleanupxmlhttp()
{
	for (var i=0; i<ess.xmlhttp.requests.length;i++)
	{
		if (ess.xmlhttp.requests[i] != null)
		{
			if (ess.xmlhttp.requests[i].completed)
			{
				ess.xmlhttp.requests[i].dispose();
				if (ess.xmlhttp.requests.length == 1)
				    ess.xmlhttp.requests = new Array();
				else
				    ess.xmlhttp.requests.splice(i,i);
			}
		}
	}
	//window.status = ess.xmlhttp.requests.length + ' ' + new Date();
}

//ess.xmlhttp Namespace ---------------------------------------------------------------------------------------------------------
function ess_xmlhttp()
{
	this.pns = 'ess';
	this.ns = 'xmlhttp';
	this.dependencies = 'ess,ess.dom'.split(',');
	this.isLoaded = false;
	this.parserName = null;
	this.contextId = 0;
	this.requests = new Array();
	this.cleanUpTimer = null;
}

ess_xmlhttp.prototype.init = function ()
{
	this.parserName = __ess_getParser();
}

ess_xmlhttp.prototype.doCallBack = function(sControlId, sArg, pSuccessFunc, sContext, pFailureFunc, pStatusFunc, bAsync, sPostChildrenId, iType)
{
	var oReq = ess.xmlhttp.createRequestObject();
	var sURL = document.location.href;
	oReq.successFunc = pSuccessFunc;
	oReq.failureFunc = pFailureFunc;
	oReq.statusFunc = pStatusFunc;
	oReq.context = sContext;
	if (bAsync == null)
		bAsync = true;
	//ljj 2007-3-15 allow all kinds of suffix
	if (sURL.indexOf(".htm") !=-1)
		sURL = sURL.substring(0,sURL.lastIndexOf('/')+1) + 'default.aspx';
	else if (sURL.lastIndexOf('/') == sURL.length-1)	//fix this for url's that dont have page name in them...  quickfix for now...
		sURL += 'default.aspx';
	
	//if (sURL.indexOf('.aspx') == -1)	//fix this for url's that dont have page name in them...  quickfix for now...
	//	sURL += 'default.aspx';
	if(sURL.indexOf('#')!=-1){
	  sURL = sURL.substring(0,sURL.indexOf('#'));
	}
	if (sURL.indexOf('?') == -1)
		sURL += '?';
	else
		sURL += '&';
	
	//sURL += '__ESSCAPISCI=' + sControlId + '&__ESSCAPISCP=' + encodeURIComponent(sArg);
	
	oReq.open('POST', sURL, bAsync);
	//oReq.send();
	
	sArg = ess.encode(sArg);
		
	if (sPostChildrenId)
		sArg += '&' + ess.dom.getFormPostString($(sPostChildrenId));

	if (iType != 0)
		sArg += '&__ESSCAPISCT=' + iType;
		
	oReq.send('__ESSCAPISCI=' + sControlId + '&__ESSCAPISCP=' + sArg);

	return oReq; //1.3
}

ess_xmlhttp.prototype.createRequestObject = function()
{
	if (this.parserName == 'ActiveX')
	{
		var o = new ActiveXObject('Microsoft.XMLHTTP');
		ess.xmlhttp.requests[ess.xmlhttp.requests.length] = new ess.xmlhttp.XmlHttpRequest(o);
		return ess.xmlhttp.requests[ess.xmlhttp.requests.length-1]; 
	}
	else if (this.parserName == 'Native')
	{
		return new ess.xmlhttp.XmlHttpRequest(new XMLHttpRequest()); 
	}
	else
	{
		var oReq = new ess.xmlhttp.XmlHttpRequest(new ess.xmlhttp.JsXmlHttpRequest());
		ess.xmlhttp.requests[oReq._request.contextId] = oReq;
		return oReq; 
	}	
}

//ess.xmlhttp.XmlHttpRequest Object ---------------------------------------------------------------------------------------------------------
ess_xmlhttp.prototype.XmlHttpRequest = function(o)
{
	this._request = o;
	this.successFunc = null;
	this.failureFunc = null;
	this.statusFunc = null;
	//this._request.onreadystatechange = ess.dom.getObjMethRef(this, 'readyStateChange');
	this._request.onreadystatechange = ess.dom.getObjMethRef(this, 'onreadystatechange');
	this.context = null;
	this.completed = false;
	//this.childNodes = this._doc.childNodes;
}

ess_xmlhttp.prototype.XmlHttpRequest.prototype.dispose = function ()
{
	if (this._request != null)
	{
		this._request.onreadystatechange = new function() {};//stop IE memory leak.  Not sure why can't set to null;
		this._request.abort();
		this._request = null;
		this.successFunc = null;
		this.failureFunc = null;
		this.statusFunc = null;
		this.context = null;
		this.completed = null;
		this.postData = null;	//1.3
	}
}

ess_xmlhttp.prototype.XmlHttpRequest.prototype.open = function (sMethod, sURL, bAsync)
{
	this._request.open(sMethod, sURL, bAsync);
	if (typeof(this._request.setRequestHeader) != 'undefined')
		this._request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
	return true;
} 

ess_xmlhttp.prototype.XmlHttpRequest.prototype.send = function (postData)
{
	//this._request.onreadystatechange = this.complete;
	this.postData = postData;
	if (ess.xmlhttp.parserName == 'ActiveX')	
		this._request.send(postData);
	else
		this._request.send(postData);
	return true;
}

ess_xmlhttp.prototype.XmlHttpRequest.prototype.onreadystatechange = function ()
{
	if (this.statusFunc != null)
		this.statusFunc(this._request.readyState, this.context, this); //1.3
		
	if (this._request.readyState == '4')
	{
		this.complete(this._request.responseText);
		if (ess.xmlhttp.parserName == 'ActiveX')
			window.setTimeout(__ess_cleanupxmlhttp, 1);	//cleanup xmlhttp object
	}
}

ess_xmlhttp.prototype.XmlHttpRequest.prototype.complete = function (sRes)
{
	var sStatusCode = this.getResponseHeader('__ESSCAPISCSI');
	this.completed=true;

	if (sStatusCode == '200')	
		this.successFunc(sRes, this.context, this);	//1.3
	else
	{
		var sStatusDesc = this.getResponseHeader('__ESSCAPISCSDI');
		if (this.failureFunc != null)
			this.failureFunc(sStatusCode + ' - ' + sStatusDesc, this.context, this); //1.3
		else
			alert(sStatusCode + ' - ' + sStatusDesc);
	}
}

ess_xmlhttp.prototype.XmlHttpRequest.prototype.getResponseHeader = function (sKey)
{
	return this._request.getResponseHeader(sKey);
}


ess_xmlhttp.prototype.dependenciesLoaded = function()
{
	return (typeof(ess) != 'undefined' && typeof(ess.dom) != 'undefined');
}

ess_xmlhttp.prototype.loadNamespace = function ()
{
	if (this.isLoaded == false)
	{
		if (this.dependenciesLoaded())
		{
			ess.xmlhttp = this; 
			this.isLoaded = true;
			ess.loadDependencies(this.pns, this.ns);
			ess.xmlhttp.init();
		}
	}	
}

__ess_m_aNamespaces[__ess_m_aNamespaces.length] = new ess_xmlhttp();

for (var i=__ess_m_aNamespaces.length-1; i>=0; i--)
	__ess_m_aNamespaces[i].loadNamespace();
