/**
 * Created with JetBrains WebStorm.
 * User: Cmma
 * Date: 7/22/14
 * Time: 5:48 PM
 * To change this template use File | Settings | File Templates.
 */

function UserLogin() {

    var _self = this;


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

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

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

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

    this.init = function() {
        $("#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();

    }

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

    this.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);
        }

    }



}