//javascript form validation, christopher bodenlos
//the following function coordinates the other verification functions

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validatePhone(theForm.telephone);
  reason += validateEmpty(theForm.lastName);
  reason += validateEmpty(theForm.firstName);
  reason += validateEmpty(theForm.addressStreet);
  reason += validateEmpty(theForm.addressNo);
  reason += validateEmpty(theForm.addressCity);
  reason += validateEmpty(theForm.dateOfBirthYear);
  reason += validateEmpty(theForm.dateOfBirthMonth);
  reason += validateEmpty(theForm.dateOfBirthDay);
  reason += validateEmpty(theForm.doctorsName);
  reason += validatePhone(theForm.doctorsTelephone);
  reason += validateEmpty(theForm.contact1Name);
  reason += validateEmpty(theForm.contact1Phone);
  reason += validateEmpty(theForm.contact1Address);
  reason += validateEmpty(theForm.contact1Relationship);
  
  //The dialog below can contain individual reports of errors in the form by placing '+ reason' in the alert dialog text, outside of the quotes.
      
  if (reason != "") {
    alert("Please carefully fill the highlighted field(s).");
    return false;
  }

  return true;
}

//the following function validates that phone number & birth-date fields contain only numbers

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}

//the following function validates the presence of input in required fields

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "A required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
