// Author: Rowan Satherley
// Date: 20 April, 2006
//
// The purpose of this code is to allow email links within the Cawthron Institute Website, but avoid any spam ramifications that go
// with it.  I've tried not to go too silly, but the techniques should be adequate to trick the spam bots.

// This function generates the staff email address in the cawthron email format
// The parameters are painfully obvious, and make sure you don't have any illegal characters in them, or else you'll receive triple the spam!!

// Pass an empty string for the LastName parameter to handle a single word address prefix to the @ symbol.  This can used for any address
// that doesn't conform to the firstname, lastname structure.

function GenerateSEA(FirstName, LastName, DisplayName) // SEA = Staff Email Address
	{
	// To avoid ardious amounts of checking if the string has a space (or invalid characters), or more than one, we'll just assume that the data passed through
	// is correct.  We'll assume this because we KNOW what to pass through to make it work.  
	
	if (FirstName == "") return; // Clearly you aren't serious about email protection
	
	// If last name is nothing, then we have a custom address, in which case we'll use DisplayName
	if (LastName == "")
		{
		if (DisplayName == "") // If you don't include a displayname here, then you'll get your address displaying
			CompileCompleteAddress(FirstName.toLowerCase(), FirstName); 
		else
			CompileCompleteAddress(FirstName.toLowerCase(), DisplayName); 
		}
	else if (DisplayName == "") // Construct Display name from first and last and save a bit of typing :)
		CompileCompleteAddress(FirstName.toLowerCase() + "." + LastName.toLowerCase(), FirstName + " " + LastName);
	else // Use custom display name
		CompileCompleteAddress(FirstName.toLowerCase() + "." + LastName.toLowerCase(), DisplayName);
	}

// This function compiles the chosen Actual address (prefix to the domain of email address) and WrittenAddress (what to be displayed on screen)
// with all the other necessary parts to make email link work.

function CompileCompleteAddress(ActualAddress, DisplayName)
	{
	// Lets declare our little trick string that contains our domain info
	var DomainInfo = "gronorhtwaczn";
	
	// Also our string splitting index array - I used this instead of delimiters, just to be more cryptic really
	var SplitArray = new Array(3, 11);
	
	// Right, lets get busy and split and reverse the strings - domain section will be a, b, c in order
	var b = StringReverse(DomainInfo.substring(0, SplitArray[0]));
	var a = StringReverse(DomainInfo.substring(SplitArray[0], SplitArray[1]));
	var c = StringReverse(DomainInfo.substring(SplitArray[1]));
	
	// Check for spaces in the final address incase the name had multiple spaces
	var EAddress = ActualAddress + "@" + a + "." + b + "." + c;
	
	if (EAddress.indexOf(" ") != -1) return;
	
	// Lets finally write our string that's needed
	// Getting way too paranoid here, but.....
	var e = 'ilto:' 
	var g = '</a>'
	var f = '">'
	var d = '<a href="ma'
	
	document.write(d + e + EAddress + f + DisplayName + g);	
	}

// This function simply reverses the string passed through in the StringToRev parameter

function StringReverse(StringToRev)
	{
	var ReversedString = "";
	var i;
	
	// We need to loop from the end to start of the string and write a new string
	for (i = StringToRev.length; i >= 0; i--)
		ReversedString += StringToRev.charAt(i);	
		
	return ReversedString;	
	}