
/// V1.0
/// Funciones de validacion de campos.
/// Especificas para formas de registro de ExpoMujer.com.mx

    String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }
    String.prototype.ltrim = function() { return this.replace(/^\s+/,""); }
    String.prototype.rtrim = function() { return this.replace(/\s+$/,""); }

    mensaje = "";

    function checkEmail(str){
      var filter=/^.+@.+\..{2,3}$/
      return (filter.test(str)); }

    function checkString(campo) { // verifica que el campo sea un string valido
        var valor = campo.value; valor = valor.trim(); campo.value = valor; return (valor.length != 0); }

    function checkNumber(campo) { // verifica que el campo sea un numero valido
        var entero = parseInt(campo.value); return (!(isNaN(entero) || entero < 0)); }

    function agregarMensaje(campo) { // agrega el campo a la lista de los mensajes
        mensaje += (mensaje.length != 0) ? ("\n" + campo) : campo; }

    function checkMessages() { // muestra la lista de los mensajes en el caso de que haya
        if(mensaje.length != 0) { alert("Los siguientes campos son requeridos : \n" + mensaje); } else { forma.submit(); } }

    function validaCampos() { // valida los campos de la forma de registro con trivia
        forma = document.forms.registro;
        mensaje = "";

        if(!checkString(forma.txt_nombre)) { agregarMensaje("- Nombre"); }
        if(!checkString(forma.txt_apellidop)) { agregarMensaje("- Apellido"); }
        if(checkString(forma.txt_correo)) { if(!checkEmail(forma.txt_correo.value)) { agregarMensaje("- Correo Electrónico"); } } else { agregarMensaje("- Correo Electrnico"); }
        if(!checkString(forma.txt_edad)) { agregarMensaje("- Edad"); } else { if(!checkNumber(forma.txt_edad)) { agregarMensaje("- Edad Incorrecta"); } }
        if(!checkString(forma.txt_trivia1)) { agregarMensaje("- Respuesta a la pregunta 1 de la Trivia"); }
        if(!checkString(forma.txt_trivia2)) { agregarMensaje("- Respuesta a la pregunta 2 de la Trivia"); }
        if(!checkString(forma.txt_trivia3)) { agregarMensaje("- Respuesta a la pregunta 3 de la Trivia"); }

        checkMessages();
    }

    function validaCorreos() { // valida los campos de la forma de registro de amigas para participar en el cambio de imagen
        forma = document.forms.registro;
        mensaje = "";
        if(checkString(forma.txt_email1)) { if(!checkEmail(forma.txt_email1.value)) { agregarMensaje("- Email de Amiga 1"); } } else { agregarMensaje("- Email de Amiga 1"); }
        if(checkString(forma.txt_email2)) { if(!checkEmail(forma.txt_email2.value)) { agregarMensaje("- Email de Amiga 2"); } } else { agregarMensaje("- Email de Amiga 2"); }
        if(checkString(forma.txt_email3)) { if(!checkEmail(forma.txt_email3.value)) { agregarMensaje("- Email de Amiga 3"); } } else { agregarMensaje("- Email de Amiga 3"); }

        correo1 = forma.txt_email1.value;
        correo2 = forma.txt_email2.value;
        correo3 = forma.txt_email3.value;

        if((correo1 == correo2) || (correo1 == correo3)) { agregarMensaje("- Email de Amiga 1 esta repetido"); }
        if(correo2 == correo3) { agregarMensaje("- Email de Amiga 2 esta repetido"); }

        checkMessages();
    }


    function validaFormaCupon() { // valida los campos de la forma de registro de amigas para obtener cupon de descuento
        forma = document.forms.registro;
        mensaje = "";
        
        if(!checkString(forma.txt_nombre)) { agregarMensaje("- Nombre"); }
        if(!checkString(forma.txt_apellidop)) { agregarMensaje("- Apellido Paterno"); }
        if(!checkString(forma.txt_apellidom)) { agregarMensaje("- Apellido Materno"); }
        if(checkString(forma.txt_correo)) { if(!checkEmail(forma.txt_correo.value)) { agregarMensaje("- Correo Electrónico"); } } else { agregarMensaje("- Correo Electrnico"); }
        if(!checkNumber(forma.txt_ciudad)) { agregarMensaje("- Ciudad"); }

        if(checkString(forma.txt_email1)) { if(!checkEmail(forma.txt_email1.value)) { agregarMensaje("- Email de Amiga 1"); } } else { agregarMensaje("- Email de Amiga 1"); }
        if(checkString(forma.txt_email2)) { if(!checkEmail(forma.txt_email2.value)) { agregarMensaje("- Email de Amiga 2"); } } else { agregarMensaje("- Email de Amiga 2"); }
        if(checkString(forma.txt_email3)) { if(!checkEmail(forma.txt_email3.value)) { agregarMensaje("- Email de Amiga 3"); } } else { agregarMensaje("- Email de Amiga 3"); }

        correo1 = forma.txt_email1.value;
        correo2 = forma.txt_email2.value;
        correo3 = forma.txt_email3.value;

        if((correo1 == correo2) || (correo1 == correo3)) { agregarMensaje("- Email de Amiga 1 esta repetido"); }
        if((correo2 == correo3)) { agregarMensaje("- Email de Amiga 2 esta repetido"); }

        checkMessages();
    }
