var httpReq = null;
var objSelf = null;
var SessionError = "{D225B468-9608-49BA-A091-D4562DAC2E2B}";
var ErrorPageGUID = "{E39DFF3A-9BC8-44C0-AB79-B286E4538B73}";	

function getRequestObject() {
	var rq = null
	if (window.XMLHttpRequest) {
		rq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		rq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return rq;
}

function appendTimeStamp(pURL){ //obliga al browser a no usar cache
	if (pURL.indexOf('?')!=-1)
	  pURL +="&httpReqTimeStamp=" + new Date().getTime()
	else
	  pURL +="?httpReqTimeStamp=" + new Date().getTime();		
	return pURL;
}

function XMLHttpRequestHandler() {
   //atributtes
	this.resultOK=true;//use this to verify session errors
	this.txtResult="";
	this.xmlResult=null;
	
  // Methods
	this.getRequestObject = XMLHttpRequestHandler_getRequestObject;
	this.submit = XMLHttpRequestHandler_submit;
	this.submitPost = XMLHttpRequestHandler_submitPost;	
	this.aSyncSubmit = XMLHttpRequestHandler_aSyncSubmit;	

	return this;
}

function XMLHttpRequestHandler_submit(pURL) {
  pURL = appendTimeStamp(pURL);
	
	this.getRequestObject(pURL);
	this.resultOK=true;
	if ((this.txtResult.indexOf(SessionError)!=-1)||   //verify if session expires.
	    (this.txtResult.indexOf(ErrorPageGUID)!=-1)){   //verify other errors
	  window.location.href=pURL;
	  this.resultOK=false;
	}	
	return this.resultOK;
}

function XMLHttpRequestHandler_submitPost(pURL,pQuery) {  
  request = getRequestObject();
	if (request) {
		request.open("POST", pURL, false);
		request.setRequestHeader("Pragma","no-cache");
		request.setRequestHeader("Cache-control","no-cache");
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");		        
		request.send(pQuery); 
		this.txtResult =request.responseText;
		this.xmlResult =request.responseXML;

        this.resultOK=true;
  	    if ((this.txtResult.indexOf(this.SessionError)!=-1)||   //verify if session expires.
	      (this.txtResult.indexOf(this.ErrorPageGUID)!=-1)){   //verify other errors
 	       window.location.href=pURL;
	       this.resultOK=false;
 	     }	
	     return this.resultOK;
	}
	return false;
}
									
function XMLHttpRequestHandler_aSyncSubmit(pURL,fnContinue){
	pURL = appendTimeStamp(pURL);
	httpReq = getRequestObject();
	objSelf=this;
	if (httpReq) {
		httpReq.open("GET", pURL);
		httpReq.setRequestHeader("Pragma","no-cache");
		httpReq.setRequestHeader("Cache-control","no-cache");
		httpReq.onreadystatechange = function () {
										if (httpReq.readyState == 4) {
											if (httpReq.status == 200) {
												objSelf.txtResult = httpReq.responseText;
												objSelf.xmlResult = httpReq.responseXML;
												fnContinue();
											}
										}					
									}
		httpReq.send();
	}	
	return true;
}

function XMLHttpRequestHandler_getRequestObject(pUrl) {
	var request = getRequestObject();
	if (request != null) {
		request.open("GET", pUrl, false);
			request.send();
			this.txtResult =request.responseText;
			this.xmlResult =request.responseXML;
//		return request.responseXML;	
	}
//	return null;
//	if (window.XMLHttpRequest) { // branch for native XMLHttpRequest object
//		request = new XMLHttpRequest();
//		request.open("GET", pUrl, false);
//		request.send(null);
//		//IE's xml property
////		oSerializer = new XMLSerializer();
////		request.responseXML.xml=oSerializer.serializeToString(request.responseXML, "text/xml");
//		this.txtResult =request.responseText;
//		return request.responseXML;
//	}
//	else if (window.ActiveXObject) {	// else use IE's ActiveXObject to get the XMLHttpRequest object
//		request = new ActiveXObject("Microsoft.XMLHTTP");
//		if (request) {
//			request.open("GET", pUrl, false);
//			request.setrequestheader("Pragma","no-cache");
//			request.setrequestheader("Cache-control","no-cache");
//			request.send();
//				this.txtResult =request.responseText;
//			this.xmlResult =request.responseXML;
//			return request.responseXML;
//		}
//	}
//	return null;
}
								
function asyncSubmit(pURL, fnContinue){
	var httpReq = null;
	if (window.XMLHttpRequest) {
		httpReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		httpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	} else {
		alert("Your browser is not supported.");
		return false;
	}
	
	if (fnContinue) {
		httpReq.onreadystatechange =  function () {
																		if (httpReq.readyState == 4) {
																			if (httpReq.status == 200) {
																				if ( (httpReq.responseText.indexOf(SessionError) != -1) ||   //verify if session expires.
																						 (httpReq.responseText.indexOf(ErrorPageGUID) != -1) ) {   //verify other errors
																					window.location.href = pURL;
																				} else {
																					fnContinue(httpReq);
																				}
																			}
																		}					
																	}
	}	
	httpReq.open("GET", appendTimeStamp(pURL), true);
	httpReq.setRequestHeader("Pragma", "no-cache");
	httpReq.setRequestHeader("Cache-control", "no-cache");
	httpReq.send();
	return true;
}
