﻿// Return true if a system that supports an MSIE-ish DOM.
// False if a Firefox-ish DOM.
function IsMsieDom()
{
	var xmlCheck;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlCheck = document.implementation.createDocument('', '', null);
		return false;
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlCheck = new ActiveXObject('Msxml2.XMLDOM');
			return true;
		}
		catch (e)
		{
			try
			{
				xmlCheck = new ActiveXObject('Microsoft.XMLDOM');
				return true;
			}
			catch (e)
			{
				// Nothing works.
				return false;
			}
		}
	}
}

// Return an XML doc object loaded with the URL.
function LoadXmlDoc(url)
{
	var xmlDok;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlDok = document.implementation.createDocument('', '', null);
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlDok = new ActiveXObject('Msxml2.XMLDOM');
		}
		catch (e)
		{
			try
			{
				xmlDok = new ActiveXObject('Microsoft.XMLDOM');
			}
			catch (e)
			{
				// Browser does not support XML parsing.
				return null;
			}
		}
	}
	
	if (!xmlDok)
	{
		return null;
	}

	// Read the whole XML file. Append a random string to the URL so that the browser thinks a different
	// file is loaded each time, in order to thwart caching.
	xmlDok.async = false;
	xmlDok.load(url + "?rnd=" + Math.random(1000).toString());
	
	return xmlDok;
}

// Return the string value of the desired child node.
function XChildNodeValue(parentNode, desiredChildName)
{
	var childVal = null;
	if (parentNode && parentNode.childNodes)
	{
		for (jjj = 0; jjj < parentNode.childNodes.length; jjj++)
		{
			var newChild = parentNode.childNodes[jjj];
			if (desiredChildName == newChild.nodeName)
			{
				if (IsMsieDom())
				{
					return newChild.text;
				}
				else
				{
					return newChild.textContent
				}
			}
		}
	}
	// Nothing found
	return null;
}

// Firefox and MSIE don't support the same DOM, so we have to do some extra stuff for Firefox.
// xmldok is an XML document object. elementPath is an xpath expression for selectNodes.
// Returns any array of node objects.
function XSelectNodes(xmldok, elementPath)
{
    try
    {
	    if (window.ActiveXObject)
	    {
		    return xmldok.selectNodes(elementPath);
	    }
	    else
	    {
		    var xpe = new XPathEvaluator();
		    var nsResolver = xpe.createNSResolver((xmldok.ownerDocument == null) ? xmldok.documentElement : xmldok.ownerDocument.documentElement);
		    var results = xpe.evaluate(elementPath, xmldok, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		    var i, nodes = [];
		    for (i=0; i<results.snapshotLength; i++)
		    {
			    nodes[i] = results.snapshotItem(i);
			    nodes[i].text = nodes[i].firstChild ? nodes[i].firstChild.nodeValue : "";
		    }
		    return nodes;
	    }
	}
	catch (ex)
	{
	    return null;
	}
}

// Firefox and MSIE don't support the same DOM, so we have to do some extra stuff for Firefox.
// xmldok is an XML document object. elementPath is an xpath expression for selectSingleNode.
// Returns a node object.
function XSelectSingleNode(xmldok, elementPath)
{
    try
    {
	    if (window.ActiveXObject)
	    {
		    return xmldok.selectSingleNode(elementPath);
	    }
	    else
	    {
		    var xpe = new XPathEvaluator();
		    var nsResolver = xpe.createNSResolver((xmldok.ownerDocument == null) ? xmldok.documentElement : xmldok.ownerDocument.documentElement);
		    var results = xpe.evaluate(elementPath, xmldok, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		    return results.singleNodeValue;
	    }
	}
	catch (ex)
	{
	    return null;
	}
}

// onload event chaining:
// http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}
