//

//  09/07/08 Se cambia la funcion Vars por la propiedad Vars

//	22/01/08 Se cambia onCompletar de método a propiedad. onCargar

//					 Se cambio Open por Cargar

//           Se cambia AddParametro por Vars

//



TAjax.AjaxPeticiones = new Array ();



TAjax.prototype.Id       = '';

TAjax.prototype.Cargando = '';

TAjax.prototype.Display  = 'block';

TAjax.prototype.Vars     = null;



TAjax.prototype.fAsincrono   = true;

TAjax.prototype.fMetodo      = 'POST';

TAjax.prototype.fPeticion    = -1;

TAjax.prototype.onCargar     = null;





TAjax.prototype.ActivarMensaje = function (bActivar)

{

	if (this.Cargando)

	{	var Etiq = document.getElementById (this.Cargando)

		if (Etiq)

		{	if (bActivar)

			{	switch (this.Display)

				{	case 'block':  	Etiq.style.display = 'block'; break;

					case 'inline': 	Etiq.style.display = 'inline'; break;

					case 'visible': Etiq.style.visibility = 'visible'; break;

				}

			} else 

			{	switch (this.Display)

				{	case 'block':  	Etiq.style.display = 'none'; break;

					case 'inline': 	Etiq.style.display = 'none'; break;

					case 'visible': Etiq.style.visibility = 'hidden'; break;

				}

			}

		}

	}

}





TAjax.prototype.asText = function ()

{

	if (TAjax.AjaxPeticiones [this.fPeticion].status == 200) 

		return TAjax.AjaxPeticiones [this.fPeticion].responseText;

	return '';

}





TAjax.prototype.asXML = function ()

{

	if (TAjax.AjaxPeticiones [this.fPeticion].status == 200) return (TAjax.AjaxPeticiones [this.fPeticion].responseXML);

	else return (null);

}





TAjax.prototype.Cargar = function (URL)

{

	if (this._CrearPeticion ())

	{	this.ActivarMensaje (true);

		this._ReadyStateChange ();

		if (this.fMetodo == 'GET')

		{	TAjax.AjaxPeticiones [this.fPeticion].open (this.fMetodo, URL + '?' + this._GetParametros (), this.fAsincrono);

			TAjax.AjaxPeticiones [this.fPeticion].send (null);

		} else if (this.fMetodo == 'POST')

		{	TAjax.AjaxPeticiones [this.fPeticion].open (this.fMetodo, URL, this.fAsincrono);

			TAjax.AjaxPeticiones [this.fPeticion].setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");

			TAjax.AjaxPeticiones [this.fPeticion].send (this._GetParametros ());

		} else this.Raise ('TAjax: Método no soportado.');

	}

}





TAjax.prototype._CrearPeticion = function ()

{

	var aVersiones =  ['Microsoft.XMLHttp', 'MSXML2.XMLHttp', 'MSXML2.XMLHttp.3.0',

	                   'MSXML2.XMLHttp.4.0', 'MSXML2.XMLHttp.5.0'];

	var i = aVersiones.length - 1;



	this._SiguientePeticion ();

	if (window.XMLHttpRequest) 

  {	try

   	{	TAjax.AjaxPeticiones [this.fPeticion] = new XMLHttpRequest ();

  		return (TAjax.AjaxPeticiones [this.fPeticion]);

   	} catch(e)

   	{	this.fPeticion = null;

   	}

  } else if (window.ActiveXObject) 

	{	for  (; i >= 0; i--)

		{	try

			{	TAjax.AjaxPeticiones [this.fPeticion] = new ActiveXObject (aVersiones [i]);

  			return (TAjax.AjaxPeticiones [this.fPeticion]);

		  } catch (E)

      {	TAjax.AjaxPeticiones [this.fPeticion] = null;

    	}

    }

  }

	return (null);		

}





TAjax.prototype.Estado = function ()

{

	return (TAjax.AjaxPeticiones [this.fPeticion].status)

}





TAjax.prototype.Formulario = function (Formulario)

{

	var i        = 0;

	var Nodo     = null;

	var Tipo     = '';

	var CharCode = false;

	var b = false;

	

	for (i = Formulario.length - 1; i >= 0; i--)

	{	Nodo = Formulario.elements [i];

		CharCode = Nodo.getAttribute ('CharCode') != null;

		if (Nodo.type == 'text') 

		{	Tipo = Nodo.getAttribute ('tipo');

			if (Tipo)

			{	switch (Tipo.toUpperCase ())

				{	case 'FLOAT': this.Vars [Nodo.name] = Nodo.value.asFloat (); break;

					case 'INT':   this.Vars [Nodo.name] = Nodo.value.asInteger (); break;

					default: alert ('Tipo ' + Tipo + ' incorrecto.');

				}

			} else this.Vars [Nodo.name] = CharCode ? Nodo.value.asCharCode () : Nodo.value;

		}

		else if (Nodo.type == 'hidden') this.Vars [Nodo.name] = CharCode ? Nodo.value.asCharCode () : Nodo.value;

		else if (Nodo.type == 'password') this.Vars [Nodo.name] = CharCode ? Nodo.value.asCharCode () : Nodo.value;

		else if (Nodo.type == 'textarea') this.Vars [Nodo.name] = CharCode ? Nodo.value.asCharCode () : Nodo.value;

		else if (Nodo.type == 'select-one') this.Vars [Nodo.name] = Nodo.value;

		else if ((Nodo.type == 'checkbox' || Nodo.type == 'radio') && Nodo.checked) this.Vars [Nodo.name] = Nodo.value;

	}

}









TAjax.prototype.InHTML = function (oElemento)

{

	var Scripts  = null;

	var Script   = null;



	if (oElemento)

	{	oElemento.innerHTML = this.asText ();

		Scripts = oElemento.getElementsByTagName ('script');

		for (var i = 0; i < Scripts.length; i++)

		{	Script = document.createElement ('script');

			Script.type = 'text/javascript';

			Script.text = Scripts [i].innerHTML;

			if (Scripts [i].src && Scripts [i].src.length) Script.src = Scripts [i].src;

			Scripts [i].parentNode.replaceChild  (Script, Scripts [i]);

		}

	}

}





TAjax.prototype.Raise = function (Mensaje)

{

	var Excepcion = new Error (Mensaje);

	

	throw Excepcion;

}





TAjax.prototype._ReadyStateChange = function ()

{

	var self = this;

	

	TAjax.AjaxPeticiones [this.fPeticion].onreadystatechange = function ()

	{	switch (TAjax.AjaxPeticiones [self.fPeticion].readyState)

		{	case 0:		// Sin inicializar

				break;

			case 1:		// Cargando

				break;

			case 2:		// Cargado

				break;

			case 3:		// Interactivo

				break;

			case 4:		// Completado

				self.ActivarMensaje (false);

				if (self.Id) self.InHTML (document.getElementById (self.Id));

				if (self.onCargar) self.onCargar (self);

				TAjax.AjaxPeticiones [self.fPeticion] = null;

				break;

		}

	}

}





TAjax.prototype.SetGet = function ()

{

	this.fMetodo = 'GET';

}





TAjax.prototype.SetPost = function ()

{

	this.fMetodo = 'POST';

}





TAjax.prototype._SiguientePeticion = function ()

{

	var l = TAjax.AjaxPeticiones.length;

	var i = 0;



	this.fPeticion = -1;

	for (i = 0; i < l && this.fPeticion <  0; i++)

		if (TAjax.AjaxPeticiones [i] == null) this.fPeticion = i;

	if (this.fPeticion < 0) this.fPeticion = TAjax.AjaxPeticiones.length;

}









function TAjax ()

{

	this.Vars = new Object ();

}





//================================================================================

//================================================================================

//================================================================================

//================================================================================



TAjax.prototype._GetParametros = function ()

{

	var Result = new Array ();

	var V;



	for (V in this.Vars) Result.push (escape (V) + '=' + escape (this.Vars [V]));

	return Result.join ('&');

}





TAjax.prototype.Editor = function (sVariable, sEditor)

{

	var Editor = tinyMCE.get (sEditor);

	

	if (Editor)

	{	var Body = Editor.getDoc().body;

	

		this.Vars [sVariable] = Editor.getContent ();

		this.Vars [sVariable + 'Text'] = Body.textContent ? Body.textContent : Body.innerText;

	}

}




