function validarNumeros(event){
	var key=window.event.keyCode;//codigo de tecla. 
		if ((key < 48 || key > 57) && key!=45){//si no es numero  o guión
			window.event.keyCode=0;//anula la entrada de texto. 
	}
} 

function validarFecha (oTxt){
	var bOk = true; 
	if (oTxt.value != ""){ 
		bOk = bOk && (valAno(oTxt)); 
		bOk = bOk && (valMes(oTxt)); 
		bOk = bOk && (valDia(oTxt)); 
		bOk = bOk && (valSep(oTxt)); 
	}
	return bOk;
}

function validarEmail(sDir){
	
	var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (sDir.length == 0 ) return true;
	if (filter.test(sDir))
		return true;
	else
		return false;

}

function ValidarUsu(form,mod){ 

	var pos1, pos2, bOk = true; 
	/*------------- Correo ------------------*/
    var sDir = document.f.email.value; 
    msgErr='';
		
	if (mod==""){	
		//if (document.f.login.value=='' || document.f.password.value=='' || document.f.CIF.value=='' || document.f.razon_social.value=='' || document.f.direccion_fabrica.value=='' || document.f.cp_fabrica.value=='' || document.f.municipio_fabrica.value=='' || document.f.provincia_fabrica.value=='' || document.f.direccion_comercial.value=='' || document.f.cp_comercial.value=='' || document.f.municipio_comercial.value=='' || document.f.provincia_comercial.value=='' || document.f.nombre.value=='' || document.f.marcas.value=='' || document.f.telefono.value=='' || document.f.fax.value=='' || document.f.email.value=='' || document.f.imagen1.value=='' || document.f.imagen2.value=='' || document.f.imagen3.value=='' || document.f.imagen4.value=='' || document.f.imagen5.value=='')
		if (document.f.login.value=='' || document.f.password.value=='' || document.f.conf_password.value=='' || document.f.CIF.value=='' || document.f.razon_social.value=='' || document.f.direccion_fabrica.value=='' || document.f.cp_fabrica.value=='' || document.f.municipio_fabrica.value=='' || document.f.provincia_fabrica.value=='' || document.f.direccion_comercial.value=='' || document.f.cp_comercial.value=='' || document.f.municipio_comercial.value=='' || document.f.provincia_comercial.value=='' || document.f.nombre.value=='' || document.f.valor_marcas.value=='' || document.f.telefono.value=='' || document.f.fax.value=='' || document.f.email.value==''  || form.titular.value=='' || form.titular_banco.value=='' || form.titular_domicilio.value=='' || form.titular_poblacion.value=='' || form.titular_cp.value=='' || form.titular_entidad.value=='' || form.titular_oficina.value=='' || form.titular_dc.value=='' || form.titular_cuenta.value=='')
			msgErr+="Todos los campos son obligatorios\n"; 
	}else{
		if (document.f.login.value=='' || document.f.CIF.value=='' || document.f.razon_social.value=='' || document.f.direccion_fabrica.value=='' || document.f.cp_fabrica.value=='' || document.f.municipio_fabrica.value=='' || document.f.provincia_fabrica.value=='' || document.f.direccion_comercial.value=='' || document.f.cp_comercial.value=='' || document.f.municipio_comercial.value=='' || document.f.provincia_comercial.value=='' || document.f.nombre.value=='' || document.f.valor_marcas.value=='' || document.f.telefono.value=='' || document.f.fax.value=='' || document.f.email.value=='')
			msgErr+="Todos los campos son obligatorios, excepto password e imágenes\n"; 
	}
	
	if (sDir!='' && !validarEmail (sDir)){ 
	    msgErr+="Dirección de correo inválida\n";      
	} 
	
	if ((document.f.password.value) != (document.f.conf_password.value)){
	    msgErr+="Las contraseñas deben ser iguales\n";  
	}
	
	if (msgErr==''){		
		cont=0;
		for (i=0;i<document.f.elements.length;++i)
		{						
			if (document.f.elements[i].getAttribute('type')=='checkbox')
				if (document.f.elements[i].checked)
					cont++;
		}
		if (cont==0){
			if (mod==""){	
				msgErr+="Todos los campos son obligatorios\n"; 
			}else{
				msgErr+="Todos los campos son obligatorios, excepto password e imágenes\n"; 
			}
		}
	}
	
	if (msgErr!=''){
		alert (msgErr);
		return false;
	}
    
} 

function esDigito(sChr){ 
    var sCod = sChr.charCodeAt(0); 
    return ((sCod > 47) && (sCod < 58)); 
} 

   function valSep(oTxt){ 
    var bOk = false; 
    var sep1 = oTxt.value.charAt(2); 
    var sep2 = oTxt.value.charAt(5); 
    bOk = bOk || ((sep1 == "-") && (sep2 == "-")); 
    bOk = bOk || ((sep1 == "/") && (sep2 == "/")); 
    return bOk; 
   } 

   function finMes(oTxt){ 
    var nMes = parseInt(oTxt.value.substr(3, 2), 10); 
    var nAno = parseInt(oTxt.value.substr(6), 10); 
    var nRes = 0; 
    switch (nMes){ 
     case 1: nRes = 31; break; 
     case 2: nRes = 28; break; 
     case 3: nRes = 31; break; 
     case 4: nRes = 30; break; 
     case 5: nRes = 31; break; 
     case 6: nRes = 30; break; 
     case 7: nRes = 31; break; 
     case 8: nRes = 31; break; 
     case 9: nRes = 30; break; 
     case 10: nRes = 31; break; 
     case 11: nRes = 30; break; 
     case 12: nRes = 31; break; 
    } 
    return nRes + (((nMes == 2) && (nAno % 4) == 0)? 1: 0); 
   } 

   function valDia(oTxt){ 
    var bOk = false; 
    var nDia = parseInt(oTxt.value.substr(0, 2), 10); 
    bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt))); 
    return bOk; 
   } 

   function valMes(oTxt){ 
    var bOk = false; 
    var nMes = parseInt(oTxt.value.substr(3, 2), 10); 
    bOk = bOk || ((nMes >= 1) && (nMes <= 12)); 
    return bOk; 
   } 

   function valAno(oTxt){ 
    var bOk = true; 
    var nAno = oTxt.value.substr(6); 
    bOk = bOk && ((nAno.length == 2) || (nAno.length == 4)); 
    if (bOk){ 
     for (var i = 0; i < nAno.length; i++){ 
      bOk = bOk && esDigito(nAno.charAt(i)); 
     } 
    } 
    return bOk; 
   } 

