// Email Widget
//
// Copyright Dalbrook Limited 2006
//
// Makes it more difficult for spam robots to extract email addresses from your web site.
//
// To use this:
//
// 1. In the page header reference this file, e.g.
//    <script type="text/javascript" src="email_widget.js"></script>
//
// 2. Call the function when the page loads, e.g.
//    <body onload='emailWidget()'>
//
// 3. In your email hyperlinks back-substitute items in the strings e.g.
//    <a href='MMAAIILLTTOO:fredAATTbloggsDDOOTTcom'>fredAATTbloggsDDOOTTcom</a>
//
// When the page is loaded into an actual web browser (such as IE6, Firefox, Mozilla) the
// strings will be substituted correctly and the email links will work.
//
// When loaded by a robot there will be no valid email addresses in the source.
//
// Will not work in ancient non-DOM browsers, or with JavaScript turned off, but a
// reasonably smart user could still work out what the email address should be.

function emailWidget() {

	function massReplace(inString, substitutesArray) {	
		var i;
		var pattern;
		var outString = inString;
		for (i=0; i<substitutesArray.length; i++) {
			pattern = new RegExp(substitutesArray[i][0],"ig");
			outString = outString.replace(pattern, substitutesArray[i][1]);
		}
		return (outString);
	}

	if (!document.getElementById || !document.getElementsByTagName) return false;

	var substitutes = [];

	substitutes[0] = []
	substitutes[0][0] = 'AATT';
	substitutes[0][1] = '@';

	substitutes[1] = []
	substitutes[1][0] = 'DDOOTT';
	substitutes[1][1] = '.';

	substitutes[2] = []
	substitutes[2][0] = 'MMAAIILLTTOO';
	substitutes[2][1] = 'mailto';

	var subPattern = new RegExp(substitutes[2][0],"i");
	var thisLink;
	var oldText, newText;
	var links = document.getElementsByTagName('a');
	var didSomething = false;

	for (var i=0; i<links.length; i++) {
		thisLink = links[i];
		thisProtocol = thisLink.protocol;
		if (subPattern.test(thisProtocol)) {
			oldText = thisLink.getAttribute('href');
			newText = massReplace(oldText,substitutes);
			if (oldText != newText) {
				thisLink.setAttribute('href',newText);
				didSomething = true;
			}
			oldText = thisLink.innerHTML;
			newText = massReplace(oldText,substitutes);
			if (oldText != newText) {
				thisLink.innerHTML = newText;
				didSomething = true;
			}
		}
	}	
	return (didSomething);
}

