function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
} 

//
// Dynamic Table of Contents script
// by Matt Whitlock <http://www.whitsoftdev.com/>
// 
function createLink(href, innerHTML) {
    var a = document.createElement("a");
    a.setAttribute("href", href);
    a.innerHTML = innerHTML;
    return a;
}
function generateTOC() {
    var toc = document.getElementById("toc");
    var i1 = 0, i2 = 0, i3 = 0, i4 = 0;
    toc = toc.appendChild(document.createElement("ul"));
    var parentNode = document.getElementById("text");
    if (parentNode.childNodes.length == 1 && parentNode.childNodes[0].nodeName.toLowerCase() == "span") {
        // Skip span introduced by RedDot SmartEdit when page is opened
        parentNode = parentNode.childNodes[0];
    }
    // alert(parentNode.childNodes.length);
    for (var i = 0; i < parentNode.childNodes.length; ++i) {
        var node = parentNode.childNodes[i];
        var tagName = node.nodeName.toLowerCase();
        if (tagName == "h5" &&  tocDepth >= 4) {
            ++i4;
            if (i2 == 0) {
                i2++;
                toc.lastChild.appendChild(document.createElement("ul")).appendChild(document.createElement("li"));
            }
            if (i3 == 0) {
                i3++;
                toc.lastChild.lastChild.lastChild.appendChild(document.createElement("ul")).appendChild(document.createElement("li"));
            }
            if (i4 == 1) toc.lastChild.lastChild.lastChild.lastChild.lastChild.appendChild(document.createElement("ul"));
            var section = i1 + "." + i2 + "." + i3 + "." + i4;
            node.insertBefore(document.createTextNode(section + ". "), node.firstChild);
            node.id = "section" + section;
            // alert("h5 - "+node.innerText);
            toc.lastChild.lastChild.lastChild.lastChild.lastChild.lastChild.appendChild(document.createElement("li")).appendChild(createLink("#section" + section, node.innerText?node.innerText:node.textContent));
        }
        else if (tagName == "h4" && tocDepth >= 3) {
            ++i3, i4 = 0;
            if (i2 == 0) {
                i2++;
                toc.lastChild.appendChild(document.createElement("ul")).appendChild(document.createElement("li"));
            }
            if (i3 == 1) toc.lastChild.lastChild.lastChild.appendChild(document.createElement("ul"));
            var section = i1 + "." + i2 + "." + i3;
            node.insertBefore(document.createTextNode(section + ". "), node.firstChild);
            node.id = "section" + section;
            // alert("h4 - "+node.innerText);
            toc.lastChild.lastChild.lastChild.lastChild.appendChild(document.createElement("li")).appendChild(createLink("#section" + section, node.innerText?node.innerText:node.textContent));
        }
        else if (tagName == "h3" && tocDepth >= 2) {
            ++i2, i3 = 0, i4 = 0;
            if (i2 == 1) toc.lastChild.appendChild(document.createElement("ul"));
            var section = i1 + "." + i2;
            node.insertBefore(document.createTextNode(section + ". "), node.firstChild);
            node.id = "section" + section;
            // alert("h3 - "+node.innerText);
            toc.lastChild.lastChild.appendChild(h3item = document.createElement("li")).appendChild(createLink("#section" + section, node.innerText?node.innerText:node.textContent));
        }
        else if (tagName == "h2" && tocDepth >= 1) {
            ++i1, i2 = 0, i3 = 0, i4 = 0;
            var section = i1;
            node.insertBefore(document.createTextNode(section + ". "), node.firstChild);
            node.id = "section" + section;
            // alert("h2 - "+node.innerText);
            toc.appendChild(h2item = document.createElement("li")).appendChild(createLink("#section" + section, node.innerText?node.innerText:node.textContent));
            h2item.className="toc1";
        }
    }
    if (i1 == 0) {
      // Hide TOC title
      var title = document.getElementById("std_lib_inhoud");
      if (title != null) {
         title.style.display='none';
      }
    }
}

