/*  Menu.js  
 *  This java script file will define the functions for displaying a menu
 *  for any of the Plus Web products.  The .htm's will provide the menu items
 *  using the addItem function.
 */

var timeOut = null;
var numMenus = 0;                   // the number of distinct menus defined
var menus = new Object();           // stores the menu data itself
var ns4 = false;                    // true if the browser supports layers
var ie4 = false;                    // true if the browser supports document.all
var otherDOM = false;               // true if ns6
var ie5plus = false;                // true if browser is IE 5+
var ie55 = false;                   // true if browser is IE 5.5
var macIE = false;                  // true if browser is a Mac
var adjustSpacing = false;          // true if we need to adjust menu spacing
var activeMenuHeading = -1;         // holds number of the active menu (heading)

/*  Define color variables */
var menuHeadBgColor = "#FFD939";           // menu heading background color
var menuHeadHighLightColor = "#e3e5ee";     // menu heading background highlight
var menuTextColor = "#000000";                // menu heading text color
var menuTextHighLightColor = "darkblue";    // menu heading text highlight color
// menu heading border highlight colors - top right bottom left
var menuBorderHLColors = "darkblue";

var menuBgColor = "#e3e5ee";                   // menu background color
var menuBorderColor = "#FFD939 #FFD939 #000000 #FFD939";           // menu border color
var menuItemBgColor = "#e3e5ee";            // menu item background color
var menuItemWidth = "180px";                // width of menu item drop down
var nullTest = null;

function detectBrowser()
{
  if (document.layers)
  {
    ns4 = true;
  }
  else if (document.all)
  {
    ie4 = true;
    if (navigator.userAgent.indexOf("MSIE 4") != -1)
    {
      ie5plus = false;
    }
    else
    {
      ie5plus = true;
    }
    if (navigator.userAgent.indexOf("MSIE 5.5") != -1) 
    {
       ie55 = true;
       adjustSpacing = true; 
    }
    if (navigator.userAgent.indexOf("MSIE 6") != -1) 
    {
       ie55 = true;
       adjustSpacing = true; 
    }
    if (navigator.userAgent.indexOf("Mac") != -1)
    {
       macIE = true;
    }
    if (((navigator.userAgent.indexOf("MSIE 5.5") == -1) &&
       (navigator.userAgent.indexOf("MSIE 6") == -1)) ||
       (navigator.userAgent.indexOf("Mac") != -1))
      adjustSpacing = true;
  }
  else if (document.getElementById)
  {
    otherDOM = true;
    adjustSpacing = true;
  }
}

function showMenu(which,x,y)
{ // accepts an integer indicating which menu to show and x/y if not ns4
  if (ns4)
  {
    document.layers["menuHeading" + which].bgColor = menuHeadBgColor;
    document.layers["menu" + which].visibility = "show";
    document.layers["menu" + which].moveAbove(document.layers["mainContent"]);
  } else if (ie4)
  {
    document.all("menu" + which).style.left = x+"px";
    document.all("menu" + which).style.top = y+"px";
    document.all("menu" + which).style.visibility = "visible";
  }
  else if (otherDOM)
  {
    document.getElementById("menu" + which).style.left = x+"px";
    document.getElementById("menu" + which).style.top = y;
    document.getElementById("menu" + which).style.visibility = "visible";
  }
}

function hideMenu(which)
{ // accepts an integer indicating which menu to hide
  if (ns4)
  {
    document.layers["menuHeading" + which].bgColor = menuHeadBgColor;
    document.layers["menu" + which].visibility = "hide";
    document.layers["menu" + which].moveBelow(document.layers["mainContent"]);
  } else if (ie4)
  {
    document.all("menu" + which).style.visibility = "hidden";
  }
  else if (otherDOM)
  {
    document.getElementById("menu" + which).style.visibility = "hidden";
  }
}

/*  Function addItem:  Adds a new item to the menu data structure.
 *
 *  Parameters:  menuName - the menu heading (a string)
 *               item  - text to display in the menu for this item
 *               directory - tserve directory for url
 *               tipWrite - tip write fields for the url
 *               configName - config file name for the url
 *               hostKey - host key for the url
 *               tipHostKey - tip host key for the url
 */
function additem(menuName, item, directory, tipWrite, 
                 configName, hostKey, tipHostKey)
{
  var targetName = null;
  var openNewWindow = null;
  if (tipWrite == "anchor") 
  {
    var action = configName;
    if (hostKey == "target")
    {
        targetName = tipHostKey;
        openNewWindow = " This link will open a new window.";
    }
  }
  else
  {
    var tserveTip = "tserve_tip_read_destroy&tserve_tip_write=" + tipWrite;
    var tserveConfig = "&tserve_trans_config=" + configName;
    var tserveHost = "&tserve_host_code=" + hostKey;
    var tserveTipHost = "&tserve_tiphost_code=" + tipHostKey;
    var mills = Date.parse(new Date());
    // action - the URL to execute on menu trigger
    var action = directory + "?" + tserveTip + tserveConfig +
        tserveHost + tserveTipHost;
  }
  if (!menus[menuName])
  {
    var newMenu = new Object();
    newMenu.name = menuName;
    newMenu.num = numMenus++;
    newMenu.items = new Array();
    newMenu.actions = new Array();
    newMenu.target = new Array();
    newMenu.title = new Array();
    menus[menuName] = newMenu;
  }
  // IE4 doesn't support push() so add manually
  menus[menuName].items[menus[menuName].items.length] = item;
  menus[menuName].actions[menus[menuName].actions.length] = action;
  menus[menuName].target[menus[menuName].target.length] = targetName;
  menus[menuName].title[menus[menuName].title.length] = openNewWindow;
}

/*  Function noItem:  This is a dummy function so a menu item is 
 *                    not accessible to the user.
 */
function noitem(menuName, item, directory, configName, 
                 tipWrite, hostKey, tipHostKey)
{
}

function createMenuHeadings()
{
  if (ns4)
  {
    document.write('<layer name="background" bgcolor=menuHeadBgColor' +  
    'width="95%" z-index="0" top="78" left="10" height="21"> &nbsp;</layer>');
  }
  else if (ie4 || otherDOM)
  {
    document.write('<div class="menuBar">');
  }
  for (var menu in menus)
  {
    var theMenu = menus[menu];
    if (ns4){
     document.write('<layer name="menuHeading' + theMenu.num + 
     '"onmouseout="timeOut=setTimeout(\'hideMenu(' + theMenu.num + ')\', 500)"'+
      ' width="100" bgcolor="darkblue" top="78" height="21" left="' + 
      ((theMenu.num * 100) + 10) + 
      '" z-index="20"><a class="menuHeadingNS4"' + 
      'href="javascript:showMenu(' + theMenu.num + ')" title="' + 
      theMenu.name +'">' + theMenu.name + 
      '</a></layer>');
    }
    else if (ie4 || otherDOM) {
      document.write('<a onclick="menuHeadingMouseClick(event, ' + 
      theMenu.num + '); return false" onmouseout="menuHeadingMouseOut(' + 
      theMenu.num + ')" onmouseover="menuHeadingMouseOver(event, ' + 
      theMenu.num + ')" class="menuHeading" id="menuHeading' + 
      theMenu.num + '" title="' + theMenu.name +'">' + theMenu.name + '</a>');
    }
    if (adjustSpacing)
      document.write('&nbsp; &nbsp; &nbsp;');
  }
  if (ie4 || otherDOM)
    document.write('</div>');
}

function createMenus()
{
  createMenuHeadings();
  for (var menu in menus)
  {
    var theMenu = menus[menu];
    if (ns4) {
       document.write('<layer class="menuNS4" ' + 
       'onmouseover="clearTimeout(timeOut)"' +
       'onmouseout="timeOut=setTimeout(\'hideMenu(' + theMenu.num + ')\',500)"'+
       'visibility="hide" name="menu' + theMenu.num + '" top="98" left="' + 
       ((theMenu.num * 100) + 10) + 
       '" bgcolor="#e3e5ee" z-index="0" width="220">');
    }
    else if (ie4 || otherDOM) {
      document.write('<div class="menu" id="menu' + theMenu.num + 
      '" onmouseover="clearTimeout(timeOut)" ' + 
      'onmouseout="timeOut = setTimeout(\'menuHeadingDeactivate(); hideMenu(' 
      + theMenu.num + ')\',500)">');
    }
    for (var i=0; i<theMenu.items.length; i++)
    {
      if (theMenu.target[i] == null)
      {
       if (ns4) {
        document.write('<a class="menuItemNS4" href="' + theMenu.actions[i] + 
        '" title="' + theMenu.items[i] + 
        '" onMouseOver="window.status=(\'' + theMenu.items[i] + '\'); ' +
        'return true;" onMouseOut="window.status=\'\'; return true">' + 
        theMenu.items[i] + '</a><br />');
       }
       else if (ie4 || otherDOM) {
        document.write('<a style="width: ' + menuItemWidth + 
        '" class="menuItem" href="' + theMenu.actions[i] + 
        '" title="' + theMenu.items[i] + 
        '" onMouseOver="window.status=(\'' + theMenu.items[i] + '\'); ' +
        'return true;" onMouseOut="window.status=\'\'; return true">' + 
        theMenu.items[i] + '</a>');
       }
      } else 
      {
       if (ns4) {
        document.write('<a class="menuItemNS4" href="' + theMenu.actions[i] + 
        '" target="' + theMenu.target[i] + '" title="' + theMenu.items[i] + 
        theMenu.title[i] +
        '" onMouseOver="window.status=(\'' + theMenu.items[i] + 
        theMenu.title[i] + '\'); ' +
        'return true;" onMouseOut="window.status=\'\'; return true">' + 
        theMenu.items[i] + '</a><br />');
       }
       else if (ie4 || otherDOM) {
        document.write('<a style="width: ' + menuItemWidth + 
        '" class="menuItem" href="' + theMenu.actions[i] + 
        '" target="' + theMenu.target[i] + '" title="' + theMenu.items[i] + 
        theMenu.title[i] +
        '" onMouseOver="window.status=(\'' + theMenu.items[i] + 
        theMenu.title[i] + '\'); ' +
        'return true;" onMouseOut="window.status=\'\'; return true">' + 
        theMenu.items[i] + '</a>');
       }
      }

      if (ie4 && !ie5plus)
        document.write('<br />');
    }
    if (ns4)
      document.write('</layer>');
    else if (ie4 || otherDOM)
      document.write('</div>');
  }
}

function menuHeadingMouseOver(event, menuHeading)
{
  if (ie4)
  {
    document.all("menuHeading" + menuHeading).style.borderColor = 
      menuBorderHLColors;
    menuHeadingMouseClick(event, menuHeading);
  }
  else if (otherDOM) 
  {
    document.getElementById("menuHeading" + menuHeading).style.borderColor = 
      menuBorderHLColors;
    menuHeadingMouseClick(event, menuHeading);
  }
}

function menuHeadingMouseOut(menuHeading)
{
  if (ie4 || otherDOM)
  {
    if (activeMenuHeading != -1 && activeMenuHeading == menuHeading)
    {
       timeOut = setTimeout('hideMenu(' + activeMenuHeading + 
       '); menuHeadingDeactivate();',500);
    }
    else
    {
      if (ie4)
      {
        document.all("menuHeading" + menuHeading).style.borderColor=
          menuBorderColor;
      }
      else
      {
        document.getElementById("menuHeading" + menuHeading).style.borderColor=
          menuBorderColor;
      }
    }
  }
}
function menuHeadingActivate(menuHeading)
{
  if (ie4)
  {
    document.all("menuHeading" + menuHeading).style.background = 
      menuHeadHighLightColor;
    document.all("menuHeading" + menuHeading).style.color = 
      menuTextHighLightColor;
    document.all("menuHeading" + menuHeading).style.borderColor = 
      menuBorderHLColors;
  }
  else if (otherDOM)
  {
    document.getElementById("menuHeading" + menuHeading).style.background = 
      menuHeadHighLightColor;
    document.getElementById("menuHeading" + menuHeading).style.color = 
      menuTextHighLightColor;
    document.getElementById("menuHeading" + menuHeading).style.borderColor = 
      menuBorderHLColors;
  }
  activeMenuHeading = menuHeading;
}

function menuHeadingDeactivate()
{
  if (activeMenuHeading != -1)
  {
    if (ie4)
    {
      document.all("menuHeading" + activeMenuHeading).style.borderColor= 
        menuBorderColor;
      document.all("menuHeading" + activeMenuHeading).style.background = 
        menuHeadBgColor;
      document.all("menuHeading" + activeMenuHeading).style.color = 
        menuTextColor;
    }
    else if (otherDOM)
    {
   document.getElementById("menuHeading" + activeMenuHeading).style.borderColor=
      menuBorderColor;
   document.getElementById("menuHeading" + activeMenuHeading).style.background=
      menuHeadBgColor;
   document.getElementById("menuHeading" + activeMenuHeading).style.color= 
      menuTextColor;
    }
    activeMenuHeading = -1;
  }
}

function menuHeadingMouseClick(event, which)
{
  if (activeMenuHeading != -1)
  {
    clearTimeout(timeOut);
    hideMenu(activeMenuHeading);
    menuHeadingDeactivate();
  }
  if (ie4 || otherDOM)
    menuHeadingActivate(which);
  if (event.srcElement)
  {
    if (macIE)
    {
       x = event.srcElement.offsetLeft;
       y = event.srcElement.offsetTop;
    } else {
       x = event.srcElement.offsetLeft;
       y = event.srcElement.offsetTop + event.srcElement.offsetHeight;
    }
  }
  else if (otherDOM)
  {
    // NS6 specific
    x = document.getElementById("menuHeading" + which).offsetLeft;
    y = document.getElementById("menuHeading" + which).offsetTop + 
        document.getElementById("menuHeading" + which).offsetHeight;
  }
  if (adjustSpacing)
  {
    if (ie55) {
      x +=14; y += 81; }
    else if (macIE) {
      x +=0;  y +=0; 
    }
    else if (ie4) {
      x += 14; y += 87; }
    else if (otherDOM) {
      x +=14; y +=0; }
  }
  showMenu(which, x, y);
}

detectBrowser();

function HomePageURL(tserveHostKey,HostZeroURL,HostOneURL,HostTwoURL,
HostThreeURL,ExitMouseoverText,ExitLinkText)
{
  if (tserveHostKey == "HostZero")
  {
      document.write('<a href="' + HostZeroURL + '"title="' +
       ExitMouseoverText + '" onMouseover="window.status=\(\'' +
       ExitMouseoverText + '\'\); return true" ' + 
       'onMouseout="window.status=\'\'; return true" ' +
       'onFocus="window.status=\'' + ExitMouseoverText + 
       '\'; return true" onBlur="window.status=\'\'; return true" ' +
       'accesskey="3" class="submenulinktext2">&nbsp; ' + 
       ExitLinkText + '&nbsp;</a>');
  }
  else  
  if (tserveHostKey == "HostOne")
  {
      document.write('<a href="' + HostOneURL + '"title="' +
       ExitMouseoverText + '" onMouseover="window.status=\(\'' +
       ExitMouseoverText + '\'\); return true" ' + 
       'onMouseout="window.status=\'\'; return true" ' +
       'onFocus="window.status=\'' + ExitMouseoverText + 
       '\'; return true" onBlur="window.status=\'\'; return true" ' +
       'accesskey="3" class="submenulinktext2">&nbsp; ' + 
       ExitLinkText + '&nbsp;</a>');
  }
  else  
  if (tserveHostKey == "HostTwo")
  {
      document.write('<a href="' + HostTwoURL + '"title="' +
       ExitMouseoverText + '" onMouseover="window.status=\(\'' +
       ExitMouseoverText + '\'\); return true" ' + 
       'onMouseout="window.status=\'\'; return true" ' +
       'onFocus="window.status=\'' + ExitMouseoverText + 
       '\'; return true" onBlur="window.status=\'\'; return true" ' +
       'accesskey="3" class="submenulinktext2">&nbsp; ' + 
       ExitLinkText + '&nbsp;</a>');
  }
  else  
  if (tserveHostKey == "HostThree")
  {
      document.write('<a href="' + HostThreeURL + '"title="' +
       ExitMouseoverText + '" onMouseover="window.status=\(\'' +
       ExitMouseoverText + '\'\); return true" ' + 
       'onMouseout="window.status=\'\'; return true" ' +
       'onFocus="window.status=\'' + ExitMouseoverText + 
       '\'; return true" onBlur="window.status=\'\'; return true" ' +
       'accesskey="3" class="submenulinktext2">&nbsp; ' + 
       ExitLinkText + '&nbsp;</a>');
  }
} 

/*  Function instMessage:  This function formats and displays the institution
 *    specific message at the bottom of Self Service pages.  All parameters 
 *    values are passed through the html from tsrvweb.ini.  
 *    If MailLink has a value, the URL will be created as an anchor link.
 *    InstMessage1 or InstMsgEmail must be filled in for any message to 
 *    display.
 */
function instMessage(msgtext1, msgtext2, maillink, mailURL, mailMouseOver)
{
   if ( maillink == "" && mailURL != "" )
   {
      document.write(msgtext1 + '&nbsp;' + msgtext2 + '&nbsp;' + mailURL);
   }
   else
   if ( maillink != "" && mailURL != "" )   
   {
      var instmsg = msgtext1 + '&nbsp;' + msgtext2 + '&nbsp;<a href="' 
        + maillink + mailURL +
        '"title="' + mailMouseOver + '" onMouseover="window.status=\(\'' +
        mailMouseOver + '\'\); return true" ' + 
        'onMouseout="window.status=\'\'; return true" ' +
        'onFocus="window.status=\'' + mailMouseOver + 
        '\'; return true" onBlur="window.status=\'\'; return true"> ' 
        + mailURL  + '</a>';
      document.write(instmsg);
   }
   else
   if ( mailURL == "" && msgtext1 != "" )
   {
      document.write(msgtext1 + '&nbsp;' + msgtext2);
   }
}

/*  Function expireLink:  This function adds current date and time to links
 *    on a page.  This prevents browsers that are set to cashe pages from
 *    using cashed pages within processing of Self Service, and prevents 
 *    users from getting other users cashed information, by forcing all 
 *    link requests back to the server. 
 */
function expireLink()
{
  mills = Date.parse(new Date());
  for (i=0; i < document.links.length; i++) {
    if ((document.links[i].href.indexOf("tserve_") != -1) &&
       (document.links[i].href.indexOf("&dt=") == -1)) {
        if (document.links[i].href.indexOf("?") != -1) {
          if (document.links[i].href.indexOf("#") == -1)
            document.links[i].href = document.links[i].href + '&dt=' + mills;
        }
        else
          if (document.links[i].href.indexOf("#") == -1)
            document.links[i].href = document.links[i].href + '?dt=' + mills;
    }
  }
}
