function XmlNodes(nodes) 
{
    var _nodesSnapshot;
    var _viewNodes;
    var _length;
    
    if(window.document.evaluate)
    {
        _nodesSnapshot = nodes;
        _length = _nodesSnapshot.snapshotLength;
    } 
    else 
    {
        _viewNodes = nodes;
        _length = _viewNodes.length;
    }    
    
    this.items = items;
    this.length = _length;
    
    function items( index ) 
    {    
        function XmlNode( node ) 
        {
            
            var _text;
            var _value;
            var _snapshotItem;
            var _viewNode;
            
            if(window.document.evaluate)
            {
               _snapshotItem = node;
               _text = _snapshotItem.textContent;
               _value = _snapshotItem.stringValue;
            } 
            else 
            {
                _viewNode = node;                
                _text = _viewNode.text;
                _value = _viewNode.value;
            }   
            
            this.text = _text;
            this.value = _value;
            this.XPath = XPath;
            this.GetDocumentObject = GetDocumentObject;
            
            function GetDocumentObject() 
            {
                if( _snapshotItem )
                    return _snapshotItem;
                else
                    return _viewNode
            }
            
            function XPath( xpathExpression ) 
            {
                if(window.document.evaluate)
                {
                    
                    var xmlDoc = _snapshotItem;
                    var nsResolver = xmlDoc.ownerDocument.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
                    var nodesSnapshot = xmlDoc.ownerDocument.evaluate(xpathExpression, xmlDoc, nsResolver, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );
                    
                    if( nodesSnapshot.snapshotLength == 0 )
                        return null;
                    var nodes = new XmlNodes(nodesSnapshot);
                    return nodes;
                    
                }
                else if(window.ActiveXObject)
	            {
                    objDoc = _viewNode;
                    objDoc.ownerDocument.setProperty("SelectionLanguage", "XPath");
                    var viewNodes = objDoc.selectNodes(xpathExpression);
                    
                    if( viewNodes.length == 0 )
                        return null;
                    var nodes = new XmlNodes(viewNodes);
                    return nodes;
                } 
            } 
        }
        
        if( _nodesSnapshot ) 
        {
            return new XmlNode( _nodesSnapshot.snapshotItem(index));
        } 
        else if (_viewNodes ) 
        {
            return new XmlNode( _viewNodes(index) );
        }
    }
    
}

function ePathAJAX ()
{
    var req;
    var responseXML;
    var xsl;
    var xml;
    this.GetRequest = GetRequest;
    this.GetResponceXML = GetResponceXML;
    this.XPath = XPath;
    this.loadXMLRequest = loadXMLRequest;
    this.applyXSLTransform = applyXSLTransform;
    this.runXSLTransform = runXSLTransform;
    
    function GetRequest() 
    {
        return req;
    }
    
    function GetResponceXML() 
    {
        if(req.responseXML && req.responseXML.childNodes.length > 0 )
            return req.responseXML;
        else
            return null;
    }
    
    function XPath( xpathExpression ) 
    {
        if(window.document.evaluate)
        {
            if( req.responseXML ) 
            {
                var xmlDoc = req.responseXML;
                //alert( GetResponceXML().childNodes.length );
                var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
                var nodesSnapshot = xmlDoc.evaluate(xpathExpression, xmlDoc, nsResolver, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );
                
                if( nodesSnapshot.snapshotLength == 0 )
                        return null;
                        
                var nodes = new XmlNodes(nodesSnapshot);
                return nodes;
            }
        }
        else if(window.ActiveXObject)
	    {
            if( req.responseXML.xml && req.responseXML.xml != "" ) {
                objDoc = req.responseXML;
                objDoc.setProperty("SelectionLanguage", "XPath");
                var viewNodes = objDoc.documentElement.selectNodes(xpathExpression);
                
                if( viewNodes.length == 0 )
                        return null;
                        
                var nodes = new XmlNodes(viewNodes);
                return nodes;
            }
        }            
    }
    
    function loadXMLRequest( XMLURL, dataSend, async, onreadystatechange ) 
    {
        
        if(window.XMLHttpRequest)
        {
		    // Mozilla
		    req = new XMLHttpRequest();		
	    }
	    else if(window.ActiveXObject)
	    {		
		    try 
   	        {
    	        req = new ActiveXObject("Msxml2.XMLHTTP");
  	        } 
  	        catch(e) 
  	        {
    	        try 
    	        {
      		        req = new ActiveXObject("Microsoft.XMLHTTP");
    	        } 
    	        catch(e) 
    	        {
      		        req = false;
    	        }
	        }
	    }
	    else
	    {
		    // Browser unknown
		    alert("Browser unknown");
		    return;
	    }
	    if(req) 
        {
	        if(onreadystatechange) req.onreadystatechange = onreadystatechange;
	        req.open("POST", XMLURL, async ); 
	        req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	        req.send(dataSend);
	        responseXML = req.responseXML;
	        return req;
        }
    }

    function applyXSLTransform(XMLString, XSLURL ) 
    {
    
        if(document.implementation && document.implementation.createDocument){
		    // Mozilla
		    var xsltProcessor = new XSLTProcessor();		
		    // load the xslt file
		    var myXMLHTTPRequest = new XMLHttpRequest();
		    myXMLHTTPRequest.open("GET", XSLURL, false);
		    myXMLHTTPRequest.send(null);		
		    // get the XML document
		    xslStylesheet = myXMLHTTPRequest.responseXML;
		    xsltProcessor.importStylesheet(xslStylesheet);
    		
		    var xmlSource = XMLString; //req.responceXML;		
		    //transform
		     var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);
    		 
		     
		     tempDiv = document.createElement('div');
		     tempDiv.innerHTML = "";
		     tempDiv.appendChild(resultDocument);
		     return tempDiv.innerHTML;
	    }
	    else if(window.ActiveXObject)
	    {
		    // IE
    		
    		if( XMLString.documentElement ) {
		    // Load XML
		    xml = new ActiveXObject("MSXML2.DOMDocument");
		    xml.async = false;		
		    xml = XMLString.documentElement.selectSingleNode("/")
    		
		    // Load XSL
		    xsl = new ActiveXObject("MSXML2.DOMDocument");
		    xsl.async = false;
		    xsl.load(XSLURL)    		
    	
		    // Transform
	    //	parentElement.innerHTML = xml.transformNode(xsl);
	            return xml.transformNode(xsl);
	        } 
	        else 
	        {
	            return "";
	        }
	    }else{
		    // Browser unknown
		    alert("Browser unknown");
	    }
    }


    function runXSLTransform( XMLURL, XSLURL, parentElement )
    {
	    if(document.implementation && document.implementation.createDocument){
		    // Mozilla

		    var xsltProcessor = new XSLTProcessor();
    		
		    // load the xslt file
		    var myXMLHTTPRequest = new XMLHttpRequest();
		    myXMLHTTPRequest.open("POST", XSLURL, false);
		    myXMLHTTPRequest.send(null);
    		
		    // get the XML document
		    xslStylesheet = myXMLHTTPRequest.responseXML;
		    xsltProcessor.importStylesheet(xslStylesheet);
    		
		    // load the xml file
		    myXMLHTTPRequest = new XMLHttpRequest();
		    myXMLHTTPRequest.open("POST", XMLURL, false);
		    myXMLHTTPRequest.send(null);
    		
		    var xmlSource = myXMLHTTPRequest.responseXML;
    		
		    //transform
		     var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);
    		 
		     parentElement.innerHTML = "";
		     parentElement.appendChild(resultDocument);
	    }
	    else if(window.ActiveXObject)
	    {
		    // IE
    		
		    // Load XML
		    xml = new ActiveXObject("MSXML2.DOMDocument");
		    xml.async = false;
		    xml.load(XMLURL);
    		
		    // Load XSL
		    xsl = new ActiveXObject("MSXML2.DOMDocument");
		    xsl.async = false;
		    xsl.load(XSLURL);
    		
    	
		    // Transform
		    parentElement.innerHTML = xml.transformNode(xsl);
	    }else{
		    // Browser unknown
		    alert("Browser unknown");
	    }

    }
}