// Function to hide or unhide a submenu each time it is clicked
function toggle_menu(menuid,plusid)
{

  if (document.getElementById) {
    subm_obj = document.getElementById(menuid);
  } else {
    subm_obj = document[menuid];
  }

  if (document.getElementById) {
    plus_obj = document.getElementById(plusid);
  } else {
    plus_obj = document[plusid];
  }

  if (subm_obj.style.display=='none')
  {
    subm_obj.style.display='';
    plus_obj.src='./images/minus.gif';
  }
  else
  {
    subm_obj.style.display='none';;
    plus_obj.src='./images/plus.gif';
  }

  document.selection.empty;

}


// Hide a submenu
function hide_menu(menuid, plusid)
{
  if (document.getElementById) {
    subm_obj = document.getElementById(menuid);
  } else {
    subm_obj = document[menuid];
  }

  if (document.getElementById) {
    plus_obj = document.getElementById(plusid);
  } else {
    plus_obj = document[plusid];
  }

  subm_obj.style.display='none';
  plus_obj.src='./images/plus.gif';
}

// Show a specific submenu
function show_menu(menuid, plusid)
{
//  alert("Called show_menu menuid=:" + menuid + ": plusid=:" + plusid + ":");

  if (document.getElementById) {
    subm_obj = document.getElementById(menuid);
  } else {
    subm_obj = document[menuid];
  }

  if (document.getElementById) {
    plus_obj = document.getElementById(plusid);
  } else {
    plus_obj = document[plusid];
  }

  subm_obj.style.display='';
  plus_obj.src='./images/minus.gif';
}


// Return all elements of s certain class
// In our case, we will look for the 'selected' class for the Links
function getElementsByClass(searchClass,node,tag) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}

// Find currently-selected menu item and set it back to the non-selected class
function unset_selected_item()
{
  selected_links = getElementsByClass("left_selected_link");

  if (selected_links.length == 0) {
    // Look for sub-menu selected item
    selected_links = getElementsByClass("left_submenu_selected_link");
  }

  if (selected_links.length > 0) {
    // Clear the class for each selected link
    var len = selected_links.length;
    for (i = 0; i < len; i++) {
       selected_links[i].className = "";
    }
  }

}



// Searches for all items with class 'tr_submenu' and then hides them
function hide_all_menus()
{
//  alert("hide_all_menus-start");

  submenus = getElementsByClass("tr_submenu");

  var len = submenus.length;

  for (i=0 ; i < len ; i++) {
    plus_id = submenus[i].id.replace("sm_","plus_");
    //alert("SubmenuID=" + submenus[i].id + "PlusID=" + plus_id);
    hide_menu(submenus[i].id, plus_id);
  }

}

// Change appearance of an item on the left menu to show it is selected
// FIRST set the currently-selected item back to its normal class

function set_selected_item(linkid)
{
//  alert("Set selected item - linkid=:" + linkid + ":");

  // First set currently-selected item back to normal class
  unset_selected_item();

  //hide_all_menus();

  if (document.getElementById) {
    link_obj = document.getElementById(linkid);
  } else {
    link_obj = document[linkid];
  }

  // Is it a main menu or sub menu item
  if (linkid.substring(0,2)=="mm")
  {
    link_obj.className="left_selected_link";
  }
  else
  {
    link_obj.className="left_submenu_selected_link";
  }

}


// Gets the current Y position of a given element
function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

// Given the ID of a LINK in the left-hand menu, scroll
// the left-hand menu so that it is shown on the screen
function scroll_left_menu(linkid)
{
//  alert("Called scroll_left_menu linkid=:" + linkid + ": Document=:" + document.location + ":");

  if (document.getElementById) {
    link_obj = document.getElementById(linkid);
  } else {
    link_obj = document[linkid];
  }

  window.scroll(0,findPosY(link_obj));

}