$(document).ready(function() {
    $('#loginForm').ajaxForm({
        beforeSubmit: clearHintBlocks,
        dataType: 'json',
        success: processLogin
    });
});

function clearHintBlocks() {
    $("[id^=HINTDIV]").hide();
    return true;
}

function processLogin(data) {
    if(data['status']==1) { // Auto redirect
        window.location.replace(redirectURL);
    } else if(data['status']==2) { // Show modal
        $("#modalTitle").html(data['modal']['title']);
        $("#modalContent").html(data['modal']['content']);
        $("#modalBlock").fadeIn("slow");
        $.timer(data['modal']['timeout'], function(timer) {
            $("#modalBlock").fadeOut("slow");
            timer.stop();
        });
    } else { // Show errors
        for (error in data['errors']) {
            $("[id=HINTDIV"+error+"]").html(data['errors'][error]);
            $("[id=HINTDIV"+error+"]").show();
            $("[id=HINTDIV"+error+"pointer]").show();
        }
    }
}

function redirect(url) {
    window.location.replace(url);
}