/*============================================================================*/
/*       Copyright (c) 2002-2003 Church of Scientology International          */
/*                         All rights reserved.                               */
/*============================================================================*/


/*============================================================================*/
/* Language-dependent Strings                                                 */
/*============================================================================*/
var myAlert_InvalidCharacters   = "The credit card number contains invalid characters.";
var myAlert_SelectCardType      = "Please select a card type.";
var myAlert_CardNumberMismatch  = "Please make sure the card number you've entered matches the card type you selected.";
var myAlert_CardLengthMismatch  = "Please make sure you've entered all of the digits on your card.";
var myAlert_IncorrectCardNumber = "Please make sure you've entered your card number correctly.";

/*============================================================================*/
/* OPERATION:  checkCardNumWithMod10                                          */
/*                                                                            */
/* Checks a supplied credit card number to ensure that it is KUHN Mod10       */
/* compliant                                                                  */
/*============================================================================*/
function checkCardNumWithMod10 (cardNum)
{
   var i;
   var cc = new Array(16);
   var checksum = 0;
   var validcc;

   //---- assign each digit of the card number to a space in the array   
   for (i = 0; i < cardNum.length; i++) {
      cc[i] = Math.floor(cardNum.substring(i, i+1));
   }

   //---- walk through every other digit - if the card number is sixteen digits
   //---- then start at the first digit (position 0), otherwise start from the
   //---- second (position 1)
   for (i = (cardNum.length % 2); i < cardNum.length; i+=2) {
      var a = cc[i] * 2;
      if (a >= 10) {
         var aStr = a.toString();
         var b = aStr.substring(0,1);
         var c = aStr.substring(1,2);
         cc[i] = Math.floor(b) + Math.floor(c);
      }
      else {
         cc[i] = a;
      }
   }

   //--- add up all of the digits in the array
   for (i = 0; i < cardNum.length; i++) {
      checksum += Math.floor (cc [i]);
   }

   //---- if the checksum is evenly divisble by 10, this is a valid card number
   return ((checksum % 10) == 0);
}

/*============================================================================*/
/* OPERATION:  cleanCardNum                                                   */
/*                                                                            */
/* Removes all non-numeric digits from the supplied card number string.       */
/*============================================================================*/
function cleanCardNum (cardNum)
{
   var i;
   var ch;
   var newCard = "";

   //---- walk through the string character by character to build a new string
   //---- with numbers only
   i = 0;
   while (i < cardNum.length) {
      ch = cardNum.substring(i, i+1);

      //---- A digit - add it to the numbers-only string we're building
      if ((ch >= "0") && (ch <= "9")) {
         newCard += ch;
      }

      //-- Not a digit, dash or space - invalid character!
      else if ((ch != " ") && (ch != "-")) {
         alert (myAlert_InvalidCharacters);
         return "";
      }
      i++;
   }

   //---- we got here if we didn't fail, so return what we built
   return newCard;
}

/*============================================================================*/
/* OPERATION:  checkCard                                                      */
/*                                                                            */
/* Checks a supplied credit card number to ensure that it is valid.           */
/*============================================================================*/
function checkCard (aCardType, aCardNum)
{
   var validCard;
   var cardLength;
   var cardLengthOK;
   var cardStart;
   var cardStartOK;
   var myCardType;

   if ((aCardType != null) && (aCardType != "")) {
      myCardType = (aCardType.substring (0, 1)).toUpperCase ();
   }

   //---- check if the card type is valid
   if ((myCardType != "V") &&
       (myCardType != "M") &&
       (myCardType != "A") &&
       (myCardType != "D")) {
      alert (myAlert_SelectCardType);
      return false;
   }

   //---- clean up any spaces or dashes in the card number
   validCard = cleanCardNum (aCardNum);
   if (validCard != "") {
      // check the first digit to see if it matches the card type
      cardStart   = validCard.substring (0,1);
      cardStartOK = (((myCardType == "V") && (cardStart == "4")) ||
                     ((myCardType == "M") && (cardStart == "5")) ||
                     ((myCardType == "A") && (cardStart == "3")) ||
                     ((myCardType == "D") && (cardStart == "6")));

      //--- card number's first digit doesn't match card type
      if (!(cardStartOK)) {
         alert (myAlert_CardNumberMismatch);
         return false;
      }

      //---- the card number is good now, so check to make sure it's a the right length
      cardLength = validCard.length;      
      cardLengthOK = (((myCardType == "V") && ((cardLength == 13) || (cardLength == 16))) ||
                      ((myCardType == "M") && (cardLength == 16)) ||
                      ((myCardType == "A") && (cardLength == 15)) ||
                      ((myCardType == "D") && (cardLength == 16)) );
      //---- not the right length
      if (!(cardLengthOK)) {
         alert (myAlert_CardLengthMismatch);
         return false;
      }

      //---- card number seems OK so do the Mod10
      if (checkCardNumWithMod10(validCard)) {
         return true;
      } else {
         alert("Please make sure you've entered your card number correctly.");
         return false;
      }
   } else {
      return false;
   }
   return true;
}
