//
// 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_contact_us_form() {
  //Form validation
  var bError = false;
  if ($j('#cont_first_name').val() == '') { addError('cont_first_name'); bError = true; } else { removeErrorClass('cont_first_name'); }
  if ($j('#cont_last_name').val() == '') { addError('cont_last_name'); bError = true; } else { removeErrorClass('cont_last_name'); }
  if ($j('#cont_address_1').val() == '') { addError('cont_address_1'); bError = true; } else { removeErrorClass('cont_address_1'); }
  if ($j('#cont_city').val() == '') { addError('cont_city'); bError = true; } else { removeErrorClass('cont_city'); }
  if ($j('#cont_state').val() == '') { addError('cont_state'); bError = true; } else { removeErrorClass('cont_state'); }
  if ($j('#cont_country').val() == '') { addError('cont_country'); bError = true; } else { removeErrorClass('cont_country'); }
  if ($j('#cont_phone').val() == '') { addError('cont_phone'); bError = true; } else { removeErrorClass('cont_phone'); }

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

  if ($j('#cont_comments').val() == '') { addError('cont_comments'); bError = true; } else { removeErrorClass('cont_comments'); }
  if ($j('#cont_security_code').val() == '') { addError('cont_security_code'); bError = true; } else { removeErrorClass('cont_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;
}
