// validate form fields, check required fields are filled in
//alert('file loaded');

function checkform(theform){
	//alert(theform.txtslrfirst.value);
	var errormsg = "";
	errormsg += valname(theform.name.value);
	errormsg += checkPhone(theform.homephone.value);
	errormsg += checkEmail(theform.contact_email.value);
	if (!theform.tos.checked) {
		errormsg += "Please check the box accepting that submission of the form does not create an attorney-client relationship with Brayton Purcell.";
	}
	//check to see if errors--if so, display error message
	if (!(errormsg == "")){
		var strerrormsg = "Please correct the following items and resubmit your form:\n"
		strerrormsg = strerrormsg + errormsg
		alert(strerrormsg);
		return false;
	}
}
// validate e-mail
function checkEmail (strng) {
	//alert(strng);
	var error="";
	if ((strng == "") || (strng == null)) {
		//alert("email");
   		error = "Please enter an e-mail address.\n";
		return error;
	}
	//test valid e-mail format
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
	   return error;
    }
    else {
	//test email for illegal characters
       var illegalChars= /[\&\s\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          	error = "The email address contains illegal characters.\n";
		 	return error;
       }
    }
	return error;    
}

// full name, required, alpha characters only
function valname(strng){
	var error = "";
	if (strng == ""){
		error = "Please enter your name.\n";
		return error;
	}
	//test for numbers
    var illegalChars= /\d/
	if (strng.match(illegalChars)){
		error = "Your name contains numbers."
		return error;
	}
	return error;
}

//validate phone
function checkPhone (strng) {
	//alert("phone");
	var error="";
	if ((strng == "") || (strng == null)) {
		error = "Please enter a phone number so we may contact you.\n";
		return error;
	}
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(stripped)) {
		error = "Please enter a valid phone number, including area code, so we may contact you.\n";
	}
	if (stripped.length != 10) {
		error = "Please enter a valid phone number, including area code, so we may contact you.\n";	
	}
	return error;
}

