// JavaScript Form Validation document used for all Forms

var elemColor = "#ffc";

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.style.backgroundColor = elemColor;
		elem.focus();
		return false;
	}
	elem.style.backgroundColor = "";
	return true;
}

function isNumeric(elem, helperMsg, numExp){
	if(elem.value.match(numExp)){
		elem.style.backgroundColor = "";
		return true;
	}else{
		alert(helperMsg);
		elem.style.backgroundColor = elemColor;
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg, alphaExp){
	if(elem.value.match(alphaExp)){
		elem.style.backgroundColor = "";
		return true;
	}else{
		alert(helperMsg);
		elem.style.backgroundColor = elemColor;
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg, alphaNumExp){
	if(elem.value.match(alphaNumExp)){
		elem.style.backgroundColor = "";
		return true;
	}else{
		alert(helperMsg);
		elem.style.backgroundColor = elemColor;
		elem.focus();
		return false;
	}
}

function matchFields(elem1, elem2, helperMsg){
	if (elem1.value != elem2.value){
		alert(helperMsg);
		elem1.style.backgroundColor = elemColor;
		elem2.style.backgroundColor = elemColor;
		elem1.focus();
		return false;
	}
	elem1.style.backgroundColor = "";
	elem2.style.backgroundColor = "";
	return true;
}

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

function madeSelection(elem, helperMsg){
	if(elem.value == "Select One"){
		alert(helperMsg);
		elem.style.backgroundColor = elemColor;
		elem.focus();
		return false;
	}else{
		elem.style.backgroundColor = "";
		return true;
	}
}

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

