﻿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.userAgent.toLowerCase().indexOf('safari') != -1 || navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
            this.handleRequestAsString = netscapeRequestAsStringHandler;
            this.handleRequestDOM = netscapeRequestDOMHandler;
            this.selectSingleNode = FFselectSingleNode;
            this.selectNodes = FFselectNodes;
            this.selectNodes2 = FFselectNodes2;
            this.getOuterXML = FFgetOuterXML;
            this.childNodeslength = FFchildNodeslength;
        } else {
            this.handleRequestAsString = ieRequestAsStringHandler;
            this.handleRequestDOM = ieRequestDOMHandler;
            this.selectSingleNode = IEselectSingleNode;
            this.selectNodes = IEselectNodes;
            this.selectNodes2 = IEselectNodes2;
            this.getOuterXML = IEgetOuterXML;
            this.childNodeslength = IEchildNodeslength
        }
    } 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;
    /*var s = new XMLSerializer();
    var d = this.XMLHttpComponent.responseText;
    var str = s.ToXML(d);
    alert(str);*/
    //		alert(333333333);
    try {
        xComp = document.implementation.createDocument("", "", null);
        //alert(1111111111);
        //var objRoot = xComp.createElement('root');
        //objRoot.nodeValue=String("pep");
        //xComp.appendChild(objRoot);
        //alert(FFgetOuterXML(xComp));
        xComp = (new DOMParser()).parseFromString(strXML, "text/xml");
        //xComp = this.XMLHttpComponent.responseXML;
        xComp.async = false;
        //xComp.nodeValue = strXML
        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;
                }
            }
            /*@cc_on@*/
            /*@if (@_jscript_version >= 5)
            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;
                }
            } @end
            @*/
            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) {
        //alert(this.name + ".asignaResponseXML()");
        this.XMLDOMComponent = this.XMLHttpComponent.responseXML;
        if (this.onreadystatechange) {
            this.onreadystatechange();
        }
    }
}

XMLRemoteRequest.prototype.getRemoteDocument = function(urlString) {
    //this.XMLHttpComponent.async=this.async;
    //alert(this.onreadystatechange);
    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();
    }
    //this.XMLDOMComponent = this.getXMLDOMComponentInstance(this.XMLHttpComponent.responseText)
    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 {
                //alert(55);
                return node.getElementsByTagName(nomNode)[0];
            }
        } else {
            //alert(3);
            for (var j = 0; i < node.getElementsByTagName(nomNode).length; j++) {
                //alert(5);
                /*alert(nomNode);
                alert(node.getElementsByTagName(nomNode)[j].nodeValue);
                alert(FFgetOuterXML(node.getElementsByTagName(nomNode)[j]));
                alert(node.getElementsByTagName(nomNode)[j].attributes["cod"]);
                alert(node.getElementsByTagName(nomNode)[j].attributes[atribut.split("=")[0]]);*/
                //alert(node.getElementsByTagName(nomNode)[j].attributes);
                //alert(node.getElementsByTagName(nomNode)[j].getAttribute("cod"));
                //alert(node.getElementsByTagName(nomNode)[j].getAttribute(atribut.split("=")[0]));
                //if (this.FFgetOuterXML(node.getElementsByTagName(nomNode)[j].attributes[atribut.split("=")[0]])==atribut.split("=")[1].substring(0,atribut.split("=")[1].length-1)){
                //alert(atribut.split("=")[1].substring(0,atribut.split("=")[1].length-1));
                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;

    //return this.XMLDOMComponent.firstChild;
    /*var pep = document.implementation.createDocument("", "", null);
    pep.async = false;
    pep.loadXML(xmlDoc.XMLHttpComponent.responseText);
    alert(pep.getElementsByTagName("xml"));
    //alert(pep.getElementsByTagName("xml")[0].firstChild.innerHtml);
    alert(pep.documentElement);
    alert(Node.TEXT_NODE);
    alert(Node.DOCUMENT_NODE);
    alert(Node.ELEMENT_NODE);
    alert(Node.DOCUMENT_FRAGMENT_NODE);
    alert(pep.documentElement.childNodes[0].firstChild.nodeType);
    alert((new XMLSerializer()).serializeToString(pep.documentElement.childNodes[0].firstChild));
    alert(pep.documentElement.childNodes[0].firstChild.attributes);
    alert((new XMLSerializer()).serializeToString(pep.documentElement.childNodes[0].firstChild.attributes["nom"]));
    alert((new XMLSerializer()).serializeToString(pep.documentElement.childNodes[0].firstChild.attributes[1]));
    alert((new XMLSerializer()).serializeToString(pep.documentElement.childNodes[0].firstChild.firstChild));*/
}
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 {
                //alert(node.getElementsByTagName(nomNode)[i].parentNode);
                //alert(FFgetOuterXML(node.getElementsByTagName(nomNode)[i].parentNode));
                //alert(FFgetOuterXML(node.childNodes[0].parentNode));return;
                if (node.getElementsByTagName(nomNode)[i].parentNode == node.childNodes[0].parentNode) {
                    FFllista[FFllista.length] = node.getElementsByTagName(nomNode)[i];
                }
                //FFllista.length++;
            }
        } 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 (xmlPath=="b[@niv=2]"){
                    alert("hola");
                    alert(FFgetOuterXML(node.getElementsByTagName(nomNode)[i]));
                    alert(FFgetOuterXML(node));
                    };*/
                    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;
}
