var Menubar_Supported = false;

if (navigator.userAgent.indexOf("MSIE")    != -1 && 
	navigator.userAgent.indexOf("Windows") != -1 && 
	navigator.appVersion.substring(0,1) > 3)
{
	Menubar_Supported = true;
}

if (Menubar_Supported)
{
	var newLineChar = String.fromCharCode(10);
	var char34 = String.fromCharCode(34);
	var LastMSMenu = "";
	var HTMLStr;
	var TBLStr;
	var x = 0;
	var y = 0;
	var x2 = 0;
	var y2 = 0;
	var x3 = 0;
	var MSMenuWidth;
	var ToolbarMinWidth;
	var ToolbarMenu;
	var ToolbarLoaded = false;
	var MaxMenu = 30;
	var TotalMenu = 0;
	var arrMenuInfo = new Array(30);
	
	var redirecturl = "<script language='Javascript' src='/library/include/ctredir.js'></script>";
	document.write(redirecturl);
	
	document.write("<SPAN ID='StartMenu' STYLE='display:none;'></SPAN>");

	HTMLStr = "<DIV ID='idMenuPane'  STYLE='position:relative;height:20px;width:10px;background-color:black;' NOWRAP><!--MS_MENU_TITLES--></DIV>";
	HTMLStr += 	"<SCRIPT TYPE='text/javascript'>" + 
				"   var ToolbarMenu = StartMenu;" + 
				"</SCRIPT>" + 
				"<DIV WIDTH=100%>";		
}

function drawMenubar()
{
	var userAgent = navigator.userAgent;
	var MSIEIndex = userAgent.indexOf("MSIE");
	if (userAgent.indexOf("Win")  != -1 &&
		userAgent.indexOf("MSIE") != -1 &&
		userAgent.substring((MSIEIndex + 5),(MSIEIndex + 6)) > 3)
	{
		HTMLStr += "</DIV>";
		document.write(HTMLStr);
		ToolbarLoaded = true;

		MSMenuWidth     = Math.max(idMenuPane.offsetWidth, (200+112));
		ToolbarMinWidth = (250+18) + MSMenuWidth;

		idMenuPane.style.backgroundColor  = level1MenuBgColor;

		for (i = 0; i < TotalMenu; i++) 
		{
			thisMenu = document.all(arrMenuInfo[i].IDStr);
			if (thisMenu != null)
			{
				if (arrMenuInfo[i].IDStr == LastMSMenu && arrMenuInfo[i].type == "R")
				{
					//Last MSMenu has to be absolute width
					arrMenuInfo[i].type = "A";
					arrMenuInfo[i].unit = 200;
				}
				if (arrMenuInfo[i].type == "A")
					thisMenu.style.width = arrMenuInfo[i].unit + 'px';
				else 
					thisMenu.style.width = Math.round(arrMenuInfo[i].width * arrMenuInfo[i].unit) + 'em';
			}
		}
	}
}

function setSubMenuWidth(MenuIDStr, WidthType, WidthUnit)
{
	var fFound = false;
	if (TotalMenu == MaxMenu)
	{
		return;
	}
	
	for (i = 0; i < TotalMenu; i++)
		if (arrMenuInfo[i].IDStr == MenuIDStr)
		{
			fFound = true;
			break;
		}

	if (!fFound)
	{
		arrMenuInfo[i] = new menuInfo(MenuIDStr);
		TotalMenu += 1;
	}

	if (!fFound && WidthType.toUpperCase().indexOf("DEFAULT") != -1)
	{
		arrMenuInfo[i].type = "A";
		arrMenuInfo[i].unit = 200;
	}
	else
	{
		arrMenuInfo[i].type = (WidthType.toUpperCase().indexOf("ABSOLUTE") != -1)? "A" : "R";
		arrMenuInfo[i].unit = WidthUnit;
	}
}

function menuInfo(MenuIDStr)
{
	this.IDStr = MenuIDStr;
	this.type  = "";
	this.unit  = 0;
	this.width = 0;
	this.count = 0;
}

function updateSubMenuWidth(MenuIDStr)
{
	for (i = 0; i < TotalMenu; i++)
		if (arrMenuInfo[i].IDStr == MenuIDStr)
		{
			if (arrMenuInfo[i].width < MenuIDStr.length) 
				arrMenuInfo[i].width = MenuIDStr.length;
			arrMenuInfo[i].count = arrMenuInfo[i].count + 1;
			break;
		}
}

function addLevel1Menu(MenuIDStr, MenuDisplayStr, MenuHelpStr, MenuURLStr)
{	
	TargetStr = "_top";
	tempID = "MS_" + MenuIDStr;
	addMenu(tempID, MenuDisplayStr, MenuHelpStr, MenuURLStr, TargetStr); 
	LastMSMenu = tempID;
}

function addMenu(MenuIDStr, MenuDisplayStr, MenuHelpStr, MenuURLStr, TargetStr)
{
	cFont   = level1MenuFontStyle;
	cColor0 = level1MenuBgColor;
	cColor1 = level1MenuFontColor;
	cColor2 = level1MenuFontMoverColor;
	tagStr  = "<!--MS_MENU_TITLES-->";
	
	MenuStr = newLineChar;
	MenuStr += "<A TARGET='" + TargetStr + "' TITLE='" + MenuHelpStr + "'" +
			"   ID='AM_" + MenuIDStr + "'" +
			"   STYLE='text-decoration:none;cursor:hand;font:" + cFont + ";background-color:" + cColor0 + ";color:" + cColor1 + ";'";
	if (MenuURLStr != "")
	{
		MenuStr += " HREF='" + formatURL(MenuURLStr, ("MS_" + MenuDisplayStr)) + "'";
	}
	else
		MenuStr += " HREF='' onclick='window.event.returnValue=false;'";
	MenuStr += 	" onmouseout="  + char34 + "mouseMenu('out' ,'" + MenuIDStr + "', '" + cColor1 + "'); hideMenu();" + char34 + 
				" onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "', '" + cColor2 + "'); doMenu('"+ MenuIDStr + "');" + char34 + ">" +
				"&nbsp;" + MenuDisplayStr + "&nbsp;</a>";
	MenuStr += tagStr;
	HTMLStr = HTMLStr.replace(tagStr, MenuStr);
	setSubMenuWidth(MenuIDStr,"default",0);
}

function addLevel1MenuSeparator()
{	
	cFont   = level1MenuFontStyle;
	tagStr  = "<!--MS_MENU_TITLES-->";
	
	MenuStr = "<SPAN STYLE='font:" + cFont + ";color:" + level1MenuSeparatorColor + "'>&nbsp;|&nbsp;</SPAN>"; 
	MenuStr += tagStr;
	HTMLStr = HTMLStr.replace(tagStr, MenuStr);	
}

function addLevel2Menu(MenuIDStr, SubMenuStr, SubMenuURLStr)
{	
	TargetStr = "_top";
	tempID = "MS_" + MenuIDStr;
	addSubMenu(tempID,SubMenuStr,SubMenuURLStr,TargetStr); 
}

function addSubMenu(MenuIDStr, SubMenuStr, SubMenuURLStr, TargetStr)
{
	cFont   = level2MenuFontStyle;
	cColor0 = level2MenuBgColor;
	cColor1 = level2MenuFontColor;
	cColor2 = level2MenuFontMoverColor;
	
	var MenuPos = MenuIDStr.toUpperCase().indexOf("MENU");
	if (MenuPos == -1) { MenuPos = MenuIDStr.length; }
	InstrumentStr = MenuIDStr.substring(0 , MenuPos) + "|" + SubMenuStr;
	URLStr        = formatURL(SubMenuURLStr, InstrumentStr);

	var LookUpTag  = "<!--" + MenuIDStr + "-->";
	var sPos = HTMLStr.indexOf(LookUpTag);
	if (sPos <= 0)
	{
		HTMLStr += newLineChar + newLineChar + "<SPAN ID='" + MenuIDStr + "'";
		HTMLStr += 	" STYLE='display:none;position:absolute;width:160px;background-color:" + cColor0 + ";padding-top:0px;padding-left:0px;padding-bottom:20px;z-index:9px;'";
		HTMLStr += "onmouseout='hideMenu();'>";		
		HTMLStr += "<HR  STYLE='position:absolute;left:0px;top:0px;color:" + level2MenuSeparatorColor + "' SIZE=1>";
		HTMLStr += "<DIV STYLE='position:relative;left:0px;top:8px;'>";
	}

	TempStr = newLineChar +
				"<A ID='AS_" + MenuIDStr + "'" +
				"   STYLE='text-decoration:none;cursor:hand;font:" + cFont + ";color:" + cColor1 + "'" +
				"   HREF='" + URLStr + "' TARGET='" + TargetStr + "'" +
				" onmouseout="  + char34 + "mouseMenu('out' ,'" + MenuIDStr + "', '" + cColor1 + "');" + char34 + 
				" onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "', '" + cColor2 + "');" + char34 + ">" +
				"&nbsp;" + SubMenuStr + "</A><BR>" + LookUpTag;
	if (sPos <= 0)
		HTMLStr += TempStr + "</DIV></SPAN>";
	else
		HTMLStr = HTMLStr.replace(LookUpTag, TempStr);	

	updateSubMenuWidth(MenuIDStr);	
}

function addLevel2MenuSeparator(MenuIDStr)
{	
	tempID = "MS_" + MenuIDStr;
	addSubMenuLine(tempID);
}

function addSubMenuLine(MenuIDStr)
{
	var LookUpTag = "<!--" + MenuIDStr + "-->";
	var sPos = HTMLStr.indexOf(LookUpTag);
	if (sPos > 0)
	{
		cColor  = level2MenuSeparatorColor;
		TempStr = newLineChar + "<HR STYLE='color:" + cColor + "' SIZE=1>" + LookUpTag;
		HTMLStr = HTMLStr.replace(LookUpTag, TempStr);
	}
}

function mouseMenu(id, MenuIDStr, color) 
{
	window.event.srcElement.style.color = color;
}

function doMenu(MenuIDStr) 
{
	var thisMenu = document.all(MenuIDStr);
	if (ToolbarMenu == null || thisMenu == null || thisMenu == ToolbarMenu) 
	{
		window.event.cancelBubble = true;
		return false;
	}
	// Reset dropdown menu
	window.event.cancelBubble = true;
	ToolbarMenu.style.display = "none";
	ToolbarMenu = thisMenu;

	// Set dropdown menu display position
	x  = window.event.srcElement.offsetLeft +
	 	 window.event.srcElement.offsetParent.offsetLeft;

	//if (MenuIDStr == LastMSMenu)
		//x += (window.event.srcElement.offsetWidth - thisMenu.style.posWidth);
	x2 = x + window.event.srcElement.offsetWidth;
	y  = getAbsoluteTop(idMenuPane) + (idMenuPane.offsetHeight);
		 
	thisMenu.style.top  = y;
	thisMenu.style.left = x;
	thisMenu.style.clip = "rect(0 0 0 0)";
	thisMenu.style.display = "block";
	thisMenu.style.zIndex = 102;

	// delay 2 millsecond to allow the value of ToolbarMenu.offsetHeight be set
	window.setTimeout("showMenu()", 2);
	return true;
}

function showMenu() 
{
	if (ToolbarMenu != null) 
	{ 
		y2 = y + ToolbarMenu.offsetHeight;

		ToolbarMenu.style.clip = "rect(auto auto auto auto)";
		x2 = x + ToolbarMenu.offsetWidth;
	}
}

function hideMenu()
{
	if (ToolbarMenu != null && ToolbarMenu != StartMenu) 
	{
		// Don't hide the menu if the mouse move between the menu and submenus
		cY = event.clientY + document.body.scrollTop;
        cX = event.clientX + document.body.scrollLeft;
		if (document.body.offsetWidth > x) {
			cX = x + 9;
		}
		if ( (cX >= (x+5) && cX<=x2) &&
			 (cY > (y-10) && cY <= y2))
		{
			window.event.cancelBubble = true;
			return; 
		}
		ToolbarMenu.style.display = "none";
		ToolbarMenu = StartMenu;
		window.event.cancelBubble = true;
	}
}

function formatURL(URLStr, InstrumentStr)
{
	return URLStr;
}

function getAbsoluteTop(obj) {
    var i = 0;
    while (obj != null) {
        i += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return i;
}

if (Menubar_Supported != null && Menubar_Supported == true)
{
	defineMenubar();
}