// ##################DECLARACIONES######################
// #####################################################


//Declaraciones
var ajax;

miRetorno= function() {
	this.span="";
	this.tagxml="";
}
	
miEnvio = function() {
	this.idrequest="";
	this.idfield="";
	this.valor="";
}

miRetornoTag = function(pSpan, pTagxml) {
	this.span = pSpan;
	this.tagxml = pTagxml;
}

miEnvioId = function(pRequest) {
	this.idrequest= pRequest;
	this.idfield= "";
	this.valor= "";
}

miEnvioFld = function(pRequest, pField) {
	this.idrequest= pRequest;
	this.idfield= pField;
	this.valor= "";
}

miEnvioVal = function(pRequest, pValor) {
	this.idrequest= pRequest;
	this.idfield= "";
	this.valor= pValor;
}

var REQ_ENVIO = 0;

var RES_CALLBACK = 0; //Client CallBack(AjaxClass) function proceses the response and does all the job.

// #####################################################
// ################## OBJECT ###########################
// #####################################################

miAjax = function(pURLBase, pCallBack) {
	this.URLBase = pURLBase;
	this.CallBack = pCallBack;
	this.ajax = this.NuevoAjax();
	this.isAsynchronize = false;
	
	this.ReqType = REQ_ENVIO;
	this.ReqEnvios = new Array();
				
	this.ResType = RES_CALLBACK;
	this.ResRetornos = new Array();
}

// Constructores
miAjax.prototype.NuevoAjax = function()
{
	try {
		http_request = new XMLHttpRequest();
	}
	catch (e) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				http_request = false;
				alert("Your Browser does not work with Web 2.0!\nPlease Update");
			}
		}
	}
	return http_request;

};

// -----------------------------------------------------
// Propiedades
// -----------------------------------------------------
miAjax.prototype.addEnvio = function(pmiEnvio) {
	this.ReqEnvios[this.ReqEnvios.length]=pmiEnvio;
	return this.ReqEnvios.length;
}

miAjax.prototype.removeEnvio = function(pmiEnvio) {
	var liRemove = -1;
	for (var i=0;i<this.ReqEnvios.length;i++) {
		if (this.ReqEnvios[i].idrequest == pmiEnvio.idrequest) {
		  liRemove = i;
		  break;
		}
	}
	if (liRemove != -1) {
		this.ReqEnvios.splice(liRemove,1);
	}
}


miAjax.prototype.addRetorno = function(pmiRetorno) {
	this.ResRetornos[this.ResRetornos.length]=pmiRetorno;
	return this.ResRetornos.length;
}

miAjax.prototype.setReqType = function (pReqType) {
	this.ReqType = pReqType;
}

miAjax.prototype.setResType = function (pResType) {
	this.ResType = pResType;
}

miAjax.prototype.setisAsynchronize = function (pisAsynchronize) {
	this.isAsynchronize = pisAsynchronize;
}

// -----------------------------------------------------
// Metodos Publicos
// 
// -----------------------------------------------------
miAjax.prototype.Realizar_peticion = function() {

		this.ajax.open("post", this.URLBase, this.isAsynchronize);
		if (this.isAsynchronize) 
				{   var lmiAjax = this;
					this.ajax.onreadystatechange = function() {
							if (lmiAjax.ajax.readyState == 4) {
									ResponseProcess(lmiAjax);
							}
						}
				}
		this.ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.ajax.send(this.MontarEnvio());
		if (!(this.isAsynchronize)) ResponseProcess(this);
		
	}	


miAjax.prototype.MontarEnvio = function() {
		var salida=new Array()
		for (var i=0;i<this.ReqEnvios.length;i++) {
      if (salida.length>0) salida[salida.length]="&"
			salida[salida.length]=this.ReqEnvios[i].idrequest + "="
			if (this.ReqEnvios[i].valor=="") {
				salida[salida.length]=escape(document.getElementById(this.ReqEnvios[i].idfield).value);
			} else {
				salida[salida.length]=escape(this.ReqEnvios[i].valor);	
			}
		}
		return salida.join("");
}


// -----------------------------------------------------
// Metodos Privados
// -----------------------------------------------------

function ResponseProcess(pmiAjax) {

		switch(pmiAjax.ResType) {
			case(RES_CALLBACK) :
				eval(pmiAjax.CallBack + '(pmiAjax.ajax)');
				break;
		}

}

