﻿function browserSupportsXSLT() {
    var support = false;
    if (document.recalc || navigator.userAgent.indexOf("Trident/5") > -1) {
        support = true;
    }
    if (typeof XMLHttpRequest != 'undefined' && typeof XSLTProcessor != 'undefined'
  && typeof DOMParser != 'undefined' && typeof XMLSerializer != 'undefined') { // Mozilla 0.9.4+, Opera 9+
        support = true;
    }
    return support;
}



function xsltTransform(xmlDoc, xslDoc, xslParams) {
    var resultado, docCache, docProcessor;
    if (!xslParams) xslParams = new Array();
    if (document.recalc || navigator.userAgent.indexOf("Trident/5") > -1) {
        var xslIE = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
        xslIE.async = false;
        xslIE.load(xslDoc);
        docCache = new ActiveXObject("MSXML2.XSLTemplate");
        docCache.stylesheet = xslIE;
        // instantiate the document processor and submit the xml document
        docProcessor = docCache.createProcessor();
        docProcessor.input = xmlDoc;
        // add parameters to the xsl document
        for (var i = 0; i < xslParams.length; i++) {
            docProcessor.addParameter(xslParams[i][0], xslParams[i][1], "");
        }
        // process the documents into html and submit to the passed div to the HMTL page
        docProcessor.transform();
        resultado = docProcessor.output;
    } else {                    // Mozilla
        var xsl = new XSLTProcessor();
        for (var i = 0; i < xslParams.length; i++) {
            xsl.setParameter(null, xslParams[i][0], xslParams[i][1]);
        }
        xsl.importStylesheet(xslDoc);
        var fragment = xsl.transformToFragment(xmlDoc, document);
        resultado = (new XMLSerializer()).serializeToString(fragment);
    }
    return resultado
}


function XMLRemoteRequest(name) {
    if (name) {
        this.name = name;
    } else {
        this.name = "xmlDoc";
    }
    this.XMLHttpComponent = this.getXMLHttpComponentInstance();
    this.XMLDOMComponent;
    this.selectSingleNode;
    this.selectNodes;
    this.selectNodes2;
    this.getOuterXML;
    this.childNodeslength;
    this.async = false;
    this.onreadystatechange = null;
}

XMLRemoteRequest.prototype.getXMLHttpComponentInstance = function() {
    var xComp = null;

    try {
        xComp = new XMLHttpRequest();

        if (navigator.appName == "Microsoft Internet Explorer") {
            this.handleRequestAsString = ieRequestAsStringHandler;
            this.handleRequestDOM = ieRequestDOMHandler;
            this.selectSingleNode = IEselectSingleNode;
            this.selectNodes = IEselectNodes;
            this.selectNodes2 = IEselectNodes2;
            this.getOuterXML = IEgetOuterXML;
            this.childNodeslength = IEchildNodeslength
        } else {
            this.handleRequestAsString = netscapeRequestAsStringHandler;
            this.handleRequestDOM = netscapeRequestDOMHandler;
            this.selectSingleNode = FFselectSingleNode;
            this.selectNodes = FFselectNodes;
            this.selectNodes2 = FFselectNodes2;
            this.getOuterXML = FFgetOuterXML;
            this.childNodeslength = FFchildNodeslength;
        }
    } catch (e) {
        try {
            //xComp = new ActiveXObject("Microsoft.XMLHTTP");
            var ids = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
            for (var i = 0; !xComp && i < ids.length; i++) {
                try {
                    xComp = new ActiveXObject(ids[i]);
                } catch (ex) {
                    xComp = false;
                }
            }
            /*@cc_on@*/
            /*@if (@_jscript_version >= 5)
            var ids = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
            for (var i = 0; !xComp && i < ids.length; i++) {
                try {
                    xComp = new ActiveXObject(ids[i]);
                } catch (ex) {
                    xComp = false;
                }
            } @end
            @*/
            this.handleRequestAsString = ieRequestAsStringHandler;
            this.handleRequestDOM = ieRequestDOMHandler;
            this.selectSingleNode = IEselectSingleNode;
            this.selectNodes = IEselectNodes;
            this.selectNodes2 = IEselectNodes2;
            this.getOuterXML = IEgetOuterXML;
            this.childNodeslength = IEchildNodeslength
        } catch (e) {
            window.alert("Versão de browser com funcionalidade insuficientes para execução desta rotina. Atualize seu browser para IE 5.5 (ou posterior) ou Netscape 6 (ou posterior).");
        }
    }

    return xComp;
}

XMLRemoteRequest.prototype.getOuterXMLFull = function() {
    return this.getOuterXML(this.XMLDOMComponent);
}

XMLRemoteRequest.prototype.putXMLString = function(xmlStr) {
    this.XMLDOMComponent = this.getXMLDOMComponentInstance(xmlStr);
}

XMLRemoteRequest.prototype.getXMLDOMComponentInstance = function(strXML) {
    var xComp = null;
    try {
        xComp = document.implementation.createDocument("", "", null);
        xComp = (new DOMParser()).parseFromString(strXML, "text/xml");
        xComp.async = false;
        xComp.readyState = 4;
    } catch (e) {
        try {
            var ids = ["Msxml2.XMLDOM", "Msxml2.XmlDom", "Msxml2.DOMDocument", "Microsoft.XMLDOM"];
            for (var i = 0; !xComp && i < ids.length; i++) {
                try {
                    xComp = new ActiveXObject(ids[i]);
                } catch (ex) {
                    xComp = false;
                }
            }
            xComp.async = false;
            xComp.loadXML(strXML);
        } catch (e) {
            window.alert("Versão de browser com funcionalidade insuficientes para execução desta rotina. Atualize seu browser para IE 5.5 (ou posterior) ou Netscape 6 (ou posterior).");
        }
    }

    return xComp;
}

XMLRemoteRequest.prototype.asignaResponseXML = function asignaResponseXML() {
    if (this.XMLHttpComponent.readyState == 4) {
        this.XMLDOMComponent = this.XMLHttpComponent.responseXML;
        if (this.onreadystatechange) {
            this.onreadystatechange();
        }
    }
}

XMLRemoteRequest.prototype.getRemoteDocument = function(urlString) {
    if (this.async) {
        this.XMLHttpComponent.onreadystatechange = new Function(this.name + ".asignaResponseXML()");
    }
    var rv = this.handleRequestDOM(this.XMLHttpComponent, urlString);
    if (!this.async && rv != null) {
        this.XMLDOMComponent = this.XMLHttpComponent.responseXML;
        this.asignaResponseXML();
    }
    return rv;
}

XMLRemoteRequest.prototype.getRemoteDocumentString = function(urlString) {
    return this.handleRequestAsString(this.XMLHttpComponent, urlString);
}

//For FireFox
FFselectSingleNode = function(xmlPath) {
    return FFselectSingleNode2(this.XMLDOMComponent, xmlPath);
}
FFselectSingleNode2 = function(node, xmlPath) {
    var primer = xmlPath;
    var resta = null;
    if (xmlPath.indexOf("/") >= 0) {
        primer = xmlPath.substring(0, xmlPath.indexOf("/"));
        resta = xmlPath.substring(xmlPath.indexOf("/") + 1);
    }
    var nomNode = primer;
    var atribut = null;
    if (primer.indexOf("@") >= 0) {
        nomNode = primer.substring(0, xmlPath.indexOf("@") - 1);
        atribut = primer.substring(primer.indexOf("@") + 1);
    }
    for (var i = 0; i < node.getElementsByTagName(nomNode).length; i++) {
        //alert(89);
        if (atribut == null) {
            if (resta != null) {
                //alert(66);
                return FFselectSingleNode2(node.getElementsByTagName(nomNode)[0], resta);
            } else {
                return node.getElementsByTagName(nomNode)[0];
            }
        } else {
            for (var j = 0; i < node.getElementsByTagName(nomNode).length; j++) {
                if (node.getElementsByTagName(nomNode)[j].getAttribute(atribut.split("=")[0]) == atribut.split("=")[1].substring(0, atribut.split("=")[1].length - 1)) {
                    //alert(4);
                    if (resta != null) {
                        return FFselectSingleNode2(node.getElementsByTagName(nomNode)[j], resta);
                    } else {
                        return node.getElementsByTagName(nomNode)[j];
                    }
                }
            }
        }
    }
    return null;
}

FFselectNodes = function(xmlPath) {
    var xmlPath2 = xmlPath;
    if (xmlPath.indexOf("/") == 0) {
        xmlPath2 = xmlPath.substring(1, xmlPath.length)
    }
    return FFselectNodes2(this.XMLDOMComponent, xmlPath2);
}
var FFllista;
FFselectNodes2 = function(node, xmlPath) {
    FFllista = new Array;
    FFselectNodes3(node, xmlPath);
    return FFllista;
}
FFselectNodes3 = function(node, xmlPath) {
    var primer = xmlPath;
    var resta = null;
    if (xmlPath.indexOf("/") >= 0) {
        primer = xmlPath.substring(0, xmlPath.indexOf("/"));
        resta = xmlPath.substring(xmlPath.indexOf("/") + 1);
    }
    var nomNode = primer;
    var atribut = null;
    if (primer.indexOf("@") >= 0) {
        nomNode = primer.substring(0, xmlPath.indexOf("@") - 1);
        atribut = primer.substring(primer.indexOf("@") + 1);
    }
    for (var i = 0; i < node.getElementsByTagName(nomNode).length; i++) {
        if (atribut == null) {
            if (resta != null) {
                FFselectNodes3(node.getElementsByTagName(nomNode)[i], resta);
            } else {
                if (node.getElementsByTagName(nomNode)[i].parentNode == node.childNodes[0].parentNode) {
                    FFllista[FFllista.length] = node.getElementsByTagName(nomNode)[i];
                }
            }
        } else {
            if (node.getElementsByTagName(nomNode)[i].getAttribute(atribut.split("=")[0]) == atribut.split("=")[1].substring(0, atribut.split("=")[1].length - 1).replace("'", "").replace("'", "")) {
                if (resta != null) {
                    FFselectNodes3(node.getElementsByTagName(nomNode)[i], resta);
                } else {
                    if (node.getElementsByTagName(nomNode)[i].parentNode == node.childNodes[0].parentNode) {
                        FFllista[FFllista.length] = node.getElementsByTagName(nomNode)[i];
                    }
                }
            }
        }
    }
    return null;
}

FFchildNodeslength = function(node) {
    return Math.floor((node.childNodes.length) / 2);
}

//For Internet Expolorer
IEselectSingleNode = function(xmlPath) {
    return this.XMLDOMComponent.selectSingleNode(xmlPath);
}
IEselectNodes = function(xmlPath) {
    return this.XMLDOMComponent.selectNodes(xmlPath);
}
IEselectNodes2 = function(node, xmlPath) {
    return node.selectNodes(xmlPath);
}
IEchildNodeslength = function(node) {
    return node.childNodes.length;
}

function FFgetOuterXML(xmlElement) {
    return (new XMLSerializer()).serializeToString(xmlElement)
}
function IEgetOuterXML(xmlElement) {
    return (xmlElement.xml)
}

// Netscape specifics
function netscapeRequestDOMHandler(xmlComp, urlString) {
    xmlComp.open("GET", urlString, this.async);
    xmlComp.send(null);
    if (xmlComp.responseXML) {
        return xmlComp.responseXML;
    }

    return null;
}

function netscapeRequestAsStringHandler(xmlComp, urlString) {
    xmlComp.open("GET", urlString, this.async);
    xmlComp.send(null);

    if (xmlComp.responseXML) {
        var dummyDoc = xmlComp.responseXML;
        var dummySerializer = new XMLSerializer();
        docString = dummySerializer.serializeToString(dummyDoc);

        return docString;
    }

    return xmlComp.responseText;
}

// IE specifics
function ieRequestDOMHandler(xmlComp, requestString) {
    //this.XMLHttpComponent.open("GET", requestString, this.async);
    xmlComp.open("GET", requestString, this.async);
    xmlComp.send();
    return xmlComp.responseXML;
}

function ieRequestAsStringHandler(xmlComp, requestString) {
    xmlComp.open("GET", requestString, this.async);
    xmlComp.send();

    return xmlComp.responseText;
}

