//carga un archivo php en una etiqueta html
function cargaArchivo(id, archivo, parametros)  
{  	
	//document.getElementById(id).innerHTML = 'Cargando..';

	var url = 'http://'+location.host+"/eng/"+archivo;  
	
	var pars = 'parametros='+parametros;  

	var myAjax = new Ajax.Updater(id, url, { method: 'get', parameters: pars});  
} 

//muestra el select con el pais de devolución que siempre será igual al del pais de recogida
function muestraPaisDevolucion()
{
	var selectDevolucion = document.getElementById('selectPaisDevolucion');
	var contenidoSelectDevolucion;
	
	muestraCamposDesactivados('pais_devolucion');
		
}

//muestra el select de la ciudad de recogida
function muestraCiudadRecogida()
{
	var paisRecogida = document.getElementById('pais_recogida').value;
	var parametros = paisRecogida + ',' + 'Recogida';

	if(paisRecogida != "--")
	{
		cargaArchivo('selectCiudadRecogida', 'select-ciudad.php', parametros);
	}
	else
	{
		muestraCamposDesactivados('ciudad_recogida');
	}
}

//muestra el select con los lugares de recogida
function muestraLugarRecogida()
{
	var ciudadRecogida = document.getElementById('ciudad_recogida').value;
	var parametros = ciudadRecogida + ',' + 'Recogida';

	if(ciudadRecogida != "--")
	{
		cargaArchivo('selectLugarRecogida', 'select-lugar.php', parametros);
		
		if(document.getElementById('ciudad_devolucion').disabled == true)
		{
			muestraCiudadDevolucion(ciudadRecogida);
			muestraLugarDevolucion(ciudadRecogida);
		}
		
		muestraCamposDesactivados('fecha_recogida');
		
	}
	else
	{
		muestraCamposDesactivados('lugar_recogida');
	}
}

//activa la fecha y hora de recogida
function muestraFechaRecogida()
{
	var lugarRecogida = document.getElementById('lugar_recogida').value;
	
	if(lugarRecogida != "--")
	{
		document.getElementById('fecha_recogida').disabled = false;
		document.getElementById('hora_recogida').disabled = false;
		
		var lugarDevolucion = document.getElementById('lugar_devolucion').value;
		
		if(lugarDevolucion == "--")
		{
			muestraLugarDevolucion();
		}
	}
	else
	{
		muestraCamposDesactivados('fecha_recogida');
	}
}

//muestra el select de la ciudad de devolucion
function muestraCiudadDevolucion(ciudadRecogida)
{
	var paisRecogida = document.getElementById('pais_recogida').value;
	var parametros = paisRecogida + ',' + 'Devolucion' + ',' + ciudadRecogida;

	if(paisRecogida != "--")
	{
		cargaArchivo('selectCiudadDevolucion', 'select-ciudad.php', parametros);
	}
	else
	{
		muestraCamposDesactivados('ciudad_devolucion');
	}
}

//muestra el select con los lugares de devolucion
function muestraLugarDevolucion(ciudadDevolucion)
{	
	if(!ciudadDevolucion)
	{
		var ciudadDevolucion = document.getElementById('ciudad_devolucion').value;
	}
		
	var parametros = ciudadDevolucion + ',' + 'Devolucion';
	
	if(ciudadDevolucion != "--")
	{
		cargaArchivo('selectLugarDevolucion', 'select-lugar.php', parametros);
	}
	else
	{
		muestraCamposDesactivados('lugar_devolucion');
	}
}

//activa la fecha y hora de devolucion
function muestraFechaDevolucion()
{
	var lugarDevolucion = document.getElementById('lugar_devolucion').value;
	
	if(lugarDevolucion != "--")
	{
		document.getElementById('fecha_devolucion').disabled = false;
		document.getElementById('hora_devolucion').disabled = false;
	}
	else
	{
		muestraCamposDesactivados('fecha_devolucion');
	}
}

//muestra los campos que se le indiquen desactivado
function muestraCamposDesactivados()
{
	var selectCiudadRecogida;
	var selectLugarRecogida;
	var selectPaisDevolucion;
	var selectCiudadDevolucion;
	var selectLugarDevolucion;
	
	for(i=0; i<arguments.length; i++)
	{
		switch(arguments[i])
		{
			case "ciudad_recogida":			
				selectCiudadRecogida = '<select id="ciudad_recogida" name="ciudad_recogida" class="selectBuscador" disabled>';
				selectCiudadRecogida += '<option value="">City</option>';
				selectCiudadRecogida += '</select>';
			
				document.getElementById('selectCiudadRecogida').innerHTML = selectCiudadRecogida;
			
			case "lugar_recogida":			
				selectLugarRecogida = '<select id="lugar_recogida" name="lugar_recogida" class="selectBuscador2" disabled>';
				selectLugarRecogida += '<option value="">Pick-up</option>';
				selectLugarRecogida += '</select>';
			
				document.getElementById('selectLugarRecogida').innerHTML = selectLugarRecogida;
			
			case "fecha_recogida":
				document.getElementById('fecha_recogida').disabled = true;
				document.getElementById('hora_recogida').disabled = true;
			break;
					
			case "pais_devolucion":				
				selectPaisDevolucion = '<select id="pais_devolucion" name="pais_devolucion" class="selectBuscador" disabled>';
				selectPaisDevolucion += '<option value="">' + document.getElementById('pais_recogida').value + '</option>';
				selectPaisDevolucion += '</select>';
				
				document.getElementById('selectPaisDevolucion').innerHTML = selectPaisDevolucion;
			
			case "ciudad_devolucion":			
				selectCiudadDevolucion = '<select id="ciudad_devolucion" name="ciudad_devolucion" class="selectBuscador" disabled>';
				selectCiudadDevolucion += '<option value="">City</option>';
				selectCiudadDevolucion += '</select>';
			
				document.getElementById('selectCiudadDevolucion').innerHTML = selectCiudadDevolucion;
			
			case "lugar_devolucion":			
				selectLugarDevolucion = '<select id="lugar_devolucion" name="lugar_devolucion" class="selectBuscador2" disabled>';
				selectLugarDevolucion += '<option value="">Drop-off</option>';
				selectLugarDevolucion += '</select>';
			
				document.getElementById('selectLugarDevolucion').innerHTML = selectLugarDevolucion;
			
			case "fecha_devolucion":
				document.getElementById('fecha_devolucion').disabled = true;
				document.getElementById('hora_devolucion').disabled = true;
			break;
		}
	}
}

//oculta los campos selects ya que ie6 los situa por encima de las capas
function ocultaSelects(opcion)
{
	switch(opcion)
	{
		case "calendar": //calendario recogida
			document.getElementById('hora_recogida').style.display = "none";
			document.getElementById('ciudad_devolucion').style.display = "none";
			
			for(i=0; i<numeroTipos; i++)
			{
				if(document.getElementById('tipos' + i))
				{
					document.getElementById('tipos'+i).style.display = "none";	
				}
			}
			
		break;
		
		case "calendar2": //calendario devolucion
			document.getElementById('hora_devolucion').style.display = "none";
			
			for(i=0; i<numeroTipos; i++)
			{
				if(document.getElementById('tipos' + i))
				{
					document.getElementById('tipos'+i).style.display = "none";	
				}
			}
			
		break;
	}
}

//muestra los campos selects ya que ie6 los situa por encima de las capas
function muestraSelects(opcion)
{
	switch(opcion)
	{
		case "calendar": //calendario recogida
			document.getElementById('hora_recogida').style.display = "";
			document.getElementById('ciudad_devolucion').style.display = "";
			
			for(i=0; i<numeroTipos; i++)
			{
				if(document.getElementById('tipos' + i))
				{
					document.getElementById('tipos'+i).style.display = "";	
				}
			}
			
		break;
		
		case "calendar2": //calendario devolucion
			document.getElementById('hora_devolucion').style.display = "";
			
			for(i=0; i<numeroTipos; i++)
			{
				if(document.getElementById('tipos' + i))
				{
					document.getElementById('tipos'+i).style.display = "";	
				}
			}
			
		break;
	}
}

//comprueba si están todos los datos y envia el formulario
function realizaBusqueda()
{
	var id_lugar1 = document.getElementById('lugar_recogida').value;
	var fecha1 = document.getElementById('fecha_recogida').value;
	var hora1 = document.getElementById('hora_recogida').value;
	var id_lugar2 = document.getElementById('lugar_devolucion').value;
	var fecha2 = document.getElementById('fecha_devolucion').value;
	var hora2 = document.getElementById('hora_devolucion').value;
	var codigo_promocion = document.getElementById('codigo_promocion').value;
	var vecesBusqueda = 0;
		
	//pasamos las fechas a formato de fecha para que el javascript pueda comparar cual es mayor
	var fecha1_data = fecha1.split("/");
	var fecha2_data = fecha2.split("/");
	
	var fecha1FormatoDate = new Date(fecha1_data[2],fecha1_data[1],fecha1_data[0]);
	var fecha2FormatoDate = new Date(fecha2_data[2],fecha2_data[1],fecha2_data[0]);
			
	//comprobamos que todos los campos estén listos para enviar los datos
	if(id_lugar1 != "--" && fecha1 != "" && hora1 != "" && id_lugar2 != "--" && fecha2 != "" && hora2 != "")
	{
		document.getElementById('buscador3').style.display = "";
		
		//comprobamos que la fecha de recogida no sea la misma que hoy
		if(fecha1 == fechaHoy)
		{
			alert("The date of the pick-up cannot be the same date as today.");
			return false;
		}
		
		//si las dos fechas son iguales no puede ser la hora ni igual ni menor solo superior
		if(fecha1 == fecha2  && (hora1 > hora2 || hora1 == hora2))
		{
			alert("You cannot select that time as the pick-up and the return of the vehicle are on the same day.");
			return false;
		}
		
		//comprobamos que la fecha 1 sea menor que la fecha2
		if(fecha2FormatoDate < fecha1FormatoDate)
		{
			alert("The date of the return of the vehicle cannot be before the date of the pick-up.");
			return false;	
		}
				
		//creamos un array para pasarlo por parametro para realizar la busqueda
		var datosBusqueda = Array(id_lugar1,fecha1,hora1,id_lugar2,fecha2,hora2,codigo_promocion,vecesBusqueda);
		
		//numeroTipos += 8;
				
		for(i=0; i<numeroTipos; i++)
		{
			//var name = parseInt(i) - 8;
			
			//alert(name);
			
			if(document.getElementById("tipos" + i))
			{
				
				if(document.getElementById("tipos" + i).checked == true)
				{					
					datosBusqueda[datosBusqueda.length] = document.getElementById("tipos" + i).value;		
					//alert(datosBusqueda.length);
				}
			
			}
		}
				
		//alert("Enviamos datos");
		
		realizandoBusqueda();		
		cargaArchivo("centroPagina","busqueda.php",datosBusqueda);
	}
		
}


function realizandoBusqueda()
{
		document.getElementById('centroPagina').innerHTML = '<div class="mensajeBusqueda"><table><tr><td><img src="http://www.vivecar.com/imagenes/loader.gif" /></td><td>We are searching, please wait... </tr></table></div>';
}

function enviaReserva(pagoTarjeta)
{
	var camposObligatorios = new Array("nombre","apellidos","telefono","email","confirma_email","pais");
	
	for(i=0; i<camposObligatorios.length; i++)
	{
		if(document.getElementById(camposObligatorios[i]).value == "")
		{
			alert("Complete the compulsory fields marked with an asterisk.");
			return false;
		}
	}
	
	if(document.getElementById(camposObligatorios[3]).value != document.getElementById(camposObligatorios[4]).value)
	{
		alert("The confirmation of the e-mail address is incorrect, repeat the same data as in the e-mail address field.");
		return false;
	}
		
	if(document.getElementById('acepto').checked == false)
	{
		alert("Read and accept the conditions of use.");
		return false;
	}
	
	document.getElementById('pagoTarjeta').value = pagoTarjeta;
	
	document.getElementById('f_reserva').submit();
}
function cambiaPrecioReserva(){
	
	var cantidadSillas = parseInt(document.getElementById('sillas_ninio').value);
	var precioSillas = parseFloat(document.getElementById('precio_sillas_ninio').value);
	
	var cantidadBacas = parseInt(document.getElementById('baca').value);
	var precioBacas = parseFloat(document.getElementById('precio_baca').value);
	
	var cantidadElevadores = parseInt(document.getElementById('elevadores').value);
	var precioElevadores = parseFloat(document.getElementById('precio_elevadores').value);
	
	var diasAlquiler = parseInt(document.getElementById('dias').value);
	
	var valorLibra = parseFloat(document.getElementById('valorLibra').value);
	
	var precio = parseFloat(document.getElementById('precioInicial').value);
	var precioTarjeta = parseFloat(document.getElementById('precioInicialTarjeta').value);
	
	precioIncrementar = parseFloat((precioSillas * cantidadSillas * diasAlquiler) + ( cantidadBacas * precioBacas * diasAlquiler ) + ( cantidadElevadores * precioElevadores * diasAlquiler ));
	
	precio += precioIncrementar;
	
	precio = Math.round(precio*100)/100;
	
	document.getElementById('muestraPrecio').innerHTML = precio;
	
	var precio2 = precio*valorLibra;
	precio2 = Math.round(precio2*100)/100;
	
	document.getElementById('muestraPrecioLibra').innerHTML = precio2;
	
	
	//si hay descuento sustituimos los precios
	if(document.getElementById('muestraPrecioDescuento'))
	{
		precio = parseFloat(document.getElementById('precioInicialDescuento').value) + precioIncrementar;
		
		precio = Math.round(precio*100)/100;
		
		document.getElementById('muestraPrecioDescuento').innerHTML = precio;
		
		var precio3 = precio*valorLibra;
		precio3 = Math.round(precio3*100)/100;
		
		document.getElementById('muestraPrecioDescuentoLibra').innerHTML = precio3;
	}
	
	if(document.getElementById('muestraPrecioTarjeta'))
	{
		precioTarjeta += precioIncrementar;
		document.getElementById('muestraPrecioTarjeta').innerHTML = precioTarjeta;
		
		precioTarjeta = parseFloat(document.getElementById('precioInicialTarjetaDescuento').value) + precioIncrementar;
		
		precioTarjeta = Math.round(precioTarjeta*100)/100;
		
		if(document.getElementById('muestraPrecioTarjetaDescuento'))
			{
			document.getElementById('muestraPrecioTarjetaDescuento').innerHTML = precioTarjeta;
			}
		
		document.f_reserva.precioTarjeta.value = precioTarjeta;
	}
	
	precio = Math.round(precio*100)/100;
	
	document.f_reserva.precio.value = precio;
	
}
function mirarOfertaDias(diaInicio, mesInicio, anoInicio, diaFin, mesFin, anoFin){
	var error=0;
	dia=document.getElementById('fecha_recogida0').value;
	mes=document.getElementById('fecha_recogida1').value;
	ano=document.getElementById('fecha_recogida2').value;
	
	dia2=document.getElementById('fecha_devolucion0').value;
	mes2=document.getElementById('fecha_devolucion1').value;
	ano2=document.getElementById('fecha_devolucion2').value;
	
	if(ano<=anoFin && ano>=anoInicio){
		if(mes<=mesFin && mes>=mesInicio){
			if(mes==mesFin && dia<=diaFin){
				error=0;
			}else if(mes==mesInicio && dia>=diaInicio){
				error=0;
			}else if(mes>mesInicio && mes<mesFin){
				error=0;
			}else{
				error=1	
			}
		}else{
			error=1;
		}
	}else{
		error=1;
	}
	
	if(ano2>=ano){
		if(mes2>=mes){
			if(mes2==mes){
				if(dia2<dia){
					error=2;
				}
			}
		}else{
			error=2	
		}
	}else{
		error=2;	
	}
	
	if(error==0){
		document.f_fecha.submit();
	}else if(error==1){
		alert("The date of collection of the vehicle must be within the indicated days of supply");
	}else{
		alert("The return date of the vehicle has to be after collection");
	}
	
	
}
