function AJAX(){
	this.xmlHttp = null;
	this.lpfnStateChangeCallBack = null;
	this.lpfnPrevSendCallBack = null;
	this.lpfnFinishCallBack = null;
	this.responseText = "";
	this.responseXML = "";
	this.lastError = "";
	AJAX.prototype.nCounter = 0;
	AJAX.prototype.ptrThis = this;
	
	/*callback params*/
	this.callbackParams = null;
	
	this._initialize = function(){
		try{
			this.xmlHttp = new XMLHttpRequest();
		}
		catch(e){
			try{
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				this.lastError = e.message;
			}			
		}
		return this.xmlHttp!=null;
	};
	
	this.getReadyState = function(){
		if(!this.xmlHttp) return -1;
		return this.xmlHttp.readyState;
	};
	
	this.getStatusCode = function(){
		if(!this.xmlHttp) return -1;
		if(this.xmlHttp.readyState==4) return this.xmlHttp.status;
		return -1;
	};
	
	this.send = function(method, url, params, lpfnStateChangeCallBack, lpfnPrevSendCallBack, lpfnFinishCallBack){
		this.nCounter = 0;		
		if(!this.xmlHttp) return false;
		
		if(this.xmlHttp.readyState!=0 && this.xmlHttp.readyState!=4) return false;
	
		this.lpfnStateChangeCallBack = lpfnStateChangeCallBack;
		this.lpfnPrevSendCallBack = lpfnPrevSendCallBack;
		this.lpfnFinishCallBack = lpfnFinishCallBack;		
		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 ? lpfnStateChangeCallBack : onReadyStateChangeRoutine;			
			this.xmlHttp.send(params);
		}
		catch(e){
			this.lastError = e.message;			
			if(lpfnFinishCallBack) lpfnFinishCallBack();
		}
	};	
	this._initialize();
}

function onReadyStateChangeRoutine(){
	if(!Ajax.xmlHttp) return;
	if(Ajax.xmlHttp.readyState == 4){
		if(Ajax.xmlHttp.status == 200){
			try{
				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!"); 
					document.location = "";
					return;
				}
				
				if(Ajax.lpfnFinishCallBack){
					Ajax.lpfnFinishCallBack();
				}
			}
			catch(e){
				Ajax.lastError = e.message;
			}
		}		
	}
}

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;
}
var Ajax = new AJAX();