/*============================================================================*/

/*

This routine checks the credit card number. The following checks are made:

1. A number has been provided
2. The number is a right length for the card
3. The number has an appropriate prefix for the card
4. The number has a valid modulus 10 number check digit if required

If the validation fails an error is reported.

The structure of credit card formats was gleaned from a variety of sources on 
the web, although the best is probably on Wikepedia ("Credit card number"):

  http://en.wikipedia.org/wiki/Credit_card_number

Parameters:
            cardnumber           number on the card
            cardname             name of card as defined in the card list below

Author:     John Gardner
Date:       1st November 2003
Updated:    26th Feb. 2005      Additional cards added by request
Updated:    27th Nov. 2006      Additional cards added from Wikipedia
Updated:    18th Jan. 2008      Additional cards added from Wikipedia
Updated:    26th Nov. 2008      Maestro cards extended

*/

/*
   If a credit card number is invalid, an error reason is loaded into the 
   global ccErrorNo variable. This can be be used to index into the global error  
   string array to report the reason to the user if required:
   
   e.g. if (!checkCreditCard (number, name) alert (ccErrors(ccErrorNo);
*/

var ccErrorNo = 0;

var ccErrors = new Array ();
ccErrors [0] = "No number"; // length = 0
ccErrors [1] = "Invalid length"; // not between 12 and 19 numbers long
ccErrors [2] = "Invalid length"; // prefix found, length doesn't match
ccErrors [3] = "Unknown type"; // can't determine card type based on prefix and length
ccErrors [4] = "Invalid"; // mod 10

var ccErrorsLong = new Array ();
ccErrorsLong [0] = "You didn't enter a number.";
ccErrorsLong [1] = "It is either not long enough, or too long to be a credit card number.";
ccErrorsLong [2] = "The length of the card number doesn't seem to match the type of card it is.";
ccErrorsLong [3] = "We weren't able to determine the type of card it is.";
ccErrorsLong [4] = "The number provided doesn't seem to be valid for the type of card it is.";

var ccErrorsMsg = "";

function checkCreditCard (cardnumber) {
  
  ccErrorsMsg = "\n";
  
  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types.
  
  //  Name:      As in the selection box of the form - must be same as user's
  //  Length:    List of possible valid lengths of the card number for the card
  //  prefixes:  List of possible prefixes for the card
  //  checkdigit Boolean to say whether there is a check digit
  
  cards [0] = {name: "Visa Electron", 
               length: "16", 
               prefixes: "417500,4917,4913,4508,4844",
               checkdigit: true};
  cards [1] = {name: "Switch", 
               length: "16,18,19", 
               prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
               checkdigit: true};
  cards [2] = {name: "Maestro", 
               length: "12,13,14,15,16,18,19", 
               prefixes: "5018,5020,5038,6304,6759,6761",
               checkdigit: true};
  cards [3] = {name: "Solo", 
               length: "16,18,19", 
               prefixes: "6334, 6767",
               checkdigit: true};
  cards [4] = {name: "Discover", 
               length: "16", 
               prefixes: "6011,622,64,65",
               checkdigit: true};
  cards [5] = {name: "enRoute", 
               length: "15", 
               prefixes: "2014,2149",
               checkdigit: true};
  cards [6] = {name: "Carte Blanche", 
               length: "14", 
               prefixes: "300,301,302,303,304,305",
               checkdigit: true};
  cards [7] = {name: "Visa", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [8] = {name: "MasterCard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [9] = {name: "Diners Club", 
               length: "14,16", 
               prefixes: "36,54,55",
               checkdigit: true};
  cards [10] = {name: "American Express", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  cards [11] = {name: "JCB", 
               length: "16", 
               prefixes: "35",
               checkdigit: true};
               

  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 0;
     return false; 
  }
  
  // Remove anything that isn't a number
  //cardnumber = cardnumber.replace (/\s/g, "");
  //cardnumber = cardnumber.replace (/-/g, "");
  cardnumber = cardnumber.replace(/[^0-9]/g, '');
  
  // Check that the number is between 12 to 19 numbers long
  var cardexp = /^[0-9]{12,19}$/;
  if (!cardexp.exec(cardnumber))  {
     ccErrorNo = 1;
     return false; 
  }
  
  // Try to find card type
  var cardType = -1;
  var prefixFound = false;
  for (var i=0; i<cards.length; i++) {
	var prefix = new Array ();
    var lengths = new Array ();
	
	prefix = cards[i].prefixes.split(",");
    for (j=0; j<prefix.length; j++) { // try to find a matching prefix
      var exp = new RegExp ("^" + prefix[j]);
      if (exp.test (cardnumber)){ // prefix found
		prefixFound = true;
		ccErrorsMsg += "\n"+cards[i].name; // possible prefix / card type
		
        lengths = cards[i].length.split(",");
    	for (k=0; k<lengths.length; k++) { // see if the length matches
	      if (cardnumber.length == lengths[k]){ // length matches
			  cardType = i; // we found the card type
			  ccErrorsMsg = "\n";
			  break;
		  }
    	} // for (length)
		
	  }
    } // for (prefix)
	
  }

  // If card type not found, but possible prefixes found, report an error
  if (cardType == -1 && prefixFound == true) {
     ccErrorNo = 2; // will also have possible card types in ccErrorMsg
     return false; 
  }
  
  // If card type not found, and no possible prefix found, report an error
  if (cardType == -1) {
     ccErrorNo = 3;
     return false; 
  }
   
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardnumber.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardnumber.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 4;
	 ccErrorsMsg += "\n"+cards[cardType].name;
     return false; 
    }
  }  
  
  // The credit card is in the required format.
  return cards[cardType].name;
}

/*============================================================================*/