//		scripts/form.js
//

// A utility function that returns true if a string contains only
// whitespace characters.
function isblank(e){
	if (e.value == null || e.value == "") {
		return true;}

	for(var i = 0; i < e.value.length; i++)	 {
		var c = e.value.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) {
			e.focus();
			return false;}
		}
	return true;
	}

// Checks if an optional field is blank
function checkblank(e) {
	if (isblank(e))	  {
		alert("The field " + e.description + " must be filled in.");
		e.focus();
		return false;
		}
	return true;
	}

// Checks if a field is numeric.
// If the optional min property is set, it checks it is greater than
// its value
// If the optional max property is set, it checks it is less than
// its value
function checknumber(e) {

	var v = parseFloat(e.value);

	if (isNaN(v))  {
		alert("The field " + e.description + " must be a number");
		e.focus();
		return false;
		}
	else {
		if ((e.minNumber != null) && (v < e.minNumber))	 {
			alert("The field " + e.description +
				" must be greater than or equal to " + e.minNumber);
			e.focus();
			return false;
			}

		if (e.maxNumber != null && v > e.maxNumber)   {
			alert("The field " + e.description +
				" must be less than or equal to " + e.maxNumber);
			e.focus();
			return false;
			}
		if (e.isInt != null && e.isInt)	{
			var intPattern = /^-?\d+$/;
			if ( !intPattern.match(e.value)){
				alert(	"The field " + e.description +
					" must be an integer.");
				e.focus();
				return false;
				}		
			}
		return true;
		}
	}

// Checks if a field looks like a date in the 9999/99/99 format
function checkdate(e) {
	
	var datePattern = /^\d{4}[-\/]\d{2}[-\/]\d{2}$/;
	datePattern = /^(20)?\d{2}[-\/][0|1]?[0-9][-\/][0-3]?[0-9]$/;
	
	if ( !datePattern.match(e.value))  {
		alert(" The field " + e.description +
			" must have the format 9999/99/99 (year month day)");
		e.focus();
		return false;
		}

	return true;
	}

// Checks if a field looks like a phone number in the 999-999-9999 format
function checkphone(e) {

	var phonePattern = /^\(?\d{3}\)?[-\/\.]?\d{3}[-\/\.]?\d{4}$/ 

	if (e.value.substr(0,1) == '1') {
		alert(" The field " + e.description +
       		" must not begin with a '1'" +
			" and must have the format 999-999-9999");
			e.focus();
           return false;
                }

	if ( !e.value.match( phonePattern )) {
		alert(" The field " + e.description + e.value +
			" must have the format 999-999-9999");
		e.focus();
		return false;
		}

	return true;
	}
// Checks if a field contains any whitespace
function checkwhitespace(e) {
	var seenAt = false;

	for(var j = 0; j < e.value.length; j++)	 {
		var c = e.value.charAt(j);

		if ((c == ' ') || (c == '\n') || (c == '\t'))	  {
		alert("The field " + e.description +
			" must not contain whitespace");
		e.focus();
		return false;
			}
		}
	return true;
	}

// Now check for fields that ar supposed to be passwords
// Checks for illegal chars, length
// Separate functions for rules!
function checkpass(e) {

	var illegalChars = /[\W_]/; // Allow only letters and numbers
	var UCASE = /[A-Z]/; 
	var lcase = /[a-z]/;
	var nums = /[0-9]/;

	if ((e.minLength != null) && ( e.value.length < e.minLength ))   {
		alert( "The field " + e.description + " must have at least " +
				e.minLength + " characters");
		e.focus();
		return false;
		}
	
	else if ((e.maxLength != null ) && ( e.value.length > e.maxLength ))	 {
		alert( "The field " + e.description + " cannot have more than " +
				e.maxLength + " characters");
		e.focus();
		return false;
		}
	
	else if ((e.hasUcase != null) && (e.hasUcase)  && (!UCASE.test(e.value)))	{
		alert("The field " + e.description +
			" must have at least one Upper case character");
		e.focus();
		return false;
		}
	
	else if ((e.hasLcase != null) && (e.hasLcase) && (!lcase.test(e.value)))	{
		alert("The field " + e.description +
			" must have at least one lower case character");
		e.focus();
		return false;
		}

	else if ((e.hasNum != null) && (e.hasNum) && (!nums.test(e.value)))   {
		alert("The field " + e.description +
			" must have at least one numeric character");
		e.focus();
		return false;
		}
	
	else if (( illegalChars.test( e.value ))) {
		alert( "The field " + e.description +
			" can only contain letters and numbers");
		e.focus();
		return false;
		}

// Make sure the two password fields have the same value!
	else if (( e.isDependent != null) && (!( (e.isDependent.value).value === e.value ))) {
//	else if (( e.secondEntry != null) && (! ( e.secondEntry.value === e.value))) {
		alert( "The field " + e.description + " entry must be the same as " +
				(e.isDependent.value).description );
		e.focus();
		return false;
		}

	return true;	
	}

// Check hint field for literal password.
function checkhint(e) {
	if (!e.hasIn.value.search(/\S/)) {
		alert(	"You must complete entry of a password " +
			"before entering " + e.description);
		return false;
		}
 
	var passwordPattern = '/' + e.hasIn.value + '/i';
	
	if ( e.value.search(passwordPattern)) {
		alert(	"The field " + e.description +
			" may not contain the literal password!.");
		e.focus();
		return false;
		}
	return true;
	}

// Now check for fields that are supposed to be usernames
// Checks for illegal chars, length
// Separate functions for rules!
function checkuser(e) {

	var illegalChars = /[\W]/; // Allow only underscores, letters and numbers

	if ((e.minLength != null) && ( e.value.length < e.minLength ))   {
		alert( "The field " + e.description + " must have at least " +
				e.minLength + " characters");
		e.focus();
		return false;
		}

	else if ((e.maxLength != null ) && ( e.value.length > e.maxLength ))	 {
		alert( "The field " + e.description + " cannot have more than " +
				e.maxLength + " characters");
		e.focus();
		return false;
		}

	else if (( illegalChars.test( e.value ))) {
		alert( "the field " + e.description +
			" can only contain letters and numbers");
		e.focus();
		return false;
		}

	return true;
	}


// Now check for fields that are supposed to be emails.
// Only checks that there's one @ symbol

// Check for no spaces must be done separately!
function checkemail(e) {

	var emailPattern = /^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
	var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

	if ( illegalChars.test( e.value )) {
		alert("The field " + e.description +
		" contains invalid characters" );
		e.focus();
		return false;
		}

	//if ( !emailPattern.match(e.value))	 {
	if ( !e.value.match( emailPattern ))	{
		alert("The field " + e.description + 
			" must be a properly formatted email address.");
		e.focus();
		return false;
		}
	return true;
	}

// This is the function that performs <form> validation.
// It is invoked from the onSubmit( ) event handler.
// The handler should return whatever value this function
// returns.

function verify(f) {
  // Loop through the elements of the form, looking for all
  // text and textarea elements. Report errors using a post validation,
  // field-by-field approach
	for(var i = 0; i < f.length; i++)  {

		var e = f.elements[i];

		if (((	 e.type == "text") || 
			(e.type == "textarea") || 
			(e.type == "password"))) {

			// first check if the field is empty and shouldn't be
			if (!e.isOptional && !checkblank(e)){
				return false;}

			// Now check for fields that are supposed to be numeric.
			if (!isblank(e) && e.isNumeric && !checknumber(e)){
				return false;}

			// Now check for fields that are supposed to be dates
			if (!isblank(e) && e.isDate && !checkdate(e)){
				return false;}

			// Now check for fields that are supposed to be emails
			if (!isblank(e) && e.isEmail && !checkemail(e)){
				return false;}

			// Now check password entries
			if (!isblank(e) && e.isPass && !checkpass(e)){
				return false;}

			// Now check Hint field
			if (!isblank(e) && e.hasIn && !checkhint(e)){
				return false;}

			// Now check username entries
			if (!isblank(e) && e.isUser && !checkuser(e)){
				return false;}

			// Check phone entries
			if (!isblank(e) && e.isPhone && !checkphone(e)){
				return false;}

			// Now check for fields that are supposed
			// not to have whitespace
			if (!isblank(e) && e.hasNospaces && !checkwhitespace(e)){
				return false;}
			}	// if (type is text or textarea)
				// for each character in field

		if ((	e.type == "radio" )){
			}	// if type is radio

		}

  // There were no errors if we got this far
	return true;
	}
