/*
isIE = (navigator.userAgent.indexOf("MSIE") != -1) ;
isMoz = !isIE;
*/
var _BT = function (){
        var cfr = function (s){return navigator.userAgent.indexOf(s)!= -1}
        if(cfr("MSIE")) return "IE";
        if(cfr("Gecko/")) return "Moz";
        if(cfr("Opera/")) return "Op";
        return "";
        }();
var browser = {type : _BT,
isIE:(_BT=="IE"),isMoz:(_BT=="Moz"),isOpera:(_BT=="Op"),isNoIE:(_BT!="IE")}
isIE=browser.isIE; isMoz= browser.isMoz;

var jsXML = new Object();

jsXML.getXMLHttpRequest = function(){
        return (typeof(XMLHttpRequest) != 'undefined')? new XMLHttpRequest():createObject("Msxml2.XMLHTTP", "Microsoft.XMLHTTP");
}
jsXML.setPost = function (req) {
        req.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
}
jsXML.sendSync = function(url,body){
        var req = this.getXMLHttpRequest();
        method=(arguments[2])?arguments[2]:"POST";
        req.open(method, url, false);
        if (method=="POST") jsXML.setPost(req);
        req.send(body);
        return req.responseText;
}

jsXML.sendAsync = function (method,url,body,action,onfault,onwait) {
        var req = this.getXMLHttpRequest();
        var onreadystate = function (){
                if (req.readyState == 4) {
                        if (req.status == 200) {
                                if(action!=null) action(req.responseText,req);
                        }
                        else {
                                if(onfault!=null) onfault(req.statusText);
                        }
                }
                else {
                        if(onwait!=null)
                          onwait(req.readyState);
                }
        }
        req.onreadystatechange = onreadystate;
        req.open(method, url, true);
        if (method=="POST") jsXML.setPost(req);
        req.send(body);
}

jsXML.sendAsyncGet = function (url,body,action,onfault,onwait) {
        this.sendAsync("GET",url,body,action,onfault,onwait);
}

jsXML.sendAsyncPost = function (url,body,action,onfault,onwait) {
        this.sendAsync("POST",url,body,action,onfault,onwait);
}

jsXML.direct = function (url,body) {//richiesta sincrona valutata direttamente come xml
return        jsXML.parseXMLDocument(jsXML.sendSync(url,body));
}

jsXML.parseXMLDocument = function (xmlText,ns) //as XMLDocument
{
        var space = (ns)?ns:'';
        var doc = jsXML.createDOMDocument(space,'root');
        doc.loadXML (xmlText);
        return doc;
} // end parseXMLDocument

/*
jsXML.openDOMDocument
carica il documento in modo sincrono
*/
jsXML.openDOMDocument = function (url) //as XMLDocument
{
        var doc = this.createDOMDocument ("","root");
        doc.async = false;
        doc.load(url);
        return doc;
}//end openDOMDocument

jsXML.newDOMDocument = function(){
        return jsXML.createDOMDocument("","root");
}

jsXML.createDOMDocument = function(strNamespaceURI, strRootTagName) {
    var objDOM = null;
    if(browser.isNoIE)  {
        objDOM = document.implementation.createDocument(strNamespaceURI, strRootTagName, null);
                if(!objDOM.load) objDOM.addEventListener("load", _Document_onload, false);
    }
        else{
        objDOM = createObject("Msxml2.DOMDocument","Microsoft.XmlDom");
        if (strRootTagName) {
            if (strNamespaceURI) {
                objDOM.loadXML("<a0:" + strRootTagName + " xmlns:a0=\"" + strNamespaceURI + "\" />");
            } else {
                objDOM.loadXML("<" + strRootTagName + "/>");
            }
        }
    }
    return objDOM;
}

jsXML.getXML = function (node){
        return (typeof(XMLSerializer) != 'undefined')? new XMLSerializer().serializeToString(node):node.xml;
}

//-----------------------------------------------------------------
// IE Initialization
//-----------------------------------------------------------------
if (browser.isIE) {
        function createObject(){ // crea oggetto activeX tentando diversi ProgID quanti sono i parametri
                var progIdArray = arguments;
                for(var i=0;i<progIdArray.length;i++){
                        try {return new ActiveXObject(progIdArray[i])}catch (ex) {}
                }
                return null;
        }
} //End: if

//-----------------------------------------------------------------
// Mozilla/Opera Initialization
//-----------------------------------------------------------------
if (browser.isNoIE) {

    //add the loadXML() method to the Document class
    Document.prototype.loadXML = function(strXML) {
        //change the readystate
        changeReadyState(this, 1);
        //create a DOMParser
        var objDOMParser = new DOMParser();
        //create new document from string
        var objDoc = objDOMParser.parseFromString(strXML, "text/xml");
        //make sure to remove all nodes from the document
                while (this.hasChildNodes()){
                        this.removeChild(this.lastChild);
                        }
        //add the nodes from the new document
        for (var i=0; i < objDoc.childNodes.length; i++) {
            //import the node
            var objImportedNode = this.importNode(objDoc.childNodes[i], true);
            //append the child to the current document
            this.appendChild(objImportedNode);
        } //End: for

        handleOnLoad(this);
    } //End: function


        Document.prototype.transformNodeToObject = function(stylesheet, outputObject){
                var xsltProcessor = new XSLTProcessor();
                xsltProcessor.importStylesheet(stylesheet);
                var fragment = xsltProcessor.transformToFragment(this, document);
                var out = document.implementation.createDocument("", "root", null);
                for(i=0 ; i<fragment.childNodes.length ; i++) {
                        out.documentElement.appendChild (out.importNode(fragment.childNodes[i],true));
                }
                outputObject.loadXML(jsXML.getXML(out));
        }

        Document.prototype.transformNode = function(stylesheet){
        var out = document.implementation.createDocument("", "", null);
        this.transformNodeToObject(stylesheet, out);
        return jsXML.getXML(out);
    }

        Element.prototype.transformNodeToObject = function(stylesheet, outputObject){
        var oDoc = document.implementation.createDocument("", "", null);
                oDoc.loadXML(jsXML.getXML(this));
                oDoc.transformNodeToObject(stylesheet,outputObject);
    }

        Element.prototype.transformNode = function(stylesheet){
        var oDoc = document.implementation.createDocument("", "", null);
                oDoc.loadXML(jsXML.getXML(this));
                return jsXML.getXML(oDoc);
    }
        //__defineGetter__ not in Opera
        if(browser.isMoz) {
                Node.prototype.__defineGetter__("text", _Node_getText);
                Node.prototype.__defineSetter__("text", _Node_setText);

            //add the getter for the .xml attribute
            Node.prototype.__defineGetter__("xml", _Node_getXML);
    }
    //add the readystate attribute for a Document
    Document.prototype.readyState = "0";

    //save a reference to the original load() method
    Document.prototype.__load__ = Document.prototype.load;

    //create our own load() method
    Document.prototype.load = _Document_load;

    //add the onreadystatechange attribute
    Document.prototype.onreadystatechange = null;

    //add the parseError attribute
    Document.prototype.parseError = 0;
// ---- / XPATH -------------------------------------------------------
    if(browser.isMoz) {
                XMLDocument.prototype.selectNodes = function(xpathExpression, contextNode){
                        var NSResolver = this.createNSResolver(this.documentElement);
                        var oResult = this.evaluate(xpathExpression,
                            (contextNode?contextNode:this),
                            NSResolver,
                            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
                        var nodeList = [];
                        for(i=0; i< oResult.snapshotLength; i++) {
                                nodeList[nodeList.length] = oResult.snapshotItem(i);
                        }
                return nodeList;
            }
                XMLDocument.prototype.selectSingleNode = function(sExpr, contextNode){
                        var nodeList = this.selectNodes(sExpr, contextNode);
                        if (nodeList.length>0) return nodeList[0];
                }
        } // end Moz
        if (browser.isOpera) {
                Node.prototype.xml = "node.xml not supported in Opera use jsXML.getXML(node) instead!";
        }
        Element.prototype.selectNodes = function(sExpr) {
                return this.ownerDocument.selectNodes(sExpr,this);
        }

        Element.prototype.selectSingleNode = function(sExpr) {
                return this.ownerDocument.selectSingleNode(sExpr,this);
        }
// ----  XPATH / -------------------------------------------------------
} //End: if noIE
        function _Document_load(strURL) {
            //set the parseError to 0
            this.parseError = 0;
            //change the readyState
            changeReadyState(this, 1);
            //watch for errors
            try {
                //call the original load method
                this.__load__(strURL);

            } catch (objException) {
                //set the parseError attribute
                this.parseError = -9999999;
                //change the readystate
                changeReadyState(this, 4);
            } // End: try...catch
        }


        function _Document_onload() {
            //handle the onload event
            handleOnLoad(this);
        }

        function handleOnLoad(objDOMDocument) {
            //check for a parsing error
            if (!objDOMDocument.documentElement || objDOMDocument.documentElement.tagName == "parsererror")
                objDOMDocument.parseError = -9999999;

            //change the readyState
            changeReadyState(objDOMDocument, 4);
        }

        function changeReadyState(objDOMDocument, iReadyState) {
            //change the readyState
            objDOMDocument.readyState = iReadyState;
            //if there is an onreadystatechange event handler, run it
            if (objDOMDocument.onreadystatechange != null && typeof objDOMDocument.onreadystatechange == "function")
                objDOMDocument.onreadystatechange();
        }

        function _Node_getText() {
                var s = "";
                for(var i=0;i<this.childNodes.length;i++){
                        if(this.childNodes[i].nodeType == 3) s += this.childNodes[i].nodeValue; //#text
                        else if(this.childNodes[i].nodeType == 4) {
                                // s += this.childNodes[i].firstChild.nodeValue; //CDATA
                                 s += this.childNodes[i].nodeValue;
                                 }
                }
                return s;
        }

        function _Node_setText(value) {
                this.appendChild(this.ownerDocument.createTextNode(value));
        }

        function _Node_getXML() {
                return jsXML.getXML (this)
        }

//parser valori xml
function XMLGetDate (sDate) {
        if(sDate=="0.00.00") return "";
        var ws = sDate.indexOf(" ");
        return sDate.substring(0,ws);
}

function XMLGetTime (sDate) {
        if(sDate=="0.00.00") return "";
        var ws = sDate.indexOf(" ");
        sDate =sDate.substr(ws + 1);
        sDate =sDate.substring(0,sDate.lastIndexOf("."));
        sDate = sDate.replace("\.",":");
        return sDate;
}