//*****************************************************************************
//
//  Clase XmlDom
//  
//  Descripción: Permite parsear un documento XML.
//
//*****************************************************************************

// DECLARACION DE VARIABLES ---------------------------------------------------

// variable para acceder al prototipo
var _XmlDom = XmlDom.prototype;
var isIE = false;

// estados del objeto XML
_XmlDom.stateLoading = 1;
_XmlDom.stateLoaded = 2;
_XmlDom.stateInteractive = 3;
_XmlDom.stateCompleted = 4;


// ----------------------------------------------------------------------------
// CONSTRUCTOR DE LA CLASE
// ----------------------------------------------------------------------------

function XmlDom()
{
    // objecto XMLDOM a emmascarar
    this.object = null;
    // indica el estado de la carga
    this.readyState = 0;
    // manejador de evento que se lanza al completarse el parseo del archivo xml
    this.onstatecompleted = null;
	
    // crear el objeto XMLDOM
    this.createObject();
}


// ----------------------------------------------------------------------------
// FUNCIONES PÚBLICAS
// ----------------------------------------------------------------------------

_XmlDom.load = function (xmltext, xmlfile, onstatecompleted)
{
	//alert("xmltext: " + xmltext + "\nxmlfile: " + xmlfile + "\nonstatecompleted: " + onstatecompleted);
    // si hemos creado el objeto XML
    if (this.object)
    {
        // inicializa la variable que indica el estado de la carga
        this.readyState = 0;
        // evento al que se llamará cuando se complete la carga
	    this.onstatecompleted = onstatecompleted;
		
        this.object.async = true;
        
        // evento al que se llama cuando cambia el estado de la carga
        //if (this.object.onreadystatechange != null)
        //{
            this.object.onreadystatechange = _XmlDom.readyStateChange.bind(this);
        //}
	
		// MOZILA - da error en internet explorer
		/*
        if (!window.ActiveXObject)
        {
            this.object.onload = function() {eval(onstatecompleted);};
        }
		*/
        
        // si queremos cargar un texto
        if (xmltext)
        {
            //this.object.loadXML(xmltext);
        }
        // si queremos cargar un archivo
        else if (xmlfile)
        {
			this.object.open("GET", xmlfile, true);
			this.object.send(null);
            //this.object.load(xmlfile);
        }
        
    }    
};


// ----------------------------------------------------------------------------
// FUNCIONES PRIVADAS
// ----------------------------------------------------------------------------

// Crea el objeto dependiendo del navegador usado
_XmlDom.createObject = function ()
{

    if (window.XMLHttpRequest) {
        this.object = new XMLHttpRequest();
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        this.object = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	
	/*
    // Mozilla
    if (document.implementation && document.implementation.createDocument)
    {
        this.object = document.implementation.createDocument("", "doc", null);
    }
    // Internet Explorer
    else if (window.ActiveXObject)
    {
        this.object = new ActiveXObject("Microsoft.XMLDOM");
    }
    else
    {
        throw new Error("Failed to create XmlDom object.");
    }
	*/
};

//
//  _XmlDom.readyStateChange
//
//  Descripción: Maneja los cambios de estado del objeto XMLDOM
//      mientras se carga el documento. Al finalizar la carga llama
//      al manejador de evento 'onstatecompleted'.
//
_XmlDom.readyStateChange = function ()
{
    // guardamos el estado de la carga
    this.readyState = this.object.readyState;

    // si se ha completado la carga, lanzar el evento onstatecompleted
    if (this.readyState == _XmlDom.stateCompleted)
    {
        if (this.onstatecompleted != null)
        {
            eval(this.onstatecompleted);
        }
    }
};



// METODOS AÑADIDOS A FUNCIONES -----------------------------------------------

/*
    Este metodo esta disponible para cualquier funcion de la clase.

    Se utiliza para que un objeto externo pueda llamar a este
    metodo de la clase y no se pierda el objeto "this" que hace
    referencia a la instancia de la clase actual.
*/
Function.prototype.bind = function (object)
{
    var _method = this;
    return function ()
    {
        _method.apply(object);
    }
};

