///////////////////////////////////////////////////////////////////////////////
// Decrypt Email Address
// Mark B 10/09/2004
// Converts email address encrypted with encrypt_email() server side
// function into readable address
//
// encryptedTokenString:	The encrypted address string
///////////////////////////////////////////////////////////////////////////////

function decrypt_email(encryptedTokenString)
{
	// Declare Variables
	var tokens = new Array();
	var decryptedString = '';
	var strippedToken = 0;
	var x, i;
	
	// Split Token String into 4-Character Tokens
	for(x=0, i=0; x<encryptedTokenString.length; x+=4)
		tokens[i++] = encryptedTokenString.substr(x,4);
	
	// Strip Leading Zeros from Tokens
	function stripLeadingZeros(token)
	{	
		while(token.length > 1 && token.substr(0,1) == '0')
			token = token.substring(1, token.length);

		return token;
	}
	
	// Get the last token in the array (decryption key)
	var decryptCode = stripLeadingZeros(tokens[tokens.length-1]);
	
	// Convert all tokens to characters
	for(x=0; x<tokens.length-1; x++)
	{
		strippedToken = stripLeadingZeros(tokens[x]); 
		decryptedString += String.fromCharCode((strippedToken - decryptCode));
	}
	
	return decryptedString;
}

///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Insert Email
// Mark B 10/09/2004
// Writes out a mailto: link form an encrypted email address
//	
// encryptedEmail: 	An email address encrypted with encrypt_email()
// title:			The Title of the mailto: link (if not specified will be 
//					the email address)
// text:			The link text of the mailto: link (if not specified will be 
//					the email address)
///////////////////////////////////////////////////////////////////////////////

function insert_email(encryptedEmail, title, text)
{
	// Decrypt the email address
	var e = decrypt_email(encryptedEmail);
	
	var link = '<a href="mailto:' + e + '" title="';
	link += (title) ? title : e;
	link += '">';
	link += (text) ? text : e;
	link += '</a>'
	
	document.write(link);
}

///////////////////////////////////////////////////////////////////////////////
