function Lector_JSON(v_documento, v_funcion_callback, v_scope_callback, v_es_cadena)
{
	this.lector=null;
	this.documento=null;
	this.funcion_callback=null;
	this.scope_callback=null;
	this.es_cadena=null;
	this.metodo='GET';
	this.datos_post=null;
	this.JSON=null;

	if(v_documento && v_funcion_callback)
	{
		this.crear(v_documento, v_funcion_callback, v_scope_callback, v_es_cadena);
	}
}

Lector_JSON.prototype.pasar_a_post=function(v_datos_post)
{
	this.metodo='POST';
	this.datos_post=v_datos_post;
}


Lector_JSON.prototype.crear=function(v_documento, v_funcion_callback, v_scope_callback, v_es_cadena) //La función callback debe recibir un parámetro: el propio lector.
{
	this.documento=v_documento;
	this.funcion_callback=v_funcion_callback;
	this.scope_callback=v_scope_callback;
	this.es_cadena=(v_es_cadena ? true : false);

	var aquello=this;


	if(false) {}
	else //El resto... Realmente es una sucia petición de AJAX. De IE7 en adelante...
	{	
		if(this.es_cadena)
		{
/*
			var temp=new DOMParser();
			this.lector=temp.parseFromString(this.documento, "text/xml");

			if(this.funcion_callback)
			{
				this.preparar.call(this);
			}
			else
			{
				this.raiz=this.lector.documentElement;
			}
*/
		}
		else
		{
			var temp=new XMLHttpRequest();

			if(this.metodo=='GET')
			{
				temp.open(this.metodo, this.documento , true);
				temp.onreadystatechange=function()
				{
					if(temp.readyState==4)
					{
						aquello.JSON=temp.responseText;
						aquello.preparar.call(aquello);
					}
				}
				temp.send(null);
			}
			else if(this.metodo=='POST')
			{
				temp.open(this.metodo, this.documento, true);	//Prepara...						

//				temp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				temp.setRequestHeader("Content-type", "application/json");
				temp.setRequestHeader("Content-length", this.datos_post.length);
				temp.setRequestHeader("Connection", "close");

				temp.onreadystatechange=function()
				{
					if(temp.readyState==4)
					{
						aquello.JSON=temp.responseText;	
						aquello.preparar.call(aquello);
					}
				}
				temp.send(this.datos_post);	//Lanza.	
			}
		}	
	}
}

//Preparar es una función de callback... La llamamos desde crear cuando se ha 
//finalizado la carga del documento. Se llama desde el scope del mismo elemento
//para evitar problemas....

Lector_JSON.prototype.preparar=function()
{	
	this.JSON=eval('('+this.JSON+')');

	if(!this.JSON)
	{
		//alert('ERROR: Imposible cargar XML');
	}
	else
	{
		if(this.scope_callback)
		{
			this.funcion_callback.call(this.scope_callback, this);
		}
		else
		{
			this.funcion_callback(this);
		}
	}
}

