function reserva(i, num) {
	var show = document.getElementById("reserva" + i)
	show.style.display = "block"
	for (var j = 1; j <= num; j++) {
		if (j != i) {
			var nombre = document.getElementById("nombre" + j)
			var email = document.getElementById("email" + j)
			var movil = document.getElementById("movil" + j)
			var localidades = document.getElementById("localidades" + j)
			var aviso = document.getElementById("aviso" + j)
			var hide = document.getElementById("reserva" + j)
			nombre.value = ""
			email.value = ""
			movil.value = ""
			localidades.value = ""
			aviso.innerHTML = ""
			hide.style.display = "none"
		}
	}
}

function desmarcar(campo, i) {
	var aviso = document.getElementById("aviso" + i)
	if (aviso.innerHTML != "") {
		aviso.innerHTML = "&nbsp;"
	}	
	campo.style.color = "black"
}

function validarEmail(email) {
	var arroba = email.value.indexOf("@")
	var punto = email.value.lastIndexOf(".")
	var longitud = email.value.length
	if (arroba < 1 || punto == -1) {
		email.style.color = "#c00"
		return false
	} else if (punto > arroba && ((punto - arroba) < 2 || (longitud - punto) < 3 || (longitud - punto) > 5)) {
		email.style.color = "#c00"
		return false
	} else if (punto < arroba) {
		email.style.color = "#c00"
		return false
	} else {
		email.style.color = "black"
		return true
	}
}

function validarNumero(campo) {
	if (isNaN(campo.value)) {
		campo.style.color = "#c00"
		return false
	} else {
		campo.style.color = "black"
		return true
	}
}

function validarTelefono(telefono) {
	if (telefono.value.length < 9) {
		telefono.style.color = "#c00"
		return false
	} if (!isNaN(telefono)) {
		telefono.style.color = "#c00"
		return false
	} else {
		telefono.style.color = "black"
		return true
	}
}

function validarFormulario(i) {
	var nombre = document.getElementById("nombre" + i)
	var email = document.getElementById("email" + i)
	var movil = document.getElementById("movil" + i)
	var localidades = document.getElementById("localidades" + i)
	var max = document.getElementById("maxReservas" + i)
	var aviso = document.getElementById("aviso" + i)
	var reserva = document.getElementById("formulario" + i)
	if (nombre.value == "" || movil.value == "" || email.value == "" || localidades.value == "" || !validarNumero(movil) || !validarTelefono(movil) ||
		!validarEmail(email) || !validarNumero(localidades) || parseInt(localidades.value) > parseInt(max.innerHTML) || parseInt(localidades.value) == 0) {		
		var mensaje = ""
		if (localidades.value != "" && (!validarNumero(localidades) || parseInt(localidades.value) > parseInt(max.innerHTML) || parseInt(localidades.value) == 0)) {
			localidades.value = ""
			mensaje = "El número de localidades es incorrecto"
		}	
		if (movil.value != "" && (!validarNumero(movil) || !validarTelefono(movil))) {
			movil.value = ""
			mensaje = "El móvil es incorrecto"
		}
		if (email.value != "" && !validarEmail(email)) {
			email.value = ""
			mensaje = "El email es incorrecto"
		}		
		if (nombre.value == "") {
			nombre.focus()
		} else if (email.value == "") {
			email.focus()
		} else if (movil.value == "") {
			movil.focus()
		} else if (localidades.value == "") {
			localidades.focus()
		}
		aviso.innerHTML = mensaje
		return 0
	}
	reserva.submit()	
}