/*
	Functions to obscure e-mail addresses.
	
	Copyright © 2007, Jon Hughes. All rights reserved.
	
	These functions use the same encoding and decoding algorithm as Base64
	encoded Multimedia Internet Mail Extension (MIME) encoded e-mail
	attachments except that the encoding table has been changed in case a
	spammer tries to scan a web site and decode any encoded strings.

	More information about MIME Base64 encoding is available in RFC 1521
	and RFC 2045 available from http://www.ietf.org (the web site of the
	Internet Engineering Task Force). See specifically:
	
	http://www.ietf.org/rfc/rfc1521.txt (section 5.2)
	http://www.ietf.org/rfc/rfc2045.txt (section 6.8)
*/

// Standard Base64 encoding table:

//var codes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

// Modified encoding table:

var codes = '0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz+/';

/*
   Encode a string.
*/

function emailEncoder(decStr)
{
	decStr = escape(decStr);
	var bits;
	var dual;
	var i = 0;

	encStr = '';

	while (decStr.length >= i + 3)
	{
		bits = (decStr.charCodeAt(i++) & 0xff) <<16 |
			   (decStr.charCodeAt(i++) & 0xff) <<8  |
			    decStr.charCodeAt(i++) & 0xff;

		encStr += codes.charAt((bits & 0x00fc0000) >>18) +
				  codes.charAt((bits & 0x0003f000) >>12) +
				  codes.charAt((bits & 0x00000fc0) >> 6) +
				  codes.charAt((bits & 0x0000003f));
	}

	if (decStr.length -i > 0 && decStr.length -i < 3)
	{
		dual = Boolean(decStr.length -i -1);
		bits = ((decStr.charCodeAt(i++) & 0xff) <<16) |
				(dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);
		encStr += codes.charAt((bits & 0x00fc0000) >>18) +
				  codes.charAt((bits & 0x0003f000) >>12) +
				  (dual ? codes.charAt((bits & 0x00000fc0) >>6) : '=') + '=';
	}

	return encStr
}

/*
   Decode a string.
*/

function emailDecoder(encStr)
{
	var bits;
	var decStr = '';
	var i = 0;

	for(; i < encStr.length; i += 4)
	{
		bits =
			(codes.indexOf(encStr.charAt(i))     & 0xff) << 18 |
			(codes.indexOf(encStr.charAt(i + 1)) & 0xff) << 12 |
			(codes.indexOf(encStr.charAt(i + 2)) & 0xff) <<  6 |
			 codes.indexOf(encStr.charAt(i + 3)) & 0xff;
		decStr += String.fromCharCode((bits & 0xff0000) >> 16,
			(bits & 0xff00) >> 8, bits & 0xff);
	}

	if (encStr.charCodeAt(i - 2) == 61)
		return decStr.substring(0, decStr.length - 2);
	else if(encStr.charCodeAt(i - 1) == 61)
		return decStr.substring(0, decStr.length - 1);
	else
		return decStr;
}

/*
   Decode a string and use at it as an email address. Open the user's
   default mail program with the address in the "to" list.
*/

function openMail(a)
{
	var m = window.open('mailto:' + emailDecoder(a), 'mailWindow',
		'toolbar=0,directories=0,menubar=0,status=0,scrollbars=0,resizable=0,' +
		'top=100,left=100,height=190,width=340');
	if (m) m.close();
}

/*
   Called from a web page hyperlink. For example:
   
      <a href="javascript:void(0);" onclick="openMail('IOzSaOlvhWlnJV1nh7BSHWzr');"
      onmouseover="return overMail('IOzSaOlvhWlnJV1nh7BSHWzr');" onmouseout="return outMail();">
   
   will display the decoded email address in the window status line when the user
   moves the mouse over the hyperlink.
*/

function overMail(a)
{
	window.status = 'Send email to ' + emailDecoder(a);
	return true;
}

/*
   Called from a web page hyperlink. For example:
   
      <a href="javascript:void(0);" onclick="openMail('IOzSaOlvhWlnJV1nh7BSHWzr');"
      onmouseover="return overMail('IOzSaOlvhWlnJV1nh7BSHWzr');" onmouseout="return outMail();">
   
   will clear the status line when the user moves the mouse away from the hyperlink.
*/

function outMail()
{
	window.status = ' ';
	return true;
}

/*
   Used by the email.htm web page take a sample email address text description
   and show the HTML that should be used in it's place on a web page.
*/

function encodeIt()
{
	var address = emailEncoder(document.mainForm.email.value);
	var description = document.mainForm.description.value;
	if (address == '')
	{
		document.mainForm.email.focus();
		alert('Enter an e-mail address!');
	}
	else if (description == '')
	{
		document.mainForm.description.focus();
		alert('Enter a text description!');
	}
	else
	{
		var result =
			'<a href="javascript:void(0);" ' +
			'onclick="openMail(\'' + address + '\');" ' +
			'onmouseover="return overMail(\'' + address + '\');" ' +
			'onmouseout="return outMail();">' + description +
			'</a>';
		document.mainForm.result.value = result;
		document.getElementById('tester').innerHTML = result;
	}
}