<!--
////////////////////////////////////////////////////////////////////
//
// SimpleEncrypt.js
//
// A Simple Symetric Encryption Class
//
////////////////////////////////////////////////////////////////////

function SimpleJsEncryptionClass ()
{
  //	Add or remove characters to 'alphabet' in the same way as shown.
  //	Any characters not in alphabet will be ignored when encrypting.
  this.alphabet = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
                            '3', '4', '5', '6', '7', '8', '9', '0', '(', ')', '~', '!', '@', '#', '$', '%', '^', '&',
                            '*', '-', '=', '+', '_', '.', ',', '`', '{', '}', '[', ']', '|', '\'', '\\', '\"', '/', '?',
                            ':', ';', '>', '<', ' ', '\n');

  //	Temporary array for when producing binary numbers. Longer length = better encryption.
  //	Note:	the maxbinary number must be more than the alphabet length.
  //		there must be an 'a' at the end of the array.
  this.tempbin = new Array("0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "a");

  //	Binary numbers stored here and refer to original alphabet.
  this.binary = new Array();

  //	Variables for the jumbled(encrypted) alphabet, decrypted text, and encrypted text.
  this.encryptedAlphabet;
  this.initialized = false;
}

new SimpleJsEncryptionClass();

SimpleJsEncryptionClass.prototype.MakeBinary = function (currentPosition, start, currentNumber)
{
  //	function to make the binary numbers
  //	len = binary number length
  //	The rest is straight forward - figure it out for yourself.

  if (currentPosition < 0)
    return;

  if (this.tempbin[currentPosition] == "1")
  {
    this.tempbin[currentPosition] = "0";
    var x = this.MakeBinary(currentPosition - 1, start, currentNumber); // ????
  }
  else if (this.tempbin[currentPosition] == "0")
  {
    this.tempbin[currentPosition] = "1";
  }
  else if (this.tempbin[currentPosition] == "a")
  {
    this.tempbin[currentPosition] = "0";
  }

  if (currentPosition == start)
  {
    this.binary[currentNumber] = "";

    for (i = 0; i <= currentPosition; i++)
        this.binary[currentNumber] += this.tempbin[i];
  }

  return;
}

SimpleJsEncryptionClass.prototype.Initialize = function ()
{
  if (!this.initialized)
  {
    //	Calls MakeBinary() to make enough numbers for all of alphabet.
    for (var currentNumber = 0; currentNumber < this.alphabet.length; currentNumber++)
    {
      this.MakeBinary(this.tempbin.length - 1, this.tempbin.length - 1, currentNumber);
    }
  }

  this.initialized = true;
}

SimpleJsEncryptionClass.prototype.IsEncrypted = function (valueIn)
{
  // Checks value to see if it can be decrypted
  return (this.Decrypt(valueIn).length > 0);
}

SimpleJsEncryptionClass.prototype.IsInEncalphabet = function (x)
{
  //	Checks to see if a letter (alphabet[x]) is in encryptedAlphabet so there are no duplicates in encryptedAlphabet.
  if (x == -1)
    return 0;

  var z = this.alphabet[x];

  for (i = 0; i < this.encryptedAlphabet.length; i++)
  {
    if (z == this.encryptedAlphabet.substr(i, 1))
      return 0;
  }

  return 1;
}

SimpleJsEncryptionClass.prototype.Encrypt = function (unencryptedValueIn)
{
  //	Encrypt function 
  if (!this.initialized)
  {
    this.Initialize();
  }

  //	Set start values
  this.encryptedAlphabet = "";
  var encryptedValue = "";
  var temp;
  var i;
  var currentNumber;
  var x;

  //	Make encryptedAlphabet (ie. jumble alphabet)
  for (i = 0; i < this.alphabet.length; i++)
  {
    x = -1;

    while (this.IsInEncalphabet(x) == 0)
    {
      x = Math.floor(this.alphabet.length * Math.random());
    }

    this.encryptedAlphabet += this.alphabet[x];
  }

  temp = this.encryptedAlphabet;

  //	First level of encryption.
  //	Uses encryptedAlphabet's order to jumble text.
  for (i = 0; i < unencryptedValueIn.length; i++)
  {
    for (currentNumber = 0; currentNumber < this.encryptedAlphabet.length; currentNumber++)
    {
      if (unencryptedValueIn.substr(i, 1) == this.alphabet[currentNumber])
      {
        temp += this.encryptedAlphabet.substr(currentNumber, 1);
        break;
      }
    }
  }

  //	Second level of encryption.
  //	Sets everything into this.binary.
  for (i = 0; i < temp.length; i++)
  {
    for (currentNumber = 0; currentNumber < this.alphabet.length; currentNumber++)
    {
      if (temp.substr(i, 1) == this.alphabet[currentNumber])
      {
        encryptedValue += this.binary[currentNumber];
        break;
      }
    }
  }

  // return encrypted text.
  return encryptedValue;
}

SimpleJsEncryptionClass.prototype.Decrypt = function (encyptedValueIn)
{
  //	Decryption function.
  if (!this.initialized)
  {
    this.Initialize();
  }

  //	Set initial values.
  this.encryptedAlphabet = "";
  var temp = "";
  var decryptedValue = "";
  var i;
  var currentNumber;

  if (encyptedValueIn != null)
  {
    //	Go from this.binary to first level encryption (ie. Still jumbled).
    for (i = 0; i < encyptedValueIn.length; i += this.tempbin.length)
    {
      for (currentNumber = 0; currentNumber < this.alphabet.length; currentNumber++)
      {
        if (encyptedValueIn.substr(i, this.tempbin.length) == this.binary[currentNumber])
        {
          temp += this.alphabet[currentNumber];
          break;
        }
      }
    }

    //	Set the order of the alphabet (into encryptedAlphabet) from jumbled text.
    for (i = 0; i < this.alphabet.length; i++)
    {
      this.encryptedAlphabet += temp.substr(i, 1);
    }

    //	Unjumble the text.
    while (i < encyptedValueIn.length)
    {
      for (currentNumber = 0; currentNumber < this.encryptedAlphabet.length; currentNumber++)
      {
        if (temp.substr(i, 1) == this.encryptedAlphabet.substr(currentNumber, 1))
        {
          decryptedValue += this.alphabet[currentNumber];
          break;
        }
      }

      i++;
    }
  }
  // return the decrypted text.
  return decryptedValue;
}
-->
