//
var newLineChar = String.fromCharCode(10);
var char34 = String.fromCharCode(34);
var LastMenu= "";
var CurMenu = "";
var HTMLStr;
var TBLStr;
var x = 0;
var y = 0;
var x2 = 0;
var y2 = 0;
var x3 = 0;
var ToolbarMinWidth = 268;
var ToolbarMenu;
var ToolbarBGColor	= "white";	// toolbar background color
var ToolbarLoaded = false;
var aDefColor = new Array(3);
var aCurColor = new Array(3);
var Font = "font-family:Verdana; font-weight:bold; font-size:8pt; ";
var MaxMenu = 30;
var TotalMenu = 0;
var arrMenuInfo = new Array(30);

aDefColor[0] = aCurColor[0] = "#6699CC";  // bgcolor;
aDefColor[1] = aCurColor[1] = "white";	  // text font color
aDefColor[2] = aCurColor[2] = "red";      // mouseover font color

// Build toolbar template
TBLStr  = "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR STYLE='height:20;vertical-align:middle'><!--MENU_TITLES--></TR></TABLE>";
HTMLStr = 
	"<DIV ID='idToolbar'     STYLE='background-color:white;width:100%'>" +
	"<DIV ID='idRow' STYLE='position:relative;height:20;width:100%'>" +
	"<DIV ID='idMenuPane' STYLE='position:absolute;top:0;left:0;height:20;color:white;background-color:black;' NOWRAP><!--MENU_TITLES--></DIV>" +
	"</DIV>" +
	"</DIV>" +
	"<SCRIPT FOR=idToolbar EVENT=onresize>resizeToolbar();</SCRIPT>" +
	"<SCRIPT FOR=idToolbar EVENT=onmouseover>hideMenu();</SCRIPT>" +
	"<SCRIPT TYPE='text/javascript'>" + 
	"   var ToolbarMenu = StartMenu;" + 
	"</SCRIPT>" +
    "<DIV width='100%'>";

function drawToolbar()
{
    HTMLStr += "</DIV>";
	document.write(HTMLStr);
	ToolbarLoaded = true;
	idToolbar.style.backgroundColor  = ToolbarBGColor;
	idMenuPane.style.backgroundColor = aDefColor[0];
	idMenuPane.style.color           = aDefColor[1];
	resizeToolbar();
	for (i = 0; i < TotalMenu; i++) 
	{
		thisMenu = document.all(arrMenuInfo[i].IDStr);
		if (thisMenu != null)
		{
			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()
{
	w = Math.max(ToolbarMinWidth, document.body.clientWidth) - ToolbarMinWidth;
	idMenuPane.style.width = ToolbarMinWidth + w;
}

function setToolbarBGColor(color)
{	
	ToolbarBGColor = color;
	if (ToolbarLoaded == true)
		idToolbar.style.backgroundColor = ToolbarBGColor;
}	

function setMenuFont(sFont)
{	
    Font = sFont;
}

function setDefaultMenuColor(bgColor, fontColor, mouseoverColor)
{	
	if (bgColor   != "")	  aDefColor[0] = bgColor;
	if (fontColor != "")	  aDefColor[1] = fontColor;
	if (mouseoverColor != "") aDefColor[2] = mouseoverColor;
}

function setMenuColor(MenuIDStr, bgColor, fontColor, mouseoverColor)
{	
	if (ToolbarLoaded == false) return;

	// Reset previous Menu color if any
	if (CurMenu != "")
	{
		PrevID = CurMenu;
		CurMenu = "";
		setMenuColor(PrevID, aDefColor[0], aDefColor[1], aDefColor[2]);
	}

	var	id = "AM_" + MenuIDStr;
	var thisMenu = document.all(id);
	if (thisMenu != null)
	{
		CurMenu = MenuIDStr;
		aCurColor[0] = bgColor;
		aCurColor[1] = fontColor;
		aCurColor[2] = mouseoverColor;

		// Change menu color
		if (bgColor != "")
			thisMenu.style.backgroundColor = bgColor;
		if (fontColor != "")
			thisMenu.children(0).style.color = fontColor;

		// Change subMenu color
		thisMenu = document.all(MenuIDStr);
		if (thisMenu != null)
		{
			if (bgColor != "")
				thisMenu.style.backgroundColor = bgColor;
			
			if (fontColor != "")
			{
				i = 0;
				id = "AS_" + MenuIDStr;
				thisMenu = document.all.item(id,i);
				while (thisMenu != null)
				{
					thisMenu.style.color = fontColor;
					i += 1;
					thisMenu = document.all.item(id,i);
				}
			}
		}
	}
}

function setSubMenuWidth(MenuIDStr, WidthType, WidthUnit)
{
	var fFound = false;
	if (TotalMenu == MaxMenu)
	{
		alert("Unable to process 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, MenuHelpStr, MenuURLStr)
{
	tagStr  = "<!--MENU_TITLES-->";
	if (LastMenu == "") HTMLStr = HTMLStr.replace(tagStr, TBLStr);
	if (addMenu.arguments.length > 4)
		TargetStr = addMenu.arguments[4];
	else
		TargetStr = "_top";
	cStyle  = Font + "background-color:" + aDefColor[0] + ";color:" + aDefColor[1] + ";";
	if (MenuHelpStr == "") MenuHelpStr = MenuDisplayStr;
	MenuStr = newLineChar;
	MenuStr += "<TD STYLE='" + cStyle + "' ID='AM_" + MenuIDStr + "' NOWRAP>";
	MenuStr += "<A STYLE='text-decoration:none;cursor:hand; " + Font + "color:" + aDefColor[1] + ";'" +
			   "   TARGET='" + TargetStr + "'" +
			   "   TITLE=" + char34 + MenuHelpStr + char34;
	if (MenuURLStr != "")
		MenuStr += " HREF='" + MenuURLStr + "'";
	else
		MenuStr += " HREF='' onclick='window.event.returnValue=false;'";
	MenuStr += " onmouseout="  + char34 + "mouseMenu('out' ,'" + MenuIDStr + "'); hideMenu();" + char34 + 
			   " onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "'); doMenu('"+ MenuIDStr + "');" + char34 + ">" +
			   "&nbsp;" + MenuDisplayStr + "&nbsp;</a>";
	MenuStr += "&nbsp;</TD><TD STYLE='" + cStyle + "'>|</TD>";
	MenuStr += tagStr;
	HTMLStr = HTMLStr.replace(tagStr, MenuStr);	
	setSubMenuWidth(MenuIDStr,"default",0);
	LastMenu = MenuIDStr;
}

function addSubMenu(MenuIDStr, SubMenuStr, SubMenuURLStr)
{
	if (addSubMenu.arguments.length > 3)
		TargetStr = addSubMenu.arguments[3];
	else
		TargetStr = "_top";
	var MenuPos = MenuIDStr.toUpperCase().indexOf("MENU");
	if (MenuPos == -1) { MenuPos = MenuIDStr.length; }
	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:" + aDefColor[0] + ";padding-top:0;padding-left:0;padding-bottom:20;z-index:9;'" +
				" onmouseout='hideMenu();'>";
		HTMLStr += "<DIV STYLE='position:relative;left:0;top:8;'>";
	}
	TempStr = newLineChar +
				"<A ID='AS_" + MenuIDStr + "'" +
				"   STYLE='text-decoration:none;cursor:hand; " + Font + "color:" + aDefColor[1] + "'" +
				"   HREF='" + SubMenuURLStr + "' TARGET='" + TargetStr + "'" +
				" onmouseout="  + char34 + "mouseMenu('out' ,'" + MenuIDStr + "');" + char34 + 
				" onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "');" + char34 + ">" +
				"&nbsp;" + SubMenuStr + "</A><BR>" + LookUpTag;
	if (sPos <= 0)
		HTMLStr += TempStr + "</DIV></SPAN>";
	else
		HTMLStr = HTMLStr.replace(LookUpTag, TempStr);	
	updateSubMenuWidth(MenuIDStr);	
}

function addSubMenuLine(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, MenuIDStr) 
{
	IsMouseout = (id.toUpperCase().indexOf("OUT") != -1);
	if (IsMouseout)
	{
		color = aDefColor[1];
		if (MenuIDStr == CurMenu && aCurColor[1] != "") 
			color = aCurColor[1];
	}
	else
	{
		color = aDefColor[2];
		if (MenuIDStr == CurMenu && aCurColor[2] != "") 
			color = aCurColor[2];
	}
	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";
	showElement("SELECT");
	showElement("OBJECT");
	showElement("IFRAME");
	ToolbarMenu = thisMenu;
	// Set dropdown menu display position
	x  = window.event.srcElement.offsetLeft + window.event.srcElement.offsetParent.offsetLeft;
	y  = idRow.offsetHeight;
	// Get main menu width
	x2 = x + window.event.srcElement.offsetWidth;
	// Get dropdown menu width
	x3 = x + 160;
	for (i = 0; i < TotalMenu; i++)
		if (arrMenuInfo[i].IDStr == MenuIDStr)
		{
			x3 = x+ arrMenuInfo[i].unit;
			break;
		}
	thisMenu.style.top  = y;
	thisMenu.style.left = x;
	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) 
	{ 
		// Get dropdown menu height
		y2 = y + ToolbarMenu.offsetHeight;
		ToolbarMenu.style.clip = "rect(auto auto auto auto)";
		hideElement("SELECT");
		hideElement("OBJECT");
		hideElement("IFRAME");
	}
}

function hideMenu()
{
	if (ToolbarMenu != null && ToolbarMenu != StartMenu) 
	{
		var cX = event.clientX + document.body.scrollLeft;
		var cY = event.clientY + document.body.scrollTop;
		var bHideMenu = true;

		if (cY >= (y - idRow.offsetHeight+10) && cY < y)
		{
			if (cX >= (x+5) && cX <= x2) bHideMenu = false;
		}
		else if (cY >= y && cY <= y2)
		{
			if (cX > (x+5) && cX <= x3) bHideMenu = false;
		}
		if (!bHideMenu) 
		{
			window.event.cancelBubble = true;
			return; 
		}

		ToolbarMenu.style.display = "none";
		ToolbarMenu = StartMenu;
		window.event.cancelBubble = true;

		showElement("SELECT");
		showElement("OBJECT");
		showElement("IFRAME");
	}
}

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 if ((y + ToolbarMenu.offsetHeight) <= 80)
			;
		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 = "";
	}
}
