﻿Ext.apply(Ext.form.VTypes, {
    password: function(val, field) {
        if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },

    passwordText: 'A confirmação da senha digitada é diferente da senha informada'
});

Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'qtip';

    var hidNovoCadastro = new Ext.form.Hidden({
        id: 'hidNovoCadastro',
        value: true
    });

    var txtNomeFantasia = new Ext.form.TextField({
        id: 'txtNomeFantasia',
        fieldLabel: '* Nome / Nome Fantasia',
        tabIndex: 1,
        width: 200,
        allowBlank: false
    });

    var txtNomeRazaoSocial = new Ext.form.TextField({
        id: 'txtNomeRazaoSocial',
        fieldLabel: '* Sobrenome / Razão Social',
        tabIndex: 2,
        width: 200,
        allowBlank: false
    });

    var txtCPFCNPJ = new Ext.form.TextCPFCNPJ({
        id: 'txtCPFCNPJ',
        fieldLabel: '* CPF / CNPJ',
        tabIndex: 3,
        width: 200,
        allowBlank: false
    });

    var txtRGIE = new Ext.form.TextField({
        id: 'txtRGIE',
        fieldLabel: '* RG / IE',
        tabIndex: 4,
        width: 200,
        listeners: {
            blur: function() {
                if (this.getValue() != '' && this.isValid()) {
                    Ext.Ajax.request({
                        url: '/GetData.aspx',
                        method: 'POST',
                        params: {
                            verificaRGIE: true,
                            txtRGIE: this.getValue()
                        },
                        success: function(Resultado, Requisicao) {
                            res = new Object();
                            res = Ext.util.JSON.decode(Resultado.responseText);

                            if (Boolean(res.success)) {
                                if (!Boolean(res.info)) {
                                    Ext.Msg.alert('Lumavi', 'Este RG / IE já está cadastrado.');

                                    txtRGIE.setValue('');
                                    txtRGIE.focus();
                                }
                            }
                        }
                    });
                }
            }
        },
        allowBlank: false
    });

    var txtSenha = new Ext.form.TextField({
        id: 'txtSenha',
        fieldLabel: '* Senha',
        inputType: 'password',
        tabIndex: 5,
        width: 200,
        allowBlank: false
    });

    var txtConfirmaSenha = new Ext.form.TextField({
        id: 'txtConfirmaSenha',
        fieldLabel: '* Confirma Senha',
        inputType: 'password',
        vtype: 'password',
        initialPassField: 'txtSenha',
        tabIndex: 6,
        width: 200,
        allowBlank: false
    });

    var hidIdTpLogradouro = new Ext.form.Hidden({
        id: 'hidIdTpLogradouro',
        value: ''
    });

    var stoTpLogradouro = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({ url: '/GetData.aspx' }),
        reader: new Ext.data.JsonReader({}, [
            'id_tp_logradouro',
            'descricao'
        ]),
        baseParams: {
            listarTpLogradouro: true
        },
        remoteSort: true
    });
    stoTpLogradouro.load();

    var cmbTpLogradouro = new Ext.form.ComboBox({
        id: 'cmbTpLogradouro',
        fieldLabel: '* Logradouro',
        store: stoTpLogradouro,
        displayField: 'descricao',
        valueField: 'id_tp_logradouro',
        mode: 'local',
        triggerAction: 'all',
        typeAhead: true,
        loadingText: 'Carregando...',
        width: 200,
        tabIndex: 7,
        allowBlank: false,
        onSelect: function(record) {
            hidIdTpLogradouro.setValue(record.json.id_tp_logradouro);

            this.setValue(record.json.descricao);
            this.collapse();
        }
    });

    var txtEndereco = new Ext.form.TextField({
        id: 'txtEndereco',
        fieldLabel: ' Endereço',
        tabIndex: 8,
        width: 200,
        allowBlank: true
    });

    var txtNumero = new Ext.form.TextField({
        id: 'txtNumero',
        fieldLabel: ' Número',
        tabIndex: 9,
        width: 200,
        allowBlank: true
    });

    var txtComplemento = new Ext.form.TextField({
        id: 'txtComplemento',
        fieldLabel: ' Complemento',
        tabIndex: 10,
        width: 200,
        allowBlank: true
    });

    var txtBairro = new Ext.form.TextField({
        id: 'txtBairro',
        fieldLabel: ' Bairro',
        tabIndex: 11,
        width: 200,
        allowBlank: true
    });

    var txtCEP = new Ext.form.TextCEP({
        id: 'txtCEP',
        fieldLabel: '* CEP',
        tabIndex: 12,
        width: 200,
        allowBlank: false
    });

    var txtCidade = new Ext.form.TextField({
        id: 'txtCidade',
        fieldLabel: '* Cidade',
        tabIndex: 13,
        width: 200,
        allowBlank: false
    });

    var hidIdEstado = new Ext.form.Hidden({
        id: 'hidIdEstado',
        value: ''
    });

    var stoEstado = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({ url: '/GetData.aspx' }),
        reader: new Ext.data.JsonReader({}, [
            'id_estado',
            'descricao'
        ]),
        baseParams: {
            listarEstado: true
        },
        remoteSort: true
    });
    stoEstado.load();

    var cmbEstado = new Ext.form.ComboBox({
        id: 'cmbEstado',
        fieldLabel: '* Estado',
        store: stoEstado,
        displayField: 'descricao',
        valueField: 'id_estado',
        mode: 'local',
        triggerAction: 'all',
        typeAhead: true,
        loadingText: 'Carregando...',
        width: 175,
        tabIndex: 14,
        allowBlank: false,
        onSelect: function(record) {
            hidIdEstado.setValue(record.json.id_estado);

            this.setValue(record.json.descricao);
            this.collapse();
        }
    });

    var txtTelefone1 = new Ext.form.TextTelefone({
        id: 'txtTelefone1',
        fieldLabel: '* Telefone 1',
        tabIndex: 15,
        width: 200,
        allowBlank: false
    });

    var txtTelefone2 = new Ext.form.TextTelefone({
        id: 'txtTelefone2',
        fieldLabel: ' Telefone 2',
        tabIndex: 16,
        width: 200,
        allowBlank: true
    });

    var txtEmail = new Ext.form.TextEmail({
        id: 'txtEmail',
        fieldLabel: '* Email',
        tabIndex: 17,
        width: 200,
        listeners: {
            blur: function() {
                if (this.getValue() != '' && this.isValid()) {
                    Ext.Ajax.request({
                        url: '/GetData.aspx',
                        method: 'POST',
                        params: {
                            verificaEmail: true,
                            txtEmail: this.getValue()
                        },
                        success: function(Resultado, Requisicao) {
                            res = new Object();
                            res = Ext.util.JSON.decode(Resultado.responseText);

                            if (Boolean(res.success)) {
                                if (!Boolean(res.info)) {
                                    Ext.Msg.alert('Lumavi', 'Este Email já está cadastrado.');

                                    txtEmail.setValue('');
                                    txtEmail.focus();
                                }
                            }
                        }
                    });
                }
            }
        },
        allowBlank: false
    });

    var txtSite = new Ext.form.TextField({
        id: 'txtSite',
        fieldLabel: ' Site',
        tabIndex: 18,
        width: 200,
        allowBlank: true
    });
    
    var formCadastro = new Ext.form.FormPanel({
        frame: false,
        border: false,
        bodyStyle: 'background-color:transparent;',
        renderTo: 'ContIntFormCadUsuario',
        layout: 'tableform',
        labelWidth: 115,
        layoutConfig: {
            columns: 2
        },
        formConfig: {
            border: false,
            bodyStyle: 'padding:3px; background-color:transparent;'
        },
        defaults: {
            border: false,
            bodyStyle: 'background-color:transparent;'
        },
        items: [
            {
                layout: 'form',
                colspan: 2,
                items: [hidNovoCadastro, hidIdTpLogradouro, hidIdEstado]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtNomeFantasia]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtNomeRazaoSocial]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtCPFCNPJ]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtRGIE]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtSenha]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtConfirmaSenha]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [cmbTpLogradouro]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtEndereco]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtNumero]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtComplemento]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtBairro]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtCEP]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtCidade]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [cmbEstado]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtTelefone1]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtTelefone2]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtEmail]
            },
            {
                layout: 'form',
                colspan: 1,
                items: [txtSite]
            }
        ],
        buttons: [
            new Ext.ImageButton({
                text: 'LIMPAR',
                imgPath: '/img/btn_limpar.png',
                tooltip: 'LIMPAR',
                tabIndex: 28,
                handler: function() {
                    formCadastro.getForm().reset();
                }
            }),
            new Ext.ImageButton({
                text: 'ENVIAR',
                imgPath: '/img/btn_cadastrar.png',
                tooltip: 'ENVIAR',
                tabIndex: 29,
                handler: function() {
                    if (formCadastro.form.isValid()) {
                        formCadastro.getForm().submit({
                            method: 'POST',
                            url: '/GetData.aspx',
                            waitTitle: 'Aguarde..',
                            waitMsg: 'Enviando informações.',
                            success: function(Resultado, Requisicao) {
                                if (Boolean(Requisicao.result.success)) {
                                    if (Boolean(Requisicao.result.info)) {
                                        window.location.href = '/Produtos.aspx?id=1';
                                    } else {
                                        Ext.Msg.alert('Lumavi - arte em iluminação', Requisicao.result.msg);

                                        //formCadastro.getForm().reset();
                                    }
                                }
                            }
                        });
                    }
                }
            })
        ]
    });

    var arr = new Array();
    var qryStr = window.location.search.substring(1);

    if (qryStr != '') {
        arr = qryStr.split('=');
        txtCPFCNPJ.setValue(arr[1]);
    }
});
