// SUBMIT APPLICATION STUFF
//

function submitLoginForm() {
    jQuery("#errorMessage").html("<font color='yellow'>Logging in....</font>");
    var theform = jQuery(this);
    var thedata = theform.serialize();
    jQuery.post("/ajax/api.php?command=login", thedata, handleLoginResults);
    return false;
}

function handleLoginResults(data) {
    if ( data.error ) {
        jQuery("#errorMessage").html("<font color='red'>ERROR: " + data.error + "</font>");
        displayLogin();
    } else {
        jQuery("#errorMessage").html("");
        loadPage();
    }
}

function displayAnnouncements() {
    jQuery.post("/ajax/api.php?view=loginAnnouncements",
    function (data) {
        var html = '';
        for (var a = 0; a < data.length; a++) {
            html += "<li>" + data[a].message + "</li>\n";
        }
        if (html != '') {
            html = "<B>ANNOUNCEMENTS:</b><ul>\n" + html + "</ul>\n";
            jQuery("#announcements").html(html);
            jQuery("#announcements").show();
        }
    });
}


function displayLogin() {
    jQuery("#loginform").show();
    displayAnnouncements();
}

function loadPage() {
    jQuery.post("/ajax/api.php?view=getAccountInfo", 
    
    function(data) { 
        if (data.account_id == null || data.account_id == 0) {
           displayLogin();
        } else {
            window.location = "http://www.thepracticelog.com/journal/";
        }
    }); 
}

jQuery(document).ready(function() {
   
    jQuery("#loginform").bind('submit', submitLoginForm);
    loadPage();

});


