function ReadCookie(cookieName) {
  var theCookie=""+document.cookie;
  var ind=theCookie.indexOf(cookieName);
  if (ind==-1 || cookieName=="") return ""; 
  var ind1=theCookie.indexOf(';',ind);
  if (ind1==-1) ind1=theCookie.length; 
  return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function SetCookie(cookieName,cookieValue) {

  var path = '/';
  
  document.cookie = cookieName+"="+escape(cookieValue)
                  + ";path=" + path;
}

function removeElement (list, element) {
  var i;
  for (i = 0; i < list.length; ++i) {
    if (list[i] == element) {
      break;
    }
  }
  if (i < list.length) {
    list.splice (i, 1);
  }
}

function addElement (list, element) {
  list.splice (0, 0, element);
}

function toElement (sectionName, entryName) {
  return sectionName + "|" + entryName;
}

function expandCollapse(sectionName, entryName) {
   var menu = document.getElementById(entryName);
   var menusection = document.getElementById(sectionName);

   var expandSections = new Array();

   if (menusection && menu) {
     var sectionscookiestring = ReadCookie (CookieName);

    if (sectionscookiestring != "") {
      expandSections = sectionscookiestring.split(" ");
    }

    var element = toElement (sectionName, entryName);

     if (menusection.className=="CMS_SideMenu_Expanded") {
       removeElement (expandSections, element );
       menusection.className="CMS_SideMenu_Collapsed";
       menu.style.display="none";
     }
     else {
       addElement (expandSections, element );
       menusection.className="CMS_SideMenu_Expanded";
       menu.style.display="block";
     }

     var expandedSectionsString = expandSections.join(" ");
     
     sectionscookiestring = expandedSectionsString;

     SetCookie (CookieName, sectionscookiestring, 30);
   }  
}

function _expand (sectionName, entryName) {
   var menu = document.getElementById(entryName);
   var menusection = document.getElementById(sectionName);

   if (menu && menusection) {
       menusection.className="CMS_SideMenu_Expanded";
       menu.style.display="block";
   }
}

function _collapse (sectionName, entryName) {
   var menu = document.getElementById(entryName);
   var menusection = document.getElementById(sectionName);

   if (menu && menusection) { 
      menusection.className="CMS_SideMenu_Collapsed";
      menu.style.display="none";
   }
}

function setInitialState () {
  var sectionscookiestring = ReadCookie (CookieName);

  if (sectionscookiestring != "") {
    var expandSections = new Array();
    expandSections = sectionscookiestring.split(" ");

    var i;
    var sectionName;
    var entryName;

    // set expanded
    for (i = 0; i < expandSections.length; ++i) {
      var temp = expandSections[i].split("|");

      sectionName = temp[0];
      entryName = temp[1];

      _expand(sectionName, entryName);
    }
  }
}
