// Class for Validated Fields
function validatedfield(fld, datatype, fldname) {
	this.fld = fld;
	this.datatype = datatype;
	// Assign onchange method to reformat date to YYYY
	if (datatype='d'&&!fld.onchange) fld.onchange=function(){;var d=new Date();if (d.IsValid(this.value)) this.value=d.FromString(this.value).Y2K();};
	this.fldname = fldname;
	this.required=(arguments.length>3)?arguments[3]:false;
	this.maxlen=(arguments.length>4)?arguments[4]:null;
	this.defaultvalue=(arguments.length>5)?arguments[5]:null;
	this.minval=(arguments.length>6)?arguments[6]:null;
	this.maxval=(arguments.length>7)?arguments[7]:null;

	this.type = this.fld.type;
	this.value=null;

	// Methods
	this.ValidateBase=function(){
		// Get Current form field value
		this.value = this.fld.value;
		// Set any default value
		if ( this.value == '' && this.defaultvalue != null ) 
			this.value = this.defaultvalue; // setup default value
		if (this.required) {
			if (this.datatype=='r') // Check for Required Radio Groups
				for (var i=0;i<this.fld.length;i++) if (this.fld[i].checked) return true;
			if (this.datatype=='r'||this.value=='') return this.ValError(this.fldname + ' is required.');
		}
		// Check against regexp
		if ( this.regexp&&!this.regexp.test(this.value) ) return this.ValError();
		// Check Max Length
		if (this.maxlen&&(this.value.length>this.maxlen)) return this.ValError(this.fldname + ' can be up to ' + this.maxlen + ' characters. Current length is ' + l + ' characters.');
		// Check Min/Max Values
		if ( (this.value.length>0)&&(this.minval&&(this.value<this.minval))||(this.maxval&&(this.value>this.maxval)) ) {
			var msg='';
			if (this.minval==null) return this.ValError(this.fldname+' must be less than or equal to ( <= ) '+this.maxval);
			if (this.maxval== null) return this.ValError(this.fldname+' must be greater than or equal to ( >= ) '+this.minval);
			return this.ValError(this.fldname+'must be between '+this.minval+' and '+this.maxval);
		}
		return true;
	}
	// Extended Data Type validation Prototype
	this.ValidateCustom=function(){return true}

	// Helper Error Display/Focus
	this.ValError=function(){
		alert((arguments.length==1)?arguments[0]:this.fldname+this.typemsg);
		if (this.fld.type=='text') this.fld.select(); 
		if (this.fld.type=='text'||this.fld.type=='select-one') this.fld.focus();
		return false;
	}

	this.Validate=function(){return (this.ValidateBase())?this.ValidateCustom():false}

	// Set up custom behaviors
	switch (this.datatype) {
		case 'i': // Integer
			this.regexp = /^[0-9]*$/;
			this.typemsg=' must be a whole number (no decimal digits).';
			break;
		case 'n': // Number
			this.regexp = /^(0|-?[1-9[0-9]*(\.[0-9](1,2))?)$/;
			this.typemsg=' must be a valid number.';
			break;
		case 'e': // E-mail address
			this.regexp = /^([-a-zA-Z0-9._]+@[-a-zA-Z0-9.]+(\.[-a-zA-Z0-9]+)+)*$/;
			this.typemsg=' must be a email address.';
			break;
		case 'k': // Keyword
			this.regexp = /^[a-zA-Z]\w*/;
			this.typemsg=' must be a valid identifier and may contain only alphanumeric characters, no whitespace and may not start with a numeral.';
			break;
		case 'd': // Date
			this.typemsg=' must be a valid date in US format: M/D/YYYY.';
			this.minage=null;
			this.ValidateCustom=function(){
				var d=new Date();
				if (!d.IsValid(this.Value)) return this.ValError();
				if (this.minage&&(d.DateAdd(0,0,-this.minage).AsInteger()<Bdate.AsInteger())) return ValError('You must be '+this.minage+' or older to participate.');
				return true;
			}
			break;
		case 'r': // Radio Group
			break;
		case 't': // Text Area
			break;
		default:
			this.regexp = null;
			this.typemsg = null;
			break;
	}
}

// Prototype of function that does any custom validation (after common validation)
function EditPageCustomValidate() {return true;}
	
// Globals (used on PageLoad and validate edit
var formfields = new Array();
function validateedit() {
	// For each validated field in the collection, stop if not valid
	for (var i=0; i<formfields.length; i++ ) if (!formfields[i].Validate()) return false;
	// Do any custom validation here, if defined
	return EditPageCustomValidate();
}

// Sample function that loads the validated fields into the collection
// Must be customized for form that needs validation
function EditPageOnLoad() {
	// if ( document.forms.length == 0 ) return;
	// formfields.Add(new validatedfield(form1.FirstName, 's', 'First Name', true));
} 

