/* ----------------------------------------------------------------------------------------
Graceful E-Mail Obfuscation - JavaScript function (decodes e-mail addresses)
Created by Roel Van Gils
Last Updated: January 11, 2008, by Ira Pearson
---------------------------------------------------------------------------------------- */

window.onload = function ()
{
	geo();
}

function geo()
{
	if (!document.getElementsByTagName) // Check for browser support
		return false;
	var map = rot13init(); 
	var anchors = document.getElementsByTagName('a'); // Get all anchors
	function geo_decode(a)
	{
		
		// function to recompose the orginal address
		// var href = a.getAttribute('href');
		var href = a['href'];
		var address = href.replace(/.*obf_link\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
		if (href != address)
		{	// a.setAttribute('href','mailto:' + str_rot13(address,map));
			a['href'] = 'mailto:' + str_rot13(address,map);
			if (href.indexOf(a['title']) != -1) a['title'] = str_rot13(address,map);
			//if (href.indexOf(a['text']) != -1) a['text'] = str_rot13(address,map);
			if (href.indexOf(a.innerHTML) != -1) a.innerHTML = str_rot13(address,map);
		}
	}
	for (var l = 0 ; l < anchors.length ; l++)
	{
		geo_decode(anchors[l]);
		/*
		// Loop through the anchors
		anchors[l].onclick = function()
		{
			// Encode links when clicked
			geo_decode(this);
		}
		anchors[l].onmouseover = function()
		{
			// Encode links when hovered (so that the address appears correctly in the browser's status bar)
			geo_decode(this);
		}
		*/
	}
}

function rot13init() {
	var map = new Array();
	var s = "abcdefghijklmnopqrstuvwxyz";
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i)] = s.charAt((i+13)%26);
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
	return map;
}

function str_rot13(a,map) {
	var s = "";
	for (var i = 0 ; i < a.length ; i++) {
		var b = a.charAt(i);
		s += (b>='A' && b<='Z' || b>='a' && b<='z' ? map[b] : b);
	}
	return s;
}