var ToolBar_Supported = false;	
if (navigator.userAgent.indexOf("MSIE")    != -1 && 
	navigator.userAgent.indexOf("Windows") != -1 && 
	navigator.appVersion.substring(0,1) > 3)
{
	ToolBar_Supported = true;
}

if (ToolBar_Supported)
{
	var newLineChar = String.fromCharCode(10);
	var char34 = String.fromCharCode(34);
	var LastMenu = "";
	var HTMLStr;
	var x = 0;
	var y = 0;
	var x2 = 0;
	var y2 = 0;
	var MenuWidth;
	var ToolbarMinWidth;
	var ToolbarMenu;
	var ToolbarBGColor;
	var ToolbarLoaded = false;
	var aDefColor = new Array(4);
	var MSFont;
	var MaxMenu = 7;
	var TotalMenu = 0;
	var arrMenuInfo = new Array(5);
	
	// Output style sheet and toolbar ID
	document.write("<SPAN ID='StartMenu' STYLE='display:none;'></SPAN>");

	// Build toolbar template
	HTMLStr = 
	"<DIV ID='idToolbar' STYLE='background-color:white;width:50%'>" +
		"<DIV ID='idBanner'    STYLE='position:absolute;top:0;left:0;height:32;width:244;overflow:hidden;vertical-align:top;'><!--BEG_BANNER--><!--END_BANNER--></DIV>" +
		"<DIV ID='idMenuCurve' STYLE='position:absolute;top:0;left:250;height:20;width:18;overflow:hidden;vertical-align:top;'><IMG SRC='/images/gcorner.gif' BORDER=0></DIV>" +
		"<DIV ID='idMenuPane'  STYLE='position:absolute;top:0;left:250;height:20;width:10;background-color:#345FB6;float:center;' NOWRAP><!--MENU_TITLES--></DIV>" + 
	"</DIV>" +
	"<SCRIPT TYPE='text/javascript'>" + 
	"   var ToolbarMenu = StartMenu;" + 
	"</SCRIPT>" + 
	"<DIV STYLE='width:100%'>" 

	// Define event handlers
	   window.onresize  = resizeToolbar;

	// Intialize global variables
	MSFont  = "xx-small Verdana";   // menu font size and face	
	
	// toolbar background color
	setToolbarBGColor("white")	
	
	// bgcolor, text font color, mouseover font color, mouseover bgcolor
	setDefaultMenuColor("#345FB6", "white", "white", "silver")	
}

// The hard-coded numbers in functions - drawToolbar() & resizeToolbar()
// correspond to the dimension of the four gif files:
//		BANNER: 35h x 250w
//		Corner:	20h x 18w

function drawToolbar()
{
	HTMLStr+= "</DIV>";
	document.write(HTMLStr);
	ToolbarLoaded = true;
	
	MenuWidth     = idMenuPane.offsetWidth;
	ToolbarMinWidth = 250 + 18 + MenuWidth;

	idToolbar.style.backgroundColor = ToolbarBGColor;
	idMenuPane.style.backgroundColor = aDefColor[0];
	resizeToolbar();

	for (i = 0; i < TotalMenu; i++) 
	{
		thisMenu = document.all(arrMenuInfo[i].IDStr);

		if (thisMenu != null)
		{
			if (arrMenuInfo[i].IDStr == LastMenu && arrMenuInfo[i].type == "R")
			{
				//Last Menu has to be absolute width
				arrMenuInfo[i].type = "A";
				arrMenuInfo[i].unit = 200;
			}
			if (arrMenuInfo[i].type == "A") 
			{
				thisMenu.style.width = arrMenuInfo[i].unit;
			}
			else 
			{
				thisMenu.style.width = Math.round(arrMenuInfo[i].width * arrMenuInfo[i].unit) + 'em';
			}
		}
	}
}

function resizeToolbar()
{
	if (ToolBar_Supported == false) return;

	w = Math.max(ToolbarMinWidth, document.body.clientWidth) - ToolbarMinWidth;
	
	idMenuCurve.style.left  = (250+w);
	idMenuPane.style.left   = (250+w+18);
	idMenuPane.style.width  = MenuWidth;
}

function setToolbarBGColor(color)
{	
	ToolbarBGColor = color;
	if (ToolbarLoaded == true)
		idToolbar.style.backgroundColor = ToolbarBGColor;
}	

function setDefaultMenuColor(bgColor, fontColor, mouseoverColor, mousebgColor)
{	
	if (bgColor   != "")	  aDefColor[0] = bgColor;
	if (fontColor != "")	  aDefColor[1] = fontColor;
	if (mouseoverColor != "") aDefColor[2] = mouseoverColor;
	if (mousebgColor != "")	  aDefColor[3] = mousebgColor;
}

function setBanner(BanGif, BanUrl, BanAltStr)
{
	SubStr = "<!--BEG_BANNER-->" + "<!--END_BANNER-->"
	SrcStr = "";
	if (BanUrl != "") SrcStr += "<A Target='_top' HREF='" + BanUrl + "'>";
	
	SrcStr += "<IMG SRC='" + BanGif + "' ALT='" + BanAltStr + "' BORDER=0>";
	
	if (BanUrl != "") SrcStr += "</A>";
	
	SrcStr = "<!--BEG_BANNER-->" + SrcStr + "<!--END_BANNER-->";
	HTMLStr = HTMLStr.replace(SubStr, SrcStr);	
}

function setSubMenuWidth(MenuIDStr, WidthType, WidthUnit)
{
	var fFound = false;
	if (TotalMenu == MaxMenu)
	{
		alert("Unable to add menu. Maximum of " + MaxMenu + " reached.");
		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 = 160;
	}
	else
	{
		arrMenuInfo[i].type = (WidthType.toUpperCase().indexOf("ABSOLUTE") != -1)? "A" : "R";
		arrMenuInfo[i].unit = WidthUnit;
	}
}

// This function creates a menuInfo object instance.
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 addMenu(MenuIDStr, MenuDisplayStr, MenuURLStr, TargetStr)
{
	tempID = "M_" + MenuIDStr;
	TargetStr = "_top";
	cFont   = MSFont;
	cColor0 = aDefColor[0];
	cColor1 = aDefColor[1];
	tagStr  = "<!--MENU_TITLES-->";
	
	MenuStr = newLineChar;
	if (LastMenu != "")
		MenuStr += "<SPAN STYLE='font:" + cFont + ";color:" + cColor1 + "'>|&nbsp;</SPAN>"; 

	MenuStr += "<A TARGET='" + TargetStr + "'" + 
				" ID='" + "T_" + tempID + "'" +
			   "   STYLE='text-decoration:none;cursor:hand;font:" + cFont + ";background-color:" + cColor0 + ";color:" + cColor1 + ";'";
	
	if (MenuURLStr != "")
	{
		MenuStr += " HREF='" + MenuURLStr + "'";
	}
	else
		MenuStr += " HREF='' onclick='window.event.returnValue=false;'";
	
	MenuStr += " onmouseout="  + char34 + "mouseMenu('out'); hideMenu();" + char34 + 
			   " onmouseover=" + char34 + "mouseMenu('over'); doMenu('"+ tempID + "');" + 
		       char34 + ">" + "&nbsp;" + MenuDisplayStr + "&nbsp;</a>";
	MenuStr += tagStr;
	
	HTMLStr = HTMLStr.replace(tagStr, MenuStr);	
	setSubMenuWidth(tempID,"default",0);
	LastMenu = tempID;
}

function addSubMenu(MenuIDStr, SubMenuStr, SubMenuURLStr)
{
	MenuIDStr = "M_" + MenuIDStr;
	TargetStr = "_top";
	cFont   = MSFont;
	cColor0 = aDefColor[0];
	cColor1 = aDefColor[1];
	
	var MenuPos = MenuIDStr.toUpperCase().indexOf("MENU");
	if (MenuPos == -1) 
	{ MenuPos = MenuIDStr.length; }
	
	InstrumentStr = MenuIDStr.substring(0 , MenuPos) + "|" + SubMenuStr;
	URLStr = SubMenuURLStr;

	var LookUpTag  = "<!--" + MenuIDStr + "-->";
	var sPos = HTMLStr.indexOf(LookUpTag);
	if (sPos <= 0)
	{
		HTMLStr += newLineChar + newLineChar +
				"<SPAN ID='" + MenuIDStr + "'" +
				" STYLE='display:none;position:absolute;width:160;background-color:" + cColor0 + 
				";padding-top:0;padding-left:0;padding-bottom:15;z-index:9;'" +
				" onmouseout='hideMenu();'>";
				
		HTMLStr += "<HR STYLE='position:absolute;left:0;top:0;color:" + cColor1 + "' SIZE=1>"
		HTMLStr += "<DIV STYLE='position:relative;left:0;top:8;'>";
	}

	TempStr = newLineChar +
				"<A ID='S_" + MenuIDStr + "'" +
				"   STYLE='text-decoration:none;cursor:hand;font:" + cFont + ";color:" + cColor1 + "'" +
				"   HREF='" + URLStr + "' TARGET='" + TargetStr + "'" +
				" onmouseout="  + char34 + "mouseMenu('out');" + char34 + 
				" onmouseover=" + char34 + "mouseMenu('over');" + char34 + ">" +
				"&nbsp;" + FormatString(SubMenuStr) + "</A><BR>" + LookUpTag;
				
	if (sPos <= 0)
		HTMLStr += TempStr + "</DIV></SPAN>";
	else
		HTMLStr = HTMLStr.replace(LookUpTag, TempStr);	

	updateSubMenuWidth(MenuIDStr);	
}

function addSubMenuLine(MenuIDStr)
{
	MenuIDStr = "M_" + MenuIDStr;
	var LookUpTag = "<!--" + MenuIDStr + "-->";
	var sPos = HTMLStr.indexOf(LookUpTag);
	if (sPos > 0)
	{
		cColor  = aDefColor[1];
		TempStr = newLineChar + "<HR STYLE='color:" + cColor + "' SIZE=1>" + LookUpTag;
		HTMLStr = HTMLStr.replace(LookUpTag, TempStr);
	}
}

function mouseMenu(id) 
{
	IsMouseout = (id.toUpperCase().indexOf("OUT") != -1);

	if (IsMouseout)	
	{
		color = aDefColor[1];
		bgcolor = aDefColor[0];
	}
	else
	{
		color = aDefColor[2];
		bgcolor = aDefColor[3];	
	}
	
	window.event.srcElement.style.color = color;
	window.event.srcElement.style.backgroundColor = bgcolor;	
}

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";
	showElement("SELECT");
	//showElement("OBJECT");	
	ToolbarMenu = thisMenu;

	// Set dropdown menu display position	
	x  = window.event.srcElement.offsetParent.offsetLeft + window.event.srcElement.offsetLeft;
	
	if (MenuIDStr == LastMenu) 
		x += (window.event.srcElement.offsetWidth - thisMenu.style.posWidth);
	
	x2 = x + window.event.srcElement.offsetWidth;
	y = idMenuPane.offsetHeight;

	thisMenu.style.left = x;
	thisMenu.style.top  = y;	
	thisMenu.style.clip = "rect(0 0 0 0)";
	thisMenu.style.display = "block";

	// 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)";
		hideElement("SELECT");
		//hideElement("OBJECT");
	}
}

function hideMenu()
{
	if (ToolbarMenu != null && ToolbarMenu != StartMenu) 
	{
		cX = event.clientX;		
		cY = event.clientY + document.body.scrollTop;
		
		// Don't hide the menu if the mouse move between the menu and submenus		
		if ((cY > (y - 50) && cY <= y2) && (cX >= (x + 20) && cX <= x2))
		{
			window.event.cancelBubble = true;
			return; 
		}
		
		ToolbarMenu.style.display = "none";
		ToolbarMenu = StartMenu;
		window.event.cancelBubble = true;
		showElement("SELECT");
		//showElement("OBJECT");		
	}
}

function hideElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;

		// Find the element's offsetTop and offsetLeft relative to the BODY tag.
		objLeft   = obj.offsetLeft;
		objTop    = obj.offsetTop;
		objParent = obj.offsetParent;
		while (objParent.tagName.toUpperCase() != "BODY")
		{
			objLeft  += objParent.offsetLeft;
			objTop   += objParent.offsetTop;
			objParent = objParent.offsetParent;
		}
		// Adjust the element's offsetTop relative to the dropdown menu
		objTop = objTop - y;

		if (x > (objLeft + obj.offsetWidth) || objLeft > (x + ToolbarMenu.offsetWidth))
			;
		else if (objTop > ToolbarMenu.offsetHeight)
			;
		else obj.style.visibility = "hidden";
	}
}

function showElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;
		obj.style.visibility = "";
	}
}

function FormatString(theString)
{
	var MinLen = 25;
	var diff = MinLen - theString.length;
	
	for (i = 0; i <= diff; i++)
		theString = theString + "&nbsp;";

	return (theString);
}
