

function resetFields(contactForm)
{
	document.contactForm.FirstName.style.background = 'White';
	document.contactForm.LastName.style.background = 'White';
	document.contactForm.Address.style.background = 'White';
	document.contactForm.City.style.background = 'White';
	document.contactForm.Phone.style.background = 'White';
	document.contactForm.Email.style.background = 'White';
	document.contactForm.Message.style.background = 'White';

}

function resetMessage(contactForm)
{
	document.contactForm.Message.value = '';
	document.contactForm.Message.onclick = null;
}

function validateForm(contactForm)
{
	var reason = "";
	reason += validateFirstName(contactForm.FirstName);
	reason += validateLastName(contactForm.LastName);
	reason += validateAddress(contactForm.Address);
	reason += validateCity(contactForm.City);
	reason += validatePhone(contactForm.Phone);
	reason += validateEmail(contactForm.Email);
	reason += validateMessage(contactForm.Message);
		  
	if (reason != "") {
	  alert("Some fields need correction:\n" + reason);
	  return false;
	}

	var answer = confirm ("Are you ready to submit?");
	if (answer) {
		return true;
	}
	else {
		return false;
	}
	
	return true;
}

function validateFirstName(fld) {
    var error = "";
    var illegalChars = /\d/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a First Name.\n";
    } else if (fld.value.length < 2) {
        fld.style.background = 'Yellow'; 
        error = "The First Name must have 2 or more characters.\n";
	} else if (fld.value.length > 30) {
        fld.style.background = 'Yellow'; 
        error = "The First Name has reached the max length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The First Name can not contains 0-9/special characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateLastName(fld) {
    var error = "";
    var illegalChars = /\d/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a Last Name.\n";
    } else if (fld.value.length < 2) {
        fld.style.background = 'Yellow'; 
        error = "The Last Name must have 2 or more characters.\n";
	} else if (fld.value.length > 30) {
        fld.style.background = 'Yellow'; 
        error = "The Last Name has reached the max length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The Last Name can not contains 0-9/special characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateAddress(fld) {
    var error="";
    var tfld = trim(fld.value);  // value of field with whitespace trimmed off
    var addressFilter = /^[0-9]{1,}\s[a-zA-Z]{1,}\s/ ;
    var illegalChars= /[\(\)\<\>\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an Address.\n";
    } else if (!addressFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter Address: ex. 2819 Oak Road\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The Address can not contains special characters. ( # allowed)\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCity(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var cityFilter = /^[a-zA-Z]{1,}[\, ]?\s[a-zA-Z]{2}\s[0-9]{5}$/ ;
    var illegalChars= /[\(\)\<\>\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a City, ST ZIP.\n";
    } else if (!cityFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter City: ex. Pearland, TX 77584\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The City, ST ZIP can not contain special characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var phoneFilter = /^[0-9]{3}[\- ][0-9]{3}[\- ][0-9]{4}$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a Phone number.\n";
    } else if (!phoneFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter Phone: ex. 832-875-2996\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The Phone number can not contain special characters.\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: ex: rezremodeling@aol.com\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The Email Address can no contain special characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validateMessage(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "" || fld.value == "** Please include your available hours for consultation. **") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a Message.\n";
    } else if (fld.value.length < 2) {
        fld.style.background = 'Yellow'; 
        error = "The Message must have 2 or more characters.\n";
	} else if (fld.value.length > 1000) {
        fld.style.background = 'Yellow'; 
        error = "The Message has reached the max length.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

