var expCaracteresEspeciales = new RegExp("(ñ|á|é|í|ó|ú)", "gi"); // /(ñ|á|é|í|ó|ú)/gi;

function convertirCaracteresEspeciales(cadena)
{
    return cadena;
    var x = cadena.replace(expCaracteresEspeciales,function(c){return "&#" + c.charCodeAt(0).toString(16) + ";";}); 

    //return escape(cadena);
    if (x != cadena)
        return x;

    return cadena;
}

function mostrarPorId(id)
{
    if (typeof id != 'object')
        id = document.getElementById(id);
	if (id != null)
	{
		id.style.display = 'block';
	}
}
function ocultarPorId(id)
{
    if (typeof id != 'object')
        id = document.getElementById(id);   
	if (id != null)
	{
		id.style.display = 'none';
	}
}

function Ajax(url)
{
	this._loadingContainer	= null;
	this._responseContainer = null;
	this._errorContainer	= null;
	this._form				= null;
	this.__e				= function(id)
	{
	    if (typeof id == 'object') return id;
	    return document.getElementById(id);
    };

	this.metodo = 'GET';
	this.url	= url;
	this.datos  = Array();

    this.addLoading     = function(id)
    {
        var o = this.__e(id);
        if (o != null)
        {
			try
			{
				var divLoading = document.createElement("div");

				divLoading.id = o.id + "_loading";
				divLoading.className = "loading";

				o.appendChild(divLoading);
				this.setLoading(divLoading);
			}
			catch(e)
			{
				
			}

        }
    };
	this.setLoading		= function(id){if ((this._loadingContainer = this.__e(id))==null){/* Crear container ! */}};
	this.setResponse	= function(id){if ((this._responseContainer = this.__e(id))==null){/* Crear container ! */}};
	this.setError		= function(id){if ((this._errorContainer = this.__e(id))==null){/* Crear container ! */}};
	this.setForm		= function(id)
	{
		if (id != null && (this._form = this.__e(id))!=null)
		{
		    var inp = document.createElement("input");
		    inp.name= id + "_rnd";
		    inp.type= "hidden";
		    inp.value = Math.random();
		    
		    this._form.appendChild(inp);
		    
			if (this._form.action != null && this._form.action.length > 0)
			{
				this.url = this._form.action;
			}
			if (this._form.method != null && this._form.method.length > 0)
			{
				this.metodo = this._form.method.toUpperCase();
			}
		}
	};

	this.escribirRespuesta = function(o)
	{
		if (o.argument._responseContainer != null)
		{
			o.argument._responseContainer.innerHTML = o.responseText;
		}
	};
	this._procesarOpcionesDeRespuestaJS = function (o,r)
	{
        if (r.opciones!= null)
        {
            if (r.opciones.redirect != null)
            {
                document.location = r.opciones.redirect;
            }
        }
	};

    this.funcionCodigo = {
	    "-1":function(o,r){o.statusText = r.mensaje; o.argument.postHandlerOk = o.argument._handlerError;},
	    "0":function(o,r){o.responseText = r.mensaje; o.argument.postHandlerOk = o.argument.escribirRespuesta;},
	    "default":function(o,r)
	    {
	        o.statusText = r.mensaje; o.argument.postHandlerOk = o.argument._handlerError;
        }
    };

	
	this.procesarCodigoRespuestaJS = function(o,r)
	{
        if (r.codigo != null)
        {
            if (this.funcionCodigo[String(r.codigo)] != null && (typeof this.funcionCodigo[String(r.codigo)] == 'function' || typeof this.funcionCodigo[String(r.codigo)] == 'object') )
            {
                this.funcionCodigo[String(r.codigo)](o,r);
            }
            else
            {
                this.funcionCodigo["default"](o,r);
            }
        }
        else
        {
        }
	};
	
	this.procesarRespuestaJS = function(o)
	{
	    var r;
        try
        {
            if (o.responseText != null && o.responseText.length >1 && o.responseText.substring(0,1) == '{')
            {
                eval("r="+o.responseText);
                
                this._procesarOpcionesDeRespuestaJS(o,r);
                this.procesarCodigoRespuestaJS(o,r);
                
                
            }
            else
            {
                o.statusText = "No es posible procesar la respuesta";
                this.postHandlerOk = this._handlerError;
            }	    
        }
        catch(e)
        {
            o.statusText = e.message + "\n" + o.responseText;
            this.postHandlerOk = this._handlerError;
        }
	};
	
	this.enviar = function(texto)
	{
		if (this._form != null)
		{
			return this.enviarForm();
		}
		else
		{
			return this._enviar(texto);
		}
	};
	this._enviar = function(texto)
	{
		this.preLoading();
		this.mostrarLoading();

		YAHOO.util.Connect.asyncRequest(this.metodo, this.url,
		{
			success:this._handlerOk,
			failure:this._handlerError,
			argument:this
		});
		
		return false;
    }	
	this.enviarForm = function(id)
	{
		if (id != null)
		{
			this.setForm(id);
		}
		else
		{
			id = this._form.id;
		}

		this.preLoading();
		this.mostrarLoading();
		YAHOO.util.Connect.setForm(this._form);//,true);
		YAHOO.util.Connect.asyncRequest(this.metodo, this.url,
		{
			success:this._handlerOk,
			failure:this._handlerError,
			argument:this
		});
		
		return false;
	};
	this.preLoading = function(){};
	
	this.mostrarLoading = function(){this.mostrar(this._loadingContainer);};
	this.ocultarLoading = function(){this.ocultar(this._loadingContainer);};
	this.mostrarResponse = function(){this.mostrar(this._responseContainer);};
	this.ocultarResponse = function(){this.ocultar(this._responseContainer);};
	this.mostrarError = function(){this.mostrar(this._errorContainer);};
	this.ocultarError = function(){this.ocultar(this._errorContainer);};

	this.mostrar = mostrarPorId;	
	this.ocultar = ocultarPorId;
	
	this._handlerOk = function(o)
	{
		o.argument.preHandlerOk(o);
		o.argument.handlerOk(o);
        o.argument.listo();
		o.argument.postHandlerOk(o);
	};
	this.preHandlerOk = function(o){};
	this.postHandlerOk = function(o){};
	this.handlerOk = this.escribirRespuesta;
	
	this._handlerError = function(o)
	{
		o.argument.preHandlerError(o);
		o.argument.handlerError(o);
		o.argument.listo();
		o.argument.postHandlerError(o);
	};
	this.preHandlerError = function(o){};
	this.postHandlerError = this.mostrarError;
	this.handlerError = function(o)
	{
		if (o.argument._errorContainer != null)
		{
			o.argument._errorContainer.innerHTML = o.statusText;
		}
		else
		{
			alert(o.statusText);
		}
	};
	this.listo = function (){this.ocultarLoading();};
}

function addEvent(obj, evType, fn)
{ 
	if (obj.addEventListener)
	{ 
		obj.addEventListener(evType, fn, false); 
			return true; 
	} 
	else 
		if (obj.attachEvent)
		{ 
			var r = obj.attachEvent("on"+evType, fn); 
			return r; 
		} 
		else 
		{ 
			return false; 

		} 
}

