var card_type

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 doing our magic
	// 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
	// then this is a valid card number
	validcc = ((checksum % 10) == 0);

	return validcc;
}

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) {
		// get the current character
		ch = cardNum.substring(i, i+1);
		if ((ch >= "0") && (ch <= "9")) {
			// if the current character is a digit then add it
			// to the numbers-only string we're building
			newCard += ch;
		} else {
			// not a digit, so check if its a dash or a space
			if ((ch != " ") && (ch != "-")) {
				// not a dash or a space so fail
				alert("This number contains invalid characters.");
				return "";
			}
		}
		i++;
	}

	// we got here if we didn't fail, so return what we built
	return newCard;
}

function checkCard(s) {
	var cardType = get_card_type(s.pSelect[s.pSelect.selectedIndex].value);
	var cardNum = s.num_x.value;
	var validCard;
	var cardLength;
	var cardLengthOK;
	var cardStart;
	var cardStartOK;
	
	// check if the card type is valid
	if ((cardType != "V") && (cardType != "M") && (cardType != "A") && (cardType != "D")) {
		alert("Please select a card type.");
		return false;
	}

	// clean up any spaces or dashes in the card number
	validCard = cleanCardNum(cardNum);
	if (validCard != "") {
		// check the first digit to see if it matches the card type
		cardStart = validCard.substring(0,1);
		cardStartOK = ( ((cardType == "V") && (cardStart == "4")) ||
				((cardType == "M") && (cardStart == "5")) ||
				((cardType == "A") && (cardStart == "3")) ||
				((cardType == "D") && (cardStart == "6")) );
		if (!(cardStartOK)) {
			// card number's first digit doesn't match card type
			alert("Please make sure the card number you've entered matches the card type you selected.");
			return false;
		}

		// the card number is good now, so check to make sure
		// it's a the right length
		cardLength = validCard.length;		
		cardLengthOK = ( ((cardType == "V") && ((cardLength == 13) || (cardLength == 16))) ||
				 ((cardType == "M") && (cardLength == 16)) ||
				 ((cardType == "A") && (cardLength == 15)) ||
				 ((cardType == "D") && (cardLength == 16)) );
		if (!(cardLengthOK)) {
			// not the right length
			alert("Please make sure you've entered all of the digits on your card.");
			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 { // no card number entered?
		alert("Please enter your credit card number.");
		s.num_x.focus();
		return false;
	}
}

function get_card_type(c_type){

	if (c_type == "1")
		return("V");
	if (c_type == "2")
		return("M");
	if (c_type == "3")
		return("A");
	if (c_type == "4")
		return("D");
}

function check_cvn(s){
	var card_type = get_card_type(s.pSelect[s.pSelect.selectedIndex].value);

	var cvn = trim(s.cvn.value);
	
	var cvn_len = cvn.length;

	if (0 == cvn_len){
		alert("Please enter your Card Verification Number.");
		s.cvn.focus();
		return(false);
	}
	
	if (!isNumber(cvn)){
		alert("The Card Verification Number you have entered is not valid.");
		s.cvn.focus();
		return(false);
	}	
	
	var cvnOK;
	
	cvnOK = ( ((card_type == "V") && (cvn_len == 3)) ||
				((card_type == "M") && (cvn_len == 3)) ||
				((card_type == "A") && (cvn_len == 4)) ||
				((card_type == "D") && (cvn_len == 3)) );

	if (!(cvnOK)) {
			// not the right length
			alert("The card verification number you've entered is not valid.");
			s.cvn.focus();
			return false;
	}
	return(true);				
}

function checkCardExp(s) {

	var d = new Date(), result;
	var test_mo = Number(s.mo[s.mo.selectedIndex].value);
	var test_yr = Number(s.yr[s.yr.selectedIndex].value);
	
	var cur_mo = d.getMonth() + 1; //getMonth is zero based
	var cur_yr = d.getFullYear();

	if ((0 == test_mo) || (0 == test_yr)){ // missing expiration value
		alert("Please enter the expiration date for your credit card.");
		return(false);	
	}

	//alert("cur_yr="+cur_yr + "\ntest_yr=" + test_yr);
	//alert("cur_mo="+cur_mo + "\ntest_mo=" + test_mo);
	
	if (cur_yr < test_yr){
		return(true);
	}
	
	if (cur_yr > test_yr){
		alert("The expiration date you have entered has past.");
		return(false); // bad year
	}

	// if we're still here, the years are equal
	if (cur_mo > test_mo){
		alert("The expiration date you have entered has past.");
		return(false); // bad month
	}
	
	//if (cur_mo > test_mo){
	//	return(true); // date is OK
	//}
	return(true);
}


