//Form validation library
function dispatcher(validationFunc){
	this.doValidate = validationFunc;
}

var dispatchLookup = new Array();
dispatchLookup["isNotEmpty"] = new dispatcher(isNotEmpty);
dispatchLookup["isPositiveInteger"] = new dispatcher(isPositiveInteger);
dispatchLookup["isValidEmail"] = new dispatcher(isValidEmail);
dispatchLookup["isClean"] = new dispatcher(isClean);

//Global variables=====================
var gField

//=====================================


function validate(field, fieldname, method){
	var vValue;
	gField = field;
	var args = validate.arguments;
	for (var i = 2; i < args.length; i++){
		if (gField.type.substring(0, 6) == "select"){
			if (gField.selectedIndex > -1)
				vValue = gField.options[gField.selectedIndex].text;
		} else {
			vValue = gField.value;
		}
		if (!dispatchLookup[args[i]].doValidate(vValue, fieldname)){
			return false;
		}
	}
	return true;
}

//Validation Functions (add more as needed)
function isNotEmpty(inputStr, fieldname){
	if ((inputStr == null) || (inputStr == "")) {
		alert(fieldname + " is required.");
		gField.focus();
		if (gField.select)
			gField.select();
		return false;
	}
	return true;
}

function isPositiveInteger(inputVal, fieldname){
	var inputStr = inputVal.toString();
	for (var i=0; i< inputStr.length; i++){
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9"){
			alert(fieldname + " must be a positive whole number");
			gField.focus();
			if (gField.select)
				gField.select();
			return false;
		}
	}
	return true;

}

function isClean(inputVal, fieldname){
	var inputStr = inputVal.toString();
	for (var i=0; i< inputStr.length; i++){
		var oneChar = inputStr.charAt(i);
		if ((oneChar < "0" || oneChar > "9") && (oneChar < "A" || oneChar > "Z") && (oneChar < "a" || oneChar > "z") && (oneChar != "_") && (oneChar != "-")){
			alert(fieldname + " contains invalid characters");
			gField.focus();
			if (gField.select)
				gField.select();
			return false;
		}
	}
	return true;

}

function isValidEmail(inputStr, fieldname){
	var bValid = false;
	var atIndex = inputStr.indexOf("@")
	var dotIndex = inputStr.lastIndexOf(".")
	
	if ((inputStr.length >= 7) && (atIndex > -1) && (dotIndex > -1)){
		if (((inputStr.length - atIndex) >= 5) && (atIndex == inputStr.lastIndexOf("@"))){
			if ((inputStr.length	- dotIndex) >= 2) {
				bValid = true;
			}
		}
	}
	if (bValid == false){
		alert(inputStr + " is not a valid email address.");
		gField.focus();
		if (gField.select)
			gField.select();
		return false;
	}
	return bValid;
	
}

function validateMonthDay(month, day, year, dayField){
	var mm = parseInt(month);
	var dd = parseInt(day);
	var yyyy = parseInt(year);
	
	//best if they enter 4-digit year, but otherwise convert to four digit year
	if (yyyy < 100){
		if (yyyy >= 50) {
			yyyy += 1900;
		} else {
			yyyy += 2000;
		}
	}
	
	var months = new Array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
	if ((mm == 4 || mm==6 || mm==9 || mm==11) && dd > 30) {
		alert(months[mm] + " has only 30 days.")
		dayField.focus();
		if (dayField.select)
			dayField.select();
		return false;
	} else if (mm==2){
		if ( ( (yyyy % 4 > 0) && (dd > 28) ) || ( (yyyy % 100 == 0) && (yyyy % 400 > 1) && (dd > 28) ) ){
			alert("February of " + yyyy + " has only 28 days.")
			dayField.focus();
			if (dayField.select)
				dayField.select();
			return false;
		} else if (dd > 29) {
			alert("February of " + yyyy + " has only 29 days.")
			dayField.focus();
			if (dayField.select)
				dayField.select();
			return false;
		}
	}
	return true;

}

function textAreaMaxLength(field, fieldname, maxlength){
	var tmpText = field.value
	var regExp = /'/
	tmpText.replace(regExp, "''")
	var fieldLength = tmpText.length
	if (fieldLength > maxlength){
		alert( fieldname + " has a maximum length of " + maxlength + " characters. You have entered a " + fieldname + " that is " + fieldLength + " characters long.")
		field.focus();
		return false;
	}
	return true;
}