var AjaxFormValidator = Class.create();

AjaxFormValidator.prototype = {
	form: '',
	url: '',
	additionalParams: '',
	inlineElem: '',
	errorDisplay: 'alert',
	errors: '',
	callback: null,

	initialize: function(form,url,additionalParams) {
		this.form = $(form);
		this.url = url;
	},

	validate: function(additionalparams) {
		if(!additionalparams) additionalparams = '';
		var params = Form.serialize(this.form) + additionalparams;
		var myAjax = new Ajax.Request(this.url,{method:'post', parameters: params, onSuccess: this.processForm.bind(this)});
		return false;
	},

	processForm: function(req) {
		var errors = eval( '(' + req.responseText + ')' );
		this.errors = errors;
		if(!errors) {
			this.form.submit();
		} else {
			switch(this.errorDisplay) {
				case 'none':
					break
				case 'inline':
					this.showErrorsInline(errors);
					break
				default:
					this.showErrorsAlert(errors);
			}
			if(this.callback) {
				this.callback();
			}
		}
	},

	showErrorsAlert: function(errors) {
		var errorString = '';
		errors.each(function(error){
			errorString += error + '\n';
		});
		alert(errorString);
	},

	showErrorsInline: function(errors) {
		var html = '<ul>';
		errors.each(function(error){
			html += '<li>' + error + '</li>';
		});
		html += '</ul>';
		$(this.inlineElem).innerHTML = html;
		$(this.inlineElem).show();
	}
}