//
// This is the javascript validation for the Prayer Request form. 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_prayer_request_form() {
  //Form validation
  var bError = false;
  if ($j('#pray_first_name').val() == '') { addError('pray_first_name'); bError = true; } else { removeErrorClass('pray_first_name'); }
  if ($j('#pray_last_name').val() == '') { addError('pray_last_name'); bError = true; } else { removeErrorClass('pray_last_name'); }
  if ($j('#pray_zip').val() == '') { addError('pray_zip'); bError = true; } else { removeErrorClass('pray_zip'); }

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

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