
//Validates the Custom Brew Formulation Form
function validateBrewForm(theForm) {
	var msg = "";
	msg += isEmpty(theForm.firstname.value,"First Name");
	msg += isEmpty(theForm.lastname.value,"Last Name");
	msg += isEmpty(theForm.phone.value,"Phone Number");
	msg += checkEmail(theForm.email.value);
	msg += isEmpty(theForm.company.value,"Company");
	msg += checkDropdown(theForm.flavor.value,"Flavor Option");
	msg += checkDropdown(theForm.application.value,"Application");
	msg += checkDropdown(theForm.processing.value,"Processing Requirement");
	if (msg != "") {
		alert(msg);
		return false;
	}
	return true;
}


function checkEmail (strng) {
	var error="";
	if (strng == "") {
		error = "Please enter an email address.\n";
	}
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) {
		error = "Please enter a valid email address.\n";
	}
	else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) {
			error = "Please enter a valid email address.\n";
		}
	}
	return error;
}


// phone number - strip out delimiters and check for 10 digits
function checkPhone(strng) {
	var error = "";
	if (strng == "") {
		error = "Please enter a phone number.\n";
	}
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
		error = "Please enter a valid phone number.\n";
	}
	if (!(stripped.length == 10)) {
		error = "The phone number is the wrong length. Make sure you included an area code.\n";
	}
	return error;
}


// non-empty textbox
function isEmpty(strng,fieldname) {
	var error = "";
	if (strng.length == 0) {
		error = fieldname + " has not been filled in.\n"
	}
	return error;
}

// valid selector from dropdown list
function checkDropdown(choice,fieldname) {
	var error = "";
	if (choice == 0) {
		error = "You didn't choose an option from the drop-down list for " + fieldname + ".\n";
	}
	return error;
}
