// Anti-spam JavaScript function by Tim Klein, written for www.yarncar.com,
// but you can modify it for your own site if you like.  If you enhance it,
// please let me know!  (See the 'addr' variable below for my email address.)
// 
// version 1.0.0      7 April 2007
//
// The idea is to put a "decoy" email address in all of your mailto anchors,
// and put a <script> element like this one in each of your web pages:
//      <script src="/antispam.js" type="text/javascript"></script>
// That will cause your visitors' web browsers to run this script to replace
// your decoy address with your real email address on the fly, wherever it
// appears in a mailto anchor.
//
// Of course this won't work for any visitor whose browser has JavaScript
// disabled.  So it's best to make your decoy address be such that a human
// could figure out your real address from it -- so even though your
// JavaScript-challenged visitors will have to manually edit the address,
// at least they can still send you a message.  For example, this is my
// decoy address:
//       tim_DELETE_THIS_PART@yarncar.com
//
function tmk_antispam() {

    // PUT YOUR OWN DECOY ADDRESS HERE:
    var decoy_addr = "tim_DELETE_THIS_PART@yarncar.com";

    // Construct the real email address, rather than spell it out explicitly.
    // PUT YOUR OWN REAL ADDRESS HERE:
	var real_addr = ["com", "car.", "yarn", "@", "tim"].reverse().join("");

    // For all mailto anchors that reference the decoy email address,
    // substitute the real address.
	var decoy_prefix = new RegExp("^mailto:\s*" + decoy_addr);
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i < anchors.length; i++) {
		var a = anchors[i];
		if (a.href && decoy_prefix.test(a.href)) {
			a.setAttribute("href", a.href.replace(decoy_prefix,
			                                      "mailto:" + real_addr));
		}
	}

}

// Call the antispam() function when the window has loaded.  This is pretty
// heavy-handed, since it wipes out any other onload handler that some other
// script may have specified.  If this becomes a problem, then I'll need to be
// more sophisticated and *add* a function call to any existing onload handler
// rather than replace the handler altogether.
//
window.onload = tmk_antispam;

// END
