﻿function luhn_check(number) {
//<![CDATA[

  // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
  var number=number.replace(/\D/g, '');

  // Set the string length and parity
  var number_length=number.length;
  var parity=number_length % 2;

  // Loop through each digit and do the maths
  var total=0;
  for (i=0; i < number_length; i++) {
    var digit=number.charAt(i);
    // Multiply alternate digits by two
    if (i % 2 == parity) {
      digit=digit * 2;
      // If the sum is two digits, add them together (in effect)
      if (digit > 9) {
        digit=digit - 9;
      }
    }
    // Total up the digits
    total = total + parseInt(digit);
  }

  // If the total mod 10 equals 0, the number is valid
  if (total % 10 == 0) {
    return true;
  } else {
    return false;
  }

//]]>
}

//function luhn_check(card_number)
//{
//	
//	cc_array = card_number.split( " " )
//	cc_array.reverse()
//	digit_string = " "
//	
//	for ( counter=0; counter < cc_array.length; counter++ )
//	{
//		current_digit = parseInt( cc_array[counter] )
//		
//		if (counter %2 != 0)
//		{
//			cc_array[counter] *= 2
//		}
//		
//		digit_string += cc_array[counter]
//	
//	}
//	
//	digit_sum = 0
//	
//	for ( counter=0; counter<digit_string.length; counter++ )
//	{
//		current_digit = parseInt( digit_string.charAt(counter) )
//		digit_sum += current_digit
//	}
//	
//	if ( digit_sum % 10 == 0 )
//	{
//		return true
//	}
//	else
//	{
//		return false
//	}

//}
