/*  Module itdFn.js - JavaScript 1.5

  IDWD Miscellaneous JavaScript Functions
    - Requires global JavaScript module itd.js

  ©2004- InnoTech Dynamics® Web Design
    http://idwd.vantonder.net
    E-mail: idwd at vantonder dot net

    2004-01-16
  Updates:
    2006-06-07
    2006-06-15
    2006-09-13
    2007-04-08
*/


function debug(msg) {
/*
  This debug function displays plain-text debugging messages in a
   special box at the end of a document.  It is a useful alternative
   to using alert() to display debugging messages.
  Original by David Flanagan
    2004-01-19
  Updates:
    2007-04-08
*/
  // Note that to avoid using another global variable,
  // we store the box node as a property of this function.
  if (!debug.box) {
    debug.box = document.createElement("div");
    //setAttribute("class", "ccc") and setAttribute("style", "sss") do not work in IE !
    //debug.box.className = "ffmss";
    debug.box.style.fontFace = "sans-serif";
    debug.box.style.border = "solid red 1px";
    debug.box.style.padding = ".5em";
    document.body.appendChild(debug.box);
    // innerHTML is not part of the W3C DOM standard, but it is supported
    // by Netscape 6 and Internet Explorer 4 and later.
    debug.box.innerHTML =
      "<p style='text-align:center; margin:-.2em 0em 0em; font-size:120%; color:red;'>Debugging Output</p>" +
      "<p style=\"margin:0; text-align:center; color:red\">Please e-mail this page to:" +
      " &nbsp; <span style=\"color:blue\">" + EMail + "&nbsp; (aaa@bbb.ccc)</span> &nbsp; Thank you</p>" +
      "<p style=\"margin: 0.1em 0; color:green\">environment:</p>" +
      "<p style=\"margin: 0.1em 0;\">UserAgent: " + navigator.userAgent + "</p>" +
      "<p style=\"margin: 0.1em 0;\">DOM: " + brwsrIs.dom + "</p>" +
      "<p style=\"margin: 0.1em 0;\">URI: " + url + "</p>" +
      "<p style=\"margin: 0.1em 0;\">Document title: " + docTitle + "</p>";
  }
  // When we get here, debug.box refers to a <div> element into which
  // we can insert our debugging message.
  var p = document.createElement("p");
  p.style.fontFace = "monospace";
  p.style.lineHeight = "1.1em";
  p.style.margin = "0.1em 0";
  p.appendChild(document.createTextNode(msg));
  debug.box.appendChild(p);
}
//** end debug


function ifrmToc(iFht) {
/*
Create iframe at left margin to contain table of contents

Updates:
  2004-04-01
  2005-03-12
  2005-06-25
  2005-12-21
  2006-06-05
*/
  if (!iFht) {
    iFht = screen.height / 2 + "";
  } else {
    iFht = iFht + "";
  }
  document.writeln('<div style="margin: 0 0 0 -.2em; width: ' +'15em; float: left;">');
  document.write('<iframe name="tocFrme" id="ifrmeLft" frameborder="0" scrolling="auto"');
  document.write(' width="100%" height="' + iFht + '"');
  document.write(' src="' + tocCntnr_loc + '"></iframe>');
  document.writeln('</div>');
  document.writeln('<div style="margin: 0 0 0 ' + '16em;">');
}
//** end ifrmToc


function ifrmTocTop(iFht) {
/*
Create iframe at top of page to contain table of contents

Updates:
  2004-03-17
  2005-03-12
  2005-06-25
  2005-12-21
*/
  if (iFht === undefined) {
    iFht = screen.height / 3 + "";
  } else {
    iFht = iFht + "";
  }
  document.writeln('<div style="margin: 0.75em 0; text-align: center;">');
  document.write('<iframe name="tocFrme" id="ifrmeTop" frameborder="0" scrolling="auto"');
  document.write(' width="100%" height="' + iFht + '"');
  document.write(" src=\"" + tocCntnr_loc + "\"></iframe>");
  document.writeln("</div>");
}
//** end ifrmTocTop


function maketoc(tocWndw, Hb, He)
{
  /*
  Create a table of contents for this document, and insert the TOC into the document
   or an iframe in the node specified by the tocPlace argument.

  Original by David Flanagan

  Enhancements and modifications by InnoTech Dynamics® ©2004
   to correspond to XHTML,
   to work with IE 6,
   to put the TOC in a frame
   to insert it in a node rather than replace the node so that the node can be reused
   to limit headers included in the Table of Contents to a specified range

  Use:
   <body onload="maketoc(tocWndw, Hb, He)">
   <span id="tocplace">Table of Contents</span>
   <a href="#" onclick="maketoc(tocWndw, Hb, He); return false;">
    Show Table of Contents</a>

    2004-01-16
   "name" replaced by "id"
   Parameters Hb and He added to define scope of <Hn>'s to be indexed
   TOC style removed
   TOC header
   No backward link before first <H.>
   Documentation
   document.body.lang = 'af'
   Table of contents in other window (optional)

  Updates:
    2005-04-01 2005-06-25 2005-07-01
    2006-06-07
    2006-06-15
  */

  var row;
  var col1;
  var col2;
  var tmp;
  var Ch_raquo = String.fromCharCode(0xbb);
  var tmpAnch = document.createElement("a");
  tmpAnch.setAttribute("href", location.protocol + "//" + location.host + location.pathname);
  var wndwLoc = tmpAnch.href;

  tmpAnch.setAttribute("href", tocWndw.location.protocol + "//" + tocWndw.location.host + tocWndw.location.pathname);
  var tocWndwLoc = tmpAnch.href;
  var tocElmnt = tocWndw.document.getElementById("tocPlace");
  tocElmnt.style.fontSize = "90%";
  tocElmnt.style.margin = "0";

  var docHdr = "Top of page";
  var tocHdr = "Table of Contents";
  var tocFst = "First item";
  var tocNtr = " entry";
  var tocNxt = "Next item";
  var tocLnk = "Table of Contents";
  var tocTop = "Top";

  if (document.body.lang == "af") {
    docHdr = "Bladsy bokant";
    tocHdr = "Inhoudsopgawe";
    tocFst = "Eerste item";
    tocNtr = " inskrywing";
    tocNxt = "Volgende item";
    tocLnk = "Inhoudsopgawe";
    tocTop = "Bo";
  }

  if (He === undefined) {
    He = 6;
    if (Hb === undefined) {
      Hb = 1;
    }
  }

  if (window.name === "") {
    window.name = "content";
  }

  // Create a <div> element that is the root of the TOC tree
  //var toc = tocWndw.document.createElement("div");

  // Set a background color and font for the TOC.
  //toc.style.backgroundColor = "white";
  //toc.style.fontFamily = "sans-serif";

  var flg = true;
  var prgf = tocWndw.document.createElement("p");
  prgf.setAttribute("id", "toc");
  prgf.style.textAlign = "center";
  prgf.style.fontWeight = "normal";
  prgf.style.marginTop = "0.25em";
  prgf.style.marginBottom = "0em";
  if (tocWndw.name != "tocFrme") {  //an iframe or a frame?
    prgf.style.fontSize = "130%";
    prgf.appendChild(tocWndw.document.createTextNode(tocHdr));
    prgf.appendChild(tocWndw.document.createElement("br"));
    spnsz = tocWndw.document.createElement("span");
    spnsz.style.fontSize = "90%";
    spnsz.appendChild(tocWndw.document.createTextNode(docId));
    prgf.appendChild(spnsz);
  } else {
    prgf.style.fontSize = "140%";
    prgf.appendChild(tocWndw.document.createTextNode(tocHdr));
  }
  tocElmnt.appendChild(prgf);

  // Create a <table> element that will hold the TOC and add it
  var table = tocWndw.document.createElement("table");
  //table.setAttribute("id", "toc");
  table.setAttribute("align", "center");
  table.setAttribute("border", "0");
  table.setAttribute("cellspacing", "0");
  table.setAttribute("cellpadding", "2");
  table.style.marginBottom = "0.25em";

  // Start the TOC with an anchor so we can link back to it.
  //var thead = tocWndw.document.createElement("thead");
  //table.appendChild(thead);
  //row = tocWndw.document.createElement("tr");
  //col1 = tocWndw.document.createElement("th");
  //col2 = tocWndw.document.createElement("th");
  //col2.setAttribute("colspan", "2");
  //col2.style.textAlign = "center";
  //col1.appendChild(tocWndw.document.createTextNode(nbsp));
  //col2.appendChild(prgf);
  //row.appendChild(col1);
  //row.appendChild(col2);
  //thead.appendChild(row);

  // Create a <tbody> element that holds the rows of the TOC
  var tbody = tocWndw.document.createElement("tbody");
  table.appendChild(tbody);
  row = tocWndw.document.createElement("tr");
  col1 = tocWndw.document.createElement("td");
  col2 = tocWndw.document.createElement("td");
  col2.style.textAlign = "right";
  var hdrlink = tocWndw.document.createElement("a");
  hdrlink.setAttribute("href", wndwLoc + "#pgTop");
  //hdrlink.setAttribute("target", window.name);
  hdrlink.setAttribute("target", "_parent");
  tmp = tocWndw.document.createTextNode(docHdr);
  hdrlink.appendChild(tmp);
  col1.appendChild(tocWndw.document.createTextNode(nbsp));
  col2.appendChild(hdrlink);
  row.appendChild(col1);
  row.appendChild(col2);
  tbody.appendChild(row);

  // Initialize an array that keeps track of section numbers
  var sectionNumbers = [0, 0, 0, 0, 0, 0];

  // Recursively traverse the body of the document, looking for sections marked with
  //   <h1>, <h2>, ... tags, and use them to create the TOC by adding rows to the table
  addSections(document.body, tbody, sectionNumbers);

  // Finally, insert the TOC into the document by replacing the node
  //   specified by the tocPlace argument with the TOC sub-tree
  //tocElmnt.parentNode.replaceChild(toc, tocPlace);
  tocElmnt.appendChild(table);
  if (tocWndw.name != "tocFrme" && tocWndw.name != "fm_nav") {  //an iframe or a frame?
    tocElmnt.scrollIntoView(true);
  }
  //tocElmnt.scrollIntoView(false);

  function addSections(n, toc, sectionNumbers) {
  // This method recursively traverses the tree rooted at node n, looking
  //   for <Hb> through <He> tags and uses the content of these tags to build
  //   the table of contents by adding rows to the HTML table specified by the
  //   toc argument.  It uses the sectionNumbers array to keep track of the
  //   current section number.
  // This function is defined inside of maketoc() so that it is not
  //   visible from the outside.  maketoc() is the only function
  //   exported by this JavaScript module.
    var flag;


    // Loop through all the children of n
    for (var m = n.firstChild; m !== null; m = m.nextSibling) {
      // Check whether m is a heading element.  It would be nice if we
      //   could just use (m instanceof HTMLHeadingElement), but this is
      //   not required by the specification and it does not work in IE.
      // Therefore we've got to check the tagname to see if it is H1-H6.
      if ((m.nodeType == 1) &&
        (m.tagName.length == 2) &&
        (m.tagName.charAt(0) == "H")) {
        // Figure out what level heading it is
        var level = parseInt(m.tagName.charAt(1), 10);
        if (!isNaN(level) && (level >= Hb) && (level <= He)) {
          // Increment the section number for this heading level
          sectionNumbers[(level - 1)]++;
          // And reset all lower heading level numbers to zero
          for (var i = level; i < He; i++) {
            sectionNumbers[i] = 0;
          }
          tmp = getTextContent(m);
          if (tmp === "" || tmp == "Sidebar:") {    //"empty" header, do nothing
            sectionNumbers[level] = -1;
            } else {
            // Now combine section numbers for all heading levels
            //   to produce a section number like 2.3.1
            var sectionNumber = "";
            var sectionSpacer = "";
            for (i = Hb - 1; i < level; i++) {
              sectionNumber += sectionNumbers[i];
              if (sectionNumbers[i] === 0) {
                sectionNumbers[i+1]--;
              }
              if (i < level - 1) {
                sectionNumber += ".";
                sectionSpacer += nbsp;
              }
            }

            // Create an anchor paragraph to contain back links
            var pbLb = document.createElement("p");
            pbLb.style.marginBottom = "-0.5em";
            if (flg) {
              flg = false;
              if (document.getElementById("tocbar")) {
                var eAnch = document.createElement("a");
                eAnch.setAttribute("id", "sect" + sectionNumber);
                n.insertBefore(eAnch, m);
              } else {
                pbLb.style.marginTop = "0.75em";
                pbLb.setAttribute("id", "sect" + sectionNumber);
                // Create a link back to the top of the TOC
                pbLb.appendChild(document.createTextNode(nbsp + " " + nbsp + " " + Ch_raquo + " "));
                var toclink = document.createElement("a");
                toclink.setAttribute("href", tocWndwLoc + "#toc");
                toclink.setAttribute("target", tocWndw.name);
                toclink.appendChild(document.createTextNode(tocLnk));
                pbLb.appendChild(toclink);
                n.insertBefore(pbLb, m);
              }
            } else {
              pbLb.style.marginTop = "1.5em";
              pbLb.setAttribute("id", "sect" + sectionNumber);
              pbLb.appendChild(document.createTextNode(nbsp + " " + nbsp + " " + Ch_raquo + " " + tocLnk + ": " + nbsp));
              // Create an anchor to mark the beginning of this section.
              // This will be the target of a link we add to the TOC.
              //var anchor = document.createElement("a");
              //anchor.setAttribute("id", "sect" + sectionNumber);
              //n.insertBefore(anchor, m);
              //n.insertBefore(document.createTextNode(tocHdr + ": "), m);

              // Create a link back to the TOC and make it a child of the anchor
              var backlink = document.createElement("a");
              backlink.setAttribute("href", tocWndwLoc + "#csect" + sectionNumber);
              backlink.setAttribute("target", tocWndw.name);
              backlink.appendChild(document.createTextNode(tocNxt));
              //n.insertBefore(backlink, m);
              //n.insertBefore(document.createTextNode(" | "), m);

              // Create a link back to the top of the TOC
              toclink = document.createElement("a");
              toclink.setAttribute("href", tocWndwLoc + "#toc");
              toclink.setAttribute("target", tocWndw.name);
              toclink.appendChild(document.createTextNode(tocTop));
              //n.insertBefore(toclink, m);

              pbLb.appendChild(backlink);
              pbLb.appendChild(document.createTextNode(nbsp+nbsp));
              pbLb.appendChild(toclink);
              n.insertBefore(pbLb, m);
            }

            // Now create a link to this section.  It will be added to the TOC below.
            var link = tocWndw.document.createElement("a");
            link.setAttribute("href", wndwLoc + "#sect" + sectionNumber);
            link.setAttribute("target", window.name);
            // Get the heading text using a function defined below
            var sectionTitle = getTextContent(m);
            link.appendChild(tocWndw.document.createTextNode(sectionTitle));

            // Create a new row for the TOC
            row = tocWndw.document.createElement("tr");
            // Create two columns for the row
            col1 = tocWndw.document.createElement("td");
            col1.style.verticalAlign = "top";
            col2 = tocWndw.document.createElement("td");
            // Put the section number in the first column
            col1.setAttribute("align", "right");
            col1.setAttribute("id", "csect" + sectionNumber);
            col1.appendChild(tocWndw.document.createTextNode(sectionNumber));
            // Put a link to the section in the second column
            col2.appendChild(tocWndw.document.createTextNode(sectionSpacer));
            col2.appendChild(link);
            // Add the columns to the row, and the row to the table
            row.appendChild(col1);
            row.appendChild(col2);
            toc.appendChild(row);

            // Modify the section header element itself to add
            //   the section number as part of the section title
            var span1 = document.createElement("span");
            span1.style.fontSize = "90%";
            span1.appendChild(document.createTextNode(sectionNumber + nbsp + nbsp));
            m.insertBefore(span1, m.firstChild);
            //m.insertBefore(document.createTextNode(sectionNumber+nbsp), m.firstChild);
          }
        }
      } else {
        // Otherwise, this is not a heading element, so recurse
        addSections(m, toc, sectionNumbers);
      }
    }
  }
  //** end addSections


  function getTextContent(n) {
  // This utility function traverses the node n, returning the content of
  //   all text nodes found, and discarding any HTML tags.  This is also
  //   defined as a nested function so that it is private to this module.

    var s = "";
    var children = n.childNodes;
    for (var i = 0; i < children.length; i++) {
      var child = children[i];
      if (child.nodeType == 3) {
        s += child.data;
      } else {
        s += getTextContent(child);
      }
    }
    return s;
  }
  //** end getTextContent
}
//** end maketoc


function createtoc(Hb, He) {
/*
Identify frame for table of contents

  2004-03-24
*/
  if (parent != self) {
    maketoc(parent.fm_nav, Hb, He);
  } else {
    maketoc(window.tocFrme, Hb, He);
  }
}
//** end createtoc


function removetoc() {
/*
Remove table of contents when page is unloaded
 to make space in navigation frame available for re-use by other TOC

  2004-03-22
  2005-06-25
 */
  if (parent != self) {
    var tocElmnt = parent.fm_nav.document.getElementById("tocPlace");
    tocElmnt.removeChild(tocElmnt.lastChild);
    tocElmnt.removeChild(tocElmnt.lastChild);
  }
}
//** end removetoc


function xtraFldrUri() {
/*
if (a URI on this page points to "xtra`" folder) {
  establish whether this host contains this folder;
  if (host does not contain this folder) {
    if (folder is only available on local host) { (xtraL)
      delete element containing the URI;
    } else {
      change URI to point to appropriate host; (xtraT only on www.vantonder.net )
    }
  }
}

  2004-01-30
Updates:
  2004-03-02
  2004-07-04
*/

  var i;
  var parent;
  var temp;
  var temp2;
  var xtHost = "www.chris.vantonder.net";

  var thisHost = window.location.hostname.toLowerCase();
  var link = document.createElement("a");    // Dummy for use with image URI's
  var imgs = document.getElementsByTagName("img");
  var lnks = document.getElementsByTagName("a");

  if (thisHost.indexOf(".") == -1) {
    xtHost = xtHost.replace(/\./g, "");    // For local testing
  }

  // Nothing to do if on local file system
  if (!thisHost) {
    return;
  }

  // Nothing to do if on local host
  if (thisHost.indexOf("myweb") > -1) {
    return;
  }

  if (thisHost.indexOf("mweb") > -1 ||
        //|| true
      thisHost.indexOf("vantonder") > -1) {
        // On internet:
        // Remove xtraL img and lnk URI's
    for (i = imgs.length - 1; i >= 0; i--) {
      if (imgs[i].src.indexOf("xtraL") > -1) {
        parent = imgs[i].parentNode;
        parent.removeChild(imgs[i]);
      }
    }

    for (i = lnks.length - 1; i >= 0; i--) {
      if (lnks[i].href.length > 2 &&
         lnks[i].pathname.indexOf("xtraL") > -1) {
        parent = lnks[i].parentNode;
        parent.removeChild(lnks[i]);
      }
    }

    // On internet:
    if (thisHost.indexOf("mweb") > -1 /* || true */) {
        // On MWeb:
        // Change xtraT img and lnk URI's to www.chris.vantonder.net
      imgs = document.getElementsByTagName("img");
      lnks = document.getElementsByTagName("a");

      for (i = 0; i < imgs.length; i++) {
        if (imgs[i].src.indexOf("xtraT") > -1) {
          link.setAttribute("href", imgs[i].src);
          temp = link.href;
          temp2 = temp.indexOf("xtraT");
          temp = temp.substr(temp2, (temp.length - temp2 + 1));
          temp = "http://" + xtHost + "/" + temp;
          link.setAttribute("href", temp);
          imgs[i].src = link.href;
        }
      }

      for (i = 0; i < document.links.length; i++) {
        if (lnks[i].href !== "" &&
           // if (lnks[i].href !== "" - mozilla 1.6 bug
          lnks[i].pathname.indexOf("xtraT") > -1)
        {
          temp = lnks[i].href;
          temp2 = temp.indexOf("xtraT");
          temp = temp.substr(temp2, (temp.length - temp2 + 1));
          temp = "http://" + xtHost + "/" + temp;
          lnks[i].setAttribute("href", temp);
        }
      }
      //* }
    }
  }
}
//** end xtraFldrUri

