/* ----------------------------------------------------------------------------
  CLIENT SNIFFING
  Purpose:  Inline code determines browser/version/platform and sets global
            varibables for use by other clientside functions
---------------------------------------------------------------------------- */

/* ----------------------------------------------------------------------------
  Global variable declarations
---------------------------------------------------------------------------- */
var g_bIsMSIE;          // true = Internet Explorer browser
var g_bIsWin;           // true = Windows operating system
var g_nBrowserVersion;  // browser version (integer)

var g_bIsNN4 = false;   // true = Netscape 4.x -- since there are so
                        //   many special cases that apply only to
                        //   NN4, this is an especially handy var.

var g_bIsIE4 = false;   // true = IE 4.x -- does not support document.getElementById
                        //   nor document.getElementsByTagName

var g_bIsW3 = false;    // true = NN 6+ or MSIE 4+

var g_strCurrentMenu = "";

/* ----------------------------------------------------------------------------
  Perform client sniffing
---------------------------------------------------------------------------- */
// Test for Netscape browser - if not Netscape the browser is most
// likely MSIE-compatible.
g_bIsMSIE = (-1 == navigator.appName.indexOf("Netscape"));

// Get browser version
if (g_bIsMSIE)
{
  if (navigator.appVersion.indexOf("MSIE 5.") > -1)
  {
    // special case MSIE 5 since it lists 4.0 as the appversion
    g_nBrowserVersion = 5;
  }
  else
  {
    // Set the current browser version
    g_nBrowserVersion = parseInt(navigator.appVersion);
  }

  if (navigator.appVersion.indexOf("MSIE 4") > -1)
  {
    g_bIsIE4 = true;
  }
  else if (g_nBrowserVersion >= 4)
  {
    g_bIsW3 = true;
  }
}
else
{
  // Set the current browser version
  g_nBrowserVersion = parseInt(navigator.appVersion);

  if (g_nBrowserVersion < 5) // NN4
  {
    g_bIsNN4 = true;
  }
  else  // NN6
  {
    g_bIsW3 = true;
  }
}

// If browser is 4.0+ test for Windows Operating Systems
if (g_nBrowserVersion >= 4)
{
  g_bIsWin = (navigator.platform.indexOf("Win") > -1);
}

/* ----------------------------------------------------------------------------
  Copyright (c) 2002 Washington Mutual.  All rights reserved. 
  Project:              SBOB
  Filename:             W3Menus.js
  Purpose:              Functions for W3 DHTML menus
  Author:               Jim Speaker, jim.speaker@wamu.net
  Creation Date:        July 22, 2002
  Dependencies:         none
---------------------------------------------------------------------------- */

/* ----------------------------------------------------------------------------
  GLOBAL variable declarations required for W3 browsers
---------------------------------------------------------------------------- */
var g_oMenu;    /* menu object var, global scope required for
                   usage in timeout */
var g_oOriginalElement; /* save state on original element used in getPos calls
                           to handle getting position of SELECT elements that
                           are on DIVs */
var g_iLongestMenuBottom = 0; /* save state on the bottom position of the longest
                                 menu */
var g_bIsWin = (navigator.platform.indexOf("Win") > -1);
var g_bIsMSIE = (-1 == navigator.appName.indexOf("Netscape"));

/* ----------------------------------------------------------------------------
  Function:       navigateTo
  Purpose:        displays the "on" image for the left nav based on the 
  Affects:        default image displayed in left menu
  Arguments In:   sURL = the URL to navigate to - pass in a virtual path
  Arguments Out:  nothing
  Dependencies:		strCurrentMenu is defined in calling page
---------------------------------------------------------------------------- */
function setCurrentContext(strCurrentMenu)
{
	if (document.images) {
		g_strCurrentMenu = strCurrentMenu;
		var imgFocus = document.images["img" + strCurrentMenu];
		if (null != imgFocus){
			imgFocus.src = "/images/" + strCurrentMenu + "_1.gif";
		}
	}
}

/* ----------------------------------------------------------------------------
  Function:       navigateTo
  Purpose:        navigates to the referenced URL
  Affects:        location object of the top level window
  Arguments In:   sURL = the URL to navigate to - pass in a virtual path
  Arguments Out:  nothing
---------------------------------------------------------------------------- */
function navigateTo(sURL)
{
  if ("" != sURL)
  {
    window.top.document.location.href = sURL;
  }
}

/* ----------------------------------------------------------------------------
  Function:       handleClick
  Purpose:        handles click events on menu items by loading the page
                  referred to in the menu element's href property
  Affects:        window.top.document.location.href
  Arguments In:   oMenu = reference to the menu element clicked
  Arguments Out:  true = allow event sequence to execute normally
                  false = stop the event sequence from executing normally
---------------------------------------------------------------------------- */
function handleClick(oMenu)
{
  var sHREF = "";
 
  var oaAnchors = oMenu.getElementsByTagName("A");
  /* ensure that there is 1 anchor in the array and the passed menu is a
     compliant menu */
  if (1 == oaAnchors.length)
  { 
    /* the target attribute value "menuitem" specifies that the element
       should load a page  */
    if ("menuitem" == oaAnchors[0].target)
    {
      /* Create the new location string */
      sHREF = oaAnchors[0].href;
  
      if ("" != sHREF)
      {
        /* Set location which causes the referenced URL to load and
           do not allow the normal event sequence to execute */
        cancelEvent();
        navigateTo(sHREF);
        return false;
      }
      else
      {
        cancelEvent();
        return false;
      }
    }
    /* the element should have returned "menuitem" if it was supposed to load
       a page so cancel the event */
    else
    { 
      cancelEvent();
      return false;
    }
  }
  /* the function was fired by an event that came from a non-menu-element
     so allow the event to propagate and execute normally */
  else
  {
    return true;
  }
}

/* ----------------------------------------------------------------------------
  Function:       handleMouseOver
  Purpose:        handles mouseover events on menu items by showing the
                  menu associated with the event src element and
                  changing color of the event src element text
  Affects:        g_oTimeout and runtime styles
  Arguments In:   oMenu = reference to the menu element clicked
  Arguments Out:  nothing
---------------------------------------------------------------------------- */
function handleMouseOver(oMenu)
{
	//Change image
	if (document.images) {
		var strItem = oMenu.id.substr(3, (oMenu.id.length - 7));
		var imgMenu = document.images["img" + strItem];
		if (null != imgMenu){
			imgMenu.src = "/images/" + strItem + "_1.gif";
		}
	}
	
	//Display submenu
  /* If a pending timeout exists (which will hide the menu upon
     completion) clear it so the menu stays visible - this test also ensures
     that the element firing this event handler is the MenuItems layer OR that
     a MenuItems layer exists for the Menu layer that is firing the event */
  if ("undefined" != typeof g_oTimeout &&
      g_oTimeout != null &&
      ((oMenu.id.length - 5) == oMenu.id.lastIndexOf("Items") ||
      document.getElementById(oMenu.id.substr(0, (oMenu.id.length - 4)) + "Items")))
  {
    clearTimeout(g_oTimeout);
    g_oTimeout = null;
  }

  /* Test for a MenuItems layer, if it is a MenuItems layer that is firing
     this event handler there is no need to do a hideall/show - if it is not a
     MenuItems layer then this is a top-level menu (secondary nav item in SBOB)
     and should do a hideall/show */
  if ((oMenu.id.length - 5) != oMenu.id.lastIndexOf("Items"))
  {
    var oMenu = document.getElementById(oMenu.id.substr(0, (oMenu.id.length - 4)) + "Items");
    if (null != oMenu)
    {
      /* When showing a new menu, first hide all menus */
      hideAllMenus();
      
      /* show the menu */
      showMenu(oMenu);
    }
  }
}

/* ----------------------------------------------------------------------------
  Function:       handleMouseOut
  Purpose:        handles mouseout events on menu items by hiding the
                  menuitems associated with the event src element
  Affects:        runtime styles
  Arguments In:   oMenu = reference to the menu element clicked
  Arguments Out:  nothing
---------------------------------------------------------------------------- */
function handleMouseOut(oMenu)
{
	//Change image
	if (document.images) {
		var strItem = oMenu.id.substr(3, (oMenu.id.length - 7));
		if (strItem != g_strCurrentMenu)
		{
			var imgMenu = document.images["img" + strItem];
			if (null != imgMenu){
				imgMenu.src = "/images/" + strItem + "_0.gif";
			}
		}
	}
	
	//Hide menu
  /* If the element being passed in is a layer but is not the MenuItems layer
     then get a reference to the corresponding MenuItems layer */
  if ("DIV" == oMenu.tagName &&
      (oMenu.id.length - 5) != oMenu.id.lastIndexOf("Items"))
  {
    oMenu = document.getElementById(oMenu.id.substr(0, (oMenu.id.length - 4)) + "Items");
  }

  /* Ensure that there is a valid reference to a layer */
  if (null != oMenu &&
      "DIV" == oMenu.tagName)
  {
    /* hide the menu */
    hideMenu(oMenu);
  }
}

/* ----------------------------------------------------------------------------
  Function:       showMenu
  Purpose:        Shows the menu items associated with the referenced
                  menu element
  Affects:        runtime styles
  Arguments In:   oMenu = reference to the menu element
  Arguments Out:  nothing
---------------------------------------------------------------------------- */
function showMenu(oMenu)
{
  /* make sure overlapping select lists are handled in MSIE - Netscape 6.2
     does not have the "windowless control" issue, and the issue does not
     exist in MSIE on MacOS */
  if (g_bIsWin && g_bIsMSIE && !(g_bIsIE4) && "DIV" == oMenu.tagName)
  {  
    hideOverlappingSelects(oMenu);
  }

  /* show the menu */
  oMenu.style.visibility = "visible";
}

/* ----------------------------------------------------------------------------
  Function:       hideMenu
  Purpose:        Instantiates a timeout to hide the menu items
                  associated with the referenced menu element
  Affects:        runtime styles
  Arguments In:   oMenu = reference to the menu element
  Arguments Out:  nothing
---------------------------------------------------------------------------- */
function hideMenu(oMenu)
{
  /* If a pending timeout exists (which will hide the menu upon
     completion) clear it so the menu stays visible - this test also ensures
     that the element firing this event handler is the MenuItems layer OR that
     a MenuItems layer exists for the Menu layer that is firing the event */
  if ("undefined" != typeof g_oTimeout &&
      g_oTimeout != null &&
      ((oMenu.id.length - 5) == oMenu.id.lastIndexOf("Items") ||
      document.getElementById(oMenu.id.substr(0, (oMenu.id.length - 4)) + "Items")))
  {
    clearTimeout(g_oTimeout);
    g_oTimeout = null;
  }

  /* set the global menu reference to the new local menu reference
     for the forthcoming timeout call */
  g_oMenu = oMenu;

  /* create a timeout object to hide the menu if is not cleared
     within 250 mSec */
  g_oTimeout = null;
  g_oTimeout = setTimeout("hideMenuComplete(g_oMenu)", 250);
}

/* ----------------------------------------------------------------------------
  Function:       hideMenuComplete
  Purpose:        Hides the menu items associated with the referenced
                  menu element
  Affects:        runtime styles
  Arguments In:   oMenu = reference to the menu element
  Arguements Out: nothing
---------------------------------------------------------------------------- */
function hideMenuComplete(oMenu)
{
  /* redisplay any hidden SELECT lists in MSIE */
  if (g_bIsWin && g_bIsMSIE && !(g_bIsIE4))
  {
    showAllSelects();
  }
  
  /* hide the menu */
  oMenu.style.visibility = "hidden";
}

/* ----------------------------------------------------------------------------
  Function:       hideAllMenus
  Purpose:        Hides all menu items except top level menu items
  Affects:        runtime styles
  Arguments In:   oMenu = reference to the menu element
  Arguments Out:  nothing
---------------------------------------------------------------------------- */
function hideAllMenus()
{
  /* Set variables and get a reference to all DIVs in the document
     as an array */
  var oaMenus = document.getElementsByTagName("DIV");
  var iLen = oaMenus.length;
  var iCnt;
  
  /* iterate the DIVs and hide all divs that are Menus but are not
     top level menu items */
  for (iCnt = 0; iCnt < iLen; iCnt++)
  {
    if (oaMenus[iCnt].id.indexOf("Items") > -1 &&
        "undefined" != typeof oaMenus[iCnt].childNodes[0])
    {
      oaMenus[iCnt].style.visibility = "hidden";
    }
  }
}

/* ----------------------------------------------------------------------------
  Function:       getLongestMenuBottom
  Purpose:        Gets the bottom position of the longest DHTML menu
  Affects:        g_iLongestMenuBottom
  Arguments In:   none
  Arguments Out:  nothing
  Notes:          MSIE on Windows only
---------------------------------------------------------------------------- */
function getLongestMenuBottom()
{
  var colDIVs = document.getElementsByTagName("DIV");
  var oDIV;
  var iDIVsLen = colDIVs.length;
  var iDIVsCnt = 0;
  var iDIVTop = 0;
  var iDIVBottom = 0;

  for (iDIVsCnt = 0; iDIVsCnt < iDIVsLen; iDIVsCnt++)
  {
    oDIV = colDIVs[iDIVsCnt];
            
    if (("undefined" != typeof oDIV.id) &&
        ("undefined" != typeof oDIV.tagName) &&
        0 == oDIV.id.indexOf("lyr") && 
        (oDIV.id.length - 5) == oDIV.id.indexOf("Items"))
    {
      
      g_oOriginalElement = oDIV;
        
      iDIVBottom = getPos(oDIV, "Top") + getSize(oDIV, "Height");
      /* save state on the bottom position of the longest menu */
      if (iDIVBottom > g_iLongestMenuBottom)
      {
        g_iLongestMenuBottom = iDIVBottom;
      }
    }
  }
}

/* ----------------------------------------------------------------------------
  Function:       hideOverlappingSelects
  Purpose:        Hides all SELECT lists that overlap the currently
                  selected menu and replaces it with a GIF image of a
                  blank select list - this eliminates the z-indexing
                  problem related to windowless controls such as SELECT in
                  MSIE
  Affects:        runtime styles
  Arguments In:   oMenu = reference to the menu element
  Arguments Out:  nothing
  Notes:          MSIE only
---------------------------------------------------------------------------- */
function hideOverlappingSelects(oMenu)
{
  /* if not already set, get the bottom position of the longest menu */
  if (0 == g_iLongestMenuBottom)
  {
    getLongestMenuBottom();
  }

  g_oOriginalElement = oMenu;
  
  var colSelects = document.getElementsByTagName("SELECT"); /* collection of SELECTs */
  var iLen = colSelects.length; /* length of SELECT collection */
  var iCnt; /* counter var for SELECT collection iteration */
  
  var iMenuTop = getPos(oMenu, "Top");
  var iMenuLeft = getPos(oMenu, "Left");
  var iMenuBottom = iMenuTop + getSize(oMenu, "Height");
  var iMenuRight = iMenuLeft + getSize(oMenu, "Width");
  var iSelectTop;
  var iSelectLeft;
  var iSelectWidth;
  var iSelectRight;
  var oFauxSelectDiv; /* div object for creating fauxselect image layers */
  var sFauxSelectID; /* id var for fauxselect */

  /* iterate the collection of SELECT elements and swap out any
     that overlap the area of the menu */
  for (iCnt = 0; iCnt < iLen; iCnt++)
  {
    /* get a reference to the images collection associated with the SELECT
       and determine the position and width of the SELECT */
    colImgs = colSelects[iCnt].parentNode.getElementsByTagName("IMG");
    nImgsLen = colImgs.length;
  
    g_oOriginalElement = colSelects[iCnt];

    iSelectTop = getPos(colSelects[iCnt], "Top");
    iSelectLeft = getPos(colSelects[iCnt], "Left");
    iSelectWidth = getSize(colSelects[iCnt], "Width");
    iSelectRight = iSelectLeft + iSelectWidth;

    /* determine if the SELECT overlaps the menu area */
    if (iSelectTop < iMenuBottom &&
        ((iMenuLeft < iSelectLeft && iMenuRight > iSelectLeft) ||
         (iMenuLeft > iSelectLeft && iSelectRight > iMenuLeft)))
    {
      /* hide the select */
      colSelects[iCnt].style.visibility = "hidden";
      
      /* show select screenshot gif */
      sFauxSelectID = "lyrFauxSelect" + iCnt;
      /* create the layer if it doesn't already exist */
      if (!(eval("document.all." + sFauxSelectID)))
      {
        oFauxSelectDiv = document.createElement("DIV");
        oFauxSelectDiv.id = sFauxSelectID;
        oFauxSelectDiv.style.position = "absolute";
        oFauxSelectDiv.style.top = iSelectTop + "px";
        oFauxSelectDiv.style.left = iSelectLeft + "px";
        oFauxSelectDiv.style.height = "18px";
        oFauxSelectDiv.style.width = iSelectWidth + "px";
        oFauxSelectDiv.style.backgroundColor = "transparent";
        oFauxSelectDiv.style.zIndex = 1;
        oFauxSelectDiv.style.display = "none";
        oFauxSelectDiv.innerHTML = "<img src=\"/images/FauxSelectLeft.gif\" border=\"0\" height=\"18\" width=\"3\" />" +
                                   "<img src=\"/images/FauxSelectMiddle.gif\" border=\"0\" height=\"18\" width=\"" + (iSelectWidth - 21) + "\" />" +
                                   "<img src=\"/images/FauxSelectRight.gif\" border=\"0\" height=\"18\" width=\"17\" />";
      
        /* add the layer to the DOM */
        document.body.appendChild(oFauxSelectDiv);
      }
      
      /* get a reference to the layer in the DOM */
      oFauxSelectDiv = eval("document.all." + sFauxSelectID);
      oFauxSelectDiv.runtimeStyle.display = "inline";
    }
    else
    {
      colSelects[iCnt].style.visibility = "visible";
      sFauxSelectID = "lyrFauxSelect" + iCnt;
      if (eval("document.all." + sFauxSelectID))
      {
        oFauxSelectDiv = eval("document.all." + sFauxSelectID);
        oFauxSelectDiv.runtimeStyle.display = "none";
      }
    }
  }

  event.cancelBubble = true;
}

/* -----------------------------------------------------------------
  Function:       showAllSelects
  Purpose:        Shows all SELECT lists that overlapped the previously
                  selected menu and hides the GIF images of
                  blank select lists - see also, hideOverlappingSelects
  Affects:        runtime styles
  Arguments In:   none
  Arguments Out:  nothing
  Notes:          MSIE only
----------------------------------------------------------------- */
function showAllSelects()
{
  var colSelects = document.getElementsByTagName("SELECT");
  var iLen = colSelects.length;
  var iCnt;
 
  /* iterate the collection of SELECT elements and show all */
  for (iCnt = 0; iCnt < iLen; iCnt++)
  {
    /* hide the faux select image layer if one exists */
    sFauxSelectID = "lyrFauxSelect" + iCnt;
    if (eval("document.all." + sFauxSelectID))
    {
      oFauxSelectDiv = eval("document.all." + sFauxSelectID);
      oFauxSelectDiv.runtimeStyle.display = "none";
    }
    
    /* show the select */
    colSelects[iCnt].style.visibility = "visible";
  }
}

/* ----------------------------------------------------------------------------
  Function:       getPos
  Purpose:        Gets the position of an element.
  Affects:        nothing
  Arguments In:   oElement    = reference to the element
                  sPos        = Position to get (Left or Top)
  Arguments Out:  iPos        = Y-axis position as integer
  Notes:          MSIE only
---------------------------------------------------------------------------- */
function getPos(oElement, sPos)
{
  var iPos = 0;
  /* Ensure the element is valid */
  if ("undefined" != typeof oElement)
  {
    /* Get the offsetTop property of the element, do not measure 
       TBODY, TR, FORM and (DIV when finding SELECT position) tags - 
       these are containing elements that do not affect the measurement */
    if (oElement.tagName != "TBODY" &&
        oElement.tagName != "TR" &&
        oElement.tagName != "FORM" &&
        !("SELECT" == g_oOriginalElement.tagName && "DIV" == oElement.tagName))
    { 
      iPos = eval("oElement.offset" + sPos);
    }
    
    /* Convert the measurement to integer */
    iPos = parseInt(iPos);
    
    /* handle getPos Top calls for SELECT elements on DIVs that are below the longest
       DHTML menu by cancelling recursion and returning a value greater than the
       bottom position of the longest menu */
    if ("SELECT" == g_oOriginalElement.tagName && "DIV" == oElement.tagName && "Top" == sPos && oElement.offsetTop > g_iLongestMenuBottom)
    {
      iPos = g_iLongestMenuBottom + 1;
    }
    /* recurse until the element being checked is a child of BODY */
    else if ("BODY" != oElement.parentNode.tagName.toUpperCase())
    {
      iPos += getPos(oElement.parentElement, sPos);
    }
  }
  return iPos;
}

/* ----------------------------------------------------------------------------
  Function:       getSize
  Purpose:        Gets the Height of an element.
  Affects:        nothing
  Arguments In:   oElement    = reference to the element
                  sSize       = size to get (Height or Width)
  Arguments Out:  iSize     = height as integer
  Notes:          MSIE only
---------------------------------------------------------------------------- */
function getSize(oElement, sSize)
{
  var iSize = eval("oElement.client" + sSize);
  iSize = parseInt(iSize);
  return iSize;
}

/* ----------------------------------------------------------------------------
  Function:       cancelEvent
  Purpose:        cancels an event and prevents the event from bubbling
                  up the DOM
  Affects:        current event object
  Arguments In:   none
  Arguments Out:  nothing
---------------------------------------------------------------------------- */
function cancelEvent(e)
{
  if ("undefined" != typeof event)
  {
    event.cancelBubble = true;
    event.returnValue = false;
  }
}

function GoToCalendar()
{
  var dt = new Date();
  var sYear = dt.getFullYear();
  var sMonth = dt.getMonth() + 1;

  if (sMonth.toString().length == 1)
  {
    sMonth = '0' + sMonth;
  }


  document.location = '/calendar/' + sYear + '/' + sYear + '_' + sMonth + '.shtml';
}
