// JavaScript Document

/*This is a main function that calls a series of subfunctions, each of which checks a single form element for compliance. If the element complies than sufunction returns an empty string. Otherwise it returns a message describing the error and highlight appropriate element with yellow.*/
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmpty(theForm.title);
  reason += validateEmpty(theForm.author_name);
  reason += validateEmpty(theForm.author_email);
      
  if (reason != "") {
    return false;
  }

  return true;
}

/*The function below checks if a required field has been left empty. If the required field is blank, we return the error string to the main function. If it’s not blank, the function returns an empty string.*/
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#FFDFDF'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}