//	safesend.js
//
//	Author:   Rob Zako
//
//	Date:     March 3, 2008
//
//	Summary:  Defines the function safeSendRewrite() to rewrite obscured mailto links.
//
//	Usage:    To protect mailto links from Web-harvesting robots, you should:
//
//	          1) Include the following code in the HEAD of an (X)HTML file:
//
//	               <script type="text/javascript" src=".../safesend.js"</script>
//
//	          2) Insert obscured mailto links using the following syntax:
//
//	               <span class="safesend">obscured-address#full-name#display-text</span>
//
//	             where
//
//	               'obscured-address' is the email address split at the at sign (@) and at each
//	                 period (.), in reverse order, and with the at sign (@) replaced with an
//	                 an exclamation mark (!) and each period (.) replaced with a slash (/);
//
//	               'full-name" is the OPTIONAL full name of the addressee; and
//
//	               'display-text' is the OPTIONAL text to be displayed in place of the email
//	                 address. The special value '*' indicates that the full name should be
//	                 displayed in place of the email address.
//
//	             For example,
//
//	               <span class="safesend">com/acme!beta/alpha#Alpha Beta#email</span>
//
//	             is rewritten to
//
//	               <a href="mailto:Alpha Beta <alpha.beta@acre.com>">email</a>
//
//	          3) Insert the following code at the very end of the BODY of an (X)HTML file:
//
//	               <script type="text/javascript">safeSendRewrite();</script>
//
//	Bugs:     The script produces incorrect results when the full name contains a comma (,).
//
//	Note:     The approach, based on JavaScript and the DOM model, should work with older HTML
//	          files and newer XHTML files. In particular, the approach avoids calling the
//	          depricated document.write() function.
//
//	Source:   Modeled after setMailtoLinks() by Milo Vermeulen.
//	          See <http://milov.nl/2330> and <http://milov.nl/milov.js>

function safeSendRewrite()
{
	if (! document.getElementsByTagName)
	{
		return;
	}
	var spanElements = document.getElementsByTagName('span');
	for (var i = spanElements.length-1; i >= 0 ; i--)
	{
		if (spanElements[i].className == 'safesend')
		{
			if (spanElements[i].childNodes.length == 1)
			{
				if (spanElements[i].firstChild.nodeType == 3) // TEXT_NODE
				{
					var args = spanElements[i].firstChild.nodeValue.split('#');					
					if (args.length >= 1)
					{
						var addressComponents = args[0].split('!').reverse();
						for (var j = 0; j < addressComponents.length; j++)
						{
							addressComponents[j] = addressComponents[j].split('/').reverse().join('.')
						}
						var address = addressComponents.join('@');						
						var name = undefined;
						var display = undefined;
						if (args.length >= 2)
						{
							name = args[1];
							if (args.length >= 3)
							{
								display = args[2] != '*' ? args[2] : name;
							}
						}
						var anchorElement = document.createElement('a');
						anchorElement.href = 'mailto:' + (name ? name + ' <' + address + '>' : address);
						anchorElement.appendChild(document.createTextNode(display ? display : address));
//						spanElements[i].replaceChild(anchorElement, spanElements[i].firstChild);
						spanElements[i].parentNode.replaceChild(anchorElement, spanElements[i]);
					}
				}
			}
		}
	}
}
