//Esta función preparar una respuesta estándar para un XML con resultado,
//código a ejecutar y mensaje a mostrar... Esto sólo vale para XMLs creados
//con el Lector XML.

function preparar_respuesta_xml(v_xml)
{
	var l=new Lector_doc(v_xml.raiz);
	var resultados=Array();

	l.bajar(0);
	resultados['resultado']=parseInt(l.posicion.texto);

	l.avanzar();
	resultados['codigo']=l.posicion.texto;

	l.avanzar();
	resultados['mensaje']=l.posicion.texto;

	return resultados;
}

//Aquí empieza la clase en si.

function Lector_XML(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;

	//Estos son para la navegación.
	this.raiz=null;

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

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


Lector_XML.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) //document.all) //IE... Una forma demasiado simplista quizás...
	{
		/*
		this.lector=new ActiveXObject("Microsoft.XMLDOM");
		this.lector.async=false;	//Lo ponemos en modo síncrono: esto es, el parser se para hasta que el documento se cargue.

		if(this.es_cadena)
		{
			this.lector.loadXML(this.documento);
		}
		else
		{
			this.lector.onreadystatechange=function() 
			{
				if(aquello.lector.readyState==4)
				{
					aquello.preparar.call(aquello);
				}
			};
		
			this.lector.load(this.documento);
		}
		*/
	}
	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.lector=temp.responseXML;
						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-length", this.datos_post.length);
				temp.setRequestHeader("Connection", "close");

				temp.onreadystatechange=function()
				{
					if(temp.readyState==4)
					{
						aquello.lector=temp.responseXML;			
						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_XML.prototype.preparar=function()
{	
	if(!this.lector)
	{
		//alert('ERROR: Imposible cargar XML');
	}
	else
	{
	
		this.raiz=this.lector.documentElement;

		if(this.scope_callback)
		{
			this.funcion_callback.call(this.scope_callback, this);
		}
		else
		{
			this.funcion_callback(this);
		}
	}
}

/*
Lector_XML.prototype.crear=function(v_documento, v_funcion_callback, v_scope_callback) //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;

	var aquello=this;

	if(document.all) //IE... Una forma demasiado simplista quizás...
	{
		this.lector=new ActiveXObject("Microsoft.XMLDOM");
		this.lector.async=false;	//Lo ponemos en modo síncrono: esto es, el parser se para hasta que el documento se cargue.
		this.lector.onreadystatechange=function() 
		{
			if(aquello.lector.readyState==4)
			{
				aquello.preparar.call(aquello);
			}
		};
	}
	else //El resto...
	{	
		this.lector=document.implementation.createDocument("", "", null);
		this.lector.onload=function()
		{
			aquello.preparar.call(aquello)
		};
	}

	this.lector.load(this.documento);
}
*/

