/* author: Juan Pablo Gil R. - jpgil@conectu.com - Conectu.com LTDA */
/* Creation date: 10-10-2002 */
/*

    Valida un formulario sacando los campos requeridos de
    <input name="required" type="hidden" value="campo1, campo2, ..." />

*/

campos_requeridos = new Array();
if (typeof(txt_llenar_los_campos) == 'undefined') {
    var txt_llenar_los_campos = 'Por favor ingrese todos los datos.';
}

function validacion(obj) {
    
    input_campos = obj.required.value;
    txt = '';
    // Correlativo de campo requerido
    index = 0;
    for (i=0; i< input_campos.length; i++) {
        if ( input_campos.charAt(i)== "," ) {
            campos_requeridos[index] = txt;
            txt = "";
            index ++;
        }
        else if ( input_campos.charAt(i) != " ") {
            txt += input_campos.charAt(i);        
        }
    }
    // Ultimo
    if (txt) { campos_requeridos[index] = txt; }
    

    // Comprueba campo a campo
    completo = true;
    i=0;
    while (i<campos_requeridos.length && completo) {
        eval ("valor = obj." + campos_requeridos[i] + ".value;");
        if ( ! valor ) {
            alert(txt_llenar_los_campos);
            eval ("obj." + campos_requeridos[i] + ".focus();");
            completo = false;            
        }
        i++;
    }
    
    
    return completo;
}


// Acepta las condiciones de uso
function acepto_condiciones(obj) {
    if ( !obj.checked ) {
        alert("IMPORTANTE\n============\nEs necesario que usted lea y acepte las condiciones de uso para \nregistrarse como usuario. Si decide no aceptar las condiciones, \npor favor presione el botón 'Volver' en su navegador.");
        obj.focus();
        return false;
    }
    else {
        return true;
    }
}




/* Email Address Validation

   autor: Sandeep V. Tamhankar (stamhankar@hotmail.com)
   version: 1.12
   url: http://javascript.internet.com/forms/check-email.html
   modificada por Juan Pablo Gil R. - julio 2003
*/

function emailCheck(emailField) {

    emailStr = emailField.value;
    // fits the user@domain format. Separate the username from the domain.
    var emailPat=/^(.+)@(.+)$/;

    // Avoid  ( ) < > @ , ; : \ " . [ ]
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";

    // range of allowed characters 
    var validChars="\[^\\s" + specialChars + "\]";

    // "jiminy cricket"@disney.com is a legal e-mail address.
    var quotedUser="(\"[^\"]*\")";

    // joe@[123.124.233.4] is a legal e-mail address.
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
        alert("El email está incorrecto (revise @ y .)");
        emailField.focus();
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];

    if (user.match(userPat)==null) {
        alert("En el email, su nombre de usuario no es válido");
        emailField.focus();
        return false;
    }

    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                alert("En el email, el IP de destino es inválido!");
                emailField.focus();
                return false;
            }
        }
        return true;
    }
    
    var domainArray=domain.match(domainPat);
    if (domainArray==null) {
        alert("En el email, el dominio no es válido.");
        emailField.focus();
        return false;
    }
    
    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
    {
        alert("En el email, la dirección debe terminar con 2 ó 3 letras.");
        emailField.focus();
        return false;
    }
    
    if (len<2) {
        alert("En el email, necesita escribir el servidor");
        emailField.focus();
        return false;
    }
    
    // If we've gotten this far, everything's valid!
    return true;
}
