var xmlDoc;

var headTag = document.getElementsByTagName('head')[0];
var linkTag = document.createElement('link');
linkTag.type = 'text/css';
linkTag.rel = 'stylesheet';
linkTag.href = '/css/menu4.css';
linkTag.media = 'screen';
headTag.appendChild(linkTag);

function writeMenu(xmlPath)
{
  
  // code for IE
  if (window.ActiveXObject)
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load(xmlPath);
	getMenuHTML();
    
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation &&
  document.implementation.createDocument)
  {
    xmlDoc=document.implementation.createDocument("","",null);
    xmlDoc.load(xmlPath);
	xmlDoc.onload=getMenuHTML;
  }
  else
  {
    alert('Your browser cannot handle this script');
  }
}

function getMenuHTML()
{
  var html="";
  
  /*** generate main menu: start ***/
  
  //get the main menu elements
  var mainm=xmlDoc.getElementsByTagName("main");
  for(var i=0;i<mainm.length;i++)
  {
  	//get the sub menu elements on each main menu element
	var subm=mainm[i].getElementsByTagName("sub");
	//get the main menu attributes
    var mainmName=mainm[i].getAttribute("name");
	var mainmHref=mainm[i].getAttribute("href");
	
	html+="<ul><li>";
	
	//if contains sub menu, enable the sub menu function
	/*if(subm.length>0)
	{
	  html+="<ul><li onMouseOver='displaySubMenu(\"sub"+i+"\");' onMouseOut='hideSubMenu(\"sub"+i+"\");' >";
	}
	else
	{
	  html+="<ul><li>";
	}*/
	
	//add the main menu hyperlink if it has the link value
	if(subm.length>0)
	{
		html+="<a href='"+mainmHref+"' title='"+mainmName+" onFocus='displaySubMenu(\"sub"+i+"\");' onBlur='hideSubMenu(\"sub"+i+"\");' onMouseOver='displaySubMenu(\"sub"+i+"\");' onMouseOut='hideSubMenu(\"sub"+i+"\");'>"+mainmName+"</a>";
	}
	else
	{
		html+="<a href='"+mainmHref+"' title='"+mainmName+"'>"+mainmName+"</a>";
	}

    /*** generate each sub menu: start ***/
	
	//if contains sub menu, write the sub menu
	if(subm.length>0)
	{
		html+="<ul id='sub"+i+"'>";
	}
    for(var j=0;j<subm.length;j++)
    {
		var submName=subm[j].getAttribute("name");
		var submHref=subm[j].getAttribute("href");
		html+="<a href='"+mainmHref+"' title='"+mainmName+" onFocus='displaySubMenu(\"sub"+i+"\");' onBlur='hideSubMenu(\"sub"+i+"\");' onMouseOver='displaySubMenu(\"sub"+i+"\");' onMouseOut='hideSubMenu(\"sub"+i+"\");'>"+mainmName+"</a>";
	}
	if(subm.length>0)
	{
		html+="</ul>";
	}
	html+="</li></ul>";
	
	/*** generate each sub menu: end ***/
	
  }
  /*** generate main menu: end ***/
  
  document.getElementById('id_menu').innerHTML=html;
}

function displaySubMenu(subMenuId)
{
	//document.getElementById(subMenuId).style.visibility='visible';
	document.getElementById(subMenuId).style.left='165px';
}

function hideSubMenu(subMenuId)
{
	//document.getElementById(subMenuId).style.visibility='hidden';
	document.getElementById(subMenuId).style.left='-1000px';
}
