/**********
COPY BILLING TO SHIPPING FUNCTION
***********/
function InitSaveVariables(form) {
	ShipFirst = form.shipFName.value;
	ShipLast = form.shipLName.value;
	ShipAddress1 = form.shipAddress1.value;
	ShipAddress2 = form.shipAddress2.value;
	ShipCity = form.shipCity.value;
	ShipStateIndex = form.shipState.selectedIndex;
	ShipState = form.shipState[ShipStateIndex].value;
	ShipZip = form.shipZip.value;
	ShipEmail = form.shipEmail.value;
	ShipPhone = form.shipPhone.value;
}

function copyBilling(form) {
	if (form.copybilling.checked) {
		InitSaveVariables(form);
		form.shipFName.value = form.billFName.value;
		form.shipLName.value = form.billLName.value;
		form.shipAddress1.value = form.billAddress1.value;
		form.shipAddress2.value = form.billAddress2.value;
		form.shipCity.value = form.billCity.value;
		form.shipZip.value = form.billZip.value;
		form.shipState.selectedIndex = form.billState.selectedIndex;
		form.shipEmail.value = form.email.value;
		form.shipPhone.value = form.phone.value;
	}
	else {
		form.shipFName.value = ShipFirst;
		form.shipLName.value = ShipLast;
		form.shipAddress1.value = ShipAddress1;
		form.shipAddress2.value = ShipAddress2;
		form.shipCity.value = ShipCity;
		form.shipZip.value = ShipZip;       
		form.shipState.selectedIndex = ShipStateIndex;
		form.shipEmail.value = ShipEmail;
		form.shipPhone.value = ShipPhone;
	}
}
/**********
END COPY BILLING TO SHIPPING FUNCTION
***********/

/**********
VALIDATE COMMENTS SUBMISSION
***********/
function validateComments(commentsForm){
	var errMsg = "Please correct the following errors:\n";
	var validateOk = true;
	
	//Check Name
	if(commentsForm.subject.value == ""){
		errMsg = errMsg + "- Missing: Name\n";
		commentsForm.subject.style.backgroundColor='FF6C6C';
		validateOk = false;
	}else{
		commentsForm.subject.style.backgroundColor='white';
	}
	
	//Check Email
	if(commentsForm.email.value == ""){
		errMsg = errMsg + "- Missing: Email Address\n";
		commentsForm.email.style.backgroundColor='FF6C6C';
		validateOk = false;
	}else if(!isEmailValid(commentsForm.email.value)){
		errMsg = errMsg + "- Invalid: Email Address\n";
		commentsForm.email.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		commentsForm.email.style.backgroundColor='white';
	}
	
	//Check Comments
	if(commentsForm.message_body.value == ""){
		errMsg = errMsg + "- Missing: Comments\n";
		commentsForm.message_body.style.backgroundColor='FF6C6C';
		validateOk = false;
	}else{
		commentsForm.message_body.style.backgroundColor='white';
	}
	
	if (!validateOk){
		alert(errMsg);
	} else{
		alert("Thank you for your comments! If this is an inquiry, we will respond to you shortly.");
	}
	
	return validateOk;
}
/**********
END VALIDATE COMMENTS SUBMISSION
***********/

/**********
Validate Customer Billing and Shipping Form
***********/
function validateForm(customerForm){
	
	var errMsg = "Please correct the following errors:\n";
	var validateOk = true;
	
	//Check Billing First Name
	if(customerForm.billFName.value == ""){
		errMsg = errMsg + "- Missing: Billing First Name\n";
		customerForm.billFName.style.backgroundColor='FF6C6C';
		validateOk = false;
	}else{
		customerForm.billFName.style.backgroundColor='white';
	}
	
	//Check Billing Last Name
	if(customerForm.billLName.value == ""){
		errMsg = errMsg + "- Missing: Billing Last Name\n";
		customerForm.billLName.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.billLName.style.backgroundColor='white';
	}
	
	//Check Billing Address Line 1
	if(customerForm.billAddress1.value == ""){
		errMsg = errMsg + "- Missing: Billing Address\n";
		customerForm.billAddress1.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.billAddress1.style.backgroundColor='white';
	}
	
	//Check Billing City
	if(customerForm.billCity.value == ""){
		errMsg = errMsg + "- Missing: Billing City\n";
		customerForm.billCity.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.billCity.style.backgroundColor='white';
	}
	
	//Check Billing State
	if(customerForm.billState.value == ""){
		errMsg = errMsg + "- Missing: Billing State\n";
		customerForm.billState.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.billState.style.backgroundColor='white';
	}
	
	//Check Billing Zip
	if(customerForm.billZip.value == ""){
		errMsg = errMsg + "- Missing: Billing Zip Code\n";
		customerForm.billZip.style.backgroundColor='FF6C6C';
		returnValue = false;
	}else if(!isZipValid(customerForm.billZip.value)){
		errMsg = errMsg + "- Invalid: Billing Zip Code\n";
		customerForm.billZip.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.billZip.style.backgroundColor='white';
	}
	
	//Check Email
	if(customerForm.email.value == ""){
		errMsg = errMsg + "- Missing: Email Address\n";
		customerForm.email.style.backgroundColor='FF6C6C';
		validateOk = false;
	}else if(!isEmailValid(customerForm.email.value)){
		errMsg = errMsg + "- Invalid: Email Address\n";
		customerForm.email.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.email.style.backgroundColor='white';
	}
	
	//Check Phone Number
	if(customerForm.phone.value == ""){
		errMsg = errMsg + "- Missing: Phone Number\n";
		customerForm.phone.style.backgroundColor='FF6C6C';
		validateOk = false;
	}else if(!isPhoneValid(customerForm.phone.value)){
		errMsg = errMsg + "- Invalid: Phone Number\n";
		customerForm.phone.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.phone.style.backgroundColor='white';
	}
	
	//Check Shipping First Name
	if(customerForm.shipFName.value == ""){
		errMsg = errMsg + "- Missing: Shipping First Name\n";
		customerForm.shipFName.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.shipFName.style.backgroundColor='white';
	}
	
	//Check Shipping Last Name
	if(customerForm.shipLName.value == ""){
		errMsg = errMsg + "- Missing: Shipping Last Name\n";
		customerForm.shipLName.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.shipLName.style.backgroundColor='white';
	}
	
	//Check Shipping Address Line 1
	if(customerForm.shipAddress1.value == ""){
		errMsg = errMsg + "- Missing: Shipping Address\n";
		customerForm.shipAddress1.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.shipAddress1.style.backgroundColor='white';
	}
	
	//Check Shipping City
	if(customerForm.shipCity.value == ""){
		errMsg = errMsg + "- Missing: Shipping City\n";
		customerForm.shipCity.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.shipCity.style.backgroundColor='white';
	}
	
	//Check Shipping State
	if(customerForm.shipState.value == ""){
		errMsg = errMsg + "- Missing: Shipping State\n";
		customerForm.shipState.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.shipState.style.backgroundColor='white';
	}
	
	//Check Shipping Zip
	if(customerForm.shipZip.value == ""){
		errMsg = errMsg + "- Missing: Shipping Zip Code\n";
		customerForm.shipZip.style.backgroundColor='FF6C6C';
		validateOk = false;
	}else if(!isZipValid(customerForm.shipZip.value)){
		errMsg = errMsg + "- Invalid: Shipping Zip Code\n";
		customerForm.shipZip.style.backgroundColor='FF6C6C';
		validateOk = false;
	}
	else{
		customerForm.shipZip.style.backgroundColor='white';
	}
	
	//Check Shipping Email
	if(customerForm.shipEmail.value != ""){
		if(!isEmailValid(customerForm.shipEmail.value)){
			errMsg = errMsg + "- Invalid: Shipping Email Address\n";
			customerForm.shipEmail.style.backgroundColor='FF6C6C';
			validateOk = false;
		} else {
			customerForm.shipEmail.style.backgroundColor='white';
		}
	} else{
		customerForm.shipEmail.style.backgroundColor='white';
	}
	
	//Check Shipping Phone Number
	if(customerForm.shipPhone.value != ""){
		if(!isPhoneValid(customerForm.shipPhone.value)){
			errMsg = errMsg + "- Invalid: Shipping Phone Number\n";
			customerForm.shipPhone.style.backgroundColor='FF6C6C';
			validateOk = false;
		} else {
			customerForm.shipPhone.style.backgroundColor='white';
		}				 
	} else{
		customerForm.shipPhone.style.backgroundColor='white';
	}
	
	//Check Credit Card Number
	if(customerForm.payMethod.value == ""){
		errMsg = errMsg + "- Missing: Payment Method\n";
		customerForm.payMethod.style.backgroundColor='FF6C6C';
		validateOk = false;
	}else{
		customerForm.payMethod.style.backgroundColor='white';
	}
	
	if(customerForm.payMethod.value != "Paypal"){
		if(customerForm.cardNumber.value == ""){
			errMsg = errMsg + "- Missing: Credit Card Number\n";
			customerForm.payMethod.style.backgroundColor='white';
			customerForm.cardNumber.style.backgroundColor='FF6C6C';
			validateOk = false;
		}
		else if (!isCCValid(customerForm.payMethod.value, customerForm.cardNumber.value)){
			errMsg = errMsg + "- Invalid: Credit Card Type & Number Combination\n";
			customerForm.payMethod.style.backgroundColor='FF6C6C';
			customerForm.cardNumber.style.backgroundColor='FF6C6C';
			validateOk = false;
		}
		else{
			customerForm.payMethod.style.backgroundColor='white';
			customerForm.cardNumber.style.backgroundColor='white';
		}
	
		//Check Expiration Month
		if(customerForm.cardMonth.value == ""){
			errMsg = errMsg + "- Missing: Credit Card Expiration Month\n";
			customerForm.cardMonth.style.backgroundColor='FF6C6C';
			validateOk = false;
		}
		else{
			customerForm.cardMonth.style.backgroundColor='white';
		}
		
		//Check Expiration Year
		if(customerForm.cardYear.value == ""){
			errMsg = errMsg + "- Missing: Credit Card Expiration Year\n";
			customerForm.cardYear.style.backgroundColor='FF6C6C';
			validateOk = false;
		}
		else{
			customerForm.cardYear.style.backgroundColor='white';
		}
		
		//Check CCV
		if(customerForm.ccv.value == ""){
			errMsg = errMsg + "- Missing: Credit Card CCV (Security) Code\n";
			customerForm.ccv.style.backgroundColor='FF6C6C';
			validateOk = false;
		} else if (!isCVVValid(customerForm.ccv.value)){
			errMsg = errMsg + "- Invalid: Credit Card CCV (Security) Code\n";
			customerForm.ccv.style.backgroundColor='FF6C6C';
			validateOk = false;
		}
		else{
			customerForm.ccv.style.backgroundColor='white';
		}
	}
	
	if (!validateOk){
		alert(errMsg);
	}
	
	return validateOk;
}
/**********
END Validate Customer Billing and Shipping Form
***********/

/**********
Commom Validation Functions: Zip, Email, Phone Number, CVV Credit Card and CCV Code
***********/
//Validate Zip
function isZipValid(zip) {
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
     return reZip.test(zip);
}

//Validate Email
function isEmailValid(email){      
   reEmail = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
   return reEmail.test(email); 
 }

//Validate Phone Number
function isPhoneValid(phone){      
   rePhone = new RegExp(/^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/);
   return rePhone.test(phone); 
}

//Validate CCV Code
function isCVVValid(cvv){
	reCVV = new RegExp(/(^\d{3,4}$)/);
    return reCVV.test(cvv);
}

//Validate Credit Card Number
function isCCValid(cctype, ccnum) {
	switch (cctype){
		case ("Visa"):
			reCC = new RegExp(/^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/); break;
		case ("MasterCard"):
			reCC = new RegExp(/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/); break;
		case ("Discover"):
			reCC = new RegExp(/^6011-?\d{4}-?\d{4}-?\d{4}$/); break;
		case ("Amex"):
			reCC = new RegExp(/^3[4,7]\d{13}$/); break;
		default: return false; break;
	}
  
  /*if (cctype == "visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var reCC = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (cctype == "mc") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var reCC = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (cctype == "disco") {
      // Discover: length 16, prefix 6011, dashes optional.
      var reCC = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (cctype == "amex") {
      // American Express: length 15, prefix 34 or 37.
      var reCC = /^3[4,7]\d{13}$/;
   } else if (cctype == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var reCC = /^3[0,6,8]\d{12}$/;
   }*/
   
   if (!reCC.test(ccnum)) return false;
   
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}
/**********
END Commom Validation Functions: Zip, Email, Phone, CVV Credit Card and CCV Code
***********/
