﻿/* CPF/CNPJ */

Ext.override(Ext.form.TextCPFCNPJ, {
    reset: function() {
        this.setValue('', true);
        this.clearInvalid();
        this.applyEmptyText();
    },
    setValue: function(v, b) {
        if (v == undefined || v == null) {
            this.originalValue = null; return;
        }

        if (v.length == 11 && this.vtype == 'cpf') {
            v = v.substring(0, 3) + '.' + v.substring(3, 6) + '.' + v.substring(6, 9) + '-' + v.substring(9, 11);
        }

        if (v.length == 14 && this.vtype == 'cnpj') {
            v = '0' + v;
        }

        if (v.length == 15 && this.vtype == 'cnpj') {
            v = v.substring(0, 3) + '.' + v.substring(3, 6) + '.' + v.substring(6, 9) + '/' + v.substring(9, 13) + '-' + v.substring(13, 15);
        }

        if (v == '') this.originalValue = '';

        if (b) {
            this.originalValue = v;
        }

        this.value = v;

        if (this.rendered) {
            this.el.dom.value = (v === null || v === undefined ? '' : v);
            this.validate();
        }
    }
});

/* TELEFONE */

Ext.override(Ext.form.TextTelefone, {
    setValue: function(v) {
        if (v == undefined || v == null) {
            return;
        }

        if (v.length == 10) {
            v = v.substring(0, 2) + '-' + v.substring(2, 6) + '-' + v.substring(6, 10);
        }

        this.value = v;

        if (this.rendered) {
            this.el.dom.value = (v === null || v === undefined ? '' : v);
            this.validate();
        }
    }
});

/* CEP */

Ext.override(Ext.form.TextCEP, {
    setValue: function(v) {
        if (v == undefined || v == null) {
            return;
        }

        if (v.length == 8) {
            v = v.substring(0, 5) + '-' + v.substring(5, 8);
        }

        this.value = v;

        if (this.rendered) {
            this.el.dom.value = (v === null || v === undefined ? '' : v);
            this.validate();
        }
    }
});
