/*
	HttpAsyncPostRequest - JavaScript Code
	Copyright (c) 2006, X Inc. All rights reserved.
	Version 1.0.0
*/


function HttpAsyncPostRequest(sRequestId)
{
	this._XHR = null;
	this._CallbackFnc = null;

	
	this.Send = function(RequestUrl, RequestParams, CallbackFunction)
	{
		if(this._XHR) return;

		if(!(typeof(CallbackFunction) == 'function' || typeof(eval(CallbackFunctionName)) == 'function'))
		{
			alert('Callback function passed as second parameter is missing. Text loading is stopped.');
			return;
		}

		this._CallbackFnc = typeof(CallbackFunction) == 'function' ? CallbackFunction : eval(CallbackFunction);
		this._XHR = null;
		
		RequestUrl += (RequestUrl.indexOf('?') < 0 ? '?' : '&') + 'HttpAsyncPostRequestCacheBuster=' + Math.random();
		
		if(window.XMLHttpRequest)
		{
			this._XHR = new XMLHttpRequest();

			if (this._XHR.overrideMimeType)
			{
				this._XHR.overrideMimeType('text/html');
			}
		}
		else if(window.ActiveXObject)
		{
			try
			{
				this._XHR = new ActiveXObject('Msxml2.XMLHTTP');
			}
			catch(e)
			{
				try
				{
					this._XHR = new ActiveXObject('Microsoft.XMLHTTP');
				}
				catch(e){}
			}
		}
		else
		{
			this._CallbackFnc
			(
				null,
				'XML_HTTP_REQEST_NOT_SUPPORTED',
				'Your browser does not support XMLHTTP or XMLHttpRequest. Use at least MSIE 5.0, Firefox 1.0, Opera 7.6'
			);

			return;
		}

		if(this._XHR)
		{
			var vP = [], sRequestBody;

			for(var p in RequestParams)
			{
				vP[vP.length] = escape(p) + '=' + escape(RequestParams[p]);
			}

			sRequestBody = vP.join('&');

			this._XHR.onreadystatechange = this._OnReadyStateChange;
			this._XHR.open('POST', RequestUrl, true);
			this._XHR.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			this._XHR.setRequestHeader('Content-length', sRequestBody.length);
			this._XHR.setRequestHeader('Connection', 'close');
			this._XHR.send(sRequestBody);
		}
	}


	this._OnReadyStateChange = function()
	{
		var Obj = window['HttpAsyncPostRequest'+sRequestId+'WinObj']
		
		if(Obj._XHR && Obj._XHR.readyState == 4)
		{
			if(Obj._XHR.status == 200)
			{
				Obj._CallbackFnc(Obj._XHR.responseText, null, null);
			}
			else
			{
				Obj._CallbackFnc(null, 'ERROR_LOADING_TEXT_FROM_SERVER', '(' + Obj._XHR.status + ') ' + Obj._XHR.statusText);
			}

			Obj._XHR = null;
		}
	}



	/*
		Control init code
	*/

	if(!document.getElementById)return;


	window['HttpAsyncPostRequest'+sRequestId+'WinObj'] = this;
}

