// JavaScript Document modelo7.js
function showVuelo() {
	document.getElementById("vuelo").style.visibility='visible';
  	document.form1.txt_vuelo.style.visibility = 'visible';
}
 
function hideVuelo() {
	document.getElementById("vuelo").style.visibility='hidden'; 	
	document.form1.txt_vuelo.style.visibility = "hidden";
}
		
function vVuelo(campo){
	if (campo == 'recogida') {
		document.getElementById('devolucion').value = document.getElementById('recogida').value;}
		// Si es aeropuerto, activar campo vuelo							
		switch (document.getElementById('recogida').value) {
    		case '31':  
    		case '80':
    		case '82':
			case '70':
      			showVuelo(); 
      			break
    		default:
			hideVuelo();
	} 
}	


	function actualizaresultado(campo) {
		
		var recogida=document.getElementById('recogida').value;
	
		if (recogida == '0') { return; }
		
		
			var day1 = $("#datBegin").datepicker('getDate').getDate();  
			if (day1 < 10) { day1 = '0'+day1;} 
            var month1 = $("#datBegin").datepicker('getDate').getMonth() + 1;  
			if (month1 < 10) { month1 = '0'+month1;}
            var year1 = $("#datBegin").datepicker('getDate').getFullYear();
		 	var day2 = $("#datEnd").datepicker('getDate').getDate();  
			if (day2 < 10) { day2 = '0'+day2;}
            var month2 = $("#datEnd").datepicker('getDate').getMonth() + 1;
			if (month2 < 10) { month2 = '0'+month2;}
            var year2 = $("#datEnd").datepicker('getDate').getFullYear();
		 
		 document.getElementById('tdatBegin').value = year1 + "-" + month1 + "-" + day1;
		 document.getElementById('tdatEnd').value   = year2 + "-" + month2 + "-" + day2;
		 
		 document.getElementById('vdatBegin').value = day1 + "/" + month1 + "/" + year1;
		 document.getElementById('vdatEnd').value   = day2 + "/" + month2 + "/" + year2;
			
		//Check data
			
	var recogida=document.getElementById('recogida').value;
	var devolucion=document.getElementById('devolucion').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;
	
	// Set 2 dates
	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);
	var desde  = new Date(year0,month0,day0);
	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);
	
	//Set 1 day in milliseconds
	var one_day=1000*60*60*24
	d = new Date();
	msg='';
	
	if (((recogida==61) || (recogida==70) || (recogida==80) || (devolucion==61) || (devolucion==70) || (devolucion==80)) && (recogida != devolucion)) {
		msg = "Oops, it is not allowed to take the vehicle to another island.\n\n";
	}
	
	//Get difference btw the two dates, and convert to days
	if ( (Math.ceil((hasta.getTime()-desde.getTime())/(one_day))) > 30) { msg = "Oops, the maximum rental period is 30 days.\n\n";}
	
	
	if ((year0 < d.getFullYear()) || (year0 == d.getFullYear() && month0 < d.getMonth()) || (year0 == d.getFullYear() && month0 == d.getMonth() && day0 <= d.getDate())) { 
		msg+="You are trying to start the rental period earlier than today’s date. Please check the rental start date and try again.\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+="Our reservation department needs at least 24 hours before the pick-up hour to process the booking.\n\n";
		}
	} 
		
	 // Puerto de Ibiza todo el año y oficinas de Abril a Octubre
	 if ((recogida != 31) && (recogida != 70) && (recogida != 80)) {
		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+="Opening hours for selected location are 8:30 to 13:00 & from 16:30 to 20:00.\n\n";	
		} 
	 }
	
	if ((fechainicio == fechafin) && (horainicio == horafin)){
		msg+="Oops, please check the dates.\n\n"; 
	} 
	
		//	Decision
		if (msg=='') {
			$("#listado").load("Scripts/search6.php", $("#form1").serialize() );
		} else { 
		msg+="Thank You.";
		alert(msg);
		}																		
	}   // End actualizaresultado
	 
	 
    function showOfertas() { 
   		//if (document.getElementById('recogida').value == 0) {
   			$("#listado").load("Scripts/procesa_ofertas-2.php", {ambito: document.getElementById('recogida').value});
		//} else { $("#listado").load("Scripts/search6.php", $("#form1").serialize() ); }
	}
	
	function showCoches() {
		$("#listado").load("Scripts/search6.php", $("#form1").serialize() );
	}
	
	function fechasIni() {
			/*if (document.getElementById('recogida').value ==61) {
						$("#datBegin").datepicker("setDate", 01+'/'+03+'/'+2011 ); 
				  		$("#datEnd").datepicker( "setDate", 04+'/'+03+'/'+2011 ); }*/
	}
	function getOfertas(loc) {
		if ((loc == 61) || (loc == 70)) {
			$("#datBegin").datepicker("setDate", 30+'/'+03+'/'+2012 ); 
			$("#datEnd").datepicker( "setDate", 02+'/'+04+'/'+2012 );
		}
		document.getElementById('recogida').value = loc;
		document.getElementById('devolucion').value = loc;
		actualizaresultado();
	}
	function textoBanner(txt_id) { //alert (txt_id);
	 $("#listado").load("Scripts/textoBanner.php", {txt_id: txt_id});
	}
	
	function accesoCliente() {	
		$("#mensaje").load("Scripts/access_customer.php",$("#cliente").serialize(),	
			function() {
  				if (document.getElementById('swcol').value != "") { 
					document.getElementById('cliente').submit(); 
				} else { return false;} 
		});
	}
	
	// Calendario
	$(function() {
		$.datepicker.regional['en'] = {
		closeText: 'X',
		prevText: 'Prev',
		nextText: 'Next',
		currentText: 'Today',
		monthNames: ['January','February','March','April','May','June',
		'July','August','September','October','November','December'],
		monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
		'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
		dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
		dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
		dayNamesMin: ['Su','Mo','Tu','We','Th','Fr','Sa'],
		weekHeader: 'Wk',
		dateFormat: 'dd/mm/yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
		$.datepicker.setDefaults($.datepicker.regional['en']);
	
		$( "#datBegin" ).datepicker({
				  showOn: 'both',
				  buttonImageOnly: true,
				  buttonImage: '',
				  showButtonPanel: true,
				  numberOfMonths: 1,
				  dateFormat: 'dd/mm/yy',
				  firstDay: 1,
				  minDate: '+1d',
				  showAnim: '',
				  showOtherMonths: true,
				  selectOtherMonths: true,
				  onChangeMonthYear: function(year, month, inst) { 
						
						var fecha1 = new Date();					
						fecha1 =($("#datBegin").datepicker( "getDate"));
						dia1 = (fecha1.getDate());
						mes = fecha1.getMonth() + 1;
						yy = fecha1.getYear();
						//alert (mes);			
						
				 		var fecha2 = new Date();					
						fecha2 =($("#datEnd").datepicker( "getDate"));
						dia2 = (fecha2.getDate());
						mes2 = fecha2.getMonth() + 1;
						yy2 = fecha2.getYear();
						/*
						
						$("#datBegin").datepicker("setDate", dia1+'/'+month+'/'+year ); 
				  		$("#datEnd").datepicker( "setDate", dia2+'/'+month+'/'+year );*/ }
				});
		
		$( "#datEnd" ).datepicker({
				  showOn: 'both',
				  buttonImageOnly: true,
				  buttonImage: '',
				  showButtonPanel: true,
				  numberOfMonths: 1,
				  dateFormat: 'dd/mm/yy',
				  firstDay: 1,
				  minDate: '+1d',
				  showAnim: '',
				  showOtherMonths: true,
				  selectOtherMonths: true,
				  defaultDate: +4
				});
	});
