// vim:shiftwidth=4:softtabstop=4:tabstop=4

	function my_parse_date( date_str ) {
		var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

		// Is date in form dd/mm/yy?
		var re = /^(\d{1,2})\/(\d{1,2})\/(\d{2})$/;

		result = date_str.match(re);
		if ( result != null ) {
			// Swap mm and dd so formast is dd/mm/yy

			dd = result[1];
			mm = result[2];
			yy = result[3];

			yy = parseInt(yy);

			if ( yy < 80 ) 
				yy = 2000 + yy;
			else
				yy = 1900 + yy;

			date_str = mm + "/" + dd + "/" + yy;
			
			return date_str;
		}
		
		// is date in form dd/mm/yyyy?
		var re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;

		result = date_str.match(re);
		if ( result != null ) {
			// Swap mm and dd so formast is dd/mm/yy
			date_str = result[2] + "/" + result[1] + "/" + result[3];
			return date_str;
		}

		// Otherwise return as is to allow normal validation
		return date_str;

		/* Is date in form dd mon yyyy?
		var re = /^(\d{1,2}) ([a-zA-Z]{3}) (\d{4})$/;

		result = date_str.match(re);
		if ( result != null )
			return date_str;

		// Not valid as far as we are concerned
		return false;
		*/
	}


	function is_an_integer(value) {
	
		if (isNaN(parseInt(value)))
			return false;
			
		return true;
	}
	
	function is_a_number(value) {
	
		if (isNaN(parseFloat(value)))
			return false;
			
		return true;
	}
	
	function is_a_date(value) {

		value = my_parse_date(value); // Swap mm and dd to allow for hard coded americaness.
	
		asDate = Date.parse(value);
		
		if (isNaN(asDate))
			return false;
			
		return asDate;
	}
	
	function invalid_type_message( type, count ) {
		result = " - The following field" +
					(count>1?"s":"") + " " +
					(count>1?"are not":"is not a") + " valid " + type +
					(count>1?"s":"") + ":\n";
		
		return result;
	}

	function clear_error(field, normal_colour) {
			field.style.backgroundColor = normal_colour;
	}

	function indicate_error(field, error_colour) {
			field.style.backgroundColor = error_colour;
	}

	function clear_errors(frm, normal_colour) {
		for(i=0;i<frm.elements.length;i++) {
			frm.elements[i].style.backgroundColor = normal_colour;
		}
			
	}

	function _validate_form(fields) {

//debugger;

		months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

		/* Set error colors */
		if (arguments.length > 1)
			normal_colour = arguments[1];
		else
			normal_colour="white";

		if (arguments.length > 2)
			error_colour = arguments[2];
		else error_colour="ff6633";

		missing = [];not_numeric = []; not_date = []; too_long = []; maxlengths = []; actual_lengths = []; lists = [];extras = [];

		f0 = fields[0];
		ff = f0.field;
		fo = ff.form;
		frm = fo;

		
		frm = fields[0].field.form;
		clear_errors(frm, normal_colour);
		
		for(i=0;i<fields.length;i++) {
			field = fields[i];
			type  = field.type;
			label = field.label;
			mandatory = field.mandatory;
			maxlength = field.maxlength;
			extra     = field.extra;
/*
			if (field.type != 'List')
				clear_error(field.field, normal_colour);
			else
				for(y=0;y<field.values.length;y++) {
					clear_error( field.values[y], normal_colour )
				}
*/

			if (mandatory) {
				if ( type == "Select" && field.field.selectedIndex == 0 ) {
					missing[missing.length] = label
					indicate_error(field.field, error_colour);
				}
				else if( !field.field.value ) {
					missing[missing.length] = label;
					indicate_error(field.field, error_colour);
				}
			}

			switch (type) {
				case 'List':
					ok = 0;
					for(y=0;y<field.values.length;y++) {
						if ( field.values[y].value )
							ok = 1;
					}
					if ( ! ok ) {
						lists[length] = label;

						for(y=0;y<field.values.length;y++) {
							indicate_error( field.values[y], error_colour )
						}
					}
					break;
				case 'Text':
					if ( maxlength ) {
						if ( field.field.value.length > maxlength ) {
							too_long[too_long.length] = label;
							maxlengths[maxlengths.length] = maxlength;
							actual_lengths[actual_lengths.length] = field.field.value.length;
							indicate_error(field.field, error_colour);
						}
					}
					break;
				case 'Number':
				case 'Currency':
				case 'Float':
					if ( field.field.value ) {
						num = field.field.value.replace(/[^0-9.]/g,'');
						if ( ! is_a_number( num ) ) {
							not_numeric[not_numeric.length] = label;
							indicate_error(field.field, error_colour);
						}
						else
							field.field.value = parseFloat(num);
					}
					break;
				case 'Integer':
					if ( field.field.value ) {
						num = field.field.value.replace(/[^0-9.]/g,'');
						if ( ! is_an_integer( num ) ) {
							not_numeric[not_numeric.length] = label;
							indicate_error(field.field, error_colour);
						}
						else
							field.field.value = parseInt(num);
					}
					break;
				case 'Date':
					if ( field.field.value )
						if (! is_a_date( field.field.value ) ) {
							not_date[not_date.length] = label;				
							indicate_error(field.field, error_colour);
						}
						else {
							date_str = field.field.value;
							date_str = my_parse_date(date_str);
							date = new Date(date_str); 
							//date = date.toLocaleString() + " ";
							field.field.value = date.toLocaleString();
							field.field.value = date.getDate() + " " + months[date.getMonth()] + " " + date.getFullYear();
						}
					break;
			}

			if (extra) {
				ok = 1;
				if (extra.check = 'LT') {
					switch (field.type) {
						case 'Date':
							v1 = is_a_date(field.field.value);
							if (v1)
								for (v=0;v<extra.fields.length;v++) {
									v2 = extra.fields[v];
									v2 = is_a_date(v2.value);
									if (v2) {
										if (v1 >= v2) {
											ok = 0;
											break;
										}
									}
								}
					}
					if (!ok) {
						extras[extras.length] = extra.message;				
						indicate_error(field.field, error_colour);
					}
				}
			}
		}
		
		num_errors = missing.length + not_numeric.length + not_date.length + too_long.length + lists.length + extras.length;

		if ( num_errors > 0 ) {
			missing_fields = "";
			not_numeric_fields = "";
			not_date_fields = "";
			too_long_fields = "";
			list_fields = "";
			extra_msgs = "";
			
			for (i=0;i<missing.length;i++) 
				missing_fields = missing_fields + "    " + missing[i] + "\n";
				
			for (i=0;i<not_numeric.length;i++) 
				not_numeric_fields = not_numeric_fields + "    " + not_numeric[i] + "\n";
				
			for (i=0;i<not_date.length;i++) 
				not_date_fields = not_date_fields + "    " + not_date[i] + "\n";
				
			for (i=0;i<too_long.length;i++) 
				too_long_fields = too_long_fields + "    " + too_long[i] + 
									"(maximum length = " + maxlengths[i] + ", actual length = " + actual_lengths[i] + ")\n";

			for (i=0;i<lists.length;i++) 
				list_fields = list_fields + " You must specify at least one of " + lists[i] + "\n";

			for (i=0;i<extras.length;i++)
				extra_msgs = extra_msgs + extras[i] + "\n";
				
			if ( missing_fields )
				missing_fields = " - The following required field" +
					(missing.length>1?"s":"") + " " +
					(missing.length>1?"are":"is") + " empty:\n" + missing_fields;

			if ( not_numeric_fields )
				not_numeric_fields = invalid_type_message('number', not_numeric.length) + not_numeric_fields;
				
			if ( not_date_fields )
				not_date_fields = invalid_type_message('date', not_date.length) + not_date_fields;
				
			if ( too_long_fields )
				too_long_fields = " - The following field" +
					(too_long.length>1?"s":"") + " " +
					(too_long.length>1?"are":"is") + " too long:\n" + too_long_fields;

			alert(
				"_______________________________________________________________________\n\n" +
				"The form was not submitted because of the following error" +
				(num_errors>1?"s":"") + ".\n" +
				"Please correct the error" + 
				(num_errors>1?"s":"") +
				" and try again.\n" +
				"_______________________________________________________________________\n\n" +
				missing_fields + not_numeric_fields + not_date_fields + too_long_fields + list_fields + extra_msgs
			);
			return false;
		}
		return true;
	}

