var div = document.getElementById('table_of_contents');
if (div) {
  h3s = document.getElementById('contents').getElementsByTagName('H3');

  var list;
  for (var i = 0; i < h3s.length; i++) {
    var title = h3s[i].innerHTML;
    var anchor = h3s[i].previousSibling;
    while (anchor && anchor.nodeType != 1) {
      anchor = anchor.previousSibling;
    }
    if (!anchor || !anchor.name) {
      continue;
    }
    if (i == 0) list = document.createElement('ul');

    var newA = document.createElement('a');
    newA.href = '#' + anchor.name;
    newA.innerHTML = title;
    var item = document.createElement('li');
    item.appendChild(newA);

/*  // This chunk is disabled for now; it was intended to provide a sub-heading
    // for h4's within each h3, but it doesn't work and isn't necessary now.
    var h4s = new Array;
    var here = h3s[i].nextSibling;
    while (here) {
      if (here.tagName == 'H4') {
        h4s.push(here);
      }
      else if (here.tagName == 'H3') {
        break;
      }
      here = here.nextSibling;
    }
      
    var sublist;
    var subitem;
    //if (h4s.length > 0) alert('found some: ' + h4s.length);
    for (var j = 0; j < h4s.length; j++) {
      var title = h4s[j].innerHTML;
      var anchor = h4s[j].previousSibling;
      while (anchor && anchor.nodeType != 1) {
        anchor = anchor.previousSibling;
      }
      if (!anchor || !anchor.name) {
        continue;
      }

      if (j == 0) sublist = document.createElement('ul');
      var newA = document.createElement('a');
      newA.href = '#' + anchor.name;
      newA.innerHTML = title;
      subitem = document.createElement('li');
      subitem.appendChild(newA);
      sublist.appendChild(subitem);
    }
    if (h4s.length > 0 && sublist) {
      item.appendChild(sublist);
    }
*/
    list.appendChild(item);
  }
  if (h3s.length > 0) {
    div.appendChild(list);
  }
}
