Login = new ( Class.create({
    
    initialize : function()
    {
    },
    
    LoginData: null,
    
    LoadTo: function(container)
    {
        var thisClass = this;
        
        this.LoginData = ActionHandler.GetResponse(
        {
            url: "CommonComponents/Login/LoginActionHandler.php",
            action: "GetLoginData"
        });
        
        var url;
        if(this.LoginData.isSessionValid) { url = "CommonComponents/Login/Logout.php"; }
        else { url = "CommonComponents/Login/Login.php"; }
        
        new Ajax.Updater(
            container,
            url,
            {
                parameters: "",
                onComplete: function() { thisClass.SetEvents(); thisClass.InitializeControl(); }
            }
        );
    },
    
    SetEvents: function() {
        if(this.LoginData.isSessionValid)
        {
            Event.observe('logoutButton', 'click', this.LogoutButtonClick.bindAsEventListener(this));
        }
        else
        {
            var thisClass = this;
            Event.observe('password', 'keypress', function(event) { if(event.keyCode == 13) { thisClass.LoginButtonClick(event) } } );
            Event.observe('loginButton', 'click', this.LoginButtonClick.bindAsEventListener(this));
            Event.observe('loginForgivenPasswordLink', 'click', this.ForgivenPasswordClick.bindAsEventListener(this));
        }
    },
    
    InitializeControl: function()
    {
        if($("userName") != null)
        {
            if(this.LoginData.email != "")
            {
                $("userName").value = this.LoginData.email;
                if($("password") != null) { $("password").focus(); }
            }                    
            else
            {
                $("userName").focus();
            }
        }
    },
    
    LoginButtonClick: function(event)
    {
        Event.stop(event);
        loginResult = ActionHandler.GetResponse(
        {
            url: "CommonComponents/Login/LoginActionHandler.php",
            action: "Login",
            parameters: "&userName=" + $('userName').value + "&password=" + $('password').value
        });
        if(loginResult.HasError)
        {
            $("loginErrorMessage").innerHTML = loginResult.Message;
        }
        else
        {
            $("loginErrorMessage").innerHTML = "";
            this.LoggedIn(loginResult.role);
        }
    },
    
    ForgivenPasswordClick: function(event)
    {
        Event.stop(event);
        
        var forgivenPasswordWindow = new Window({
            container: "window",
            title: "Elfelejtett jelszó",
            width: 400,
            contentURL: "CommonComponents/Login/ForgivenPassword.php"
        });
        
        forgivenPasswordWindow.Show();
        $("userName").focus();
        
        var thisClass = this;
        
        Event.observe("Cancel", "click", function(event) { Event.stop(event); forgivenPasswordWindow.Close(); } );
        Event.observe("OK", "click", function(event) { Event.stop(event); thisClass.SendNewPassword(); forgivenPasswordWindow.Close(); });
    },
    
    SendNewPassword: function()
    {
        ActionHandler.GetResponse(
        {
            url: "CommonComponents/Login/LoginActionHandler.php",
            action: "SendNewPassword",
            parameters: "&userName=" + $('userName').value
        });
    },
    
    LogoutButtonClick: function(event)
    {
        Event.stop(event);
        var role = ActionHandler.GetResponse(
        {
            url: "CommonComponents/Login/LoginActionHandler.php",
            action: "Logout"
        });
        
        var confirmWindow = new Window({
            container: "window",
            title: "Kilépés",
            width: 400,
            contentURL: "CommonComponents/Login/Confirm.php"
        });
        
        confirmWindow.Show();
        
        var thisClass = this;
        
        Event.observe("ForceLogout", "click", function(event) { Event.stop(event); thisClass.ForceLogout(); } );
        Event.observe("Close", "click", function(event) { Event.stop(event); confirmWindow.Close(); });
    },
    
    ForceLogout : function()
    {
        ActionHandler.GetResponse(
        {
            url: "CommonComponents/Login/LoginActionHandler.php",
            action: "ForceLogout"
        });
        window.location = "http://www.comfy.hu/";
    }
    
}))();

