///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//using XMLHttpRequest Object
function AJAX()
{	
	this.xmlHttp = null;
	this.lpfnStateChangeCallBack = null;
	this.lpfnPrevSendCallBack = null;
	this.lpfnFinishCallBack = null;
	this.responseText = "";
	this.responseXML = "";
	this.lastError = "";
	
	//callback params
	this.callbackParams = null;
	
	//initialize xmlHttp object
	this.Initialize = function()
	{		
		try
		{
			this.xmlHttp = new XMLHttpRequest();
		}
		catch(e)
		{
			try{
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");	//require IE6.0 or newer
			}catch(e)
			{
				this.lastError = e.message;
			}			
		}
		return this.xmlHttp!=null;
	}
	
	//get readystate
	this.getReadyState = function()
	{
		if(!this.xmlHttp)
			return -1;
		return this.xmlHttp.readyState;
	}
	
	//get status
	this.getStatusCode = function()
	{
		if(!this.xmlHttp)
			return -1;
		if(this.xmlHttp.readyState==4)
			return this.xmlHttp.status;
		return -1;
	}
	
	//send request
	this.send = function(method, url, params, lpfnStateChangeCallBack, lpfnPrevSendCallBack, lpfnFinishCallBack, callbackParams)
	{
		if(!this.xmlHttp)
			return false;
		
		if(this.xmlHttp.readyState!=0 && this.xmlHttp.readyState!=4)	//not initialized or busy
			return false;
	
		this.lpfnStateChangeCallBack = lpfnStateChangeCallBack;
		this.lpfnPrevSendCallBack = lpfnPrevSendCallBack;
		this.lpfnFinishCallBack = lpfnFinishCallBack;
		this.callbackParams = callbackParams;
		
		if(this.lpfnPrevSendCallBack)
		{
			this.lpfnPrevSendCallBack();
		}
			
		try
		{
			this.xmlHttp.open(method, url);
			if(method.toLowerCase()=="post")
				this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.xmlHttp.onreadystatechange = lpfnStateChangeCallBack!=null ? lpfnStateChangeCallBack : onReadyStateChangeRoutine;
			this.xmlHttp.send(params);
		}
		catch(e)
		{
			this.lastError = e.message;
			alert("Error connect to server. System message: " + e.message);
			if(lpfnFinishCallBack)
				lpfnFinishCallBack();
			
		}
	}
}//end class

//onreadystatechange callback
function onReadyStateChangeRoutine()
{		
	if(!Ajax.xmlHttp)
	{
		alert("not initialized");
		return;
	}
	if(Ajax.xmlHttp.readyState == 4)
	{
		if(Ajax.xmlHttp.status == 200)
		{
			try
			{
				Ajax.responseText = getResponseText(Ajax.xmlHttp.responseText);
				Ajax.responseXML = Ajax.xmlHttp.responseXML;
				if(Ajax.lpfnFinishCallBack)
				{
					Ajax.lpfnFinishCallBack(Ajax.callbackParams);
				}
			}
			catch(e)
			{
				Ajax.lastError = e.message;
				alert(e.message);
			}
		}		
	}
}//end onReadyStateChange

function getResponseText(str)
{
	var i = str.indexOf("<response>");
	str = str.substring(i + String("<response>").length, str.length - String("</response").length-1);	
	return str.replace(/&amp;/g, "&");
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Initializing GLOBAL Ajax object
var Ajax = new AJAX();
Ajax.Initialize();
//End Initializing
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

