function validateOnSubmit(theForm) 
{
    var reason = "";

    reason += validateName(theForm.Name);
    if(document.forms.sendcontactform.HomePhone.value.length > 0 && document.forms.sendcontactform.EMAIL.value.length == 0)
    {
        reason += validatePhone(theForm.HomePhone);
    }
    else if(document.forms.sendcontactform.HomePhone.value.length == 0 && document.forms.sendcontactform.EMAIL.value.length > 0)
    {
        reason += validateEmail(theForm.EMAIL);
    }
    else if(document.forms.sendcontactform.HomePhone.value.length > 0 && document.forms.sendcontactform.EMAIL.value.length > 0)
    {
        reason += validatePhone(theForm.HomePhone);
        reason += validateEmail(theForm.EMAIL);
    }
    else if(document.forms.sendcontactform.HomePhone.value.length == 0 && document.forms.sendcontactform.EMAIL.value.length == 0)
    {
        reason += validatePhone(theForm.HomePhone);
        reason += validateEmail(theForm.EMAIL);
    }
    reason += validateBoxes();
        
    if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
}
  
//alert("All fields are filled correctly");
return true;
}

function validateBoxes()
{
    var error
    if (document.forms.sendcontactform.checkbox1.checked == 0 && document.forms.sendcontactform.checkbox2.checked == 0 && document.forms.sendcontactform.checkbox3.checked == 0 && document.forms.sendcontactform.checkbox4.checked == 0)
    {
        error = "Sorry...please select 1 or more home builders below." 
        //document.boxes.visibilty = "visible";
        document.getElementById("boxes").style.display = "inline"
        document.getElementById("boxes").style.background = 'Yellow';


    }else{
        document.getElementById("boxes").style.display = "none"
        document.getElementById("boxes").style.background = 'White';
        error = "";
    }
    return error;
}

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 = 'White';
    }
    return error;  
}

function validateName(fld)
{
    var error = "";
    
    if (fld.value == "")
    {
        fld.style.background = 'Yellow'; 
        error = "Sorry...Can you please enter a 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 = 'Yellow';
        error = "Sorry...Can you please enter an email address.\n";
    } 
    else if (!emailFilter.test(tfld))
    {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Sorry...Can you please enter a valid email address.\n";
    }
    else if (fld.value.match(illegalChars))
    {
        fld.style.background = 'Yellow';
        error = "Sorry...this 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 = "Sorry...Can you please enter a phone number.\n";
        fld.style.background = 'Yellow';
    } 
    else if (isNaN(parseInt(stripped)))
    {
        error = "Sorry...this phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    }
    else if (!(stripped.length == 10))
    {
        error = "Sorry...this phone number is the incorrect length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}