LhqSteelhand.web.sys.ajax = (function() {

	function AjaxObj(lhqStagehandCommand,callbackFunction) {
		/*//////////////////////////////////////////////////////////////////////////////////////
		// private variables
		//////////////////////////////////////////////////////////////////////////////////////*/
		this.update = false;
		this.urlCall = LhqSteelhand.web.config.ajax.url;
		this.reqMethod = LhqSteelhand.web.config.ajax.method;
		this.responseType = 'text';
		this.lhqStagehandCmd = lhqStagehandCommand || '';
		this.params = new Array();
		this.callback = callbackFunction || function () { };
		this.AJAX=null;
		that = new Object;
 		that = this;
		/*//////////////////////////////////////////////////////////////////////////////////////
		// private functions
		//////////////////////////////////////////////////////////////////////////////////////*/
		this._createXmlHttpRequestObject = function() {
			try {
				myXmlHttpRequestObj = new XMLHttpRequest();
			}
			catch(ERROR) {
				try {
					myXmlHttpRequestObj = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(ERROR) {
					try {
						myXmlHttpRequestObj = new ActiveXObject("MSXML2.XMLHTTP");
					}
					catch(ERROR) {
						alert("ERROR: Can't create XMLHttpRequest object!");
					}
				}
			}
			return myXmlHttpRequestObj;
		};
		
		this._getReqData = function() {
			myReqData='lhqStagehandCmd='+this.lhqStagehandCmd;
			cnt=0;
			for (var param in this.params) {
				myReqData = myReqData + '&' +encodeURIComponent(param)+'='+encodeURIComponent(this.params[param]);
				cnt++;
			}
			return myReqData;
		};
	}
	
	AjaxObj.prototype.setCommand = function(myCmd) {
		that.lhqStagehandCmd = myCmd;
	};
	
	AjaxObj.prototype.addParam = function(myKey,myValue) {
		that.params[myKey] = myValue;
	};
	
	AjaxObj.prototype.setResponseType = function(myRespType) {
		if(myRespType.toLowerCase() == 'text' || myRespType.toLowerCase() == 'xml') {
			that.responseType=myRespType;
		}
	};
	
	AjaxObj.prototype.getResponseType = function() {
		return that.responseType;
	};
	
	AjaxObj.prototype.trigger = function() { 
		if (that.updating) { return false; }
		myAJAX = that._createXmlHttpRequestObject();
		myCallback = that.callback;
		if (myAJAX == null) {
			return false;
		} else {
			myAJAX.onreadystatechange = function() {
				if (myAJAX != null && myAJAX.readyState == 4) {
					that.updating=false;
					myCallback(myAJAX.responseText,myAJAX.status,myAJAX.responseXML);
					myAJAX=null;
				}
			}
	//		that.updating = new Date();
			if (/post/i.test(that.reqMethod)) {
	// 			var uri=that.urlCall+'?'+that.updating.getTime();
				myAJAX.open("POST", that.urlCall, true);
				myAJAX.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
				myAJAX.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
				passData = that._getReqData();
				myAJAX.setRequestHeader("Content-Length", passData.length);
				myAJAX.send(passData);
			} else {
	// 			var uri=that.urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
				var uri=that.urlCall+'?'+passData; 
				myAJAX.open("GET", uri, true);
				myAJAX.send(null);
			}
			return true;
		}
	};

	
	return {
/*//////////////////////////////////////////////////////////////////////////////////////
// public variables
//////////////////////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////////////////////
// public functions
//////////////////////////////////////////////////////////////////////////////////////*/
		AjaxObj : AjaxObj
	};
})();
