// Credit Card Format Validator
// Version 1.0 completed April 28, 2002
// Programmer:  William Bontrager
// Website: http://willmaster.com

// Customization:
//
// There are 4 customization steps:
//

// Step 1:
// Between the quotes, type what the alert box message shall say when
// the credit card is unknown and the number did not pass?
// To force a line break, use "\n" (without the quotes).
// For no message, type "" (nothing between the quotes).

Step1Message = "Credit card is not known.\nCredit card number is invalid.";

//
// Step 2:
// Between the quotes, type what the alert box message shall say when
// the credit card is known and the number did not pass?
// To force a line break, use "\n" (without the quotes).
// For no message, type "" (nothing between the quotes).

Step2Message = "Credit card number is invalid.";

//
// Step 3:
// Between the quotes, type what the alert box message shall say when
// the credit card is unknown and the number passed?
// To force a line break, use "\n" (without the quotes).
// For no message, type "" (nothing between the quotes).

Step3Message = "Credit card is not known.";

//
// Step 4:
// When the credit card is known and the number passed, 
// the script can display the credit card type and the 
// number (stripped of hyphens and other non-digit characters).
// To display this message, type a Y or a y between the quotes.
// To not display this message, type "" (no space between the quotes).

DisplayAllOkayMessage = "";



////////////////////////////////////////
//                                    //
// No further customization required. //
//                                    //
////////////////////////////////////////


////////////////////////////////////////
//
//    N O T E S :
//
// Three global variables are set when function Validate() is called.
// If appropriate, your application can consult these variables:
//
// Variable Name     Value
// ^^^^^^^^^^^^^     ^^^^^
// ccType          - The name of the credit card, if determinable. 
//                   Otherwise null.
// ccNumberOkay    - Digit 1 if the credit card number is of a valid 
//                   format. Otherwise digit 0.
// ccNumberChecked - The credit card number that was checked (all spaces 
//                   and non-digit characters stripped out). Example:
//                   CC # 1234-5678-901-234 is checked as 12345678901234
//
////////////////////////////////////////



ccType = '';
ccNumberOkay = 0;
ccNumberChecked = '';

function StripNonDigits(number) {
var Re = /\d+/g;
if(Re.lastIndex > 1) { Re.lastIndex = 0; }
var Array = Re.exec(number);
if(Re.lastIndex < 1) { return 'X'; }
var ss = Array.join();
while(Re.lastIndex > 0 && Re.lastIndex < number.length) {
	Array = Re.exec(number);
	if(Array) { ss += Array.join(); }
	}
return ss;
} // StripNonDigits()


function GetType(number) {
var len = number.length;
var Re = /^5[1-5]/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		var Array = Re.exec(number);
			if(Array && len == 16) { return 'MasterCard'; }
Re = /^4/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 13 || len == 16)) { return 'Visa'; }
Re = /^3[47]/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && len == 15) { return 'AmericanExpress'; }
Re = /^(30[0-5]|3[68])/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && len == 14) { return 'Diners Club'; }
Re = /^6011/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && len == 14) { return 'Discover'; }
Re = /^(3|1800|2131)/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 15 || len == 16)) { return 'JCB'; }
return '';
} // GetType()


function Reverse(number) {
var n = '';
for(i = number.length; i >= 0; i--) { n += number.substr(i,1); }
return n;
} // Reverse()


function AddedTogether(number) {
var n = 0;
for(i = 0; i < number.length; i++) {
	var s = number.substr(i,1);
	var si = parseInt(s,10);
	if(i % 2 > 0) {
		var ii = si * 2;
		if(ii < 10) { n += ii; }
		else {
			var ss = ' ' + ii;
			for(xi = 1; xi < ss.length; xi++) {
				var xs = ss.substr(xi,1);
				var xsi = parseInt(xs,10);
				n += xsi;
				} // for
			} // else
		} // if
	else { n += si; }
	} // for
return n;
} // AddedTogether()


function Mod10(n) {
var reversed = Reverse(n);
var total = AddedTogether(reversed);
if(total % 10 > 0) { return 0; }
return 1;
} // Mod10()


function validate(n,inType) {
	ccNumberChecked = StripNonDigits(n);
	if(ccNumberChecked == 'X') {
		alert('Please supply a credit card number.');
		return false;
		}
	ccType = GetType(ccNumberChecked);
	ccNumberOkay = Mod10(ccNumberChecked);
	if ( Step1Message.length > 0 && ccType.length < 2 && ccNumberOkay == 0) { 
		alert(Step1Message); 
		return false;
	} else if ( Step2Message.length > 0 && ccNumberOkay == 0 ) { 
		alert(Step2Message); 
		return false; 
	} else if(Step3Message.length > 0 && ccType.length < 2 ) { 
		alert(Step3Message); 
		return false;
	} else if ( ccType != inType ) { //type doesn't match
		alert( "Your card number does not match card type selected." );
		return false;
	} else {
		return true;
	}
} // Validate()