/*
There are three functions in this set for credit card validation.
The main function is:
validateCard(cardNumber,cardType,cardMonth,cardYear)
	parameters:
		all paramaters are string values.
		Month & Year come from the select input fields in the form, so they are defined.
		cardType can be:
			'AMEX' for American Express
			'Discover' for Discover
			'Mastercard' for MasterCard
			'Visa' for Visa
	description:
		this function will check string length, valid characters, specific credit card prefixes and test
		the Mod 10 (LUHN Formula) for validating possible credit card numbers. this function can only
		authorize that the given card data is potentially valid. You would still need to run actual
		card validation routines to verify the actual account.
	returns:
		this function returns true if the card number could be valid for the card type and expiration date.
		false otherwise.	
supporting functions:
mod10( cardNumber )
	parameters:
		this function takes the text string card number and runs the Mod 10 formula on its respective digits.
	description:
		Mod 10 is the check digit formula for the supported cards these functions attempt to validate.
	returns:
		this function returns true if the number passes the check digit test.
		false otherwise.
expired( cardMonth, cardYear )
	parameters:
		this function takes the text string values given by the html form.
	description:
		this function basically will check to make sure todays date is less than the expiration date the user inputs.
		this function is not locked into using 2 digit dates.
	returns:
		this fucntion returns true if the card is expired.
		false otherwise.
*/
function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;


    	for( i = 0; i < cardNumber.length; ++i ) {
    		ar[i] = parseInt(cardNumber.charAt(i));
    	}
    	for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
    		ar[i] *= 2;							 // every second digit starting with the right most (check digit)
    		if( ar[i] > 9 ) ar[i]-=9;			 // will be doubled, and summed with the skipped digits.
    	}										 // if the double digit is > 9, ADD those individual digits together 


        	for( i = 0; i < ar.length; ++i ) {
        		sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
        	}
        	return (((sum%10)==0)?true:false);	 	
    }



function expired( month, year ) {
	var now = new Date();							// this function is designed to be Y2K compliant.
	var expiresIn = new Date(year,month,0,0,0);		// create an expired on date object with valid thru expiration date
	expiresIn.setMonth(expiresIn.getMonth()+1);		// adjust the month, to first day, hour, minute & second of expired month
	if( now.getTime() < expiresIn.getTime() ) return false;
	return true;									// then we get the miliseconds, and do a long integer comparison

}


function getRadioVal(radioObj) {
		var value = "";

		if (!radioObj.length){
			if (radioObj.checked) value = radioObj.value;
		}
		else{
			for (var i = 0; i < radioObj.length; i++){
				if (radioObj[i].checked){
					value = radioObj[i].value;
					break;
				}
			}
		}

		return value;
}
		
	 	
function validateCard(cardNumber,cardMonth,cardYear,cvvNumber) {
	
	var cardType = getRadioVal(document.IntuitForm.CCType);
	
	if( cardNumber.length == 0 ) {						//most of these checks are self explanitory
		alert("Please enter a valid card number.");
		return false;				
	}
	for( var i = 0; i < cardNumber.length; ++i ) {		// make sure the number is all digits.. (by design)
		var c = cardNumber.charAt(i);


			if( c < '0' || c > '9' ) {
				alert("Please enter a valid card number. Use only digits. Do not use spaces or hyphens.");
				return false;
			}
		}
		var length = cardNumber.length;			//perform card specific length and prefix tests
		var cvvlength = cvvNumber.length;

			switch( cardType ) {
				case 'AMEX':


						if( length != 15 ) {
							alert("Please enter a valid American Express Card number.");
							return false;
						}
						
						if( cvvlength != 4 ) {
							alert("Please enter a valid American Express CVV number.");
							return false;
						}
						
						var prefix = parseInt( cardNumber.substring(0,2));


							if( prefix != 34 && prefix != 37 ) {
								alert("Please enter a valid American Express Card number.");
								return false;
							}
							break;
						case 'Discover':


								if( length != 16 ) {
									alert("Please enter a valid Discover Card number.");
									return false;
								}
								
								if( cvvlength != 3 ) {
									alert("Please enter a valid Discover CVV number.");
									return false;
								}
								
								var prefix = parseInt( cardNumber.substring(0,4));


									if( prefix != 6011 ) {
										alert("Please enter a valid Discover Card number.");
										return false;
									}
									break;
								case 'Mastercard':


										if( length != 16 ) {
											alert("Please enter a valid Mastercard number.");
											return false;
										}
										
										if( cvvlength != 3 ) {
											alert("Please enter a valid Mastercard CVV number.");
											return false;
										}
										
										var prefix = parseInt( cardNumber.substring(0,2));


											if( prefix < 51 || prefix > 55) {
												alert("Please enter a valid MasterCard Card number.");
												return false;
											}
											
											break;
										case 'Visa':


												if( length != 16 && length != 13 ) {
													alert("Please enter a valid Visa Card number.");
													return false;
												}
												
												if( cvvlength != 3 ) {
													alert("Please enter a valid Visa CVV number.");
													return false;
												}
												
												var prefix = parseInt( cardNumber.substring(0,1));


													if( prefix != 4 ) {
														alert("Please enter a valid Visa Card number.");
														return false;
													}
													break;
											}
											if( !mod10( cardNumber ) ) { 		// run the check digit algorithm
												alert("Sorry! this is not a valid credit card number.");
												return false;
											}
											if( expired( cardMonth, cardYear ) ) {							// check if entered date is already expired.
												alert("Sorry! The expiration date you have entered would make this card invalid.");
												return false;
											}
											
											return true; // at this point card has not been proven to be invalid
									}

function testNum(mynum){
	if (mynum != parseInt(mynum)){
		return false;
	}
	else{
		return true;
	}
}

function validateEFT(bankName,checkNumber,accountNumber,routingNumber){

	if (bankName.length < 1){
		alert("Sorry, you must enter a valid bank name.");
		return false;
	}
	
	if (testNum(checkNumber)){
		if (testNum(accountNumber)){
			if (testNum(routingNumber)){
				return true;
			}
			else{
				alert("Sorry, the routing number should contain only digits. Do not use spaces or hyphens.");
				return false;
			}
		}
		else{
			alert("Sorry, the account number should contain only digits. Do not use spaces or hyphens.");
			return false;
		}
	}
	else{
		alert("Sorry, the check number should contain only digits. Do not use spaces or hyphens.");
		return false;
	}
}

function validateForm(cardNumber,cardMonth,cardYear,cvvNumber,bankName,checkNumber,accountNumber,routingNumber){
	
	var paymentType = getRadioVal(document.IntuitForm.PaymentTypeGroup);
	
	if (paymentType == "CC"){
		return validateCard(cardNumber,cardMonth,cardYear,cvvNumber);
	}
	
	else if (paymentType == "EFT"){
		return validateEFT(bankName,checkNumber,accountNumber,routingNumber);
	}
	
	else{
		return true;
	}
	
}