// Order Form and Winner's Circle Validation Routines
function nullField (field){
  if (field.length < 1)
    retstat = true
  else
    retstat = false
  return retstat
}
function validName (name, inname){
  var retval = ""
  if (nullField(inname)) {
    retval = "Please enter a "+name+".\n"
  }
  return retval
}
function validStreet (name, street){
  var retval = ""
  if (nullField(street)) {
    retval = "Please enter a "+name+".\n"
  }
  return retval
}
function validCity (name, city){
  var retval = ""
  if (nullField(city)) {
    retval = "Please enter a "+name+".\n"
  }
  return retval
}
function validState (name, state){
  var retval = ""
//  AE|AL|AK|AP|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MP|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY
  var valid_state = new RegExp ("^(a[elkpszr]|c[aot]|d[ec]|f[ml]|g[au]|hi|i[dlna]|k[sy]|la|m[ehdainsopt]|n[evhjmyc]|o[hkr]|p[war]|ri|s[cd]|t[nx]|ut|v[tia]|w[aviy])$")
  if (nullField(state)) {
    retval = "Please enter a "+name+".\n"
  }
  else if (state.length < 2){
    retval = "Please enter a 2 character "+name+" abbreviation.\n"
  }
  else if (!valid_state.test(state.toLowerCase())){
    retval = "Please enter a valid "+name+".\n"
  }
  return retval
}
function validZip (name, zip){
  var retval = ""
  if (nullField(zip)) {
    retval = "Please enter a "+name+".\n"
  }
  else {
  //strip out acceptable non-numeric characters
    var stripped = zip.replace(/[\.\-\ ]/g, '')
    if (isNaN(stripped)) {
       retval += "The "+name+" contains illegal characters.\n"
    }
    else {
      if (!((stripped.length == 5) || (stripped.length == 9))) {
	       retval += "The "+name+" is the wrong length.\n"
      }
    }
  }
  return retval
}
function validCard (cardtype, cardnumber){
  var valid_visa = new RegExp ("^4[0-9]{12}(?:[0-9]{3})?$")
  var valid_mc = new RegExp ("^5[1-5][0-9]{14}$")
  var valid_amex = new RegExp ("^3[47][0-9]{13}$")
  var valid_dsc = new RegExp ("^6(?:011|5[0-9]{2})[0-9]{12}$")
  var stripped
  var retval = ""
  if (nullField(cardnumber)) {
    retval = "Please enter a credit card number.\n"
  }
  else {
    stripped = cardnumber.replace(/[ -]+/g, '')
    switch (cardtype){
      case "A":
        if (!valid_amex.test(stripped))
          retval = "Invalid credit card number.\n"
        break
      case "D":
        if (!valid_dsc.test(stripped))
          retval = "Invalid credit card number.\n"
        break
      case "M":
        if (!valid_mc.test(stripped))
          retval = "Invalid credit card number.\n"
        break
      case "V":
        if (!valid_visa.test(stripped))
          retval = "Invalid credit card number.\n"
        break
      default : retval = "There is a coding error in the form.\n"
    }
  }
  return retval
}
function validCode (cardtype, code){
  var retval = ""
  var valid_amex = new RegExp ("^[0-9]{4}$")
  var valid_rest = new RegExp ("^[0-9]{3}$")
  if (nullField(code))
    retval = "Please enter a security code.\n"
  else if (isNaN(code))
    retval = "Security code has invalid characters.\n"
  else if (cardtype == "A"){
    if (!valid_amex.test(code))
      retval = "Invalid security code.\n"
  }
  else if (!valid_rest.test(code))
    retval = "Invalid security code.\n"
  return retval
}
function validExp (month, year){
  var retval = ""
  var valid_month = new RegExp ("^0[1-9]|1[0-2]$")
  var valid_year = new RegExp ("^0[8-9]|[1-2][0-9]$")
  if (nullField(month))
    retval += "Please enter an expiration month.\n"
  else if (!valid_month.test(month))
    retval += "Please enter a valid expiration month.\n"
  if (nullField(year))
    retval += "Please enter an expiration year.\n"
  else if (!valid_year.test(year))
    retval += "Please enter a valid expiration year.\n"
  return retval
}
function validPhone (name, phone){
  var retval = ""
  if (nullField(phone)) {
    retval += "Please enter a "+name+".\n"
  }
  else {
  //strip out acceptable non-numeric characters
    var stripped = phone.replace(/[\(\)\.\-\ ]/g, '')
    if (isNaN(stripped)) {
       retval += "The "+name+" contains illegal characters.\n"
    }
    else {
      if (!(stripped.length == 10)) {
	       retval += "The "+name+" is the wrong length. Make sure you included an area code.\n"
      }
    }
  }
  return retval
}
function validFax (name, fax){
  var retval = ""
  if (!nullField(fax.value)) {
  //strip out acceptable non-numeric characters
    var stripped = fax.value.replace(/[\(\)\.\-\ ]/g, '')
    if (isNaN(stripped)) {
       retval += "The "+name+" contains illegal characters.\n"
    }
    else {
      if (!(stripped.length == 10)) {
	       retval += "The "+name+" is the wrong length. Make sure you included an area code.\n"
      }
    }
  }
  return retval
}
function validEMail (name, email){
  var emailFilter=/^.+@.+\..{2,3}$/;
  var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
  var retval = ""
  if (nullField(email)) {
    retval = "Please enter a "+name+".\n"
  }
  else if (!(emailFilter.test(email))) { 
    retval = "Please enter a valid "+name+".\n";
  }
  else if (email.match(illegalChars)) {
    retval = "The "+name+" contains illegal characters.\n";
  }
  return retval
}
function validPurchase (name){
  var retval = ""
  var nstring
  nstring = name.replace(/[$\s]/g, "")
  if ((nstring-0 <= 0)&&(!document.ordform.clubCheckbox.checked)) {
    retval = "Please select some wine to purchase.\n"
  }
  return retval
}
function validCreditCard (){
  var retval = ""
  var cardtype = ""
  var i = -1
  do {
    i++
    cardtype = document.ordform.Payment[i].value
  }
  while (!document.ordform.Payment[i].checked)
  if (cardtype != "C") {
    retval += validCard (cardtype, document.ordform.billCard.value)
    retval += validCode (cardtype, document.ordform.billCode.value)
    retval += validExp (document.ordform.billMonth.value, document.ordform.billYear.value)
  }
  return retval
}
function validateForm () {
  var alert_string =""
  updateShippingInfo()
  alert_string += validName("billing name", document.ordform.billName.value)
  alert_string += validStreet("billing address", document.ordform.billStreet.value)
  alert_string += validCity("billing city", document.ordform.billCity.value)
  alert_string += validState("billing state", document.ordform.billState.value)
  alert_string += validZip("billing zip code", document.ordform.billZip.value)
  alert_string += validCreditCard()
  alert_string += validPhone("billing phone number", document.ordform.billPhone.value)
  alert_string += validEMail("billing email address", document.ordform.billEMail.value)
  alert_string += validFax("billing FAX number", document.ordform.billFAX)
  alert_string += validPurchase(document.ordform.subtotal2.value)
  if (!document.ordform.shipCheckbox.checked){
    alert_string += validName("shipping name", document.ordform.shipName.value)
    alert_string += validStreet("shipping address", document.ordform.shipStreet.value)
    alert_string += validCity("shipping city", document.ordform.shipCity.value)
    alert_string += validState("shipping state", document.ordform.shipState.value)
    alert_string += validZip("shipping zip code", document.ordform.shipZip.value)
    alert_string += validPhone("shipping phone number", document.ordform.shipPhone.value)
  }
  if (alert_string.length >1){
    retval = false
    alert (alert_string)
  }
  else {
    retval = true
  }
  return retval
}
function updateShippingInfo() {
  if (document.ordform.shipCheckbox.checked){
  	document.ordform.shipName.value=document.ordform.billName.value
  	document.ordform.shipStreet.value=document.ordform.billStreet.value
  	document.ordform.shipStreet2.value=document.ordform.billStreet2.value
  	document.ordform.shipCity.value=document.ordform.billCity.value
  	document.ordform.shipState.value=document.ordform.billState.value
  	document.ordform.shipZip.value=document.ordform.billZip.value
  	document.ordform.shipPhone.value=document.ordform.billPhone.value
  	document.ordform.shipName.readOnly=true
  	document.ordform.shipStreet.readOnly=true
  	document.ordform.shipStreet2.readOnly=true
  	document.ordform.shipCity.readOnly=true
  	document.ordform.shipState.readOnly=true
  	document.ordform.shipZip.readOnly=true
  	document.ordform.shipPhone.readOnly=true
  	updateTotals()
  }
  else {
  	document.ordform.shipName.readOnly=false
  	document.ordform.shipStreet.readOnly=false
  	document.ordform.shipStreet2.readOnly=false
  	document.ordform.shipCity.readOnly=false
  	document.ordform.shipState.readOnly=false
  	document.ordform.shipZip.readOnly=false
  	document.ordform.shipPhone.readOnly=false
  }
  return
}
function updateShippingBlock (bill, ship) {
  if (document.ordform.shipCheckbox.checked)
    ship.value=bill.value
  return
}

