/*////////////////////////////////////////////////////////////////////////////////////
// Browser independent method to load a DOM XML document
////////////////////////////////////////////////////////////////////////////////////*/

/*****************************************************************************************
// Example: write in asp form: 

<script language="JavaScript" src="/SCRIPT/loadXML.js"></script>		// <--- change this link when on a different domain and other directory
<script type="text/javascript">
var sXmlTest = "updates.xml";	// Name of the Xml file to load
xmlTest = null;

function OnLoad() {

	xmlTest = LoadXMLDoc(sXmlTest);

	var criterium, nodes = null, root = xmlTest.documentElement;
	criterium = "Row" //"Row[ReleaseID='1']";
	nodes = root.selectNodes(criterium);

	if (nodes.length < 1) {
		document.getElementById('releases').innerHTML += "Currently there are no items.";
	}
	else
	{
		var sNaam;
		document.getElementById('Personen').innerHTML = "";
		for (var i=0; i<nodes.length; i++) {						
			sNaam = GetTagnameValue(nodes[i].getElementsByTagName("bestandsnaam"));
			sNaam = nodes[i].selectSingleNode("bestandsnaam").firstChild.nodeValue;
			document.getElementById('Personen').innerHTML += sNaam+"<br/>";
		}
		document.getElementById('Personen').innerHTML += "<br/><hr/>";
	}
}
</script>

<body onload="OnLoad();">
	<div id="Personen">	<!-- +++ dynamic content +++ --></div>
</body>
*****************************************************************************************/


// If FireFox then XPath is used to analyse XML documents.
if( document.implementation.hasFeature("XPath", "3.0") ) {
	if( typeof XMLDocument == "undefined" ) 	{
		XMLDocument = Document;
	}

	Element.prototype.selectNodes = function (sXPath) {
		var oEvaluator = new XPathEvaluator();
		var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

		var aNodes = new Array;

		if (oResult != null)
		{
		    var oElement = oResult.iterateNext();
		    while(oElement)
		    {
		        aNodes.push(oElement);
		        oElement = oResult.iterateNext();
		    }
		}
		return aNodes;
	};
	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;
	}
	Element.prototype.selectSingleNode = function (sXPath) {
	    var oEvaluator = new XPathEvaluator();
	    var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		if (oResult != null) {
	        return oResult.singleNodeValue;
	    }
	    else {
	        return null;
	    }
	}
}

//*---------------------------------------------------------------------------+
//  +FUNCTION+
//  +PURPOSE+
//  +PARAMETERS+         +TYPE/DESCRIPTION+
//
//  +RETURN VALUES+
//+---------------------------------------------------------------------------*/
function LoadXMLDoc(sXmlFilename) {
	var xmlDoc = null;
/*
// doesn't function in Safari / Google Chrome
	try //Internet Explorer
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e)
	{
		try //Firefox, Mozilla, Opera, etc.
		{
		    xmlDoc=document.implementation.createDocument("","",null);
		}
		catch(e)
		{
			alert(e.message);
		}
	}
	try
	{
									// Load the document asynchronously to make sure it is loaded before continuing
	  xmlDoc.async=false;
	  xmlDoc.load(sXmlFilename);
	}
	catch(e)
	{
		alert(e.message);
	}
	
	return xmlDoc;
*/

// new method, needs to be tested on all browsers
	if (window.XMLHttpRequest)
	{
		xhttp=new window.XMLHttpRequest();
	}
	else
	{
		xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhttp.open("GET",sXmlFilename,false);
	xhttp.send("");
	return xhttp.responseXML;

}

//*---------------------------------------------------------------------------+
//  +FUNCTION+  GetTagnameValue(tagObj)
//  +PURPOSE+	Get the value of an XML node.
//  +PARAMETERS+         +TYPE/DESCRIPTION+
//	tagObj				(object) parent tag object that contains the value
//  +RETURN VALUES+ The value of the tag node or an empty string if the node has no value
// 	+REMARK+ 1. The object will only contain children if the XML tag node has a value attached.
//				If the node value is empty, the there are no children and an empty string is returned.
//				ex. <Row>
//						<Node>something</node>
//					</Row>
//						--> the return value will be 'something'
//				ex. <Row>
//						<Node />
//					</Row>
//						--> the return value will be ''
//						--> Trying to read the value of this node without checking if there are children
//							in the node object causes a Javacript error.
//+---------------------------------------------------------------------------*/
function GetTagnameValue(tagObj)
{
	var sValue = "";
										// If the tag object does not exist, return an error string.
										// The string will provide an indication where the error occurs when it is used
										// to fill the related form field.
	if (tagObj[0])
	{
										// If the tag object has no children, then there's no value attached
										// in that case return an empty string
		if (tagObj[0].firstChild)
			sValue = tagObj[0].firstChild.nodeValue;
		else
			sValue = "";
	}
	else
	{
		sValue = "ERROR: Invalid XML reference:" + tagObj[0];
	}
		
	return sValue;
}
