// REQUIRES LOADING 'ccValidate.js' BEFORE
// THIS FILE IN YOUR CALLING PAGE

//-----------------------------------//
//	ESTABLISH RETURN STATE
//-----------------------------------//
var isOkay = true;

//-----------------------------------//
//	MAIN FUNCTION - ONE YOU CALL IN YOUR SUBMIT BUTTON
//	ONCE YOU HAVE ESTABLISHED YOUR FORM VALIDATION WORKS
//	SET YOUR FORM TAG ACTION (IN YOUR CALLING FORM) TO THE SCRIPT YOU DESIRE
//	THEN COMMENT OUT THE alert() STATEMENT UNDER if(isOkay)
//	AND UNCOMMENT THE document.forms["f1"].submit() line beneath it
//-----------------------------------//
function CheckCCInformation(){
	isOkay = CheckCC();
	if(isOkay){
	//	alert("Form completed!");
	//	document.forms["f1"].submit();
	} else {
	    //	DEBUG
	    //	UNCOMMENT IN CASE YOU RUN INTO TROUBLE
	    //alert("Form incomplete!");
	    //isOkay = true;
	    return false;
	}
}

//-----------------------------------//
//	THIS FUNCTION, CALLED ABOVE, DOES ALL THE WORK
//-----------------------------------//

function CheckCC(){ 

	//-----------------------------------//
	//	ENSURE A PAYMENT METHOD IS SELECTED
	//	0 = VISA
	//	1 = MASTERCARD
	//	2 = AMEX NOT USED
	//	3 = DISCOVER
	//	4 = DINERSCLUB
	//-----------------------------------//
	if(
		document.forms[0].billing[0].checked == true
		||
		document.forms[0].billing[1].checked == true
		){
		
		var mycardtype = GetRadioValue(document.forms[0].billing);
		
		//-----------------------------------//
		//	BIT OF DEBUG FOR YA - ONE LINE BELOW
		//-----------------------------------//
		//alert("Card type: " + mycardtype + "Card Number:" + document.forms[0].ccnumber.value);
		//-----------------------------------//
		//	IF A CREDIT CARD TYPE IS SELECTED ...
		//	CHECK CC NUMBER FOR VALIDITY BEFORE DOING ANYTHING
		//-----------------------------------//
		ccResultEquals = isValidCreditCardNumber(document.forms[0].ccnumber,mycardtype);
		
		//-----------------------------------//
		//	IF CC NUMBER IS VALID FOR CC TYPE THEN PROCEED WITH OTHER CHECKS
		//-----------------------------------//
		if(ccResultEquals){
			//-----------------------------------//
			//	CHECK EXPIRATION DATE
			//-----------------------------------//
			if(!isCCExpireDate(document.forms[0].ccexpiration)){
				return false;
			}
			//-----------------------------------//
			//	CHECK SECURITY CODE
			//-----------------------------------//
			//if(!isDigit(document.forms[0].payment_cc_security_code,"a Security Code Number")){
				//return false;
			//}
			
		}
		
		return true;
    } else {
		//-----------------------------------//
		//	DISPLAY ALERT IF NOT CC TYPE SELECTED
		//-----------------------------------//
    	alert("Please select a Credit Card Type");
    	document.forms[0].billing[0].focus();
    	return false;
    }
    return false;
}

