// JavaScript Form Validation

function formValidator(){
	// Make quick references to our fields
	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');
	var company = document.getElementById('company');
	var addr = document.getElementById('addr');
	var state = document.getElementById('state');
	var postcode = document.getElementById('postcode');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	var financecontact = document.getElementById('financecontact');
	var financeemail = document.getElementById('financeemail');
	var liasoncontact = document.getElementById('liasoncontact');
	var liasonemail = document.getElementById('liasonemail');
	var aimia = document.getElementById('aimia');
	
	// Check each input in the order that it appears in the form!
	if(notEmpty(firstname, "Please enter your first name.")){
		if(notEmpty(lastname, "Please enter your surname.")){
			if(notEmpty(company, "Please enter your company name.")){
				if(notEmpty(addr, "Please enter your address.")){
					if(madeSelection(state, "Please select your state.")){	
						if(isNumeric(postcode, "Please enter a valid postcode.")){
							if(emailValidator(email, "Please enter a valid email address.")){
								if(notEmpty(phone, "Please enter a valid phone number.")){
									if(notEmpty(financecontact, "Please enter the name of a financial contact.")){
										if(emailValidator(financeemail, "Please enter the financial contact's email address.")){
											if(notEmpty(liasoncontact, "Please enter the name of a Liason to contact.")){
												if(emailValidator(liasonemail, "Please enter a Liason's email address.")){
													if(madeSelection(aimia, "Please confirm if you are an AIMIA member.")){	
														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 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 == "Select"){
		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;
	}
}