UserRegistrationForm = Class.create({

    form   : null,
		errors : false,

    initialize : function(form, errors)
    {
        this.form = $(form);
        this.form.observe('submit', this.onSubmit.bindAsEventListener(this));
        if(!errors)	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),
						onFailure  : this.onFormSuccess.bind(this)
        };

        this.resetErrors();
				this.form.getElementsBySelector('.notice').invoke('hide');
        new Ajax.Request(this.form.action, options);
    },
		
    onFormSuccess : function(transport)
    {
			
				//alert(transport.responseText);
        var json = transport.responseText.evalJSON(true);
				
				//alert('ok'+Object.isObject(json.errors));
        var errors = $H(json.errors);
				//alert(errors.size());
        if (errors.size() > 0) {
						
            this.form.down('.error').show();
            window.scrollTo(0,0);
            	errors.each(function(pair) {
							//alert(pair.key + ":" +pair.value);
              this.showError(pair.key, pair.value);
            }.bind(this));
        }
        else {
						//alert('submitting...');
            this.form.submit();
        }
    }
});

