// Funcion que muestra un contenido html en una zona
function mostrarContenido (zona, contenido) {

	document.getElementById(zona).innerHTML = contenido;

}

// Funcion que devuelve las opciones de busqueda filtradas
function devolverOpcionBusqueda (provincia) {

	return "<span class=\"filtro\" onclick=\"mostrarCaracteristicas ('"+provincia+"')\">"+
		"<img src=\"imagenes/buscar.png\" alt=\"+\"/>"+
		"<span class=\"titulo\">B&uacute;squeda por caracter&iacute;sticas</span>"+
	  "<span id=\"advice\"></span>";

}

// Funcion que reduce la info de opciones de busqueda
function reducirInfoRestaurante (referencia) {

	document.getElementById("ampliacion"+referencia).innerHTML = "<div class=\"botones\"><button type=\"button\" class=\"positive\" name=\"ampliarInfo\" onclick=\"ampliarInfoRestaurante('"+referencia+"')\" /><img src=\"imagenes/ampliar.png\" alt=\"+\" />Ampliar</div>";

}

// Deshabilida/Habilita los campos de telefono de reserva y email segun este activada la caracteristica
function cambiarEstadoDatosReserva (objeto) {
	
	if (objeto.checked == 1) {
		document.getElementById("rtelefono").disabled = false;
		document.getElementById("remail").disabled = false;
	}
	else {
		document.getElementById("rtelefono").disabled = "disabled";
		document.getElementById("remail").disabled = "disabled";
	}
}

// Redirige a la ficha
function verFicha (id) {
	document.location.href = "index.php?seccion=ficha&id=" + id;
}

function gestionarTarifas (id) {
	document.location.href = "index.php?seccion=gestionar&id=" + id;
}

// Redirige a la portada del area privada
function volverHome () {
	document.location.href = "index.php?seccion=intranet";
}

function volverAtras (url) {
	document.location.href = url;
}

// Limpia la caja de texto y cambia el color del texto a negro
function limpiarCampo (objeto) {
	objeto.value="";
	objeto.style.color = "black";
	objeto.onclick="";
}

function urlPagSeleccionada () {
	var pag = document.getElementById("pagina").value;
	var url = location.href;
	var pagAnt = url.match(/&pag=\d/);
	if (pagAnt == null) {
		url += "&pag="+pag;
	}
	else {
		url = url.replace(pagAnt, "&pag="+pag);
	}
	return url;
}

// Limita el tamaño de una caja de texto
function limit (limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum)
		limitField.value = limitField.value.substring(0, limitNum);
	else
		limitCount.value = limitNum - limitField.value.length; 
}

function establecer_fecha(dia, mes) {

	document.getElementsByName("dia")[0].value = dia;
	document.getElementsByName("mes")[0].value = mes;

}

function actualizarImporte (importe, objeto) {
  var total, num;
  if (objeto.checked) {
    document.getElementById("importegr").innerHTML = importe;
    total = document.getElementById("importetotal").innerHTML;
    if (total == null)
      total = 0;
    num = parseInt(total);
    num += importe;
    document.getElementById("importetotal").innerHTML = num;
  }
  else {
    document.getElementById("importegr").innerHTML = "";
    total = document.getElementById("importetotal").innerHTML;
    if (total == null)
      total = 0;
    num = parseInt(total);
    num -= importe;
    document.getElementById("importetotal").innerHTML = num;
  }

}

function redondear2dec (numero) {

	return Math.round (numero * Math.pow (10, 2)) / Math.pow (10, 2);

}

// Muestra el importe total al contratar o renovar el alta de restaurante
function mostrarImporteBase () {
	var iva = 16/100;
	var activado = document.getElementById("base").checked;
	var zonaImporte = document.getElementById("importeBase");
	if (activado) {
		var importe = 20 + (20 * iva);
		zonaImporte.innerHTML = "+ " + importe + "&euro;";
	}
	else
		zonaImporte.innerHTML = "";

}

// Muestra el importe total al renovar la condicion de estrella
function mostrarImporteRenovacionEstrella (estrella) {
	var iva = 16/100;
	var zonaImporte = document.getElementById("importeEstrella");
	var meses = document.getElementById("mesesEstrella").selectedIndex;
	var activado = document.getElementById("estrella").checked;
	// Descuento por contratar un año
	if (meses == 12)
		meses = 10;
	if (activado && meses != 0) {
		if (estrella == 1 || estrella == 2)
			var importe = 50 + (50 * iva);
		else
			var importe = 30 + (30 * iva);
		zonaImporte.innerHTML = "+ " + redondear2dec (importe * meses) + "&euro;";
	}
	else
		zonaImporte.innerHTML = "";
}

// Muestra el importe total al contratar la condicion de estrella
function mostrarImporteEstrella () {
	var iva = 16/100;
	var estrella = document.getElementById("estrella").selectedIndex;
	var zonaImporte = document.getElementById("importeEstrella");
	var meses = document.getElementById("mesesEstrella").selectedIndex;
	// Descuento por contratar un año
	if (meses == 12)
		meses = 10;
	if (estrella != 0 && meses != 0) {
		if (estrella == 1 || estrella == 2)
			var importe = 50 + (50 * iva);
		else
			var importe = 30 + (30 * iva);
		zonaImporte.innerHTML = "+ " + redondear2dec (importe * meses) + "&euro;";
	}
	else
		zonaImporte.innerHTML = "";
}

// Muestra el importe total al contratar o renovar la condicion de recomendado global
function mostrarImporteGrecomendado () {
	var iva = 16/100;
	var activado = document.getElementById("grecomendado").checked;
	var zonaImporte = document.getElementById("importeGrecomendado");
	var meses = document.getElementById("mesesGrecomendado").selectedIndex;
	// Descuento por contratar un año
	if (meses == 12)
		meses = 10;
	if (activado && meses != 0) {
		var importe = 20 + (20 * iva);
		zonaImporte.innerHTML = "+ " + redondear2dec (importe * meses) + "&euro;";
	}
	else
		zonaImporte.innerHTML = "";
}

// Muestra el importe total al contratar o renovar la condicion de recomendado prov
function mostrarImportePrecomendado () {
	var iva = 16/100;
	var activado = document.getElementById("precomendado").checked;
	var zonaImporte = document.getElementById("importePrecomendado");
	var meses = document.getElementById("mesesPrecomendado").selectedIndex;
	// Descuento por contratar un año
	if (meses == 12)
		meses = 10;
	if (activado && meses != 0) {
		var importe = 15 + (15 * iva);
		zonaImporte.innerHTML = "+ " + redondear2dec (importe * meses) + "&euro;";
	}
	else
		zonaImporte.innerHTML = "";
}

// Muestra el importe total al contratar o renovar cucharas
function mostrarImporteCucharas () {
	var iva = 16/100;
	var cucharas = document.getElementById("cucharas").selectedIndex;
	var zonaImporte = document.getElementById("importeCucharas");
	var meses = document.getElementById("mesesCucharas").selectedIndex;
	// Descuento por contratar un año
	if (meses == 12)
		meses = 10;
	if (cucharas != 0 && meses != 0) {
		var importe;
		if (cucharas == 1)
			importe = 10 + (10 * iva);
		if (cucharas == 2)
			importe = 15 + (15 * iva);
		if (cucharas == 3)
			importe = 20 + (20 * iva);
		zonaImporte.innerHTML = "+ " + redondear2dec (importe * meses) + "&euro;";
	}
	else
		zonaImporte.innerHTML = "";
}

/////////////////////////////////////////////////////////////////////////////////////////////
// fUNCIONES PARA VALIDAR CAMPOS ////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

function validar_texto (campo) {
	var camp = document.getElementsByName(campo)[0];

	if (camp != null)
		return (camp.value.length != 0)
	else return true;

}

function validar_password_online() {
	var password = document.getElementById("password").value;
	var password_repetido = document.getElementById("rep_password").value;
	var ok = document.createElement("img");
	ok.src = "./imagenes/icono_ok.png";
	var error = document.createElement("img");
	error.src = "./imagenes/icono_no_ok.png";
	var contenedor = document.getElementById("verif");
	contenedor.innerHTML = "";
	if (password.length >= 4 && password == password_repetido)
		contenedor.appendChild(ok);
	else
		if (password.length > 0)
			contenedor.appendChild(error);
}

function validar_password () {
	var password = document.getElementById("password");

	if (password != null)
		return (password.value.length >= 4)
	else return true;
}

function validar_password_repetido () {
	var password = document.getElementById("password");
	var password_repetido = document.getElementById("rep_password");

	if (password != null && password_repetido != null)
		return (password.value == password_repetido.value)
	else return true;
}

function validarCambioPassword () {

	var texto = "";
	var res = true;

	if (!validar_password ()) { 
		texto = "La contraseña debe tener al menos cuatro caracteres\n";
		res = false;
	}

	if (!validar_password_repetido ()) {
		texto += "Las contraseñas no coinciden\n";
		res = false;
	}
	
	if (res == false) {
		alert (texto);
		return false;
	}
	else {
		hashpActualizar();
	}

}

function validar_numero (campo) {
	var camp = document.getElementsByName(campo)[0];

	if (camp != null) {
		var checkOK = "0123456789";

		for (i = 0;  i < camp.value.length;  i++)
		{
			ch = camp.value.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
			if (j == checkOK.length)
			{
				return false;
			}
		}

		return true;
	}

	else return true;

}

function validar_telefono (campo) {
	var camp = document.getElementsByName(campo)[0];
	if (camp != null) {
		return (camp.value.length == 9 || camp.value.length == 0)
	}
	else return true;

}


function validarRegistro () {
	var res = true;
	var texto = "";
	
	if (!validar_texto ("Usuario")) {
		texto = "Nombre no puede estar en blanco\n";
		res = false;
	}
	
	if (!validar_texto ("Nombre")) {
		texto = "Nombre no puede estar en blanco\n";
		res = false;
	}

	if (!validar_texto ("Apellidos")) {
		texto += "Apellidos no puede estar en blanco\n";
		res = false;
	}

	if (!validar_numero ("Teléfono")) {
		texto += "El teléfono del cliente tiene caracteres no numéricos\n";
		res = false;
	}

	if (!validar_telefono ("Teléfono")) {
		texto += "El teléfono del cliente no tiene 9 números\n";
		res = false;
	}
	
	if (!validar_numero ("Teléfono2")) {
		texto += "El teléfono2 del cliente tiene caracteres no numéricos\n";
		res = false;
	}

	if (!validar_telefono ("Teléfono2")) {
		texto += "El teléfono2 del cliente no tiene 9 números\n";
		res = false;
	}

	if (!validar_texto ("Email")) {
		texto += "El email no puede estar en blanco\n";
		res = false;
	}
	
	if (!validar_password ()) { 
		texto += "La contraseña debe tener al menos cuatro caracteres\n";
		res = false;
	}

	if (!validar_password_repetido ()) {
		texto += "Las contraseñas no coinciden\n";
		res = false;
	}

	if (!validar_texto ("nombre local")) {
		texto += "El nombre del local no puede estar en blanco\n";
		res = false;
	}
//PENDING: Unificar campos iguales
	if (!validar_texto ("lnombre")) {
		texto += "El nombre del local no puede estar en blanco\n";
		res = false;
	}

	if (!validar_texto ("provincia")) {
		texto += "La provincia no puede estar en blanco\n";
		res = false;
	}
	
	if (!validar_texto ("localidad")) {
		texto += "La localidad no puede estar en blanco\n";
		res = false;
	}
	
	if (!validar_texto ("direccion")) {
		texto += "La dirección no puede estar en blanco\n";
		res = false;
	}

	/////////////////////////////Este/////
	if (!validar_numero ("utelefono")) {
		texto += "El teléfono del cliente tiene caracteres no numéricos\n";
		res = false;
	}

	if (!validar_telefono ("utelefono")) {
		texto += "El teléfono del cliente no tiene 9 números\n";
		res = false;
	}
	//////////////////////////////
	
	if (!validar_numero ("telefono")) {
		texto += "El teléfono del cliente tiene caracteres no numéricos\n";
		res = false;
	}

	if (!validar_telefono ("telefono")) {
		texto += "El teléfono del cliente no tiene 9 números\n";
		res = false;
	}
	
	if (!validar_numero ("telefono2")) {
		texto += "El teléfono2 del cliente tiene caracteres no numéricos\n";
		res = false;
	}

	if (!validar_telefono ("telefono2")) {
		texto += "El teléfono2 del cliente no tiene 9 números\n";
		res = false;
	}
		/////////////////////////////Este/////
	if (!validar_numero ("rtelefono")) {
		texto += "El teléfono de reservas tiene caracteres no numéricos\n";
		res = false;
	}

	if (!validar_telefono ("rtelefono")) {
		texto += "El teléfono de reservas no tiene 9 números\n";
		res = false;
	}
	//////////////////////////////
	
	if (!validar_numero ("telefono reservas")) {
		texto += "El teléfono de reservas tiene caracteres no numéricos\n";
		res = false;
	}

	if (!validar_telefono ("telefono reservas")) {
		texto += "El teléfono de reservas no tiene 9 números\n";
		res = false;
	}
	
	if (!validar_numero ("precio cubierto")) {
		texto += "El precio debe ser sólo números\n";
		res = false;
	}

		/////////////////////////////Este/////
	if (!validar_numero ("preciomedio")) {
		texto += "El precio debe ser sólo números\n";
		res = false;
	}
	//////////////////////////////
	
	if (res == false) {
		alert (texto);
		return false;
	}
	else {
		hashpRegistro();
	}
}


function validarReserva() {

	var res = true;
	var texto = "";

	if (!validar_texto ("nombre")) {
		texto += "El nombre no puede estar en blanco\n";
		res = false;
	}
	
	if (!validar_texto ("dia")) {
		texto += "El dia no puede estar en blanco\n";
		res = false;
	}
	
	if (!validar_texto ("mes")) {
		texto += "El mes no puede estar en blanco\n";
		res = false;
	}


	if (!validar_texto ("hora")) {
		texto += "La hora no puede estar en blanco\n";
		res = false;
	}
	
	if (!validar_texto ("minuto")) {
		texto += "Los minutos no pueden estar en blanco\n";
		res = false;
	}

	if (!validar_texto ("email")) {
		texto += "El email no puede estar en blanco\n";
		res = false;
	}
	
	if (res == false) {
		alert (texto);
		return false;
	}
}

