
// test if the specified character is numeric. only looks for numerals
function isnumeric(mychar)
{
	var numbers = '0123456789';	
	return ( numbers.indexOf(mychar) != -1);
}

// function to duplicate the IE xml property for firefox/mozilla browsers that dont have it
function xmlSerialize(oNode)
{
	if (oNode.xml)
		return oNode.xml;	// simply use the xml property if present
		
	var oSerializer = new XMLSerializer();
	return oSerializer.serializeToString(oNode);
}

// extract the base pagename from the complete url 
function getbasename(myurl)
{
	var basename = myurl.split('/');				// split url into array of strings by slashes
	basename = basename[basename.length-1];  		// use the last array string, which is the base pagename after the last slash
	basename = basename.replace('%20', ' ');		// replace up to 3 %20 codes with spaces
	basename = basename.replace('%20', ' ');
	basename = basename.replace('%20', ' ');
	
	// check if the basename has no period, indicating an empty basename
	if ( basename.indexOf('.') == -1)
		basename = 'index.htm';
		
	return basename.toLowerCase();
}

// add selected="yes" attribute to all nodes that are either active, or the parent of an active node
function MarkActiveNodes(xmlDoc)
{
	var pagename = getbasename(String(this.location));	// extract base pagename from complete url
	var menuitems = xmlDoc.getElementsByTagName('menuitem')		// get all menuitem elements, regardless of level
	var activemenuindex = -1;
	var activenode = null;
	
	//alert('found ' + menuitems.length + ' xml elements with "menuitem" tag');
		
	// search through all nodes in no particular order for the active node whose url pages the current page
	for (var menuindex = 0; menuindex < menuitems.length; menuindex++)
	{
		if ( menuitems[menuindex].getAttribute('url') == pagename )
			{
			activemenuindex = menuindex;
			//alert('found active node with text: ' + menuitems[menuindex].getAttribute('text') + ' and url: ' + menuitems[menuindex].getAttribute('url'));
			break;
			}
	}
	if (activemenuindex > -1)
	{
		// set the selected attribute to yes for the active node and all its parents
		activenode = menuitems[activemenuindex]
		while (activenode.parentNode)
		{
			if (activenode.getAttribute("text"))
				{
				activenode.setAttribute('selected', 'yes');
				//alert('added selected attribute to node with text: ' + activenode.getAttribute("text"));
				}
			activenode = activenode.parentNode;
		}
	}
}

function RenderMenuItemNodeAndChildren(objnode)
{
	if (!objnode)
		return;
		
	if (! objnode.getAttribute("text"))
		document.write('<ul id="nav">');
	else
		document.write('<ul>');
		
	for (var index = 0; index < objnode.childNodes.length; index++)
		{
		if (objnode.childNodes[index].nodeType != 3)
			{
			var childnode = objnode.childNodes[index];
			var childnodeurl = childnode.getAttribute("url");
			var childnodetext = childnode.getAttribute("text");
			document.write('<LI><A href="' + childnodeurl + '"');
			if (childnode.getAttribute('selected'))
				document.write(' class="selected"');
			if (childnodeurl != null && childnodeurl.toLowerCase().indexOf(".pdf") > 0 )
				document.write(' target="_blank"');
			document.write('>' + childnodetext + '</A>');
			if (childnode.hasChildNodes())
				RenderMenuItemNodeAndChildren(childnode);
			document.write('</LI>');
			}
		}
	document.write('</UL>');
}

function generatemenu()
{
var xmlDoc;
var xmlsrc = "menu.xml";

try //Internet Explorer
	{
	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	// turn off asynchronized loading, to make sure the parser fully loads the xml document,
	// then load the server-side xml document
	xmlDoc.async=false;			
	xmlDoc.load(xmlsrc);
	}
catch(e)
	{
	try //Firefox, Mozilla, Opera
		{
		xmlDoc=document.implementation.createDocument("","",null);
		// turn off asynchronized loading, to make sure the parser fully loads the xml document,
		// then load the server-side xml document
		xmlDoc.async=false;			
		xmlDoc.load(xmlsrc);
		}
	catch(e)
		{
		try	// Safari, Chrome
			{
			var xmlhttp = new window.XMLHttpRequest();
			xmlhttp.open("GET", xmlsrc, false);
			xmlhttp.send(null);
			xmlDoc = xmlhttp.responseXML;    // .documentElement;
			}
		catch(e)
			{
			// alert('error during xml object load: ' + e.message);
			return;	
			}
		}
	}
	
// mark nodes that are active, or the parent of an active
MarkActiveNodes(xmlDoc);

// get root element of the document
var rootnode = xmlDoc.documentElement;	// get root element, has 7 children in IE, 15 children in firefox, doesnt work at all in Safari
RenderMenuItemNodeAndChildren(rootnode);
		
}

// use this statement to insert the menu into a webpage: <script language='javascript'; src='menu2.js'></script>
generatemenu();
