// not valid email
emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/
// valid email
emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/
// valid phone
phoneReg = /^\(?(\d{3})\)?\-?[\.\-\/ ]?(\d{3})[\.\-\/ ]?(\d{4})$/
// valid amount
amountReg = /^\d{1,10}\.\d{2}$/

function check_form(payForm) {
  payForm.Submit.disabled = true;
  var error = 0;
  var error_message = "Errors have occured during the process of your form!\nPlease make the following corrections:\n\n";
  var payment_type = payForm.payment_type.value;
  var payment_value = null;
  if (payForm.payment_type.length) {
    for (var i = 0; i < payForm.payment_type.length; i++)
      if (payForm.payment_type[i].checked)
        payment_value = payForm.payment_type[i].value;
  } else if (payForm.payment_type.checked) {
    payment_value = payForm.payment_type.value;
  } else if (payForm.payment_type.value) {
    payment_value = payForm.payment_type.value;
  }
  var firstname = payForm.firstname.value;
  var lastname = payForm.lastname.value;
  var address = payForm.address.value;
  var city = payForm.city.value;
  var state = payForm['state'].value;
  var zip = payForm.zip.value;
  var email = payForm.email.value;
  var phone = payForm.phone.value;
  var cnp_security = payForm.cnp_security.value;

  var cc_number = payForm.cc_num.value;
  var amount = payForm.amount.value;

  if (firstname == "" || lastname == "" || address == "" || city == "" || state == "" || zip == "" || phone == "" || email == "") {
    error_message = error_message + "* Missing Required Address Information.\n";
        error = 1;

  }
  if (!state) {
  	error_message += "* Billing State Is Missing.\n";
	error = 1;
  }
  
  if (!(phoneReg.exec(phone))) {
	error_message = error_message + "* Invalid Phone Number.\n";
	error = 1;
  }
  

  if (!(!emailReg1.exec(email) && emailReg2.test(email))) {
	error_message = error_message + "* Invalid Email Address.\n";
	error = 1;
  }

  if (payment_value == "cc") {
	  // cycle through the form elements and trim all fields
	  var cc = '';
	  var cclen = 0;
	  var ccid = 0;
	  var myDate = new Date();
	  var thisMonth = myDate.getMonth();
	  var thisYear = myDate.getFullYear();
	  var year = payForm.year.options[payForm.year.selectedIndex].value;
	  var month = payForm.month.options[payForm.month.selectedIndex].value;
	  var cclen;
	  var ccid;
	  month = month.substr(0,2);
	  var str = payForm.cc_type.options[payForm.cc_type.selectedIndex].value;
	  str=str.slice(0,3);
	  str=str.toLowerCase();
	  if (payForm.cc_num.value) {
	    var cc = payForm.cc_num.value;
	    cc=cc.replace(/[^0-9]/g, "")
	    cclen=cc.length;
	    ccid=cc.charAt(0);
	  }
	  else {
	    error_message += "* Credit Card Number Is Missing.\n";
	    error = 1;
	  }
	  switch (str) {
	    case "vis":
	      if (ccid != 4) {
		  error_message += "* Credit Card Number " + cc + " Is Not A Valid Visa.\n";
		  error = 1;
	      }
	      if (cclen != 16) {
		  error_message += "* You Have " + cclen + " Digits In Your Credit Card.  16 Digits Are Required.\n";
		  error = 1;
	      }
	      var cnp_len = 3;
	      break;
	    case "mas":
	      if (ccid != 5) {
	        error_message += "* Credit Card Number " + cc + " Is Not A Valid MasterCard.\n";
		error = 1;
	      }
	      if (cclen != 16) {
	        error_message += "* You Have " + cclen + " Digits In Your Credit Card.  16 Digits Are Required.\n";
		error = 1;
	      }
	      var cnp_len = 3;
	      break;
	    case "dis":
	        if (ccid != 6) {
	          error_message += "* Credit Card Number " + cc + " Is Not A Valid Discover.\n";
		  error = 1;
		}
	  	if (cclen != 16) {
	    	  error_message += "* You Have " + cclen + " Digits In Your Credit Card.  16 Digits Are Required.\n";
	    	  error = 1;
	  	}
	        var cnp_len = 4;
 	  	break;
	    case "ame":
	      if (ccid != 3) {
	      error_message += "* Credit Card Number " + cc + " Is Not A Valid American Express.\n";
	      error = 1;
	      }
	      if (cclen != 15) {
	        error_message += "* You Have " + cclen + " Digits In Your Credit Card.  15 Digit Are Required.\n";
		error = 1;
	      }
	      var cnp_len = 4;
	      break;
	    default:
		error_message += "* Please Select a Credit Card Type.\n";
		error = 1;
	      break;
	  }
	  
	  if (year == thisYear) {
	    if (month < thisMonth) {
	      error_message += "* Expiration Date Is Invalid.\n";
	      error = 1;
	    }
	  }
	  
	  if (!payForm.no_cnp_security.checked) {
	    if (!payForm.cnp_security.value) {
	      error_message += "* Card Security Code Is Missing.\n";
	      error = 1;
	    }
	    else {
	      var cnp_val = payForm.cnp_security.value;
	      if (cnp_val.length != cnp_len) {
	        error_message += "* Card Security Code Is Invalid.\n";
		error = 1;
	        payForm.cnp_security.focus();
	        payForm.cnp_security.select();
	      }
	    }
	  }
  }
  if (amount == "") {
        error_message = error_message + "* Amount Is Missing.\n";
        error = 1;
  } else {
  	if (!(amountReg.exec(amount))) {
	        error_message = error_message + "* Invalid Amount, Enter A Valid Amount e.g. 49.99.\n";
	        error = 1;
	}
  }
  if (error == 1) {
        alert(error_message);
	payForm.Submit.disabled = false;
        return false;
  } else {
    return true;
  }
}

