function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isZipCode(elem, helperMsg){
	var zip = /^[0-9]{5,5}$/;
	if(elem.value.match(zip)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphanumExp = /^[0-9a-zA-Z\s]+$/;
	if(elem.value.match(alphanumExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isStateAbbreviation(elem, helperMsg){
	var stateAbbrev = /^[a-zA-Z]{2,2}$/;
	if(elem.value.match(stateAbbrev)){
		return true;
	}
	else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isPhoneNumber(elem, helperMsg){
	var phoneNum = /^\(\d\d\d\)\d\d\d-\d\d\d\d$/;
	if(elem.value.match(phoneNum)){
		return true;
	}
	else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isMatching(elem1, elem2, helperMsg){
	if(elem2.value == elem1.value){
	return true;
	}
	else{
	alert(helperMsg);
	elem2.focus();
	return false;
	}
}



function formValidator(){
	// Make quick references to our fields
	var FRCTeamNumber = document.getElementById('FRCTeamNumber');
	var TeamAffiliation = document.getElementById('TeamAffiliation');

	var Contact1Name = document.getElementById('Contact1Name');
	var Contact1Email = document.getElementById('Contact1Email');
	var Contact1Phone = document.getElementById('Contact1Phone');
	
	var Contact2Name = document.getElementById('Contact2Name');
	var Contact2Email = document.getElementById('Contact2Email');
	var Contact2Phone = document.getElementById('Contact2Phone');
	
	var StreetAddress = document.getElementById('StreetAddress');
	var City = document.getElementById('City');
	var State = document.getElementById('State');
	var ZipCode = document.getElementById('ZipCode');
	var CheckPayableTo = document.getElementById('CheckPayableTo');
	
	
	// Check each input in the order that it appears in the form!
if(isNumeric(FRCTeamNumber, "Please enter your team number.")){
 	if(isAlphanumeric(TeamAffiliation, "Please enter your team affiliation.")){
		if(isAlphabet(Contact1Name, "Please enter your first contact\'s name")){
			if(emailValidator(Contact1Email, "Please enter your first contact\'s email")){
				if(isPhoneNumber(Contact1Phone, "Please enter your first contact\'s phone number in this format: \(123\)456-7890.")){
					if(isAlphabet(Contact2Name, "Please enter your second contact\'s name")){
						if(emailValidator(Contact2Email, "Please enter your second contact\'s email")){
							if(isPhoneNumber(Contact2Phone, "Please enter your second contact\'s phone number in this format: \(123\)456-7890.")){
								if(isAlphanumeric(StreetAddress, "Please enter a street address.")){
									if(isAlphabet(City, "Please enter a city")){
										if(isStateAbbreviation(State, "Please enter a state.")){
											if(isZipCode(ZipCode, "Please enter a zip code.")){
												if(isAlphanumeric(CheckPayableTo, "Please enter to whom the check is payable to")){
													return true;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
return false;
}
