$(document).ready(function () {
    if ($.UrlParam("isLogout") != "true") {
        if ($.cookie("token_key") != null) {
            if ($.cookie("token_key") != "") {
                location.href = "/Home/UserList";
                //alert("has token_key value :" + $.cookie("token_key"));
            }
            
        }
    }


    $("#loginBtn").click(function () {
        var id = $("#inputUser").val();
        var pwd = $("#inputPassword").val();
        var remember = "N";

        if ($("#remember-me").prop("checked")) {
            remember = "Y";
        }

        var err_msg = "";

        if (id === "") {
            err_msg += "請輸入帳號!\n";
        }

        if (pwd === "") {
            err_msg += "請輸入密碼!\n";
        }

        if (err_msg !== "") {
            alert(err_msg);
            return;
        }

        pwd = sha256_digest(pwd);

        var formData = {
            id: id,
            pwd: pwd,
            remember: remember
        }

        $.ajax({
            url: "/AuthApi/signin",
            type: "POST",
            data: formData,
            success: function (data, textStatus, jqXHR) {
                if (data.ret == "yes") {
                    location.href = "/Home/UserList";
                    //location.href = "/BackEnd/nounsList";
                 
                } else {
                    alert(data.message);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('網路或伺服器發生錯誤，請稍後重試!');
            }
        });
    });
});