// JavaScript Document
Ext.onReady(function(){
  Ext.QuickTips.init();
  var rooms=new Ext.data.JsonStore({
          url: 'getRooms.php',
          id: 'id',
          root: 'rooms',
          fields: [
            {name:'id'}, 
            {name:'title'}
          ]
      });    
  rooms.load();
  var login_form=new Ext.FormPanel({
    title: 'Войти в чат',
    iconCls: 'logo',
    width: 320,
    autoHeight: true,
    frame: true,
    layout: 'form',
    labelAlign: 'right',
    labelWidth: 100,
    bodyStyle: 'text-align: left;',
    buttonAlign: 'center',
    items: [{
      bodyStyle: 'padding: 5px; text-align: left;',
      frame: false,
      autoHeight: true,
      layout: 'form',
      width: 320,
      items:[
      {
        html: "<div style='padding: 5 5 10 5px;'><font class='text'>Для входа в чат используйте логин и пароль такой же, как для <a href='http://www.buryatia.org'>www.buryatia.org</a>, для новых пользователей регистрация</font> <a href='http://buryatia.org/modules.php?name=Your_Account&op=new_user'>здесь</a></div>"
      },
      {
        xtype: 'textfield',
        fieldLabel: 'Пользователь',
        id: 'login_username',
        allowBlank: false,
        name: 'username',
        width: 150
      },
      {
        xtype: 'textfield',
        fieldLabel: 'Пароль',
        inputType: 'password',
        id: 'login_pswd',
        name: 'password',
        allowBlank: false,
        width: 150
      },
      combo=new Ext.form.ComboBox({
                              xtype: 'combo',
                              labelWidth: 50,
                              labelAlign: 'left',
                              name: 'room',
                              fieldLabel: 'Комната',
                              store: rooms,
                              emptyText: 'Выберите комнату...',
                              editable: false,
                              forceSelection: true,
                              typeAhead: true,
                              triggerAction: 'all',
                              valueField: 'id',
                              displayField: 'title',
                              selectOnFocus: true,
                              width: 150
                        
                          })
      ]
    }
    ],
    buttons:[
      {
        text: 'Вход',
        handler: function(){
          if(Ext.getCmp('login_username').getValue()!="" && Ext.getCmp('login_pswd').getValue()!=""){ 
          login_form.getForm().submit({
            url: 'login.php',
            params: {task: 'login', room_id: combo.value},
            method: 'post',
            waitMsg: 'Заходим...',
            success: function(form,action){
              window.location.replace("index.php");
            },
            failure: function(){
              Ext.MessageBox.alert("Ошибка", "Вход запрещен");
            }
          });
          }else{
            Ext.MessageBox.alert("Ошибка","Заполните все поля");
          }
        }
      }
    
    ]
  
  });
  combo.on('focus', function(){
              rooms.reload();
              combo.render();
            });
  login_form.render('login');
});
