/*
     JSCookMenu v1.25.  (c) Copyright 2002 by Heng Yuan

     Permission is hereby granted, free of charge, to any person obtaining a
     copy of this software and associated documentation files (the "Software"),
     to deal in the Software without restriction, including without limitation
     the rights to use, copy, modify, merge, publish, distribute, sublicense,
     and/or sell copies of the Software, and to permit persons to whom the
     Software is furnished to do so, subject to the following conditions:

     The above copyright notice and this permission notice shall be included
     in all copies or substantial portions of the Software.

     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     DEALINGS IN THE SOFTWARE.
*/

// Globals
var _cmIDCount = 0;
var _cmIDName = 'cmSubMenuID';          // for creating submenu id

var _cmTimeOut = null;             // how long the menu would stay
var _cmCurrentItem = null;         // the current menu item being selected;

var _cmNoAction = new Object ();   // indicate that the item cannot be hovered.
var _cmSplit = new Object ();      // indicate that the item is a menu split

var _cmItemList = new Array ();         // a simple list of items

// default node properties
var _cmNodeProperties =
{
     // main menu display attributes
     //
     // Note.  When the menu bar is horizontal,
     // mainFolderLeft and mainFolderRight are
     // put in <span></span>.  When the menu
     // bar is vertical, they would be put in
     // a separate TD cell.

     // HTML code to the left of the folder item
     mainFolderLeft: '',
     // HTML code to the right of the folder item
     mainFolderRight: '',
     // HTML code to the left of the regular item
     mainItemLeft: '',
     // HTML code to the right of the regular item
     mainItemRight: '',

     // sub menu display attributes

     // HTML code to the left of the folder item
     folderLeft: '',
     // HTML code to the right of the folder item
     folderRight: '',
     // HTML code to the left of the regular item
     itemLeft: '',
     // HTML code to the right of the regular item
     itemRight: '',
     // cell spacing for main menu
     mainSpacing: 0,
     // cell spacing for sub menus
     subSpacing: 0,
     // auto dispear time for submenus in milli-seconds
     delay: 500
};

//////////////////////////////////////////////////////////////////////
//
// Drawing Functions and Utility Functions
//
//////////////////////////////////////////////////////////////////////

//
// produce a new unique id
//
function cmNewID ()
{
     return _cmIDName + (++_cmIDCount);
}

//
// return the property string for the menu item
//
function cmActionItem (item, prefix, isMain, idSub, orient, nodeProperties)
{
     // var index = _cmItemList.push (item) - 1;
     _cmItemList[_cmItemList.length] = item;
     var index = _cmItemList.length - 1;
     idSub = (!idSub) ? 'null' : ('\'' + idSub + '\'');
     orient = '\'' + orient + '\'';
     prefix = '\'' + prefix + '\'';
     return ' onmouseover="cmItemMouseOver (this,' + prefix + ',' + isMain + ',' + idSub + ',' + orient + ',' + index + ')" onmouseout="cmItemMouseOut (this,' + nodeProperties.delay + ')" onmousedown="cmItemMouseDown (this,' + index + ')" onmouseup="cmItemMouseUp (this,' + index + ')"';
}

function cmNoActionItem (item, prefix)
{
     return item[1];
}

function cmSplitItem (prefix, isMain, vertical)
{
     var classStr = 'cm' + prefix;
     if (isMain)
     {
          classStr += 'Main';
          if (vertical)
               classStr += 'HSplit';
          else
               classStr += 'VSplit';
     }
     else
          classStr += 'HSplit';
     var item = eval (classStr);
     return cmNoActionItem (item, prefix);
}

//
// draw the sub menu recursively
//
function cmDrawSubMenu (subMenu, prefix, id, orient, nodeProperties)
{
     var str = '<div class="' + prefix + 'SubMenu" id="' + id + '"><table summary="sub menu" cellspacing="' + nodeProperties.subSpacing + '" class="' + prefix + 'SubMenuTable">';
     var strSub = '';

     var item;
     var idSub;
     var hasChild;

     var i;

     var classStr;

     for (i = 5; i < subMenu.length; ++i)
     {
          item = subMenu[i];
          if (!item)
               continue;

          hasChild = (item.length > 5);
          idSub = hasChild ? cmNewID () : null;

          str += '<tr class="' + prefix + 'MenuItem"' + cmActionItem (item, prefix, 0, idSub, orient, nodeProperties) + '>';

          if (item == _cmSplit)
          {
               str += cmSplitItem (prefix, 0, true);
               str += '</tr>';
               continue;
          }

          if (item[0] == _cmNoAction)
          {
               str += cmNoActionItem (item, prefix);
               str += '</tr>';
               continue;
          }

          classStr = prefix + 'Menu';
          classStr += hasChild ? 'Folder' : 'Item';

          str += '<td class="' + classStr + 'Left">';

          if (item[0] != null && item[0] != _cmNoAction)
               str += item[0];
          else
               str += hasChild ? nodeProperties.folderLeft : nodeProperties.itemLeft;

          str += '<td class="' + classStr + 'Text">' + item[1];

          str += '<td class="' + classStr + 'Right">';

          if (hasChild)
          {
               str += nodeProperties.folderRight;
               strSub += cmDrawSubMenu (item, prefix, idSub, orient, nodeProperties);
          }
          else
               str += nodeProperties.itemRight;
          str += '</td></tr>';
     }

     str += '</table></div>' + strSub;
     return str;
}

//
// The function that builds the menu inside the specified element id.
//
// @param id   id of the element
//        orient    orientation of the menu in [hv][ab][lr] format
//        menu the menu object to be drawn
//        nodeProperties properties for each menu node
//
function cmDraw (id, menu, orient, nodeProperties, prefix)
{
     var obj = cmGetObject (id);

     if (!nodeProperties)
          nodeProperties = _cmNodeProperties;
     if (!prefix)
          prefix = '';

     var str = '<table summary="main menu" class="' + prefix + 'Menu" cellspacing="' + nodeProperties.mainSpacing + '">';
     var strSub = '';

     if (!orient)
          orient = 'hbr';

     var orientStr = String (orient);
     var orientSub;
     var vertical;

     // draw the main menu items
     if (orientStr.charAt (0) == 'h')
     {
          // horizontal menu
          orientSub = 'v' + orientStr.substr (1, 2);
          str += '<tr>';
          vertical = false;
     }
     else
     {
          // vertical menu
          orientSub = 'v' + orientStr.substr (1, 2);
          vertical = true;
     }

     var i;
     var item;
     var idSub;
     var hasChild;

     var classStr;

     for (i = 0; i < menu.length; ++i)
     {
          item = menu[i];

          if (!item)
               continue;

          str += vertical ? '<tr' : '<td';
          str += ' class="' + prefix + 'MainItem"';

          hasChild = (item.length > 5);
          idSub = hasChild ? cmNewID () : null;

          str += cmActionItem (item, prefix, 1, idSub, orient, nodeProperties) + '>';

          if (item == _cmSplit)
          {
               str += cmSplitItem (prefix, 1, vertical);
               str += vertical? '</tr>' : '</td>';
               continue;
          }

          if (item[0] == _cmNoAction)
          {
               str += cmNoActionItem (item, prefix);
               str += vertical? '</tr>' : '</td>';
               continue;
          }

          classStr = prefix + 'Main' + (hasChild ? 'Folder' : 'Item');

          str += vertical ? '<td' : '<span';
          str += ' class="' + classStr + 'Left">';

          str += (item[0] == null) ? (hasChild ? nodeProperties.mainFolderLeft : nodeProperties.mainItemLeft)
                          : item[0];
          str += vertical ? '</td>' : '</span>';

          str += vertical ? '<td' : '<span';
          str += ' class="' + classStr + 'Text">';
          str += item[1];

          str += vertical ? '</td>' : '</span>';

          str += vertical ? '<td' : '<span';
          str += ' class="' + classStr + 'Right">';

          str += hasChild ? nodeProperties.mainFolderRight : nodeProperties.mainItemRight;

          str += vertical ? '</td>' : '</span>';

          str += vertical ? '</tr>' : '</td>';

          // special for titan-europa
          str += vertical ? '' : '<td>&nbsp;|&nbsp;</td>';

          if (hasChild)
               strSub += cmDrawSubMenu (item, prefix, idSub, orientSub, nodeProperties);
     }
     if (!vertical)
          str += '</tr>';
     str += '</table>' + strSub;
     obj.innerHTML = str;
     //document.write ("<xmp>" + str + "</xmp>");
}

//////////////////////////////////////////////////////////////////////
//
// Mouse Event Handling Functions
//
//////////////////////////////////////////////////////////////////////

//
// action should be taken for mouse moving in to the menu item
//
function cmItemMouseOver (obj, prefix, isMain, idSub, orient, index)
{
     clearTimeout (_cmTimeOut);

     if (!obj.cmPrefix)
     {
          obj.cmPrefix = prefix;
          obj.cmIsMain = isMain;
     }

     var thisMenu = cmGetThisMenu (obj, prefix);

     // insert obj into cmItems if cmItems doesn't have obj
     if (!thisMenu.cmItems)
          thisMenu.cmItems = new Array ();
     var i;
     for (i = 0; i < thisMenu.cmItems.length; ++i)
     {
          if (thisMenu.cmItems[i] == obj)
               break;
     }
     if (i == thisMenu.cmItems.length)
     {
          //thisMenu.cmItems.push (obj);
          thisMenu.cmItems[i] = obj;
     }

     // hide the previous submenu that is not this branch
     if (_cmCurrentItem)
     {
          // occationally, we get this case when user
          // move the mouse slowly to the border
          if (_cmCurrentItem == thisMenu)
               return;

          var thatPrefix = _cmCurrentItem.cmPrefix;
          var thatMenu = cmGetThisMenu (_cmCurrentItem, thatPrefix);
          if (thatMenu != thisMenu.cmParentMenu)

          {
               if (_cmCurrentItem.cmIsMain)
                    _cmCurrentItem.className = thatPrefix + 'MainItem';
               else
                    _cmCurrentItem.className = thatPrefix + 'MenuItem';
               if (thatMenu.id != idSub)
                    cmHideMenu (thatMenu, thisMenu, thatPrefix);
          }
     }

     // okay, set the current menu to this obj
     _cmCurrentItem = obj;

     // just in case, reset all items in this menu to MenuItem
     cmResetMenu (thisMenu, prefix);

     var item = _cmItemList[index];
     var isDefaultItem = cmIsDefaultItem (item);

     if (isDefaultItem)
     {
          if (isMain)
               obj.className = prefix + 'MainItemHover';
          else
               obj.className = prefix + 'MenuItemHover';
     }

     if (idSub)
     {
          var subMenu = cmGetObject (idSub);
          cmShowSubMenu (obj, prefix, subMenu, orient);
     }

     var descript = '';
     if (item.length > 4)
          descript = (item[4] != null) ? item[4] : (item[2] ? item[2] : descript);
     else if (item.length > 2)
          descript = (item[2] ? item[2] : descript);

     window.defaultStatus = descript;
}

//
// action should be taken for mouse moving out of the menu item
//
function cmItemMouseOut (obj, delayTime)
{
     if (!delayTime)
          delayTime = _cmNodeProperties.delay;
     _cmTimeOut = window.setTimeout ('cmHideMenuTime ()', delayTime);
     window.defaultStatus = '';
}

//
// action should be taken for mouse button down at a menu item
//
function cmItemMouseDown (obj, index)
{
     if (cmIsDefaultItem (_cmItemList[index]))
     {
          if (obj.cmIsMain)
               obj.className = obj.cmPrefix + 'MainItemActive';
          else
               obj.className = obj.cmPrefix + 'MenuItemActive';
     }
}

//
// action should be taken for mouse button up at a menu item
//
function cmItemMouseUp (obj, index)
{
     var item = _cmItemList[index];

     var link = null, target = '_self';

     if (item.length > 2)
          link = item[2];
     if (item.length > 3 && item[3])
          target = item[3];

     if (link != null)
     {
          window.open (link, target);
     }

     var prefix = obj.cmPrefix;
     var thisMenu = cmGetThisMenu (obj, prefix);

     var hasChild = (item.length > 5);
     if (!hasChild)
     {
          if (cmIsDefaultItem (item))
          {
               if (obj.cmIsMain)
                    obj.className = prefix + 'MainItem';
               else
                    obj.className = prefix + 'MenuItem';
          }
          cmHideMenu (thisMenu, null, prefix);
     }
     else
     {
          if (cmIsDefaultItem (item))
          {
               if (obj.cmIsMain)
                    obj.className = prefix + 'MainItemHover';
               else
                    obj.className = prefix + 'MenuItemHover';
          }
     }
}

//////////////////////////////////////////////////////////////////////
//
// Mouse Event Support Utility Functions
//
//////////////////////////////////////////////////////////////////////

//
// move submenu to the appropriate location
//
// @param obj  the menu item that opens up the subMenu
//        subMenu   the sub menu to be shown
//        orient    the orientation of the subMenu
//
function cmMoveSubMenu (obj, subMenu, orient)
{
     var mode = String (orient);
     var p = subMenu.offsetParent;
     if (mode.charAt (0) == 'h')
     {
          if (mode.charAt (1) == 'b')
               subMenu.style.top = (cmGetYAt (obj, p) + cmGetHeight (obj)) + 'px';
          else
               subMenu.style.top = (cmGetYAt (obj, p) - cmGetHeight (subMenu)) + 'px';
          if (mode.charAt (2) == 'r')
               subMenu.style.left = (cmGetXAt (obj, p)) + 'px';
          else
               subMenu.style.left = (cmGetXAt (obj, p) + cmGetWidth (obj) - cmGetWidth (subMenu)) + 'px';
     }
     else
     {
          if (mode.charAt (2) == 'r')
               subMenu.style.left = (cmGetXAt (obj, p) + cmGetWidth (obj)) + 'px';
          else
               subMenu.style.left = (cmGetXAt (obj, p) - cmGetWidth (subMenu)) + 'px';
          if (mode.charAt (1) == 'b')
               subMenu.style.top = (cmGetYAt (obj, p)) + 'px';
          else
               subMenu.style.top = (cmGetYAt (obj, p) + cmGetHeight (obj) - cmGetHeight (subMenu)) + 'px';
     }
}

//
// show the subMenu w/ specified orientation
// also move it to the correct coordinates
//
// @param obj  the menu item that opens up the subMenu
//        subMenu   the sub menu to be shown
//        orient    the orientation of the subMenu
//
function cmShowSubMenu (obj, prefix, subMenu, orient)
{
     if (!subMenu.cmParentMenu)
     {
          // establish the tree w/ back edge
          var thisMenu = cmGetThisMenu (obj, prefix);
          subMenu.cmParentMenu = thisMenu;
          if (!thisMenu.cmSubMenu)
               thisMenu.cmSubMenu = new Array ();
          //thisMenu.cmSubMenu.push (subMenu);
          thisMenu.cmSubMenu[thisMenu.cmSubMenu.length] = subMenu;
     }

     // position the sub menu
     cmMoveSubMenu (obj, subMenu, orient);
     subMenu.style.visibility = 'visible';

     //
     // On IE, controls such as SELECT, OBJECT, IFRAME (before 5.5)
     // are window based controls.  So, if the sub menu and these
     // controls overlap, sub menu would be hidden behind them.  Thus
     // one needs to turn the visibility of these controls off when the
     // sub menu is showing, and turn their visibility back on
     // when the sub menu is hiding.
     //
     if (document.all)   // it is IE
     {
          /* part of Felix Zaslavskiy's fix on hiding controls
             not really sure if this part is necessary, but shouldn't
             hurt. */
          if (!subMenu.cmOverlap)
               subMenu.cmOverlap = new Array ();
/*@cc_on @*/
/*@if (@_jscript_version >= 5.5)
@else @*/
          cmHideControl ("IFRAME", subMenu);
/*@end @*/
          cmHideControl ("SELECT", subMenu);
          cmHideControl ("OBJECT", subMenu);
     }
}

//
// reset all the menu items to class MenuItem in thisMenu
//
function cmResetMenu (thisMenu, prefix)
{
     if (thisMenu.cmItems)
     {
          var i;
          var str;
          var items = thisMenu.cmItems;
          for (i = 0; i < items.length; ++i)
          {
               if (items[i].cmIsMain)
                    str = prefix + 'MainItem';
               else
                    str = prefix + 'MenuItem';
               if (items[i].className != str)
                    items[i].className = str;
          }
     }
}

//
// called by the timer to hide the menu
//
function cmHideMenuTime ()
{
     if (_cmCurrentItem)
     {
          var prefix = _cmCurrentItem.cmPrefix;
          cmHideMenu (cmGetThisMenu (_cmCurrentItem, prefix), null, prefix);
     }
}

//
// hide thisMenu, children of thisMenu, as well as the ancestor
// of thisMenu until currentMenu is encountered.  currentMenu
// will not be hidden
//
function cmHideMenu (thisMenu, currentMenu, prefix)
{
     var str = prefix + 'SubMenu';

     // hide the down stream menus
     if (thisMenu.cmSubMenu)
     {
          var i;
          for (i = 0; i < thisMenu.cmSubMenu.length; ++i)
          {
               cmHideSubMenu (thisMenu.cmSubMenu[i], prefix);
          }
     }

     // hide the upstream menus
     while (thisMenu && thisMenu != currentMenu)
     {
          cmResetMenu (thisMenu, prefix);
          if (thisMenu.className == str)
          {
               thisMenu.style.visibility = 'hidden';
               cmShowControl (thisMenu);
          }
          else
               break;
          thisMenu = cmGetThisMenu (thisMenu.cmParentMenu, prefix);
     }
}

//
// hide thisMenu as well as its sub menus if thisMenu is not
// already hidden
//
function cmHideSubMenu (thisMenu, prefix)
{
     if (thisMenu.style.visibility == 'hidden')
          return;
     if (thisMenu.cmSubMenu)
     {
          var i;
          for (i = 0; i < thisMenu.cmSubMenu.length; ++i)
          {
               cmHideSubMenu (thisMenu.cmSubMenu[i], prefix);
          }
     }
     cmResetMenu (thisMenu, prefix);
     thisMenu.style.visibility = 'hidden';
     cmShowControl (thisMenu);
}

//
// hide a control such as IFRAME
//
function cmHideControl (tagName, subMenu)
{
     var x = cmGetX (subMenu);
     var y = cmGetY (subMenu);
     var w = subMenu.offsetWidth;
     var h = subMenu.offsetHeight;

     var i;
     for (i = 0; i < document.all.tags(tagName).length; ++i)
     {
          var obj = document.all.tags(tagName)[i];
          if (!obj || !obj.offsetParent)
               continue;

          // check if the object and the subMenu overlap

          var ox = cmGetX (obj);
          var oy = cmGetY (obj);
          var ow = obj.offsetWidth;
          var oh = obj.offsetHeight;

          if (ox > (x + w) || (ox + ow) < x)
               continue;
          if (oy > (y + h) || (oy + oh) < y)
               continue;

          // if object is already made hidden by a different
          // submenu then we dont want to put it on overlap list of
          // of a submenu a second time.
          // - bug fixed by Felix Zaslavskiy
          if(obj.style.visibility == "hidden")
               continue;

          //subMenu.cmOverlap.push (obj);
          subMenu.cmOverlap[subMenu.cmOverlap.length] = obj;
          obj.style.visibility = "hidden";
     }
}

//
// show the control hidden by the subMenu
//
function cmShowControl (subMenu)
{
     if (subMenu.cmOverlap)
     {
          var i;
          for (i = 0; i < subMenu.cmOverlap.length; ++i)
               subMenu.cmOverlap[i].style.visibility = "";
     }
     subMenu.cmOverlap = null;
}

//
// returns the main menu or the submenu table where this obj (menu item)
// is in
//
function cmGetThisMenu (obj, prefix)
{
     var str1 = prefix + 'SubMenu';
     var str2 = prefix + 'Menu';
     while (obj)
     {
          if (obj.className == str1 || obj.className == str2)
               return obj;
          obj = obj.parentNode;
     }
     return null;
}

//
// return true if this item is handled using default handlers
//
function cmIsDefaultItem (item)
{
     if (item == _cmSplit || item[0] == _cmNoAction)
          return false;
     return true;
}

//
// returns the object baring the id
//
function cmGetObject (id)
{
     if (document.all)
          return document.all[id];
     return document.getElementById (id);
}

//
// functions that obtain the width of an HTML element.
//
function cmGetWidth (obj)
{
     var width = obj.offsetWidth;
     if (width > 0 || !cmIsTRNode (obj))
          return width;
     if (!obj.firstChild)
          return 0;
     // use TABLE's length can cause an extra pixel gap
     //return obj.parentNode.parentNode.offsetWidth;

     // use the left and right child instead
     return obj.lastChild.offsetLeft - obj.firstChild.offsetLeft + cmGetWidth (obj.lastChild);
}

//
// functions that obtain the height of an HTML element.
//
function cmGetHeight (obj)
{
     var height = obj.offsetHeight;
     if (height > 0 || !cmIsTRNode (obj))
          return height;
     if (!obj.firstChild)
          return 0;
     // use the first child's height
     return obj.firstChild.offsetHeight;
}

//
// functions that obtain the coordinates of an HTML element
//
function cmGetX (obj)
{
     var x = 0;

     do
     {
          x += obj.offsetLeft;
          obj = obj.offsetParent;
     }
     while (obj);
     return x;
}

function cmGetXAt (obj, elm)
{
     var x = 0;

     while (obj && obj != elm)
     {
          x += obj.offsetLeft;
          obj = obj.offsetParent;
     }
     if (obj == elm)
          return x;
     return cmGetX (elm) - x;
}

function cmGetY (obj)
{
     var y = 0;
     do
     {
          y += obj.offsetTop;
          obj = obj.offsetParent;
     }
     while (obj);
     return y;
}

function cmIsTRNode (obj)
{
     var tagName = obj.tagName;
     return tagName == "TR" || tagName == "tr" || tagName == "Tr" || tagName == "tR";
}

//
// get the Y position of the object.  In case of TR element though,
// we attempt to adjust the value.
//
function cmGetYAt (obj, elm)
{
     var y = 0;

     if (!obj.offsetHeight && cmIsTRNode (obj))
     {
          var firstTR = obj.parentNode.firstChild;
          obj = obj.firstChild;
          y -= firstTR.firstChild.offsetTop;
     }

     while (obj && obj != elm)
     {
          y += obj.offsetTop;
          obj = obj.offsetParent;
     }

     if (obj == elm)
          return y;
     return cmGetY (elm) - y;
}

//
// debug function, ignore :)
//
function cmGetProperties (obj)
{
     if (obj == undefined)
          return 'undefined';
     if (obj == null)
          return 'null';

     var msg = obj + ':\n';
     var i;
     for (i in obj)
          msg += i + ' = ' + obj[i] + '; ';
     return msg;
}

/* JSCookMenu v1.25 1. fix Safari positioning issue.  The problem is that all TR elements are located
                            at the top left corner.  Thus, need to obtain the "virtual"
                            position of these element could be at.
*/
/* JSCookMenu v1.24 1. fix window based control hiding bug
                            thanks to Felix Zaslavskiy <felix@bebinary.com> for the fix.
*/
/* JSCookMenu v1.23 1. correct a position bug when the container is positioned.
                           thanks to Andre <anders@netspace.net.au> for narrowing down
                           the problem.
*/
/* JSCookMenu v1.22 1. change Array.push (obj) call to Array[length] = obj.
                            Suggestion from Dick van der Kaaden <dick@netrex.nl> to
                            make the script compatible with IE 5.0
                         2. Changed theme files a little to add z-index: 100 for sub
                            menus.  This change is necessary for Netscape to avoid
                            a display problem.
                         3. some changes to the DOM structure to make this menu working
                            on Netscape 6.0 (tested).  The main reason is that NN6 does
                            not do absolute positioning with tables.  Therefore an extra
                            div layer must be put around the table.
*/
/* JSCookMenu v1.21 1. fixed a bug that didn't add 'px' as part of coordinates.
                            JSCookMenu should be XHTML validator friendly now.
                         2. removed unnecessary display attribute and corresponding
                            theme entry to fix a problem that Netscape sometimes
                            render Office theme incorrectly
*/
/* JSCookMenu v1.2. 1. fix the problem of showing status in Netscape
                         2. changed the handler parameters a bit to allow
                            string literals to be passed to javascript based
                            links
                         3. having null in target field would cause the link
                            to be opened in the current window, but this behavior
                            could change in the future releases
*/
/* JSCookMenu v1.1.      added ability to hide controls in IE to show submenus properly */
/* JSCookMenu v1.01.     cmDraw generates XHTML code */
/* JSCookMenu v1.0.      (c) Copyright 2002 by Heng Yuan */
// <?php !! This fools phpdocumentor into parsing this file
/**
* @version $Id: mambojavascript.js,v 1.10 2005/02/16 17:06:07 eddieajau Exp $
* @package Mambo
* @copyright (C) 2000 - 2005 Miro International Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* Mambo is Free Software
*/

// general utility for browsing a named array or object
function xshow(o) {
     s = '';
     for(e in o) {s += e+'='+o[e]+'\n';}
     alert( s );
}

/**
* Writes a dynamically generated list
* @param string The parameters to insert into the <select> tag
* @param array A javascript array of list options in the form [key,value,text]
* @param string The key to display for the initial state of the list
* @param string The original key that was selected
* @param string The original item value that was selected
*/
function writeDynaList( selectParams, source, key, orig_key, orig_val ) {
     var html = '\n <select ' + selectParams + '>';
     var i = 0;
     for (x in source) {
          if (source[x][0] == key) {
               var selected = '';
               if ((orig_key == key && orig_val == source[x][1]) || (i == 0 && orig_key != key)) {
                    selected = 'selected="selected"';
               }
               html += '\n         <option value="'+source[x][1]+'" '+selected+'>'+source[x][2]+'</option>';
          }
          i++;
     }
     html += '\n    </select>';

     document.writeln( html );
}

/**
* Changes a dynamically generated list
* @param string The name of the list to change
* @param array A javascript array of list options in the form [key,value,text]
* @param string The key to display
* @param string The original key that was selected
* @param string The original item value that was selected
*/
function changeDynaList( listname, source, key, orig_key, orig_val ) {
     var list = eval( 'document.adminForm.' + listname );

     // empty the list
     for (i in list.options.length) {
          list.options[i] = null;
     }
     i = 0;
     for (x in source) {
          if (source[x][0] == key) {
               opt = new Option();
               opt.value = source[x][1];
               opt.text = source[x][2];

               if ((orig_key == key && orig_val == opt.value) || i == 0) {
                    opt.selected = true;
               }
               list.options[i++] = opt;
          }
     }
     list.length = i;
}

/**
* Adds a select item(s) from one list to another
*/
function addSelectedToList( frmName, srcListName, tgtListName ) {
     var form = eval( 'document.' + frmName );
     var srcList = eval( 'form.' + srcListName );
     var tgtList = eval( 'form.' + tgtListName );

     var srcLen = srcList.length;
     var tgtLen = tgtList.length;
     var tgt = "x";

     //build array of target items
     for (var i=tgtLen-1; i > -1; i--) {
          tgt += "," + tgtList.options[i].value + ","
     }

     //Pull selected resources and add them to list
     for (var i=srcLen-1; i > -1; i--) {
          if (srcList.options[i].selected && tgt.indexOf( "," + srcList.options[i].value + "," ) == -1) {
               opt = new Option( srcList.options[i].text, srcList.options[i].value );
               tgtList.options[tgtList.length] = opt;
          }
     }
}

function delSelectedFromList( frmName, srcListName ) {
     var form = eval( 'document.' + frmName );
     var srcList = eval( 'form.' + srcListName );

     var srcLen = srcList.length;

     for (var i=srcLen-1; i > -1; i--) {
          if (srcList.options[i].selected) {
               srcList.options[i] = null;
          }
     }
}

function moveInList( frmName, srcListName, index, to) {
     var form = eval( 'document.' + frmName );
     var srcList = eval( 'form.' + srcListName );
     var total = srcList.options.length-1;

     if (index == -1) {
          return false;
     }
     if (to == +1 && index == total) {
          return false;
     }
     if (to == -1 && index == 0) {
          return false;
     }

     var items = new Array;
     var values = new Array;

     for (i=total; i >= 0; i--) {
          items[i] = srcList.options[i].text;
          values[i] = srcList.options[i].value;
     }
     for (i = total; i >= 0; i--) {
          if (index == i) {
               srcList.options[i + to] = new Option(items[i],values[i], 0, 1);
               srcList.options[i] = new Option(items[i+to], values[i+to]);
               i--;
          } else {
               srcList.options[i] = new Option(items[i], values[i]);
        }
     }
     srcList.focus();
}

function getSelectedOption( frmName, srcListName ) {
     var form = eval( 'document.' + frmName );
     var srcList = eval( 'form.' + srcListName );

     i = srcList.selectedIndex;
     if (i != null && i > -1) {
          return srcList.options[i];
     } else {
          return null;
     }
}

function setSelectedValue( frmName, srcListName, value ) {
     var form = eval( 'document.' + frmName );
     var srcList = eval( 'form.' + srcListName );

     var srcLen = srcList.length;

     for (var i=0; i < srcLen; i++) {
          srcList.options[i].selected = false;
          if (srcList.options[i].value == value) {
               srcList.options[i].selected = true;
          }
     }
}

function getSelectedRadio( frmName, srcGroupName ) {
     var form = eval( 'document.' + frmName );
     var srcGroup = eval( 'form.' + srcGroupName );

     if (srcGroup[0]) {
          for (var i=0, n=srcGroup.length; i < n; i++) {
               if (srcGroup[i].checked) {
                    return srcGroup[i].value;
               }
          }
     } else {
          if (srcGroup.checked) {
               return srcGroup.value;
          } // if the one button is checked, return zero
     }
   // if we get to this point, no radio button is selected
   return null;
}

function getSelectedValue( frmName, srcListName ) {
     var form = eval( 'document.' + frmName );
     var srcList = eval( 'form.' + srcListName );

     i = srcList.selectedIndex;
     if (i != null && i > -1) {
          return srcList.options[i].value;
     } else {
          return null;
     }
}

function getSelectedText( frmName, srcListName ) {
     var form = eval( 'document.' + frmName );
     var srcList = eval( 'form.' + srcListName );

     i = srcList.selectedIndex;
     if (i != null && i > -1) {
          return srcList.options[i].text;
     } else {
          return null;
     }
}

function chgSelectedValue( frmName, srcListName, value ) {
     var form = eval( 'document.' + frmName );
     var srcList = eval( 'form.' + srcListName );

     i = srcList.selectedIndex;
     if (i != null && i > -1) {
          srcList.options[i].value = value;
          return true;
     } else {
          return false;
     }
}

// Form specific functions for editting content images

function showImageProps(base_path) {
     form = document.adminForm;
     value = getSelectedValue( 'adminForm', 'imagelist' );
     parts = value.split( '|' );
     form._source.value = parts[0];
     setSelectedValue( 'adminForm', '_align', parts[1] || '' );
     form._alt.value = parts[2] || '';
     form._border.value = parts[3] || '0';
     form._caption.value = parts[4] || '';
     setSelectedValue( 'adminForm', '_caption_position', parts[5] || '' );
     setSelectedValue( 'adminForm', '_caption_align', parts[6] || '' );
     form._width.value = parts[7] || '';

     //previewImage( 'imagelist', 'view_imagelist', base_path );
     srcImage = eval( "document." + 'view_imagelist' );
     srcImage.src = base_path + parts[0];
}

function applyImageProps() {
     form = document.adminForm;
     if (!getSelectedValue( 'adminForm', 'imagelist' )) {
          alert( "Select and image from the list" );
          return;
     }
     value = form._source.value + '|'
     + getSelectedValue( 'adminForm', '_align' ) + '|'
     + form._alt.value + '|'
     + parseInt( form._border.value ) + '|'
     + form._caption.value + '|'
     + getSelectedValue( 'adminForm', '_caption_position' ) + '|'
     + getSelectedValue( 'adminForm', '_caption_align' ) + '|'
     + form._width.value;
     chgSelectedValue( 'adminForm', 'imagelist', value );
}

function previewImage( list, image, base_path ) {
     form = document.adminForm;
     srcList = eval( "form." + list );
     srcImage = eval( "document." + image );
     var fileName = srcList.options[srcList.selectedIndex].text;
     var fileName2 = srcList.options[srcList.selectedIndex].value;
     if (fileName.length == 0 || fileName2.length == 0) {
          srcImage.src = 'images/blank.gif';
     } else {
          srcImage.src = base_path + fileName2;
     }
}

/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
* @param An alternative field name
*/
function checkAll( n, fldName ) {
  if (!fldName) {
     fldName = 'cb';
  }
     var f = document.adminForm;
     var c = f.toggle.checked;
     var n2 = 0;
     for (i=0; i < n; i++) {
          cb = eval( 'f.' + fldName + '' + i );
          if (cb) {
               cb.checked = c;
               n2++;
          }
     }
     if (c) {
          document.adminForm.boxchecked.value = n2;
     } else {
          document.adminForm.boxchecked.value = 0;
     }
}

function listItemTask( id, task ) {
    var f = document.adminForm;
    cb = eval( 'f.' + id );
    if (cb) {
        for (i = 0; true; i++) {
            cbx = eval('f.cb'+i);
            if (!cbx) break;
            cbx.checked = false;
        } // for
        cb.checked = true;
        f.boxchecked.value = 1;
        submitbutton(task);
    }
    return false;
}

function hideMainMenu()
{
     document.adminForm.hidemainmenu.value=1;
}

function isChecked(isitchecked){
     if (isitchecked == true){
          document.adminForm.boxchecked.value++;
     }
     else {
          document.adminForm.boxchecked.value--;
     }
}

/**
* Default function.  Usually would be overriden by the component
*/
function submitbutton(pressbutton) {
     submitform(pressbutton);
}

/**
* Submit the admin form
*/
function submitform(pressbutton){
     document.adminForm.task.value=pressbutton;
     try {
          document.adminForm.onsubmit();
          }
     catch(e){}
     document.adminForm.submit();
}

/**
* Submit the control panel admin form
*/
function submitcpform(sectionid, id){
     document.adminForm.sectionid.value=sectionid;
     document.adminForm.id.value=id;
     submitbutton("edit");
}

/**
* Getting radio button that is selected.
*/
function getSelected(allbuttons){
     for (i=0;i<allbuttons.length;i++) {
          if (allbuttons[i].checked) {
               return allbuttons[i].value
          }
     }
}

// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
     cal.sel.value = date; // just update the value of the input field
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
     cal.hide();              // hide the calendar

     // don't check mousedown on document anymore (used to be able to hide the
     // calendar when someone clicks outside it, see the showCalendar function).
     Calendar.removeEvent(document, "mousedown", checkCalendar);
}

// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown.  If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
     var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
     for (; el != null; el = el.parentNode)
     // FIXME: allow end-user to click some link without closing the
     // calendar.  Good to see real-time stylesheet change :)
     if (el == calendar.element || el.tagName == "A") break;
     if (el == null) {
          // calls closeHandler which should hide the calendar.
          calendar.callCloseHandler(); Calendar.stopEvent(ev);
     }
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id) {
     var el = document.getElementById(id);
     if (calendar != null) {
          // we already have one created, so just update it.
          calendar.hide();         // hide the existing calendar
          calendar.parseDate(el.value); // set it to a new date
     } else {
          // first-time call, create the calendar
          var cal = new Calendar(true, null, selected, closeHandler);
          calendar = cal;          // remember the calendar in the global
          cal.setRange(1900, 2070);     // min/max year allowed
          calendar.create();       // create a popup calendar
     }
     calendar.sel = el;       // inform it about the input field in use
     calendar.showAtElement(el);   // show the calendar next to the input field

     // catch mousedown on the document
     Calendar.addEvent(document, "mousedown", checkCalendar);
     return false;
}

/**
* Pops up a new window in the middle of the screen
*/
function popupWindow(mypage, myname, w, h, scroll) {
     var winl = (screen.width - w) / 2;
     var wint = (screen.height - h) / 2;
     winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
     win = window.open(mypage, myname, winprops)
     if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

function mosDHTML(){
     this.ver=navigator.appVersion
     this.agent=navigator.userAgent
     this.dom=document.getElementById?1:0
     this.opera5=this.agent.indexOf("Opera 5")<-1
     this.ie5=(this.ver.indexOf("MSIE 5")<-1 && this.dom && !this.opera5)?1:0;
     this.ie6=(this.ver.indexOf("MSIE 6")<-1 && this.dom && !this.opera5)?1:0;
     this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
     this.ie=this.ie4||this.ie5||this.ie6
     this.mac=this.agent.indexOf("Mac")<-1
     this.ns6=(this.dom && parseInt(this.ver) <= 5) ?1:0;
     this.ns4=(document.layers && !this.dom)?1:0;
     this.bw=(this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5);

     this.activeTab = '';
     this.onTabStyle = 'ontab';
     this.offTabStyle = 'offtab';

     this.setElemStyle = function(elem,style) {
          document.getElementById(elem).className = style;
     }
     this.showElem = function(id) {
          if (elem = document.getElementById(id)) {
               elem.style.visibility = 'visible';
               elem.style.display = 'block';
          }
     }
     this.hideElem = function(id) {
          if (elem = document.getElementById(id)) {
               elem.style.visibility = 'hidden';
               elem.style.display = 'none';
          }
     }
     this.cycleTab = function(name) {
          if (this.activeTab) {
               this.setElemStyle( this.activeTab, this.offTabStyle );
               page = this.activeTab.replace( 'tab', 'page' );
               this.hideElem(page);
          }
          this.setElemStyle( name, this.onTabStyle );
          this.activeTab = name;
          page = this.activeTab.replace( 'tab', 'page' );
          this.showElem(page);
     }
     return this;
}
var dhtml = new mosDHTML();

function MM_findObj(n, d) { //v4.01
     var p,i,x;
     if(!d) d=document;
     if((p=n.indexOf("?"))>0&&parent.frames.length) {
          d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
     }
     if(!(x=d[n])&&d.all) x=d.all[n];
     for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
     for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
     if(!x && d.getElementById) x=d.getElementById(n);
     return x;
}
function MM_swapImage() { //v3.0
     var i,j=0,x,a=MM_swapImage.arguments;
     document.MM_sr=new Array;
     for(i=0;i<(a.length-2);i+=3)
     if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;
     if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
     var i,x,a=document.MM_sr;
     for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
     var d=document;
     if(d.images){
     if(!d.MM_p) d.MM_p=new Array();
     var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
     for(i=0; i<a.length; i++)
     if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function saveorder( n ) {
     checkAll_button( n );
     submitform('saveorder');
}

//needed by saveorder function
function checkAll_button( n ) {
     for ( var j = 0; j <= n; j++ ) {
          box = eval( "document.adminForm.cb" + j );
          if ( box.checked == false ) {
               box.checked = true;
          }
     }
}
/**
* @param object A form element
* @param string The name of the element to find
*/
function getElementByName( f, name ) {
     if (f.elements) {
          for (i=0, n=f.elements.length; i < n; i++) {
               if (f.elements[i].name == name) {
                    return f.elements[i];
               }
          }
     }
     return null;
}
// directory of where all the images are
var cmThemeOfficeBase;
//= 'index1_files/';

var cmThemeOffice =
{
     // main menu display attributes
     //
     // Note.  When the menu bar is horizontal,
     // mainFolderLeft and mainFolderRight are
     // put in <span></span>.  When the menu
     // bar is vertical, they would be put in
     // a separate TD cell.

     // HTML code to the left of the folder item
     mainFolderLeft: '&nbsp;',
     // HTML code to the right of the folder item
     mainFolderRight: '&nbsp;',
     // HTML code to the left of the regular item
     mainItemLeft: '&nbsp;',
     // HTML code to the right of the regular item
     mainItemRight: '&nbsp;',

     // sub menu display attributes

     // 0, HTML code to the left of the folder item
     folderLeft: '<img alt="" src="images/spacer.png">',
     // 1, HTML code to the right of the folder item
     folderRight: '<img alt="" src="images/arrow.png">',
     // 2, HTML code to the left of the regular item
     itemLeft: '<img alt="" src="images/spacer.png">',
     // 3, HTML code to the right of the regular item
     itemRight: '<img alt="" src="images/blank.png">',
     // 4, cell spacing for main menu
     mainSpacing: 0,
     // 5, cell spacing for sub menus
     subSpacing: 0,
     // 6, auto dispear time for submenus in milli-seconds
     delay: 500
};

// for horizontal menu split
var cmThemeOfficeHSplit = [_cmNoAction, '<td class="ThemeOfficeMenuItemLeft"></td><td colspan="2"><div class="ThemeOfficeMenuSplit"></div></td>'];
var cmThemeOfficeMainHSplit = [_cmNoAction, '<td class="ThemeOfficeMainItemLeft"></td><td colspan="2"><div class="ThemeOfficeMenuSplit"></div></td>'];
var cmThemeOfficeMainVSplit = [_cmNoAction, '&nbsp;'];