﻿Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'qtip';

    var txtEmail = new Ext.form.TextEmail({
        fieldLabel: 'Email',
        allowBlank: false,
        width: 215,
        tabIndex: 20
    });

    var txtSenha = new Ext.form.TextField({
        fieldLabel: 'Senha',
        inputType: 'password',
        allowBlank: false,
        width: 130,
        tabIndex: 21
    });

    var formLogin = new Ext.form.FormPanel({
        labelWidth: 60,
        width: 490,
        border: false,
        frame: false,
        buttonAlign: 'center',
        renderTo: 'ContIntForm3',
        bodyStyle: 'background-color: transparent;',
        items: [
            txtEmail,
            txtSenha
        ]
    });

    var btnEnviarLogin = new Ext.ImageButton({
        text: 'ENVIAR',
        imgPath: '/img/btn_continuar.png',
        tooltip: 'CONTINUAR',
        tabIndex: 22,
        renderTo: 'div_botao_log',
        handler: function() {
            if (txtEmail.getValue() == '') {
                txtEmail.markInvalid('Este campo é obrigatório');
            }

            if (txtSenha.getValue() == '') {
                txtSenha.markInvalid('Este campo é obrigatório');
            }
            
            var ReturnURL = '';

            if (location.href.indexOf('ReturnURL') > -1) {
                ReturnURL = location.href.substring(location.href.indexOf('ReturnURL') + 10);
            }

            if (formLogin.form.isValid()) {
                formLogin.getForm().submit({
                    method: 'POST',
                    url: 'Login.aspx',
                    waitTitle: 'Aguarde..',
                    waitMsg: 'Enviando informações.',
                    params: {
                        verificaLogin: true,
                        ReturnURL : ReturnURL,
                        txtEmail: txtEmail.getValue(),
                        txtSenha: txtSenha.getValue()
                    },
                    success: function(Resultado, Requisicao) {
                        if (Boolean(Requisicao.result.success)) {
                            if (Boolean(Requisicao.result.info)) {
                                window.location.href = Requisicao.result.location;
                            } else {
                                Ext.Msg.alert('STEEL SEMI JÓIAS', Requisicao.result.msg);

                                formLogin.getForm().reset();
                            }
                        }
                    }
                });
            }
        }
    });
});
