var AjaxPeticiones = new Array ();

TAjax.prototype.fAsincrono   = true;
TAjax.prototype.fMensaje     = null;
TAjax.prototype.fMetodo      = 'POST';
TAjax.prototype.fParametros  = null;
TAjax.prototype.fPeticion    = -1;
TAjax.prototype.fOnCompletar = function () { };


TAjax.prototype.ActivarMensaje = function (IdMensaje, Absoluto)
{
	if (typeof IdMensaje != 'undefined') this.SetMensaje (IdMensaje);
	if (typeof Absoluto == 'undefined') Absoluto = false;
	
	if (this.fMensaje)
	{	if (Absoluto)
		{	this.fMensaje.style.top  = document.body.scrollTop;
		  this.fMensaje.style.left = document.body.clientWidth - this.fMensaje.clientWidth;
		}
		this.fMensaje.style.visibility = 'visible';
	}
}


TAjax.prototype.AddFormulario = function (Formulario)
{
	var i    = 0;
	var Nodo = null;
	
	for (i = Formulario.length - 1; i >= 0; i--)
	{	Nodo = Formulario.elements [i];
		if (Nodo.type == 'text') this.AddParametro (Nodo.name, Nodo.value);
		else if (Nodo.type == 'hidden') this.AddParametro (Nodo.name, Nodo.value);
		else if (Nodo.type == 'password') this.AddParametro (Nodo.name, Nodo.value);
		else if (Nodo.type == 'textarea') this.AddParametro (Nodo.name, Nodo.value);
		else if (Nodo.type == 'select-one') this.AddParametro (Nodo.name, Nodo.value);
		else if (Nodo.type == 'radio' || Nodo.type == 'checkbox') 
		{	if (Nodo.checked) this.AddParametro (Nodo.name, Nodo.value);
		}
	}
}


TAjax.prototype.AddParametro = function (Variable, Valor)
{
	this.fParametros [this.fParametros.length] = new Array (escape (Variable), escape (Valor));
}


TAjax.prototype.AsText = function ()
{
	if (AjaxPeticiones [this.fPeticion].status == 200) return (AjaxPeticiones [this.fPeticion].responseText);
	else return ('');
}


TAjax.prototype.AsXML = function ()
{
	if (AjaxPeticiones [this.fPeticion].status == 200) return (AjaxPeticiones [this.fPeticion].responseXML);
	else return (null);
}


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
   	{	AjaxPeticiones [this.fPeticion] = new XMLHttpRequest ();
  		return (AjaxPeticiones [this.fPeticion]);
   	} catch(e)
   	{	this.fPeticion = null;
   	}
  } else if (window.ActiveXObject) 
	{	for  (; i >= 0; i--)
		{	try
			{	AjaxPeticiones [this.fPeticion] = new ActiveXObject (aVersiones [i]);
  			return (AjaxPeticiones [this.fPeticion]);
		  } catch (E)
      {	AjaxPeticiones [this.fPeticion] = null;
    	}
    }
  }
	return (null);		
}


TAjax.prototype.Estado = function ()
{
	return (AjaxPeticiones [this.fPeticion].status)
}


TAjax.prototype._GetParametros = function ()
{
	var Result = new Array ();
	var i      = 0;
	var l      = this.fParametros.length;
	
	for (i = 0; i < l; i++)
		Result [i] = this.fParametros [i][0] + '=' + this.fParametros [i][1];
	return (Result.join  ('&'));	
}


TAjax.prototype.OnCompletar = function (Valor)
{
	if (typeof (Valor) == 'function') this.fOnCompletar = Valor;
	else alert ('TAjax.OnCompletar: Tipo de parámetro erróneo.');
}


TAjax.prototype.Open = function (URL, IdMensaje, Absoluto)
{
	if (this._CrearPeticion ())
	{	this.ActivarMensaje (IdMensaje, Absoluto);
		this._ReadyStateChange ();
		if (this.fMetodo == 'GET')
		{	AjaxPeticiones [this.fPeticion].open (this.fMetodo, URL + '?' + this._GetParametros (), this.fAsincrono);
			AjaxPeticiones [this.fPeticion].send (null);
		} else if (this.fMetodo == 'POST')
		{	AjaxPeticiones [this.fPeticion].open (this.fMetodo, URL, this.fAsincrono);
			AjaxPeticiones [this.fPeticion].setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
			AjaxPeticiones [this.fPeticion].send (this._GetParametros ());
		} else this.Raise ('TAjax: Método no soportado.');
	}
}


TAjax.prototype.Raise = function (Mensaje)
{
	var Excepcion = new Error (Mensaje);
	
	throw Excepcion;
}


TAjax.prototype._ReadyStateChange = function ()
{
	var self = this;
	
	AjaxPeticiones [this.fPeticion].onreadystatechange = function ()
	{	switch (AjaxPeticiones [self.fPeticion].readyState)
		{	case 0:		// Sin inicializar
				break;
			case 1:		// Cargando
				break;
			case 2:		// Cargado
				break;
			case 3:		// Interactivo
				break;
			case 4:		// Completado
				if (self.fMensaje) self.fMensaje.style.visibility = 'hidden';
				self.fOnCompletar (self);
				AjaxPeticiones [self.fPeticion] = null;
				break;
		}
	}
}


TAjax.prototype.SetMensaje = function (EtiquetaId)
{
	this.fMensaje = document.getElementById (EtiquetaId);
}

TAjax.prototype.SetGet = function ()
{
	this.fMetodo = 'GET';
}


TAjax.prototype.SetOnCompletar = function (Valor)		// En deshuso, usar OnCompletar
{
	this.OnCompletar (Valor);
}


TAjax.prototype.SetPost = function ()
{
	this.fMetodo = 'POST';
}


TAjax.prototype._SiguientePeticion = function ()
{
	var l = AjaxPeticiones.length;
	var i = 0;

	this.fPeticion = -1;
	for (i = 0; i < l && this.fPeticion <  0; i++)
		if (AjaxPeticiones [i] == null) this.fPeticion = i;
	if (this.fPeticion < 0) this.fPeticion = AjaxPeticiones.length;
}


function TAjax ()
{
	this.fParametros = new Array ();
}


//================================================================================
//================================================================================
//================================================================================
//================================================================================


