<!--
/*

    Derechos Reservados © 2007 Ernesto Spiro Peimbert Andreakis

    @archivo  cFormatoCampos.js

    @autor    Ernesto Spiro Peimbert Andreakis

    @fecha    abril 23 2007

    @correo   spiro79@gmail.com

    @comentarios
        Clase Javascript para facilitar la implementación de formato a campos de formularios.

    INFORMACION GENERAL:

        Alonso de la Veracruz 25
        Cd. Satélite, México
        Mexico, C.P. 53100
        Cel: 0445531935844

    LICENCIA:
        Copyright ©
        This program is free software; you can redistribute it and/or
        modify it under the terms of the GNU General Public License
        as published by the Free Software Foundation; either version 2
        of the License, or (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
        
*/

/*
clase cFormatoCampos
@prop        letters caracteres alfabeticos validos
@prop        numbers caracteres numericos validos
@prop        signs caracteres de signo validos
@prop        mathsigns caracteres de signos matematicos validos
@prop        custom caracteres customizados validos

@method      tipoCampoValido
@method      existe
@method      registra
@method      soloAcepta
@method      mayusculas
@method      minusculas
@method      tamanoMaximo
*/
function cFormatoCampos () {
    this.letters = ' ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ';
	this.letters2 = ' ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyzáÁéÉíÍïÏóÓúÚüÜ';
	this.url = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz//:.-_~?&;1234567890';
    this.numbers = '1234567890';
    this.signs = ',.:;@-\'';
    this.mathsigns = '+-=()*/';
    this.custom = '<>#$%&?¿';

    /*
        @fn       cFormulario.tipoCampoValido = function (obj)
        @param    obj objeto
        @return   boolean
        @brief    determina si el objeto es valido para utilizarse con la clase
        @author   Ernesto Spiro Peimbert Andreakis
        @date     22 abril 2007
    */
    this.tipoCampoValido = function (obj) {
        var regresa = true;
        var tipo = obj.type;
        if ( tipo != 'text' && tipo != 'textarea' ) {
            alert('El campo con id = ' + obj.id + ' NO es valido.\nSe esperaba text o textarea.');
            regresa = false;
        }
        return regresa;
    }

    /*
        @fn       cFormulario.existe = function (obj,tipo,especial)
        @param    obj nombre u objeto que se quiere validar
        @param    tipo tipo de parametro que se esta proporcionando: 'string' u 'object' son los unicos aceptables
        @param    especial unicamente contiene el id del objeto, para los alerts.
        @return   boolean
        @brief    valida si el objeto existe
        @author   Ernesto Spiro Peimbert Andreakis
        @date     22 abril 2007
    */
    this.existe = function (obj,tipo,especial) {
        var regresa = true;
        if ( obj == null ) {
            switch(tipo) {
                case 'string':
                    alert('El objeto con id = ' + especial + ' no existe.\nVerificar que exista en la pagina.');
                    break;
                case 'object':
                    alert('El objeto proporcionado como parametro no existe.\nVerificar que exista en la pagina.');
                    break;
            }
            regresa = false;
        }
        return regresa;
    }

    /*
        @fn       cFormulario.registra = function (obj)
        @param    obj objeto
        @return   boolean
        @brief    metodo inicial - valida y prepara el objeto
        @author   Ernesto Spiro Peimbert Andreakis
        @date     22 abril 2007
    */
    this.registra = function(obj) {
        var tipo = typeof(obj);
        var el_objeto;
        var regresa = true;
        switch ( tipo ) {
            case 'string':
                el_objeto = document.getElementById(obj);
                regresa = el_objeto;
                break;
            case 'object':
                el_objeto = obj;
                regresa = el_objeto;
                break;
            default:
                alert('Parametro NO valido.\nSe esperaba string u object.');
                regresa = false;
                break;
        }
        if ( regresa ) {
            if ( !this.existe(el_objeto,tipo,obj) ) {
                regresa = false;
            }
            else if ( !this.tipoCampoValido(el_objeto) ) {
                regresa = false;
            }
        }
        return regresa;
    }

    /*
        @fn       cFormulario.soloAcepta = function (obj,validos)
        @param    obj objeto
        @param    validos string con los caracteres validos
        @return   void
        @brief    limita los caracteres que se pueden introducir en un campo
        @author   Ernesto Spiro Peimbert Andreakis
        @date     22 abril 2007
    */
    this.soloAcepta = function (obj,validos) {
        var el_objeto = this.registra(obj);
        if ( el_objeto != false ) {
            var primera_parte = "";
            var ultima_parte = "";
            if ( el_objeto.attachEvent ) {
                primera_parte = "el_objeto.attachEvent( 'onkeypress',";
                ultima_parte = ");";
            }
            else {
                primera_parte = "el_objeto.addEventListener( 'keypress',";
                ultima_parte = ",false );";
            }
            var la_funcion = "";
            la_funcion += primera_parte;
            la_funcion += "function (e) {";
            la_funcion += "    var validos = '" + validos + "';";
            la_funcion += "    var tecla;";
            la_funcion += "    var resultado;";
            la_funcion += "    if(!e) var e = window.event;";
            la_funcion += "    tecla = window.event?parseInt(e.keyCode): parseInt(e.which);";
            la_funcion += "    resultado = validos.indexOf(String.fromCharCode(tecla));";
            la_funcion += "    if ( resultado == -1 && tecla != 0 && tecla != 8 && tecla != 13 ) {";
            la_funcion += "        if (e.stopPropagation)";
            la_funcion += "            e.stopPropagation();";
            la_funcion += "        else";
            la_funcion += "            e.cancelBubble = true;";
            la_funcion += "        if (e.preventDefault)";
            la_funcion += "            e.preventDefault();";
            la_funcion += "        else";
            la_funcion += "            e.returnValue = false;";
            la_funcion += "        alert('El campo no acepta este valor.\\nLos valores permitidos se listan a continuacion:\\n '+ validos);";
            la_funcion += "    }";
            la_funcion += "}";
            la_funcion += ultima_parte;
            eval(la_funcion);
        }
    }

    /*
        @fn       cFormulario.mayusculas = function (obj)
        @param    obj objeto
        @return   void
        @brief    forza a que un campos solo contenga mayusculas
        @author   Ernesto Spiro Peimbert Andreakis
        @date     22 abril 2007
    */
    this.mayusculas = function (obj) {
        var el_objeto = this.registra(obj);
        if ( el_objeto != false ) {
            var primera_parte = "";
            var ultima_parte = "";
            if ( el_objeto.attachEvent ) {
                primera_parte = "el_objeto.attachEvent( 'onkeyup',";
                ultima_parte = ");";
            }
            else {
                primera_parte = "el_objeto.addEventListener( 'keyup',";
                ultima_parte = ",false );";
            }
            var la_funcion = "";
            la_funcion += primera_parte;
            la_funcion += "function () {";
            la_funcion += "    var valor = document.getElementById('" + el_objeto.id + "').value;";
            la_funcion += "    document.getElementById('" + el_objeto.id + "').value = valor.toUpperCase();";
            la_funcion += "}";
            la_funcion += ultima_parte;
            eval(la_funcion);
        }
    }

    /*
        @fn       cFormulario.minusculas = function (obj)
        @param    obj objeto
        @return   void
        @brief    forza a que un campos solo contenga minusculas
        @author   Ernesto Spiro Peimbert Andreakis
        @date     22 abril 2007
    */
    this.minusculas = function (obj) {
        var el_objeto = this.registra(obj);
        if ( el_objeto != false ) {
            var primera_parte = "";
            var ultima_parte = "";
            if ( el_objeto.attachEvent ) {
                primera_parte = "el_objeto.attachEvent( 'onkeyup',";
                ultima_parte = ");";
            }
            else {
                primera_parte = "el_objeto.addEventListener( 'keyup',";
                ultima_parte = ",false );";
            }
            var la_funcion = "";
            la_funcion += primera_parte;
            la_funcion += "function () {";
            la_funcion += "    var valor = document.getElementById('" + el_objeto.id + "').value;";
            la_funcion += "    document.getElementById('" + el_objeto.id + "').value = valor.toLowerCase();";
            la_funcion += "}";
            la_funcion += ultima_parte;
            eval(la_funcion);
        }
    }

    /*
        @fn       cFormulario.tamanoMaximo = function (obj,tam)
        @param    obj objeto
        @param    tam longitud a la que se limitara el contenido del objeto
        @return   void
        @brief    forza a que un campo sea de una longitud dada
        @author   Ernesto Spiro Peimbert Andreakis
        @date     22 abril 2007
    */
    this.tamanoMaximo = function (obj,tam) {
        var el_objeto = this.registra(obj);
        if ( tam != "" ) {
            el_objeto.maxLength = tam;
        }
    }
}
-->
