/*
*Clase Ajax creada por Gragel
*
*Puede ser utilizada, pasada, modificada pero no olvides mantener
*el espiritu del software libre y respeta GNU-GPL
*/
var clsAjx = Class.create();

clsAjx.prototype = {

	/**********************************************************************************************
	* Constructor
	**********************************************************************************************/
	initialize: function(){
		try {
			/*Para navegadores distintos a internet explorer*/
			this.objAjax = new ActiveXObject("Msxml2.XMLHTTP");
			//alert("1");
		} 
		catch (e) {
			try {
				/*Para explorer*/
				this.objAjax = new ActiveXObject("Microsoft.XMLHTTP");
				//alert("2");
			}
			catch (E) {
				this.objAjax = false;
				//alert("3");
			}
		}
		
		if (!this.objAjax && typeof XMLHttpRequest!='undefined') {
			this.objAjax = new XMLHttpRequest();
			//alert("4");
		}
		
		/*
		==========================================================================
		== Variables obligatorias
		==========================================================================*/
		this.objOwner = "";
		this.url = ""; // URL a la que se llamará por POST o GET
		this.capa = ""; // ID de la capa DIV donde aparecerán los resultados
		this.valores = ""; // parametros
		this.metodo = "POST"; //POST o GET
		this.callBack = ""; //Llamada una vez finalizado el runAjx
		/*
		==========================================================================
		== Variables complementarias
		==========================================================================*/
		this.txtVacia = "No se han obtenido resultados";
		this.txtCargando  = "... cargando ...";
		this.urlNoExiste = "La direcci&oacute;n no existe."; 
	},

	/**********************************************************************************************
	* setTxtCargando
	**********************************************************************************************/
	setTxtCargando: function(_txt){
		if (_txt!=""){
			this.txtCargando = _txt;
		}else{
			alert ("[setTxtCargando] La cadena está vacía.");
		}
	},
	
	/**********************************************************************************************
	* setTxtCargando
	**********************************************************************************************/
	setCallBack: function(_txt){
		if (_txt!=""){
			this.callBack = _txt;
		}else{
			alert ("[setCallBack] No se ha especificado ninguna función.");
		}
	},
	
	/**********************************************************************************************
	* setTxtVacia
	**********************************************************************************************/
	setTxtVacia: function(_txt){
		if (_txt!=""){
			this.txtVacia = _txt;
		}else{
			alert ("[setTxtVacia] La cadena está vacía.");
		}
	},

	/**********************************************************************************************
	* setValores
	**********************************************************************************************/
	setValores: function(_val){
		if (_val!=""){
			this.valores = _val;
		}else{
			alert ("[setValores] No existen valores.");
		}
	},
	
	/**********************************************************************************************
	* setURL
	**********************************************************************************************/
	setURL: function(_url){
		if (_url!=""){
			this.url = _url;
		}else{
			alert ("[setURL] No ha especificado la URL.");
		}
	},
	
	/**********************************************************************************************
	* setCapa
	**********************************************************************************************/
	setCapa: function(_capa){
		if (_capa!=""){
			this.capa = _capa;
		}else{
			alert ("[setCapa] No ha especificado la capa.");
		}
	},

	/**********************************************************************************************
	* Ejecutamos el envío por Ajax
	**********************************************************************************************/
	runAjx: function(){
		if (this.url!="" && this.capa!="" && this.metodo!=""){
		  var capaContenedora = document.getElementById(this.capa);
		
			/*Creamos y ejecutamos la instancia*/
			ajax = this.objAjax;
			TMPtxtCargando = this.txtCargando;
			TMPtxtVacia = this.txtVacia;
			TMPurlNoExiste = this.urlNoExiste;
			TMPcallBack = this.callBack;
			TMPobjOwner = this.objOwner;
			
			ajax.open (this.metodo, this.url, true);
			ajax.onreadystatechange = function() {
				switch (ajax.readyState){
					case 1:
						capaContenedora.innerHTML = TMPtxtCargando;
					break;
					case 4:
						switch (ajax.status){
							case 200:
								if (ajax.responseText!="")
									capaContenedora.innerHTML=ajax.responseText;
								else{
									capaContenedora.innerHTML=TMPtxtVacia;
								}
								if (TMPcallBack!=""){
									//alert("llamando a "+TMPcallBack);
									setTimeout(eval(TMPobjOwner.TMPcallBack), 10);
								}
							break;
							case 400:
								capaContenedora.innerHTML = TMPurlNoExiste;
							break;
							case 404:
								capaContenedora.innerHTML = TMPurlNoExiste;
							break;
							default:
								capaContenedora.innerHTML = "Error: "+ajax.status+"<br>Descripcion: "+ajax.statusText;
							break;
						}//switch (this.objAjax.status)
					break;
				}//switch (this.objAjax.readyState)
				
			}
			//alert("[CALL] url: "+this.url+"\ncapa: "+this.capa+"\nvalores: "+this.valores);
			this.objAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.objAjax.send(this.valores);
		}else{
			alert("No se han especificado todos los parámetros.\n url:"+this.url+"\n capa:"+this.capa+"\n metodo:"+this.metodo);
			
		}
	},

	/**********************************************************************************************
	**********************************************************************************************/
	URLEncode: function (p1){
		// The Javascript escape and unescape functions do not correspond
		// with what browsers actually do...
		var SAFECHARS = "0123456789" +					// Numeric
						"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
						"abcdefghijklmnopqrstuvwxyz" +
						"-_.!~*'()";					// RFC2396 Mark characters
		var HEX = "0123456789ABCDEF";
		
		var plaintext = p1;//document.URLForm.F1.value;
		var encoded = "";
		for (var i = 0; i < plaintext.length; i++ ) {
			var ch = plaintext.charAt(i);
		    if (ch == " ") {
			    encoded += "+";				// x-www-urlencoded, rather than %20
			} else if (SAFECHARS.indexOf(ch) != -1) {
			    encoded += ch;
			} else {
			    var charCode = ch.charCodeAt(0);
				if (charCode > 255) {
				    alert( "Unicode Character '" 
		                      + ch 
		                      + "' cannot be encoded using standard URL encoding.\n" +
					          "(URL encoding only supports 8-bit characters.)\n" +
							  "A space (+) will be substituted." );
					encoded += "+";
				} else {
					encoded += "%";
					encoded += HEX.charAt((charCode >> 4) & 0xF);
					encoded += HEX.charAt(charCode & 0xF);
				}
			}
		} // for
		
		//document.URLForm.F2.value = encoded;
		
		//return false;
		return encoded;
	}
};

