// $Id: deobfuscate.js 236 2007-11-14 10:41:44Z  $
// This script deobfuscates obfuscated email addresses.

// Like the .replace() method, but replaces all matches (doesn't
// properly handle the case of a replacement creating a new match):
String.prototype.replaceAll = function(Pat, Repl) {
  var Subj = this;
  while (Pat.test(Subj)) {
    Subj = Subj.replace(Pat, Repl);
  }
  return Subj;
}

// Look for anchors with a deobfuscate.pl (or deobfuscate.php) href; get
// email addresses from the href and deobfuscate them in place:
function Main() {
  if (document.createTextNode) { // DOM supported?
    if (decodeURI) {
      var Anchors = document.getElementsByTagName('a');
      for (var I = 0; I < Anchors.length; I++) {
        var Anchor = Anchors[I];
        var Href = Anchor.href;
        if (Href) {
          // The first capture is nongreedy in case there are two
          // instances of "+CXE+":
          Captures = Href.match(
            /deobfuscate.(?:pl|php)\?(.*?)\+CXE\+([^|]*)\|?(.*)/);
          if (Captures) {
            // Change the href to the deobfuscated email address:
            Anchor.href = "mailto:" + Captures[1] + "@" + Captures[2];
            // Replace the link text if necessary:
            var LinkText = Captures[3];
            if (LinkText != "") {
              // Decode characters that were encoded to make this a
              // valid URL:
              LinkText = LinkText.replaceAll(/\+/, " ");
              LinkText = decodeURI(LinkText);
              // The link text might also contain an obfuscated email
              // address.  Deobfuscate it:
              LinkText = LinkText.replace(/ CXE /, "@");
              // This will error out if the browser doesn't support
              // innerHTML:
              Anchor.innerHTML = LinkText;
            }
          }
        }
      }
    }
  }
}

window.onload = Main;
