/*
	js class for XMLHttpRequest object
	Nov 2006 Yeah You
*/

/*
	oXHR.init(params) -> works
	var oXHR = new XHR(params) -> doesn't work
*/

XHR = function(){};
/*
	ACE doesn't have .prototype.
	but we can't remove it here, why?
*/

XHR.prototype.setURL = function(sURL){
	this._sURL = sURL;
};
XHR.prototype.setMethod = function(sMethod){
	this._sMethod = sMethod;
};
XHR.prototype.setParam = function(sParamName, sParamValue){
	if(!this._sParams){
		this._sParams = ""; //w/o this, operation will fail
	}
	if(this._sParams.length>0){
		this._sParams += "&";
	}
	this._sParams = this._sParams + encodeURIComponent(sParamName) + "=" + encodeURIComponent(sParamValue);
};
XHR.prototype.setResponseType = function(sResponseType){
	this._sResponseType = sResponseType;
};
XHR.prototype.setCallback = function(fnCallback){
	this._fnCallback = fnCallback;
}

XHR.prototype._createXMLHttpRequest = function(){
	if (window.ActiveXObject) {
		this._oRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}else if (window.XMLHttpRequest) {
		this._oRequest = new XMLHttpRequest();
	}
};

XHR.prototype.startRequest = function(){
	this._createXMLHttpRequest();
	var ptr_oRequest = this._oRequest;
	var ptr_sResponseType = this._sResponseType;
	var ptr_fnCallback = this._fnCallback;
	
	this._oRequest.onreadystatechange = function(){
		if(ptr_oRequest.readyState < 4){
			//$( statusId ).innerHTML = "<b style='background: red; color: white;'>Loading ... </b>";
		}
		if(ptr_oRequest.readyState == 4){
			if(ptr_oRequest.status == 200){
				switch(ptr_sResponseType){
					case XHR.defsResponseType.responseText: ptr_fnCallback(ptr_oRequest.responseText); break;
					case XHR.defsResponseType.responseXML: ptr_fnCallback(ptr_oRequest.responseXML); break;
				}
			}
		}
	};
	
	switch(this._sMethod){
		case XHR.defsMethod.POST:
			this._oRequest.open(this._sMethod, this._sURL, true);
			this._oRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this._oRequest.send(this._sParams);
			break;
		case XHR.defsMethod.GET:
			this._oRequest.open(this._sMethod, this._sURL + "?" + this._sParams, true);
			this._oRequest.send(null);
			break;
	}
};

XHR.defsResponseType =
{
	responseText: "responseText",
	responseXML: "responseXML"
};

XHR.defsReadyState =
{
	Uninitialized: 0,
	Loading: 1,
	Loaded: 2,
	Interactive: 3,
	Complete: 4
};

XHR.defsStatus =
{
	OK: 200
};

XHR.defsMethod =
{
	GET: "GET",
	POST: "POST", 
	HEAD: "HEAD"
};



