function Auth() {
    this.init();

    this.cfg = {
        hasIcon: true,
        hasSizeBtn: false,
        hasCloseBtn: true,
        hasRefreshBtn: false,
        hasSettingsBtn: false,
        hasDrag: false,
        hasOnCloseConfirm: false,
        isOpenHidden: false,
        isSystem: true,
        title: "User profile",
        module: "Auth"
    }

    this.user = { id: null,
                  email: null,
                  password: null}        




    this.domContent = [
	{ tag: "div", style: {textAlign: "center", padding: "30px"} , childs: [

        { tag: "div", className: "menu_panel", id: "messages", style: {textAlign: "center"}},

	{ tag: "table",  align: "center", style: {width: "400px"}, childs: [ { tag: "tbody", childs: [ 
	
	{ tag: "tr", childs: [ 
	  { tag: "td", className: "menu_panel", style: {textAlign: "center"},
            innerHTML: "<h1>Existing user</h1>"},
         { tag: "td", className: "menu_panel", style: {textAlign: "center"},
           innerHTML: "<h1>New User?</h1>"}]},

	{ tag: "tr", childs: [ 
	  { tag: "td", childs: [
          { tag: "div", className: "menu_panel", style: {textAlign: "center"},
            innerHTML: "<b>Your E-mail</b><br>",
            childs: [{ tag: "input", type: "text", /*size: "30",*/ id: "selectEmail" }]
          },
          { tag: "div", className: "menu_panel", style: {textAlign: "center"},
            innerHTML: "<b>Your password</b><br>",
            childs: [{ tag: "input", type: "password", /*size: "30",*/ id: "selectPassword" }]
          }]},

	  { tag: "td", className: "menu_panel", style: {textAlign: "center"},
            innerHTML: "Registration makes it possible to access your personal startpage from anywhere and makes sure your data isn't lost when cookies are deleted."}]},

	{ tag: "tr", childs: [
          { tag: "td", style: {textAlign: "center"}, childs: [
	    { tag: "input", type: "button", id: "submitButton", value: "Go", events: {onclick: "login()"}}]},
	  { tag: "td", className: "menu_panel", style: {textAlign: "center"},
	    childs: [{ tag: "input", type: "button", id: "registerButton", value: "Register", events: {onclick: "showRegisterPanel()"}}]
	  }]}]}]}]}
	]


    this.getSignature = function() {
        return SYSWIDGET_WKEY+2;
    }

    this.onBuildInterface = function() {
        this.buildDomModel(this.elements.content, this.domContent);

        this.buildDomModel(desktop.elements.authButton, 
                           [{ tag: "span", id:"signinBtn",
                              childs: [createButtonDom("Sign in", "signIn()", "widgets/auth/img/sign_in.gif")]  },
                            { tag: "span", id:"signoutBtn",
                              childs: [createButtonDom("Sign out", "logout()", "widgets/auth/img/sign_out.gif")]  }]);

        if(this.isLogged()) {
            hideEl(this.elements.signinBtn);
        } else {
            hideEl(this.elements.signoutBtn);
        }
    }

    this.showRegisterPanel = function() {
    clearEl(this.elements.content);
    reg = [
        { tag: "div", className: "menu_panel", id: "messages", style: {padding: "10px", textAlign: "center"}},

        { tag: "div", classname: "menu_panel", style: {textAlign: "center"},
          innerHTML: "<h1>Sign up for free</h1>"},

        { tag: "div", classname: "menu_panel", style: {textAlign: "center"},
          innerHTML: "<b>your e-mail</b><br>",
          childs: [{ tag: "input", type: "text", size: "50", id: "selectEmail" }]
        },

        { tag: "div", classname: "menu_panel", style: {textAlign: "center"},
          innerHTML: "<b>your password</b><br>",
          childs: [{ tag: "input", type: "password", size: "50", id: "selectPassword" }]
        },

        { tag: "div", className: "menu_panel", style: {textAlign: "center"},
          childs: [{ tag: "input", type: "button", id: "submitButton", value: " Register ", events: {onclick: "registerUser()"}}]
        }

    ];
	this.buildDomModel(this.elements.content, reg);
    }

    this.close = function() {
        desktop.closeCurrentPage();
    }


    this.signIn = function() {
        desktop.showPage('auth');
    }



    this.isLogged = function() {
        return this.user.email != '';
    }

    this.login = function() {
        var errors = [];
        email = trim(this.elements.selectEmail.value);
        password = trim(this.elements.selectPassword.value);
        hideEl(this.elements.submitButton);
        this.elements.messages.innerHTML = "Processing ...";
        request.send({act: "login", email: email, password: password}, this);
    }


    this.logout = function() {
        request.send({act: "logout"}, this);
    }



    this.loadUser = function() {
        this.anonymusId = getCookie("anonymusid");
        this.user.id = getCookie("userid");
        this.user.email = getCookie("useremail");
        this.user.password = getCookie("userpassword");
    }


    this.saveUser = function() {
        setCookie("userid", this.user.id);
        setCookie("useremail", this.user.email);
        setCookie("userpassword", this.user.password);
    }





    this.start = function() {
        this.loadUser();
        if(this.user.email && this.user.password) {
            request.send({act: "start_user", user_id: this.user.id, email: this.user.email, password: this.user.password}, this);
        } else {
            request.send({act: "start_anonymus", user_id: this.user.id}, this);
        }
    }



    this.dispatchMsg = function(msg) {
        switch(msg.status) {
            case 'start_result': 
                this.user = msg.user;
                this.saveUser();
                this.onLoad();
                break;

            case 'register_new':
                this.user = msg.user;
                this.elements.messages.innerHTML = "<b>Thanks for registration !!!</b>";
                this.elements.submitButton.style.display="inline";
                break;

            case 'user_ok':
                if(!this.user.email) {
                    setCookie("anonymusid", this.user.id);
                }
                this.user = msg.user;
                this.saveUser();
                location.reload();
                break;


            case 'wrong_password':
                this.elements.messages.innerHTML = "<b>Incorrect password for "+msg.email+"</b>";
                this.elements.submitButton.style.display="inline";
                break;

            case 'logout_ok':
                var anonymusId = getCookie("anonymusid");
                if(anonymusId) {
                    this.user = {id: anonymusId, email: "", password: ""};
                    this.saveUser();
                } else {
                    delCookie("userid");
                    delCookie("useremail");
                    delCookie("userpassword");
                }
                location.reload();
                break;
            }
        }


 
    this.onLoad = function() {
	kernel.imgCache.auth = {};
	kernel.imgCache.auth.singin = new Image();
	kernel.imgCache.auth.singin.src = "widgets/auth/img/sign_in.gif";
	kernel.imgCache.auth.singout = new Image();
	kernel.imgCache.auth.singout.src = "widgets/auth/img/sign_out.gif";
    }


    this.registerUser = function() {
	alert("!");
        var errors = [];
        email = trim(this.elements.selectEmail.value);
        password = trim(this.elements.selectPassword.value);
        if(!this.checkEmail(email)) {
            errors[errors.length] = "<b>Invalid email.</b>";
        }
        if(!this.checkPassword(password)) {
            errors[errors.length]= "<b>Invalid password.</b> Password length must be > 5 symbols.";
        }
        if(errors.length > 0) {
            this.elements.messages.innerHTML = errors.join("<BR />");
        } else {
            hideEl(this.elements.submitButton);
            this.elements.messages.innerHTML = "Processing ...";
            request.send({act: "register_user", email: email, password: password}, this);
        }
    }



    this.checkEmail = function(str) {
        return /^[\_a-zA-Z][a-z0-9\-\_\.]{1,40}@[a-z0-9\-\_\.]{5,60}$/.test(str);
    }

    this.checkPassword = function(str) {
        return (str.length > 5);
    }
}
Auth.prototype = new Widget();
