function validar() 
   {
 //Comprobación valores del formulario
    nombre = document.formulario.nombre.value;
    apellidos= document.formulario.apellido1.value;
    apellidos2= document.formulario.apellido2.value;
        correoe = document.formulario.email.value;
    calle= document.formulario.calle.value;
    poblacio = document.formulario.poblacion.value;
    codigo= document.formulario.cpostal.value;
    telefono= document.formulario.telefono.value;
    edad = document.formulario.fechanaci .value;

    dni = document.formulario.dni.value;
    sexo =document.formulario.sexoList.value;
    usuario = document.formulario.usuario.value;
    clave = document.formulario.clave.value;
    clave2 = document.formulario.clave2.value;


    valor = 0;
    txt = "Datos invalidos\n\nIncluye o revisa la información siguiente:\n\n"


   if (promojs){
      entidad = document.formulario.entidad.value
      enticif = document.formulario.enticif.value
      entitele = document.formulario.entitele.value
      entiweb = document.formulario.entiweb.value

      if (entiweb!=""){
         http = 'http://'
         x = http.length
         charac = entiweb.substring(0,x);
         charac = charac.toLowerCase()
         if(charac==http){
           valor = valor + 0;
         } else{
            document.formulario.entiweb.value = http + document.formulario.entiweb.value;
            }
         }
    
    if (entidad){
    }else{
        txt = txt+'   Empresa Promotora \n';
            valor = valor + 1;   
    }
    if (entitele){
    }else{
        txt = txt+'   Teléfono Promotor \n';
            valor = valor + 1;   
    }
    if (enticif){
        charac = ""
         re = /\//g;
            newDNI=enticif.replace(re, "");
            enticif=newDNI.replace(/ /g, "");
           enticif=newDNI.replace(/\./g, "");
            enticif=newDNI.replace(/-/g, "");
             cifLong = enticif.length
            letraPos = 0
           charac = enticif.substring(letraPos,letraPos+1);
   
            if((charac * 1 != charac) && (cifLong==9)){
              document.formulario.enticif.value = enticif

        }else{
            txt = txt+'   CIF Empresa incorrecto\n';
                valor = valor + 1;   

        }

        
    }else{
        txt = txt+'   CIF Empresa\n';
            valor = valor + 1;   
    }

    txt = txt+'\n';

    
   }

    if (nombre && apellidos){

           valor = valor + 0;
        }
    else {         
      txt = txt+'   Nombre \n';
        valor = valor + 1;   
       }
    if (apellidos){
          valor = valor + 0;
    }
    else {
         txt = txt+'   1er apellido \n';
          valor = valor + 1;       
           }
    
    

    var at, dot, charac;
    at = false;
    if(correoe!="")
    {
        for(i=1;i<(correoe.length-1);i++)
        {
            charac = correoe.substring(i,i+1);
            if(charac=="@"){at = true;}
        }
        if(at)
        {
            for(i=3;i<(correoe.length-1);i++)
            {
                charac=correoe.substring(i,i+1)
                if(charac=="."){dot = true;}
            }
            if(dot)
              valor = valor + 0;
            else
            {
                txt = txt+'   email incorrecto\n';
                 valor = valor + 1;
            }    
        }
        else
        {
                txt = txt+'   email incorrecto\n';
                 valor = valor + 1;
        }
    }
    else
    {
            txt = txt+'   email \n';
             valor = valor + 1;
    }


    at = false;
    charac = ""
    if (dni){
        re = /\//g;
        newDNI=dni.replace(re, "");
        dni=newDNI.replace(/ /g, "");
        dni=newDNI.replace(/\./g, "");
        dni=newDNI.replace(/-/g, "");
           dniLong = dni.length
        letraPos = dniLong -1
        charac = dni.substring(letraPos,letraPos+1);
        if(charac * 1 != charac){at = true;}

        if (at && (dniLong==9)){
            charac = caps(charac)
            dniCheck = dni.substring(0,letraPos);
            checkChar = LetraNIF (dniCheck)
            if(checkChar == charac){
                // ok!
                valor = valor + 0; 
            }else{
                txt = txt+'   DNI Invalido '+dni+'\n';
                     valor = valor + 1; }     
        }else{
            
            if (dniLong<8 || dniLong>8){
                txt = txt+'   DNI Invalido '+dni+'\n';
                     valor = valor + 1; }     
            else{
                /*OK de momento nos lo creemos
                txt = txt+'   DNI Invalido '+dni+'\n';*/
                checkChar = LetraNIF (dni)
                dni = dni+checkChar
                document.formulario.dni.value = dni
                     valor = valor + 0;      
            }

        }
        
     }else {
           txt = txt+'   DNI\n';
           valor = valor + 1;       
       }




    if (edad){
        //var fecha = new Date();
        //fecha.setDate(fecha.getDate() - (18* 365));
        //anyolimite = fecha.getYear();
	  //anyolimite calculado en formusuario

	 // barra
        re = /\//g;
        newEdad=edad.replace(re, "-");
	  // no numerico
	  re2 = /[^0-9]/g;
        newEdad2=newEdad.replace(re2, "-");
        fechaEdad=newEdad2.replace(/\./g, "-");

        puntero = fechaEdad.indexOf("-");
        puntero2 = fechaEdad.indexOf("-", puntero+1);

        dia = fechaEdad.substring(0, puntero);
        mes = fechaEdad.substring(puntero+1, puntero2);
        anyo = fechaEdad.substring(puntero2+1, fechaEdad.length);
        
        //tempEdad = mes + "-" + dia + "-" + anyo
        //var fechaEdad = new Date(tempEdad);
        if (anyo.length > 2){anyo = anyo - 1900}
        if (anyo < 0){
             txt = txt+'   Año o Fecha de nacimiento inválido\n';
             valor = valor + 1;       
        }
        if (anyo <= anyolimite){
              //alert(anyolimite + 'OK solicitar el carnet' + anyo);
                valor = valor + 0;
             }
        else{
            txt = txt +'   Debes ser mayor de edad para convertirte en Usuario\n';
            txt = txt +'   Comprueba que has entrado una fecha valida: dd-mm-aa\n';
            txt = txt +'   Ejemplo: 12-1-65\n';
            valor = valor + 1;       
            }        
     }else {
           txt = txt+'   Fecha de nacimiento\n';
           valor = valor + 1;       
       }

    if (sexo == 0){
         txt = txt+'   Sexo\n';
          valor = valor + 1;       
    }

    txt = txt+'\n';
    if (usuario){
        if (usuario.length < 6){
                txt = txt+'   Usuario debe ser mayor de 5 carácteres\n';
                  valor = valor + 1;
            }
        
    }else{
         txt = txt+'   Usuario\n';
          valor = valor + 1;       
    }

    if (clave && clave2){

        if (clave != clave2){
             txt = txt+'   Claves no son iguales\n';
              valor = valor + 1; }  
        else{
            if (clave.length < 4){
                txt = txt+'   Clave debe ser mayor de 3 carácteres\n';
                  valor = valor + 1;
            }else{
            // Ok pasa a minuscula
            clave = lower(clave)
            document.formulario.clave.value = clave
            document.formulario.clave2.value = clave
            }
        }    
    }else{
     txt = txt+'   Clave ó Repetir Clave\n';
          valor = valor + 1;       

    }
    

if (valor == 0){
    return true;}
else{
    txt = txt + '\nGracias'
    alert(txt)
     return false;
    }   

}

function lower(x) { 
x = x.toLowerCase() 
return x
} 
function caps(x) { 
x = x.toUpperCase() 
return x
} 


function LetraNIF (dni) {
/* Obtiene letra del NIF a partir del DNI */

 cadena="TRWAGMYFPDXBNJZSQVHLCKET"
   posicion = dni % 23
   letra = cadena.substring(posicion,posicion+1)
 
//alert(posicion+" " +letra + " " + dni)
return letra;
}

function testFecha(edad){

}


var compa = new Array();
compa[1] = "H"
compa[2] = "M"
var compades = new Array();
compades[1] = "Hombre"
compades[2] = "Mujer"


function definecompael()
{

for(i=0;i<document.formulario.sexoList.length;i++)
    {
        if(document.formulario.sexoList[i].selected)
        {
            document.formulario.sexo.value = compa[i]
            document.formulario.sexoShow.value = compades[i]
        }
    }


}

