﻿DIG_SCV_002.QueroMeCadastrar = Ext.extend(Ext.FormPanel, {
    id: 'form_quero_me_cadastrar',
    initComponent: function() {
        var config = {
            bodyStyle: 'background-color:transparent;',
            border: false,
            frame: false,
            labelWidth: 40,
            monitorValid: true,
            items: [
                new Ext.form.TextField({
                    id: 'cpf_cadastro',
                    fieldLabel: 'CPF',
                    plugins: [ new Ext.ux.InputTextMask('999.999.999-99', true) ],
                    width: 190
                }),
                new Ext.form.TextField({
                    id: 'senha_cadastro',
                    fieldLabel: 'Senha',
                    inputType: 'password',
                    width: 190,
                    enableKeyEvents: true,
                    listeners: {
                        'keypress': {
                            fn: function(txt, e) {
                                 if (e.getKey() == e.ENTER) {
				            	    execQueroMecadastrar();
				                }
                            }
                        }
                    }
                })
            ],
            buttonAlign: 'center',
            buttons: [
                new Ext.Button({
                    text: 'CADASTRAR',
                    ctCls: 'btn_transp',
                    handler: function() {                    
                        execQueroMecadastrar();
                    }
                }),
                new Ext.Button({
                    text: '    LIMPAR    ',
                    ctCls: 'btn_transp',
                    handler: function() {
                        Ext.getCmp('form_quero_me_cadastrar').form.reset();
                    }
                })
            ],
            renderTo: 'divQueroMeCadastrar'
        };
		
        Ext.apply(this, Ext.apply(this.initialConfig, config));
        
        DIG_SCV_002.QueroMeCadastrar.superclass.initComponent.call(this);
    }
});

DIG_SCV_002.JaSouCadastrado = Ext.extend(Ext.FormPanel, {
    id: 'form_ja_sou_cadastrado',
    initComponent: function() {
        var config = {
            bodyStyle: 'background-color:transparent;',
            border: false,
            frame: false,
            labelWidth: 40,
            monitorValid: true,
            items: [
                new Ext.form.TextField({
                    id: 'cpf_atualizar',
                    fieldLabel: 'CPF',
                    plugins: [ new Ext.ux.InputTextMask('999.999.999-99', true) ],
                    width: 190
                }),
                new Ext.form.TextField({
                    id: 'senha_atualizar',
                    fieldLabel: 'Senha',
                    inputType: 'password',
                    width: 190,
                    enableKeyEvents: true,
                    listeners: {
                        'keypress': {
                            fn: function(txt, e) {
                                 if (e.getKey() == e.ENTER) {
				            	    execJaSouCadastrado();
				                }
                            }
                        }
                    }
                })
            ],
            buttonAlign: 'center',
            buttons: [
                new Ext.Button({
                    text: ' ATUALIZAR ',
                    ctCls: 'btn_transp',
                    handler: function() {                    
                        execJaSouCadastrado();
                    }
                }),
                new Ext.Button({
                    text: '    LIMPAR    ',
                    ctCls: 'btn_transp',
                    handler: function() {
                        Ext.getCmp('form_ja_sou_cadastrado').form.reset();
                    }
                })
            ],
            renderTo: 'divJaSouCadastrado'
        };
		
        Ext.apply(this, Ext.apply(this.initialConfig, config));
        
        DIG_SCV_002.JaSouCadastrado.superclass.initComponent.call(this);
    }
});

DIG_SCV_002.WindowRecuperarSenha = Ext.extend(Ext.Window, {
    initComponent: function() {
        var config = {
            title: 'Esqueceu sua senha?',
            style: 'padding:2px 2px;',
            layout: 'fit',
            border: false,
	        modal: true,
	        width: 276,
	        height: 128,
            items: [
                new Ext.form.FormPanel({
                    id: 'frm_request_senha',
                    bodyStyle: 'padding: 2px 5px;',
                    labelWidth: 50,
                    items: [
                        new Ext.form.TextField({
                            id: 'senha_cpf',
                            fieldLabel: 'CPF',
                            plugins: [ new Ext.ux.InputTextMask('999.999.999-99', true) ],
                            width: 190,
                            allowBlank: false
                        }),
                        new Ext.form.TextField({
                            id: 'senha_email',
                            fieldLabel: 'E-mail',
                            vtype: 'email',
                            width: 190,
                            allowBlank: false
                        })
                    ]
                })
            ],
            buttons: [
                new Ext.Button({
                    text: 'Recuperar Senha',
                    handler: function() {
                        var senha_cpf = Ext.getCmp('senha_cpf').getValue();
                        var senha_email = Ext.getCmp('senha_email').getValue();
                        
                        if (senha_cpf == '' || senha_email == '') {
                            Ext.Msg.alert(
                                'Aten&ccedil;&atilde;o', 
                                'Por favor, preencha todos os campos.'
                            );
                            
                            return false;
                        };
                    
                        lbegin();
                        
                        var post = {
                            cpf: normalizeValue(senha_cpf),
                            email: normalizeValue(senha_email)
                        };
                    
                        Data.GetWindowRecuperarSenha(post, function(result, response) {
                            var msgBox = '';
                            
                            if (response.result) {
                                msgBox = 'A sua senha foi enviada para o email ' + post.email;
                            } else {
                                msgBox = 'CPF e/ou E-mail inválido(s).';
                            }
                            
                            lend();
                            
                            Ext.MessageBox.show({
                                title: '',
                                msg: msgBox,
                                buttons: Ext.MessageBox.OK,
                                fn: function(btn){
                                    if (btn == 'ok') {
                                        WindowRecuperarSenha.destroy();
                                    }
                                }
                            });
                        });
                    }
                })
            ]
        };
        
        Ext.apply(this, Ext.apply(this.initialConfig, config));
                
        DIG_SCV_002.WindowRecuperarSenha.superclass.initComponent.call(this);
    }
});

// carrega o sistema
var WindowRecuperarSenha = null;

function renderContent() {
    new DIG_SCV_002.QueroMeCadastrar();
    new DIG_SCV_002.JaSouCadastrado();
};

function execQueroMecadastrar() {
    GetSetInfo('form_quero_me_cadastrar', 'cpf_cadastro', 'senha_cadastro', 'cadastro');
};

function execJaSouCadastrado() {
    GetSetInfo('form_ja_sou_cadastrado', 'cpf_atualizar', 'senha_atualizar', 'atualizacao');
};

function GetSetInfo(form_id, cpf_id, senha_id, tipo) {
    if (!Ext.getCmp(form_id).form.isValid()) {
        return;
    }
    
    lbegin();
    
    Data.GetSetSession(
        Ext.getCmp(cpf_id).getValue(),
        Ext.getCmp(senha_id).getValue(),
        tipo,
        function(result, response) {
            lend();
            
            if (response.result == 0) {
                window.location.href = VirtualPath + '/InformacoesPessoais/';
            } else if (response.result == 1) {
                Ext.Msg.alert('', 'Login e/ou senha inválido.');
            } else if (response.result == 2) {
                Ext.Msg.alert('', 'Este CPF já está cadastrado no sistema.');
            } else if (response.result == 3) {
                Ext.Msg.alert('', 'O CPF é inválido.');
            } else {
                Ext.Msg.alert('', 'FALHA GERAL');
            }
        }
    );
};

function execRequestSenha() {
    WindowRecuperarSenha = new DIG_SCV_002.WindowRecuperarSenha();
    WindowRecuperarSenha.show();
};
