﻿$(document).ready(function () {
    $("#loginInputFieldsContainer").keypress(function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);

        if (code == 13) {
            $("#loginbutton").get(0).click();
        }
    });

    $("#logoutbutton").click(function (event) {
        event.preventDefault();

        LoginWebServiceProxy.logout(function (afterLogoutUri) {
            window.location.replace(afterLogoutUri);
        });
    });

    $("#loginbutton").click(function (event) {
        event.preventDefault();

        var username = $("#loginUsername").val();
        var password = $("#loginPassword").val();

        LoginWebServiceProxy.login(username, password, function (authenticated, afterLoginUri) {
            if (authenticated) {
                window.location.replace(afterLoginUri);
            } else {
                alert('Fel användarnamn eller lösenord angavs!');
            }
        });
    });

    $("#loginUsername").focus(function () {
        $(this).filter(function () {
            return $(this).val() == "";
        }).removeClass("usernameTextInfo");
    });

    $("#loginUsername").blur(function () {
        $(this).filter(function () {
            return $(this).val() == "";
        }).addClass("usernameTextInfo");
    });

    $("#loginPassword").focus(function () {
        $(this).removeClass("passwordTextInfo");
    });

    $("#loginPassword").change(function () {
        $(this).filter(function () {
            return $(this).val().length > 0;
        }).removeClass("passwordTextInfo");
    });

    $("#loginPassword").blur(function () {
        $(this).filter(function () {
            return $(this).val() == "";
        }).addClass("passwordTextInfo");
    });

    setInterval(function () {
        $("#loginUsername").filter(function () {
            return $(this).val().length > 0;
        }).removeClass("passwordTextInfo");

        $("#loginPassword").filter(function () {
            return $(this).val().length > 0;
        }).removeClass("passwordTextInfo");
    }, 100);
});