// The leftbar menu was written to work with IE 6 and above.

// Used for determining when the collapse child menus
var intNavigating = []; // new Array();  Used in the timed collapse of child menus
    intNavigating[0] = 999; // The 0 array item has a special use to allow for an immediate collapse of the specified child menu, ie no timed collapse
    intNavigating[1] = 0;   // Used for level 1 child menus collapsing
    intNavigating[2] = 0;   // Used for level 2 child menus collapsing
    intNavigating[3] = 0;   // Used for level 3 child menus collapsing

function writeline(strValue)
  { 
  /*
   Purpose: Write information to a DIV for display.  
   
   Notes: For testing purposes only.  This can be removed from
          production applications. Its purpose is to display
		  information in a predefined div tag as script runs 
		  in the example flyout HTML page.
  */
  var divOut = document.getElementById("divOut");  // A object on page flyout.html used to display information messages
  var strOut = "";  // Used to hold the combined value of the current DIV and the additional string passed in

  strOut = divOut.innerHTML + "<BR>" + strValue;
  divOut.innerHTML = strOut;
  }

function initPage()
  {
	 /* 
     Purpose: Run page initialization events
    
     Notes: This is called from the Body_Onload event. 
    
            The naming of levels must follow a specific format, 
			ex: Level1A1 
			
            Level - The most basic portion of the name
                1 - This indicates it is a level 1 menu level.  
				    There may be up to three menu levels (1-3)
                A - This indicates it is sub level 'A' of level 1. 
                1 - This indicates it is sub level 1 of sub level 'A'. 
            The two sub level indicators allow for 1296 unique 
			level names at each level (36 * 36). 
    */
	
    /* 
	Turn off the display property for each level if it exists.
	By doing it here it will allow the child menus to display 
	in case scripting is turned off.  */
	if (document.getElementById('Level1A1_Child'))
	  {
     document.getElementById('Level1A1_Child').style.display = "none";
	  }
	if (document.getElementById('Level1B1_Child'))
	  {
     document.getElementById('Level1B1_Child').style.display = "none";
	  }
	if (document.getElementById('Level1C1_Child'))
	  {
     document.getElementById('Level1C1_Child').style.display = "none";
	  }
	if (document.getElementById('Level1D1_Child'))
	  {
     document.getElementById('Level1D1_Child').style.display = "none";
	  }
	if (document.getElementById('Level1E1_Child'))
	  {
     document.getElementById('Level1E1_Child').style.display = "none";
	  }
	if (document.getElementById('Level1F1_Child'))
	  {
     document.getElementById('Level1F1_Child').style.display = "none";
	  }

    // Assign the CSS class to the left bar div so in case
    // scripting is turned off it does not appear as a flyout
    // There are the three code ways of setting a class on an element:
    //    document.getElementById("leftbardiv").setAttribute("class","leftbarMenu"); This solution works for all major non-IE browsers (e.g. Firefox, Safari, Netscape).
    //    document.getElementById("leftbardiv").setAttribute("className","leftbarMenu"); This solution works for IE but not for any other major browsers.
    //    The example below works in all major browsers
	// If the form does not have a left bar this will not run
    if (document.getElementById("leftbardiv"))
	  {
    document.getElementById("leftbardiv").className = "leftbar_flyout"; 
	  }

    return;
  }

function toggleList(e)
{ 
  /*
   Purpose: Toggle the menus expanded/collapsed view
   Inputs: Parent of child menu to expand/collapse
  
  */
  
  // Don't run if document.getElementById method is not valid for browser
  if (document.getElementById)
  {
    var strItemID = e.id + "_Child"; // The name of the child UL element
    // Stop the event that called this routine from bubbling up to its parent
     suppressBubble(e)
	 
     if (document.getElementById(strItemID).style.display  == "none")
     {
       document.getElementById(strItemID).style.display = "";
       document.getElementById(e.id).style.listStyleImage="none";
       document.getElementById(e.id).alt = "Click to collapse menu";
       document.getElementById(e.id).title = "Click to collapse menu";
     }
     else
     {
       document.getElementById(strItemID).style.display = "none";
       document.getElementById(e.id).style.listStyleImage="none";
       document.getElementById(e.id).alt = "Click to expand menu";
       document.getElementById(e.id).title = "Click to expand menu";
     }
  }	
return;
}

function suppressBubble(e)
{
	/*
  Purpose: Stop the event the called this routine from bubbling up 
           to its parent
  Inputs: The element that needs it event bubbling cancelled.
  
   */

    if (window.event)
       {
       // IE browsers
       window.event.cancelBubble = true;
       }
      else
       {
       // non-IE, like Firefox
       e.cancelBubble = true;  
       }
}

function collapseList(e, intLevel)
{
  /*	 
   Purpose: Collapse the child menu
   Inputs: - Parent of child menu to collapse
           - level indicator for child menu 
   Notes:  The object that called this function.  To allow 
           cancelling of event bubbling
           The intLevel variable will be zero immediate collapse
		   is desired. This means this method was called directly. 				           This is the only time the zero array index should be used. 
  
  */
  // Don't run if document.getElementById method is not valid for browser
  if (document.getElementById)
  {
    // Stop the bubble-up to of events
    suppressBubble(e)

      var  strItemID = e.id + "_Child"; // The name of the child UL element
      var intValue = null; // Used to hold the correct setTimeout return value

      // Get the appropriate timer value based on the level
      // If the time value is not null then the child menu will
      // be collapsed. 
      if (intLevel === 0)
        {
        intValue = intNavigating[0];
        }
      else if(e.id.substring(0,6) == 'Level1')
        {
        intValue = intNavigating[1];
        }
      else if(e.id.substring(0,6) == 'Level2')
        {
        intValue = intNavigating[2];
        }
      else if(e.id.substring(0,6) == 'Level3')
        {
        intValue = intNavigating[3];
        }
      else
        {
        alert("Unknown level in Collapse '" + e.id.substring(0,6) + "'");
        }

      if(intValue !== null)
        {
          document.getElementById(strItemID).style.display = "none";
          document.getElementById(e.id).style.listStyleImage='none';
          document.getElementById(e.id).alt = 'Click to expand menu';
          document.getElementById(e.id).title = "Click to expand menu";
        }
  }	
return;
}

function collapseListAtSameLevel(e)
  {
    /*   
    Purpose: Close all child menu items at the same level, and 
	their children, other than the child of the level specified
	in the input parameter.
   
    Inputs: The name of the menu level that should remain open.
    Notes:  Called from onmouseover and onfocus events. 
   
            You need to close any open child levels for the 
			elements (e) that have an ID, and are on the same level,
			as the input parameter to prevent children elements 
			from being left open. 
   
            The order of checking the intLevel variable MUST be
			performed from the outmost child to Level1 to avoid 
			a ghosting display of the child levels list. 
   */

   var intLevel; // The level number
   var  strLevelAlpha; // The associated alpha identifier for the level

    // The event bubble is stopped in the Expand function so no need to repeat it here since the only
   //  place this function is called from is the Expand function.

   // Get the parent element of the element provided
   var theParent = e.parentNode; 
   var currentNode; // Used to hold current node 
   // Loop through the parent element and locate all child menus
   // with a name other than the one passed in. 
   for (i=0;i<theParent.childNodes.length;i++)
     {
       currentNode=theParent.childNodes[i];
       if(currentNode.nodeType === 1 && currentNode.id !== '')
         {
          if (e.id != currentNode.id)
             {
             intLevel = Number(currentNode.id.substring(5,6)); 
             strLevelAlpha = currentNode.id.substring(6); // Take the remainder of the level name after the level indicator
	     if(intLevel <= 3)
	       {
               // Wil close for levels 3-1 if exist
           if (document.getElementById('Level3' + strLevelAlpha))
			 {
 	       collapseList(document.getElementById('Level3' + strLevelAlpha ), 0);
			 }						     
	       }

	     if(intLevel <= 2)
	       {
               // Wil close for levels 2-1 if exist
           if (document.getElementById('Level2' + strLevelAlpha))
			 {
 	       collapseList(document.getElementById('Level2' + strLevelAlpha ), 0);
			 }						     
	       }

	   if(intLevel == 1)
	       {
               // Wil close for level 1
	       collapseList(document.getElementById('Level1' + strLevelAlpha ), 0);
	       }

	    if(intLevel > 3 | intLevel < 1)
	       {
	       alert("Unknown level in collapseListAtSameLevel '" + String(intLevel) + "'. Contact MIS");
   	       }
             }
         }
     }

  }

function cancelCollapseList(e, intLevel)
  {
	 /* 
     Purpose: Cancel the timed collapse for the specified level
    
     Inputs: - The element whose event called this function
             - Level indicator for child menu
			 
     Notes: Called from onmouseover and onfocus events.  The 
	        routine will clear the timeout variables for each 
			level <= the passed in value.
    
            The order you cancel the Collapse for each intLevel 
			does not appear to matter but was done from high level
			to low to be consistent with functions 
			collapseListAtSameLevel and timedCollapse (read their 
			note sections for more detail). 
   */

      // Stop the bubble up of events
      suppressBubble(e)

    if (intLevel == 3)
      {
       window.clearTimeout(intNavigating[3]); 
       intNavigating[3]=null;
      }

    if (intLevel >= 2)
      {
       window.clearTimeout(intNavigating[2]); 
       intNavigating[2]=null;
      }

    if (intLevel >= 1)
      {
       window.clearTimeout(intNavigating[1]); 
       intNavigating[1]=null;
      }

    if(intLevel > 3 | intLevel < 1)
      {
        alert("Unknown level in cancelCollapseList '" + String(intLevel) + "'. Contact MIS");
      }
  }

function expandList(e)
{
  /*	
   Purpose: Expand the child menu
   Inputs: - Parent of child menu to expand
  
   Notes: There are two special function calles within this routine.
   
   The first makes sure there are no flyout menus at the same level
   left open and the second cancels any pending menu collapse for 
   the current menu level and any menu levels below it. 
  */
  
  // Don't run if document.getElementById method is not valid for browser
  if (document.getElementById)
  {
     var strItemID = e.id + "_Child"; // The name of the child UL element
     // Stop the event that called this routine from bubbling up to its parent
     suppressBubble(e)

     collapseListAtSameLevel(e);  // Make sure no other flyouts, at the same level, are left open
     cancelCollapseList(e, Number(e.id.substring(5,6)));  // Get the menu level number and pass to function

     // Only expand the menu if it is not already expanded.  Due to some
     // allowed event bubbling this may occur. 
     if (document.getElementById(strItemID).style.display == "none")
       {
         document.getElementById(strItemID).style.display = "";
         document.getElementById(e.id).style.listStyleImage="none";
         document.getElementById(e.id).alt = "Click to collapse menu";
         document.getElementById(e.id).title = "Click to collapse menu";
       }
  }	
return;
}

function timedCollapse(e)
  {
	/* 
     Purpose: Start the timer to collapse the specified child 
	 menu level
	 
     Inputs: The highest level that needs collapsing
     Notes:  Internet Explorer supports using the setTimeout as so 
	         you could use: 
             intNavigating[3] = window.setTimeout("collapseList(" + e.id + ")",2000);
             This is not supported by all browsers, like FireFox so 
			 I used the more highly support call you see below. 
             intNavigating[3] = window.setTimeout(function() {collapseList(e);},2000);
    
             This is called from mouseout, and onblur 
    
             The order of checking the intLevel variable MUST be
			 performed from the outmost child to Level1 to avoid 
			 a ghosting display of the child levels list. 
    */

    // Stop the bubble-up to of events
    suppressBubble(e)

     // Set the appropriate timer value based on the level
     // If the time value is not null then the child menu will
     // be collapsed. 
 
     var intLevel = Number(e.id.substring(5,6)); // Get the level number
     var strLevelAlpha = e.id.substring(6); // Take the remainder of the level name after the level indicator

     if(intLevel == 3)
       {
       intNavigating[3] = window.setTimeout(function() {collapseList(document.getElementById('Level3' + strLevelAlpha ), 3);},2000);
       }

     if(intLevel >= 2)
       {
       intNavigating[2] = window.setTimeout(function() {collapseList(document.getElementById('Level2' + strLevelAlpha ), 2);},2000);
       }

     if(intLevel >= 1)
       {
       intNavigating[1] = window.setTimeout(function() {collapseList(document.getElementById('Level1' + strLevelAlpha ), 1);},2000);
       }

    if(intLevel > 3 | intLevel < 1)
       {
       alert("Unknown level in timedCollapse '" + String(intLevel) + "'. Contact MIS");
       }

    return;
  }

