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


  reason += validateNameEmpty(theForm.txtName);
  reason += validateEmail(theForm.txtEmail);
  reason += validatePhone(theForm.txtTel);
  reason += validateValue(theForm.txtValue);
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

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


  reason += validateNameEmpty(theForm.txtName);
  reason += validateEmail(theForm.txtEmail);
  reason += validatePhone(theForm.txtTel);
  reason += validateValueSale(theForm.txtValue);
  reason += validateValuePurchase(theForm.txtValue2);
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateNameEmpty(fld) {
    var error = "";
  
  	
  
    if (fld.value.length == 0 || fld.value == "Your NAME here!") {
        fld.style.background = 'Yellow'; 
        error = "You have not filled in your NAME.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateNumberEmpty(fld) {
    var error = "";
  
  	
  
    if (fld.value.length == 0 || fld.value == "Your NUMBER here!") {
        fld.style.background = 'Yellow'; 
        error = "You have not filled in your CONTACT NUMBER.\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 = 'Yellow';
        error = "You didn't enter an EMAIL ADDRESS.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        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 CONTACT NUMBER.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The CONTACT NUMBER contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 11)) {
        error = "The CONTACT NUMBER is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}


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

   if (fld.value == "" || fld.value == "Property VALUE here!" ) {
        error = "You didn't enter a PROPERTY VALUE.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "PROPERTY VALUE must be numbers only\n";
		fld.style.background = 'Yellow';
		
    }
    return error;
}

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

   if (fld.value == "" || fld.value == "Property VALUE here!" ) {
        error = "You didn't enter a PURCHASE PROPERTY VALUE.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "PURCHASE PROPERTY VALUE must be numbers only\n";
		fld.style.background = 'Yellow';
		
    }
    return error;
}

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

   if (fld.value == "" || fld.value == "Property VALUE here!" ) {
        error = "You didn't enter a SALE PROPERTY VALUE.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "SALE PROPERTY VALUE must be numbers only\n";
		fld.style.background = 'Yellow';
		
    }
    return error;
}