/*
 * [FILE]		AJAX_minilib.js
 * [AUTHOR]	GIRALT CELIMÉNDIZ, Jacobo (jacobo.giralt[AT]gmail[DOT]com)
 * [DATE]		April 2006
 * [VERSION]	pre-release
 * [DESCRIPTION]	Minimal AJAX library
 */

function AJAX_obj()
{
	var t = this; //Variable para referirse al objeto raíz
	
	//Propiedades
	this.method='get';
	this.debug=false;
	this.handler;
	
	//Métodos
	this.out = function(mensaje)
	{
		if (this.debug) alert(mensaje);
	}
	
	this.checkAndCall = function()
	{
		if (t.xmlHttp.readyState != 4)
		{
			t.out('not ready');
			return;
		}
		if (t.xmlHttp.status != 200)
		{
			t.out('bad status');
			return;
		}
		//Llamada al handler definitivo con la respuesta como parámetro
		t.out('now I am ready');
		t.handler(t.xmlHttp.responseText);
	}
	
	this.fetch = function(query)
	{
		//Creación del objeto xmlHttpRequest
		if (window.ActiveXObject) 
			this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest)
			this.xmlHttp = new XMLHttpRequest();
		
		if (this.debug && !this.xmlHttp)
			this.out('No se pudo crear el objeto xmlHttpRequest');
		
		
		//Definimos un handler propio que comprobará que no hay errores antes de pasar los
		//resultados de la consulta al handler definido por el usuario.
		this.xmlHttp.onreadystatechange = t.checkAndCall;
		this.xmlHttp.open(this.method, query, true);
		this.xmlHttp.send(null);
	}
}

