Ext.onReady(function() {
	
	var cookie = new Ext.state.CookieProvider();
	Ext.state.Manager.setProvider(cookie);
	
	var submiter = function(){
		if (form.getForm().isValid()) {
			form.getForm().submit({
				method: 'POST',
				waitTitle: 'Подключение',
				waitMsg: 'Отправка данных...',
				url: 'core/php/login.php',
				success: function(){
					cookie.set('login',Ext.getCmp('on_start_login_field').getValue());
					location.href = './';
				},
				failure: function(forms, action){
					if (action.failureType == 'server') {
						obj = Ext.util.JSON.decode(action.response.responseText);
						Ext.Msg.alert('Oшибка!', 'Неверный логин или пароль');
					} else {
						Ext.Msg.alert('Ошибка!', 'Сервер не отвечает');
					}
				}
			});
		}
	};
	
    var form = new Ext.form.FormPanel({
        baseCls: 'x-plain',
        labelWidth: 55,
        defaultType: 'textfield',
		
        items: [{
            id: 'on_start_login_field',
        	fieldLabel: 'Логин',
            name: 'login',
			allowBlank:false,
            anchor:'100%',
            value: cookie.get('login','')?cookie.get('login',''):'',
            fireKey: function(e){
            	if (e.getKey() == e.ENTER) {
					submiter();
				}
            }
        },{
        	id: 'on_start_pass_field',
            fieldLabel: 'Пароль',
			inputType:'password',
            name: 'pass',
			allowBlank:false,
            anchor: '100%',
            fireKey: function(e){
            	if (e.getKey() == e.ENTER) {
					submiter();
				}
            }
			
        }],
        buttons:[{
                text:'Войти',
                handler: submiter
        }]		
    });
	
    var window = new Ext.Window({
        title: 'Форма входа',
		closable: false,
        width: 300,
        height:130,
        minWidth: 300,
        minHeight: 100,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        items: form
    });


    window.show();
    
    Ext.getCmp('on_start_'+(cookie.get('login','')?'pass':'login')+'_field').focus(false, 50);
	
});