function validateNewsLetterFormOnSubmit(theForm) {
var reason = "";
 	reason += validateName(theForm.name);
	reason += validateEmail(theForm.email);
     
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateApplicationFormOnSubmit(theForm) {
var reason = "";
 	reason += validateName(theForm.name);
	reason += validatePhone(theForm.tel);
	reason += validateEmail(theForm.email);
     
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateName(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#cc0000'; 
        error = "You didn't enter your name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 55)) {
        fld.style.background = '#cc0000'; 
        error = "The name has the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#cc0000'; 
        error = "The name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validatePhone(fld) {
    var error = ""; 
    if (fld.value.length == 0) {
        fld.style.background = '#FFFFCC'; 
        error = "The required field - telephone - not been filled in.\n";
	} else if ((fld.value.length < 6) || (fld.value.length > 20)) {
        fld.style.background = '#FFFFCC'; 
        error = "The telephone has the wrong value (6-18).\n";
	} else {
        fld.style.background = 'White';
    }
    return error; 
}

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 = '#cc0000';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#cc0000';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#cc0000';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
