// 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 += validateName(theForm.firstname);
	reason += validateName(theForm.lastname);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.telephone);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  } else {
        fld.style.background = 'White';
  }

  return true;
}


function validateName(fld) {
	
    var error = "";
    var illegalChars = /^[a-zA-Z0-9 -']+$/; // allow letters, numbers, and underscores [\'\-\ ]
	
	//alert("Validate Name activated");
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a name.\n";
    } else if (fld.value.length < 2) {
        fld.style.background = 'Yellow'; 
        error = "Please enter your full name.\n";
    } else if (!illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Only letters, spaces, apostrophes, and hyphens are allowed in the name field.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

//Next we want to see if the email address the user entered is real. This means that the input data must contain at least an @ sign and a dot (.). Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign. At first we check if the user entered anything at all in the email field. Next, we use regular expression and the test() method to check the field for a compliance. Also we will use trim() function that will trim leading whitespace off the string. This won’t be perfect validation — it is possible to slip not compliant addresses by it — but it's normally good enough.
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


//The function below checks if the phone number is valid. At first we use regular expression and the replace() method to clear out any spacer characters. Next, we use the isNaN() function to check if the phone number contain only numbers. At last we check the length of the string and permit only phone numbers with 10 digits.
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(stripped)) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure to include your area code.\n";
        fld.style.background = 'Yellow';
    } else {
			  validateEmpty(fld);
		}
		
    return error;
}



