﻿
				function clearError(field) {
					error = document.getElementById(field);
					if ((error.value.length)>0 && (error.style.backgroundColor == "red")) {
						error.value = '';
					}
					if (error.style.backgroundColor == "red") {
						error.style.backgroundColor = "white";
					}
					if (error == 'pwd2Error') {
						error.innerHTML = '';
					}
					var f = document.getElementsByTagName('input');
					for(var i=0;i<f.length;i++){
						if(f[i].getAttribute('type')=='button'){
							f[i].removeAttribute('disabled');
						}
					}

				}
				function checkPassword() {
					var pwd1 = document.getElementById("Password1").value;
					var pwd2 = document.getElementById("Password2");
					var pwdErr = document.getElementById("pwd2Error");

					if (pwd1 != pwd2.value) {
						pwdErr.innerHTML = 'Passwords do not match!';
						pwdErr.style.backgroundColor="beige";
						pwd2.value = '';
						pwd2.value = '';
					} else {
						pwdErr.innerHTML = '';
					}
				}


				function noPOBox(checkThis) {
						checkThis = checkThis.toUpperCase();
						var shipaddress = document.getElementById('ShipAddress1');
					if (checkThis.charAt(0) == 'P') {
						shipaddress.value = '';
						alert("We cannot ship to PO boxes");
					}
					if (checkThis.charAt(0) == 'C') {
						shipaddress.value = '';
						alert("Please place C/o information in Address 2.");
					}
				}

				function enquiryForm() {
					var success = true;
					var contact = document.getElementById("name");
					var email = document.getElementById("email");

					if (contact.value.length == 0) {
						success = false;
						contact.style.backgroundColor = "red";
					}

   					if ((email.value.indexOf(".") < 0) || (email.value.indexOf("@") < 0)) {
	   					success = false;
	   					email.style.backgroundColor = "red";
					}

					return success;
				}

				function sameAddress(val) {
					if (val.checked == true) {
					document.getElementById('ShipFirstName').value = document.getElementById('BillFirstName').value;
					document.getElementById('ShipLastName').value = document.getElementById('BillLastName').value;
					document.getElementById('ShipAddress1').value = document.getElementById('BillAddress1').value;
					document.getElementById('ShipAddress2').value = document.getElementById('BillAddress2').value;
					document.getElementById('ShipCity').value = document.getElementById('BillCity').value;
					document.getElementById('ShipState').value = document.getElementById('BillState').value;
					document.getElementById('ShipState').selectedIndex = document.getElementById('BillState').selectedIndex;
					document.getElementById('ShipZip').value = document.getElementById('BillZip').value;
					} else {
					document.getElementById('ShipFirstName').value = '';
					document.getElementById('ShipLastName').value = '';
					document.getElementById('ShipAddress1').value = '';
					document.getElementById('ShipAddress2').value = '';
					document.getElementById('ShipCity').value = '';
					document.getElementById('ShipState').value = '';
					document.getElementById('ShipState').selectedIndex = 0;
					document.getElementById('ShipZip').value = '';
					}
				}
				function validateCoupon(coupon) {
				    var error = "";
				    error = mod10(coupon.value);
				    var stripped = coupon.value.replace(/[\(\)\.\-\ ]/g, '');

				   if (coupon.value == "") {
				        error = "You didn't enter a Coupon number.\n";
				        coupon.style.background = 'Yellow';
				    } else if (isNaN(parseInt(stripped))) {
				        error = "The Coupon number contains illegal characters.\n";
				        coupon.style.background = 'Yellow';
				    } else if (!(stripped.length == 16)) {
				        error = "Invalid Coupon Number";
				        coupon.style.background = 'Yellow';
				    }
				    couponError = document.getElementById("couponError");
				    couponError.innerHTML = error;
				}
				function validateCredit(credit) {
				    var error = "";
				    error = mod10(credit.value);
				    var stripped = credit.value.replace(/[\(\)\.\-\ ]/g, '');

				   if (credit.value == "") {
				        error = "You didn't enter a credit card number.\n";
				        credit.style.background = 'Yellow';
				    } else if (isNaN(parseInt(stripped))) {
				        error = "The credit card number contains illegal characters.\n";
				        credit.style.background = 'Yellow';
				    } else if (!(stripped.length == 16)) {
				        error = "Invalid credit card number";
				        credit.style.background = 'Yellow';
				    }
				    creditError = document.getElementById("creditError");
				    creditError.innerHTML = error;
				}
				function isValidEmail(str) {
						var email = document.getElementById("emailError");
   					if ((str.value.indexOf(".") < 0) || (str.value.indexOf("@") < 0)) {
	   					email.innerHTML = 'Invalid email address<br/>';
					} else {
						email.innerHTML = '';
					}
 				}


				function numbersOnly(field) {
				var stripped = field.value.replace(/[^0-9]/g, '');
					field.value = stripped;
				}
				function formatPhone(field) {
					var stripped = field.value.replace(/[^0-9\-]/g, '');
					field.value = stripped;
				}

				/*function mod10( cardNumber )
				{
				           var clen = new Array( cardNumber.length );
				           var n = 0,sum = 0;
				           for( n = 0; n < cardNumber.length; ++n )
				          {
				                      clen [n] = parseInt ( cardNumber.charAt(n) );
				          }
				          for( n = clen.length -2; n >= 0; n-=2 )
				                      clen [n] *= 2;
				          if( clen [n] > 9 )
				                      clen [n]-=9;

				for( n = 0; n < clen.length; ++n )
				{
				          sum += clen [n];
				}
				 if ((sum%10) > 1) {
					 error = "Invalid Number";
				    //coupon.style.background = 'red';
				    return error;
				    //couponError = document.getElementById("couponError");
				    //couponError.innerHTML = error;
				 } else {
					 return '';
				 }
				}*/
				function mod10(ccField) { // v2.0
					ccNumb = ccField.value;
					if (ccNumb == '4222222222222') return;
					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){ // 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
					}
					}
					}
					// change alert to on-page display or other indication as needed.

					if(!bResult){
						ccField.style.backgroundColor="red";
					}
					//return bResult; // Return the results
					}
					// -->


				function cartOption(Page,button) {
					button.disabled=true;
					document.forms[1].action = document.forms[1].action + '=' + Page + '&';

					if ((Page == 'checkout')) {
						if (checkPayment()) {
						document.forms[1].submit();
						} else {
						return false;
						}
					}

					if ((Page == 'payment') && (checkForm())) {
						document.forms[1].submit();
					}
					if (Page != 'payment') {
						document.forms[1].submit();
					}
				}

				function checkForm() {
					var BillFirstName = document.getElementById('BillFirstName');
					var BillLastName = document.getElementById('BillLastName');
					var BillPhone = document.getElementById('BillPhone');
					var email = document.getElementById('BillEmail');
					//var Password1 = document.getElementById('Password1');
					//var Password2 = document.getElementById('Password2');
					var BillAddress1 = document.getElementById('BillAddress1');
					var BillCity = document.getElementById('BillCity');
					var BillState = document.getElementById('BillState');
					var BillZip = document.getElementById('BillZip');
					var ShipFirstName = document.getElementById('ShipFirstName');
					var ShipLastName = document.getElementById('ShipLastName');
					var ShipAddress1 = document.getElementById('ShipAddress1');
					var ShipCity = document.getElementById('ShipCity');
					var ShipState = document.getElementById('ShipState');
					var ShipZip = document.getElementById('ShipZip');
					var Status = true;

					var stripped = BillPhone.value.replace(/[^0-9]/g, '');

					BillPhone.value = stripped;

					if (BillFirstName.value.length == 0) {
						BillFirstName.style.backgroundColor = "red";
						Status = false;
					}
					if (BillLastName.value.length == 0) {
						BillLastName.style.backgroundColor = "red";
						Status = false;
					}
					if (BillPhone.value.length == 0) {
						BillPhone.style.backgroundColor = "red";
						Status = false;
						alert('Enter 999-999-9999 if you do not want to provide your phone number.  NOTE: If there is an issue with your order and we cannot promptly reach you by email we may cancel your order.');
					}
					if (email.value.length == 0) {
						email.style.backgroundColor = "red";
						Status = false;
					}
					//if (Password1.value.length == 0) {
					//	Password1.style.backgroundColor = "red";
					//	Status = false;
					//}
					//if (Password2.value.length == 0) {
					//	Password2.style.backgroundColor = "red";
					//	Status = false;
					//}
					if (BillAddress1.value.length == 0) {
						BillAddress1.style.backgroundColor = "red";
						Status = false;
					}
					if (BillCity.value.length == 0) {
						BillCity.style.backgroundColor = "red";
						Status = false;
					}
					if (BillState.value.length == 0) {
						BillState.style.backgroundColor = "red";
						Status = false;
					}
					if (BillCity.value.length == 0) {
						BillCity.style.backgroundColor = "red";
						Status = false;
					}
					if (BillZip.value.length < 5) {
						BillZip.style.backgroundColor = "red";
						Status = false;
					}
					if (ShipFirstName.value.length == 0) {
						ShipFirstName.style.backgroundColor = "red";
						Status = false;
					}
					if (ShipLastName.value.length == 0) {
						ShipLastName.style.backgroundColor = "red";
						Status = false;
					}
					if (ShipAddress1.value.length == 0) {
						ShipAddress1.style.backgroundColor = "red";
						Status = false;
					}
						noPOBox(ShipAddress1.value);
					if (ShipCity.value.length == 0) {
						ShipCity.style.backgroundColor = "red";
						Status = false;
					}
					if (ShipState.value.length == 0) {
						ShipState.style.backgroundColor = "red";
						Status = false;
					}
					if (ShipZip.value.length < 5) {
						ShipZip.style.backgroundColor = "red";
						Status = false;
					}

					if ((BillState.value == "OH") && (document.getElementById("countyList").selectedIndex == 0)) {
						document.getElementById("CountyError").value = "County required";
					}
					if (Status == false) {
						alert('Required fields missing.  Please review your information.');
						return false;
					} else {
						return true;
					}
				}

				function checkPayment() {
					var error = false;
					var ccNumber = document.getElementById("ccNumber");
					var ccExpMonth = document.getElementById("ccExpMonth");
					var ccExpYear = document.getElementById("ccExpYear");
					var ccCVC = document.getElementById("ccCVC");
					var phone = document.getElementById("BillPhone");

						//= document.getElementById("ccExpMonth");
					if (ccNumber.value.length < 13) {
						ccNumber.style.backgroundColor = "red";
						error = true;
					}

					if (ccExpMonth.value.length == 0) {
						ccExpMonth.style.backgroundColor = "red";
						error = true;
					}
					if (ccExpYear.value.length == 0) {
						ccExpYear.style.backgroundColor = "red";
						error = true;
					}
					if (ccCVC.value.length == 0) {
						ccCVC.style.backgroundColor = "red";
						error = true;
					}

					if (document.forms[1].couponNum1) {
						var couponNum1 = document.getElementById("couponNum1");
						var couponExpMonth1 = document.getElementById("couponExpMonth1");
						var couponExpYear1 = document.getElementById("couponExpYear1");
						var couponCVC1 = document.getElementById("couponCVC1");

						if (couponNum1.value.length < 16) {
							couponNum1.style.backgroundColor = "red";
							error = true;
						}

						if (couponExpMonth1.value.length == 0) {
							couponExpMonth1.style.backgroundColor = "red";
							error = true;
						}
						if (couponExpYear1.value.length == 0) {
							couponExpYear1.style.backgroundColor = "red";
							error = true;
						}
						if (couponCVC1.value.length == 0) {
							couponCVC1.style.backgroundColor = "red";
							error = true;
						}
					}
					if (document.forms[1].couponNum2) {
						var couponNum2 = document.getElementById("couponNum2");
						var couponExpMonth2 = document.getElementById("couponExpMonth2");
						var couponExpYear2 = document.getElementById("couponExpYear2");
						var couponCVC2 = document.getElementById("couponCVC2");

						if (couponNum2.value.length < 16) {
							couponNum2.style.backgroundColor = "red";
							error = true;
						}

						if (couponExpMonth2.value.length == 0) {
							couponExpMonth2.style.backgroundColor = "red";
							error = true;
						}
						if (couponExpYear2.value.length == 0) {
							couponExpYear2.style.backgroundColor = "red";
							error = true;
						}
						if (couponCVC2.value.length == 0) {
							couponCVC2.style.backgroundColor = "red";
							error = true;
						}

					}

					if (error == true) {
						return false;
					} else {
						return true;
					}
				}


				function checkLength() {
					password = document.getElementById("Password1");
					if (password.value.length < 8) {
						password.style.backgroundColor="red";
					}
				}

				function showCounties(state) {
					if (state.value == "OH") {
						document.getElementById("county").style.visibility="visible";
					}
				}

				function noCoupon(val) {
						var couponDiscount = document.getElementById('discount').innerHTML * 1;
						var afterDiscount = document.getElementById('afterDiscount').innerHTML * 1;
						var total = document.getElementById('total').innerHTML * 1;
					if (val.checked == true) {
						var adjustedDiscount = Math.round((couponDiscount - 40)*100)/100;
						var adjustedAfterDiscount = Math.round((afterDiscount + 40)*100)/100;
						var adjustedTotal = Math.round((total + 40)*100)/100;
						document.getElementById('discount').innerHTML = adjustedDiscount;
						document.getElementById('afterDiscount').innerHTML = (adjustedAfterDiscount.toFixed(2));
						document.getElementById('total').innerHTML = (adjustedTotal.toFixed(2));
					} else {
						var adjustedDiscount = Math.round((couponDiscount + 40)*100)/100;
						var adjustedAfterDiscount = Math.round((afterDiscount - 40)*100)/100;
						var adjustedTotal = Math.round((total - 40)*100)/100;
						document.getElementById('discount').innerHTML = adjustedDiscount;
						document.getElementById('afterDiscount').innerHTML = (adjustedAfterDiscount.toFixed(2));
						document.getElementById('total').innerHTML = (adjustedTotal.toFixed(2));
					}
				}
