/*
 * File:      Mailto.js
 * Project:   JSLib
 * Author:    Andy Tidball
 * Copyright: Andy Tidball, all rights reserved
 */

/*
 * Function: Mailify
 *
 * Description:
 *	Unmangles a the href attribute of an anchor tag to create a valid mailto: link.
 *	The link's href must have been encoded in the following fashion:
 *		- "http://www." in place of "mailto:"
 *		- "*" in place of "@"
 *	This encoding results in an href string like so: http://www.andy*tidball.net, which looks a lot like a normal URL
 *
 * Parameters:
 *	(<a> Tag) *Link: The anchor tag to unmangle.
 */
function Mailify(Link) {
	Link.href = ExtractMailLink(Link.href);
}

/*
 * Function: ExtractMailLink
 *
 * Description:
 *	Extracts a normal mailto: URL from the a given encoded URL.
 *
 * Parameters:
 * 	(String) *Address: A string representing an encoded mailto: URL (see Mailify for encoding scheme)
 *
 * Returns:
 *	(String): The encoded mailto URL.
 */
function ExtractMailLink(Address) {
	if (Address.indexOf("*") == -1) {
		return Address;
	}
	if (Address.indexOf("http://www.") == 0) {
		Address = Address.substr(11);
	}
	if (Address.indexOf("http://") == 0) {
		Address = Address.substr(7);
	}
	if (Address.charAt(Address.length-1) == "/") {
		Address = Address.substr(0, Address.length-1);
	}
	Address = Address.replace(/\*/, "@");
	Address = Address.replace(/!/, ".");
	if (Address.indexOf("mailto:") == -1) {
		Address = "mailto:" + Address;
	}
	return Address;
}