﻿function validateFormOnSubmit(theForm) 
{
var reason = "";

  reason += validateFn(theForm.fn);
  reason += validateLn(theForm.ln); 
  
  reason += validateEmail(theForm.email);
    
  reason += validateRequest(theForm.request);
  
  
  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 = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = '#999999';
    }
    return error;   
}



// validate first name
function validateFn(fld) {
    var error = "";
    //var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "" ) 
    {
    	fld.style.background = 'Yellow';
        error = "The required field has not been filled in.\n"
    } 
    
    else if (fld.value.length <= 3) 
    {
        fld.style.background = 'Yellow'; 
        error = "Fisrt name too short.\n"
    } 
    
    else if (fld.value.length > 10)
    {
    	fld.style.background = 'Yellow'; 
        error = "Fisrt name too long.\n"
    }
     
    else {
    fld.style.background = 'white';
        
    } 
    return error;
}

function validateLn(fld) {
    var error = "";
    //var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "" ) 
    {
    	fld.style.background = 'Yellow';
        error = "The required field has not been filled in.\n"
    } 
    
    else if (fld.value.length <= 3) 
    {
        fld.style.background = 'Yellow'; 
        error = "Last name too short.\n"
    } 
    
    else if (fld.value.length > 10)
    {
    	fld.style.background = 'Yellow'; 
        error = "Last name too long.\n"
    }
     
    else {
    fld.style.background = 'white';
        
    } 
    return error;
}


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 = "The required field has not been filled in.\n"
        
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "The required field has not been filled in.\n"

        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The required field has not been filled in.\n";
        
    } else {
        fld.style.background = 'white';
        
       
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

// validate first name
function validateRequest(fld) {
    var error = "";
    //var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "" ) {
    	
        fld.style.background = 'Yellow';
        error = "The required field has not been filled in.\n"
            } else if (fld.value.length < 15) {
        fld.style.background = 'Yellow'; 
        error = "Your request should be more than 15 words.\n"
    } 
        else {
        fld.style.background = 'white';
        
    } 
    return error;
}
