﻿/*
 * Ext JS Library 2.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.onReady(function(){


    var bd = Ext.get("form_new_item");

    var simple = new Ext.form.FormPanel({
        labelAlign: 'top',
        url:'add_new.php',
        frame:true,
        title: 'e-morze - Dane nowej kwatery',
        bodyStyle:'padding:5px 5px 0',
        width: 450,
        defaults: {width: 350},
        defaultType: 'textfield',
         
        items: [{
                
                            columnWidth:.5,
                            fieldLabel: 'Nazwa*',
                            name: 'nazwa',
                            allowBlank:false
                
            },{
                xtype:'htmleditor',
                id:'opis',
                fieldLabel:'Opis*',
                height:150,
                enableColors: false,
                enableAlignments: false,
                enableLists : false
            },{
                fieldLabel: 'Adres*',
                name: 'adres',
                allowBlank:false
            },{
                fieldLabel: 'Telefon*',
                name: 'telefon',
                allowBlank:false
            }, {
                fieldLabel: 'Email',
                name: 'email',
                allowBlank:true
            }, {
                
                fieldLabel: 'Cena od*',
                name: 'cena_od',
                allowBlank:false
            }, new Ext.form.ComboBox({
                fieldLabel: 'Miasto*',
                name: 'miasto',
                allowBlank:false,
                editable: false,
                blankText: 'Wybierz miasto',
                store: new Ext.data.SimpleStore({
                    fields: ['city'],
                    data: EMorze.miasta,
                    sortInfo: {field: 'city', direction: 'ASC'}
                    }),
                displayField:'city',
                valueField:'city',
                triggerAction:'all',
                mode:'local'
            }), new Ext.form.ComboBox({
                fieldLabel: 'Typ*',
                name: 'typ',
                allowBlank:false,
                editable: false,
                blankText: 'Wybierz typ obiektu',
                store: new Ext.data.SimpleStore({
                    fields: ['item_type'],
                    data: EMorze.typ,
                    sortInfo: {field: 'item_type', direction: 'ASC'}
                    }),
                displayField:'item_type',
                valueField:'item_type',
                triggerAction:'all',
                mode:'local'
            }), new Ext.form.ComboBox({
                fieldLabel: 'Czynne*',
                name: 'czynne',
                allowBlank:false,
                editable: false,
                blankText: 'Wybierz dostępność obiektu',
                store: new Ext.data.SimpleStore({
                    fields: ['czynne'],
                    data: EMorze.czynne,
                    sortInfo: {field: 'czynne', direction: 'ASC'}
                    }),
                displayField:'czynne',
                valueField:'czynne',
                triggerAction:'all',
                mode:'local'
            }), new Ext.form.ComboBox({
                fieldLabel: 'Pakiet*',
                name: 'pakiet',
                allowBlank:false,
                editable: false,
                blankText: 'Wybierz pakiet wyróżniający obiektu',
                store: new Ext.data.SimpleStore({
                    fields: ['pakiet'],
                    data: EMorze.status
                    }),
                displayField:'pakiet',
                valueField:'pakiet',
                triggerAction:'all',
                mode:'local'
            }), {
                fieldLabel: 'Adres strony www',
                name: 'www_address',
                allowBlank:true
            }, {
                fieldLabel: 'Adres do obrazka (w internecie)',
                name: 'image_address',
                allowBlank:true
            }],
            buttons:
            [
                {
                    text: 'Wyślij',
                    formBind: true,
                    handler:function()
                    {
                    simple.getForm().submit({ 
                        method:'POST',
                        waitTitle:'Łączenie', 
                        waitMsg:'Wysyłanie danych, proszę czekać...',
                        success:function(form, action)
                        { 
							simple.getForm().reset();
                        	var redirect = 'index.php'; 
							Ext.Msg.alert('Dodwanie rekordu', 'Informacja o obiekcie została wysłana.\nCzekaj na potwierdzenie dodania e-mailem na adres, który podałeś');
							window.location = redirect;
                        },
 
                        failure:function(form, action)
                        {
                            var reason = 'Nie wszystkie pola zostały wypełnione!';
                            if (null != action.response)
                            {
                                obj = Ext.util.JSON.decode(action.response.responseText);
                                reason = obj.errors.reason;
                            }
                            Ext.Msg.alert('Dodwanie rekordu', reason);
                            
                             
                        } 
                    })
                    }
                },
                {
                    text: 'Wyczyść',
                    formBind: true,
                    handler:function()
                    {
                        simple.getForm().reset()
                    }
                }
            ]
        
    });

    simple.render(bd);

    
});