<!-- Hide script from older browsers
// This file contains form validation functions.
// To include it in a webpage: <script src="validation.js"></script>
// Make sure the file path is correct

// checks that the email is valid. if valid, returns false. if invalid, returns true.
function isInvalidEmail(email) {
	// if blank, treat as valid
	if (email.value == "") {
		return false;
	}
	
	// get position of @ character from beginning of string
	var atPos = email.value.indexOf("@");

	// if there is no @, or no characters before the @, it is invalid
	if (atPos < 1) {
		return true;
	}
	
	// get position of last @. if it's not the same as the first @, there are 2 @s, and the email is invalid
	var atCheck = email.value.lastIndexOf("@");
	if (atPos != atCheck) {
		return true;
	}
	
	// make sure there are not 2 adjacent periods
	if (email.value.indexOf("..") >= 0) {
		return true;
	}

	// get position of the last period
	var perPos = email.value.lastIndexOf(".");
	
	// if no period, or no period at least one char after the @ sign, invalid
	if (perPos == -1 || perPos < (atPos+2)) {
		return true;
	}
	
	// extract the domain extension (all chars after the final period)
	var extension = email.value.substring(perPos+1);

	// if extension is less than 1 char long, invalid
	if (extension.length < 1) {
		return true;
	}
	
	// if there are any spaces in the email address, invalid
	if (email.value.indexOf(" ") >= 0) {
		return true;
	}

	return false;
}

function isEmpty(field)
{
	// returns true if field is empty, false if not empty
	var blanks = 0;
	for (i=0; i<field.value.length; i++)
	{
		if (field.value.charAt(i) == " ")
			blanks++;
	}

	if (field.value.length == blanks)
		return true;
	else
		return false;
}

function isNotEmpty(field)
{
	// returns true if field is not empty, false if empty
	var blanks = 0;
	for (i=0; i<field.value.length; i++)
	{
		if (field.value.charAt(i) == " ")
			blanks++;
	}

	if (field.value.length == blanks)
		return false;
	else
		return true;
}

function isNotNumeric(field)
{
	// returns true if field contains a non-numeric character, false if field is all numeric
	for (i=0; i<field.value.length; i++)
	{
		if ((field.value.charAt(i) < "0")||(field.value.charAt(i) > "9"))
		{
				return true;
		}
	}
	return false;
}
function isNotFloat(field)
{
	// returns true if field contains more than one period, or any non-numeric data
	var periodFound = false;
	
	for (i=0; i<field.value.length; i++)
	{
		if ((field.value.charAt(i) < "0")||(field.value.charAt(i) > "9"))
		{
			if (field.value.charAt(i) == "." && !periodFound) {
				periodFound = true;
			} else {
				return true;
			}
		}
	}
	return false;
}
function isNotCurrency(field)
{
	// returns true if a field contains more than one period, more than 2 numbers after the period, or any non-numeric data
	var periodFound = false;
	for (i=0; i<field.value.length; i++)
	{
		if ((field.value.charAt(i) < "0")||(field.value.charAt(i) > "9"))
		{
			if (field.value.charAt(i) == "." && !periodFound) {
				periodFound = true;
			} 
			else if (field.value.charAt(i) != ",") {
				return true;
			}
		}
	}
	if (periodFound) {
		var perPos = field.value.indexOf(".");	
		if (i > (perPos+3))
			return true;
	}
	return false;	
}
function invalidDate(intYear, intMonth, intDay, fieldcaption)
{
	// This function validates a date. If invalid, shows error message and returns true. If valid, returns false.
	// It does not check if the date is empty. However, if only part of the date fields are filled in, it will show an error message and return false.
	// fieldcaption = the caption of the field they are filling in.
	
	// check if only part of the date is filled in
	if ((intDay == 0 && (intMonth > 0 || intYear > 0)) || (intMonth == 0 && (intDay > 0 || intYear > 0)) || (intYear == 0 && (intDay > 0 || intMonth > 0))){
		alert("Invalid " + fieldcaption + " Date.");
		return true;
	}
	//  check if they chose day 31 for a month that only has 30 days
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30)) {
		alert("The month you selected for " + fieldcaption + " has only 30 days.");
		return true;
	}
	// if they chose February, validate the day. If leap year, may not be greater than 29. If not leap year, may not be greater than 28
	if (intMonth == 2) {
		var leapyear = 0;
		if (intYear % 100 == 0) {
			if (intYear % 400 == 0) {
				leapyear = 1;
			}
		} else if (intYear % 4 == 0) {
			leapyear = 1;
		}
		if (leapyear == 1) {
			if (intDay > 29) {
				alert("The month you selected for " + fieldcaption + " has only 29 days.")
				return true;
			}
		} else {
			if (intDay > 28) {
				alert("The month you selected for " + fieldcaption + " has only 28 days.");
				return true;
			}
		}
	}
	return false;
}
// checks that a New password is valid. if valid, returns false. if invalid, returns true.
function PasswordCheck(password,minlength) {
	var message = '';
	if (password.value.length < minlength) {
		message = 'Your Password must be at least ' + minlength + ' characters!\nPlease enter a different password.';
		return message;
	}

	var alphaflag = 0;
	var numericflag = 0;
	for (i=0; i<password.value.length; i++){
		if ((password.value.charAt(i) < "0")||(password.value.charAt(i) > "9")){
			alphaflag = 1;
		} else {	
			numericflag = 1;
		}
	}
	if (alphaflag == 0) {
		message = 'Your Password must contain at least one letter (a-z).\nPlease enter a different password.';
		return message;
	}
	if (numericflag == 0) {
		message = 'Your Password must contain at least one numeric character (1-9).\nPlease enter a different password.';
		return message;
	}
}
// End hide script -->
