// JavaScript Document
function isValidCif(abc){	
	par = 0;	
	non = 0;	
	letras = "ABCDEFGHKLMNPQS";	
	let = abc.charAt(0); 	
	if (abc.length!=9) {		
		//alert('El Cif debe tener 9 dígitos');		
		return false;	
	} 	
	if (letras.indexOf(let.toUpperCase())==-1) {		
		//alert("El comienzo del Cif no es válido");		
		return false;	
	} 	
	for (zz=2;zz<8;zz+=2) {		
		par = par+parseInt(abc.charAt(zz));	
	} 	
	for (zz=1;zz<9;zz+=2) {		
		nn = 2*parseInt(abc.charAt(zz));		
		if (nn > 9) nn = 1+(nn-10);		
		non = non+nn;	
	} 	
	parcial = par + non;	
	control = (10 - ( parcial % 10));	
	if (control==10) control=0; 	
	if (control!=abc.charAt(8)) {		
		//alert("El Cif no es válido");		
		return false;
	}	
	//alert("El Cif es válido");	
	return true;
}

function isValidNif(abc){	
	dni=abc.substring(0,abc.length-1);	
	let=abc.charAt(abc.length-1);	
	if (!isNaN(let)) {		
		//alert('Falta la letra');		
		return false;	
	}else{		
		cadena = "TRWAGMYFPDXBNJZSQVHLCKET";		
		posicion = dni % 23;		
		letra = cadena.substring(posicion,posicion+1);		
		if (letra!=let.toUpperCase()){			
			//alert("Nif no válido");			
			return false;		
		}	
	}	
	//alert("Nif válido")	
	return true;
}

function isValidEmail(str)
{	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;	
	return (filter.test(str));
}

function validation(){
	var form = document.registro;
	with (form){
		error ="";
		if (nombre.value=="") { error+="- Nombre\n"; 	}
		if (apellido.value=="") { error+="- Apellido\n"; 	}
		/*
		if (NIF_CIF.value=="") { 
			error+="- NIF\n";	
		} else {
			x = isValidNif(NIF_CIF.value);
			y = isValidCif(NIF_CIF.value); 
			
			if (x==false && y==false) {
				error+="- El CIF/NIF no es correcto\n";
			} 
		}
		*/
		if (telefono.value=="") { 
			error+="- Teléfono\n"; 	
		} else {
			if(isNaN(telefono.value)){
				error+="- Télefono: solo puede introducir números\n";
			}
		}
		
		if (direccion.value=="") { error+="- Dirección\n"; 	}
		if (localidad.value=="") { error+="- Localidad\n"; 	}
		if (codigo_postal.value=="") { error+="- Código Postal\n"; 	}
			
		if (usuario.value=="") { 
			error+="- Email (Usuario)\n"; 	
		} else {
			x = isValidEmail(usuario.value);
			if(!x) {
				error+="- Email (Usuario) no válido\n";
			}
		}
		
		if (clave.value=="") { error+="- Clave\n"; 	}
		
	}
	if (error!=="") {
		alert("Complete los siguientes campos:\n" + error + "\n");
	} else {
		document.registro.submit();
	}
}

function validaSoporte(){
	var form = document.soporte;
	with (form){
		error ="";
		if (nombre.value=="") { error+="- Nombre\n"; 	}
		if (apellido.value=="") { error+="- Apellido\n"; 	}
		if (email.value=="") { error+="- Email\n"; 	}
		if (mensaje.value=="") { error+="- Duda o Consulta\n"; 	}
	}
	if (error!=="") {
		alert("Complete los siguientes campos:\n" + error + "\n");
	} else {
		document.soporte.submit();
	}
}

function validaContacto(){
	var form = document.contacto;
	with (form){
		error ="";
		if (nombre.value=="") { error+="- Nombre\n"; 	}
		if (apellido.value=="") { error+="- Apellido\n"; 	}
		if (email.value=="") { error+="- Email\n"; 	}
		if (mensaje.value=="") { error+="- Mensaje\n"; 	}
	}
	if (error!=="") {
		alert("Complete los siguientes campos:\n" + error + "\n");
	} else {
		document.contacto.submit();
	}
}
