// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/


//-- Expand the dom model in firefox etc to mimic selectnodes and selectsinglenode functions

if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

}

function getXMLNodeListSerialisation(xmlNodeList) {
    var result = "";
    if (xmlNodeList) {
        for (Idx=0; Idx < xmlNodeList.length; Idx++) {
            xmlNode = xmlNodeList[Idx];
            result = result+getXMLNodeSerialisation(xmlNode);
        }
    }
    return result;
}

function getXMLNodeSerialisation(xmlNode) {
 
  var text = "hello";
  try {
    // Gecko-based browsers, Safari, Opera.
    var serializer = new XMLSerializer();
    text = serializer.serializeToString(xmlNode);
  }
  catch (e) {
    try {
      // Internet Explorer.
      text = xmlNode.xml;
    }
    catch (e) {}
  }
  return text;
}

//function getXmlNodeValue(xmlNode){
//    return try.these(
//        function() {return xmlNode.text;},
//        function() {return xmlNode.textContent;}
//        );
//}

function setSelectInnerHTML(selectObject, xmlNodeList)
{
    try {
        for (optionIdx = selectObject.options.length-1; optionIdx>=0; optionIdx--)
        {
            selectObject.options[optionIdx] = null;
//            selectObject.options.remove(optionIdx);
        }
        for (nodeIdx = 0; nodeIdx<xmlNodeList.length; nodeIdx++) {
            xmlNode = xmlNodeList[nodeIdx];
            if (xmlNode.nodeType != 3) {            //-- not text node
                newOption = new Option();
                var ValueAttribute = xmlNode.getAttribute("value");          // get this from attributes
                var SelectedAttribute = xmlNode.getAttribute("selected");
                newOptionValue = ValueAttribute;
                newOptionSelected = false;
                if (SelectedAttribute) {
                    newOptionSelected = true;
                }

                if (xmlNode.text)
                {
                    newOptionText = xmlNode.text;
                }
                else
                {
                    newOptionText = xmlNode.textContent;
                }
                
                selectObject.options.add( new Option(newOptionText, newOptionValue, newOptionSelected), nodeIdx );
                
                if (newOptionSelected)
                {
                    selectObject.options.selectedIndex = nodeIdx;
                }
            }           
        }
    } catch (e) {
        alert(e.message);
        selectObject.innerHTML = getXMLNodeListSerialisation(xmlNodeList)
    }
}

function getParent(node)
{
    var Parent = null;
    if (node.parentElement)
    {
        Parent = node.parentElement;
    }
    else 
    {
        Parent = node.parentNode;
    }
    return Parent;
}

function getChildren(node)
{
    var children = null;
    if (node.children)
    {
        children = node.children;
    }
    else if (node.childNodes)
    {
        children = node.childNodes;
    }
    return children;
}

function isTextNode(node)
{
    return node.nodeType==3;
}
