$(function () {
    if (Request('LoginFail')) {
        alertError(
            Request('LoginFail'),
            "로그인 오류"
        );
    }
    const id = $("input[name = 'username']");
    const pwd = $("input[name = 'password']");
    const logBtn = $("#login");
    id.focus();
    function init() {
        logBtn.on("click", () => {
            loginCheck();
        });

        id.on("keydown", (e) => {
            if (e.key === "Enter") {
                pwd.focus();
            }
        });

        pwd.on("keydown", (e) => {
            if (e.key === "Enter") {
                loginCheck();
            }
        });

    }

    init();
    function nullChecker(checkItem) {
        return checkItem || null;
    }
    function loginCheck() {
        let idVal = $.trim(id.val());
        let pwdVal = $.trim(pwd.val());
        if (!nullChecker(idVal)) {
            return alertWarning("ID를 입력해주세요", null, id);
        }
        if (!nullChecker(pwdVal)) {
            return alertWarning("비밀번호를 입력해주세요", null, pwd);
        }

        $("form").submit();
    }

});

function Request(valuename)    //javascript로 구현한 Request
{
    var rtnval = "";
    var nowAddress = decodeURI(location.href);

    var parameters = (nowAddress.slice(nowAddress.indexOf("?")+1,nowAddress.length)).split("&");

    for(var i = 0 ; i < parameters.length ; i++){
        var varName = parameters[i].split("=")[0];
        if(varName.toUpperCase() == valuename.toUpperCase())
        {
            rtnval = parameters[i].split("=")[1];
            break;
        }
    }
    console.log(rtnval);
    console.log(nowAddress);
    return rtnval.replaceAll('+',' ');
}