// addressScrambler.js version 0.3
// copyright 2001, 2002, Josiah Q. Hamilton
// Rewritten by Max Spicer

// This software is provided under the Artistic license of the Open
//  Source Initiative, as it exists on 2001-12-19, including the optional
//  provision regarding aggregation with a commercial distribution.
// This notice must be included with any distribution.

// If you are using a server side component to scramble the
// addresses on their way out of the server, you must be using
// the same algorithm as this javascript uses to unscramble them,
// and for this one, you must have the same scramble string
var scrambleString = "..goats.central.science.laboratory.donkey..";


// This function holds the scrambling algorithm
// The first argument is the text to be scrambled or unscrambled
// The second argument is boolean -- false if the text is to be
// scrambled or true if it is to be unscrambled
function scramble(inText, inverse) {
  var outText = "";
  var scrambleLen = scrambleString.length;
  for (var i = 0; i < inText.length; ++i) {
    var currentCode = inText.charCodeAt(i);
    var offset = scrambleString.charCodeAt(i % scrambleLen);
    if (inverse) {
      // offset should be non-negative, hence the 10*26 below
      offset = 10 * 26 - offset;
    }

    var newCode = currentCode;
    // caveat: the following lines assume ASCII encoding
    if (currentCode == 46) {
      newCode = 64;  // replace '.' by '@'
    } else if (currentCode == 64) {
      newCode = 46;  // replace '@' by '.'
    } else if (65 <= currentCode && currentCode <= 90) {
      newCode = (currentCode - 65 + offset) % 26 + 65;
    } else if (97 <= currentCode && currentCode <= 122) {
      newCode = (currentCode - 97 + offset) % 26 + 97;
    }

    outText += String.fromCharCode(newCode);
  }
  return outText;
}


// This function writes a mailto tag for scrambledAddress
// If a 2nd argument is given, this is used as the display text for the link.
// If a 3rd argument is given it is a boolean -- true if the display text is scrambled

// in order to get single apostrophes through the scramble function
// we need to replace escaped apostrophes (\') with double (''), then we need to swap them back
// to single for use in the 'a' element, lines 58-59 and 64-65
function writeMailTo(scrambledAddress) {
  var descrambledAddress = scramble(scrambledAddress.replace(/\'/g, "''"), true);
  descrambledAddress = descrambledAddress.replace(/\'\'/g, "'");
  var displayText;
  if (arguments.length > 1) {
    if (arguments.length > 2 && arguments[2]) {
      // Display text will be scrambled, so descramble it
      displayText = scramble(arguments[1].replace(/\'/g, "''"), true);
      displayText = displayText.replace(/\'\'/g, "'");
    } else {
      displayText = arguments[1];
    }
  } else {
    displayText = descrambledAddress;
  }
  
  document.open();
  document.write('<a href="mailto:' + descrambledAddress + '" class="scrambledEmail">' + displayText + '</a>');
  document.close();
}


// This function writes a mailto tag for scrambledAddress using scrambledDisplayText
function writeMailToWithScrambledDisplayText(scrambledAddress, scrambledDisplayText) {
  writeMailTo(scrambledAddress, scrambledDisplayText, true);
}


// Function provided for backwards compatablity only
function writeMailToWithClearDisplayText(scrambledAddress, text) {
  writeMailTo(scrambledAddress, text);
}

