/******************************
con TILDES POR EL NETSCAPE/MOZILLA
*******************************/
var tlcXml = Class.create();
tlcXml.prototype = {
    xmlDoc: null,
    initialize: function () { },
    load: function (xmlFile) {
        this.xmlDoc = this.loadXmlDoc(xmlFile);
    },
    loadXsl: function (xslFile) {
        this.xslManager = this.loadXslDoc(xslFile);
    },
    /*Crea un objeto con un documento y un procesador para usarlo depende en que navegador*/
    loadXslDoc: function (xslFile) {
        xslDoc = this.loadXmlDoc(xslFile);
        var processor;
        if (typeof XSLTProcessor != "undefined") {
            processor = new XSLTProcessor();
            processor.importStylesheet(xslDoc);
        }
        return { xslDoc: xslDoc, processor: processor, xslFile: xslFile };
    },
    loadXmlDoc: function (xmlFile) {
        var xdoc;
        if (window.ActiveXObject && /Win/.test(navigator.userAgent)) {
            xdoc = new ActiveXObject("MSXML2.FreeThreadedDomDocument");
        } else if (document.implementation && document.implementation.createDocument) {
            xdoc = document.implementation.createDocument("", "", null);
        }
        else {
            return false;
        }
        xdoc.async = false;
        xdoc.load(xmlFile);
        return xdoc;
    },
    getXml: function (doc) {
        if (doc == null)
            doc = this.xmlDoc;
        if (window.ActiveXObject) {
            return doc.xml
        } else if (document.implementation && document.implementation.createDocument) {
            var s = new XMLSerializer();
            var str = s.serializeToString(doc);
            return (str);
        }
    },
    getInnerXml: function (doc) {
        if (doc == null)
            doc = this.xmlDoc;
        if (window.ActiveXObject) {
            return doc.text;
        } else if (document.implementation && document.implementation.createDocument) {
            return doc.textContent;
        }
    },
    setInnerXml: function (node, txt) {
        if (window.ActiveXObject) {
            node.text = txt;
        } else if (document.implementation && document.implementation.createDocument) {
            node.textContent = txt;
        }
    },
    transformStr: function (node, xslManager, parameters) {
        if (node == null)
            node = this.xmlDoc;
        if (xslManager == null)
            xslManager = this.xslManager;

        //Siempre aņadimos el entorno
        if (parameters) {
            parameters.push({ nombre: "ENTORNO", valor: ENTORNO });
        } else { 
            parameters = new Array({ nombre: "ENTORNO", valor: ENTORNO });
        }
        return this.xslTransform(xslManager.xslDoc, node, parameters);

    },
    transformStrOld: function (node, xslManager) {
        if (node == null)
            node = this.xmlDoc;
        if (xslManager == null)
            xslManager = this.xslManager;
        if (xslManager.processor != null) {
            var auxDoc = xslManager.processor.transformToDocument(node);
            return this.getXml(auxDoc);
        } else if ("transformNode" in node) {
            return node.transformNode(xslManager.xslDoc);
        }
    },
    /*Hace una busqueda "no modificable" de nodos*/
    selectXPath: function (expr, node) {
        if (node == null)
            node = this.xmlDoc;
        if (window.ActiveXObject) {
            return {
                list: node.selectNodes(expr),
                i: 0,
                next: function () {
                    if (this.i > this.list.length)
                        return null;
                    return this.list[this.i++];
                }
            }
        } else if (document.implementation && document.implementation.createDocument) {
            return {
                list: node.evaluate(expr, node, null, XPathResult.ANY_TYPE, null),
                next: function () {
                    return this.list.iterateNext()
                }
            }
        }
    },
    /*Hace una busqueda "modificable" de nodos*/
    selectXPathST: function (expr, node) {
        if (node == null)
            node = this.xmlDoc;
        if (window.ActiveXObject) {
            return {
                list: node.selectNodes(expr),
                getItem: function (i) {
                    return this.list[i];
                },
                length: function () {
                    return this.list.length
                }
            }
        } else if (document.implementation && document.implementation.createDocument) {
            return {
                list: node.evaluate(expr, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null),
                getItem: function (i) {
                    return this.list.snapshotItem(i);
                },
                length: function () {
                    return this.list.snapshotLength;
                }
            }
        }
    },
    selectSingleNode: function (expr, node) {
        if (node == null)
            node = this.xmlDoc;
        return this.selectXPath(expr, node).next();
    },
    loadFromString: function (string) {
        this.actXAllowed = true;
        if (window.ActiveXObject) {
            //--- Creo un nuevo objeto de la libreria MSXML2.FreeThreadedDomDocument que es la que se encarga
            //--- en Internet Explorer de 'parsear' un archivo XML.
            try {
                this.xmlDoc = new ActiveXObject("MSXML2.FreeThreadedDomDocument");
                //--- Defino la asincronizacion a false
                this.xmlDoc.async = false;
                //Cargo la cadena xml en el documento
                this.xmlDoc.loadXML(string);
            } catch (e) {
                this.actXAllowed = false;
            }
        } else if (document.implementation && document.implementation.createDocument) {
            var xmlParser, xmlDocument;
            xmlParser = new DOMParser();
            this.xmlDoc = xmlParser.parseFromString(string, 'text/xml');
        } else {
            alert('Script not supported, upgrade your browser');
        }
    },
    isDefaultViewActive: function () {
        vNode = this.selectSingleNode("//view[@activa=1]");
        return vNode.getAttribute("pordefecto") == "1";
    },
    isOriginal: function () {
        return (this.xmlDoc.documentElement.getAttribute("original") == "1")
    },
    xslTransform: function (xslStyleSheet, node, parameters) {//Function to perform the XSLT based transformation
        if (window.ActiveXObject) {//Code for internet explorer

            var XSLTCompiled = new ActiveXObject("MSXML2.XSLTemplate");
            XSLTCompiled.stylesheet = xslStyleSheet.documentElement;
            // create XSL-processor
            var XSLTProc = XSLTCompiled.createProcessor();
            XSLTProc.input = node;
            if (parameters) {
                for (j = 0; j < parameters.length; j++) {
                    par = parameters[j];
                    XSLTProc.addParameter(par.nombre, par.valor);
                }
            }
            XSLTProc.transform();

            return XSLTProc.output;
        }
        else if (document.implementation && document.implementation.createDocument) {//Code for mozilla
            XSLTProc = new XSLTProcessor();
            XSLTProc.importStylesheet(xslStyleSheet);

            if (parameters) {

            }
            for (j = 0; j < parameters.length; j++) {
                par = parameters[j];
                XSLTProc.setParameter("", par.nombre, par.valor);
            }
            TransformDoc = XSLTProc.transformToFragment(node, document);
            return this.getXml(TransformDoc);
        }
        else {
            window.alert("Browser does not support XSLT.");
            return false;
        }
    }
}


//Clase que extiende el gestor de xml con caracteristicas 
//para gestionar el envio de una accion al servidor
var tlcXmlRActionSend = Class.create();
Object.extend(Object.extend(tlcXmlRActionSend.prototype, tlcXml.prototype),{
	_parentXML : tlcXml.prototype,
	initialize: function(options){
		this._parentXML.initialize.call(this,options);
		
		//Creamos el documento y marcamos los atributos que vienen en las opciones
		this.loadFromString("<xmlAccion></xmlAccion>");
		for(j=0; j<options.attributes.length; j++){
			attr = options.attributes[j];
			this.xmlDoc.documentElement.setAttribute(attr.nombre,attr.valor);	
		}
	},
	//Agrega un nodo con la definicion de los filtros de un informe
	//El objeto que recibe como parametro es un tlcXmlReport
	agregarFiltros:function(tlcXmlR){
		//Le aņadimos el nodo de filtros al documento de comunicacion
		nfs = tlcXmlR.selectSingleNode("//filtros/defs");
		nf = nfs.cloneNode(true);
		this.xmlDoc.documentElement.appendChild(nf);
	}
});

//Clase que extiende el gestor de xml con caracteristicas 
//para gestionar la respuesta del servidor para los informes
var tlcXmlRActionResp = Class.create();
Object.extend(Object.extend(tlcXmlRActionResp.prototype, tlcXml.prototype),{
	_parentXML : tlcXml.prototype,
	initialize: function(){
		this._parentXML.initialize.call(this);
	},
	hasError: function(){
		if(this.xmlDoc.documentElement.getAttribute("error") == "1"){
			window.location.replace(wsRoot + "/index.aspx");
			return true;
		}else{
			return false;
		}
	},
	hasChange: function(){
		return this.xmlDoc.documentElement.getAttribute("cambio") == "1";
	}	
});
