/**
 * GENERIC LOGIN SERVICE
 * Form needs to contain the following ids:
 * username - username input                    e.g. <input type="text" id="username">
 * password - password input                    e.g. <input type="password" id="password">
 * msg      - message for login failure         e.g. <div id="msg"></div>
 * login    - button or image to execute login  e.g. <button id="login">Login</button>
 */
$(document).ready(function()
{
    $("#login").click(function(){                       // Action when login button clicked
        var $user = $("#username").val();               // Get username from form
        var $pass = $("#password").val();               // Get password from form

        $.post("/services/json/login.php",              // Login validation service
            {"username":$user,"password":$pass},        // Pass username and password as post variables
            function($data){                            // Process returned JSON data
                $("#msg").html($data.statustext);       // Show status message
                if ($data.status == 0) {                // Check status
                    window.location = $data.url;        // Status OK, load target page
                }
            }
        );
    });
});
