﻿var XmlDocument = Class.create();

XmlDocument.prototype = 
{
    // Constructor taking the name of the requested WebPage
    initialize : function(piXmlDoc)
    {        
        // Set private _xmlDoc variable holding the XmlDocument 
        this._xmlDoc = piXmlDoc;
        // Set public attributes array variable
        this.attributes = this._xmlDoc.attributes;
        // Set public text variable according to current browser
        if(window.ActiveXObject)
        {
            this.text = this._xmlDoc.text;
        }
        else
        {
            this.text = this._xmlDoc.textContent;
        }
    },
    
    SetInnerText : function(piValue)
    {
        try
        {
            if(window.ActiveXObject)
            {
                this._xmlDoc.text = piValue;
            }
            else
            {
                this._xmlDoc.textContent = piValue;
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'SetInnerText', arguments, 'XMLDOCUMENT', null, e, false)
        }
    },
    
    SelectSingleNode : function(XPath)
    {
        try
        {
            if(window.ActiveXObject)
            {
                try
                {
                    return new XmlDocument(this._xmlDoc.selectSingleNode(XPath));
                }
                catch(ex)
                {
                    return null;
                }
            }
            else
            {
               try
               {
                   var xpe = new XPathEvaluator();
                   var nsResolver = xpe.createNSResolver( this._xmlDoc.ownerDocument == null ? this._xmlDoc.documentElement : this._xmlDoc.ownerDocument.documentElement);
                   var result = xpe.evaluate(XPath,this._xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                   
                   return new XmlDocument(result.singleNodeValue);
               }
               catch(ex)
               {
                return null;
               }
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'SelectSingleNode', arguments, 'XMLDOCUMENT', null, e, false)
        }
    },
    
    SelectNodes : function (XPath) 
    {
        try
        {
            if(window.ActiveXObject)
            {
                var nodeArray = this._xmlDoc.selectNodes(XPath);
                var returnArray = new Array();
                
                for(var i = 0; i < nodeArray.length; i++)
                {
                    returnArray.push(new XmlDocument(nodeArray[i]));
                }
                
                return returnArray;
            }
            else
            {
                var oEvaluator = new XPathEvaluator();
                var oResult = oEvaluator.evaluate(XPath, this._xmlDoc, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
            	  
	            var aNodes = new Array;
                
                if (oResult != null) 
                {
                    var oElement = oResult.iterateNext();
                    while(oElement) 
                    {
                        aNodes.push(new XmlDocument(oElement));
                        oElement = oResult.iterateNext();
                    }
                }
                
                return aNodes;
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'SelectNodes', arguments, 'XMLDOCUMENT', null, e, false)
        }
    }
}