/*
	This is some very very simple form validation that runs 
	to run it you need to send a reference to the form element and an array of objects that has 
	properties set for th selectors of requiremed inputs and, their types and the label that will 
	be displayed in an error message. Right now the errors come back in an alert message.
	
	See the sample below
*/
var QuickFormValidator = new Class({
	regexps : {
// here are the  different data types - they are matched against the data as regular expressions
		'emailAddress' : new RegExp(/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/),
		'phoneNumber' : new RegExp(/^([0-9])+/),
		'zipCode' : new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/)},
	validations : [],
	initialize : function (form, requiredInputs) {
		for (var i = 0, l=requiredInputs.length; i<l;i++ ) {
			var ri = requiredInputs[i];
			var elems = form.getElements(ri.inputSelectors);
			for (var j=0, l2=elems.length; j<l2;j++ ) {
				this.validations.push({'elem' : elems[j] , 'displayName' : ri.displayName,  'type' :  ri.type});
			}
		}		
		form.addEvent('submit', this.doCheck.bindWithEvent(this));
	},
	
	doCheck : function (e) {
		var missing = [];
		var invalid = [];
		for (var i = 0, l=this.validations.length; i<l;i++ ) {
			var elem = this.validations[i].elem;
			if (elem.value == null || elem.value.replace(/^\s*/, "").replace(/\s*$/, "") == ''){
				missing.push(this.validations[i].displayName);
			} else {
				if (this.validations[i].type && this.regexps[this.validations[i].type].test(elem.value) == false) {
					invalid.push(this.validations[i].displayName);
				}
			}
		}
// if there are missign or invalid items 		
		if (missing.length > 0 || invalid.length > 0) {
// report the error
			this.doError(missing, invalid);
// stop the submit			
			e = new Event(e).stop();
			return false;
		}
		return true;
	},
	doError : function (missing, invalid) {
		var errSrt = [];
		if (missing.length > 0) {
			errSrt.push('The following fields are required :');
			errSrt = errSrt.concat(missing);
			errSrt.push('\n');
		}	
		if (invalid.length > 0) {
			errSrt.push('The following fields appear to be invalid :');
			errSrt = errSrt.concat(invalid);
			errSrt.push('\n');
		}
		alert(errSrt.join('\n'));
	}
});


/* SAMPLE USAGE

	new QuickFormValidator($$('.formTable form')[0], [
		{'inputSelectors' : '.zipCode', 'displayName' : 'Zip Code', 'type': 'zipCode'},
		{'inputSelectors' : '.emailAddress', 'displayName' : 'Email Address', 'type': 'emailAddress'},
		{'inputSelectors' : '.city', 'displayName' : 'City'} // required inout oif any typr
	]);
			
*/