/**
 *	Objeto para encolar las Peticiones AJAX
 *
 *	@param		string		url				Dirección donde enviar los Datos
 */
var colaAJAX = function(url)
{
	var cola = this;
	this.url = url;
	this.colaPeticiones = new Array();
	this.xmlHTTP = null;

	/**
	 *	Instancia el Objeto XMLHttpRequest
	 */
	this.getXmlHttpObject = function()
	{
		this.xmlHTTP = null;
		try
		{
			/**
			 *	Firefox, Opera 8.0+, Safari
			 */
			this.xmlHTTP = new XMLHttpRequest();
		}
		catch (e)
		{
			/**
			 *	Internet Explorer
			 */
			try
			{
				this.xmlHTTP = new ActiveXObject('Msxml2.XMLHTTP');
			}
			catch (e)
			{
				this.xmlHTTP = new ActiveXObject('Microsoft.XMLHTTP');
			}
		}
	};
	
	/**
	 *	Agrega una Petición a la Cola
	 *
	 *	@param		string		parametros		Parametros a enviar
	 *	@param		string		metodo			Método de Envío de los Datos (GET ó POST)
	 *	@param		boolean		asincrono		Establece si el Envío de los Datos será Asíncrono
	 *	@param		string		funcionCargando	Nombre de la Función a ejecutar comenzar con el Envío de los Datos
	 *	@param		string		funcionProcesar	Nombre de la Función a ejecutar al recibir los Datos
	 *	@param		string		funcionError	Nombre de la Función a ejecutar si se produce un Error al recibir los Datos
	 */
	this.addPeticion = function(parametros, metodo, asincrono, funcionCargando, funcionProcesar, funcionError)
	{
		peticion = new Array();
		peticion['parametros'] = parametros;
		peticion['funcionCargando'] = funcionCargando;
		peticion['funcionProcesar'] = funcionProcesar;
		peticion['funcionError'] = funcionError;
		peticion['metodo'] = metodo.toUpperCase();
		peticion['asincrono'] = asincrono;
		
		this.colaPeticiones.push(peticion);
		/**
		 *	Si es la Primera Petición la ejecuto
		 */

		if (this.colaPeticiones.length == 1)
		{
			this.ejecutarPeticion();
		}
	};
	
	/**
	 *	Ejecuta la Primera Petición pendiente
	 */
	this.ejecutarPeticion = function()
	{
		/**
		 *	Debe haber por lo menos una Petición Pendiente
		 */
		if (this.colaPeticiones.length > 0)
		{
			/**
			 *	La dirección URL debe estar especificada
			 */
			if ((this.url != null) && (this.url != undefined) && (this.url.length > 0))
			{
				/**
				 *	Si está definido el Objeto XMLHttpRequest, lo defino
				 */
				if ((this.xmlHTTP == null) || (this.xmlHTTP == undefined))
				{
					this.getXmlHttpObject();
				}
				/**
				 *	Si el Objeto XMLHttpRequest está definido, ejecuto las consultas
				 */
				if (this.xmlHTTP != null)
				{
					peticion = this.colaPeticiones.shift();
					if ((peticion['funcionProcesar'] != null) && (peticion['funcionProcesar'] != undefined) && (peticion['funcionProcesar'].length > 0))
					{
						this.colaPeticiones.unshift(peticion);
						if ((peticion['asincrono'] != null) && (peticion['asincrono'] != undefined) && (peticion['asincrono']))
						{
							asincrono = true;
						}
						else
						{
							asincrono = false;
						}
						if (peticion['metodo'] == 'POST')
						{
							this.xmlHTTP.onreadystatechange = function() { cola.procesarCambioEstado(); };
							this.xmlHTTP.open('POST', this.url, asincrono);
							this.xmlHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
							this.xmlHTTP.send(peticion['parametros']);
						}
						else
						{
							this.xmlHTTP.onreadystatechange = function() { cola.procesarCambioEstado(); };
							this.xmlHTTP.open('GET', this.url + '?' + peticion['parametros'], true);
							this.xmlHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
							this.xmlHTTP.send(null);
						}
					}
					else
					{
						/**
						 *	Si quedan Peticiones pendientes, proceso la siguiente
						 */
						if (this.colaPeticiones.length > 0)
						{
							this.ejecutarPeticion();
						}
					}
				}
			}
		}
	};
	
	/**
	 *	Procesa el Cambio de Estado de un Objeto XMLHttpRequest
	 */
	this.procesarCambioEstado = function()
	{
		/**
		 *	Debe haber por lo menos una Petición Pendiente
		 */
		if (this.colaPeticiones.length > 0)
		{
			peticion = this.colaPeticiones.shift();
			/**
			 *	Completado
			 */
			if (this.xmlHTTP.readyState == 4)
			{
				if (this.xmlHTTP.status == 200)
				{
					if ((peticion['funcionProcesar'] != null) && (peticion['funcionProcesar'] != undefined) && (peticion['funcionProcesar'].length > 0))
					{
						if (this.xmlHTTP.responseText.length > 0)
						{
							datos = this.xmlHTTP.responseXML;
							if ((datos != null) && (datos != undefined))
							{
								eval(peticion['funcionProcesar'] + '(datos.documentElement)');
							}
						}
					}
				}
				else
				{
					if ((peticion['funcionError'] != null) && (peticion['funcionError'] != undefined) && (peticion['funcionError'].length > 0))
					{
						eval(peticion['funcionError'] + '()');
					}
				}
				this.xmlHTTP = null;
				/**
				 *	Si quedan Peticiones pendientes, proceso la siguiente
				 */
				if (this.colaPeticiones.length > 0)
				{
					this.ejecutarPeticion();
				}
			}
			else
			{
				/**
				 *	Cargando
				 */
				if (this.xmlHTTP.readyState == 1)
				{
					if ((peticion['funcionCargando'] != null) && (peticion['funcionCargando'] != undefined) && (peticion['funcionCargando'].length > 0))
					{
						eval(peticion['funcionCargando'] + '()');
					}
				}
				this.colaPeticiones.unshift(peticion);
			}
		}
	};
};
