<!-- Begin

var submitcount=0;


function checkFields() {                       


	var e='';

	if (document.orderform.bname.value == "") {
		var e = e + 'Customer Name is required.\n';
	}

	if (document.orderform.baddr1.value == "") {
		var e = e + 'Address is required.\n';
	}

	if (document.orderform.bcity.value == "") {
		var e = e + 'City is required.\n';
	}

	var badstate = 0;
	if (document.orderform.bstate.value == "") {
		var e = e + 'State is required.\n';
		var badstate = 1;
	}

	var badzip = 0
	if (document.orderform.bzip.value == "") {
		var e = e + 'Zip Code is required.\n';
		var badzip = 1;
	} else {
		var justfive = document.orderform.bzip.value.slice(0,5);
		document.orderform.bzip.value = justfive;
	}

	if (badstate==0 && badzip==0) {
		var stateRange = "00215NH00544NY00795PR00851VI00988PR02791MA02940RI03897NH04992ME05495VT05544MA05907VT06389CT06390NY06928CT08989NJ09899AE14925NY19640PA19980DE20099DC20199VA20599DC21930MD24658VA26886WV28909NC29945SC31999GA33994FL34099AA34997FL36925AL38589TN39776MS39901GA42788KY45999OH47997IN49971MI52809IA54990WI56763MN57799SD58856ND59937MT62999IL65899MO67954KS69367NE71497LA72959AR73199OK73344TX74966OK79999TX81658CO83128WY83422ID83422WY83888ID84791UT86556AZ88441NM88595TX89883NV96162CA96698AP96797HI96799AS96898HI96932GU96940PW96944FM96952MP96970MH97920OR99403WA99950AK";
		var zip = document.orderform.bzip.value;
		var state = "";
		if (zip.length == 5) {
			for (var i = 0; i < stateRange.length; i += 7) {
				if (zip <= 1 * stateRange.substr(i, 5)) {
					var state = stateRange.substr(i + 5, 2);
					i = stateRange.length;
				}
			}
		} else {
			var state = "";
		}

		if (state == document.orderform.bstate.value) {
			//all ok
		} else {
			var e = e + 'State and zip code do not match.\n';
		}

	}





	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if (!(document.orderform.email.value.match(emailExp))) {
		var e = e + 'Email address is required, or we cannot contact you if there are any questions.\n';
	}

	if (!(document.orderform.cctype.value == 'V' || document.orderform.cctype.value == 'A' || document.orderform.cctype.value == 'M' || document.orderform.cctype.value == 'D')) {
		var e = e + 'Credit Card Type must be specified.\n';
	}

	var ctype = "";
	if (document.orderform.cctype.value == 'V') { var ctype='Visa' }
	if (document.orderform.cctype.value == 'M') { var ctype='Mastercard' }
	if (document.orderform.cctype.value == 'A') { var ctype='American Express' }
	if (document.orderform.cctype.value == 'D') { var ctype='Discover' }



	if (document.orderform.cardnumber.value == "") {

		var e = e + 'Credit Card Number is required.\n';

	} else {

		if (!(ctype == "")) {

			var ccNumb = document.orderform.cardnumber.value;
			ccNumb = ccNumb.split("-").join("");
			ccNumb = ccNumb.replace(/ /,"");
			ccNumb = ccNumb.replace(/ /,"");
			ccNumb = ccNumb.replace(/ /,"");
			ccNumb = ccNumb.replace(/ /,"");
			ccNumb = ccNumb.replace(/ /,"");
			ccNumb = ccNumb.replace(/ /,"");
			ccNumb = ccNumb.replace(/ /,"");
			ccNumb = ccNumb.replace(/ /,"");
			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

			document.orderform.cardnumber.value = sCCN;

			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) {  
		
						// check to see if the sum Mod 10 is zero
						bResult = true;  // This IS (or could be) a valid credit card number.
		
					} else {
		
						bResult = false;  // This could NOT be a valid credit card number
		
					}
		
				}
		
			}
		
			if(bResult) {

				// card number is a potentially valid card, but now (after the fact), check
				// card TYPE is correct

				if (ctype=="Visa") {
					if (ccNumb.slice(0,1) == "4") {
						// it's a valid visa
					} else {
						bResult = false;
					}
					if (!(ccNumb.length==16)) {
						bResult = false;
					}
				}

				if (ctype=="Mastercard") {
					if (ccNumb.slice(0,2) == "51" || ccNumb.slice(0,2) == "52" || ccNumb.slice(0,2) == "53" || ccNumb.slice(0,2) == "54" || ccNumb.slice(0,2) == "55") {
						// it's a valid mastercard
					} else {
						bResult = false;
					}
					if (!(ccNumb.length==16)) {
						bResult = false;
					}
				}

				if (ctype=="American Express") {
					if (ccNumb.slice(0,1) == "3") {
						// valid amex
					} else {
						bResult = false;
					}
					if (!(ccNumb.length==15)) {
						bResult = false;
					}
				}
					
				if (ctype=="Discover") {
					if (ccNumb.slice(0,4) == "6011") {
						// valid Discover
					} else {
						bResult = false;
					}
					if (!(ccNumb.length==16)) {
						bResult = false;
					}
				}


			}
		
			if(!bResult){
				var e = e + 'Card number is not a valid ' + ctype + ' card number\n';
			}

		}	
	
	}

	if (document.orderform.expmonth.value=="" || document.orderform.expyear.value == "") {
		var e = e + 'Credit Card Expiration is required.\n';
	}

	if (e.length > 0) {

		alert('The following errors need to be corrected:\n\n' + e);
		return false;
	
	} else {
	
		if (submitcount == 0) {
	
			submitcount++;
			return true;

		} else {
	
			//alert('This form has already been submitted, no need to click again.  Thank you!');
			document.orderform.submit.disabled=true;
			return false;
	
		}
	}

}




// --> End
