// (c) 1999-2005 Bright Interactive Limited. All rights reserved.
// http://www.bright-interactive.com | info@bright-interactive.com
// Tel: 0870 240 6520
// Author: Eric Clack, d6

// Check that fields on a form are properly filled. Works for text fields, 
// textareas and radio buttons.
//
// Configured by calling the set_* functions below, or by adding attributes 
// to the form fields:
// 	 required="1"	The field is required, by default fields are not required
//   numeric="1"	The field is numeric and can only contain numbers
//   emailaddr="1"	The field is an email address
//
// NOTE THAT ADDING THESE FIELDS MEANS THAT THE PAGE WILL NOT VALIDATE
// IF THIS IS IMPORTANT TO YOU, USE THE set_* functions instead
//
//   alt="Pretty Field Name"	Use this tag to make errors more readable.
//
// Simply add an event onSubmit="return validate(this);" to your form.


//-------------------------------------------------------------------
// Functions to set required, numeric, emailaddr fields
// Call these in <body onload="">
// e.g. set_required( ['username', 'password'] );
//-------------------------------------------------------------------


function set_required( field_array )
{
	var n;
	for( n = 0; n < field_array.length; n++ ) {
		document.getElementsByName(field_array[n])[0].required = 1;	
	}
}

function unset_required( field_array )
{
	var n;
	for( n = 0; n < field_array.length; n++ ) {
		document.getElementsByName(field_array[n])[0].required = 0;	
	}
}


function set_numeric( field_array )
{
	var n;
	for( n = 0; n < field_array.length; n++ ) {
		document.getElementsByName(field_array[n])[0].numeric = 1;	
	}
}


function set_emailaddr( field_array )
{
	var n;
	for( n = 0; n < field_array.length; n++ ) {
		document.getElementsByName(field_array[n])[0].emailaddr = 1;	
	}
}




//-------------------------------------------------------------------
// The validate function
// Call this using onSubmit="return validate(this);"
//-------------------------------------------------------------------


function validate(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	var first_bad_field = null;
	
	// Which radio buttons have we checked?
	var checked_radios = [];

	for(var i = 0; i < f.length; i++ ) {
		var e = f.elements[i];
		
		
		
		if ((e.type == "text") || (e.type == "textarea") || (e.type == "password")) {
		
			// is the field required, but empty?
			if (e.required && ((e.value == null) || (e.value == "") || isblank(e.value))) {
				if (empty_fields != "") empty_fields += ", ";
				if (first_bad_field == null) first_bad_field = e;

				if (e.alt) empty_fields += e.alt;
				else empty_fields += e.name;
				
				continue;
			}

			// If the field is required, or it's optional, but has content...
			
			if (e.required || ((e.value != null) && (e.value != "") && (!isblank(e.value)))) {
				
				// now check numerics
				if ((e.numeric) && (!isnumber(e.value))) {
					if (errors != "") errors += ", ";
					if (first_bad_field == null) first_bad_field = e;

					if (e.alt) errors += e.alt + " must be a number ";
					else errors += e.name + " must be a number ";

				}
				// now check email addresses
				if ((e.emailaddr) && (!isemail(e.value))) {
					if (errors != "") errors += ", ";
					if (first_bad_field == null) first_bad_field = e;
					
					if (e.alt) errors += e.alt + " is not an email address ";
					else errors += e.name + " is not an email address ";
					
				}	
			}	
		}
		else if (e.type == "radio" && e.required && !checked_radios[e.name]) {
			// Check radio buttons, provided we've not checked this one before
			if (!radio_value(e.name)) {
				if (empty_fields != "") empty_fields += ", ";
				if (first_bad_field == null) first_bad_field = e;
				if (e.alt) empty_fields += e.alt;
				else empty_fields += e.name;
			}
			checked_radios[e.name] = 1;			
		}
	}

	// any errors?
	if (!empty_fields && !errors) {
		return true;
	}

	msg =  "The form was not submitted because of the following ";
	msg += "errors. Please correct them and try again.\n-\n";

	if (empty_fields) {
		msg += "The following required fields are empty:\n" + empty_fields;
		if (errors) msg += "\n-\n";
	}
	msg += errors;
	alert (msg);
	
	first_bad_field.focus();
	return false;
}


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


function isnumber(n)
{
	var v = parseFloat(n);
	if (isNaN(v)) {
		return false;
	}
	return true;
}


function isemail(e)
{
	return e.match( /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z0-9_\-\.]+)$/);
}


function radio_value(rname) {
	// Return the value of the checked radio button or null
	// otherwise.
	var radios = document.getElementsByName(rname);
	var value = null;
	for (var i=0; i<radios.length; i++){
		if (radios[i].checked) {
			value = radios[i].value;
		}
	}
	return value;
}