var validNameChars = "abcdefghijklmnopqurstuvwxyz1234567890-'`. ";

var firstNameMessage = "Please enter your first name.";
var lastNameMessage = "Please enter your last name.";
var zipCodeMessage = "Please enter your 5-digit ZIP code.";
var emailMessage = "Please enter your e-mail address.";
var buyMessage = "Please select when you think you will buy a new vehicle.";

function checkForm() {
	var validForm = true;
	var errorMessage = "";
	if (!val_name(document.brochureForm.STFN_490.value, validNameChars)) {
		errorMessage = errorMessage + firstNameMessage + "\n";
		validForm = false;
	}
	if (!val_name(document.brochureForm.STFN_491.value, validNameChars)) {
		errorMessage = errorMessage + lastNameMessage + "\n";
		validForm = false;
	}
	if (!val_zip(document.brochureForm.STFN_492.value)) {
		errorMessage = errorMessage + zipCodeMessage + "\n";
		validForm = false;
	}
	if (!val_email(document.brochureForm.STFN_493.value)) {
		errorMessage = errorMessage + emailMessage + "\n";
		validForm = false;
	}
	if (document.brochureForm.STFN_502.options[document.brochureForm.STFN_502.selectedIndex].value == "") {
		errorMessage = errorMessage + buyMessage + "\n";
		validForm = false;
	}
	if (!validForm) {
		alert(errorMessage);
	}
	return validForm;
}

// validation for email format
function val_email(item) {
	var str = item;	
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var valid = true;
	if (re.test(str) == false) {
		valid = false;
	}
	return valid;
}

// tests zip code
function val_zip(str) {
	var valid = false;
	var digits=0;
	for (var i = 0; i < str.length; i++) {
		if (parseInt(str.charAt(i),10) > -1 
			|| parseInt(str.charAt(i),10) < 10) {
			digits++;
		}
	}
	if(digits == 5){
		valid = true;
	}
	return valid;
}

// tests zip code
function val_phone(str) {
	var valid = false;
	var digits=0;
	for (var i = 0; i < str.length; i++) {
		if (parseInt(str.charAt(i),10) > -1 
			|| parseInt(str.charAt(i),10) < 10) {
			digits++;
		}
	}
	if(digits == 10){
		valid = true;
	}
	return valid;
}

// validation for name strings
function val_name(item,list){
	var valid = false;
	// make sure there is something other than space
	for(var i=0; i<item.length; i++){
		if(item.charAt(i) != ' ') valid = true;
	}
	// make sure all the characters are in the acceptable set
	for(var i=0; i<item.length; i++){
		if(!val_char(item.charAt(i),list)) valid = false;
	}
	return valid;
}

// tests whether a given character is part of a given string
function val_char(chr,list){
	var valid = false;
	for(var i=0; i<list.length; i++){
		if(chr.toLowerCase() == list.charAt(i) ||
		  (chr == " " && list.charAt(i) == " "))
		{
			valid = true;
		}
	}
	return valid;
}

// tests whether the submittes string is all spaces
function all_space(str){
	var valid = true;
	for(var i=0; i<str.length; i++){
		if(str.charAt(i) != ' ') valid = false;
	}
	return valid;
}
