RegistrationForm = Class.create();
RegistrationForm.prototype = 
{
	form : null,
	initialize : function(form)
	{
		this.form = $(form);
		this.form.observe('submit', this.onSubmit.bindAsEventListener(this));
		this.resetErrors();
	},
	resetErrors : function()
	{
		this.form.getElementsBySelector('.error').invoke('hide');
	},
	showError : function(key, val)
	{
		var formElement = this.form[key];
		var container = formElement.up().down('.error');
		if (container) {
			container.update(val);
			container.show();
		}
	},
	onSubmit : function(e)
	{
		Event.stop(e);
		var options = {
			parameters : this.form.serialize(),
			method : this.form.method,
			onSuccess : this.onFormSuccess.bind(this)
		};
		this.resetErrors();
		new Ajax.Request(this.form.action, options);
	},
	onFormSuccess : function(transport)
	{
		var json = transport.responseText.evalJSON(true);
		var errors = $H(json.errors);
		if (json.errors.constructor == Object) {			
			errors.each(function(pair) {
				this.showError(pair.key, pair.value);
			}.bind(this));
		}
		else if ((json.errors.constructor == Array) && (json.errors.length == 0)) {
			this.form.submit();
		}
	}
};
Ajax.Responders.register(
	{
		onCreate: showProcessing,
		onComplete: hideProcessing
	}
);

function showProcessing() {
  	if(Ajax.activeRequestCount > 0)
    	$('inProgress').style.display = 'inline';
};

function hideProcessing () {
  	if(Ajax.activeRequestCount <= 0)
    	$('inProgress').style.display = 'none'; 
};




