/* Validation methods to test form element states against a Data::Validation js data structure
Usage: <form action="/path/scriptname" method="[post|get]" onSubmit="test(this);return false;">

In head of html document containing the form, pull in your validation structure as a JS include:
<script language="JavaScript" type="text/javascript" src="/sitepath/validation_structure.js"></script>

Also in the head of the document customise error messages like this:
<script language="JavaScript" type="text/javascript">
<!--
var mandFieldMsg = "Please fill out, or select an option, from\nthe following mandatory fields:\n\n";
var invalidEmailMsg = "The email address(es) you entered is/are not valid - please check\n\n";
var regexMsg = "Please check that your phone numbers\ncontain number characters only\n\n";
var maxLengthMsg = "You have exceeded the maximum number of characters for the following field(s)\n\n";
var minLengthMsg = "The following field(s) require more characters than you have inputted:\n\n";
//-->
</script>
You could also pull these in as a javascript include:
<script language="JavaScript" type="text/javascript" src="/sitepath/cutomised_messages.js"></script>

Note: radio button groups should be set up as lists in the validation data structure in order for them to work with this script.

Author: phill
Date: 10/03/2004

NB: only needed functionality has been implemented - does not cover all possible validation.pm checks */

function isBlank(s) {
	for (var i=0; i<s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '')) return false;
	}
	return true;
}

function rbNotChecked(g) { // expects an radio button array
	for(var b=0; b<g.length; b++) {
		if (g[b].checked == true) {
			return false;
			continue;
		}
	}
	return true;
}

function cbNotChecked(cb) { // single checkbox
	if (cb.checked) return false;
	return true;
}

function optNotSelected(option) { // check whether an option is selected from list
	if (option.selectedIndex == -1) return true;
	return false;
}

/* validation routine called from form submit button */
function test(form) {
	// validation object
	var val = Validation.Params;
	
	/* error strings - the message displayed in the alert box should be 
	customised in the head of the html document - see usage above */
	var msg = "";
	var err_mand = "";
	var err_regex = "";
	
	// iteration through page form elements
	for (var i=0; i<form.length; i++) {
 		var e = form.elements[i];
		var field = e.name; // this is just a string variable containing the name property of the form element
		
		if (val[field]) {
			// check mandatory fields (of input type 'text', 'textfield', 'radio', 'select-one', 'checkbox')
			if ( val[field].Mandatory && (e.type == "text" || e.type == "textarea") && (e.value == null || e.value == "" || isBlank(e.value)) ) {
				err_mand += field + "\n";
				
			} else if ( val[field].Mandatory && val[field].List && (e.type == "radio") ) {
				// stop all radio button names appearing more than once
				if ( (err_mand.indexOf(field) == -1) ) {
					if ( rbNotChecked(form[field]) ) {
						err_mand += field + "\n";
					}
				}
			
			} else if ( val[field].Mandatory && val[field].List && e.type == "select-one" ) {
				if ( optNotSelected(form[field]) ) { // must be dropdown menu option object!!
					err_mand += field + "\n";
				}
				
			} else if ( val[field].Mandatory && (e.type == "checkbox") && cbNotChecked(form[field]) ) {
				err_mand += field + "\n";
			}			
			
			// check whether text string parses in RegExp check
			if (val[field].Regex && !isBlank(e.value)) {
				var pattern = new RegExp(val[field].Regex);
				var result = e.value.match(pattern);
				if (result == null) err_regex += field + "\n";
			}
			
			// check any email addresses
			var emailFlag = false;
			if ( (val[field].Type == 'Email') && !isBlank(e.value) ) {
				if (e.value.indexOf('<') >= 0 ||
					e.value.indexOf('>') >= 0 ||
					e.value.indexOf(';') >= 0 ||
					e.value.indexOf(',') >= 0 ||
					e.value.indexOf('@') == -1 ||
					e.value.indexOf('.') == -1) { // must have an '@' and a '.'
					emailFlag = true;
					break;
				}
			}
			
			// check MaxLength and MinLength on 'text' and 'textarea' fields
			var err_maxlength = "";
			var err_minlength = "";
			if ( val[field].MaxLength && !isBlank(e.value) ) {
				//alert(e.value.length);
				if (e.value.length > val[field].MaxLength) err_maxlength += field + "\n";
			}
			if (val[field].MinLength && (e.type == "text" || e.type == "textarea") ) {
				if (e.value.length > val[field].MinLength) err_minlength += field + "\n";
			}
			
		}
	}

	// build error messages and alert if necessary
	if (err_mand != "") msg += mandFieldMsg + err_mand + "\n";
	if (err_regex != "") msg += regexMsg + err_regex + "\n";
	if (emailFlag) msg += invalidEmailMsg + "\n";
	if (err_maxlength != "") msg += maxLengthMsg + err_maxlength + "\n";
	if (err_minlength != "") msg += minLengthMsg + err_minlength + "\n";
	if ( (err_mand != "") || (err_regex != "") || (emailFlag) || (err_maxlength != "") || (err_minlength != "") ) {
		alert(msg + "\nThank you\n");
		return false;
	}
	return true;
}