// 
// Customer/Credit card information verification
//
// Version for Netscape 4.x
// 
//  Copyright (c) 2001 Hellriser Customs. All rights reserved.
//
//  This code cannot be duplicated,used or distributed without written authorizazion of
//  Hellriser Customs. 
//


function customerCheck() {
  var emptyFields = new Array();
  var wrongFields = new Array();
  var isPoBox = false;
  i=0;
  l=0;

  if ( document.ccinfo.firstName.value == "" ) {
    emptyFields[i++] = 'First name';
  }
  if ( document.ccinfo.lastName.value == "" ) {
    emptyFields[i++] = 'Last name';
  }
  if ( document.ccinfo.address.value == "" ) {
    emptyFields[i++] = 'Address';
  }
  if ( document.ccinfo.city.value == "" ) {
    emptyFields[i++] = 'City';
  }
  if ( document.ccinfo.zip.value == "" ) {
    emptyFields[i++] = 'Zip';
  }
  else if ( ! checkPostalCode() ) {
    wrongFields[l++] = "Zip code for USA orders must be 5 digits!";
  }
  /*
  else if ( (document.ccinfo.zip.value.search(/\d{5}/) == -1) && (document.ccinfo.zip.value.search(/\w\d\w\d\w\d/) == -1) ) {
    wrongFields[l++] = 'Zip code must be numeric for USA addresses or in the format AXAXAX for Canadian addresses';    
  }
  */
  if ( document.ccinfo.state.value == "" ) {
    emptyFields[i++] = 'State';
  }
  if ( document.ccinfo.telephone.value == "" ) {
    emptyFields[i++] = 'Telephone';
  }
  else if ( !checkPhoneFormat() ) {
    wrongFields[l++] = 'Please enter a telephone number using the format: (AAA) BBB-CCCC or AAA BBB-CCCC or AAA-BBB-CCCC or AAA BBB CCCC';
  }
  if ( document.ccinfo.email.value == "" ) {
    emptyFields[i++] = 'email';
  }
  else {
    if ( ! checkEmailAddress(document.ccinfo.email) ) {
      wrongFields[l++] = "The email address doesn't seem correct";
    }
  }

  if ( document.ccinfo.address.value.match(/P\.?O\.?\s*box/i) ) {
    isPoBox = true;
  }

  if ( isPoBox && (document.ccinfo.s_firstName.value == "") ) {
    wrongFields[l++] = 'FedEx cannot ship to PO Boxes. Please enter an alternative address for shipment';
  }

  if ( document.ccinfo.s_firstName.value != "" ) {
    if ( document.ccinfo.s_lastName == "" ) {
      emptyFields[i++] = 'Shipping First Name';
    }
    if ( document.ccinfo.s_address.value == "" ) {
      emptyFields[i++] = 'Shipping Address';
    }
    if ( document.ccinfo.s_city.value == "" ) {
      emptyFields[i++] = 'Shipping City';
    }
    if ( document.ccinfo.s_zip.value == "" ) {
      emptyFields[i++] = 'Shipping Zip';
    }
    if ( document.ccinfo.s_state.value == "" ) {
      emptyFields[i++] = 'Shipping State';
    }
  }


  if ( i > 0 ) {
    flds = "";
    for( j = 0; j < i; j++ ) { 
      flds = flds + emptyFields[j] + "\n";
    }
    alert("The following fields must be filled before continuing:\n" + flds);
  }
  else if ( l > 0 ) {
    flds = "";
    for( j = 0; j < l; j++ ) { 
      flds = flds + wrongFields[j] + "\n";
    }
    alert("Found the following errors in the form:\n" + flds);
  }
  else {
    // if the alt_payment field is defined then we are logged into the 
    // system and we have the option to use differen payment than credit card
    // If the alt_payment check box is checked then we can just submit the form.
    if ( (document.ccinfo.alt_payment && document.ccinfo.alt_payment.checked) ) {
      document.ccinfo.submit();
    }  
    else if ( ! isCreditCard( document.ccinfo.cardNo.value.replace(/\s+/g,"") ) ) {
      alert("The credit card number is invalid !");
    }
    else {
      // Check if the date is in the future...
      var today = new Date();
      cYear = today.getYear() + 1900;
      cMonth = today.getMonth() + 1;

      if (  (cYear == document.ccinfo.years.value) && (document.ccinfo.months.value <= cMonth) ) {
	alert("Expiration date invalid. It must be in the future !");
	return;
      }
      // Otherwise we are all right and the form can be submitted.
      document.ccinfo.submit();
    }
  }
}

function checkBBZLogin() {
  if ( (document.bbzlogin.firstName.value == "") || (document.bbzlogin.lastName.value == "") ) {
    alert("You must provide both your first and your last name !");
    return false;
  }
  if ( !checkEmailAddress(document.bbzlogin.email) ) {
    alert("Your email address doesn't seem to be correct ... Please verify it and try again.");
    return false;
  }
  return true;
//  document.bbzlogin.submit();
}

function checkEmailAddress(field) { 
  // Note: The next expression must be all on one line... 
  // allow no spaces, linefeeds, or carriage returns! 
  var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\..{2,2}))$)\b/gi);

  return goodEmail;
} 



/* 
  Portion of this code Copyright 1996, Infohiway, Inc. Use
  under the terms of the license.
  ==================================================================
   THIS FUNCTION IS TAKEN DIRECTLY FROM NETSCAPE FROM:
   http://developer.netscape.com/library/examples/...
                    .../javascript/formval/FormChek.js
   which is a bunch of functions to validate forms
    
   FUNCTION: isCreditCard(st)
   INPUT:    st - a string representing a credit card number
   RETURNS:  true, if the credit card number passes the Luhn Mod-10 test
                 false, otherwise
   ================================================================== */

// change it to false if you do not want
//the credit card number to be encrypted
var encrypt_it = false;

function isCreditCard(st) {
  if ( st == "" ) {
    return false;
  }
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; 
  mul = 1; 
  l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}

function checkPostalCode() {
  var country = document.getElementById('country').value;
  var zip = document.getElementById('zip').value;
  if ( country == 'USA' ) {
    if (zip.search(/^\d{5}$/) == -1) {
      return(false);
    }
  }
  return(true); // If the zip is numeric or the country is non USA we assume it's correct.
}

function checkPhoneFormat() {
  var country = document.getElementById('country').value;
  var phone = document.getElementById('telephone').value;
  if ( country == 'USA' ) {
    if (!phone.match(/\(?\d{3}\)?(\s|-)\d{3}(-|\s)\d{4}/)) {
      return(false);
    }
  }
  return(true); // If the country is non USA we assume it's correct.

}
