// JavaScript Document

function formValidator(){
	// Make quick references to our fields
	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');
	var age = document.getElementById('age');
	var homeaddr = document.getElementById('homeaddr');
	var city = document.getElementById('city');
	var province = document.getElementById('province');
	var postal = document.getElementById('postal');
	var homephone = document.getElementById('homephone');
	var email = document.getElementById('email');
	var position1 = document.getElementById('position1');
	var position2 = document.getElementById('position2');
	var position3 = document.getElementById('position3');
	var position4 = document.getElementById('position4');
	var position5 = document.getElementById('position5');
	var position6 = document.getElementById('position6');
	var position7 = document.getElementById('position7');
	var position8 = document.getElementById('position8');
	var position9 = document.getElementById('position9');
	var employername = document.getElementById('employername');

	// Check each input in the order that it appears in the form!
	if(isAlphabet(firstname, "Please use only Letters for your First name")){
		if(isAlphabet(lastname, "Please use only Letters for your Last name")){
			if(isNumeric(age, "Please use only Numbers for your Age")){
				if(isAlphanumeric(homeaddr, "Please use only Numbers and Letters for your Home Address")){
					if(isAlphanumeric(city, "Please use only Numbers and Letters for your City")){
						if(madeSelection(province, "Please Choose a Province")){
							if(isAlphanumeric(postal, "Please use only Numbers and Letters for your Postal Code")){ 
								if(isNumeric(homephone, "Please use only Numbers and Letters for your Phone Number")){
									if(emailValidator(email, "Please Enter a Valid Email Address")){
										//if(CheckBoxes(position+v, "Please Choose a Position to Apply For")){
											//if(isAlphabet(employername, "Please Choose a Position to Apply For")){
											return true;
											//}
										//}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	
	return false;
	
}
function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		//elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function CheckBoxes(elem, helperMsg){
	 if(elem.checked == false){
		alert(helperMsg);
		//elem.focus();
		return false;
	}
		return true;
}

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\.\ \-]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		//elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z\.\ \-]+$/;
	if(elem.value.match(alphaExp)){
		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 madeSelection(elem, helperMsg){
	if(elem.value == "None Selected"){
		alert(helperMsg);
		//elem.focus();
		return false;
	}else{
		return true;
	}
}

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;
	}
}

