
// *******************************

function setShippingLocation(shipping_same_as_billing) {
	if (shipping_same_as_billing.checked) {
		Form.shipto_name.value = Form.billto_name.value;
		Form.shipto_address1.value = Form.billto_address1.value;
		Form.shipto_address2.value = Form.billto_address2.value;
		Form.shipto_city.value = Form.billto_city.value;
		Form.shipto_state.selectedIndex = Form.billto_state.selectedIndex;
		Form.shipto_zip.value = Form.billto_zip.value;
		Form.shipto_country.value = Form.billto_country.value;
		Form.shipto_phone.value = Form.billto_phone.value;
		Form.shipto_fax.value = Form.billto_fax.value;
	} 
	else {
		Form.shipto_name.value = '';
		Form.shipto_address1.value = '';		
		Form.shipto_address2.value = '';
		Form.shipto_city.value = '';
		Form.shipto_state.selectedIndex = -1;
		Form.shipto_zip.value = '';
		Form.shipto_country.value = '';
		Form.shipto_phone.value = '';
		Form.shipto_fax.value = '';
	}
}

// *******************************

function validateForm(objForm) {
// Set all variables to true, so if the variable is not on the page being checked, it will not set off an error.
	var valid_name = true;
	var valid_card_type = true;
	var valid_card_number = true;
	var valid_name_on_card = true;
	var valid_card_expiration_month = true;
	var valid_card_expiration_year = true;
	var valid_card_security_code = true;	
	var valid_email = true;
	var valid_password = true;
	var valid_confirm_password = true;	
	var agree_to_policies_checked = true;
	var valid_options1 = true;
	var valid_options2 = true;
	var valid_options3 = true;

// Check that object exists before calling function.
	if (objForm.name) {
  	valid_name = isNotBlank(objForm.name.value);
	}
	if (objForm.card_type) {
  	valid_card_type = isDropdownValid(objForm.card_type.value);
	}	
	if (objForm.card_number) {
  	valid_card_number = isCardNumValid(objForm.card_number.value);
	}
	if (objForm.name_on_card) {
  	valid_name_on_card = isNotBlank(objForm.name_on_card.value);
	}
	if (objForm.card_expiration_month) {
  	valid_card_expiration_month = isDropdownValid(objForm.card_expiration_month.value);
	}
	if (objForm.card_expiration_year) {
  	valid_card_expiration_year = isDropdownValid(objForm.card_expiration_year.value);
	}
	if (objForm.card_security_code) {
  	valid_card_security_code = isSecurityCodeValid(objForm.card_security_code.value);
	}
	if (objForm.email) {
  	valid_email = isEmailValid(objForm.email.value);
	}
	if (objForm.password) {
  	valid_password = isPasswordValid(objForm.password.value);
	}
	if (objForm.confirm_password) {
  	valid_confirm_password = isConfirmPasswordValid(objForm.confirm_password.value , objForm.password.value);
	}		
	if (objForm.agreeToPolicies) {
	agree_to_policies_checked = isAgreeToPoliciesChecked(objForm.agreeToPolicies);
	}
	if (objForm.options1) {
  	valid_options1 = isDropdownValid(objForm.options1.value);
	}
	if (objForm.options2) {
  	valid_options2 = isDropdownValid(objForm.options2.value);
	}
	if (objForm.options3) {
  	valid_options3 = isDropdownValid(objForm.options3.value);
	}
	
	if (valid_name && valid_card_type && valid_card_number && valid_name_on_card && valid_card_expiration_month && valid_card_expiration_year && valid_card_security_code && valid_email && valid_password && valid_confirm_password && agree_to_policies_checked && valid_options1 && valid_options2 && valid_options3 ) {return true}
	
	msg = "The form was not submited due to the following error(s).\n";
	
	upperLine = "\n_________________________________________________________\n\n";
	lowerLine = "_________________________________________________________\n";
	
	if (!valid_name) {
		msg += upperLine;
		msg += "Please enter a value for Name.\n";
		msg += lowerLine;
		objForm.name.focus();
	}
	if (!valid_card_type) {
		msg += upperLine;
		msg += "You must select a Credit Card Type.\n";
		msg += lowerLine;
	}		
	if (!valid_card_number) {
		msg += upperLine;
		msg += "The Credit Card Number is an invalid format.\n";
		msg += lowerLine;
	}
	if (!valid_name_on_card) {
		msg += upperLine;
		msg += "Please enter the Name of the Card Holder.\n";
		msg += lowerLine;
	}
	if (!valid_card_expiration_month) {
		msg += upperLine;
		msg += "You must select the Card Expiration Month.\n";
		msg += lowerLine;
	}	
	if (!valid_card_expiration_year) {
		msg += upperLine;
		msg += "You must select the Card Expiration Year.\n";
		msg += lowerLine;
	}
	if (!valid_card_security_code) {
		msg += upperLine;
		msg += "You must enter the three or four digit Security Code.\n";
		msg += lowerLine;
	}	
	if (!valid_email) {
		msg += upperLine;
		msg += "The email Address is an invalid format.\n";
		msg += lowerLine;
		objForm.email.focus();
	}
	if (!valid_password) {
		msg += upperLine;
		msg += "Password must be 4 to 10 characters long.\n";
		msg += lowerLine;
		objForm.password.focus();
	}
	if (!valid_confirm_password) {
		msg += upperLine;
		msg += "The Password and Confirm Password does not match!\n";
		msg += lowerLine;
		objForm.confirm_password.focus();
	}		
	if (!agree_to_policies_checked) {
		msg += upperLine;
		msg += "You must agree to the policies.\n Please check the box and resubmit.\n";
		msg += lowerLine;
	}
	if (!valid_options1) {
		msg += upperLine;
		msg += "You must select the Piston and ring size.\n";
		msg += lowerLine;
	}
	if (!valid_options2) {
		msg += upperLine;
		msg += "You must select the Rod bearing size.\n";
		msg += lowerLine;
	}
	if (!valid_options3) {
		msg += upperLine;
		msg += "You must select the Main bearing size.\n";
		msg += lowerLine;
	}
	

	alert(msg);
return false;	

}

// *******************************

function isCardNumValid(ccNumb) {  // v2.0
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 = true;}
}

// 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
    }
  }
}

  return bResult; // Return the results
}

// *******************************

function isSecurityCodeValid(code) { 
if (code == "")
{
bResult = false;  // This is an invalid code.
}
else
{
if ((code.length < 3) || (code.length > 4)){
    bResult = false;  // This is an invalid code.
  } else {
    bResult = true;  // This is a valid code.
    }
}
  return bResult; // Return the results
}

// *******************************

function isEmailValid(email) {  // 
if (email == "")
{
bResult = false;  // This IS an invalid email address.
}
else
{
checkemail = email;
if ((checkemail.indexOf('@') < 0) || ((checkemail.charAt(checkemail.length-4) != '.') && (checkemail.charAt(checkemail.length-3) != '.'))){
    bResult = false;  // This IS an invalid email address.
  } else {
    bResult = true;  // This IS (or could be) a valid email address.
    }
}
  return bResult; // Return the results
}

// *******************************

function isPasswordValid(password) { 
if (password == "")
{
bResult = false;  // This is an invalid password.
}
else
{
if ((password.length < 4) || (password.length > 10)){
    bResult = false;  // This is an invalid password.
  } else {
    bResult = true;  // This is a valid password.
    }
}
  return bResult; // Return the results
}

// *******************************

function isConfirmPasswordValid(confirm_password,password) {
if (confirm_password != password){
    bResult = false;  // This is an invalid password.
    } 
else {
    bResult = true;  // This is a valid password.
    }
return bResult; // Return the results
}

// *******************************

function isAgreeToPoliciesChecked(policies) {
if (policies.checked){
    bResult = true;  // Policies have been checked.
    }
else {
    bResult = false;  // Policies have NOT been checked.
	}
return bResult; // Return the results
}

// *******************************

function isNotBlank(field) { 
if (field != ""){
    bResult = true;  // field is not blank.
    }
else {
    bResult = false;  // field is blank.
	}
return bResult; // Return the results
}

// *******************************

function isDropdownValid(choice) {
if (choice == 0){
    bResult = false;  // choice was not selected.
    }
else {
    bResult = true;  // choice was not selected.
	}
return bResult; // Return the results
}

