rex = /firefox/i;
result = navigator.userAgent.match(rex);

if (result == "Firefox" || result == "firefox" ||document.all)
{
  n=0;
  ie=1;
  fShow="visible";
  fHide="hidden";
  menuSubItemStyleClass="IESubMenuItem";
  menuHeaderStyleClass="IEMenuHeaderStyle";
}
else if (document.layers)
{
  n=1;
  ie=0;
  fShow="show";
  fHide="hide";
  menuSubItemStyleClass="NSSubMenuItem";
  menuHeaderStyleClass="NSMenuHeaderStyle";
}
else
{
  n=0;
  ie=0;
}

/* Menu bar table width */
tblWidth=0;

function MenuLayerDataObject()
{
	// menu properties container
}

function addItem(idItem, text, location, altLocation, target)
{

  var Lookup = "<!-- ITEM "+idItem+" -->";
  var mHSC = "";
  if (HTMLstr.indexOf(Lookup) != -1)
  {
    alert(idItem + " already exists");
    return;
  }

  var MENUitem = "\n<!-- ITEM "+idItem+" -->\n";

  /* New code that calculates the width of each menu heading individually depending on heading text. */
  this.menuHeaderPaneWidth = Math.ceil((text.length + 2) * this.menuHeaderPaneWidthFactor);
  
//alert (idItem + this.menuHeaderPaneWidth);

  /* This is a more robust way of making sure that menubar headers don't wrap onto a second line. */
  /* But take it out again because it causes other problems. */
  text = text.replace(/ /g, "&nbsp;");

  if (n)
  {
    tblWidth += this.menuHeaderPaneWidth;
    var index = this.subMenuLayerDataArray.length;
    this.subMenuLayerDataArray[index] = new Array();
    this.menuHeaderLayerDataArray[idItem] = new MenuLayerDataObject();
    this.menuHeaderLayerDataArray[idItem].location = location;

    MENUitem += "<td width="+this.menuHeaderPaneWidth+" height="+this.subMenuPaneHeight+">\n";
    MENUitem += "<ilayer name="+idItem+"menuheader" + " width="+this.menuHeaderPaneWidth+" visibility=show>";
    
    if (pgName == idItem)
    {
    	mHSC = 'NSmHSC';
    }
    else
    {
    	mHSC =  this.menuHeaderStyle;
    }    
    
    MENUitem += "<a title='" + text + "' href='"+location+"' class="+mHSC;
   	MENUitem += " target='" + target +"'";
    MENUitem += " onMouseOver='displaySubMenu(";
    MENUitem += '"';
    MENUitem += idItem;
    MENUitem += '"';
    MENUitem += ")' onMouseOut='setCurMenuInvisible2();' onclick='return true;'>";
    MENUitem += "&nbsp;&nbsp;";
    
	MENUitem += text;
    MENUitem += "&nbsp;";
    MENUitem += "</a>";
    MENUitem += "</ilayer>";
    MENUitem += "</td>\n";
  }
  else if (ie)
  {
    tblWidth += this.menuHeaderPaneWidth;
    MENUitem += "<td  height="+this.subMenuPaneHeight+">\n";
    

    
	/* the menuHeaderStyle on the DIV seems to affect vertical positioning of the header menubar text? */
    MENUitem += "<div width="+this.menuHeaderPaneWidth+" id='"+idItem+"' style='position:relative; "+this.menuHeaderStyle+"' "
    MENUitem += 'onMouseOver="';
    MENUitem += "displaySubMenu('"+idItem+"')";
    MENUitem += '" ';
    MENUitem += "onMouseOut='setCurMenuInvisible();'>\n";
    
    if (pgName == idItem)
    {
    	mHSC = 'IEmHSC';
    }
    else
    {
    	mHSC =  this.menuHeaderStyle;    	 
    }    
    MENUitem += "<a class=" + mHSC + " title=''";
    
	if (target)
	{
   	MENUitem += " target='" + target +"'";
	}
	
    if (location != null)
    {
      MENUitem += " href='"+location+"' onClick='hideCurrentMenu()'";
    }
    else
    {
      if (altLocation != null)
      {
        MENUitem += " href='"+altLocation+"' ";
      }
      else
      {
        MENUitem += " href='.' ";
      }
      MENUitem += "onClick='return false;'"
    }
    MENUitem += ">";
    MENUitem += "&nbsp;&nbsp;";
    
	MENUitem += text;    
    MENUitem += "&nbsp;";
    MENUitem += "</a>\n";
    MENUitem += "</div>\n";
    MENUitem += "</td>\n";
  }

  MENUitem += "<!-- END OF ITEM "+idItem+" -->\n";
  MENUitem += "<!-- MAIN_MENU -->\n";

  HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);

}

function addSubItem(idParent, text, location, target)
{
  var MENUitem = "";
  Lookup = "<!-- ITEM "+idParent+" -->";
  if (HTMLstr.indexOf(Lookup) == -1)
  {
    alert(idParent + " not found");
    return;
  }
  Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
  if (HTMLstr.indexOf(Lookup) == -1)
  {
    MENUitem += "\n";
    if (n)
    {
      MENUitem += "<layer id='"+idParent+"submenu' visibility=hide bgcolor=''";	// removing background color stops the initial one-time color flash
      MENUitem += " onMouseOut='setCurMenuInvisible();' onMouseOver='setCurMenuVisible2();'";
      MENUitem += "'>\n";
      MENUitem += "<table border="+this.subMenuPaneBorder+" bgcolor="+this.bgColor+" cellspacing=0 cellpadding=1>\n"; // added cellspacing and cellpadding
      MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
      MENUitem += "</table>\n";
      MENUitem += "</layer>\n";
    }
    if (ie)
    {
      MENUitem += "<span bgcolor=black id='"+idParent+"submenu' onMouseOut='setCurMenuInvisible();'";
      MENUitem += " onMouseOver='setCurMenuVisible();'";
      MENUitem += " style='background-color:"+this.bgColor+"; position:absolute; visibility: hidden '>\n";
      MENUitem += "<table border="+this.subMenuPaneBorder+" bgcolor="+this.bgColor+">\n";
      MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
      MENUitem += "</table>\n";
      MENUitem += "</span>\n";
    }
    MENUitem += "\n";
    MENUitem += "<!-- SUB_MENU -->\n";
    HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);
  }

  if (n)
  {
      var currentMenuIndex = this.subMenuLayerDataArray.length - 1;
      var index = this.subMenuLayerDataArray[currentMenuIndex].length;
      this.subMenuLayerDataArray[currentMenuIndex][index] = new MenuLayerDataObject();
      this.subMenuLayerDataArray[currentMenuIndex][index].text = text;
      this.subMenuLayerDataArray[currentMenuIndex][index].location = location;
  }

  // Inserted target into tag
  MENUitem = "<tr><td><a class=" + menuSubItemStyleClass + " title='' href='" + location + "' " + "target='" + target + "'>" + text + "&nbsp;</a></td></tr>\n";
  //MENUitem = "<tr><td><a style="+menuSubItemStyle+" title='' href='"+location+"'>"+text+"&nbsp;</a></td></tr>\n";
  //MENUitem = "<tr><td><a class="+menuSubItemStyleClass+" title='"+text+"' href='"+location+"'>"+text+"&nbsp;</a></td></tr>\n";
  MENUitem += Lookup;
  HTMLstr = HTMLstr.replace(Lookup, MENUitem);

}

function showMenuBar()
{
  document.writeln(HTMLstr);
}

 
/* Menubar Constructor */

function MenuBar()
{
   this.bgColor = "#FFFFF7";
  if (ie)
  {
    /* style (as well as styleClass) needed for IE DIV */
    this.menuHeaderStyle = menuHeaderStyleClass;
    //this.subMenuPaneWidth = subMenuPaneWidth;
    this.menuHeaderPaneWidthFactor = 6.5; // originally 15
    this.subMenuPaneHeight = 18;
    this.menuHeaderIndent = 1;
	this.rightMargin = 1;
  }
  else if (n)
  {
    this.subMenuLayerDataArray = new Array();
    this.menuHeaderLayerDataArray = new Array();
    this.menuHeaderStyle = menuHeaderStyleClass;
    //this.subMenuPaneWidth = subMenuPaneWidth - 9;
    this.menuHeaderPaneWidthFactor = 6.0; // originally 6
    this.subMenuPaneHeight = 18;
    this.menuHeaderIndent = 1;  // originally 1
	this.rightMargin = 1;
  }
  this.mainPaneBorder = 0;
  this.subMenuPaneBorder = 0;

   this.addItem    = addItem;
  this.addSubItem = addSubItem;
  this.showMenuBar   = showMenuBar;

   lastMenu = null;
  lastMenuHeading = null;

  HTMLstr = "<!-- MENU PANE DECLARATION BEGINS -->\n";
  HTMLstr += "\n";
  HTMLstr += "<table width="+ "''" +" cellpadding=0 height=14 cellspacing=0 bgcolor="+this.bgColor+" border="+this.mainPaneBorder+">\n";
  HTMLstr += "<tr>";
  HTMLstr += "<td width='"+this.menuHeaderIndent+"'><font face='verdana,arial,sans-serif' size='1' color=''>&nbsp;</font></td>";
  HTMLstr += "<!-- MAIN MENU STARTS -->\n";
  HTMLstr += "<!-- MAIN_MENU -->\n";
  HTMLstr += "<!-- MAIN MENU ENDS -->\n";
  HTMLstr += "<td width='"+this.rightMargin+"'><font face='verdana,arial,sans-serif' size='1' color=''>&nbsp;</font></td>";
  HTMLstr += "</tr>\n";
  HTMLstr += "</table>\n";
  HTMLstr += "\n";
  HTMLstr += "<!-- SUB MENU STARTS -->\n";
  HTMLstr += "<!-- SUB_MENU -->\n";
  HTMLstr += "<!-- SUB MENU ENDS -->\n";
  HTMLstr += "\n";
  HTMLstr += "<!-- MENU PANE DECLARATION ENDS -->\n";
}

function displaySubMenu(idMainMenu)
{
  var menu;
  var submenu;
  //alert(eval("nav" + idMainMenu));

  if (n)
  {
    idMainMenuLayerName = idMainMenu + "menuheader";
    menu = document.layers[idMainMenuLayerName];

     if (document.layers[idMainMenu+"submenu"] != null)
    {
      submenu = document.layers[idMainMenu+"submenu"];
      if (lastMenu != null && lastMenu != submenu) hideCurrentMenu();

      submenu.left = document.layers[idMainMenuLayerName].pageX + 3;
      submenu.top  = document.layers[idMainMenuLayerName].pageY +
      document.layers[idMainMenuLayerName].clip.height;
		if (eval("nav" + idMainMenu)) {
      		submenu.visibility = fShow;
		}

      submenu.onMouseOut=hideCurrentMenu;
    } else
    {
      if (lastMenu != null) hideCurrentMenu();
    }
  }
  else if (ie)
  {
     if (document.getElementById(idMainMenu+"submenu") != null)
//     if (document.all.item(idMainMenu+"submenu") != null)
    {
	menu = document.getElementById(idMainMenu);
	submenu = document.getElementById(idMainMenu+"submenu").style;

//      menu = eval(idMainMenu);
//      submenu = eval(idMainMenu+"submenu.style");

  
      var totalOffset = 0;
      var item = menu;
      do
      {
        totalOffset += eval('item.offsetLeft');
        item = eval('item.offsetParent');
      } while (item != null);
      submenu.left = totalOffset+3;

      submenu.top  = menu.style.top;
	  if (eval("nav" + idMainMenu)) {
      		submenu.visibility = fShow;
		}

    }

    if (lastMenu != null && lastMenu != submenu)
    {
      hideCurrentMenu();
    }

  }

  setCurMenuVisible();
  lastMenu = submenu;
  lastMenuHeading = menu;

}

function hideCurrentMenu()
{
  if (lastMenu != null)
  {
    lastMenu.visibility = fHide;
    lastMenu = null;
    lastMenuHeading = null;
  }
}

var timeOutID = null;
function setCurMenuVisible()
{
   if (timeOutID)
  {
    clearTimeout(timeOutID);
    timeOutID = null;
  }
}

function setCurMenuVisible2()
{
   if (timeOutID)
  {
    clearTimeout(timeOutID);
    timeOutID = null;
  }

  /* This timeout when executed should clear out any hide timeout set by leaving the menu bar. (?) */
  timeOutID = setTimeout("setCurMenuVisible()", 300)

}

function setCurMenuInvisible()
{
   if (ie ||
         (n && lastMenuHeading && lastMenu &&
          lastMenuHeading.name+"submenu" != lastMenu.name))
  {
    timeOutID = setTimeout("hideCurrentMenu()", 300)
  }
}

function setCurMenuInvisible2()
{
   if (n)
  {
    timeOutID = setTimeout("hideCurrentMenu()", 300)
  }
}
