//
// This is the javascript validation for the Contact form (contact.php). If all of the
// validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display
// a javascript alert popup with the error message returned from the PHP script.
//
function validate_book_order_form() {
  //Form validation
  var bError = false;
  if ($j('#book_first_name').val() == '') { addError('book_first_name'); bError = true; } else { removeErrorClass('book_first_name'); }
  if ($j('#book_last_name').val() == '') { addError('book_last_name'); bError = true; } else { removeErrorClass('book_last_name'); }
  if ($j('#book_address_1').val() == '') { addError('book_address_1'); bError = true; } else { removeErrorClass('book_address_1'); }
  if ($j('#book_city').val() == '') { addError('book_city'); bError = true; } else { removeErrorClass('book_city'); }
  if ($j('#book_state').val() == '') { addError('book_state'); bError = true; } else { removeErrorClass('book_state'); }
  if ($j('#book_zip').val() == '') { addError('book_zip'); bError = true; } else { removeErrorClass('book_zip'); }
  if ($j('#book_country').val() == '') { addError('book_country'); bError = true; } else { removeErrorClass('book_country'); }
  if ($j('#book_hm_phone').val() == '') { addError('book_hm_phone'); bError = true; } else { removeErrorClass('book_hm_phone'); }
  if ($j('#book_cell_phone').val() == '') { addError('book_cell_phone'); bError = true; } else { removeErrorClass('book_cell_phone'); }
  if ($j('#book_promotion').val().toLowerCase() != 'word') { addError('book_promotion'); bError = true; } else { removeErrorClass('book_promotion'); }

  var sEmail = $j('#book_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('book_email'); bError = true; } else { removeErrorClass('book_email'); }

//  if ($j('#book_comments').val() == '') { addError('book_comments'); bError = true; } else { removeErrorClass('book_comments'); }
  if ($j('#book_security_code').val() == '') { addError('book_security_code'); bError = true; } else { removeErrorClass('book_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

function removeErrorClass(control_id) {
  $j('#'+control_id).parent().parent().removeClass('error');
  return true;
}

function addError(control_id) {
  $j('#'+control_id).select().focus();
  $j('#'+control_id).parent().parent().addClass('error');
  return true;
}
