var _or_enabled = false;
Ext.onReady(function() {
    Ext.BLANK_IMAGE_URL = '/lib/resources/images/default/s.gif';
    
    check_login = new TK24Backend();
    check_login.load({
        params: {realm: 'ENDUSER_MNGM', action: 'CheckLogin', request: Ext.util.JSON.encode({id_reseller: _resellerid})}
    });

    check_login.on('load',function(store,records,options) {
        _or_enabled = records[0].data['or_enabled'];
        if(records[0].data['logged'] == true) {
            window.location = '/panel.php';
        } else {
            RenderLoginPage();
        }
    });
});


/*==============================================================================
==============================================================================*/
function RenderLoginPage() {

    this.loginBtnHandler = function() {
        if(!loginUsernameField.getValue() || !loginPasswordField.getValue())
            return;
            
        login_form_mask.show();
        login_backend.load({
            params: {
                realm: 'ENDUSER_MNGM',
                action: 'Login', 
                request: Ext.util.JSON.encode({
                    email: loginUsernameField.getValue(),
                    password: loginPasswordField.getValue(),
                    id_reseller: _resellerid
                })
            }
        });
    }

    this.registrationBtnHandler = function() {
        window.location = '/register.php';
    }

    var login_backend = new TK24Backend();
    var loginUsernameField = new Ext.form.TextField({
        name: 'email',
        fieldLabel: 'E-Mail',
        allowBlank: false,
        labelSeparator: '',
        enableKeyEvents: true
    })
    var loginPasswordField = new Ext.form.TextField({
        name: 'password',
        fieldLabel: 'Hasło',
        allowBlank: false,
        inputType: 'password',
        labelSeparator: '',
        enableKeyEvents: true
    });

    
    login_form = new Ext.form.FormPanel({
        collapsible: false,
        width: 300,
        autoHeight: true,
        frame: true,
        title: 'Logowanie do panelu',
        autoScroll: true,
        labelWidth: 50,
        region: 'center',
        bodyStyle: 'padding-left:10px;padding-top: 15px;',
        labelPad: 5,
        defaultType: 'textfield',
        defaults: {
            width: 200
        },
        items: [
            loginUsernameField,loginPasswordField
        ],
        buttons: [
            {text: 'Zaloguj Się', handler: this.loginBtnHandler,scope:this},
            {text: 'Załóż Konto', handler: this.registrationBtnHandler, disabled:!_or_enabled, scope: this}
        ],
        renderTo: 'login-form',
        id: 'login-form-panel'
    });
    login_form_mask = new Ext.LoadMask(login_form.body,{msg: 'Logowanie...'});

    login_backend.on('load',function(store,records,options) {
        //Ext.MessageBox.alert('aaa','bbb');
        
        login_form_mask.hide();
        if(store.reader.BSsuccess == true) {
            login_form.setVisible(false);
            window.location = '/panel.php';
        } else {
            login_form.setVisible(false);
            Ext.MessageBox.show({
               title:'Błąd logowania',
               msg: 'Nieprawidłowy adres e-mail lub hasło',
               buttons: Ext.Msg.OK,
               fn: function() { 
                    login_form.setVisible(true);
                    loginUsernameField.setValue('');
                    loginPasswordField.setValue('');
               },
               icon: Ext.MessageBox.ERROR
            });
        }
    },this);
    
    
    loginUsernameField.on('keypress',function(field,event) {
    	if(event.getKey() == Ext.EventObject.ENTER) {
    		loginPasswordField.focus();
    	}
    },this);
    
    loginPasswordField.on('keypress',function(field,event) {
    	if(event.getKey() == Ext.EventObject.ENTER) {
    		this.loginBtnHandler();
    	}
    },this);

}

