/**
 * Created with JetBrains WebStorm.
 * User: Cmma
 * Date: 7/22/14
 * Time: 5:48 PM
 * To change this template use File | Settings | File Templates.
 */
define(function (require, exports, module) {

    function UserLogin() {
    }

    UserLogin.getUserName = function () {
        return $("#username").val();
    }

    UserLogin.getPassword = function () {
        return $("#password").val();
    }

    UserLogin.setErrorMsg = function (msg) {
        $("#loginMsg")[0].innerHTML = msg;
    }

    UserLogin.clearErrorMsg = function () {
        $("#loginMsg")[0].innerHTML = "";
    }

    UserLogin.init = function () {
        var _self = this;

        $("#username").focus(function () {
            $("#loginMsg")[0].innerHTML = "";
        });

        $("password").focus(function () {
            $("#loginMsg")[0].innerHTML = "";
        });

        $("#getPasswd").click(function () {
            var gui = parent.require('nw.gui');
            gui.Shell.openExternal(parent.AppRemoteSvcCfg.UserSvcGetPassword);
        });

        $("#register").click(function () {
            var gui = parent.require('nw.gui');
            gui.Shell.openExternal(parent.AppRemoteSvcCfg.UserSvcRegister);
        });

        _self.getUserInfo();

    }

    UserLogin.setUserInfo = function () {
        var _self = this;
        var userName = _self.getUserName();
        var passwd = _self.getPassword();
        localStorage["user"] = userName;
        if ($("#savePass").prop("checked")) {
            localStorage["passwd"] = passwd;
        }
        else {
            localStorage.removeItem("passwd");
        }
    }

    UserLogin.getUserInfo = function () {
        var userName = localStorage["user"];
        var passwd = localStorage["passwd"];
        if (passwd) {
            $("#savePass").attr("checked", "checked");
        }
        if (userName) {
            $("#username").val(userName);
        }
        if (passwd) {
            $("#password").val(passwd);
        }

    }
    exports.userLogin = UserLogin;
});