/* JavaScript functions for protecting embedded email adresses
 * from spam attacks.
 *
 * (C) Saturday, December 2nd 2006, Dennis Schulmeister
 * This code is licenced under any GNU GPL you may find appropriate.
 *
 * Although I hope this code will be of use to you there's absolutely
 * NO WARANTY for correct operation or fitness given.
 *
 * Usage:
 *    First encode your email adress using some code like this:
 *    <script type="text/javascript">
 *    <!--
 *       document.write(encode_adress("my@email.org"));
 *    -->
 *    </script>
 *
 *    Than open the document in your favorite browser and copy the encrypted
 *    string to the clipboard. Note: A html file for exactly that purpose comes
 *    with this package.
 *
 *    Finaly implement the link to your email like this:
 *    <a href="javascript:decode_and_link_to('ENCRYPTED STRING GOES HERE')">my(at)email(dot)com</a>
 *
 *    Please make sure to inlcude this file to all your html files. This is done by adding the
 *    following line to the header section of your html files:
 *    <script src="PATH TO THIS FILE GOES HERE" type="text/javascript" language="JavaScript"></script>
 *
 * Please tell me your feelings about this code. Write to: egopjuAyjpeqxu40eg
 * or dennis(at)windows3(dot)de.
*/


// This function returns an encrypted string of the email adress given to it.
function encode_adress(a)
{
   var nr = 0;
   var enc = "";

   for(var i = 0; i < a.length; i++)
   {
      nr = a.charCodeAt(i);

      if (i % 2 == 0)
      {
         if ((nr >= 33) && (nr < 127))
         {
            nr = nr + 1
         }

         else if (nr == 127)
         {
            nr = 33;
         }
      }

      else
      {
         if ((nr >= 33) && (nr < 126))
         {
            nr = nr + 2
         }

         else if (nr == 126)
         {
            nr = 33;
         }
                  
         else if (nr == 127)
         {
            nr = 34;
         }

      }

      enc += String.fromCharCode(nr);
   }

   return enc;
}
         
// This function returns the decrypted email adress.
function decode_adress(a)
{
   var nr = 0;
   var dec = "";

   for(var i = 0; i < a.length; i++)
   {
      nr = a.charCodeAt(i);

      if (i % 2 == 0)
      {
         if ((nr > 33) && (nr <= 127))
         {
           nr = nr - 1
         }

         else if (nr == 33)
         {
           nr = 127;
         }
      }
               
      else
      {
         if ((nr > 34) && (nr <= 126))
         {
            nr = nr - 2
         }

         else if (nr == 33)
         {
            nr = 126;
         }

         else if (nr == 34)
         {
           nr = 127;
         }

      }

      dec += String.fromCharCode(nr);
   }

   return dec;
}

// This function adds the mailto-header for linking against an adress
function add_head(s)
{
   r = "";
   r = decode_adress("ncjnuq;") + s;
   return r;
}

// This is your function of choice. It decrypts and adds the header for linking.
function decode_and_link_to(s)
{
   location.href = add_head(decode_adress(s));
}
