/* $Id: NoSpam.js,v 1.1 2005/03/24 18:28:23 justin Exp $ */
/*
	Author:	Justin Makeig (justin [at] thatone | com)
	Credit:	Modified from code from http://lojjic.net/blog/20030828-142754.rdf.html
			jj [at] lojjic | net

	Purpose:
		Obfuscate email addresses (and links) from spambots. Converts a span in the form of
			<span class="electronic-mail">user[at]domain|com</span>
		into
			<a href="mailto:user@domain.com">user@domain.com</a>
		at runtime using client-side processing with the DOM. Browsers without JavaScript or
		DOM support will see the readable, but unclickable, span shown above.
*/

function ParseEmail() {
	if(!document.getElementsByTagName) return;
	var allElts = document.getElementsByTagName('SPAN'); // get an array of all span elements in the document
	if(allElts.length == 0 && document.all)
		allElts = document.all; // hack for IE5
	for(var i=0; i<allElts.length; i++) { // loop through all spans
		var elt = allElts[i];
		var className = elt.className || elt.getAttribute("class") || elt.getAttribute("className");
		if(className && className.match(/\belectronic-mail\b/)) { // if the current span has a class attribute and its value is 'electronic-mail'
			if(elt.firstChild.nodeType == 3) {
				var addr = elt.firstChild.nodeValue; // get the value of first child (text) node i.e. the encoded email address
				//addr = addr.replace(/ *\[at\] */,"@").replace(/ *\| */gi,"."); // decode the address
				addr = decodeEmail(addr);
				var lnk = document.createElement("a"); // create an anchor element
				lnk.setAttribute("href","mailto:"+addr); // set the achor's href atribute
				lnk.title = "Send an email to " + addr;
				lnk.appendChild(document.createTextNode(addr)); // add a text node with the decoded email address to the anchor element
				elt.replaceChild(lnk, elt.firstChild); // replace the current span's first child (text node) with the anchor created above
			}
			if(elt.firstChild.nodeType == 1) {
				/*
					span class="electronic-mail"	<-- elt
						img							<-- elt.firstChild
				*/



				if(elt.firstChild.nodeName == 'IMG') {
					var addr = elt.firstChild.getAttribute("title");
					addr = decodeEmail(addr);
					var lnk = document.createElement("a"); // create an anchor element
					lnk.setAttribute("href","mailto:"+addr); // set the achor's href atribute
					var newImg = elt.firstChild.cloneNode(true);
					newImg.title = "Send an email to " + addr;
					lnk.appendChild(newImg);
					elt.replaceChild(lnk, elt.firstChild);
				}
			}
		}
	}
}
function decodeEmail(email) {
	return email.replace(/ *\[at\] */,"@").replace(/ *\| */gi,".");
}
