// JavaScript Document

function revealEmailAddresses(className, token, element, cosmaddy) {
	// Note re. className: This script assumes all addresses to be revealed are of the same class.
	// Note re. token: This is whatever is being used in the HTML code instead of "@", e.g., "--at--"
	//		For the token argument, using symbols that have special significance in regex
	// 		(see http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Regular_Expressions#Using_Special_Characters)
	// 		may break the script.
	// Note re. element: This is the type of element you wish to wrap your tokenized email address in
	//		e.g., use 'span' if your HTML looks like this: <span class="email">foo--at--bar.com</span>
	// This function depends on the getElementsByClass function.

	// Regex stuff
	var atSign = new RegExp(token) ;
	var q = '?' ;
	
	if (token.indexOf(q) > -1) {
		alert('The token may not contain a question mark.') ;
		return ;
	}

	if (element == null) {
		element = 'span' ;
	}
	

	var emailLinks = getElementsByClass(className, null, element, cosmaddy) ;

	for (i = 0; i < emailLinks.length; i++) {
		// here we replace the token with the correct @ symbol
		emailString = emailLinks[i].innerHTML.replace(atSign, "@") ;
		
		// the next block splits the string into its parts
		var qIndex = emailString.indexOf(q) ;
		if (qIndex < 0) {
			var addy = emailString ;
			var queryString = '' ;
		} else {
			var addy = emailString.substr(0, qIndex) ;
			var queryString = emailString.substr(qIndex) ;
		}
		// here we set cosmaddy to the email address if cosmaddy is non-existant
		if(cosmaddy==null){
		cosmaddy = addy;
	}
		// and here is where we put the constructed link into the page
		// addy is the email address
		// queryString is whatever headers you ewant to add to the email
		// cosmaddy is whatver you want the email link to look like to the user - example:"Email Us"
		// if cosmaddy is completely removed from the html onload statement, it defaults to the actual email address
		emailLinks[i].innerHTML = '<a style="color:#FCD1B6" href="mailto:' + addy + queryString + '">' + cosmaddy + '</a>' ;
	}
	
}

function getElementsByClass(searchClass,node,tag) { /* Created by: Dustin Diaz :: http://www.dustindiaz.com/ */
	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}