// esta clase requiere global.js
/*===========================================================================================*/
function AJAX_HANDLER()
{
	this.handler	= false;	// XMLHttpRequest Object
	this.text		= '';		// texto recibido
	this.XML		= '';		// XML recibido

	/*=============================================*/

	this.init = function()
	{
		if (window.XMLHttpRequest)
		{
			this.handler = new XMLHttpRequest();
			//if (MOZ) this.handler.overrideMimeType('text/xml'); da errores en la consola si no se recibe xml en firefox 1.5
		}else{
			if (window.ActiveXObject)
			{
				try
				{
					this.handler = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					try
					{
						this.handler = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
						//alert("Navigator is not compatible with AJAX!");
					}
				}
			}
		}
	}

	/*=============================================*/

	this.ok = function()
	{
		// @return: true, si se completo el request correctamente
		/*========================*\
			Status :
			# 0 (uninitialized)
			# 1 (loading)
			# 2 (loaded)
			# 3 (interactive)
			# 4 (complete)
		\*========================*/
		// error detector
		var filter = /^\{ERROR\}/;
		if (this.handler.readyState == 4)
		{
			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();
			}else{
				alert('Server error!');
			}
		}
		return false;
	}

	/*=============================================*/

	// AJAX Request
	// request(string method, string queryString [, string targetLocation])
	this.request = function(method, URI, dest, async)
	{
		if (this.handler && (this.handler.readyState == 0 || this.handler.readyState == 4))
		{
			if (typeof dest == 'undefined') var dest = window.location.href;
			if (typeof async == 'undefined') var async = true;
			if (method == 'POST')
			{
				// POST request
				this.handler.open("POST", dest, async);
				this.handler.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.handler.send(URI);
			}else{
				// GET request
				this.handler.open("GET", dest + '?' + URI, async);
				this.handler.send(null);
			}
		}
	}

	/*=============================================*/

	// muestra mensaje en el centro de la pantalla automaticamente
	// showMsg(string message [, string objectID])
	this.showMsg = function(msg, divID)
	{
		
		var div = (typeof divID == 'undefined') ? document.getElementById('ajax_status') : document.getElementById(divID);
		if (div)
		{
			var dimXY = getMaxXY();
			var scrollXY = getScrollXY();
			if (this.handler.readyState == 1)
			{
				div.style.position = 'absolute';
				div.style.left = dimXY[0] / 2 + scrollXY[0] - div.offsetWidth / 2 + 'px';
				div.style.top = dimXY[1] / 2 + scrollXY[1] - div.offsetHeight / 2 + 'px';
				div.innerHTML = msg;
				div.style.display = 'block';
			}else{
				if (this.handler.readyState == 4) div.style.display = 'none';
			}
		}
	}

	/*=============================================*/

	/**
	*	@desc	muestra mensaje de estado en un contenedor en bloque
	*	@param	texto a mostrar
	*	@param	(opcional) id del objeto
	*/
	this.showStatus = function(msg, pID)
	{
		var o, customStatus = false;
		if (pID)
		{
			o = document.getElementById(pID);
			customStatus = true;
		}
		else o = document.getElementById('_ajax_handler_status_');
		if (o == null)
		{
			// creamos nuestra propia capita
			var div = document.createElement('div');
			div.id = '_ajax_handler_status_';
			div.style.position = 'absolute';
			div.style.border = '1px solid #000';
			div.style.padding = '6px';
			div.style.backgroundColor = '#FF8000';
			div.style.color = '#000';
			div.style.fontWeight = 'bold';
			document.body.appendChild(div);
			o = div;
		}
		o.innerHTML = msg;
		if (this.handler.readyState == 1)
		{
			o.style.display = 'block';
			if (!customStatus)
			{
				// posicionar capa segun scroll
				o.style.left = 2 + getScrollXY()[0] + 'px';
				o.style.top = 2 + getScrollXY()[1] + 'px';
			}
		}
		else if (this.handler.readyState == 4)
		{
			o.style.display = 'none';
		}
	}
	/*=============================================*/
	this.init();
}