/********************************************************************************
*      Script: mdscic_email.js
* Description: Routine for hiding e-mails/links from harvester programs
*    Comments: This script determines the valid e-mail address for mailto: links
*              This file needs to be stored in http://www.mdsc-ic.org/scripts
*     Created: 01/23/2005 [CLC]
*    Modified:
*********************************************************************************/


// Reverse a string; abcd becomes dcba //                                     
function reverse_string() {
   if (this.length <= 1) return this;
	var reversestring = "";
   for (var x = 0; x < this.length; x++) {
      reversestring = this.charAt(x) + reversestring;
   }
   return reversestring;
} //reverse_string
String.prototype.reverse = reverse_string

function FixMail(email, faketoname, fakeatname, subject) { 
// This function derives the correct e-mail for an e-mail link when the user clicks on the link to send.
// All e-mails in the system are set to mdscic01@ggtss.org which is a spam trap for harvesting bots and
// which gets forwarded to my private id for messages sent to such harvested addresses.
// The 'onclick' option of each of these links passes a number which tells us which real address to send
// the message to.
// Include this script with...

// <script src="http://www.mdscic.org/scripts/mdscic_email.js"
//		languaqe="JavaScript"
//		type="text/javascript">			
// </script>

// This function derives the correct e-mail for an e-mail link when the user clicks on the link to send.
// The 'onclick' option of each of these links passes the real name and system address written in
// reverse which tells us which real address to send the message to.  the calling code for this is... 
// <A href="mailto:<spamtrap@somesystem>" onclick="FixMail(this,'<reversename>','<reverseatvalue>','<subject>');">  

	var oldemail;
	var realtoname;
	var realatname;
	oldemail = email.href;
// reverse the order to give us our real value here.
	realtoname = faketoname.reverse();
	realatname = fakeatname.reverse();
	var atsign = "@";
// Set the correct hyperlink e-mail address
	if (subject.length >= 0) {
	   email.href = "mailto:" + realtoname + atsign + realatname + '?Subject=' + subject; }
	else {
		email.href = "mailto:" + realtoname + atsign + realatname; }
// for debuging....
//	alert("Fake E-mail address was ... \n   " + oldemail + "\n\n\n" +
//			"Real E-mail address is  ... \n   " + email.href);
	return true;             // make it work...
} //FixMail
