/*********************************************************
AUTHOR: Shawn Pyle spyle@the-jci.org
VERSION: 1.0

SUMMARY
This file contains the functions to obfuscate email addresses
within the page that it is included in.  It uses the 
Browser.js::addEventLister function to call the rewrite_email_anchors
function below on page load. It will rewrite the email
anchors correctly once the page is loaded correctly.

This file is currently being included in the html_head function
and is available to all manuscripticon project files.

INSTRUCTIONS
1) Include these scripts in the head tag:
	<script type="text/javascript" language="javascript" src="'.$URL_ROOT.'/js/Browser.js"></script>
	<script type="text/javascript" language="javascript" src="'.$URL_ROOT.'/js/email_obfuscator.js"></script>
2) Create email anchors with a user and domain attribute:
	<a user="some_user" domain="example.com" href="#"></a>
3) Wish you had coding skills like me.
	
**********************************************************/

//this function call requires that Browser.js be loaded before it
window.addEventListener('load',rewrite_email_anchors,true);

//finds all anchors and sets the display text and onclick function
function rewrite_email_anchors()
{
	//find all anchors that have an attribute of email
	anchors = document.links

	//replace all innerHTML  for these emails
	for(var i=0; i<anchors.length; i++)
	{
		anchor = anchors[i];
		email = get_email(anchor)
		if(email)
		{
			anchor.innerHTML = HtmlEncode(email)
			anchor.onclick = function()
			{
				open_email(get_email(this));
				return false;
			}
		}
	}
}

//returns an email address from the attributes of the given HTML element
function get_email(element)
{
	if(element.getAttribute('user') && element.getAttribute('domain'))
	{
		return element.getAttribute('user')+'@'+element.getAttribute('domain')
	}
}

//opens up the user's email client
function open_email(email)
{
	window.location = "mailto:"+email
}


// ====================================================================
//       URL Obfuscator functions
//
// Copyright Albion Research Ltd. 2004
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) you include a credit on your web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// ====================================================================

function HtmlEncode( s ) 
{
	var result = "";
	for (var j = 0; j < s.length; j++ ) {
		// Encode 25% of characters
		if (Math.random() < 0.25 
		|| s.charAt(j) == ':' 
		|| s.charAt(j) == '@'
		|| s.charAt(j) == '.') { 
			var charCode = s.charCodeAt(j);
			result += "&#";
			result += charCode;
			result += ";"
		} else {
			result += s.charAt(j);
		}
	}
	return result;
}

function UrlEncode( s ) 
{
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < s.length; i++ ) {
		// Encode 25% of characters
		if (Math.random() < 0.25) { 
			var charCode = s.charCodeAt(i);
			encoded += "%";
			encoded += HEX.charAt((charCode >> 4) & 0xF);
			encoded += HEX.charAt(charCode & 0xF);
		} else {
			encoded += s.charAt(i);
		}
	} // for
	return encoded;
}
