/*
FORM VALIDATION SCRIPTS
code by Gyrus & Grufty Jim (http://www.norlonto.net)
some sections adapted from scripts by
- Russ Michaels (http://www.satachi.com)
- Webmonkey (http://www.webmonkey.com)
- Chris Nott (http://www.dithered.com)

FUNCTIONS
- isFormComplete(formName,compList)
- isEmailValid(formName,emailList)
- isPwdValid(formName,pwdMinLength)
- isURLValid(formName,URLList)
- isAlphanumeric(formName,alphaNumList,ignoreWhiteSpace)
- isNumeric(formName,numList,ignoreWhiteSpace)
- isMinLength(formName,minChecks)
- isMaxLength(formName,maxChecks)
- isRadio(formName,elementName)

NOTES
- Call using:
  <input type="submit" onclick="return (func1() && func2() && func3());">
  (whichever functions you need to run on the form in question)
- For functions that require array lists passed to them (where
  arguments are called 'xxxxList'), this you would create the
  array like this:
  <input type="submit" onclick="compList=new Array('field1','field2',
  'field3'); emailList=new Array('email'); return (isFormComplete('form',
  compList) && isEmailValid('form',emailList));">
- Focuses input on the element that's got a problem on returning.
*/



/*
This checks to see if all required fields are filled in.

formName   = name of the form to be checked
compList   = array list of fields to be checked for a value
*/

function isFormComplete(formName,compList) {

	for(element=0;element<compList.length;element++) {

		var field = eval('document.forms[formName].'+compList[element]);
		
		if(field.value == "") {
			alert('You must enter a value for this field:\n\n\t\''+compList[element]+'\'');
			document.forms[formName].elements[compList[element]].focus();
			return false;
		}

	}
	
	return true;
	
}



/*
This validates an email address.

formName   = name of the form to be checked
emailList  = array list of fields to be checked for a valid email address
*/

function isEmailValid(formName,emailList) {

	for(element=0;element<emailList.length;element++) {

		var field	= eval('document.forms[formName].'+emailList[element]);
		var atSym	= field.value.indexOf('@');
		var period	= field.value.lastIndexOf('.');
		var space	= field.value.indexOf(' ');
		var length	= field.value.length - 1;
	
		if	((atSym < 1) ||
			 (period <= atSym+1) ||
			 (period == length ) ||
			 (space  != -1)) {
			alert('Please enter a valid e-mail address for \''+emailList[element]+'\'!');
			field.focus();
			return false;
		}
	}

	return true;
	
}



/*
This validates passwords - field must be called 'password',
with a 'confirm password' field (optional) called 'cPassword'.

formName      = name of the form to be checked
pwdMinLength  = minimum required length of password
*/

function isPwdValid(formName,pwdMinLength) {

	var field			= document.forms[formName].password;
	var length			= field.value.length;
	var pwdList			= new Array('password');
	var minList			= new Array(new Array('password',pwdMinLength));

	// do the alphanumeric and minimum length checks
	if	((!isAlphaNumeric(formName,pwdList,0)) ||
		 (!isMinLength(formName,minList))) {
		return false;
	}
	
	// if a 'confirm password' field exists, make sure it matches 'password'
	if (document.forms[formName].cPassword) {
		var cField	= document.forms[formName].cPassword;
		if (field.value != cField.value) {
			alert('The \'Password\' and \'Confirm password\' fields don\'t match!');
			cField.focus();
			return false;
		}
	}
	
	return true;

}



/*
This validates URLs.

formName  = name of the form to be checked
URLList   = array list of fields to be checked for a valid URL
*/

function isURLValid(formName,URLList) {

	for(element=0;element<URLList.length;element++) {

		var field	= eval('document.forms[formName].'+URLList[element]);
		var space	= field.value.indexOf(' ');
		var period	= field.value.indexOf('.');
		var http	= eval(field.value.substring(0,7) == 'http://');
		var https	= eval(field.value.substring(0,8) == 'https:///');
		var ftp		= eval(field.value.substring(0,6) == 'ftp:///');
		var gopher	= eval(field.value.substring(0,9) == 'gopher://');
		var telnet	= eval(field.value.substring(0,9) == 'telnet://');
		var nntp	= eval(field.value.substring(0,7) == 'nntp://');
		var wais	= eval(field.value.substring(0,7) == 'wais://');
	
		if	((period == -1) ||
		     (space  != -1) ||
			 ((!http)&&(!https)&&(!ftp)&&(!gopher)&&(!telnet)&&(!nntp)&&(!wais))) {
			alert('Please enter a valid URL for \''+URLList[element]+'\'!');
			field.focus();
			return false;
		}
	}
		
	return true;

}



/*
This checks that form fields contain only letters and numbers.

formName           = the name of the form to be checked
alphaNumList       = array list of fields to be checked for an alphanumeric value
ignoreWhiteSpace   = An optional boolean that specifies whether
                     to ignore spaces and tabs in string. If false,
                     any spaces or tabs will be considered non-
                     alphanumeric characters and strings containing
                     these characters will fail the test. If true,
                     spaces and tabs will be disregarded. Defaults
                     to false.
*/

function isAlphaNumeric(formName,alphaNumList,ignoreWhiteSpace) {

	for(element=0;element<alphaNumList.length;element++) {

		var field	= eval('document.forms[formName].'+alphaNumList[element]);
		if (!ignoreWhiteSpace) {
			var spaceMessage1 = ', and can\'t include spaces';
		}
		else {
			var spaceMessage1 = '';
		}

		if (field.value.search) {
			if	((ignoreWhiteSpace && field.value.search(/[^\w\s]/) != -1) ||
				 (!ignoreWhiteSpace && field.value.search(/\W/) != -1)) {
				alert('The field \''+alphaNumList[element]+'\' can only contain letter and numbers'+spaceMessage1+'.');
				field.focus();
				return false;
			}
		}
	}

	return true;

}



/*
This checks that form fields contain only numbers.

formName          = the name of the form
numList           = array list of fields to be checked for a numeric value
ignoreWhiteSpace  = An optional boolean that specifies whether
                    to ignore spaces and tabs in string. If false,
                    any spaces or tabs will be considered non-
                    alphanumeric characters and strings containing
                    these characters will fail the test. If true,
                    spaces and tabs will be disregarded. Defaults
                    to false.
*/

function isNumeric(formName,numList,ignoreWhiteSpace) {

	for(element=0;element<numList.length;element++) {

		var field	= eval('document.forms[formName].'+numList[element]);
		if (!ignoreWhiteSpace) {
			var spaceMessage2 = ', and can\'t include spaces';
		}
		else {
			var spaceMessage2 = '';
		}

		if (field.value.search) {
			if	((ignoreWhiteSpace && field.value.search(/[^\d\s]/) != -1) ||
				 (!ignoreWhiteSpace && field.value.search(/\D/) != -1)) {
				alert('The field \''+numList[element]+'\' can only contain numbers'+spaceMessage2+'.');
				field.focus();
				return false;
			}
		}
	}
	
	return true;
	
}



/*
This checks for a minimum length on a form field.

formName		= the name of the form
minChecks		= 2-dimensional array
                          - First dimension contains list of fields to be checked
                          - Second dimension contains corresponding minimum lengths
*/

function isMinLength(formName,minChecks) {

	for(element=0;element<minChecks.length;element++) {
	
		var field	= eval('document.forms[formName].'+minChecks[element][0]);
		var check	= minChecks[element][1];

		if (field.value.length < check) {
			alert('The field \''+minChecks[element][0]+'\' must contain a minimum of '+check+' characters.');
			field.focus();
			return false;
		}
	}

	return true;
	
}



/*
This checks that textarea fields are under a specified maximum length.

formName		= the name of the form
maxChecks		= 2-dimensional array
                          - First dimension contains list of fields to be checked
                          - Second dimension contains corresponding maximum lengths
*/

function isMaxLength(formName,maxChecks) {

	for(element=0;element<maxChecks.length;element++) {
	
		var field	= eval('document.forms[formName].'+maxChecks[element][0]);
		var check	= maxChecks[element][1];

		if (field.value.length > check) {
			alert('The field \''+maxChecks[element][0]+'\' must not contain more than '+check+' characters.');
			field.focus();
			return false;
		}
	}

	return true;
	
}



/*
This checks to ensure that one radio button in a set has been chosen

formName     = the name of the form
elementName  = the name of the radio button set in the form
*/

function isRadio(formName,elementName) {

	var field		= eval('document.forms[formName].'+elementName);
	
	for(element=0;element<field.length;element++) {

		if(field[element].checked) {
			return true;
		}
	}
	alert('The section \''+elementName+'\' must have a radio button selected.');
	return false;

}

