if(typeof(isIE) == 'undefined') isIE = (navigator.userAgent.toLowerCase().indexOf("msie") != -1) ;
if(typeof(isMoz) == 'undefined')  isMoz = !isIE;
//-----------------------------------------------------------------
// IE Initialization
//-----------------------------------------------------------------
if (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 Initialization
//-----------------------------------------------------------------
if (isMoz) {
    
    //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(out.xml);
	}
	
	Document.prototype.transformNode = function(stylesheet){		
        var out = document.implementation.createDocument("", "", null);	
        this.transformNodeToObject(stylesheet, out);
        return out.xml;
    }
	
	Element.prototype.transformNodeToObject = function(stylesheet, outputObject){
        var oDoc = document.implementation.createDocument("", "", null);
		oDoc.loadXML(this.xml);		
		oDoc.transformNodeToObject(stylesheet,outputObject);
    }
	
	Element.prototype.transformNode = function(stylesheet){
        var oDoc = document.implementation.createDocument("", "", null);
		oDoc.loadXML(this.xml);
		return oDoc.xml;
    }
	
	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 -------------------------------------------------------  
    
	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;		
    } 
	Element.prototype.selectNodes = function(sExpr) {
		return this.ownerDocument.selectNodes(sExpr,this);
	}

	XMLDocument.prototype.selectSingleNode = function(sExpr, contextNode){
		var nodeList = this.selectNodes(sExpr, contextNode);
		if (nodeList.length>0) return nodeList[0];
	}
	
	Element.prototype.selectSingleNode = function(sExpr) {
		return this.ownerDocument.selectSingleNode(sExpr,this);
	}
	
// ----  XPATH / -------------------------------------------------------	
} //End: if Moz

	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() {    
	    //create a new XMLSerializer
	    var objXMLSerializer = new XMLSerializer;
	    
	    //get the XML string
	    var strXML = objXMLSerializer.serializeToString(this);
	    
	    //return the XML string
	    return strXML;
	}

//function isDefined (o) {return (typeof(o) != 'undefined')}

var jsXML = new Object();

jsXML.getXMLHttpRequest = function(){
	return (typeof(XMLHttpRequest) != 'undefined')? new XMLHttpRequest():createObject("Msxml2.XMLHTTP", "Microsoft.XMLHTTP");
}

jsXML.sendSync = function(url,body){
	var req = this.getXMLHttpRequest();
	method=(arguments[2])?arguments[2]:"POST";
	req.open(method, url, false);
	if (method=="POST") req.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");	
	req.send(body);
	return req.responseText;
}

jsXML.sendAsync = function (url,body,method,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")	req.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");	
	req.send(body);
}

jsXML.sendAsyncGet = function (url,body,action,onfault,onwait) {
	this.sendAsync(url,body,"GET",action,onfault,onwait);
}

jsXML.sendAsyncPost = function (url,body,action,onfault,onwait) {
	this.sendAsync(url,body,"POST",action,onfault,onwait);
}

jsXML.direct = function (url,body) {//richiesta sincrona valutata direttamente come xml
return	jsXML.parseXMLDocument(jsXML.sendSync(url,body));
}

/*
jsXML.parseXMLDocument
carica il documento direttamente da una stringa XML 
*/
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.createDOMDocument = function(strNamespaceURI, strRootTagName) {
    var objDOM = null;    
    if (isMoz) {    
        objDOM = document.implementation.createDocument(strNamespaceURI, strRootTagName, null);    
        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;
}


