function writemenu(rootindex) 
{	
	var i;
	if(rootindex != 0)
	{
		document.write("<tr ");

		if(myMenu[rootindex].target == "")
		{
			document.write("onClick=\"clicked(this)\" ");
		}
		
		document.write("id=\"" + rootindex + "\" ");
		if(myMenu[rootindex].level > 2)
		{
			document.write("style=\"display:none\"");
		}
		document.write(">");
        document.write("<td class=\"td_menu_lvl_" + myMenu[rootindex].level + "\">");
		
		if(myMenu[rootindex].target != "")
		{
			document.write("<a href=\"");
			if (myMenu[rootindex].target.length > 0)
				if (myMenu[rootindex].target.substr(0,4) != "http") document.write(MenuInitialization.PathToTheRoot);
			document.write(myMenu[rootindex].target + "\" ");
			document.write("class=\"level_" + myMenu[rootindex].level + "_link\"");
			//document.write("class=\"blue_link\"");
			if (myMenu[rootindex].target.length > 0)
				if (myMenu[rootindex].target == "http://www.inseadonline.com")
				document.write(" target='_blank'");
				document.write(">");
				
		}
		//document.write("<td class=\"td_menu_lvl_" + myMenu[rootindex].level + "\">");
		document.write(myMenu[rootindex].caption +"</a>"+"</td>");
		document.write("</tr>");
	}
	
	for(i = 0; i < myMenu[rootindex].children.length; i++)
	{
		writemenu(myMenu[rootindex].children[i]);	
	}
}

function makechildren()
{
	var i;
	myMenu[0].children = new Array();
	for(i=1; i<myMenu.length; i++)
	{
		myMenu[i].children = new Array();
		var parent = myMenu[i].parent;
		myMenu[parent].children.push(i);
	}
}

function assignlevels(index, level)
{
	myMenu[index].level = level;
	var i;
	for(i=0; i<myMenu[index].children.length; i++)
	{
		assignlevels(myMenu[index].children[i], level+1)
	}
}

function init()
{
	MenuInitialization.currentMenu = MenuInitialization.id;
	if(MenuInitialization.id != "")
	{
		var nodelist = new Array(MenuInitialization.id);
		do
		{
			var parent = myMenu[MenuInitialization.id].parent;
			nodelist.push(parent);
			MenuInitialization.id = parent;
		} while(parent!=0);


		nodelist.pop()
		while(nodelist.length > 0)
		{
			MenuInitialization.id = nodelist.pop();
			clicked(MenuInitialization);
		}
	}
}

function reinit()
{
	var i;
	for(i=0; i<myMenu[0].children.length; i++)
	{
		HideSubtreeOf(myMenu[0].children[i]);
		document.getElementById(myMenu[0].children[i]).style.display = "";
	}
}

function HideSubtreeOf(index)
{
	var i;
	if(index != 0) {
		if(myMenu[index].level > 2) {
			document.getElementById(index).style.display = "none";
		}
	}
	
	for(i=0; i<myMenu[index].children.length; i++)
	{
		HideSubtreeOf(myMenu[index].children[i]);
	}
}

function clicked(objClicked)
{	
	if ((MenuInitialization.currentMenu != "") && (MenuInitialization.currentMenu != objClicked.id))HideSubtreeOf(MenuInitialization.currentMenu)
	MenuInitialization.currentMenu = objClicked.id;
	var index = objClicked.id;
	var i;
	for(i=0; i<myMenu[index].children.length; i++)
	{
		var status = document.getElementById(myMenu[index].children[i]).style.display;
		if(status == "none")
		{
			document.getElementById(myMenu[index].children[i]).style.display = "";
		}
		else
		{
			HideSubtreeOf(myMenu[index].children[i]);
		}
	}
}

