// prepare the form when the DOM is ready 
$(document).ready(function() { 
    
    $('#registerForm').ajaxForm({
    	dataType: 'json',
    	beforeSubmit: preSubmit,
    	success: postSubmit
    });
}); 

function preSubmit()
{
	$('#btnSubmit')[0].value = 'Sending registration...';
	$('#btnSubmit')[0].disabled = true;
	$('.error').hide(); // Hide all errors for now
}

function postSubmit(json)
{
	if (json.errors) {
		$.each(json.errors, function(i, value) {
			$('#err' + i).fadeIn('slow');
		});
	}
	else {
		document.location = json.location;
	}
	
	$('#btnSubmit')[0].value = 'Submit registration';
	$('#btnSubmit')[0].disabled = false;
}