/*******************************************************************************
 * This work is licensed under the Creative Commons 
 * Attribution-NonCommercial-ShareAlike License. To view a copy of this license,
 * visit http://creativecommons.org/licenses/by-nc-sa/2.0/ or send a letter to 
 * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
 ******************************************************************************/

function EmailEncoder() {
   /****************************************************************************
    * Change this as is appropriate to your web site. Do not make this one of
    * your normal passwords, however, because anyone running this script will be
    * able to see it.  This is just a string to provide simple XOR encrypting
    * against.  If you change this, you must regenerate your encoded strings, or
    * else they will decode into gibberish.
    ***************************************************************************/
   var passkey = "aardwulf Systems";

   // Public functions:

   this.setClass = _setCssClass;
   this.setStyle = _setCssStyle;

   /****************************************************************************
    * Use this function to write an email link (a href) given an encrypted email
    * and the encrypted text to be displayed on the link, to the document from
    * the client side.
    * USAGE: writeEmailAndText(encodedEmail, encodedText)
    ***************************************************************************/
   this.writeEmailAndText = _writeEncodedEmailAndEncodedText;

   /****************************************************************************
    * Use this function to write an email link (a href) given an encrypted email
    * and the plain text to be displayed on the link, to the document from the
    * client side.
    * USAGE: writeEmailAndPlainText(encodedEmail, plainText)
    ***************************************************************************/
   this.writeEmailAndPlainText = _writeEncodedEmailAndText;

   /****************************************************************************
    * Use this function to write an email link (a href) given an encrypted email
    * to the document from the client side.  This will use the encrypted email
    * for both the to address, and the link text.
    * USAGE: writeEmail(encodedEmail);
    ***************************************************************************/
   this.writeEmail = _writeEncodedEmail;

   /****************************************************************************
    * Use this function to write an email address given an encrypted email to
    * the document from the client side.  This will use the encrypted email and 
    * simply write the plaintext email address to the document, not a link.
    * USAGE: writeNoLinkEmail(encodedEmail);
    ***************************************************************************/
   this.writeNoLinkEmail = _writeNoLinkEncodedEmail;

   /****************************************************************************
    * Returns the encrypted plaintext. Use this to generate your encrypted text.
    * DO NOT USE THIS IN A PUBLIC PAGE, OR BOTS WILL SEE THE PLAIN TEXT IN THE
    * FUNCTION CALL.  Use the provided helper page to generate your encrypted
    * text.
    * USAGE: getEncryptedText(plainText)
    ***************************************************************************/
   this.getEncryptedText = _getEncryptedText;

   function _writeEncodedEmailAndEncodedText(encodedEmail, encodedText) {
      document.write("<a href=\"mailto:" + 
         _cipher(encodedEmail, true) + "\"" + _getCss() + ">" +
         _cipher(encodedText, true) + "</a>");
   }

   function _writeEncodedEmailAndText(encodedEmail, text) {
      document.write("<a href=\"mailto:" + 
         _cipher(encodedEmail, true) + "\"" + _getCss() + ">" +
         text + "</a>"); 
   }

   function _writeEncodedEmail(encodedEmail) {
      document.write("<a href=\"mailto:" + 
         _cipher(encodedEmail, true) + "\"" + _getCss() + ">" +
         _cipher(encodedEmail, true) + "</a>");
   }

   function _writeNoLinkEncodedEmail(encodedEmail) {
      document.write(_cipher(encodedEmail, true));
   }
   function _getEncryptedText(plaintext) {
      return _cipher(plaintext, false);
   }

   // Private Functions
   function _cipher(text, isEncoded) {
      var result = "";
      if(isEncoded) text = decode64(text);

      var i = 0;

      do {
         result = result + 
            String.fromCharCode(
               passkey.charCodeAt(i % passkey.length) ^ 
               text.charCodeAt(i++)
            );
      } while (i < text.length);

      if (isEncoded) return result
      else return encode64(result);
   }

   // The class variable that allows you to change the look of the mailto link to match
   // a pre-defined css class. If an empty string, the class will not be added.
   var css_class = "";

   // The style variable that allows you to change the look of the mailto link
   // to a custom css style. If an empty, the style will not be added.
   var css_style = "";

   function _setCssClass(newCssClass) {
      css_class = newCssClass;
   }

   function _setCssStyle(newCssStyle) {
      css_style = newCssStyle;
   }

   function _getCss() {
      var css = "";
      if (css_class != "") {
         css = " class=\"" + css_class + "\"";
      }
      if (css_style != "") {
         css = " style=\"" + css_style + "\"";
      }

      //alert("CSS: " + css);
      return css;
   }

   // Just the base64 conversion map
   var keyStr = 
      "ABCDEFGHIJKLMNOP" +
      "QRSTUVWXYZabcdef" +
      "ghijklmnopqrstuv" +
      "wxyz0123456789+/" +
      "=";

   function encode64(input) {
      var output = "";
      var chr1, chr2, chr3 = "";
      var enc1, enc2, enc3, enc4 = "";
      var i = 0;

      do {
         chr1 = input.charCodeAt(i++);
         chr2 = input.charCodeAt(i++);
         chr3 = input.charCodeAt(i++);

         enc1 = chr1 >> 2;
         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
         enc4 = chr3 & 63;

         if (isNaN(chr2)) {
            enc3 = enc4 = 64;
         } else if (isNaN(chr3)) {
            enc4 = 64;
         }

         output = output + 
            keyStr.charAt(enc1) + 
            keyStr.charAt(enc2) + 
            keyStr.charAt(enc3) + 
            keyStr.charAt(enc4);
         chr1 = chr2 = chr3 = "";
         enc1 = enc2 = enc3 = enc4 = "";
      } while (i < input.length);

      return output;
   }

   function decode64(input) {
      var output = "";
      var chr1, chr2, chr3 = "";
      var enc1, enc2, enc3, enc4 = "";
      var i = 0;

      // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
      input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

      do {
         enc1 = keyStr.indexOf(input.charAt(i++));
         enc2 = keyStr.indexOf(input.charAt(i++));
         enc3 = keyStr.indexOf(input.charAt(i++));
         enc4 = keyStr.indexOf(input.charAt(i++));

         chr1 = (enc1 << 2) | (enc2 >> 4);
         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
         chr3 = ((enc3 & 3) << 6) | enc4;

         output = output + String.fromCharCode(chr1);

         if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
         }
         if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
         }

         chr1 = chr2 = chr3 = "";
         enc1 = enc2 = enc3 = enc4 = "";

      } while (i < input.length);

      return output;
   }

}

/*
<!-- Creative Commons License -->
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/2.0/"><img alt="Creative Commons License" border="0" src="http://creativecommons.org/images/public/somerights20.gif" /></a><br />
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/2.0/">Creative Commons License</a>.
<!-- /Creative Commons License -->


<!--

<rdf:RDF xmlns="http://web.resource.org/cc/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<Work rdf:about="">
   <dc:title>EmailEncoder</dc:title>
   <dc:description>This is a Javascript Object used to encode email addresses in web pages in order to somewhat protect the user from e&#45;mail harvesters.  Utilizes Javascripts ability to modify the HTML document once it has been delivered to the client.</dc:description>
   <dc:creator><Agent>
      <dc:title>Chris Melnick</dc:title>
   </Agent></dc:creator>
   <dc:type rdf:resource="http://purl.org/dc/dcmitype/Text" />
   <license rdf:resource="http://creativecommons.org/licenses/by-nc-sa/2.0/" />
</Work>

<License rdf:about="http://creativecommons.org/licenses/by-nc-sa/2.0/">
   <permits rdf:resource="http://web.resource.org/cc/Reproduction" />
   <permits rdf:resource="http://web.resource.org/cc/Distribution" />
   <requires rdf:resource="http://web.resource.org/cc/Notice" />
   <requires rdf:resource="http://web.resource.org/cc/Attribution" />
   <prohibits rdf:resource="http://web.resource.org/cc/CommercialUse" />
   <permits rdf:resource="http://web.resource.org/cc/DerivativeWorks" />
   <requires rdf:resource="http://web.resource.org/cc/ShareAlike" />
</License>

</rdf:RDF>

-->
*/
