function handleSubmit(b,f) {
	b.value="Processing..."
	b.disabled=true;
	if(!valid8(f)){
		b.value="Submit";
		b.disabled=false;
		return false;
	}
	else {
		f.submit();
		return true;
	}
}

function valid8(f){
	fname = f.billing_first_name.value;
	lname = f.billing_last_name.value;
	cname = f.billing_company.value;
	address = f.billing_address.value;
	city = f.billing_city.value;
	state = f.billing_state.value; // selected
	zip = f.billing_zip.value;
	phone = f.phone.value;
	promo = f.promo_code.value;
	email = f.email_address.value;
	cc_type = f.credit_card_type.value; // selected
	cc_num = f.credit_card_number.value.replace(/ /g,""); // strip out spaces
	exp_mo = f.expiration_month.value; // selected
	exp_yr = f.expiration_year.value; // selected
	cc_code = f.security_code.value;
	
	var msg = '';

	if(promo.length != 0) {
		if(promo == "PARK2008Z" || promo == "TRAILS2008Q" || promo == "CW2008" || promo == "SELLIT2009" || promo == "PAPER_AD") {}
		else {
			msg += "Invalid promotional code. Note that promo codes use all capital letters. \n";
		}			
	}
	if(email.length == 0) {
		msg += "E-mail address is required.\n";
	}
	if(!isValidEmail(email)){
		msg += "E-mail address is invalid.\n";
	}
	if(cc_type.length == 0) {
		msg += "Credit Card Type is required.\n";
	}
	if(cc_num.length == 0) {
		msg += "Credit Card Number is required.\n";
	}
	if(!isValidCard(cc_num, cc_type)){
		msg += "Credit Card Number is invalid.\n";
	}
	if(exp_mo.length == 0) {
		msg += "Expiration Month is required.\n";
	}
	if(exp_yr.length == 0) {
		msg += "Expiration Year is required.\n";
	}
	if(!isValidExpDate(exp_mo, exp_yr)){
		msg += "Credit Card has expired.\n";
	}
	if(cc_code.length == 0) {
		msg += "Security code is required.\n";
	}
	if(!isValidCode(cc_code, cc_type)){
		msg += "Security code is not valid.\n";
	}
	if(fname.length == 0) {
		msg += "First Name is required.\n";
	}
	if(lname.length == 0) {
		msg += "Last Name is required.\n";
	}
	if(address.length == 0) {
		msg += "Billing Address is required.\n";
	}
	if(city.length == 0) {
		msg += "City is required.\n";
	}
	if(state.length == 0) {
		msg += "State is required.\n";
	}
	if(zip.length == 0) {
		msg += "Zip is required.\n";
	}
	if(phone.length == 0) {
		msg += "Phone is required.\n";
	}

	if(msg.length > 0){
		alert(msg); 
		return false;
	}
	else {
		return true;
	}	
}

function isValidCard(cardNumber, cardType){
  var isValid = false;
  var ccCheckRegExp = /[^\d ]/;
  isValid = !ccCheckRegExp.test(cardNumber);

  if (isValid)
  {
    var cardNumbersOnly = cardNumber.replace(/ /g,"");
    var cardNumberLength = cardNumbersOnly.length;
    var lengthIsValid = false;
    var prefixIsValid = false;
    var prefixRegExp;

    switch(cardType)
    {
      case "MC":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^5[1-5]/;
        break;

      case "VISA":
        lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
        prefixRegExp = /^4/;
        break;

      case "AMEX":
        lengthIsValid = (cardNumberLength == 15);
        prefixRegExp = /^3(4|7)/;
        break;

      default:
        lengthIsValid = false;
        prefixRegExp = /^$/;
    }

    prefixIsValid = prefixRegExp.test(cardNumbersOnly);
    isValid = prefixIsValid && lengthIsValid;
  }

  if (isValid)
  {
    var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;

    for (digitCounter = cardNumberLength - 1; 
      digitCounter >= 0; 
      digitCounter--)
    {
      checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
      for (var productDigitCounter = 0;
        productDigitCounter < numberProduct.length; 
        productDigitCounter++)
      {
        checkSumTotal += 
          parseInt(numberProduct.charAt(productDigitCounter));
      }
    }

    isValid = (checkSumTotal % 10 == 0);
  }

  return isValid;
}

function isValidExpDate(month, year) {
	var now = new Date();							
  if (now.getFullYear() > year || (now.getFullYear() == year && now.getMonth() + 1 > month)){
    return false;
  }
  else {
    return true;								
  }
}

function isValidCode(cardCode, cardType){
	var ccCheckRegExp = /[^\d ]/;
  isValid = !ccCheckRegExp.test(cardCode);
	if(isValid) {
		codeLength = cardCode.length;
		switch(cardType)
    {
      case "MC":
        lengthIsValid = (codeLength == 3);
        break;

      case "VISA":
        lengthIsValid = (codeLength == 3);
        break;

      case "AMEX":
        lengthIsValid = (codeLength == 4);
        break;

      default:
        lengthIsValid = false;
    }
    
    isValid = lengthIsValid;
  }
  return isValid;
}

function isValidEmail(str){
	var check = /^.+\@.+\..+$/;
	var OK = check.exec(str);
	if(!OK){
		return false;
	}
	else {
		return true;
	}
}
