/*
	nospam.js
	
	Description:
	
		Obfuscate email addresses (and links) from spambots. Converts a span in 
		the form of
			<span class="electronic-mail">user[AT]domain[D0T]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.


	Revision History:
	
		??/??/?? - Created by jj, from logic.net
		??/??/?? - Updated by Justin Makeig from Berkeley
		3/18/04  - Modified by Erik for the Daily Jolt to use a slightly 
				   different obfuscation format (in case spammers work around 
				   the original and to become vastly simpler.  Now it just
				   replaces all [AT]'s and [D0T]'s inside electronic-mail spans
				   with their replacements indiscriminantly.  This allows for
				   things like putting other wierd html inside the span.
		
	Original source:
	
		http://lojjic.net/blog/20030828-142754.rdf.html
		
*/



/* UnencodeString takes a string and returns an unencoded version (i.e., replaces
   any encoded email addresses with their unencoded versions. */
function UnencodeString(s) {
	// Note: I don't know why we need to do this
	// twice, but if we don't, it just doesn't seem
	// to work reliably.
	s = s.replace(/\[AT\]/,"@");
	s = s.replace(/\[D0T\]/gi,".");
	s = s.replace(/%5BAT%5D/, "@");
	s = s.replace(/%5BD0T%5D/, ".");

	s = s.replace(/\[AT\]/,"@");
        s = s.replace(/\[D0T\]/gi,".");
        s = s.replace(/%5BAT%5D/, "@");
        s = s.replace(/%5BD0T%5D/, ".");
	return s;
}


/* ParseEmail goes through the entire document and unencodes all of the email
   addresses it finds. */
function ParseEmail() {

	/* If it's an oldish browser, just return.  It's not worth it. */
	if(!document.getElementsByTagName) return;
	
	/* Get an array of all span elements in the document. */
	var allElts = document.getElementsByTagName('SPAN');
	
	/* Hack for IE5 */
	if(allElts.length == 0 && document.all) {
		allElts = document.all; 
	}
	
	/* Loop through all spans */
	for(var i=0; i<allElts.length; i++) { 
		var elt = allElts[i];
		var className = elt.className || elt.getAttribute("class") || elt.getAttribute("className");
		
		/* If the current span has a class attribute, its value is 
		   'electronic-mail', replace the inner html (stuff between <span> 
		   and </span>.) */
		if(className && className.match(/\belectronic-mail\b/)) {
			elt.innerHTML = UnencodeString(elt.innerHTML);
		}
	}
}
