
/* JavaScript functions for validating form */

function validData(contact){
	
	var error_string = "";
	

	// check the name fields
	if ((document.contact.first_name.value == '')|| (document.contact.first_name.value.length <=1)){
		error_string += "Missing or invalid first name.\n";
	}
	
	if ((document.contact.last_name.value == '')|| (document.contact.last_name.value.length <=1)){
		error_string += "Missing or invalid last name.\n";
	}
	
	//check address field
	if ((document.contact.address1.value =='')||(document.contact.address1.value.length <=1)){
		error_string += "Missing or invalid address.\n";
	}
	
	//check city field
	if ((document.contact.city.value =='')||(document.contact.city.value.length <=1)){
		error_string += "Missing or invalid city.\n";
	}
	
	//check state information
	if(document.contact.state.selectedIndex <0){
		error_string += "Missing or invalid state.\n";
	}
	
	//check zipcode
	
		if (checkZip(document.contact.zip.value)== true){
			error_string += "Missing or invalid zipcode.\n";
		}
		
	
	//check phone
		
		if (checkPhone(document.contact.phone.value) == true){
			error_string += "Missing or invalid phone number.\n";	
		}	
	
	//check e-mail
		
		if (checkEmail(document.contact.email.value) == true){
			error_string += "Missing or invalid e-mail address.\n";	
		}
	
	if(error_string ==""){
		return true;
	}
	
	else{
		error_string = "We found the following omissions in your form: \n" +error_string;
		alert(error_string);
		return false;
	}
}	
	
	
function checkEmail(addy){

	var emailFilter = /^[a-z][\w\.]*@[\w\.]+\.[a-z]{2,3}/i;
	var illegalChars = /[\(\)\<\>\,\;\:\\\"[\]]/;
	
	
	if((addy == "") || (!(emailFilter.test(addy))) || (addy.match(illegalChars))){
		return true;
	}
}

function checkZip(zipcode){
	var i;
	var legalChars = "0123456789()-+ " 
	
	if((zipcode=="") || (!(zipcode.length<=10))|| (!(zipcode.length>=5))){
		return true;
	}
	
	for (i =0; i <= zipcode.length -1; i++) {
		if (legalChars.indexOf(zipcode.charAt(i)) == -1) {
		
		return true;
		}
	}
}
		


function checkPhone(numString){
	
	var i;
	var legalChars = "0123456789()-+ " 
	
	if((numString=="") || (numString<7) || (numString >19)){
		return true;
	}
	
	for (i =0; i <= numString.length -1; i++) {
		if (legalChars.indexOf(numString.charAt(i)) == -1) {
		
		return true;
		}
	}
}


