﻿Ext.onReady(function () {
    //    Ext.Msg.alert('wadsad');
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'qtip';

    var stoAssunto = new Ext.data.Store
     ({
         proxy: new Ext.data.HttpProxy({ url: '/GetData/ListarTipoAssunto' }),
         reader: new Ext.data.JsonReader({}, ['id_assunto_contato', 'assunto']),
         baseParams: {
             listarTpAssunto: true

         },
         callback: function () {
         },

         remoteSort: true
     });

    stoAssunto.load();
    new Ext.form.ComboBox
     ({
         id: 'cmbAssunto',
         store: stoAssunto,
         fieldLabel: '* Assunto',
         displayField: 'assunto',
         valueField: 'assunto',
         typeAhead: true,
         mode: 'local',
         width: 380,
         listWidth: 363,
         forceSelection: true,
         selectOnFocus: true,
         style: 'height: 20px; padding: 10px;',
         renderTo: 'div_assunto',
         allowBlank: false,
         lazyRender: true,
         listClass: 'ajusta_lista',
         triggerClass: 'x-form-combo-site-trigger',
         triggerAction: 'all',
         emptyText: 'Selecione um assunto',
         tabIndex: 101,
         labelStyle: 'text-align:left;'
     });

    new Ext.form.TextField
     ({
         id: 'txtNome',
         fieldLabel: '* Nome',
         emptyText: '',
         allowBlank: false,
         width: 364,
         renderTo: 'div_nome',
         style: 'height: 20px; padding: 10px;',
         tabIndex: 102,
         labelStyle: 'text-align:left;'
     });

    new Ext.form.TextField
     ({
         id: 'txtEmail',
         fieldLabel: '* E-mail',
         emptyText: '',
         style: 'height: 20px; padding: 10px;',
         vtype: 'email',
         allowBlank: false,
         renderTo: 'div_email',
         width: 364,
         tabIndex: 103,
         labelStyle: 'text-align:left;'
     });

    new Ext.form.TextField
     ({
         id: 'txtTelefone',
         fieldLabel: ' Telefone',
         emptyText: '',
         width: 180,
         style: 'height: 20px; padding: 10px;',
         tabIndex: 104,
         renderTo: 'div_tel',
         labelStyle: 'text-align:left;',
         maskRe: /[0-9]/,
         maxLength: 14,
         minLength: 14,
         allowBlank: false,
         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: '',
             allowBlank: true,
             width: 173,
             style: 'height: 20px; padding: 10px;',
             renderTo: 'div_cel',
             tabIndex: 105,
             labelStyle: 'text-align:left;',
             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: 'txtCidade',
         fieldLabel: '* Cidade',
         emptyText: '',
         allowBlank: false,
         style: 'height: 20px; padding: 10px;',
         renderTo: 'div_cidade',
         width: 364,
         tabIndex: 106,
         labelStyle: 'text-align:left;'
     });

    new Ext.form.TextArea
         ({
             id: 'txaMensagem',
             fieldLabel: '* Mensagem',
             emptyText: '',
             allowBlank: false,
             style: 'height: 20px; padding: 10px;',
             renderTo: 'div_mensagem',
             width: 364,
             height: 165,
             tabIndex: 107,
             labelStyle: 'text-align:left;'
         })

    new Ext.ImageButton
            ({
                id: 'enviarMensagem',
                imgPath: '/img/btn_enviar.gif',
                text: 'Enviar',
                renderTo: 'botao_enviar',
                tooltip: 'Enviar',
                tabIndex: 108,
                handler: function () {
                    // valida os campos
                    var error = false;

                    var params = {};

                    Ext.each(Ext.ComponentMgr.all.items, function (item) {
                        if (item.isValid && !item.isValid()) {
                            error = true;
                        };

                        if (item.id && item.getValue) {
                            params[item.id] = item.getValue();

                            if (item.id.substring(0, 2) == 'dt' && params[item.id] != '') {
                                params[item.id] = item.getValue().dateFormat('d/m/y');
                            };
                        };
                    });
                    // fim

                    if (error) {
                        alert('Preencha todos os campos corretamente');
                        return false;
                    };

                    Ext.MessageBox.wait('Aguarde um momento..');
                    Ext.Ajax.request({
                        url: '/FaleConosco',
                        params: {
                            envia_email: true,
                            cmbAssunto: Ext.getCmp('cmbAssunto').getValue(),
                            txtNome: Ext.getCmp('txtNome').getValue(),
                            txtEmail: Ext.getCmp('txtEmail').getValue(),
                            txtTelefone: Ext.getCmp('txtTelefone').getValue(),
                            txtCelular: Ext.getCmp('txtCelular').getValue(),
                            txtCidade: Ext.getCmp('txtCidade').getValue(),
                            txaMensagem: Ext.getCmp('txaMensagem').getValue()
                        },

                        method: 'POST',
                        waitTitle: 'Aguarde..',
                        waitMsg: 'Enviando informações...',
                        success: function () {
                            Ext.Msg.alert('Status', 'Mensagem Enviada com sucesso!', function (btn, text) {
                                if (btn == 'ok') {
                                    //FormMensagem.getForm().reset();
                                    Ext.getCmp('cmbAssunto').setValue(''),
                                    Ext.getCmp('txtNome').setValue(''),
                                    Ext.getCmp('txtEmail').setValue(''),
                                    Ext.getCmp('txtTelefone').setValue(''),
                                    Ext.getCmp('txtCelular').setValue(''),
                                    Ext.getCmp('txtCidade').setValue(''),
                                    Ext.getCmp('txaMensagem').setValue('')
                                }
                            });
                        },
                        failure: function (form, action) {
                            if (action.failureType == 'server') {
                                obj = Ext.util.JSON.decode(action.response.responseText);
                                Ext.Msg.alert('Não foi posssivel enviar a mensagem!', obj.errors.reason);
                                Ext.getCmp('cmbAssunto').setValue(''),
                                    Ext.getCmp('txtNome').setValue(''),
                                    Ext.getCmp('txtEmail').setValue(''),
                                    Ext.getCmp('txtTelefone').setValue(''),
                                    Ext.getCmp('txtCelular').setValue(''),
                                    Ext.getCmp('txtCidade').setValue(''),
                                    Ext.getCmp('txaMensagem').setValue('')
                            }
                        }
                    });
                }

            });
});



