$("document").ready(function() {
    ajaxify_home_signup_form();
});

/*
 * Ajaxify the home signup form.
 */
function ajaxify_home_signup_form() {
    $("#home_signup_form").ajaxForm({
        complete: function(xhr, statusText) {
            if (xhr.status == 201) {
                /* If account was created, redirect to verification email sent page. */
                window.location.href = xhr.getResponseHeader("Location");
            } else {
                /* 
                 * Form was not validated, we display the
                 * new form from the response text in a popup.
                 */
                jQuery.facebox(xhr.responseText);
                $('#home_signup_form button').attr("disabled", false).text('Signup');
            }
        }
    });
}

/*
 * Ajaxify the signup popup form.
 */
function ajaxify_signup_form() {
    $("#signup_form").ajaxForm({
        complete: function(xhr, statusText) {
            if (xhr.status == 201) {
                /* If account was created, redirect to verification email sent page. */
                window.location.href = xhr.getResponseHeader("Location");
            } else {
                $("#signup_form").replaceWith(xhr.responseText);
                ajaxify_signup_form();
            }
        }
    });
}

