function validateFormOnSubmit(RIFForm) {
var reason = "";

  reason += validateFirstname(RIFForm.CPPfirstName);
  reason += validateLastname(RIFForm.CPPlastName);
  reason += validateAreacode(RIFForm.CPPphoneNumberAreaCode);
  reason += validatePrefix(RIFForm.CPPphoneNumberPrefix);
  reason += validateExchange(RIFForm.CPPphoneNumberExchange);
  reason += validateEmail(RIFForm.CPPemailAddress);
  reason += validateZip(RIFForm.CPPzip);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#DFF0F9'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateFirstname(fld) {
    var error = "";
    var illegalChars = /[0-9\(\)\<\>\,\;\:\\\"\[\]\*\+\-\/\~\`\!\@\#\$\%\^\&\_\-\=\|\}\{\?\.]/;
 
    if (fld.value == "") {
        fld.style.background = '#DFF0F9'; 
        error = "You didn't enter a First Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#DFF0F9'; 
        error = "The First Name contains characters that are not allowed.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateLastname(fld) {
    var error = "";
    var illegalChars = /[0-9\(\)\<\>\,\;\:\\\"\[\]\*\+\-\/\~\`\!\@\#\$\%\^\&\_\-\=\|\}\{\?\.]/; 
 
    if (fld.value == "") {
        fld.style.background = '#DFF0F9'; 
        error = "You didn't enter a Last Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#DFF0F9'; 
        error = "The Last Name contains characters which are not allowed.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateAreacode(fld) {
    var error = "";
    var illegalChars = /[a-zA-Z\(\)\<\>\,\;\:\\\"\[\]\*\+\-\/\~\`\!\@\#\$\%\^\&\_\-\=\|\}\{\?\.]/; 
 
    if (fld.value == "") {
        fld.style.background = '#DFF0F9'; 
        error = "You didn't enter an Area Code.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#DFF0F9'; 
        error = "The Area Code contains characters which are not allowed.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePrefix(fld) {
    var error = "";
    var illegalChars = /[a-zA-Z\(\)\<\>\,\;\:\\\"\[\]\*\+\-\/\~\`\!\@\#\$\%\^\&\_\-\=\|\}\{\?\.]/; 
 
    if (fld.value == "") {
        fld.style.background = '#DFF0F9'; 
        error = "You didn't enter a Prefix.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#DFF0F9'; 
        error = "The Prefix contains characters which are not allowed.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateExchange(fld) {
    var error = "";
    var illegalChars = /[a-zA-Z\(\)\<\>\,\;\:\\\"\[\]\*\+\-\/\~\`\!\@\#\$\%\^\&\_\-\=\|\}\{\?\.]/; 
 
    if (fld.value == "") {
        fld.style.background = '#DFF0F9'; 
        error = "You didn't enter an Exchange.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#DFF0F9'; 
        error = "The Exchange contains characters which are not allowed.\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);                        
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = '#DFF0F9';
        error = "You didn't enter an Email address.\n";
    } else if (!emailFilter.test(tfld)) {              
        fld.style.background = '#DFF0F9';
        error = "Please enter a valid Email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#DFF0F9';
        error = "The Email address contains characters that are not allowed.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateZip(fld) {
    var error = "";
    var illegalChars = /[a-zA-Z\(\)\<\>\,\;\:\\\"\[\]\*\+\-\/\~\`\!\@\#\$\%\^\&\_\-\=\|\}\{\?\.]/; 
 
    if (fld.value == "") {
        fld.style.background = '#DFF0F9'; 
        error = "You didn't enter a ZIP Code.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#DFF0F9'; 
        error = "The ZIP Code contains characters which are not allowed.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
