// check required fields - checkout.php
function checkform ( form )
{
  // see http://www.thesitewizard.com/archive/validation.shtml
  // for an explanation of this script and how to use it on your
  // own website

  // ** START **
  
  if (IsEmpty(form.Name)) {
    alert( "Please enter your name." );
    form.Name.focus();
    return false;
  }
  
  if (IsEmpty(form.Email)) {
    alert( "Please enter your email address." );
    form.Email.focus();
    return false ;
  }
  
  if (!echeck(form.Email.value)){
    alert( "Please enter a valid email address." );
    form.Email.focus();
    return false;
  }
  
  if (IsEmpty(form.Phone)) {
    alert( "Please enter your phone number." );
    form.Phone.focus();
    return false;
  }
  
  if (IsEmpty(form.Address)) {
    alert( "Please enter a shipping address." );
    form.Address.focus();
    return false;
  }
  
  // do credit card checks if paypal account field is empty
  if (IsEmpty(form.PayPal_Account)) {
  
    if (!cardval(form.Credit_Card_Number.value)) {
      alert( "Please enter a valid credit card number." );
      form.Credit_Card_Number.focus();
      return false;
    }
  
    if (!checkMMYY(form.Credit_Card_Exp_MMYY.value)) {
      alert( "Please enter a valid expiration date." );
      form.Credit_Card_Exp_MMYY.focus();
      return false;
    } 
  
    if (IsEmpty(form.Credit_Card_CVV) || !IsNumeric(form.Credit_Card_CVV.value)) {
      alert( "Please enter a valid CVV code found on the back of your credit card. Amex card CVV code is on the front." );
      form.Credit_Card_CVV.focus();
      return false;
    }
    
  }
  
  // otherwise, paypal address is not empty, so do paypal address email check
  else {
    if (!echeck(form.PayPal_Account.value)){
      alert( "Please enter a valid PayPal account (in the form of an email address). Otherwise, leave this field blank." );
      form.PayPal_Account.focus();
      return false;
    }
    
    // just to be safe, clear credit card for paypal orders
    if (!IsEmpty(form.Credit_Card_Number)) {
      alert( "Please clear the credit card number field if you are using PayPal." );
      form.Credit_Card_Number.focus();
      return false;
    }
  }

  // ** END **
  return true ;
}

// check required fields - contact.php
function checkcontactform ( form )
{
  // see http://www.thesitewizard.com/archive/validation.shtml
  // for an explanation of this script and how to use it on your
  // own website

  // ** START **
  
  if (IsEmpty(form.Name)) {
    alert( "Please enter your name." );
    form.Name.focus();
    return false;
  }
  
  if (IsEmpty(form.Email)) {
    alert( "Please enter your email address." );
    form.Email.focus();
    return false ;
  }
  
  if (!echeck(form.Email.value)){
    alert( "Please enter a valid email address." );
    form.Email.focus();
    return false;
  }
  
  if (IsEmpty(form.Comments)) {
    alert( "Please submit your comments in the text box provided." );
    form.Comments.focus();
    return false;
  }
  
  var n1 = parseInt(form.n1.value);
  var n2 = parseInt(form.n2.value);
  sum = n1 + n2;
  
  if (IsEmpty(form.MathAnswer)) {
    alert( "Please answer the math question.");
    form.MathAnswer.focus();
    return false;
  }
  
  if (parseInt(form.MathAnswer.value) != sum) {
    alert( "You have answered incorrectly. Please try again!");
	form.MathAnswer.focus();
	return false;
  }
  
  // ** END **
  return true ;
}













// Credit Card Validation Javascript
// http://www.felgall.com/jstip47.htm
function cardval(s) {
  // remove non-numerics
  var v = "0123456789";
  var w = "";
  for (i=0; i < s.length; i++) {
    x = s.charAt(i);
    if (v.indexOf(x,0) != -1)
      w += x;
  }
  // validate number
  j = w.length / 2;
  if (j < 6.5 || j > 8 || j == 7) return false;
  k = Math.floor(j);
  m = Math.ceil(j) - k;
  c = 0;
  for (i=0; i<k; i++) {
    a = w.charAt(i*2+m) * 2;
    c += a > 9 ? Math.floor(a/10 + a%10) : a;
  }
  for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
  return (c%10 == 0);
}


// email validation
// http://www.smartwebby.com/dhtml/email_validation.asp
function echeck(str) {

  var at="@";
  var dot=".";
  var lat=str.indexOf(at);
  var lstr=str.length;
  var ldot=str.indexOf(dot);
  if (str.indexOf(at)==-1){
    return false;
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
    return false;
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    return false;
  }

  if (str.indexOf(at,(lat+1))!=-1){
    return false;
  }

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    return false;
  }

  if (str.indexOf(dot,(lat+2))==-1){
    return false;
  }
  
  if (str.indexOf(" ")!=-1){
    return false;
  }
  return true;
}

/* http://www.codetoad.com/javascript/isnumeric.asp */
/* check numeric */
function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
     Char = sText.charAt(i); 
     if (ValidChars.indexOf(Char) == -1) {
       IsNumber = false;
     }
   }
   return IsNumber;
}

/* check MMYY - accept only if MM is 12 or less */
function checkMMYY(mmyy) {
  if (mmyy == "") {
    return false;
  }
  else if (mmyy == null) {
    return false;
  }
  else if (!IsNumeric(mmyy)) {
    return false;
  }
  else if (parseInt(mmyy.charAt(0)) > 1) {
    return false;
  }
  else if (parseInt(mmyy.charAt(0)) == 1 && parseInt(mmyy.charAt(1)) > 2) {
    return false;
  }
  else {
    return true;
  }
}

// limit textarea character count
// http://www.mediacollege.com/internet/javascript/form/limit-characters.html
function limitText(limitField, limitNum) {
  if (limitField.value.length > limitNum) {
    // trim if too long
    limitField.value = limitField.value.substring(0, limitNum);
  }
  else {
  }
}

// check if a text field is empty
// http://www.codetoad.com/javascript/isempty.asp
function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) || (aTextField.value==null) || (aTextField.value == "")) {
      return true;
   }
   else { return false; }
}
