/*
 * Menu highlighting and collapsing functions, with a breadcrumb trail
 * and a "related" link feature. This version works in conjunction with
 * a HTML  set of pages that use frames to eliminate duplicated markup.
 *
 * Unpublished Work. Copyright (c) Extensia Solutions Pty Ltd 2005.
 * All rights reserved worldwide.
 *
 * $Rev: $
 */

/**
 * Globals must be accessed through the getters and setters as only
 * the JS instance attached to the NAV frame page hold the state.
 */
var m_subMenu = "";  // currently only one sub menu per main menu permitted
var m_openMenu = "";
var m_menuItem = "";
var m_relatedLinks = "";
var m_relatedImage = "";
var m_isIE = (navigator.appName.indexOf("Microsoft") >= 0);

var NAV = "nav"; // ID of navigation frame in the index.html page

/**
 * Ensures all nav menu icons have an initial state and that only top
 * level menu items are visible.
 */
function init ()
{
  highlight("00", false);
  highlight("01", false);
  highlight("01_02", false);
  highlight("01_03", false);
  highlight("01_04", false);
  highlight("02", false);
  highlight("02_02", false);
  highlight("02_03", false);
  highlight("02_04", false);
  highlight("03", false);
  highlight("03_01", false);
  highlight("03_02", false);
  highlight("03_03", false);
  highlight("03_03_01", false);
  highlight("03_03_02", false);
  highlight("05", false);
  highlight("04", false);
  highlight("04_03", false);
  highlight("04_04", false);
  highlight("04_05", false);
  highlight("06", false);
  highlight("06_01", false);
  highlight("06_02", false);
  highlight("06_03", false);
  highlight("06_04", false);
  highlight("07", false);
  highlight("07_01", false);
  highlight("07_02", false);

  showMenu("M01", false);
  showMenu("M02", false);
  showMenu("M03", false);
  showMenu("M04", false);
  showMenu("M03_03", false);
  showMenu("M05", false);
  showMenu("M06", false);
  showMenu("M07", false);

  select("00");
}

/**
 * @return A HTML formatted date string
 */
function displayDate() {
  var month = new Array(12);
  month[0]  = "January";
  month[1]  = "February";
  month[2]  = "March";
  month[3]  = "April";
  month[4]  = "May";
  month[5]  = "June";
  month[6]  = "July";
  month[7]  = "August";
  month[8]  = "September";
  month[9]  = "October";
  month[10] = "November";
  month[11] = "December";
  var now = new Date();
  var day = now.getDate();
  var mon = now.getMonth();
  var year = now.getYear();
  if (year < 1900) {
    year += 1900;
  }
  return(day+" "+month[mon]+" " +year);
}

/**
 * Changes the menu image displayed for a nominated item id by changing
 * the src of the image for the menu item. The "current" user selection and
 * any superior container menu items are always forced to highlighted.
 * @param item An identifier of the form "nn[_nn]*"
 * @param state True to highlight
 */
function highlight(item, state)
{
  // Force highlight on the current page menu item and all superior
  // container menus.
  var currMenu = getCurrentMenu();
  if (currMenu) {
    if ((item == getCurrentItem()) || ('M' + item == currMenu)) {
      state = true;
    }
  }
   
  var targ = getItem('S' + item);
  if (targ) {
    var src = "menu_" + item + (state ? "_on" : "_off") + ".gif";
    targ.src="images/menu_images/" + src;
  }
}

/**
 * Manages selection of a menu item by a user, or through a hyperlink to
 * the menu item (from breadcrumbs, or related item links). If the item
 * is a container menu, it is expanded (if not already expanded) and any
 * previously expanded menu is collapsed.
 * @param item An identifier in the form "T|Snn[_nn]*" Where 'T' indicates the
 *             item is a container menu and 'S' a contained menu item
 */
function select (item)
{
  var oldMenu = getCurrentMenu();
  var oldItem = getCurrentItem();
  var subMenu = getCurrentSubMenu();
  var topMenu = getTopLevelMenu(item);

  setCurrentItem(item);
  highlight(oldItem, false);

  if (oldMenu != topMenu) {
    setCurrentMenu(topMenu); // This must be done first

    showMenu(oldMenu, false);
    highlight(oldMenu.substring(1), false);
    if (subMenu) {
      showMenu(subMenu, false);
      setCurrentSubMenu("");
    }

    showMenu(topMenu, true);
    highlight(topMenu.substring(1), true);
  }

  highlight(item, true);
  var tmp = 'M' + item;
  var hasSubMenu = showMenu(tmp, true);
  if (hasSubMenu) {
    setCurrentSubMenu(tmp);
  }

  reloadStatusPage();
}

/**
 * @param id ID of a tag to hide or show
 * @param state True to make visible
 */
function showMenu (id, state)
{
  var res = false;
  menu = getItem(id);
  if (menu) {
    menu.style.display = state ? 'block' : 'none';
    res = true;
  }
  return res;
}

/**
 * @return A string comprising hylerlinks derived from the globals for
 * the currently selected page. The names are extracted from the anchors
 * of the items on the NAV frame page.
 */
function displayCrumbs ()
{
  var st = "<a href=\"index.html\" target=\"_top\">Home</a>";
  var item = getCurrentItem();
  if (item && (item != "00")) {
    var doc = window.parent.frames[NAV].document;
    var ids = item.split('_');
    var id = ids[0];
    for (var idx = 0; idx < ids.length; idx++) {
      if (idx > 0) {
        id += '_';
        id += ids[idx];
      }
      var ref = doc.getElementById(id);
      var st2 = " &gt; <a href=\"" + ref.href +
                "\" target=\"main\" onClick=\"select('" + id + "')\">" +
                ref.name + "</a>";
      st += st2;
    }
  }
  return st;
}

/**
 * @param name An image in the images dir to display above the related links
 * @param ref One or more comma separated nav menu IDs to show as related links
 */
function relImage (name, ref)
{
  setRelatedImage(name);
  setRelatedLinks(ref);
  reloadPage("rel");
}

function getRelImage ()
{
  var st= "<img src=\"images/" + getRelatedImage() +
         "\" height=\"132\" width=\"132\" border=\"0\">";
  return st;
}

/**
 * Creates "Related links" string from a comma separated list. Each entry is
 * either the id of a menu item (eg: "04_04"), or an external URL in the form
 * [name;]URL. If <name> is absent, the URL is used as the name.  Note that
 * the <url> should NOT include the protocol.
 *
 * @return A string of hyperlinks created from a comma separated list of
 *         navigation menu item identifiers.
 */
function getRelLinks ()
{
  var st = "";
  var doc = window.parent.frames[NAV].document;
  var links = getRelatedLinks();
  links = links.split(',');
  for (var idx = 0; idx < links.length; idx++) {
    var id = links[idx];
	if(id!="04"){
    var ref = doc.getElementById(id);
	
    if (ref) {
	
      st += ("<a href=\"" + ref.href +  
             "\" target=\"main\" class=\"newsletter\" onClick=\"select('" +
             id + "')\">" + ref.name + "</a><br>");
			 
    }
    else {
      var name = id;
      var jdx = name.indexOf(';');
      if (jdx < 0) {
        jdx = 0;
      }
      else {
        name = id.substring(0, jdx);
        ++jdx;
      }
      var url = "http://" + id.substring(jdx);
      st += ("<a href=\"" + url + "\" target=\"_top\" class=\"newsletter\">" +
            name + "</a><br>");
			
    }
	}
  }
  return st;
}

/**
 * Forces a reload of the named frame from memory. This is used where a
 * page calls a script function that dynamically builds a string of HTML
 * from variables dynamic set by user interactions.
 */
function reloadPage (id)
{
  var frame = m_isIE ?
    window.parent.frames(id) : window.parent.frames[id];
  if (frame) {
    frame.location.reload(false);
  }
  else {
    alert("Error: frame " + id + " does not exist");
  }
}

function reloadStatusPage ()
{
  reloadPage("status");
}

function getItem (id)
{
  return m_isIE ?
    window.parent.frames(NAV).document.getElementById(id) :
    window.parent.frames[NAV].document.getElementById(id);
}

function getTopLevelMenu (id)
{
  return 'M' + id.substring(0, 2);
}

function getCurrentMenu ()
{
  return m_isIE ?
    window.parent.frames(NAV).m_openMenu :
    window.parent.frames[NAV].m_openMenu;
}

function getCurrentItem ()
{
 return  m_isIE ?
    window.parent.frames(NAV).m_menuItem :
    window.parent.frames[NAV].m_menuItem;
}

function getCurrentSubMenu ()
{
 return  m_isIE ?
    window.parent.frames(NAV).m_subMenu :
    window.parent.frames[NAV].m_subMenu;
}

function getRelatedLinks ()
{
  var tmp = m_isIE ?
    window.parent.frames(NAV).m_relatedLinks :
    window.parent.frames[NAV].m_relatedLinks;
  return (tmp ? tmp : "04_04");
}

function getRelatedImage ()
{
  var tmp = m_isIE ?
    window.parent.frames(NAV).m_relatedImage :
    window.parent.frames[NAV].m_relatedImage;
  return (tmp ? tmp : "general_01.jpg");
}

function setCurrentMenu (id)
{
  if (m_isIE) {
    window.parent.frames(NAV).m_openMenu = id;
  }
  else {
    window.parent.frames[NAV].m_openMenu = id;
  }
}

function setCurrentItem (id)
{
  if (m_isIE) {
    window.parent.frames(NAV).m_menuItem = id;
  }
  else {
    window.parent.frames[NAV].m_menuItem = id;
  }
}

function setCurrentSubMenu (id)
{
  if (m_isIE) {
    window.parent.frames(NAV).m_subMenu = id;
  }
  else {
    window.parent.frames[NAV].m_subMenu = id;
  }
}

function setRelatedLinks (st)
{
  if (m_isIE) {
    window.parent.frames(NAV).m_relatedLinks = st;
  }
  else {
    window.parent.frames[NAV].m_relatedLinks = st;
  }
}

function setRelatedImage (st)
{
  if (m_isIE) {
    window.parent.frames(NAV).m_relatedImage = st;
  }
  else {
    window.parent.frames[NAV].m_relatedImage = st;
  }
}

/**
 * Loads the Home page and resets the object state.
 */
function zonk ()
{
  var menu = getCurrentMenu();
  if (menu) {
    setCurrentMenu("");
    setCurrentItem("");
    setCurrentSubMenu("");
    setRelatedLinks("");
    setRelatedImage("");
    showMenu(menu, false);
    highlight(getCurrentItem(), false);
    highlight(menu.substring(1), false);
    select("00");
    reloadStatusPage();
  }
}

/**
 * Extracts the argument of the 'ref' arg in a HTTP request
 * placing it in a hidden input tag also named 'ref'
 */
function parseRefArg ()
{
  var req = document.location;
  var regex = /.+ref=(\d+).*/;
  var ref = regex.exec(req);
  if (ref) {
    var obj = document.getElementById('ref');
    if (obj) {
      obj.value = ref[1];
    }
  }
}

// EOF
