<!--
/***********************************************
* Required field(s) validation
***********************************************/
function validate_form(form) {
//-------- Check Form Information ------------------------------------------
	if (form.fname.value.length == 0) {
		alert("Please enter a Card Holder's First Name.");
		form.fname.focus();
		return false;
	}
	
	if (form.lname.value.length == 0) {
		alert("Please enter a Card Holder's Last Name.");
		form.lname.focus();
		return false;
	}

//--------- Check Email Format ------------------------------------------------
	var Email_Check = false;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value)){
		Email_Check = true;
	}

	if (!Email_Check) {
		alert("Email is a required field.");
		form.email.focus();
		return false;
	}
	

	if (form.Billing_Address1.value.length == 0) {
		alert("Please enter a Billing Address.");
		form.Billing_Address1.focus();
		return false;
	}

	if (form.Billing_Phone.value.length == 0) {
		alert("Please enter a Contact Number.");
		form.Billing_Phone.focus();
		return false;
	}

	
	if (form.Billing_City.value.length == 0) {
		alert("Please enter a City/Town.");
		form.Billing_City.focus();
		return false;
	}
	
	if (form.Billing_State.value.length == 0) {
		alert("Please enter a State.");
		form.Billing_State.focus();
		return false;
	}

	if (form.Billing_Zipcode.value.length == 0) {
		alert("Please enter a Zipcode.");
		form.Billing_Zipcode.focus();
		return false;
	}

// ---------- Check Check Card Information -----------------------------
	var ccnum= document.getElementById('cardnumber');
	if(!ccvalid(ccnum.value))
	{
		alert("Please enter a valid credit card.");
		form.cardnumber.focus();
		return false;
	}

	if(form.cvv2number.value.length < 3) {
		alert("Credit Card verification number must be at least 3 digits.");
		form.cvv2number.focus();
		return false;
	}

	if(form.expmonth.selectedIndex == 0) {
		alert("Please select a expiration month.");
		form.elements['expmonth'].focus();
		return false;
	}

	if(form.expyear.selectedIndex == 0) {
		alert("Please select a expiration year.");
		form.elements['expyear'].focus();
		return false;
	}

	return true;
}

function ccvalid(s) {
	var v = "0123456789";
	var w = "";
	for (i=0; i < s.length; i++) {
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
		w += x;
	}
	j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	for (i=0; i<k; i++) {
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}

function formCheck(formobj){
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].text == "--- Select ---"){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
				if (obj.name == "email" || obj.name == "Email") {
					if (obj.value.indexOf("@") == -1 || obj.value.indexOf(".") == -1) {
						alertMsg += " - Valid " + fieldDescription[i] + " Address\n";
					}
					break;
				}

				if (obj.name == "cardnumber" || obj.name == "cardNumber") {
					if(ccvalid(obj.value)== -1)  { 		// run the check digit algorithm
					alert(ccvalid(obj.value));
					//if( Mod10(obj.value) == false ) { 		// run the check digit algorithm
						alertMsg += "Sorry! This is not a valid credit card number.\n";
					}
					break;
				}
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		// Change return to True if you wish to submit form 
		// information to another page.
		//alert("Just between the two of us, form submitted!")
		//return false;
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

function Mod10(ccNumb) {
	var valid = "0123456789"  // Valid digits in a credit card number
	var len = ccNumb.length;  // The length of the submitted cc number
	var iCCN = parseInt(ccNumb);  // integer of ccNumb
	var sCCN = ccNumb.toString();  // string of ccNumb
	sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
	var iTotal = 0;  // integer total set at zero
	var bNum = true;  // by default assume it is a number
	var bResult = false;  // by default assume it is NOT a valid cc
	var temp;  // temp variable for parsing string
	var calc;  // used for calculation of each digit
	
	// Determine if the ccNumb is in fact all numbers
	for (var j=0; j<len; j++) {
		temp = "" + sCCN.substring(j, j+1);
		if (valid.indexOf(temp) == "-1"){bNum = false;}
	}
	
	// if it is NOT a number, you can either alert to the fact, or just pass a failure
	if(!bNum){
		/*alert("Not a Number");*/
		bResult = false;
	}
	
	// Determine if it is the proper length 
	if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
		bResult = false;
	} else{  
		// ccNumb is a number and the proper length - let's see if it is a valid card number
		if(len >= 15){  // 15 or 16 for Amex or V/MC
		for(var i=len;i>0;i--){  // LOOP throught the digits of the card
			calc = parseInt(iCCN) % 10;  // right most digit
			calc = parseInt(calc);  // assure it is an integer
			iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
			i--;  // decrement the count - move to the next digit in the card
			iCCN = iCCN / 10;             // subtracts right most digit from ccNumb
			calc = parseInt(iCCN) % 10 ;  // NEXT right most digit
			calc = calc *2;              // multiply the digit by two
			// Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
			// I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
			switch(calc){
			case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
			case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
			case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
			case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
			case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
			default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
			}                                               
			iCCN = iCCN / 10;  // subtracts right most digit from ccNum
			iTotal += calc;  // running total of the card number as we loop
		}  // END OF LOOP

			if ((iTotal%10)==0) { 
				bResult = true;  // This IS (or could be) a valid credit card number.
			} else {
				bResult = false;  // This could NOT be a valid credit card number
			}
		}
	}
  return bResult; // Return the results
}

// -->