var form_subfields;

var SubscriptionDialog = function(){

    var dialog, showBtn;
    

    return {
        init : function(){
             showBtn = Ext.get('sub-dialog-btn');
             if (showBtn) {
        	 showBtn.on('click', this.showDialog, this);
	     }
        },
       
        showDialog :  function(){
            if(!dialog){ 
                dialog = new Ext.BasicDialog("subscription-dlg", {
                        autoTabs:false,
                        width:500,
                        height:250,						
                        shadow:true,
                        minWidth:300,
                        minHeight:250
                });
                dialog.addKeyListener(27, dialog.hide, dialog);
                dialog.addButton('Закрыть', dialog.hide, dialog);
            }			
            dialog.show(showBtn.dom);
        }
	}
}();

var form_sub = new Ext.form.Form({
        labelAlign: 'right',
        labelWidth: 120,
        
        reader : new Ext.data.XmlReader({
            record : 'auth',
            success: '@success'
        }, [

            'fname', 'fstyle'
        ]),

        // reusable eror reader class defined at the end of this file
        errorReader: new Ext.form.XmlErrorReader()		
    });

   form_sub.fieldset(
        {legend:'Параметры подписки'},
        new Ext.form.TextField({
            fieldLabel: 'Название фильтра',
            name: 'fname',
			value: '',
            width:300
        }),
        new Ext.form.ComboBox({
            fieldLabel: 'Формат подписки',
            hiddenName:'fstyle',
            store: new Ext.data.SimpleStore({
                fields: ['code', 'name'],
                data : Ext.enotdata.fstyles
            }),
            displayField:'name',
			valueField: 'code',
            typeAhead: true,
            mode: 'local',
            triggerAction: 'all',
            emptyText:'Формат подписки',
			value: '0',
            selectOnFocus:true,
            width:300
        }),
        new Ext.form.ComboBox({
            fieldLabel: 'Периодичность',
            hiddenName:'ftype',
            store: new Ext.data.SimpleStore({
                fields: ['code', 'name'],
                data : Ext.enotdata.ftypes
            }),
            displayField:'name',
			valueField: 'code',
            typeAhead: true,
            mode: 'local',
            triggerAction: 'all',
            emptyText:'Периодичность',
			value: '1',
            selectOnFocus:true,
            width:300
        })
   );
   
    form_sub.addButton(
	{text: 'Создать',
	 handler: function() {	 			
	 	form_sub.baseParams = {'fquery' : form_subfields.getValues(true)};
	 	form_sub.submit(
		{url:"/account/mailing/add/",
    		success:function(form, action){ 
	  			Ext.MessageBox.alert('','Подписка успешно создана');
				SubscriptionDialog.dialog.hide();					  
    		},								

    		// callback handler if submit has failed
    		failure: function(form, action) {
      			Ext.MessageBox.alert('Ошибка', 'Достигнут максимальный предел подписок');
    		}		
		});
		
	 }});
    form_sub.addButton(
	{text: 'Создать и перейти к списку',
	 handler: function() {
	 	form_sub.baseParams = {'fquery' : form_subfields.getValues(true)};
	 	form_sub.submit(
		{url:"/account/mailing/add/",
    		success:function(form, action){ 
	  			window.location = '/account/mailing/';  
    		},								

    		// callback handler if submit has failed
    		failure: function(form, action) {
      			Ext.MessageBox.alert('Ошибка', 'Достигнут максимальный предел подписок');
    		}		
		});
		
	 }});



