/* http://www.sitepoint.com/blogs/2009/05/05/stop-spam-harvesting-email-obfuscation/ */

$(document).ready(function() {

	$("a.published_email").each(function(index){

		obsfurcatedEmail = $(this).text();

		if (obsfurcatedEmail.length > 0) {

			// If there is a value then assume that it is an
			// obsfurcated email address and unobsfurcate it.

			email = obsfurcatedEmail.replace(/dot/ig, ".");
			email = email.replace(/\(at\)/ig, "@");
			email = email.replace(/\s/g, "");

			// By default the value of the text will be the email address.

			label = email;

			// If the rel attribute has a value the use
			// it as the text of the link.

			rel = $(this).attr("rel");
			if (rel.length != 0)
				label = rel;

			// Set the text on the link to be the label.

			$(this).text(label);

			// If the email is valid then assign it to the href as a "mailto".

			if (/^[^@]+@[a-z0-9]+([_\.\-]{0,1}[a-z0-9]+)*([\.]{1}[a-z0-9]+)+$/.test(email)) {

				// alert("Valid[" + email + "]");
				href = "mailto:" + email;
				$(this).attr("href",href);

			} else {

				// alert("Invalid[" + email + "]");

			}

		}

	})

});

function EmailUnobsfuscate() {

	// find all links in HTML
	var link = document.getElementsByTagName && document.getElementsByTagName("a");
	var email, e;

	// examine all links
	for (e = 0; link && e < link.length; e++) {

		// does the link have use a class named "email"
		if ((" "+link[e].className+" ").indexOf(" published_email ") >= 0) {

			// get the obfuscated email address
			email = link[e].firstChild.nodeValue.toLowerCase() || "";

			// transform into real email address
			email = email.replace(/dot/ig, ".");
			email = email.replace(/\(at\)/ig, "@");
			email = email.replace(/\s/g, "");

			// is email valid?
			if (/^[^@]+@[a-z0-9]+([_\.\-]{0,1}[a-z0-9]+)*([\.]{1}[a-z0-9]+)+$/.test(email)) {

				// change into a real mailto link
				link[e].href = "mailto:" + email;
				link[e].firstChild.nodeValue = email;

			}
		}
	}
}

// window.onload = EmailUnobsfuscate;
