
function getPagePath(){
	var siteroot='2007';
	var pagePath='';

	fullPath=(document.location).toString();
	if (document.location.protocol == "file:")
	{
		// for local testing
		siteroot = "WebContent";
	}
	else if(fullPath.lastIndexOf(siteroot)!=-1)
	{
		// if we're in a staging are such as a test directory
		//alert('testing')
		siteroot = siteroot;
	}
	else{
		//live at the site root eg:http://www.silonex.com
		siteroot = document.location.hostname;
	}
	myIndex = fullPath.lastIndexOf(siteroot);
	myPath = fullPath.substring(myIndex,fullPath.length);
	myDepth = myPath.split('/');

	for(var i=1;i<myDepth.length-1;i++)
	{
			if(myDepth[i]!= "")
			{pagePath = pagePath+"../"}
	}
	return pagePath
}

var _path = getPagePath();

var xmlDoc;


function loadMenu(){
	if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) navInit();
		};
 	}
 	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = navInit;
	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}

	xmlDoc.load(_path+"Menu2.xml");
}



function navInit(){
	var navigationArray = new Array();
	var x = xmlDoc.getElementsByTagName('TopLevelItem');

	for (i=0;i<x.length;i++)// for each rubric entry
	{
		navInfo = getItemInfo(x[i])// create an array to store the data for each rubric

		for (j=0;j<x[i].childNodes.length;j++)
		{
			if(x[i].childNodes[j].nodeName == 'SecondLevelMenu')
			{
				navInfo['cats'] = getSubMenuInfo(x[i].childNodes[j]);// a new array to hold the sub category name/description and page list
			}
		}
		navigationArray[i] = navInfo;// stack the rubric entry on the navigationArray
	}//  end for i

	createMenu(navigationArray);
}//init


function getItemInfo(xmlNode){
	var thisInfo;
	var pageInfo = new Array();

	thisInfo=xmlNode.getAttribute('id')
	pageInfo['id'] =  thisInfo.toString();// add the rubric id to the rubric entry
	thisInfo=xmlNode.getAttribute('tagtype')
	pageInfo['tag'] = thisInfo.toString() //add the type of tag
	thisInfo=xmlNode.getAttribute('class')
	pageInfo['class'] = thisInfo.toString() //add the page address
	thisInfo=xmlNode.getAttribute('address')
	pageInfo['address'] = thisInfo.toString() //add the page address
	thisInfo=xmlNode.getAttribute('linktext')
	pageInfo['text'] = thisInfo.toString() //add the page address
	pageInfo['cats'] = new Array();

	return pageInfo
}


function getCategoryInfo(xmlNode){

	var thisInfo;
	var subMenuArray = new Array();
	var infoArray = new Array();

	thisInfo = xmlNode.getAttribute('id');
	subMenuArray['id'] = thisInfo.toString();
	thisInfo = xmlNode.getAttribute('tagtype');
	subMenuArray['tag'] = thisInfo.toString();
	thisInfo = xmlNode.getAttribute('class');
	subMenuArray['class'] = thisInfo.toString();

	return subMenuArray
}


function getSubMenuInfo(xmlNode){
	var thisInfo;
	var subMenuArray = new Array();
	var infoArray = new Array();

	/*thisInfo = xmlNode.getAttribute('id');
	subMenuArray['id'] = thisInfo.toString();
	thisInfo = xmlNode.getAttribute('tagtype');
	subMenuArray['tag'] = thisInfo.toString();
	thisInfo = xmlNode.getAttribute('class');
	subMenuArray['class'] = thisInfo.toString();*/

	subMenuArray = getCategoryInfo(xmlNode)

	for(var i=0;i<xmlNode.childNodes.length;i++){
		if(xmlNode.childNodes[i].nodeName == 'SecondLevelItem')
		{
			thisInfo = getItemInfo(xmlNode.childNodes[i]);

			if(xmlNode.childNodes[i].childNodes.length>0){

				for(var j=0;j<xmlNode.childNodes[i].childNodes.length;j++){

					if(xmlNode.childNodes[i].childNodes[j].nodeName == "ThirdLevelMenu")
					{
						thisInfo['cats']=getSubSubMenuInfo(xmlNode.childNodes[i].childNodes[j]);
					}

				}
			}

			infoArray.push(thisInfo);
		}

	}

	subMenuArray['pages']=infoArray;
	return subMenuArray
}

function getSubSubMenuInfo(xmlNode){

	var thisInfo;
	var subMenuArray = new Array();
	var infoArray = new Array();

	thisInfo = xmlNode.getAttribute('id');
	subMenuArray['id'] = thisInfo.toString();
	thisInfo = xmlNode.getAttribute('tagtype');
	subMenuArray['tag'] = thisInfo.toString();
	thisInfo = xmlNode.getAttribute('class');
	subMenuArray['class'] = thisInfo.toString();

	for(var i=0;i<xmlNode.childNodes.length;i++){
		if(xmlNode.childNodes[i].nodeName == 'ThirdLevelItem')
		{
			thisInfo = getItemInfo(xmlNode.childNodes[i]);
			infoArray.push(thisInfo);
		}
	}
	subMenuArray['pages']=infoArray;
	//	alert("subMenuArray['pages'][0]['id']="+subMenuArray['pages'][0]['id']	)
	return subMenuArray
}

function createMenu(array){

	var myHTML = ""; // main HTML string
	myHTML = myHTML +'<ul id="MenuMain">'; // open the top level list
	for(i=0;i<array.length;i++){
		MenuItem = makeItem(array[i]);// add the top level items
		myHTML = myHTML + MenuItem; //add the menu item to the main HTML sring;
	}

	myHTML = myHTML +"</ul>"; //close the top level list
	document.getElementById('NavWrapper').innerHTML = myHTML;

	for(i=0;i<array.length;i++){
		if(array[i]['cats']['id'] != undefined){
			document.getElementById(array[i]['id']).innerHTML = document.getElementById(array[i]['id']).innerHTML + makeList(array[i]['cats']);

			//alert('subMenuArray['pages'] length='+subMenuArray['pages'].length)
			for(var j=0;j<array[i]['cats']['pages'].length;j++)
			{
				var jref = 	array[i]['cats']['pages'][j];
				document.getElementById(jref['id']).innerHTML = document.getElementById(jref['id']).innerHTML + makeList(jref['cats']);
				//alert( jref['cats']['id'])
			}
		}

	}
	//alert(document.getElementById('NavWrapper').innerHTML)
}

function makeList(arrayEntry){
	var item=""

	if(arrayEntry['id'] != undefined){
	 	item ="<" + arrayEntry['tag'] +" id='"+ arrayEntry['id'] +"' class='"+ arrayEntry['class']+"'>";//set the tag
	 //	alert(arrayEntry['id'])
	 	for(var i=0;i<arrayEntry['pages'].length;i++){
	 		item = item +makeItem(arrayEntry['pages'][i]);
	 	}
	 	item = item +"</ul>"
	 }
	return item
}

function makeItem(arrayEntry){

	var _thelink;
	if(arrayEntry['address'] == 'null' || arrayEntry['address'] == undefined ){_thelink = '#'}
	else{_thelink = _path+arrayEntry['address']}



	var item ="<" + arrayEntry['tag'] +" id='"+ arrayEntry['id'] +"' class='"+ arrayEntry['class']+"'>";//set the tag
	if(arrayEntry['class']!='listheading'){
		item = item + "<a href='"+ _thelink + "'>";//set the anchor
		item = item + arrayEntry['text'];// set the text
		item = item + "</a>"; //close tha anchor
	}
	else
	{
		item = item + arrayEntry['text'];// set the text
	}
	item = item +"</"+arrayEntry['tag']+">";//close the tag
	return item
}
