deObfuscate = function(text) {
	// decode html entities
	text = text.replace(/&#\w{1,3};/g, decodeEntity);
	
	//remove tags 
	text = text.replace(/<[^>]+>/g, "");
		
	// find the "dot"
	text = text.replace(/\W*\.\W*|\W+dot\W+|\W+d0t\W+/gi, ".");
	text = text.replace(/([a-z0-9])DOT([a-z0-9])/g, "$1.$2");
	text = text.replace(/([A-Z0-9])dot([A-Z0-9])/g, "$1.$2");
	
	//find the "at"
	text = text.replace(/\W*@\W*|\W+at\W+/gi, "@");
	text = text.replace(/([a-z0-9])AT([a-z0-9])/g, "$1@$2");
	text = text.replace(/([A-Z0-9])at([A-Z0-9])/g, "$1@$2");	
	
	// get rid of obfuscating phrases; if the offending phrase includes the "at" or "dot," we 
	// have to put that back.
	text = text.replace(/[_\W]*n[_\W]*(o|0)[_\W]*(s|5)[_\W]*p[_\W]*a[_\W]*m[_\W]*/gi, function(match){
		if (match.indexOf(".") > -1) return ".";
		if (match.indexOf("@") > -1) return "@";
		return "";
	});
	
	//decode simple javascript fromCharCode obfuscations
	text = decodeJs(text);
	return text;
}

// this only works on really naive js obfuscations; tacks decoded emails on the end.
decodeJs = function(text) {
	decoded = "";
	if (! (m = text.match(/fromCharCode\((.+)\)/))) return text;
	points = m[1].split(",")  //only reads the first email; one could iterate to be more thorough
	for (i=0; i<points.length; i++) {
	  decoded += String.fromCharCode(points[i]);
	}
	return text + " " + decoded;
}


decodeEntity = function(entity) {
	var codePoint = entity.slice(2, -1);
	var decCodePoint = (entity.indexOf("x") > -1) ? parseInt(codePoint.slice(1), 16) : codePoint;
	return String.fromCharCode(decCodePoint);
}

writeEmails = function(cleanText) {
	var str = "";
	emails = cleanText.match(/\w+@\w+\.(com|org|biz|gov|edu)\b/ig);
	if (!emails) return false;
	
	for (i=0; i<emails.length; i++) {
		str += ("<li>" + emails[i] + "</li>");
	}
	$("#results ul").empty().append(str);
}

$(document).ready(function() {
	$("<ul></ul>").appendTo("#results"); // empty ul doesn't validate
	setInterval("writeEmails(deObfuscate($('textarea').val()))", 200)
	$("a.show-hide").click(function() {
		if (this.innerHTML == "(show the code)") { $("pre").slideDown(); this.innerHTML = "(hide the code)"; }
		else  { $("pre").slideUp(); this.innerHTML = "(show the code)"; }
	});
});






