function cambiarColor(nombre,flag){
	
	if(flag)
		window.document.getElementById(nombre).style.background="#FFFFFF";
	else
	window.document.getElementById(nombre).style.background="#999999";
}
function cambiaBoton(nombre, imagen){
document.getElementById(nombre).src = imagen;
}
function mandaFormulario(){
	
	if(validarFormulario())
		window.document.getElementById("formulario").submit();
		
}
function validarFormulario(){
	var b=true;
	if(!checkDni())
		b=false;
	return b;
}
function checkDni(){
	var b   = true;
	var dni = window.document.getElementById("dni").value;	
	
	if(dni.length == 9){
		b = compruebaDni("" + dni);
	}
	else{
		b = false;
		window.alert("Introduzca correctamente el DNI");
		window.document.getElementById("dni").style.color="red";
	}
	
	return b;
}
function compruebaDni(dni){
	var dniOk     = true;
	var expresion = /[0-9]{8}[a-zA-Z]/; 
	var pos       = dni.search(expresion);
	
	if(pos == -1){
		dniOk = false;
		window.document.getElementById("dni").style.color="red";
		window.alert("El DNI introducido no es válido");
	}
	else{
		dniOk = compruebaLetra(dni);
	}
	
	return dniOk;
	

}
function compruebaLetra(dni){

	dni = dni.toUpperCase();
	var numero = dni.substring(0,dni.length-1);// esto nos quedamos con el número
	var letra1  = dni.charAt(dni.length-1);//esto con la letra
	var res    = true;
	var letra2 = ObtenerLetra(numero);
	
	if(letra2 != letra1){
		window.alert("La letra del DNI no se corresponde con el número");
		res = false;
	}

	return res;	
}
function ObtenerLetra(cDNI){
	var cadena = 'TRWAGMYFPDXBNJZSQVHLCKET';
	var posicion = cDNI % 23;
	var letra = cadena.charAt(posicion);
	
	return letra;
}
function mandaFormulario(){
document.getElementById("formulario").submit();
}
function enviaRespuesta(nombre){
	window.document.getElementById(nombre).submit();
	
}
function manda(){
	
	ponerEnRojo();
	
	if(validarFormularioHabilitado())
			window.document.getElementById("formulario").submit();
	
			
}
function ponerEnRojo(){
	window.document.getElementById("nombre").style.color="red";
	window.document.getElementById("apellido").style.color="red";
	window.document.getElementById("dni").style.color="red";
	window.document.getElementById("domicilio").style.color="red";
	window.document.getElementById("ciudad").style.color="red";
	window.document.getElementById("provincia").style.color="red";
	window.document.getElementById("pais").style.color="red";
	window.document.getElementById("telefono").style.color="red";
	window.document.getElementById("mail").style.color="red";
}
function validarFormularioHabilitado(){
	var b=true;
	if(!checkNombre() || !checkApellidos()  || !checkDni() || !checkDomicilio() || !checkCiudad() || !checkProvincia() || !checkPais() || !checkTelefono() || !checkEmail2())
		b=false;
	return b;
}
function checkNombre(){
	var nombre="nombre";
	return noNull(nombre) && esTxt(window.document.getElementById(nombre).value);
}
//=================================================================================================
function checkTelefono(){
	var nombre   = "telefono";
	var telefono = window.document.getElementById(nombre).value;
	var valido   = noNull(nombre);
	
	if(valido){
		
		valido = (telefono.length == 9);

		if(valido){
			valido = ((telefono.charAt(0) == "6") || (telefono.charAt(0) == "9") || (telefono.charAt(0) == "8"));
		}
	}
	if(!valido){
			document.getElementById("telefono").style.color="red";
			window.alert("Introduzca correctamente el teléfono");
	}
	
	return valido;
}
//=================================================================================================
function checkApellidos(){
	var apellido="apellido";
	return noNull(apellido) && esTxt(window.document.getElementById(apellido).value);
}
//=================================================================================================
function checkPais(){
	var pais="pais";
	return noNull(pais) && esTxt(window.document.getElementById(pais).value);
}
//=================================================================================================
function checkDomicilio(){
	var domicilio="domicilio";
	return noNull(domicilio) ;//&& esTxt(window.document.getElementById(domicilio).value);
}
//=================================================================================================
function checkCiudad(){
	var ciudad="ciudad";
	return noNull(ciudad) && esTxt(window.document.getElementById(ciudad).value);
}
//=================================================================================================
function checkProvincia(){
	var provincia="provincia";
	return noNull(provincia) && esTxt(window.document.getElementById(provincia).value);
}
//=================================================================================================
function checkEmail2() {
	var noError = false;
	var plant   = /[^\w^@^\.^-]+/gi;
	var cadena  = window.document.getElementById("mail").value;
	
	if (plant.test(cadena)){
		noError = false;
		document.getElementById("mail").style.color="red";
		window.alert("El e-mail " + cadena + " contiene caracteres extraños.");
	}
	else{
		// plant =/(^\w+)(@{1})([\w\.-]+$)/i;
		plant = /([\w\_\-]+)(@{1})(\w+)(\.{1})(\w+)/i;
		if (plant.test(cadena)){
			noError = true;
		}
		else{
			noError = false;
			document.getElementById("mail").style.color="red";
			window.alert("El e-mail " + cadena + " no es válido.");
		}
	}
	
	return noError;
}
//=================================================================================================
function checkDni(){
	var b   = true;
	var dni = window.document.getElementById("dni").value;	
	
	if(dni.length == 9){
		b = compruebaDni("" + dni);
	}
	else{
		b = false;
		window.alert("Introduzca correctamente el DNI");
		window.document.getElementById("dni").style.color="red";
	}
	
	return b;
}
//=================================================================================================
function compruebaDni(dni){
	var dniOk     = true;
	var expresion = /[0-9]{8}[a-zA-Z]/; 
	var pos       = dni.search(expresion);
	
	if(pos == -1){
		dniOk = false;
		window.document.getElementById("dni").style.color="red";
		window.alert("El DNI introducido no es válido");
	}
	else{
		dniOk = compruebaLetra(dni);
	}
	
	return dniOk;
	

}
//=================================================================================================	
function compruebaLetra(dni){

	dni = dni.toUpperCase();
	var numero = dni.substring(0,dni.length-1);// esto nos quedamos con el número
	var letra1  = dni.charAt(dni.length-1);//esto con la letra
	var res    = true;
	var letra2 = ObtenerLetra(numero);
	
	if(letra2 != letra1){
		window.alert("La letra del DNI no se corresponde con el número");
		res = false;
	}

	return res;	
}
//=================================================================================================	
function ObtenerLetra(cDNI){
	var cadena = 'TRWAGMYFPDXBNJZSQVHLCKET';
	var posicion = cDNI % 23;
	var letra = cadena.charAt(posicion);
	
	return letra;
}
//=================================================================================================	
function esTxt(cad){
	var esTexto = true;
	var expr1 	= /[a-zA-Z]+/;
	var expr2 	= /[0-9]+/;
	if(cad.search(expr1) == -1 || cad.search(expr2) != -1)          
	{
		esTexto = false;
		window.alert("Debes introducir caracteres alfabéticos");
	}

	return esTexto;	
}
function esBlanco(campo){
	var hayBlanco = true;
	var expr = /^\s/;
	if(campo.search(expr) == -1)
	{
		hayBlanco = false;
	}
	return hayBlanco;
}
function noNull(campo){
	var nombre=window.document.getElementById(campo);
	var b=true;
	
	if(nombre.value.length == 0 || esBlanco(nombre.value)){
		b=false;
		window.alert("Debes introducir al menos un " + campo);
		document.getElementById(campo).style.color="red";
	}
	return b;
}
function borrarFormularioHabilita()
{
document.getElementById("formulario").reset();
habilita();
}
function habilita()
{
document.getElementById("nombre").disabled=false;
document.getElementById("nombre").style.background="#FFFFFF";
document.getElementById("apellido").disabled=false;
document.getElementById("apellido").style.background="#FFFFFF";
document.getElementById("domicilio").disabled=false;
document.getElementById("domicilio").style.background="#FFFFFF";
document.getElementById("ciudad").disabled=false;
document.getElementById("ciudad").style.background="#FFFFFF";
document.getElementById("provincia").disabled=false;
document.getElementById("provincia").style.background="#FFFFFF";
document.getElementById("pais").disabled=false;
document.getElementById("pais").style.background="#FFFFFF";
document.getElementById("telefono").disabled=false;
document.getElementById("telefono").style.background="#FFFFFF";
document.getElementById("mail").disabled=false;
document.getElementById("mail").style.background="#FFFFFF";
}




