function valid(theForm) {
    var why = "";
    why = checkClause(theForm.clause) + why;
    why = checkBody(theForm.body) + why;
    why = checkPhone(theForm.phone) + why;
    why = checkEmail(theForm.email) + why;
    why = checkUsername(theForm.name) + why;
    if (why == "") {
        return true;
    }
    alert(why);        
    return false;
}
function checkClause(field){
    var error = "";
    if (!field.checked) {
       error = "You must check that you understand that an attorney-client relationship has not yet been established.\n";
       field.focus();
       field.select();
       field.style.background = "#FAFBA3";
    }
    if (error.length == 0) field.style.background = "#FFFFFF";
    return error;
}
function checkBody(field) {
	var str = field.value;
	var error = "";
	var illegalChars = /[\<\>]/;
	if (illegalChars.test(str)) {
	       error = "Special symbols are not allowed in the body of the message.\n";
        }
	if (str.length == 0) {
	    error = "Please enter a message.\n";
	}
	if (error != "") {
		field.focus();
		field.select();
		field.style.background = "#FAFBA3";
	}
	if (error.length == 0) {
		field.style.background = "#FFFFFF";
		field.value = TextTidy(str)
	}	
	return error;
}
function checkPhone(field) {	
	var str = field.value;
	var error = "";
	var stripped = str.replace(/\ /g, "");
	stripped = stripped.replace(/\(/g, "");
	stripped = stripped.replace(/\)/g, "");
	stripped = stripped.replace(/\-/g, "");
	stripped = stripped.replace(/\./g, "");
	if (isNaN(parseInt(stripped))) {
		error = "The phone number contains special symbols.";
	}
	if (!(stripped.length == 10)) {
		error = "The phone number must contain 10 digits. Please be sure to include an area code.\n";
	}
	if (str == "") {
		error = "Please include a phone number.\n";
	}	
	if (error != "") {
		field.focus();
		field.select();
		field.style.background = "#FAFBA3";
	}
	if (error.length == 0) {
		field.style.background = "#FFFFFF";
		field.value = stripped.substring(0, 3) + "." + stripped.substring(3, 6) + "." + stripped.substring(6, 10);
	}
	return error;
}
function checkEmail(field) {
	var str = field.value;
	var error = "";
	var illegalchar = /\(\)\<\>\,\;\:\\\"\[\]/;
	if (str.match(illegalchar)) {
		error = "Please enter a valid email address.\n";
	}
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	if (!regex.test(str)) {
		error = "Please enter a valid email address.\n";
	}	
	if (!window.RegExp) {
		if(str.indexOf("@") <= 0)
		error = "Please enter a valid email address.\n";
	}	
	if (str == "") {
		error = "Please enter a valid email address.\n";
	}	
	if (error != "") {
		field.focus();
		field.select();
		field.style.background = "#FAFBA3";
	}
	if (error.length == 0) field.style.background = "#FFFFFF";
	return error;
}
function checkUsername(field) {
	var str = field.value;
	var error = "";
	var regex = /[\<\>]/;
	if (regex.test(str)) {
		error = "The name contains special symbols.\n";
	}
 	if (str.length < 4) {
		error = "The name provided is too short.\n";
	}
	if (str.indexOf(" ") < 0) {
		error = "Please specify a first AND last name.\n";
	}
	if (str == "") {
		error = "Please enter a contact name.\n";
	}	
 	if (error != "") {
		field.focus();
		field.select();
		field.style.background = "#FAFBA3";
	}
	if (error.length == 0) {
		field.style.background = "#FFFFFF";
		field.value = toProperCase(str);
	}	
	return error;
}

function TextTidy(VALUE){
var STRING = VALUE;
var strReturn_Value = "";
var iTemp = STRING.length;
if(iTemp==0){
return"";
}

var stri = " i ";
var strI = " I ";

while(STRING.indexOf(stri) > -1){
STRING = STRING.replace(stri,strI);
}

while(STRING.indexOf("  ") > -1){
STRING = STRING.replace("  "," ");
}

var UcaseNext = false;
strReturn_Value += STRING.charAt(0).toUpperCase();

for(var iCounter=1;iCounter < iTemp;iCounter++){
if(UcaseNext == true){
strReturn_Value += STRING.charAt(iCounter).toUpperCase();
}
else{
strReturn_Value += STRING.charAt(iCounter);
}
var iChar = STRING.charCodeAt(iCounter);
if(iChar == 46 || iChar == 33 || iChar == 63){
	if(STRING.charCodeAt(iCounter+1)==32){
	strReturn_Value += (STRING.substring(iCounter+1,iCounter+2)) + " ";
	iCounter+=1;
	}

	UcaseNext = true;

}
else if(iChar==13 && STRING.charCodeAt(iCounter+1)==10){
	iCounter+=1;
	//Comment out the next line if you want to stop capitalization on a new line
	UcaseNext = true;
}
else if(iChar==44 && STRING.charCodeAt(iCounter+1)!=32){
strReturn_Value += (STRING.substring(iCounter,iCounter)) + " ";
}
else{
UcaseNext = false
}
if(iChar == 99 || iChar == 67){
if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109){
UcaseNext = true;
}
}


} //End For

return strReturn_Value;
} //End Function


function toProperCase(s)
{
  return s.toLowerCase().replace(/^(.)|\s(.)/g, 
          function($1) { return $1.toUpperCase(); });
}