/* Copyright Commcam Limited 2008. All rights reserved. (See licence/licence.txt) */

try
{
	new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
	Document.prototype.loadXML = function(strXML) 
	{
		//create a DOMParser
		var objDOMParser = new DOMParser();
		
		//create new document from string
		var objDoc = objDOMParser.parseFromString(strXML, "text/xml");
		
		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);
		 
		// insert and import nodes
		for (var i = 0; i < objDoc.childNodes.length; i++) 
		{
			this.appendChild(this.importNode(objDoc.childNodes[i], true));
		}
	}
	
	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;
	}
	
	Node.prototype.__defineGetter__("xml", _Node_getXML);
	
	Element.prototype.selectSingleNode = function(strXPath)
	{
		var objXmlDom = this.ownerDocument;
		
		if(objXmlDom.selectNodes)
		{
            return objXmlDom.selectNodes(strXPath, this);
		}
        else
        {
            throw "Method selectNodes is only supported by XML Elements";
        }
	}
	
	XMLDocument.prototype.selectNodes = function(strXPath, objXmlNode)
	{
		var oNSResolver, arrItems, arrResults;
		
		if( !objXmlNode ) 
		{ 
			objXmlNode = this; 
		}      
		
		var oNSResolver = this.createNSResolver(this.documentElement);
		var arrItems = this.evaluate(strXPath, objXmlNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		var arrResults = [];     

		for( var i = 0; i < arrItems.snapshotLength; i++)     
		{        
			arrResults[i] =  arrItems.snapshotItem(i);    
		}     
         
		return arrResults;

    };

    
	XMLDocument.prototype.selectSingleNode = function(strXPath, objXmlNode)
	{
		var objXmlNode, objNodeList;
		
        objXmlNode = objXmlNode ? objXmlNode : null;
        strXPath = "("+strXPath+")[1]";
        
        objNodeList = this.selectNodes(strXPath, objXmlNode);
        
        if(objNodeList.length > 0)
            return objNodeList[0];
        else
            return null;
    }

    Element.prototype.selectNodes = function(strXPath)
    {
        var objXmlDom = this.ownerDocument;
        
        if(objXmlDom.selectNodes)
        {
            return objXmlDom.selectNodes(strXPath, this);
        }
        else
        {
            throw "Method selectNodes is only supported by XML Elements";
        }
    }
    
    Element.prototype.selectSingleNode = function(strXPath)
    {
        var objXmlDom = this.ownerDocument;
        
        if(objXmlDom.selectSingleNode)
        {
            return objXmlDom.selectSingleNode(strXPath, this);
        }
        else
        {
            throw "Method selectNodes is only supported by XML Elements";
        }
    }

}

function xml_createXmlDom()
{
	var objXmlDom;
	
	try
	{
		objXmlDom = new ActiveXObject("Microsoft.XMLDOM");
	}
	catch (e)
	{
		objXmlDom = document.implementation.createDocument("","",null);
	}
	
	return objXmlDom;
}

function xml_createHtmlNode(objParent, strName, strText) 
{
	var objElement = null;
	objElement = document.createElement(strName);

	if (strText != null) {
		objElement.innerHTML = strText;
	}

	if (objParent != null) 
	{
		objParent.appendChild(objElement);
	}

	return objElement;
}

function xml_createNode(objParent, strName, strText) 
{
	var objElement, objXmlDom, objTextNode;
	
	objXmlDom = xml_createXmlDom();
	
	try
	{
		objElement = objXmlDom.createNode(1, strName, "");
	}
	catch (e)
	{
		objElement = objXmlDom.createElement(strName);
	}
		
	if (strText)
	{
		objTextNode = objXmlDom.createTextNode(strText);
		objElement.appendChild(objTextNode);
	}

	if (objParent != null) 
	{
		objParent.appendChild(objElement);
	}
		
	return objElement;
}
