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

  	reason += validateName(analysisForm.name);
	reason += validateEmail(analysisForm.email);
	reason += validateAddress(analysisForm.address);
	reason += validateCity(analysisForm.city);
	reason += validateZip(analysisForm.zip);
	reason += validateBedrooms(analysisForm.bedrooms);
	reason += validateBathrooms(analysisForm.bathrooms);     
	reason += validateSqFt(analysisForm.squarefootage);   
	reason += validateYear(analysisForm.yearbuilt); 
	reason += validateHomephone(analysisForm.homephone); 
 
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
}

function validateName(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fcf6c8'; 
        error = "Please enter your full name.\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 = '#fcf6c8';
        error = "Please enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {   //test email for illegal characters
        fld.style.background = '#fcf6c8';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#fcf6c8';
        error = "Please enter a valid email address.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateAddress(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fcf6c8'; 
        error = "Please enter your street address.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCity(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fcf6c8'; 
        error = "Please enter your city.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateZip(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fcf6c8'; 
        error = "Please enter your zip code.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateBedrooms(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fcf6c8'; 
        error = "Please enter your number of bedrooms.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateBathrooms(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fcf6c8'; 
        error = "Please enter your number of bathrooms.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateSqFt(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fcf6c8'; 
        error = "Please enter your square footage.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateYear(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#fcf6c8'; 
        error = "Please enter the year your home was built.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateHomephone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Please enter your phone number.\n";
        fld.style.background = '#fcf6c8';
    } else if (isNaN(parseInt(stripped))) {
        error = "Please enter a valid phone number.\n";
        fld.style.background = '#fcf6c8';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#fcf6c8';
    } else {
        fld.style.background = 'White';
    }
    return error;
}


