﻿$(document).ready(function ()
{
    $(".lang").click(function ()
    {
        window.lang = this.id;
        $.getJSON(GetWebAddress("LogOn/GetLogInTranslations"), { language: window.lang }, function (data)
        {
            var result = data;
            $("#lblMessage").html(result[0]);
            $("#lblUsername").html(result[1] + ' (e-mail)' + ':');
            $("#lblPassword").html(result[2] + ':');
            $("#lblRegistrationCode").html(result[3]);
            $("#doLog").val(result[5]);
            $("#lnkRegisterCustomer").attr('href', GetWebAddress('CustomerRegistration/RegisterCustomer') + '?isoCode=' + window.lang);
            $("#lnkRegisterCustomer").html(result[6]);
            $("#lnkForgotPassword").html(result[7]);
            $("#imgHelp").attr('title', result[8]);
            $("#lbl-Help").html(result[8]);
            $("#dialog_link").html(result[9]);
            $("#btnLegend").html(result[10]);
            $("#lblPrava").html(result[11]);
            $("#lblSaveUser").html(result[12]);
            $("#dialog").dialog('option', 'title', result[9]);
            SetCurrentLanguage(window.lang);

        });
        $("#languag").val(window.lang);
        $(".lang").css({ border: '' });
        $(this).css({ border: '1px solid green' });
        $("#error").html('');
        SetHelpPath();
    });

    $("#lnkForgotPassword").click(function ()
    {
        $("#lnkForgotPassword").attr('href', GetWebAddress('LogOn/ShowRecoverPassword') + '?ic=' + $("#languag").val());
    });

    $("#modlgn_username")[0].focus();
    if (window.messages.PasswordValidationMessage)
    {
        $("#modlgn_passwd").val('');
        $("#modlgn_passwd")[0].focus();
    }

    if (window.messages.UsernameValidationMessage)
    {
        $("#modlgn_username")[0].focus();
        $("#modlgn_username").select();
    }


    SetHelpPath();

    function SetHelpPath()
    {
        $.postx(GetWebAddress("LogOn/GetHelpPath"), { isoCode: window.lang }, function (data)
        {
            $("#lbl-Help").attr('href', data);
        });
    }

    function SetCurrentLanguage(l)
    {
        $.getx(GetWebAddress("LogOn/SetCurrentLanguage"), { isoCode: l }, function () { $("#modlgn_username")[0].focus(); });
    }


    $("#doLog").click(function ()
    {
        if ($("#chkSave").prop("checked"))
        {
            createCookie("un", $("#modlgn_username").val(), 15);
            createCookie("pw", $("#modlgn_passwd").val(), 15);
        }
    });



    if (messages.sessionEndMessage)
    {
        $.pnotify({
            pnotify_title: window.messages.pnotifyTitle,
            pnotify_text: sessionEndMessage,
            pnotify_history: false,
            pnotify_type: "error",
            pnotify_hide: false
        });
    }


});




function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++)
    {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
function createCookie(name, value, days)
{
    var expires;
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else
    {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}
