/*
	Show Fieldsets Script
  Written by: Peter Crackenberg
  Last Changed: 7/10/2006
  
  This script is for use on the George Fox University A-Z site index,
  employee directory, and other situations that require dynamic changing of
  fieldset content.
  
  1/31/06 - This script was changed to add in the correct DOM Event registration
  model, so that the script would play nice with other things that also used
  onload events in the window.
  
  7/10/06 - Script changed to not rely on the "content-right" element that is
  used on the main GFU site.
  
*/

var contentElement;
var fieldSetElements;

// add the initialize function to the DOM Event Model of choice
if(window.addEventListener)
{
    window.addEventListener("load",initScript,true);
}
else
{
    window.attachEvent("onload",initScript);
}

//hide all of the Fieldset elements
document.writeln("<style>#content-right fieldset{display:none;}</style>");

// initialize the script
function initScript()
{

  // get all of the fieldset elements in the "content-right" object, or the body
  // if no content-right element is present
  contentElement = document.getElementById("content-right");
  if(!contentElement)
  {
      contentElement = document.body;
  }
  fieldSetElements = contentElement.getElementsByTagName("fieldset");

  // the element that we need left on
  var elementToSet;
  // all of the cookies for the webpage
  var cookies = document.cookie.split(";");

  // go through the cookies to find the right one to use
  for(i=0; i < cookies.length; i++)
  {
  	if(cookies[i].indexOf("fieldSetDisplay=") > -1)
    {
  		var splitInfo = cookies[i].split("=");
      elementToSet = splitInfo[1];
    }
  }
  
  // if an element hasn't been chosen before by a cookie, then
  // choose the first fieldset element on the page 
  if(!elementToSet)
  {
  	// cut a slice from the ID of the first fieldset by going from the start 
    // of the string to the start of the "_field" in the ID
  	elementToSet = fieldSetElements[0].id.slice(0,fieldSetElements[0].id.indexOf("_field"));
  }

  // look through all of the field set elements that were retrieved, and enable or disable the ones as needed
  for(i=0; i < fieldSetElements.length; i++)
  {
  	// if this is the element we need to turn on, or "show all" was chosen, turn this on
  	if(fieldSetElements[i].id == elementToSet+"_field" || elementToSet == "show all")
    {
  		fieldSetElements[i].style.display = "block";  
    }
    // otherwise, turn it off
    else if(fieldSetElements[i].id.indexOf("_field") > -1)
    {
  		fieldSetElements[i].style.display = "none";  
    }
  }
  
  // add an event handler to all of the links in the nav_buttons tag
  var navButtonsTag = document.getElementById("nav_buttons");
  var navButtonsLinks = navButtonsTag.getElementsByTagName("a");
  
  for(i=0; i < navButtonsLinks.length; i++)
  {
  	navButtonsLinks[i].onclick = displayFieldSet;
  }
}

// display the given field set, after making sure all the other ones are hidden
function displayFieldSet(callingElement)
{
		var fieldSetElements = document.getElementsByTagName("fieldset");
    
    for(i=0; i < fieldSetElements.length; i++)
    {
    	// if the "show all" button was clicked, then show this element
    	if(this.innerHTML.toLowerCase() == "show all")
      {
      	fieldSetElements[i].style.display = "block";
      }
      else
      {
      	// if this field set is the one we want to turn on, turn it on
      	if(fieldSetElements[i].id == this.innerHTML.toLowerCase() + "_field")
        {
  				fieldSetElements[i].style.display = "block"
          
          // add a cookie named "fieldSetDisplay"
          document.cookie = "fieldSetDisplay="+this.innerHTML.toLowerCase();
        }
  			// if this one isn't the one we want on, and it's on, turn it off    
      	else if(fieldSetElements[i].style.display == "block")
        {      
  				fieldSetElements[i].style.display = "none";      
        }
      }
    }
    
    // if the "show all" button was clicked, then set a cookie to show it was
    if(this.innerHTML.toLowerCase() == "show all")
    {
   		document.cookie = "fieldSetDisplay="+this.innerHTML.toLowerCase();
    }
}