// if required DOM methods are supported, add function to onLoad event
if (document.addEventListener && document.getElementsByTagName && document.childNodes && document.appendChild && document.createElement && document.createTextNode) {
	addEventListener("load", makeMailto, false);
}

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 emailAddress	NB: use lowercase for MSIE or xhtml
				newAnchor.setAttribute("href", "mailto:" + emailAddress);

				var newTextNode = document.createTextNode(emailAddress);

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

				// remove the span's text node
				spans[i].removeChild(spans[i].childNodes[0]);

				// append new anchor element to span
				spans[i].appendChild(newAnchor);

			} // end if email address is valid

		} // end if span class = emailAddress etc.

	} // end for loop

} // end function
