<!--
	/*	Form validation functions.
	All functions require three arguments(the form-field object, the name of the field, 
	and a true/false flag denoting if a field is required (true) or optional (false).
	True is assumed to be 1, optional is assumed to be 2.
	The CheckTextAreaLength function requires a fourth argument: maximum length of the textarea. */

	function CheckEmail (field, name, required) {
		// Checks to make sure e-mail address has the correct form.
		var errStr = "";
		if(field.value == "" && required == true)
			errStr = name + " is a required field, but was left empty.\n";
		if(field.value != ""){
			regEmail = /^[\w-]+([\w-]|\.[\w-])*@[\w-\.]+\.[\w]+(\.[\w-])*$/;
			if(! regEmail.test(field.value))
				errStr += name + " is an invalid e-mail address.  It must be of the form yourname@yourdomain.com.\n";
		}
		return errStr;
	}

	function CheckText (field, name, required) {
		// Accepted characters: letters, numbers, space and ! & ( ) - : ; , . ? # ' "
		var errStr = "";
		if(field.value == "" && required == true)
			errStr += name + " is a required field, but was left empty.\n";
		if(field.value != ""){
			regText = /[^\w!&\(\)\-:;\,\.\? #'"]/g;
			if(regText.test(field.value)){
				errStr += name + " contains invalid text.  Only letters, numbers, and punctuation symbols are allowed.\n";
			}
		}
		return errStr;
	}

	function CheckPhoneNumber (field, name, required) {
		// Accepted characters: numbers, space and - ( ) . 
		var errStr = "";
		if(field.value == "" && required == true)
			errStr += name + " is a required field, but was left empty.\n";
		if(field.value != ""){
			regPhone = /[^\d-\(\)\. ]/;
			if(regPhone.test(field.value))
				errStr += name + " contains invaild text.  Only numbers and ( ) - . are allowed.\n";
		}
		return errStr;
	}

	function CheckNumber (field, name, required) {
		// Checks for a correct positive number.  Decimals allowed.
		var errStr = "";
		if(field.value == "" && required == true)
			errStr += name + " is a required field, but was left empty.\n";
		if(field.value != ""){
			regNumber = /^-*\d+\.?\d*$/;
			if(! regNumber.test(field.value))
				errStr += name + " contains invalid text.  Only numbers are allowed.\n";
		}
		return errStr;
	}
	
	function CheckDate (field, name, required) {
		// Checks to see if dates conform to mm/dd/yyyy format
		// Also checks that 1 <= dd <= 31, 1 <= mm <= 12, 0 < yyyy
		var errStr = "";
		var leapYear;
		var correct;	// Flag to determine if numbers in dates are correct
		if(field.value == "" && required == true) 
			errStr += name + " is a required field, but was left empty.\n";
		if(field.value != ""){
			regDate = /(\d|\d\d)[-|\/](\d|\d\d)[-|\/](\d\d\d\d)/;
			var correct = true;
			if (regDate.exec(field.value)){
				matches = regDate.exec(field.value);
				// Determine if this is a leap year.
				// It is a leap year if the year is divisble by four, except if the year is a century.
				// If the year is a century, it must be divisble by 400 (1900 isn't a leap year, but 2000 is)
				if ((matches[3] % 4 == 0 && matches[3] % 100 != 0) || (matches[3] % 4 == 0 && matches[3] % 100 == 0 && matches[3] % 400 == 0)){
					leapYear = true;
				} else {
					leapYear = false;
				}
				if (matches[2] >= 1 && matches[2] <= 31){ // If the second number is in the valid range for all months, check specific months.
					// If it is April, June, September, or November, day shouldn't be 31 (there are only 30 days in those months)
					if ((matches[1] == 4 || matches[1] == 6 || matches[1] == 9 || matches[1] == 11) && (matches[2] == 31)){
						errStr += "The day in the " + name + " is incorrect.\n";
					} else {	// Else 
						if (matches[1] == 2 && matches[2] > 28){	// If it's February and there are more than 28 days, make sure its a leap year
							if(leapYear == true && matches[2] == 29){
								// Do nothing.  Date is correct.
							} else {	// Date is incorrect.
									errStr += "The day in the " + name + " is incorrect.\n";
							}
						}
						// If it's not February, or it is February and there are less than 28 days in the date, the date is correct
					}
				} else {
					errStr += "The day in the " + name + " is incorrect.\n";
				}

				if (matches[1] < 1 || matches[1] > 12 ) // If the first number isn't a valid month
					errStr += "The month in the " + name + " is incorrect.\n";

				if (matches[3] < 0) // If the third number isn't a valid year.
					errStr += "The year in the " + name + " is incorrect.\n";
			} else {
				errStr += name + " is incorrect.  It must be of the form mm/dd/yyyy.\n";
			}
		}
		return errStr;
	}
	
	function CheckUsername (field, name, required){
		// Accepted characters: letters, numbers, underscore, dash
		var errStr = "";
		if(field.value == "" && required == true)
			errStr += name + " is a required field, but was left empty.\n";
		if(field.value != ""){
			regUsername = /[^\w\-]/g;
			if(regUsername.test(field.value))
				errStr += name + " contains invalid characters.  Only letters, numbers, '-', and '_' are allowed.\n";
		}
		return errStr;
	}
	
	function CheckPassword(field, name, required){
		// Checks to see if password has been entered.  DOES NOT VALIDATE PASSWORD!
		// If a password is required, this will return an error if password is empty.
		// Essentially it checks that a required field has a value in it.
		var errStr = "";
		if(field.value == "" && required == true)
			errStr += name + " is a required field, but was left empty.\n";
		return errStr;
	}
	
	function CheckLength(field, name, required, length){
		//Checks the length of a textarea so that it is length characters long.
		//If it is too long, the user is asked to shorten it.
		var errStr = "";
		textArea = new String;
		textArea = field.value;
		if(field.value == "" && required == true)
			errStr += name + " is a required field, but was left empty.\n"
		if(textArea.length > length)
			errStr += name + " has " + textArea.length + " characters but should not contain more than " + length + ".\n"
		return errStr;		
	}
//-->