// Because javascript cookies can suck a dick
function GetCookie(CookieName)
{
  if (document.cookie.length > 0)
  {
    var startpoint = document.cookie.indexOf(CookieName + "=");
    var endpoint = "";
    var returnvalue = null;
    if (startpoint != -1)
    {
      startpoint += CookieName.length + 1; 
      endpoint = document.cookie.indexOf(";", startpoint);
      if (endpoint == -1)
      {
        endpoint = document.cookie.length;
      }
      returnvalue = unescape(document.cookie.substring(startpoint, endpoint));
    }
  }
  return returnvalue;
}

// See above function
function SetCookie(CookieName, CookieValue, CookieExpireDays)
{
  var ExpirationDate = new Date();
  ExpirationDate.setTime(ExpirationDate.getTime() + (parseInt(CookieExpireDays) * 24 * 3600 * 1000));
  document.cookie = CookieName + "=" + escape(CookieValue) + ((CookieExpireDays === null) ? "" : "; expires=" + ExpirationDate.toGMTString());
}

// See above function
function DeleteCookie (CookieName)
{
  if (GetCookie(CookieName))
  {
    document.cookie = CookieName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// Display a message in the help bar
function ShowHelp(str)
{
  if (typeof str == "undefined")
  {
    str = "";
  }
  else
  {
    str = " - " + str;
  }
  document.getElementById("helpbar").innerHTML = "<span class=\"helpbartext\">" + document.title + str + "</span>";
}

// Shows or hides (based on /ugh/ cookie settings) the selected menu group.
function ShowOrHideSubMenu(menuid, toggle)
{
  var menuvisible = GetCookie("aes-menu-visible-" + menuid);
  var submenu = document.getElementById(menuid).style;
  if (toggle)
  {
    if (menuvisible == "false")
    {
      menuvisible = "true";
    }
    else
    {
      menuvisible = "false";
    }
    SetCookie("aes-menu-visible-" + menuid, menuvisible, 1);
  }
  
  // First-time use check
  if (menuvisible != "true" && menuvisible != "false")
  {
    SetCookie("aes-menu-visible-" + menuid, false, 1);
  }
  
  if (menuvisible == "true")
  {
    submenu.position = "static";
    submenu.visibility = "visible";
  }
  else
  {
    submenu.position = "fixed";
    submenu.visibility = "hidden";
  }
}

function InitPage()
{
  ShowOrHideSubMenu("aesicanet", false);
  ShowOrHideSubMenu("bar2", false);
  ShowHelp();
}