///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//using XMLHttpRequest Object
function AJAX()
{	
	this.xmlHttp = null;
	this.lpfnStateChangeCallBack = null;
	this.lpfnPrevSendCallBack = null;
	this.lpfnFinishCallBack = null;
	this.responseText = "";
	this.responseXML = "";
	this.lastError = "";
		
	this.lpfnInProgress = 0;
	AJAX.prototype.nCounter = 0;
	AJAX.prototype.ptrThis = this;
	
	//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, lpfnInProgress)
	{
		this.nCounter = 0;
		
		if(!this.xmlHttp)
			return false;
		
		if(this.xmlHttp.readyState!=0 && this.xmlHttp.readyState!=4)//not initialized or busy
			return false;
		if(this.xmlHttp.readyState!=4)
			this.xmlHttp.abort();
	
		this.lpfnStateChangeCallBack = lpfnStateChangeCallBack;
		this.lpfnPrevSendCallBack = lpfnPrevSendCallBack;
		this.lpfnFinishCallBack = lpfnFinishCallBack;		
		this.lpfnInProgress = lpfnInProgress;
		
		if(this.lpfnPrevSendCallBack)
		{
			this.lpfnPrevSendCallBack();
		}
			
		try
		{
			//start timer proc
			setTimeout("AJAX.prototype._lpfninprogress()", 0);
			//open
			this.xmlHttp.open(method, url);
			if(method.toLowerCase()=="post")
				this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.xmlHttp.onreadystatechange = lpfnStateChangeCallBack ? 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 send
	
	//timer function
	AJAX.prototype._lpfninprogress = function()
	{
		pthis = AJAX.prototype.ptrThis;
		++pthis.nCounter
		
		if(pthis.xmlHttp.readyState==4 || pthis.nCounter>60)
		{
			if(pthis.nCounter>60){
				alert("Sorry, server not responding. Press F5 to reload this page.");
				document.location = document.location.href;
			}
			pthis.nCounter = 0;
			return;
		}
		
		if(pthis.lpfnInProgress)
			pthis.lpfnInProgress(pthis.nCounter);
		setTimeout("AJAX.prototype._lpfninprogress()", 1000)
	}//end timer
	
	//initialize AJAX object
	this._initialize();
}//end class

//onreadystatechange callback
function onReadyStateChangeRoutine()
{		
	if(!Ajax.xmlHttp)
	{
		return;
	}
	if(Ajax.xmlHttp.readyState == 4)
	{
		try
		{	
			if(Ajax.xmlHttp.status == 200)
			{			
				Ajax.responseText = getResponseText(Ajax.xmlHttp.responseText);
				Ajax.responseXML = Ajax.xmlHttp.responseXML;
				var ret = Ajax.responseText;
				if(ret.indexOf("-ERROR_CONNECT")==0)
				{
					alert("Server is too busy. Please try later. Thanks!"); 
					document.location = "";
					return;
				}
				
				if(Ajax.lpfnFinishCallBack)
				{
					Ajax.lpfnFinishCallBack();
				}
			}
		}catch(e)
		{
			Ajax.lastError = e.message;
		}		
	}
}//end onReadyStateChange

function getResponseText(str)
{
	var len1 = String("<response>").length;
	var len2 = String("</response>").length;
	
	var istart = str.indexOf("<response>");
	var iend = str.indexOf("</response>");
	str = str.substr(istart + len1, iend - istart - len1);
	return str;
	//return str.replace(/&amp;/g, "&");
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//create and initialize GLOBAL Ajax object
var Ajax = new AJAX();
//End Initializing
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////