// 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. Put a call in 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>
//

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);
	}

	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);
}

