﻿function submitForm() {

    if (document.frmLogin.UserName.value == "") {
        alert("이메일을 입력하십시오");
        document.frmLogin.UserName.focus();
    }
    else if (document.frmLogin.Password.value == "") {

        alert("Password를 입력하십시오");
        document.frmLogin.Password.focus();
    }
    else {
        document.frmLogin.action = "login_ok.asp";
        document.frmLogin.submit();
        doCookie();
    }
}

function submitPoPForm() {

    if (document.frmLogin.UserName.value == "") {
        alert("이메일을 입력하십시오");
        document.frmLogin.UserName.focus();
    }
    else if (document.frmLogin.Password.value == "") {

        alert("Password를 입력하십시오");
        document.frmLogin.Password.focus();
    }
    else {
        document.frmLogin.action = "/Login/PoPlogin_ok.asp";
        document.frmLogin.submit();
        doCookie();
    }
}


cookieName = "LGERID";
cookieNamePassword = "LGERIW";
cookieNameIdChk = "LGERIIC";
cookieNamePwChk = "LGERIPC";

cookieNameAuto = "LGERIAUTO";

function Encrypt(theText) {
output = new String;
Temp = new Array();
Temp2 = new Array();
TextSize = theText.length;
for (i = 0; i < TextSize; i++) {
rnd = Math.round(Math.random() * 122) + 68;
Temp[i] = theText.charCodeAt(i) + rnd;
Temp2[i] = rnd;
}
for (i = 0; i < TextSize; i++) {
output += String.fromCharCode(Temp[i], Temp2[i]);
}
return output;
}

function unEncrypt(theText) {
output = new String;
Temp = new Array();
Temp2 = new Array();
TextSize = theText.length;
for (i = 0; i < TextSize; i++) {
Temp[i] = theText.charCodeAt(i);
Temp2[i] = theText.charCodeAt(i + 1);
}
for (i = 0; i < TextSize; i = i+2) {
output += String.fromCharCode(Temp[i] - Temp2[i]);
}
return output;
}

function doCookie() {

    var index = -1;
    var indexPW = -1;
    var indexIdChk = -1;
    var indexPwChk = -1;
    var indexAutoChk = -1;

    if (document.cookie) {
        index = document.cookie.indexOf(cookieName);
        indexPW = document.cookie.indexOf(cookieNamePassword);
        indexIdChk = document.cookie.indexOf(cookieNameIdChk);
        indexPwChk = document.cookie.indexOf(cookieNamePwChk);
        indexAutoChk = document.cookie.indexOf(cookieNameAuto);
    }

    if (document.frmLogin.idsave.checked == true) {  // ID를 저장하므로 쿠키를 생성 갱신함.

        //var ID = escape(document.frmLogin.UserName.value);
        var ID =  escape(Encrypt(document.frmLogin.UserName.value));
        var IdChk = 1;
        //var PW = escape(document.frmLogin.Password.value);
    
        
        var PwChk = 1;

        if (index == -1) {

            document.cookie = cookieName + "=" + ID + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
            document.cookie = cookieNameIdChk + "=" + IdChk + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";

        } else {

            document.cookie = cookieName + "=" + ID + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
            document.cookie = cookieNameIdChk + "=" + IdChk + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";

        } 
    }
    else {          // ID를 저장하지 않음으로 쿠키를 수정함.

        ID = "0";
        IdChk = "0";

        document.cookie = cookieName + "=" + ID + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
        document.cookie = cookieNameIdChk + "=" + IdChk + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
    }

    if (document.frmLogin.pwsave.checked == true & document.frmLogin.idsave.checked == true) {  // Password를 저장하므로 쿠키를 생성 갱신함.
        //var PW = escape(document.frmLogin.Password.value);
        var PW = escape(Encrypt(document.frmLogin.Password.value));
        var PwChk = 1;

        if (indexPW == -1) {
            document.cookie = cookieNamePassword + "=" + PW + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
            document.cookie = cookieNamePwChk + "=" + PwChk + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
        } else {
            document.cookie = cookieNamePassword + "=" + PW + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
            document.cookie = cookieNamePwChk + "=" + PwChk + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
        } 
    }
    else {          // Password를 저장하지 않음으로 쿠키를 수정함 .

        PW = "0";
        PwChk = "0";
        document.cookie = cookieNamePassword + "=" + PW + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
        document.cookie = cookieNamePwChk + "=" + PwChk + "; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
    }


    if (document.frmLogin.autoCheck.checked == true) {
        document.cookie = cookieNameAuto + "=1; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
    }
    else {
        document.cookie = cookieNameAuto + "=0; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
    }
}


function doCookie2() {
    var index = -1;
    var indexPW = -1;
    var indexIdChk = -1;
    var indexPwChk = -1;
    var indexAutoChk = -1;

    if (document.cookie) {
        index = document.cookie.indexOf(cookieName);
        indexPW = document.cookie.indexOf(cookieNamePassword);
        indexIdChk = document.cookie.indexOf(cookieNameIdChk);
        indexPwChk = document.cookie.indexOf(cookieNamePwChk);
        indexAutoChk = document.cookie.indexOf(cookieNameAuto);
    }


    if (document.frmLogin.autoCheck.checked == true) {
        document.cookie = cookieNameAuto + "=1; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
    }
    else {
        document.cookie = cookieNameAuto + "=0; expires=Saturday, 01-Apr-2020 08:00:00 GMT" + ";path=/";
    }

}


function getID() {

    if (document.cookie) {
        var index = document.cookie.indexOf(cookieName);
        var temp;
        if (index != -1) {
            var countbegin = (document.cookie.indexOf("=", index) + 1);
            var countend = document.cookie.indexOf(";", index);
            if (countend == -1) {
                countend = document.cookie.length;
            }

            //temp = unescape(document.cookie.substring(countbegin, countend));
            var text= unescape(document.cookie.substring(countbegin, countend));
            temp = unEncrypt(text);
            
            if (temp == "0") {
                return "";
            }
            else

            { return temp; }
        }
    }
    return "";
}


function getIdChk() {
    if (document.cookie) {
        var index = document.cookie.indexOf(cookieNameIdChk);
        if (index != -1) {
            var countbegin = (document.cookie.indexOf("=", index) + 1);
            var countend = document.cookie.indexOf(";", index);
            if (countend == -1) {
                countend = document.cookie.length;
            }
            temp = unescape(document.cookie.substring(countbegin, countend));

            if (temp == "0") {
                return "";
            }
            else

            { return temp; }
        }
    }
    return "";
}


function getPwChk() {
    if (document.cookie) {
        var index = document.cookie.indexOf(cookieNamePwChk);
        if (index != -1) {
            var countbegin = (document.cookie.indexOf("=", index) + 1);
            var countend = document.cookie.indexOf(";", index);
            if (countend == -1) {
                countend = document.cookie.length;
            }
            temp = unescape(document.cookie.substring(countbegin, countend));

            if (temp == "0") {
                return "";
            }
            else

            { return temp; }
        }
    }
    return "";
}


function getPW() {
    if (document.cookie) {
   
        var index = document.cookie.indexOf(cookieNamePassword);
        if (index != -1) {
            var countbegin = (document.cookie.indexOf("=", index) + 1);
            var countend = document.cookie.indexOf(";", index);
            if (countend == -1) {
                countend = document.cookie.length;
            }
            //temp = unescape(document.cookie.substring(countbegin, countend));
            var text= unescape(document.cookie.substring(countbegin, countend));
           
            temp = unEncrypt(text);
           
            if (temp == "0") {
                return "";
            }
            else

            { return temp; }
        }
    }
    return "";
}


function getAUTO() {
    if (document.cookie) {
        var index = document.cookie.indexOf(cookieNameAuto);
        if (index != -1) {
            var countbegin = (document.cookie.indexOf("=", index) + 1);
            var countend = document.cookie.indexOf(";", index);
            if (countend == -1) {
               countend = document.cookie.length;
            }
            temp = unescape(document.cookie.substring(countbegin, countend));

            if (temp == "0") {
                return "";
            }
            else

            { return temp; }
        }
    }
    return "";
}

function getinfo() {
    document.frmLogin.UserName.value = getID();
    document.frmLogin.Password.value = getPW();
    document.frmLogin.idsave.checked = getIdChk();
   // document.frmLogin.pwsave.checked = getPwChk();
   //document.frmLogin.autoCheck.checked = getAUTO();
}

function getinfo2() {
    if (document.all.lid) {
        document.all["lid"].value = getID();
        if (document.all["lid"].value.length == 0) {
            document.all.lid.style.backgroundImage = "url(\'/images/common/include/login_bg_id.gif\')";
        }
        else {
            document.all.lid.style.backgroundImage = "";
        }
    }

    if (document.all.lpwd) {
        document.all["lpwd"].value = getPW();
        if (document.all["lpwd"].value.length == 0) {
            document.all.lpwd.style.backgroundImage = "url(\'/images/common/include/login_bg_password.gif\')";
        }
        else {
            document.all.lpwd.style.backgroundImage = "";
        }
    }
}

function pass() {
    if (document.cookie) {
        var index = document.cookie.indexOf(cookieName);
        var indexPw = document.cookie.indexOf(cookieNamePassword);
        if (index != -1) {
            document.frmLogin.submit();
        }
        else { return "" }
    }
    return "";
}

function autoLogin() {
    if (document.cookie) {
        //1.자동로그인 상태인지 확인한다.
        //2.자동로그인 상태인 경우 아이디 패스워드를 쿠키애서 가져온다.
        //3.가져온 값으로 로그인후 로그인 정보를 저장한다.

        //4.로그인 정보가 틀린경우 자동로그인 리셋, 아이디,패스워드를 리셋한다.
        //5.로그인 페이지로 이동한다.
        if (getAUTO()) {

        }
    }
}
