var FormValidator = new Class({
	
	setOptions: function(options) {
		this.options = {
			errorClass: 'error'
		};
		Object.extend(this.options, options || {});
	},

	
	initialize : function(form, errorMsg, options) {
		this.form = $(form);
		this.errorMsg = errorMsg;
		this.setOptions(options);
	
		$E('input', this.form).focus();
		
		this.form.addEvent('submit', this.onSubmit.bind(this));
	},
	
	onSubmit: function(evt) {
		this.firstErrorField = undefined;
		
		$$('.mandatory', this.form).each(function(el) {
			var row = el.getParent();
			
			if (el.getValue().length < 1) {
				if (this.firstErrorField === undefined) {
					this.firstErrorField = el;
				}
				
				row.addClass(this.options.errorClass);
	
				new Event(evt).stop();
			} else {
				if (row.hasClass('error')) {
					row.removeClass('error');
				}
			}
		}.bind(this));
		
		this.showError();
	},
	
	showError: function() {
		if (this.firstErrorField !== undefined) {
			alert(this.errorMsg);

			// get back to the field
			var pos = this.firstErrorField.getPosition();
			window.scrollTo(pos.x, pos.y);
			this.firstErrorField.focus();
		}
	}
});