function activateDDs() {
  var dts,tohide,newlink,newtext;
  // get all DT elements, loop over them
  dts=document.getElementsByTagName('dt');
  for (i=0;i<dts.length;i++)  {
    // get the next sibling until it really is an element, if so, hide it
    tohide=dts[i].nextSibling;
    while(tohide.nodeType!=1)  {
      tohide=tohide.nextSibling;
    }
    tohide.style.display='none';
    // assemble a link and take the content of the dt as its text
    newlink=document.createElement('a');
    newtext=document.createTextNode(dts[i].firstChild.nodeValue);
    newlink.appendChild(newtext);
    newlink.href='#'
    // create a new object attribute, this saves us looping in the 
    // showhide function add the event handlers
    newlink.colobj=tohide;
    newlink.onclick=function(){showhide(this.colobj);return false}
    // replace the dt with the link
    dts[i].replaceChild(newlink,dts[i].firstChild)
    newlink.style.color = "green";
    newlink.style.textDecoration = "none";
  }
}

function toggleAll(noneBlock) {
  // Lesser derivative of activateDDs() that just collapses/expands all 
  // elements - incoming var "noneBlock" must be string "none" or "block"
  var dts,tohide;
  // get all DT elements, loop over them
  dts=document.getElementsByTagName('dt');
  for (i=0;i<dts.length;i++)  {
    // get the next sibling until it really is an element, if so, hide it
    tohide=dts[i].nextSibling;
    while(tohide.nodeType!=1)  {
      tohide=tohide.nextSibling;
    }
    tohide.style.display=noneBlock;
  }
}

function searchpage(searchtext) {
  var toshow, resultblock, hitcount=0;
  var dts=document.getElementsByTagName('dt');
  var dds=document.getElementsByTagName('dd');
  // the parens aren't needed for the search: they're needed for the 
  // highlighting, which replaces the elements at a later point.  
  // "g" needed for the same reason.
  searchtext="\(" + searchtext + "\)";
  var re=new RegExp(searchtext, "gi");

  // find the space where we write pretty colours to indicate search results:
  resultblock=document.getElementById("searchresults");
  resultblock.innerHTML="<span style='border: 1px solid yellow; background-color: yellow;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";

  for (i=0;i<dds.length;i++) {
    if (dds[i].innerHTML.search(re) != -1) {
      hitcount++;
      dds[i].innerHTML=dds[i].innerHTML.replace(re, "<span class='searchHighlight' style='background-color: orange;'>$1</span>");
      // get the next sibling until it really is an element, if so, show it
      toshow=dds[i];
      while(toshow.nodeType!=1)  {
        toshow=toshow.nextSibling;
      }
    toshow.style.display='block';
    }
  }
  for (i=0;i<dts.length;i++) {
    // This is a bit different: the DD search expands the parent, you have to expand the child.
    if (dts[i].innerHTML.search(re) != -1) {
      hitcount++;
      dts[i].innerHTML=dts[i].innerHTML.replace(re, "<span class='searchHighlight' style='background-color: orange;'>$1</span>");
      toshow=dds[i]; //This assumes a 1:1 relationship between DDs and DTs!!!
      // get the next sibling until it really is an element, if so, show it
      while(toshow.nodeType!=1)  {
        toshow=toshow.nextSibling;
      }
      toshow.style.display='block';
    }
  }
  // Do something to show if we have results:
  if(hitcount > 0) {
    resultblock.innerHTML="<span style='border: 1px solid green; background-color: green;'>&nbsp;&nbsp;" + hitcount + "&nbsp;&nbsp;</span>";
  } else {
    resultblock.innerHTML="<span style='border: 1px solid red; background-color: red;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
  }
}

function checkDTDDcount() {
  if(document.getElementsByTagName('dt').length != document.getElementsByTagName('dd').length) {
    alert("There are " + document.getElementsByTagName('dt').length + " DT tags and " + document.getElementsByTagName('dd').length + " DD tags.  As these numbers don't match, your search results will be incorrect.  You should never see this error - the page maintainer is obviously getting lax.");
  }
}

function removeHighlights() {
  var searchtext;

  // find our results block and blank it yellow:
  resultblock=document.getElementById("searchresults");
  resultblock.innerHTML="<span style='border: 1px solid yellow; background-color: yellow;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";

  var hightext=getElementsByClassName("searchHighlight");
  for (i=0;i<hightext.length;i++) {
    // quotes are replaced by dots because they seem to get 
    // transmogrified from single to double ... and IE has none at all ...
    searchtext="(<span class=.?searchHighlight.? [^>]*>)+([^<>]*)(</span>)+";
    var re=new RegExp(searchtext, "gi");
    if (hightext[i].parentNode) {
      /* if the searchtext is found more than once in the hightext, 
       * we get multiple hightext[i] and correcting the first breaks 
       * the second, third, etc.
       */
      hightext[i].parentNode.innerHTML=hightext[i].parentNode.innerHTML.replace(re, "$2");
     }
  }
}

