/*==============================================================================
   MAIN MENU
==============================================================================*/

/*******************************************************************************
   toggleSubmenu
    
   Show or hide a submenu when the master link is clicked
   
   This function and the ones below assume that the UL and A elements have IDs
   menu_XXX and link_XXX respectively (where XXX is the general code for the
   menu and can be anything).
   
   General format for menus:
   
   <ul id="an_overall_menu_id">

   .   .   .
   
   <!-- For ordinary links with no dropdown submenu -->
   
   <li><a href="ordinary">Ordinary</a></li>

   .   .   .
   
   <!-- For menus with a dropdown submenu -->
   
   <li>
      <a id="link_XXX" class="submenu-closed" href="javascript:toggleSubmenu('XXX')">XXX Heading</a>
      <ul id="menu_XXX" class="section_hide">
         <li><a href="something">Something</a></li>
         <li><a href="whatever">Whatever</a></li>
         <li><a href="stuff">Stuff</a></li>
      </ul>
   </li>

   .   .   .
   
   <!-- For links leading to a page with a replacement submenu -->
   
   <li><a href="newstuff" class="downmenu">New stuff</a></li>

   .   .   .

   <!-- For 'return to previous level' menu links -->
   
   <li><a href="oldstuff" class="upmenu">Old stuff</a></li>

   .   .   .

   </ul>

*******************************************************************************/

function toggleSubmenu(menuid)
{
   var menu_ul   = document.getElementById('menu_' + menuid);
   var menu_link = document.getElementById('link_' + menuid);

   if (menu_ul.className == 'section_show')
   {
      menu_ul.className   = 'section_hide';
      menu_link.className = 'submenu-closed';

      Set_Cookie(menuid, 'hide', 60);
   }
   else
   {
      menu_ul.className   = 'section_show';
      menu_link.className = 'submenu-open';

      Set_Cookie(menuid, 'show', 60);
   }
}


/*******************************************************************************
   restoreSubmenus
    
   Look up cookies when a page is loaded or refreshed to ensure that the
   previous open/closed states of the submenus are preserved
*******************************************************************************/

function restoreSubmenus(menuid)
{
   var idlist = getSubmenuIds(menuid);

   for (var i in idlist)
   {
      if (Get_Cookie(idlist[i]) == 'show') toggleSubmenu(idlist[i]);
   }
}


/*******************************************************************************
   clearSubmenus
    
   Remove all cookies when a link is clicked and redirect to the specified
   page (presumably the logout page)
*******************************************************************************/

function clearSubmenus(menuid, url)
{
   var idlist = getSubmenuIds(menuid);
   
   for (var i in idlist) Delete_Cookie(idlist[i]);
   location.href = base_href() + url;
}


/*******************************************************************************
   getSubmenuIds
    
   Extract a list of all submenu IDs given the base menu ID (assuming that we
   have the standard LW/BC style arrays)
*******************************************************************************/

function getSubmenuIds(menuid)
{
   // loop through list items
   
   var idlist = [];
   var menu = document.getElementById(menuid);

   for (var i in menu.childNodes)
   {
      // loop through what list items contain looking for submenu lists

      for (var j in menu.childNodes[i].childNodes)
      {
         // just use numeric indexes -- Opera creates an additional entry under
         // the name of the item, which would cause calls to be duplicated
         // (often a Bad THing)

         if (isNaN(j)) continue; 

         // ignore anything that isn't another list
         
         var node = menu.childNodes[i].childNodes[j];
         if (node.nodeName != 'UL') continue;

         idlist.push(node.id.substr(5));
      }
   }

   return idlist;
}


/*==============================================================================
   SHOW/HIDE SECTIONS
==============================================================================*/

/*******************************************************************************
   toggleSection
    
   Show or hide the display table for a section
*******************************************************************************/

function toggleSection(sectionid)
{
   var div = document.getElementById('section_' + sectionid);
   var img = document.getElementById('img_'     + sectionid);

   if (div.className == 'section_show')
   {
      div.className = 'section_hide';
      img.src = 'images/section-closed.gif';
   }
   else
   {
      div.className = 'section_show';
      img.src = 'images/section-open.gif';
   }
}


/*==============================================================================
   'TABBED' DISPLAYS
==============================================================================*/

/*******************************************************************************
   showTab
    
   Show or hide the display content for a section
*******************************************************************************/

function showTab(tabsetid, tabids_str, tabid)
{
   var tabids = tabids_str.split(',');
   var tablink, tabcontent;

   for (var i in tabids)
   {
      id = tabids[i];

      tablink    = document.getElementById(tabsetid + '_tab_' + id);
      tabcontent = document.getElementById(tabsetid + '_content_' + id);

      if (id == tabid)
      {
         tablink.className    = 'current';
         tabcontent.className = 'show';
      }
      else
      {
         tablink.className    = 'normal';
         tabcontent.className = 'hide';
      }
   }
}


/*******************************************************************************
   toggleImage
    
   Show or hide an image in the graphs tabset
*******************************************************************************/

function toggleImage(id)
{
   var cb  = document.getElementById('cb_'  + id);
   var img = document.getElementById('img_' + id);
   
   if (cb.checked)
      img.style.display = 'inline';
   else
      img.style.display = 'none';
}



/*==============================================================================
   DATA TABLES
==============================================================================*/

/*******************************************************************************
   equaliseTableRowHeights
    
   Go through a table and set all rows to the height of the highest for better
   appearance -- optionally ignoring header rows
*******************************************************************************/

function equaliseTableRowHeights(tableid, ignore_header_rows)
{
   var rows = document.getElementById(tableid).rows;
   var max_height = 0;
   
   for (var r=0; r<rows.length; r++)
   {
      if (ignore_header_rows && rows[r].cells[0].tagName == 'TH') continue;
      
      if (rows[r].clientHeight > max_height)
         max_height = rows[r].clientHeight; 
   }
   
   for (var r=0; r<rows.length; r++)
   {
      if (ignore_header_rows && rows[r].cells[0].tagName == 'TH') continue;
      rows[r].cells[0].height = max_height - 6;
   }
}


/*==============================================================================
   FORMS
==============================================================================*/

/*******************************************************************************
   submitForm
   
   Submit a form while dynamically specifying the action attribute
*******************************************************************************/

function submitForm(formid, action)
{
   var form = document.getElementById(formid);
   form.action = action;
   form.submit();
}


/*******************************************************************************
   synchroniseCheckboxes
   
   Keep the state of two checkboxes that either have to be both checked or both
   unchecked in sync with each other -- first one will be set to the state of
   the second
*******************************************************************************/

function synchroniseCheckboxes(cbid1, cbid2)
{
   document.getElementById(cbid1).checked = document.getElementById(cbid2).checked;
}


/*******************************************************************************
   setAllCheckboxes
   
   Set all checkboxes on a form to checked or not
*******************************************************************************/

function setAllCheckboxes(formid, checked)
{
   var form = document.getElementById(formid);
   
   for (var i=0; i < form.elements.length; i++)
   {
      if (form.elements[i].type != 'checkbox') continue;
      form.elements[i].checked = checked;
   }
}


/*==============================================================================
   UTILITY FUNCTIONS (WHITESPACE TRIMMING ETC)
==============================================================================*/

function trim(str)   { return str.replace(/^\s+|\s+$/g,""); }
function ltrim(str)  { return str.replace(/^\s+/,""); }
function rtrim(str)  { return str.replace(/\s+$/,""); }
function base_href() { return document.getElementsByTagName('base')[0].href; }


/*==============================================================================
   SETTING OF VARIABLES ETC
==============================================================================*/

// Highslide popup configuration

hs.graphicsDir           = 'images/highslide/';
hs.outlineType           = 'rounded-white';
hs.outlineWhileAnimating = true;


