// E-Mail Address Decoder (EM@D)
// Version 1.1.2
// by Jasper Yeh 
// http://www.nullstack.net/direcode/emad/index.php
//
// EM@D is an open-source JavaScript to hide email addresses and phone numbers from spam bots and
// spiders crawling the web for such items of contact. Such pieces of "eyes-only" information are
// stored on the web page in ROT-13.5 and are decoded when the document is loaded in a browser that
// supports JavaScript. Since, in general, bots only process the raw HTML of the page, the information
// remains safe.
//
// Note: Of course, there is no guarantee that this will necessarily fool every bot-h@arvester in
//       existence. Though extremely unlikely, it is feasible that some moron will decide that instead
//       of bulk volume of addresses, he or she would go for breaking all these supposed methods of
//       hiding e-mail addresses for the sake of doing so... in which case a well-coded script could
//       decipher even things like benny.gorilla-AT-i.love.bananas-DOT-com... Still, it is even more
//       unlikely that the script would have javascript processing capability
//
// License and Disclaimer
// http://creativecommons.org/licenses/by-nc-sa/2.0/
// EM@D (the "SCRIPT") is licensed under a Creative Commons license. Jasper Yeh (the "AUTHOR") is not
// responsible for the application or usage of the Script, nor any damage that may or may not arise
// from you making your e-mail address, phone number, or any other string of bytes available online
// whether or not you use the Script (including, but not limited to, having data harvested by human
// spammers, having data harvested by spambots, having organs harvested by extraterrestrials,
// receiving spam, receiving telemarketer calls, and receiving utter crap via snailmail.)
//
// Release History
// 1.0.0    2005-04-10      Initial Release
// 1.1.0    2006-05-25      Fixed bug in IE6 where innerHTML becomes the same as the href
//                           (this is, of course, due to 2 bugs in IE that have an entirely
//                           unexpected result that is *almost* correct)
// 1.1.1    2006-06-16      Updated webpage URL in source comments
// --------------------------------------------------------------------------------------------------

function emad() {
  var html;
  for(var n=0;n<document.links.length;n++) {
    if(document.links[n].id.indexOf("emad")==0) {
      html = document.links[n].innerHTML;   // save the innerHTML first due to IE bug
      emad_decode_href(document.links[n]);
      document.links[n].innerHTML = html;   // restore innerHTML
      emad_decode_innerHTML(document.links[n]);
      emad_mark(document.links[n]);
    }
  }
  for(var n=0;n<document.anchors.length;n++) {
    if(document.anchors[n].id.indexOf("emad")==0) {
      emad_decode_innerHTML(document.anchors[n]);
      emad_mark(document.links[n]);
    }
  }
  return false;
}

function emad_decode_href(elem) {
  elem.href=emad_rot(elem.href,true);
}

function emad_decode_innerHTML(elem) {
  elem.innerHTML=emad_rot(elem.innerHTML,false);
}

function emad_mark(elem) {
  elem.id = "i" + elem.id;
}

function emad_rot(source,protocol) {
  var curchar="";
  var tagged=false;
  var start=-1;
  var result="";

  if(protocol)
    start=source.indexOf(":");

  result=source.substring(0,start+1);

  for(var n=start+1;n<source.length;n++) {
    curchar=source.charAt(n);

    if(tagged) {
      result+=curchar;
      if(curchar=='>')
        tagged=false;
    } else if(curchar=='<') {
      result+=curchar;
      tagged=true;
    } else if(curchar>='a' && curchar<='z') {
      result+=String.fromCharCode((((source.charCodeAt(n) - 97) + 13) % 26) + 97);
    } else if(curchar>='A' && curchar<='Z') {
      result+=String.fromCharCode((((source.charCodeAt(n) - 65) + 13) % 26) + 65);
    } else if(curchar>='0' && curchar<='9') {
      result+=String.fromCharCode((((source.charCodeAt(n) - 48) + 5) % 10) + 48);
    } else {
      result+=curchar;
    }
  }
  return result;
}
