// V1.0 - PG - 01/11/2006 - Initial Release
// V1.1 - PG - 23/03/2007 - Amended so that it will display councils own forward plan derived from the Global.asa
// V1.2 - PG - 04/04/2007 - Amended so it will only display Login item if it is set in the global.asa
// V1.3 - PG - 10/11/2008 - Amended to load menu vis HttpRequestObject to cater for Safari browser
// V1.4 - MS - 19/11/2008 - XML file changed to menu1_5.xml to include County Councillors (England only)

// Global variables
var user = "";
var site = "";
var docsub = "";
var forwardPlanLink = "";
var menuXML = 'styles/menu1_7.xml';

function loadMenu(userID, siteID, docsubID, forwardPlan) {
	
	// Set globals
	xmlDoc = '';
	user = parseFloat(userID);
	site = siteID;
	docsub = docsubID;
	forwardPlanLink = forwardPlan

	var xmlHttp=GetXmlHttpObject();
			
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) { 
			xmlDoc = xmlHttp.responseXML;
			buildMenu();
		}
	}
	xmlHttp.open("GET",menuXML,true);
	xmlHttp.send(null);
	return xmlDoc;
	
		//if (document.implementation && document.implementation.createDocument) 
		//{
		//	xmlDoc = document.implementation.createDocument("", "", null);
		//	xmlDoc.onload = buildMenu;
		//}
		//else if (window.ActiveXObject)
		//{
		//	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		//	xmlDoc.onreadystatechange = function () {
		//		if (xmlDoc.readyState == 4) buildMenu()
		//	};
		//}
		//else
		//{
		//	alert('Your browser can\'t handle this script, please contact Anite');
		//	return;
		//}
		//xmlDoc.load(menuXML);
}

// Create XML HTTP Request object
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
	  // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
			
function buildMenu() {

	// Get array of all top level link tags from XML document for main structure
	var links = xmlDoc.getElementsByTagName('MenuItem');
	
	// Fin our root <ul> element
	var ul = document.getElementById('nav');
	
	// go through the childNodes of the top link elements
	for (i=0;i<links.length;i++)
	{
		// Create the HTML elements we need
		var li = document.createElement('li');
		var a = document.createElement('a');
		
		for (j=0;j<links[i].childNodes.length;j++) {
			// Netscape considers the empty text node between the parent tag as the first child. 
			// We have to check if the nodeType of the childNode is 1 (= it's a tag) If it isn't, continue with the next child
			if (links[i].childNodes[j].nodeType != 1) continue;
			// If the item should only be shown if the user is logged in then do not show it
			if (links[i].childNodes[j].getAttribute("secure") == 1 && user == 0) continue;
			// If the item should only be shown if the user is logged out then do not show it
			if (links[i].childNodes[j].getAttribute("secure") == 0 && user > 0) continue;
			// Check if the item is English or Scottish specifc and check that against the "site" variable, if it matches or is set to "All" then show it
			if (links[i].childNodes[j].getAttribute("site") != "all" && links[i].childNodes[j].getAttribute("site") != site) continue;
			// Check if the site should be submitting documents or not
			if (links[i].childNodes[j].getAttribute("docsub") == 1 && docsub == 0) continue;

			// Set attributes of the link element before appending to li element
			a.href = links[i].childNodes[j].getAttribute("url");
			a.title = links[i].childNodes[j].getAttribute("title");
			//a.accesskey = links[i].childNodes[j].getAttribute("accesskey");
			a.setAttribute("accesskey",links[i].childNodes[j].getAttribute("accesskey"));
			a.appendChild(document.createTextNode(links[i].childNodes[j].firstChild.nodeValue));
			
			// Append to list item
			li.appendChild(a);
			li.className = links[i].childNodes[j].getAttribute("class");
			
			// Are there sub menus
			if (links[i].childNodes[j].getAttribute("sub") == 1) {
			
				// Create a new list to append to the existing <li> element
				var ulSub = document.createElement('ul');
				ulSub.setAttribute('id',links[i].childNodes[j].getAttribute("id"))

				// Append all the sub items for this top level link
				for (k=j+1;k<links[i].childNodes.length;k++) {
					if (links[i].childNodes[k].nodeType != 1) continue;
					if (links[i].childNodes[k].getAttribute("secure") == 1 && user == 0) continue;
					if (links[i].childNodes[k].getAttribute("secure") == 0 && user > 0) continue;
					if (links[i].childNodes[k].getAttribute("site") != "all" && links[i].childNodes[k].getAttribute("site") != site) continue;
					if (links[i].childNodes[k].getAttribute("docsub") == 1 && docsub == 0) continue;

					
					var liSub = document.createElement('li');
					var aSub = document.createElement('a');
					
					// Added check for councils own forward plan link, this is specified in the Global.asa file
					if (links[i].childNodes[k].getAttribute("url") == "forward.asp") {
						if (forwardPlanLink.length != 0) {
							aSub.href = forwardPlanLink;
						} else {
							aSub.href = links[i].childNodes[k].getAttribute("url");
						}
					} else {
						aSub.href = links[i].childNodes[k].getAttribute("url");
					}
					aSub.title = links[i].childNodes[k].getAttribute("title");
					aSub.setAttribute("accesskey",links[i].childNodes[k].getAttribute("accesskey"));
					aSub.appendChild(document.createTextNode(links[i].childNodes[k].firstChild.nodeValue));
					liSub.appendChild(aSub);
					liSub.className = links[i].childNodes[k].getAttribute("class");
					ulSub.appendChild(liSub);
					
					// Set class of menu so the CSS knows it's a submenu
					ulSub.className = 'sub';
					li.appendChild(ulSub);
				}
				j=k;
			}
			// Append list item to list
			ul.appendChild(li);
		}
	}

	// MSIE hack to get the :hover CSS class to work by setting a class of over which can then be used by the CSS
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById('nav');
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			} 
		}
	} 
}
