//
// This is the javascript validation for the Send to Friend 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_send_to_friend_form() {
  //Form validation
  var bError = false;
  if ($j('#stf_name').val() == '') { addError('stf_name'); bError = true; } else { removeErrorClass('stf_name'); }
  var sEmail = $j('#stf_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('stf_email'); bError = true; } else { removeErrorClass('stf_email'); }
  if ($j('#stf_friend_name_1').val() == '') { addError('stf_friend_name_1'); bError = true; } else { removeErrorClass('stf_friend_name_1'); }
  var sEmail = $j('#stf_friend_email_1').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('stf_friend_email_1'); bError = true; } else { removeErrorClass('stf_friend_email_1'); }
  if ($j('#stf_friend_name_2').val() !== "") {
    var sEmail = $j('#stf_friend_email_2').val();
    if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('stf_friend_email_2'); bError = true; } else { removeErrorClass('stf_friend_email_2'); }
  }

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

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

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

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