﻿// Contains helper functions for parsing XML documents in javascript

// Returns a string indicator of the current browser type
function getBrowser() {
    // Determine the brower type
    if (window.ActiveXObject) return 'ie';
    if (document.implementation && document.implementation.createDocument) return 'moz';
}


// Creates an appropriate XMLDOM object and load the supplied XML file.
function createXMLDoc() 
{
    var xmlDoc = null;
	
	// Create the XML document according to the browser type
    if (browser=='ie') {
		xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
	}
	else if ( browser=='moz') {
		xmlDoc = document.implementation.createDocument("","",null);
	}
	
	// Turn off asynchronous loading
	xmlDoc.async = false;
	
	// Return the XML Doc
    return xmlDoc;
}


// FireFox and IE access a single node in an XML document
// differently.   This function will use  the appropriate 
// method based based on the browser it is in for getting
// a single node. 
function selectSingleNode(xmlDoc, elementPath)
{
    if(window.ActiveXObject)
    {
        return xmlDoc.selectSingleNode(elementPath);
    }
    else
    {
       var xpe = new XPathEvaluator();
       var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
       var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
       return results.singleNodeValue; 
    }
}

// Gets the inner text of an XML node.
function getNodeText(xmlNode) {
	return (xmlNode.childNodes.length > 0) ? xmlNode.childNodes[0].nodeValue : xmlNode.nodeValue;
}

// Gets the text within a CDATA section whose parent is the supplied node
function getCDATAText(xmlNode) {
	for(var i = 0; i < xmlNode.childNodes.length; i++) {
		if (xmlNode.childNodes[i].nodeName == "#cdata-section") return xmlNode.childNodes[i].nodeValue;
	}
	return "";
}