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 (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 ()
{
	this.SiguientePeticion ();
		
	if (window.XMLHttpRequest) 
  {	try
   	{	AjaxPeticiones [this.fPeticion] = new XMLHttpRequest ();
   	} catch(e)
   	{	this.fPeticion = null;
   	}
  } else if (window.ActiveXObject) 
	{	try
   	{	AjaxPeticiones [this.fPeticion] = new ActiveXObject ("Msxml2.XMLHTTP");
		} catch(e) 
    {	try
    	{	AjaxPeticiones [this.fPeticion] = new ActiveXObject ("Microsoft.XMLHTTP");
      } catch(e)
      {	AjaxPeticiones [this.fPeticion] = null;
      }
		}
  }
  return (AjaxPeticiones [this.fPeticion]);
}


TAjax.prototype.Estado = function ()
{
	return (AjaxPeticiones [this.fPeticion].status)
}


TAjax.prototype.GetParametros = function ()
{
	var Result = '';
	var i = 0;
	var l = this.fParametros.length;
	
	for (i = 0; i < l; i++)
	{	if (i == 0)
		{	if (this.fMetodo == 'GET') Result = '?';
			Result += this.fParametros [i][0] + '=' + this.fParametros [i][1];
		} else Result += '&' + this.fParametros [i][0] + '=' + this.fParametros [i][1];
	}
	
	return (Result);
}


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 ();
}


//================================================================================
//================================================================================
//================================================================================
//================================================================================



