
  function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    document.cookie = curCookie;
  }

  function getCookie(name) {
    // returns value of cookie or null if cookie does not exist
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
    } else
      begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
      end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
  }

  function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
      document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
  }



function LeftNavSetUp () {

  // set up leftnav. Need to do a vertical expand of the menu
  // when an item is clicked and need to keep menus opened on the referring page
  // also set highlight on currently active link if any


  var s = getCookie("menuSetup"); // get the block state from the cookie
  if (s == null) s = "";
  var cookieString = s;

  //var s = queryString("ln"); // get the block states from the querystring
  s = s.split("/");

  var theLis = document.getElementById("leftnav").getElementsByTagName("li");
  for (var i=0; i<s.length; i++ ) {

    if (s[i]=="b") {
      var theUL = theLis[i].getElementsByTagName("ul"); // get the block;
      if (theUL.length > 0) {
        theUL[0].style.display = "block";
      }
    }
    if (s[i]=="s") { // this is the selected item so highlight it
        theLis[i].className = "leftnavhilight";
    }
  }
  if (s.length > 0) {
    openULs = s;
  } else {
    openULs = new Array();
  }
  var d = "";
  var theLis = document.getElementById("leftnav").getElementsByTagName("li"); // get the lis
  for (var i=0; i<theLis.length; i++) {
    // get the ul to expand
    theLis[i].openIndex = i; // store i so we can get at it in the onclick handler
    theLis[i].onclick=function() {
      var theUL = this.getElementsByTagName("ul"); // possible to return more than one but we will only use the first one
      // the click gets passed up the hierarchy so need to stop it and reset
      if (this.notme == true) { this.parentNode.parentNode.notme = true; this.notme = false; return; }
      if (theUL.length == 0) { // no submenu so store this as the selection
        this.parentNode.parentNode.notme = true; // stop the parent li triggering an onclick
        return;
      }
      d = theUL[0].style.display; // get the current display setting: undef, block or none

      // what type of submenu do we have?
      if (theUL[0].className == "submenuopen") {
        switch (d) {
          case "block": d = "n";
          break;
          case "none": d = "b";
          break;
          default: d = "n";
        }
      } else {
        switch (d) {
          case "block": d = "n";
          break;
          case "none": d = "b";
          break;
          default: d = "b";
        }
      }
      // map the switch back to the style
      switch (d) {
        case "b": theUL[0].style.display = "block";
        break;
        case "n": theUL[0].style.display = "none";
        break;
        default: theUL[0].style.display = "none";
      }
      openULs[this.openIndex] = d;
      this.parentNode.parentNode.notme = true;
    }
  }
  // pass the leftnav open/close conditions in the cookie
  var theLinks = document.getElementsByTagName("a"); // get all the links on the page
  for ( var i=0; i<theLinks.length; i++ ) {
    // code round the DCE links - if this is a dce link then the parent will be an li with
    // the class "page"
    if (theLinks[i].parentNode.className != 'page') { 
      theLinks[i].onclick = function () { // add an onclick function to handle the querystring
        var h = this.href;
        var here = "" + window.location.pathname;
  
        //var hash = this.hash;
        //alert("click: " + h + "\nHere: " + here + "\nHash: " + hash + "\nSearch: " + window.location.search + "\n" + h.indexOf(here));
        //if ((h.indexOf(here) != -1) && (hash != "" )) { return; } // link to anchor on same page don't add string
  
        if ((h.indexOf("javascript:") != -1)) { return; } // link to javascript function, don't add string
        if (h.indexOf("ln") != -1) { return; } // already been clicked
        //if ((h.indexOf(here) != -1) && (hash != "" )) { // link to anchor on same page - need to re-order string
          //h = window.location.protocol + "//" + window.location.host + window.location.pathname; // use the  full path to this page without the hash string
        //}
  
        // are we in the leftnav? if so we need to set the current item
        var theLis = document.getElementById("leftnav").getElementsByTagName("li");
        for ( var j=0; j<theLis.length; j++ ) {
          // clear the old select setting if present
          if (openULs[j] == "s") { openULs[j] = ""; }
          var theULs = theLis[j].getElementsByTagName("ul");
          if (theULs.length == 0) { // there must be no submenu
            var theAs = theLis[j].getElementsByTagName("a"); // get all the a tags in the li
            for ( var k=0; k<theAs.length; k++ ) {
              if (theAs[k].href == h) { // we have found the href inside the a tag
                openULs[j] = "s";
              }
            }
          }
        }
        // set up cookieString
        cookieString = "";
        //var append = "?";
        //if (h.indexOf('?') != -1) { // link already has querystring, append & instead of ?
          //append = "&";
        //}
        var str = openULs.join('/');
        cookieString = str;
        //this.href += append;
        //if (hash != "" ) {  // case for link on same page
          //append += hash;
          //this.href = window.location.pathname + append;
        //}
        setCookie("menuSetup", cookieString, "", "/");
      }
    }
  }

}

function doLoad() {
  LeftNavSetUp();
}

window.onload = doLoad;
