﻿/* CPF/CNPJ */

Ext.form.VTypes['cnpjVal'] = /^([0-9]{3}.[0-9]{3}.[0-9]{3}\/[0-9]{4}-[0-9]{2})|([0-9]{2}.[0-9]{3}.[0-9]{3}\/[0-9]{4}-[0-9]{2})$/;
Ext.form.VTypes['cnpjMask'] = /[0-9]/;
Ext.form.VTypes['cnpjText'] = '<b>CNPJ</b> está incorreto, ex: 999.999.999/9999-99';
Ext.form.VTypes['cnpj'] = function(v) { if (Ext.form.VTypes['cnpjVal'].test(v) && VerificaCNPJ(v)) { return true; } else { return false; } }

Ext.form.VTypes['cpfVal'] = /^[0-9]{3}.[0-9]{3}.[0-9]{3}-[0-9]{2}$/;
Ext.form.VTypes['cpfMask'] = /[0-9]/;
Ext.form.VTypes['cpfText'] = '<b>CPF</b> está incorreto, ex: 999.999.999-99';
Ext.form.VTypes['cpf'] = function(v) { if (Ext.form.VTypes['cpfVal'].test(v) && VerificaCPF(v)) { return true; } else { return false; } }

Ext.form.TextCPFCNPJ = Ext.extend(Ext.form.TextField, {
    fieldLabel: 'CPF/CNPJ',
    //vtype: 'cpf',
    enableKeyEvents: true,
    listeners: {
        /*'keydown': {
        fn: function(a, b) {
        if (b.getKey() != b.BACKSPACE && b.getKey() != b.DELETE) {
        var v = a.getValue();

                    if (this.vtype == 'cpf') {
        Ext.form.VTypes['cpfText'] = '<b>CPF/CNPJ</b> está incorreto, ex: 999.999.999-99';

                        if (v.length == 3) {
        this.setValue(v + '.');
        }
        else if (v.length == 7) {
        this.setValue(v + '.');
        }
        else if (v.length == 11) {
        this.setValue(v + '-');
        }
        }
        else if (this.vtype == 'cnpj') {
        Ext.form.VTypes['cnpjText'] = '<b>CPF/CNPJ</b> está incorreto, ex: 999.999.999/9999-99';
        }
        }
        }
        },*/
        'blur': {
            fn: function(a) {
                var v = a.getValue();

                /*11
                14 / 15*/

                if (v.replace('.', '').replace('.', '').replace('/', '').replace('-', '').length == 11) {
                    this.vtype = 'cpf';

                    //v = v.substring(0, 3) + '.' + v.substring(3, 6) + '.' + v.substring(6, 9) + '-' + v.substring(9, 11);
                } else if (v.replace('.', '').replace('.', '').replace('/', '').replace('-', '').length == 14 || v.replace('.', '').replace('.', '').replace('/', '').replace('-', '').length == 15) {
                    this.vtype = 'cnpj';

                    if (v.length == 14) {
                        v = '0' + v;
                    }

                    /*if (v.length == 15) {
                    v = v.substring(0, 3) + '.' + v.substring(3, 6) + '.' + v.substring(6, 9) + '/' + v.substring(9, 13) + '-' + v.substring(13, 15);
                    }*/
                }

                if (this.vtype == 'cpf') {
                    if (v.replace('.', '').replace('.', '').replace('/', '').replace('-', '').length == 11) {
                        if (!VerificaCPF(v.replace('.', '').replace('.', '').replace('/', '').replace('-', ''))) {
                            Ext.Msg.alert('Steel Semi Jóias', 'CPF é inválido.');
                            this.markInvalid('CPF é inválido.');
                            return;
                        }
                    }
                }
                else if (this.vtype == 'cnpj') {
                    if (v.replace('.', '').replace('.', '').replace('/', '').replace('-', '').length == 14) {
                        v = '0' + v;
                    }

                    if (!VerificaCNPJ(v.replace('.', '').replace('.', '').replace('/', '').replace('-', ''))) {
                        Ext.Msg.alert('Steel Semi Jóias', 'CNPJ é inválido.');
                        this.markInvalid('CNPJ é inválido.');
                        return;
                    }
                }

                this.setValue(v);
            }
        }
    }
});

Ext.reg('textcpfcnpj', Ext.form.TextCPFCNPJ);

/* EMAIL */

Ext.form.VTypes['emailVal'] = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
Ext.form.VTypes['emailMask'] = /([0-9])|([a-z])|([A-Z])|([@.\-_])/;
Ext.form.VTypes['emailText'] = '<b>E-mail</b> está incorreto, ex: seunome@dominio.com.br';
Ext.form.VTypes['email'] = function(v) { return Ext.form.VTypes['emailVal'].test(v); }

Ext.form.TextEmail = Ext.extend(Ext.form.TextField, {
    fieldLabel: 'E-mail',
    vtype: 'email'
});

Ext.reg('textemail', Ext.form.TextEmail);

/* TELEFONE */

Ext.form.VTypes['telefoneVal'] = /^([0-9]{2}-[0-9]{4}-[0-9]{4})$/;
Ext.form.VTypes['telefoneMask'] = /[0-9]/;
Ext.form.VTypes['telefoneText'] = '<b>Telefone</b> está incorreto, ex: 99-9999-9999';
Ext.form.VTypes['telefone'] = function(v) { return Ext.form.VTypes['telefoneVal'].test(v); }

Ext.form.TextTelefone = Ext.extend(Ext.form.TextField, {
    fieldLabel: 'Telefone',
    vtype: 'telefone',
    enableKeyEvents: true,
    listeners: {
        'keydown': {
            fn: function(a, b) {
                if (b.getKey() != b.BACKSPACE && b.getKey() != b.DELETE) {
                    var v = a.getValue();

                    if (this.vtype == 'telefone') {
                        if (v.length == 2) {
                            this.setValue(v + '-');
                        }

                        if (v.length == 7) {
                            this.setValue(v + '-');
                        }
                    }
                }
            }
        }
    }
});

Ext.reg('texttelefone', Ext.form.TextTelefone);

/* CEP */

Ext.form.VTypes['cepVal'] = /^[0-9]{5}-[0-9]{3}$/;
Ext.form.VTypes['cepMask'] = /[0-9]/;
Ext.form.VTypes['cepText'] = '<b>CEP</b> está incorreto, ex: 99999-999';
Ext.form.VTypes['cep'] = function(v) { return Ext.form.VTypes['cepVal'].test(v); }

Ext.form.TextCEP = Ext.extend(Ext.form.TextField, {
    fieldLabel: 'CEP',
    vtype: 'cep',
    enableKeyEvents: true,
    listeners: {
        'keydown': {
            fn: function(a, b) {
                if (b.getKey() != b.BACKSPACE && b.getKey() != b.DELETE) {
                    var v = a.getValue();

                    if (this.vtype == 'cep') {
                        if (v.length == 5) {
                            a.setValue(v + '-');
                        }
                    }
                }
            }
        }
    }
});

Ext.reg('textcep', Ext.form.TextCEP);

/* LAYOUT */

Ext.namespace('Ext.ux.layout');

Ext.ux.layout.TableFormLayout = Ext.extend(Ext.layout.TableLayout, {
    renderAll: function(ct, target) {
        var items = ct.items.items;
        for (var i = 0, len = items.length; i < len; i++) {
            var c = items[0];
            if (c && (!c.rendered || !this.isValidParent(c, target))) {
                this.renderItem(c, i, target);
            }
        }
    },
    renderItem: function(c, position, target) {
        if (c && !c.rendered) {
            var td = this.getNextCell(c);
            var p = new Ext.Panel(Ext.apply(this.container.formConfig, {
                layout: 'form', items: c, renderTo: td
            }));
        }
    }
});

Ext.Container.LAYOUTS['tableform'] = Ext.ux.layout.TableFormLayout;
