// if required DOM methods are supported, add function to onLoad event
if (document.getElementsByTagName && document.childNodes && document.appendChild && document.createElement && document.createTextNode) {
  addLoadEvent(makeMailto); // addLoadEvent function is in separate file
}

function makeMailto() {

  // get all span elements, then process in a loop
  var spans = document.getElementsByTagName("span");

  for (var i=0; spans[i]; i++) {

    // check only span elements that meet these criteria:
    // (1) class includes "email" (can have other classes, too)    hasClass method is in separate file
    // (2) contains only 1 child node which is text node
    if (spans[i].hasClass("email") && spans[i].childNodes.length==1 && spans[i].childNodes[0].nodeName=="#text") {
      // store span element's text node
      var spanText = spans[i].childNodes[0].data;

      // strip spaces before and after text; this is the would-be email address
      var emailAddress = spanText.replace(/^\s+|\s+$/g, '');

      // replace span text with mailto: anchor only if email address passes validation
      if (/^[^@]+@[^@.]+\.[^@]*\w\w$/.test(emailAddress)) {

        // create new <a> element     use lowercase in case the doc is xhtml
        var newAnchor = document.createElement("a");
        // set href attribute to mailto emailAddress  NB: use lowercase for MSIE or xhtml
        newAnchor.setAttribute("href", "mailto:" + emailAddress);

        // create new text node, and set it to emailAddress
        var newTextNode = document.createTextNode(emailAddress);

        // append new text node to new anchor element
        newAnchor.appendChild(newTextNode);

        // replace span's text node with new anchor element which now contains email address
        spans[i].removeChild(spans[i].childNodes[0]);
        spans[i].appendChild(newAnchor);

      } // end if email address is valid

    } // end if span class = emailAddress etc.

  } // end for loop

} // end function
