var xhttp;
function MenuInit(Section, Language, Role) {
	if(Section !='' && Language !='' && Role !='') {
		
		var xmlFile = "/Resources/" + Language + "/" + Role + "/" + Section + ".xml";
		//if(showDebug==1) prompt('hello', 'XML (' + xmlFile + ')');
			
		if (window.XMLHttpRequest) { // Mozilla - based browser 
			xhttp = new XMLHttpRequest(); 
			
			
			//hook the event handler
			xhttp.onreadystatechange = MenuHandler;
			
			//prepare the call, http method=GET, false=asynchronous call	
			xhttp.open("GET", xmlFile, true);		
			
			//finally send the call
			xhttp.send(null); 
			
		} else if (window.ActiveXObject) {  // to see if we are running in IE 
			
			xhttp = new ActiveXObject("Msxml2.XMLHTTP");
			
			//hook the event handler
			xhttp.onreadystatechange = MenuHandler;
		
			//prepare the call, http method=GET, false=asynchronous call	
			xhttp.open("GET", xmlFile, false);	
			
			//finally send the call			
			xhttp.send();   
		}
	}
}
var nodes=Array();
function MenuHandler() {		
	// This handler is called 4 times for each 
	// state change of xmlhttp
	// States are: 0 uninitialized
	//      1 loading
	//      2 loaded
	//      3 interactive
	//      4 complete
	if (xhttp.readyState==4){		
		try {
			//responseXML contains an XMLDOM object				
			if (window.XMLHttpRequest)
				nodes = xmlChildNodes(xhttp.responseXML.documentElement, "menuitem");
			else 
				nodes = xhttp.responseXML.selectNodes("//menuitem");
		}
		catch(er) {
			return;
		}
		
		//if(showDebug==1) alert('Handling XML (' + nodes.length + ' nodes)');
		if(nodes.length >0) {
			if(showTopMenu) LoadTopMenu(nodes);
			if(showSideMenu) LoadSideMenu(nodes);
		}
		
	}
}  

function LoadTopMenu(nodes) {		
	if(document.all) {
		var div = document.all['HeaderMenu'];
	}
	else {
		var div = document.getElementById('HeaderMenu');
	}
		
	div.innerHTML = "";
	var cookieCrumbList = new Array();
	
	var currMenuItem = getMenuItem(currMenuItemId, nodes);
	
	var currParentId = currMenuItemId;
	
	while(currParentId != '') {
		
		var menu = getMenuItem(currParentId, nodes);
		if(menu != null) {
			if(menu.AutoForward==true && menu.NextId != null) {
				var autoMenu = getMenuItem(menu.NextId, nodes);
				if(autoMenu != null) cookieCrumbList[cookieCrumbList.length] = autoMenu;
			}
			cookieCrumbList[cookieCrumbList.length] = menu;
			currParentId = menu.ParentId;
			
		} else {
			//alert('No Menu found for parent' + currParentId)
			currParentId = "";
		}				
	}
	
	for(var i=cookieCrumbList.length-1; i>=0; i--) {
		var isValid = true;
		
		var qryString = ((cookieCrumbList[i].PageURL != '')?cookieCrumbList[i].PageURL:currPage) + "?Action=" + cookieCrumbList[i].Action + "&MenuId=" + cookieCrumbList[i].Id + "&NextMenuId=" + cookieCrumbList[i].NextId + "&formname=" + cookieCrumbList[i].FormName;
		
		if(cookieCrumbList[i].RequiredParameters != '') {
			var arrParams = cookieCrumbList[i].RequiredParameters.split();
			for(var j=0; j<arrParams.length; j++) {
				var val = '';
				try {
					var frm = eval('frm' + currMenuItem.Action);
					val = eval('frm.' + arrParams[j] + '.value');
				}
				catch (er) {
				}
				if(val != '') 
					qryString += "&" + arrParams[j] + "=" + val;
				else 
					isValid = false;
			}
		}
		if(isValid==true) {
			//if(!document.all) alert('cookieCrumbList[i].Title = ' + cookieCrumbList[i].Title)
			div.innerHTML += "<img src=\"http://obeo.360house.com/images/obeo/Menu/Chevron_Small_1.gif\" width=5 height=6 align=\"absmiddle\" />";
			div.innerHTML += "<a class=\"TopMenuItemSmall\""+ ((!cookieCrumbList[i].IsDisabled)?" href=\"" + qryString + "\"":"") + " />" + cookieCrumbList[i].Title + "</a>";
			
		}
	} // END OF LOOP -- for(var i=cookieCrumbList.length-1; i>=0; i--)
	//alert('cookieCrumbList has ' + cookieCrumbList.length + ' elements, and has produced the following menu\n\n' + div.innerHTML);
}

function LoadMenus(nMenuItemId) {
	currMenuItemId=nMenuItemId;
	LoadSideMenu(nodes);
	LoadTopMenu(nodes);
}

function LoadSideMenu(nodes) {
	/**************************************************
	 * ********** CREATE SIDE MENU SECTION ********** *
	 **************************************************/
	if(document.all)
		document.all['SideMenu'].style.display='none';
	else
		document.getElementById('SideMenu').style.display='none';
										   
	
	var showOtherLinks = false;
	
	if(document.all) {
		var mItmDiv = document.all['SideMenuItems'];
	}
	else {
		var mItmDiv = document.getElementById('SideMenuItems');
	}
	
	mItmDiv.innerHTML = "";

	var currMenuItem = getMenuItem(currMenuItemId, nodes);
	try { 
		var frm = eval('frm' + currMenuItem.Action);
	}
	catch (er) {
		
	}
	
	var menuItemList = new Array();
	for(var i=0; i<nodes.length; i++) {
		var menu = getMenuItem(nodes[i].attributes.item(0).value, nodes);
		if(menu != null) {
			if(!menu.IsHidden && menu.ParentId==currMenuItemId && (menu.IsPartnerOnly==false && menu.IsAdminOnly==false || (menu.IsPartnerOnly && isPartner) || (menu.IsAdminOnly && isAdmin))) menuItemList[menuItemList.length] = menu;
		}
	}
	
	if(menuItemList.length>0) {	
		/**********************************************
		 * ********** LOAD THE MENU HEADER ********** *
		 **********************************************/
		if(currMenuItem.ParentId != null && currMenuItem.ParentId != "") {
			if(currMenuItem.Width != '') mItmDiv.style.width = currMenuItem.Width;
			
			var qryString = (currMenuItem.PageURL != '') ? currMenuItem.PageURL : currPage;
			qryString += (qryString.indexOf('?')<=0) ? '?' : '&';
			qryString += "Action=" + currMenuItem.Action + "&MenuId=" + currMenuItem.ParentId + "&formname=" + currMenuItem.FormName;
			
			if(currMenuItem.FormName != '' && currMenuItem.RequiredParameters != '') {
				var arrParams = currMenuItem.RequiredParameters.split();
				for(var j=0; j<arrParams.length; j++) {
					var val = '';
					try {
						val = eval('document.' + currMenuItem.FormName + '.' + arrParams[j] + '.value');
					}
					catch (er) {			
					}
					if(val != '') 
						qryString += "&" + arrParams[j] + "=" + val;				
				}
			}
			if(isValid == true) {
				mItmDiv.innerHTML += "";	
				//mItmDiv.innerHTML += "<div style=\"position: relative; float: left; height: 90px; width: 8px;\"><a href=\"" + qryString + "\"><img src=\"http://obeo.360house.com/images/obeo/Menu/Sidemenu/Back.gif\" border=0 align=absmiddle /></a></div>";
			}
		} 
		else 
		{
			
			mItmDiv.innerHTML += "";	
			//mItmDiv.innerHTML += "<div style=\"float: left; position: relative; height: 90px; width: 8px;\"><img src=\"http://obeo.360house.com/images/obeo/blank.gif\" width=8 height=90 border=0 align=absmiddle /></a></div>";	
		}
		
		/**********************************************
		 * ********** LOAD MENU ITEM LINKS ********** *
		 **********************************************/
		mItmDiv.innerHTML += "<div style=\"white-space: nowrap;\">";
		if (currMenuItem.Title != "")
			mItmDiv.innerHTML += "<div class=\"MenuItemTitle\">" + currMenuItem.Title + "</div>";
			
		if(menuItemList.length>0) {
			if(showOtherLinks) mItmDiv.innerHTML += "<div><b>Other Links:</b></div>";
			//	mItmDiv.innerHTML += "<a href=\"" + ((cookieCrumbList[i].PageURL != '')?cookieCrumbList[i].PageURL:currPage) + "?Action=" + currMenuItem.Action + "&MenuId=" + currMenuItem.Id; + "\"><img src=\"http://obeo.360house.com/images/obeo/menu/sidemenu/Back.gif\"></a>";
			
			//if(showOtherLinks) mItmDiv.innerHTML += "<b>Other Links:</b><br />";
			for(var i=0; i<menuItemList.length; i++) {
				var isValid = true;
				if(menuItemList[i].PageURL != '' && menuItemList[i].PageURL.toLowerCase().lastIndexOf('menu.cfm')<=0) {
					var qryString = (menuItemList[i].PageURL != '') ? menuItemList[i].PageURL : currPage;
					qryString += (qryString.indexOf('?')<=0) ? '?' : '&';
					qryString += "Action=" + menuItemList[i].Action + "&MenuId=" + menuItemList[i].Id + "&NextMenuId=" + menuItemList[i].NextId + "&formname=" + menuItemList[i].FormName;
				} else {
					var qryString = 'javascript:LoadMenus(\'' + menuItemList[i].Id + '\');'

				}
				if(menuItemList[i].RequiredParameters != '') {
					var arrParams = menuItemList[i].RequiredParameters.split();
					for(var j=0; j<arrParams.length; j++) {
						var val = '';
						try {
							val = eval('frm.' + arrParams[j] + '.value');
						}
						catch (er) {
							isValid = false;	
						}
						if(val != '') 
							qryString += "&" + arrParams[j] + "=" + val;
						else 
							isValid = false;	
						
					}
				}
				if(isValid == true)
					mItmDiv.innerHTML += "<div style=\"clear: all;\"><a class=\"MenuItemOffSmall\" href=\""+qryString+"\">"+menuItemList[i].Title+"</a></div>";	
				else
					mItmDiv.innerHTML += "<div style=\"clear: all;\" class=\"MenuItemDisabled\">"+menuItemList[i].Title+"</div>";	

				if(menuItemList[i].Description != "") 
					mItmDiv.innerHTML += "<div style=\"clear: all;\" class=\"small\">" + menuItemList[i].Description + "</div>";			
			}
		} 
		mItmDiv.innerHTML += "</div>";

		if(menuItemList.length>0 && mItmDiv.innerHTML.length >25) {
			if(document.all)
				document.all['SideMenu'].style.display='';
			else
				document.getElementById('SideMenu').style.display='';
		}
	}
}

function LoadSiteMap() {	
	if (xhttp.readyState==4) {
		//responseXML contains an XMLDOM object				
		if (window.XMLHttpRequest)
			nodes = xmlChildNodes(xhttp.responseXML.documentElement, "menuitem");
		else 
			nodes = xhttp.responseXML.selectNodes("//menuitem");

		if(nodes.length >0) {
			if(document.all) {
				var siteMapDiv = document.all['SiteMap'];
				var sideMenuDiv = document.all['SideMenu'];
			}
			else {
				var siteMapDiv = document.getElementById('SiteMap');
				var sideMenuDiv = document.getElementById('SideMenu');
			}
			sideMenuDiv.style.display = 'none';
			for(var i=0; i<nodes.length; i++) {
				var menu = getMenuItem(nodes[i].attributes.item(0).value, nodes);
				if(menu != null) {
					if(!menu.IsHidden) {
						var qryString = (menu.PageURL != '') ? menu.PageURL : currPage;
						qryString += (qryString.indexOf('?')<=0) ? '?' : '&';
						qryString += "Action=" + menu.Action + "&MenuId=" + menu.ParentId + "&formname=" + menu.FormName;
					
						if(menu.RequiredParameters != '') {
							var arrParams = menu.RequiredParameters.split();
							for(var j=0; j<arrParams.length; j++) {
								var val = '';
								try {
									val = eval('frm.' + arrParams[j] + '.value');
								}
								catch (er) {			
								}
								if(val != '') 
									qryString += "&" + arrParams[j] + "=" + val;				
							}
						}
						siteMapDiv.innerHTML += "<div style=\"clear: all;\"><a class=\"MenuItemOffSmall\" href=\"" + qryString + "\">" + menu.Title + "</a></div>";	
					}		
				}
			}	
			
			siteMapDiv.style.display = 'block';

		}	
	}
}
function getMenuItem(id, nodes) {
	for (var i=0; i<nodes.length; i++) {
		var attrib = xmlAttributes(nodes[i]);
		if(attrib['id']==id) {	
			var title = '';
			var description = '';
			if(typeof(attrib['title']) != 'undefined') {
				title = attrib['title'];
				if(!document.all) 
					description = nodes[i].firstChild.nodeValue;
				else
					description = nodes[i].text;
			} else {
				if(!document.all) 
					var title = nodes[i].firstChild.nodeValue;
				else
					var title = nodes[i].text;
			}
			return new MenuItem (id, title, description, attrib['icon'], attrib['action'], attrib['page'], attrib['form'], attrib['parentid'], attrib['nextid'], attrib['requiredparameters'], attrib['partneronly'], attrib['adminonly'], attrib['autoforward'], attrib['hidden'], attrib['disabled'], attrib['width'])
			}
	}
	
	return null;
}


function MenuItem (id, title, description, iconURL, action, pageURL, formName, parentId, nextId, requiredParameters, isPartnerOnly, isAdminOnly, autoForward, isHidden, isDisabled, width) {
	this.Id = (typeof(id) != 'undefined') ? id : '';
	this.Title = (typeof(title) != 'undefined') ? title : '';
	this.Description = (typeof(description) != 'undefined') ? description : '';
	this.IconURL = (typeof(iconURL) != 'undefined') ? iconURL : '';
	this.PageURL = (typeof(pageURL) != 'undefined') ? pageURL : '';
	this.Action = (typeof(action) != 'undefined') ? action : '';
	this.PageURL = (typeof(pageURL) != 'undefined') ? pageURL : '';
	this.FormName = (typeof(formName) != 'undefined') ? formName : '';
	this.ParentId = (typeof(parentId) != 'undefined') ? parentId : '';
	this.NextId = (typeof(nextId) != 'undefined') ? nextId : '';
	this.RequiredParameters = (typeof(requiredParameters) != 'undefined') ? requiredParameters : '';
	this.IsPartnerOnly = (typeof(isPartnerOnly) != 'undefined') ? isPartnerOnly : false;
	this.IsAdminOnly = (typeof(isAdminOnly) != 'undefined') ? isAdminOnly : false;
	this.AutoForward = (typeof(autoForward) != 'undefined') ? autoForward : false;
	this.IsHidden = (typeof(isHidden) != 'undefined') ? isHidden : false;
	this.IsDisabled = (typeof(isDisabled) != 'undefined') ? isDisabled : false;
	this.Width = (typeof(width) != 'undefined') ? width : '';
}
	