// functions.js checks form data calls alquiler-coche-datos.php
function ActionDetermina(){
	var recogida=document.getElementById('select').value;
	var devolucion=document.getElementById('select2').value;
	var fechainicio=document.getElementById('datBegin').value;
	var fechafin=document.getElementById('datEnd').value;
	var horainicio=document.getElementById('horainicio').value;
	var horafin=document.getElementById('horafin').value;
	var booster=document.getElementById('booster').value;
	var sillita=document.getElementById('sillita').value;
	var dias=document.getElementById('hdias').value;
	var grupo=document.getElementById('hgrupo').value;
	var importe=document.getElementById('hprecio').value;	
	var dday   = (document.getElementById('datBegin').value).substr(0,2);
	var dmonth = (document.getElementById('datBegin').value).substr(3,2) - 1;
	var dyear  = (document.getElementById('datBegin').value).substr(6,4);
	var desde  = new Date(dyear,dmonth,dday);
	var dday   = (document.getElementById('datEnd').value).substr(0,2);
	var dmonth = (document.getElementById('datEnd').value).substr(3,2) - 1;
	var dyear  = (document.getElementById('datEnd').value).substr(6,4);
	var hasta  = new Date(dyear,dmonth,dday);
	var in23 = new Date(2009,11,23); // Ojo, el mes - 1
	var in26 = new Date(2009,11,26);
	var in2  = new Date(2010,0,2);
	var in30 = new Date(2009,11,30);		
	var inicio_temporada = new Date(2010,2,1);
	var day0   = (document.getElementById('datBegin').value).substr(0,2);
	var month0 = (document.getElementById('datBegin').value).substr(3,2) - 1;
	var year0  = (document.getElementById('datBegin').value).substr(6,4);
	d = new Date();
	msg='';
	//Set the two dates
	var today=new Date(year0,month0,day0);
	var christmas=new Date(dyear, dmonth, dday);
	//Set 1 day in milliseconds
	var one_day=1000*60*60*24
	//Calculate difference btw the two dates, and convert to days
	if ( (Math.ceil((christmas.getTime()-today.getTime())/(one_day))) > 30) { msg = "Ooops, el periodo maximo de alquiler es de 30 dias.\n\n";}
	
	
	if ((year0 < d.getFullYear()) || (year0 == d.getFullYear() && month0 < d.getMonth()) || (year0 == d.getFullYear() && month0 == d.getMonth() && day0 <= d.getDate())) { 
		msg+="Por favor, revisa la fecha de recogida.\n\n";
	}
	else if (year0 == d.getFullYear() && month0 == d.getMonth() && day0 <= d.getDate()+ 1) {
		var x0 = 1;
		x0 =  horainicio.substr(0,2);
			
		if ((eval(d.getHours() - x0) > 11) && (x0 < 12) && (d.getHours() > 12)) { 
			msg+="Nuestro departamento de reservas precisa de 24h de antelacion para procesar la reserva.\n\n";
		}
	} 
	
	if ((desde < inicio_temporada) && (recogida != 31) && (recogida != 80) && (recogida != 82) && (recogida != 32) && (recogida != 61)) {
		msg+="La oficina seleccionada abre a partir del 1 de marzo.\n\n";
	} else if ((recogida != 80) && (recogida != 31) && (recogida != 82)) { // Horario de oficinas que no sean aeropuertos	
		
		if (recogida == 61 ) {
			if (((horainicio < '08:30') || (horainicio > '20:00')  ) || ((horafin < '08:30') || (horafin > '20:00') )) {
					msg+="El horario de la oficina de Formentera es entre las 8:30 y las 20:00.\n\n";	
				} 
			}
		else 
		if (((horainicio < '08:30') || (horainicio > '20:00') || ((horainicio > '13:00' ) && (horainicio < '16:30') ) ) || ((horafin < '08:30') || (horafin > '20:00') || ((horafin > '13:00' ) && (horafin < '16:30') ) )) {
			msg+="Horario de oficina seleccionada es entre 8:30 y 13:00 y entre las 16:30 y las 20:00.\n\n";	
		} 
	} 
		
	if  (((desde > in23) && (desde < in26)) || ((desde > in30) && (desde < in2))) {
		msg+="Oooops no hay servicio de recogida - devolucion los dias 24,25 y 31 de diciembre y el 1 de enero.\n\n"; 	
	}
	
	if (dias == 0) {
		msg+="Por favor revisa las fechas.\n\n"; 
	} 
	if (importe == 0) {
		msg+="Por favor revisa los datos.\n\n"; 
	} 
	if (document.getElementById('clausulas').checked == false) {
		msg+="Por favor revisa nuestras condiciones de alquiler.\n\n"; 
	}
	if (msg=='') {
	document.location = '../../alquiler-coche-datos.php?recogida='+recogida+'&devolucion='+devolucion+'&fechainicio='+fechainicio+'&fechafin='+fechafin+'&horainicio='+horainicio+'&horafin='+horafin+'&booster='+booster+'&sillita='+sillita+'&dias='+dias+'&grupo='+grupo+'&importe='+encodeURIComponent(importe);
	} else { 
		msg+="Gracias.";
		alert(msg);
	}
}

