// checkform.js - 10 October 2000 - MP
// JavaScript functions for checking data entered into a form

// Valid characters:
var letters = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var specials = "àáèéìíòóùúÀÁÈÉÌÍÒÓÙÚ";
var digits = "0123456789";
var nameString = letters + specials + "-_. '";
var usernameString = letters + digits + "_";
var emailString = letters + digits + "-_.@";
var questionString = nameString + digits + "?"

// Date Arrays:
var monthArray = new Array("January", "February", "March", "April", "May", "June", "July",
                           "August", "September", "October", "November", "December");
var dayArray = new Array("31", "29", "31", "30", "31", "30", "31", 
                         "31", "30", "31", "30", "31");

// Error Messages:								 
var emailMsg = "Please enter your email address in the form: mary@eumom.com.";
var firstnameMsg = "Please enter your first name, using the letters A-Z.";
var lastnameMsg = "Please enter your last name, using the letters A-Z.";
var usernameMsg = "Please enter a username, using the letters A-Z, the numbers 0-9 and the underscore character.";
var passwordMsg = "Please enter a password, using the letters A-Z, the numbers 0-9 and the underscore character.";
var confirmMsg = "Oops! Your password and password confirmation do not match, try again.";
var questionMsg = "Please enter a secret question using the letters A-Z and the numbers 0-9";
var answerMsg = "Please enter the answer to your secret question using the letters A-Z and the numbers 0-9";
var pollMsg = "You must choose an option";
						
function validDate(f, isOptional) {
	// Checks if the date entered in the form is valid.
	// Year is not specified, cannot check for Leap Year and assumes 29 days in Feb.
	// isOptional indicates if a blank date is ok.
					  					  
		if (eval(f.day.selectedIndex) > dayArray[eval(f.month.selectedIndex - 1)]) {
			alert("Oops - There are only " + dayArray[eval(f.month.selectedIndex - 1)] + " days in " + monthArray[eval(f.month.selectedIndex - 1)] + ".\nPlease try again.");
			f.day.focus();
			return false;
		}
		return true;
	 	 
}

function isEmpty(elem) {
	// Checks if a form element is empty:
	return ((elem.value == null) || (elem.value.length == 0))
}

function isValidPoll(form, noOptions) {
	// Checks if a form element is empty:
	validPoll = false;
	for (var i=0; i<noOptions; i++) {
		if (form.poll[i].checked) {
			validPoll = true;
			form.submit();
		}
	}
	if (validPoll) {
		form.submit();		
	}
	else {
		alert (pollMsg);
	}
}

function validText(elem, range, msg, isOptional) {
	// Checks if the text entered is within a valid range of characters.
	// Alerts the user if this is not the case. 
	// isOptional indicates if the field can be left blank.
 
	var isOK = true;
	if (isEmpty(elem)) {
		if (! isOptional) {
			alert(msg);
			elem.focus();
			return false;
		} else {
			return true;
		}	
	} else { 
		for (var i = 0; i < elem.value.length; i++) {
			isOK = isOK && (range.indexOf(elem.value.charAt(i)) != -1);
		}
		if (! isOK) {
			alert(msg);
			elem.focus();
		}
		return isOK;
	} 
}

function validEmail(elem, range, msg, isOptional) {
	// Checks if the text entered is within a valid range of characters and that the text 
	// contains a single "@". Alerts user if this is not the case. 
	// isOptional indicates if the field can be left blank.
	
	var isOK = true;
	var atSign = 0;	
	if (isEmpty(elem)) {
		if (! isOptional) {
			alert(msg);
			elem.focus();
			return false;
		} else {
			return true;
		}	
	} else { 
		for (var i = 0; i < elem.value.length; i++) {
			isOK = isOK && (range.indexOf(elem.value.charAt(i)) != -1);
			if (elem.value.charAt(i) == "@") {
				atSign++;
			} 
		}
		if ((! isOK) || (atSign != 1)) {
			alert(msg);
			elem.focus();
			return false;
		}
	}
	return true;
}

function checkPasswords(elem1, elem2, msg) {
	// Checks that elem1 and elem2 have the same value, otherwise
	// displays an error message.
     
	if ((validText(elem1, usernameString, passwordMsg, false)) &&
		(validText(elem2, usernameString, passwordMsg, false))) {
			if (elem1.value == elem2.value) {
				return true;
			} else {
				alert(msg);
				elem1.value = "";
				elem2.value = "";
				elem1.focus();
				return false;
			}
	}
}

function checkLength(elem, minLength, maxLength) {
	// Checks that the value of elem has the correct number of characters.
	if ((elem.value.length >= minLength) && (elem.value.length <= maxLength)) {
		return true;
	} else {
		alert("Please enter a value for '" + elem.name + "' with between " + minLength + " and " + maxLength + " characters.");
		elem.focus();
		return false;
	}
}
