document.write('<script language="javascript" src="' + '/Libs/JS/funcsEncode.js"></script>');

var req = null;

var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

function sendRequest(url,params,HttpMethod){
	if (!HttpMethod)
		HttpMethod="GET";
	req = initXMLHTTPRequest();
	if (req){
		req.onreadystatechange=onReadyState;
		req.open(HttpMethod,url,true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(params);
	}
}
function initXMLHTTPRequest(){
	var xRequest=null;
	try {
		xRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			xRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			try {
				xRequest = new XMLHttpRequest();
			} catch(e) {
				xRequest = null;
			}
		}
	}
	return xRequest;
}
function sendRequest2(url,fAtiendeRta,params,HttpMethod){
	if (!HttpMethod)
		HttpMethod="GET";
	req=initXMLHTTPRequest();
	if (req){
		req.onreadystatechange=fAtiendeRta;
		req.open(HttpMethod,url,true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(params);
	}
}

// Funciones que utilizan el objeto net.ContentLoader ******* /

function callUrl (url, params, method, proc_rta) { //Agrega a cualquier peticion una variable para evitar la cache de los browsers
	params += ((params == '') ? "nocache=" : "&nocache=") + Math.random();
	
	if (method == "GET")
		new net.ContentLoader(url + '?' + params, proc_rta, null);
	else
		new net.ContentLoader(url, proc_rta, null, true, params)
}
function sendRequestForm(url, fAtiendeRta, form) { // Manda todo un formulario por método POST.
	var params = "", name_value = "", i;	
	for (i = 0; i < form.length; i++) { //Recorre todo el form y crea la cadena de parámetros
		if ((form.elements[i].type != 'radio' && form.elements[i].type != 'button') || (form.elements[i].type == 'radio' && form.elements[i].checked == true)) {
			name_value = this.URLEncode(form.elements[i].name) + "=" + this.URLEncode(form.elements[i].value);
			params += (params == '') ? name_value : '&' + name_value;
		}
	}
	callUrl(url, params, "POST", fAtiendeRta);
}
function sendRequestGet (url, params, fAtiendeRta) {
	callUrl(url, params, "GET", fAtiendeRta);
}

// OBJETO net.ContentLoader ************************************************************/
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;

net.ContentLoader=function(url, onload, onerror, mostrarError, params) { // Si se pasan valores por params, la petición se realizará por método POST.
	if (mostrarError == undefined)
		mostrarError = true;
	this.url = url;
	this.params = params;
	this.req = null;
	this.onload = onload;
	this.onerror = (onerror) ? onerror : this.defaultError;
	this.mostrarErrorConexion = mostrarError;
	
	if (typeof params == "undefined")
		this.loadXMLDoc(url);
	else
		this.postToUrl(url, params);
}
net.ContentLoader.prototype= {
	loadXMLDoc:function(url) {
		this.req=initXMLHTTPRequest();
		if (this.req) {
			try {
				var loader=this;
				this.req.onreadystatechange=function() {
					loader.onReadyState.call(loader);
				}
				this.req.open('GET',url,true);
				this.req.send(null);
			} catch (err) {
				this.onerror.call(this);
			}
		}
	},
	postToUrl:function(url, params) {
		this.req=initXMLHTTPRequest();
		if (this.req) {
			try {
				var loader = this;
				this.req.onreadystatechange=function() {
					loader.onReadyState.call(loader);
				}
				this.req.open("POST", url, true);
				this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.req.send(params);
			} catch (err) {
				this.onerror.call(this);
			}
		}
	},
	onReadyState:function(){
		var req=this.req;
		var ready=req.readyState;
		if (ready==net.READY_STATE_COMPLETE){
			var httpStatus=req.status;
			if (httpStatus==200 || httpStatus==0){
				this.onload.call(this);
			}else{
				this.onerror.call(this);
			}
		}
	},
	defaultError:function(){
		if (URLEncode)	{
			var urlError =  "/Libs/JS/Ajax/errorHandle/logError.asp?readyState=" + this.req.readyState + "&status=" + this.req.status + "&url=" + this.url + "&responseText=" + URLEncode(this.req.responseText);
			reqLogError = initXMLHTTPRequest();
			if (reqLogError){
				reqLogError.open("GET",urlError,true);
				reqLogError.send(null);
			}
		}
		if (this.mostrarErrorConexion)
			alert("Ha ocurrido un error al consultar los datos. \n\nPor favor verifique su conexión a Internet y vuelva a intentar.");
		if (debug=='S')	{
			alert("error fetching data!"
			+"\n\nreadyState:"+this.req.readyState
			+"\nstatus: "+this.req.status + "statusText: "+ this.req.statusText
			+"\nurl: "+this.url
			+"\nheaders: "+this.req.getAllResponseHeaders()
			+"\n" + this.req.responseText); 
		}
	}
}