var menuSize = 9;                       // This needs to be maintained to the size of the main menu
var displayMenu = new Array(menuSize);

function load() // called when the page is loaded, checks for a saved menu state
{
  var menuState = getCookie("menu");
	if (menuState == null)
	{
		for (var i = 0; i < menuSize; i++)
		{
			displayMenu[i] = false;
		}
	}
	else
	{
		for (var i = 0; i < menuSize; i++)
		{
			if (menuState.substring(i, i + 1) == 0)
			{
				displayMenu[i] = false;
				document.getElementById("menu" + i).style.display="none";
			}
			else
			{
				displayMenu[i] = true;
				document.getElementById("menu" + i).style.display="block";
			}
		}
	}
}

function saveMenuState() // writes a cookie with the current menu state
{
	var state = "";
	for (var i = 0; i < menuSize; i++)
	{
		displayMenu[i] ? state += "1" : state += "0";
	}
	document.cookie = "menu=" + escape(state) + "; path=/; + domain=soilsurveydemo.org;";
}

function showIndex()
/*****************************************************************
  Display the "Home" page from the navigation menu while correctly
  closing all the menu items.
  JHC III                                       12/11/2007 9:19AM
*****************************************************************/
{
  expand(0);
  document.location.href = "/index.asp";
}  // showIndex

function showPhotoGallery()
/*****************************************************************
  Display the "Photo Gallery" page from the navigation menu while
  correctly closing all the menu items.
  JHC III                                       12/11/2007 9:24AM
*****************************************************************/
{

  expand(8);
  document.location.href = "/gallery.asp";

}  // showPhotoGallery

function AdminMenu()
/*****************************************************************
  Display the "Admin Menu" page from the navigation menu while
  correctly closing all the menu items
  JHC III                                       9/29/2008 10:39AM
*****************************************************************/
{

  expand(9);
  document.location.href = "/AdminTools/index.asp"

}  // AdminMenu

function forgotpass()
/*****************************************************************
  Display the "Forgot Password" page from the navigation menu while
  correctly closing all the menu items
  JHC III                                       9/29/2008 10:39AM
*****************************************************************/
{

  expand(0);
  document.location.href = "/AdminTools/passsender.asp"

}  // forgotpass

function showNoPedon()
/*****************************************************************
  Display the "Photo Gallery" page from the navigation menu while
  correctly closing all the menu items.
  JHC III                                       12/11/2007 9:24AM
*****************************************************************/
{

  expand(3);
  document.location.href = "/pedon_nologon.asp";

}  // showNoPedon

function expand(index) // expands or retracts a menu and updates the saved menu state
{
  displayMenu[index] = !displayMenu[index];
  // Close all the menus before opening another - JHC III
  for ( x = 0; x < menuSize; x++ )
  {
    if ( x != index )
    {
      document.getElementById("menu" + x).style.display="none";
      displayMenu[x] = false;
    }                                   // end of if ( x != index )
  }                                     // end of for ( x = 0; x < menuSize; x++ )

	if (displayMenu[index])
	{
		document.getElementById("menu" + index).style.display="block";
		saveMenuState();
	}
	else
	{
		document.getElementById("menu" + index).style.display="none";
		saveMenuState();
	}
}

function getCookie(name) // generic function for getting a cookie
{
	if (document.cookie.length > 0)
	{
    var start = document.cookie.indexOf(name + "=")
		if (start != -1)
		{
			start = start + name.length + 1;
      var end = document.cookie.indexOf(";", start);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(start, end));
		}
	}
	return "";
}
