window.onload = function() {
  var hname = location.host;
  var root = document.getElementById('toc');

  var lis = root.getElementsByTagName('a');  
  for (i=0; i<lis.length; i++) {
    lis[i].onclick=function() {	
      var x = this.parentNode.childNodes;
      var theOne;
      for (j=0; j<x.length; j++) {
        if ( x[j].nodeName == 'UL' ) {
          theOne = x[j];
        }
      }
//      alert(this);
      if (theOne.style.className == "expand") { 
        theOne.style.display = "none";
        theOne.style.className = "closed";
        loadXMLDoc('http://' + hname + '/menustate?a=del;c=' + theOne.id, processReqChange);
      }
      else {
        theOne.style.display = "block";
        theOne.style.className = "expand";     
//        theOne.style.backgroundImage = 'url("/images/contents/arrow_tocdown.gif".gif")';
        loadXMLDoc('http://' + hname + '/menustate?a=add;c=' + theOne.id, processReqChange);
      }
    }
  }
  loadXMLDoc('http://' + hname + '/menustate?a=get', processReqChange);
}

var xmlhttp=false;

function loadXMLDoc(url, handler) {
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
  try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
    xmlhttp = false;
  }
 }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  // branch for native XMLHttpRequest object
  if (window.XMLHttpRequest) {
    xmlhttp.onreadystatechange = handler;
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
  // branch for IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    if (xmlhttp) {
      xmlhttp.onreadystatechange = handler;
      xmlhttp.open("GET", url, true);
      xmlhttp.send();
    }
  }
}

function processReqChange() {
  // only if req shows "complete"
  if (xmlhttp.readyState == 4) {
    // only if "OK"
    if (xmlhttp.status == 200) {
      // ...processing statements go here...
      eval('response = ' + xmlhttp.responseText);
      state(response.state);
      var highlight = document.getElementById(response.hl);
      highlight.style.backgroundColor = "white";
    } 
  }
}

function state(str) {
  var ra = str.split(':');
  for(i=0; i<ra.length; i++) {
    var showMe = document.getElementById(ra[i]);
    showMe.style.display = "block";
  }
}
