//////////////////////////////////////////////////
// -- XML Functions:

function useActiveX()
{	return ( typeof ActiveXObject != "undefined" );	}

function useDom()
{	return ( document.implementation && document.implementation.createDocument );	}


/**
 * Selects the first node matching a given XPath expression.
 * @param oRefNode The node from which to evaluate the expression.
 * @param sXPath The XPath expression.
 * @param oXmlNs An object containing the namespaces used in the expression. Optional.
 * @return An XML node matching the expression or null if no matches found.
 */
selectNodes = function (oRefNode /*:Node*/, sXPath /*:String*/, oXmlNs /*:Object*/) {
    if (typeof XPathEvaluator != "undefined") {
    
        oXmlNs = oXmlNs || {};
        
        var nsResolver = function (sPrefix) {
    			  return oXmlNs[sPrefix];
        };
		
        var oEvaluator = new XPathEvaluator();
        var oResult = oEvaluator.evaluate(sXPath, oRefNode, nsResolver, 
                                          XPathResult.ORDERED_NODE_ITERATOR_TYPE, 
                                          null);

        var aNodes = new Array;
        
        if (oResult != null) {
            var oElement = oResult.iterateNext();
            while(oElement) {
                aNodes.push(oElement);
                oElement = oResult.iterateNext();
            }
        }
        
        return aNodes;
        
    } else if (useActiveX) {
    		if (oXmlNs) {
            var sXmlNs = "";
            for (var sProp in oXmlNs) {
                sXmlNs += "xmlns:" + sProp + "=\'" + oXmlNs[sProp] + "\' ";
            }
    			  oRefNode.ownerDocument.setProperty("SelectionNamespaces", sXmlNs);
    		}  		
        return oRefNode.selectNodes(sXPath);
    } else {
        throw new Error("No XPath engine found.");
    }

};

/**
 * Selects the first node matching a given XPath expression.
 * @param oRefNode The node from which to evaluate the expression.
 * @param sXPath The XPath expression.
 * @param oXmlNs An object containing the namespaces used in the expression.
 * @return An XML node matching the expression or null if no matches found.
 */
selectSingleNode = function (oRefNode /*:Node*/, sXPath /*:String*/, oXmlNs /*:Object*/) {
    if (typeof XPathEvaluator != "undefined") {            
	
        oXmlNs = oXmlNs || {};
        
        var nsResolver = function (sPrefix) {
    			  return oXmlNs[sPrefix];
        };
    
        var oEvaluator = new XPathEvaluator();
        var oResult = oEvaluator.evaluate(sXPath, oRefNode, nsResolver,
                                          XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    
        if (oResult != null) {
            return oResult.singleNodeValue;
        } else {
            return null;
        }              
    
    } else if (useActiveX) {
    		if (oXmlNs) 
			{
				var sXmlNs = "";
				for (var sProp in oXmlNs) 
					sXmlNs += "xmlns:" + sProp + "=\'" + oXmlNs[sProp] + "\' ";
					
    			oRefNode.ownerDocument.setProperty("SelectionNamespaces", sXmlNs);
    		}    
        return oRefNode.selectSingleNode(sXPath);
    } else {
        throw new Error("No XPath engine found.")
    }

};

// transform xml  by xslt to xml
function transformToXml(oXml,oXslt)
{
	var oResultXml = null;
	if( typeof ActiveXObject != "undefined" )
	{
		var x  = new ActiveXObject("Microsoft.XMLDOM");
		oResultXml = string2xml( oXml.transformNode(oXslt) );
		//alert( xml2string(oResultXml) )
	}else if(typeof(XSLTProcessor)!="undefined")
	{
		var oProcessor=new XSLTProcessor();
		oProcessor.importStylesheet(oXslt);
		oResultXml = oProcessor.transformToDocument(oXml);
	}	
	return oResultXml;
}

function string2xml( XMLstr )
{
	var x;
	if( window.ActiveXObject ) 
	{
		x  = new ActiveXObject("Microsoft.XMLDOM");
		x.loadXML( XMLstr );
	}
	else if ( DOMParser )
		x = (new DOMParser()).parseFromString( XMLstr, "text/xml" );
	else alert( "error - no xml parser" );
	
	return x;
}

function xml2string( oXml )
{
	if( useActiveX() )
		return (oXml ? oXml.xml : '');
	else {
		var xs = new XMLSerializer();
		return xs.serializeToString( oXml );
	} 
}

function showXml( oXml )
{
	if( useActiveX() ) alert( oXml.xml )
	else
	{
		var xs = new XMLSerializer();
		alert( xs.serializeToString( oXml ) );
	}
}


