/*===========================================================================================*/
/**
* @throws	Error	Si el browser no soporta AJAX.
*/
function AJAXHandler()
{
	this.handler	= null;
	this.text		= '';
	this.XML		= '';

	// Status 
	this.ST_UNINITIALIZED	= 0;
	this.ST_LOADING			= 1;
	this.ST_LOADED			= 2;
	this.ST_INTERACTIVE		= 3;
	this.ST_COMPLETE		= 4;
	/*=============================================*/
	if (window.XMLHttpRequest)
	{
		this.handler = new XMLHttpRequest();
	}
	else
	{
		try
		{
			this.handler = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			try
			{
				this.handler = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				this.handler = null;
				throw new Error("Your browser does not support AJAX.\n\nPlease update your browser.");
			}
		}
	}
	
	/**
	*	Setear funcion que se ejecuta onreadystatechange
	*	@param	function
	*/
	this.setReadyStateListener = function (func) {
		if (!this.handler) return;
		this.handler.onreadystatechange = func;
	};

	/**
	*	Avisa si el request se completó correctamente
	*	@return	boolean
	*/
	this.ok = function() {
		if (!this.handler) return;
		var filter = /^\{ERROR\}/;
		if (this.handler.readyState == this.ST_COMPLETE)
		{
			if (this.handler.status == 200)
			{
				if (this.handler.responseText && filter.test(this.handler.responseText))
				{
					alert(this.handler.responseText.substr(7));
				}
				else
				{
					if (this.handler.responseXML && this.handler.responseXML.getElementsByTagName('error')[0])
					{
						alert(this.handler.responseXML.getElementsByTagName('error')[0].firstChild.data);
					}
					else
					{
						if (this.handler.responseText) this.text = this.handler.responseText;
						if (this.handler.responseXML) this.XML = this.handler.responseXML;
						if (IE)	this.handler.abort();
						return true;
					}
				}
				if (IE)	this.handler.abort();
			}
		}
		return false;
	};

	/**
	*	Enviar request al server
	*	@param	string	metodo: POST / GET 
	*	@param	string	cadena de variables a enviar
	*	@param	string	archivo que recibe el request
	*	@param	boolean	sincronico / ascincronico
	*/
	this.request = function(method, URI, dest, async) {
		if (this.handler && (this.handler.readyState == this.ST_UNINITIALIZED || this.handler.readyState == this.ST_COMPLETE))
		{
			if (typeof dest == 'undefined') var dest = window.location.href;
			if (typeof async == 'undefined') var async = true;
			if (method == 'POST')
			{
				this.handler.open("POST", dest, async);
				this.handler.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.handler.send(URI);
			}
			else
			{
				this.handler.open("GET", dest + '?' + URI, async);
				this.handler.send(null);
			}
		}
	};

	/**
	*	Muestra mensaje de estado en un contenedor de bloque
	*	@param	string		texto a mostrar
	*	@param	string|null	(optional) id del objeto donde mostrar el texto
	*	@param	boolean		centrar en el viewport
	*/
	this.showStatus = function(msg, oID, center) {
		var o, customStatus = false;
		if (typeof center != 'boolean') var center = false;
		if (oID && (o = document.getElementById(oID)) != null)
			customStatus = true;
		else
			o = document.getElementById('ajax_handler__status');
		if (o == null)
		{
			var div = document.createElement('div');
			div.id = 'ajax_handler__status';
			div.style.position = (OLD_IE) ? 'absolute' : 'fixed';
			div.style.border = '1px solid #000';
			div.style.padding = '6px';
			div.style.backgroundColor = '#FF8000';
			div.style.color = '#000';
			div.style.fontWeight = 'bold';
			div.style.fontSize = '11px';
			document.body.appendChild(div);
			o = div;
		}
		if (this.handler.readyState == this.ST_LOADING)
		{
			o.innerHTML = msg;
			o.style.display = 'block';
			if (center)
			{
				if (OLD_IE)
				{
					o.style.position = 'absolute';
					var dimXY = getMaxXY();
					var scrollXY = getScrollXY();
					o.style.left = dimXY[0] / 2 + scrollXY[0] - o.offsetWidth / 2 + 'px';
					o.style.top = dimXY[1] / 2 + scrollXY[1] - o.offsetHeight / 2 + 'px';
					window.attachEvent('onscroll', function() {
						var dimXY = getMaxXY();
						var scrollXY = getScrollXY();
						o.style.left = dimXY[0] / 2 + scrollXY[0] - o.offsetWidth / 2 + 'px';
						o.style.top = dimXY[1] / 2 + scrollXY[1] - o.offsetHeight / 2 + 'px';
					});
				}
				else
				{
					o.style.position = 'fixed';
					o.style.left = '50%';
					o.style.top = '50%';
					o.style.marginLeft = -(o.offsetWidth/2) + 'px';
					o.style.marginTop = -(o.offsetHeight/2) + 'px';
				}
			}
			else if (!customStatus)
			{
				if (OLD_IE)
				{
					o.style.left = 2 + getScrollXY()[0] + 'px';
					o.style.top = 2 + getScrollXY()[1] + 'px';
					window.attachEvent('onscroll', function() {
						o.style.left = 2 + getScrollXY()[0] + 'px';
						o.style.top = 2 + getScrollXY()[1] + 'px';
					});
				}
				else
				{
					o.style.left = '2px';
					o.style.top = '2px';
				}
			}
		}
		else if (this.handler.readyState == this.ST_COMPLETE)
		{
			o.style.display = 'none';
		}
	};
}
