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

  reason += validateFName(theForm.realname);
  
  reason += validatePhone(theForm.Phone);
  reason += validateEmail(theForm.email);
 
      
  if (reason != "") {
    alert("Please fill in the following information\n\n" + reason);
    return false;
  }

 
}
function validateIncome(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You must fill in an Income figure.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateOutgoings(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You must fill in an Outgoings figure.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateFName(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a First Name.\n";
    }  else {
        fld.style.background = 'White';
    }
    return error;
}
function validateLName(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a Surname.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 20)) {
        fld.style.background = 'Yellow'; 
        error = "The Surname not a valid length.\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 = 'Yellow';
        error = "- You didn't enter an EMAIL address.\n";
    } else if (!emailFilter.test(tfld)) {             
        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;
}
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 = "- PHONE contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length > 5)) {
        error = "- PHONE number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}
