﻿Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'qtip';

    var stoAssunto = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({ url: 'getData.aspx?Lista=Contato' }),
        reader: new Ext.data.JsonReader({}, ['ID_TP_ASSUNTO', 'ASSUNTO']),
        remoteSort: true
    });
    stoAssunto.load();

    var FormContato = new Ext.form.FormPanel({
        id: 'FormContato',
        labelWidth: 100,
        width: 480,
        border: false,
        frame: false,
        renderTo: 'form_contato',
        bodyStyle: 'background-color:transparent;',
        items: [
            new Ext.form.ComboBox({
                id: 'cmbAssunto',
                store: stoAssunto,
                fieldLabel: '* Assunto',
                allowBlank: false,
                displayField: 'ASSUNTO',
                valueField: 'ID_TP_ASSUNTO',
                typeAhead: true,
                mode: 'local',
                width: 370,
                listWidth: 370,
                forceSelection: true,
                selectOnFocus: true,
                lazyRender: true,
                listClass: 'ajusta_lista',
                emptyText: 'Selecione um assunto',
                tabIndex: 15,
                labelStyle: 'text-align:right;'
            }),
            new Ext.form.TextField({
                id: 'txtNome',
                fieldLabel: '* Nome',
                emptyText: '',
                allowBlank: false,
                width: 370,
                tabIndex: 16,
                labelStyle: 'text-align:right;'
            }),
            new Ext.form.TextField({
                id: 'txtEmail',
                fieldLabel: '* E-mail',
                emptyText: '',
                vtype: 'email',
                allowBlank: false,
                width: 370,
                tabIndex: 17,
                labelStyle: 'text-align:right;'
            }),
            new Ext.form.TextField({
                id: 'txtCidade',
                fieldLabel: '* Cidade',
                emptyText: '',
                width: 370,
                tabIndex: 18,
                allowBlank: false,
                labelStyle: 'text-align:right;'
            }),
            new Ext.form.TextField({
                id: 'txtTelefone',
                fieldLabel: 'Telefone',
                emptyText: '',
                width: 165,
                tabIndex: 19,
                labelStyle: 'text-align:right;',
                maskRe: /[0-9]/,
                maxLength: 14,
                minLength: 14,
                minLengthText: 'Formato correto para este campo é: (00) 0000-0000',
                maxLengthText: 'Formato correto para este campo é: (00) 0000-0000',
                enableKeyEvents: true,
                listeners: {
                    'keydown': {
                        fn: function(a, b) {
                            var v = a.getValue();
                            if (v == null || v == undefined || v == '') {
                                return;
                            }
                            v = v.replace(/\D/g, '');
                            v = v.replace(/^(\d\d)(\d)/g, '($1) $2');
                            v = v.replace(/(\d{4})(\d)/, '$1-$2');
                            this.setValue(v);
                        }
                    }
                }
            }),
            new Ext.form.TextField({
                id: 'txtCelular',
                fieldLabel: 'Celular',
                emptyText: '',
                width: 165,
                tabIndex: 20,
                labelStyle: 'text-align:right;',
                maskRe: /[0-9]/,
                maxLength: 14,
                minLength: 14,
                minLengthText: 'Formato correto para este campo é: (00) 0000-0000',
                maxLengthText: 'Formato correto para este campo é: (00) 0000-0000',
                enableKeyEvents: true,
                listeners: {
                    'keydown': {
                        fn: function(a, b) {
                            var v = a.getValue();
                            if (v == null || v == undefined || v == '') {
                                return;
                            }
                            v = v.replace(/\D/g, '');
                            v = v.replace(/^(\d\d)(\d)/g, '($1) $2');
                            v = v.replace(/(\d{4})(\d)/, '$1-$2');
                            this.setValue(v);
                        }
                    }
                }
            }),
            new Ext.form.TextArea({
                id: 'txaMensagem',
                fieldLabel: '* Mensagem',
                emptyText: '',
                allowBlank: false,
                width: 370,
                height: 200,
                tabIndex: 21,
                labelStyle: 'text-align:right;'
            })
        ],
        buttons: [
            new Ext.Button({
                id: 'limparContato',
                text: 'Limpar',
                tooltip: 'Limpar',
                tabIndex: 25,
                handler: function() {
                    FormContato.getForm().reset();
                }
            }),
            new Ext.Button({
                id: 'enviarContato',
                text: 'Enviar',
                tooltip: 'Enviar',
                tabIndex: 22,
                handler: function() {
                    FormContato.getForm().submit({
                        url: 'Contato.aspx',
                        params: { envia_email: true },
                        method: 'POST',
                        waitTitle: 'Aguarde..',
                        waitMsg: 'Enviando informações...',
                        success: function() {
                            Ext.Msg.alert('Status', 'Mensagem Enviada com sucesso!', function(btn, text) {
                                if (btn == 'ok') {
                                    FormContato.getForm().reset();
                                }
                            });
                        },
                        failure: function(form, action) {
                            if (action.failureType == 'server') {
                                obj = Ext.util.JSON.decode(action.response.responseText);
                                Ext.Msg.alert('Não foi posssivel enviar o e-mail!', obj.errors.reason);
                                FormContato.getForm().reset();
                            }
                        }
                    });
                }
            })
        ]
    });
});
