$(document).ready(function(){

	// popAlpi HELP
	jQuery('.popAlpiSearch').popAlpiSearch();
	jQuery('#poaltoSearch').livequery(function(){
		jQuery(this).bgiframe();
	});	


	//	variabili utilizzate nel parsing dell'xml
	var messaggi	= [];
			
	var chiamaWs = function(){
		var $sb = $('#searchBox');
		var offset = $sb.offset();
		var w = $sb.innerWidth();
		var h = $sb.outerHeight();
		//h -= $('#hotel_ricerca_avanzata').outerHeight();
		
		$('<div id="blocker" style="background-color:#a6cfe8; position:absolute; top:' + offset.top + 'px; left:' + (offset.left + 3) + 'px; height:' + h + 'px; width:' + (w - 6) + 'px;"></div>')
			.css('opacity', '0.5')
			.bgiframe()
			.appendTo('body');
		$('<div id="loader" style="position:absolute; top:' + (offset.top + 121) + 'px; left:' + (offset.left + 80) + 'px; background:url(/ResEBook/img/pb/ajax-loader.gif) 3px center no-repeat #a6cfe8; width:80px; color:#fff; padding:2px 0 0 23px; text-align:left;">Attendere caricamento...</div>')
			.bgiframe()
			.appendTo('body');
		
		$.ajax({
			url: '/EBookingWeb/SEAVOHO',
			type: 'get',
			dataType: 'xml',
			success: function(xmlData){
						    
				$('#blocker, #loader').remove();
				data = xmlData;
				// msg-list
				$(data).find('msg-list > msg').each(function(i){
					messaggi[i] = {};
					messaggi[i]['code'] = $(this).find('code').text();
					messaggi[i]['text'] = $(this).find('text').text();
				});
				
				if (messaggi[0].code == '0' && messaggi[0].text == 'Completed.') {
					$('P000001', data).each(function(i){
						xmlDoc=this;
						// funzione per inserimento livello T = 1
						loadLev(this);
						loadDest('aptp','','1','');
						setComboPar('form_volohotel');				
					});
					$('D000001', data).each(function(i){
						xmlDoc=this;
						// funzione per inserimento livello T = 1
						loadLev(this);
						loadDest('apta','','1','');
						setComboArr('form_volohotel');						
					});				
				}	//	fine if (messaggi.code && messaggi.text)
				
					//	se si ricevono messaggi diversi da "0" e "Completed." dalla servlet
				else alert("Errore nella ricezione dei dati.");
			},	// fine success
			error: function(){
				alert("ERRORE: Impossibile contattare il server. Provare a ricaricare la pagina.");
			}	// fine error
		});
	};
	
	/** FUNZIONI COMUNI **/
		
	//	datepicker
	//	inizializzo le variabili per il calcolo delle date
	var laData = new Date();
	var gg = laData.getDate();
	var mm = laData.getMonth();
	var aaaa = laData.getFullYear();
	var oggi = gg + '/' + mm + '/' + aaaa;
	var annoprossimo = (+ aaaa) + 1;

	var fraUnAnno = new Date();
	fraUnAnno.setDate(laData.getDate() + 365);
	
	var ggP = $('#h_g_p').val(); 
	var mmP = $('#hotel_m_p').val(); 
	var aaP = $('#hotel_a_p').val(); 
	var ggA = $('#h_g_a').val(); 
	var mmA = $('#hotel_m_a').val(); 
	var aaA = $('#hotel_a_a').val(); 
	
	var ggVoloA = $('#v_g_a').val(); 
	var mmVoloA = $('#volo_m_a').val(); 
	var aaVoloA = $('#volo_a_a').val(); 
	var ggVoloR = $('#v_g_r').val(); 
	var mmVoloR = $('#volo_m_r').val(); 
	var aaVoloR = $('#volo_a_r').val(); 

	var domani = new Date();
	domani.setDate(laData.getDate() + 1);
		
	var dopodomani = new Date();
	if(laData.getDay() == 6 || laData.getDay() == 0)
		dopodomani.setDate(laData.getDate() + 3);
	else
		dopodomani.setDate(laData.getDate() + 2);
	
	var ggDomani = dopodomani.getDate();
	var mmDomani = dopodomani.getMonth()+1;
	var aaaaDomani = dopodomani.getFullYear();

	var ggDomaniStr = ggDomani.toString();
	var mmDomaniStr = mmDomani.toString();

	if (ggDomaniStr.length==1) 
		ggDomaniStr = '0' + ggDomaniStr;
	if (mmDomaniStr.length==1) 
		mmDomaniStr = '0' + mmDomaniStr;
	
	var maDomani = mmDomaniStr  + '/' + aaaaDomani;
			
	//	popolo le select per giorno e mese/anno
	//	gg
	for (var t = 1; t < 32; t++) {
		
		var day, valday;
		if (t < 10) day = valday = '0' + t;
		else day = valday = t;
		var optA = '<option value="' + valday + '">' + day + '</option>';
		var optP = '<option value="' + valday + '">' + day + '</option>';
		var optVoloA = '<option value="' + valday + '">' + day + '</option>';
		var optVoloR = '<option value="' + valday + '">' + day + '</option>';
				
		//DATE x VOLO
		if (ggVoloA=='' && ggDomaniStr==day) {
			$(optVoloA).attr('selected','selected').appendTo('#volo_g_a');
			$('#v_g_a').val(day); 
		}
		else if(valday==ggVoloA)
			$(optVoloA).attr('selected','selected').appendTo('#volo_g_a');
		else
			$(optVoloA).appendTo('#volo_g_a');
		
		if(valday==ggVoloR)
			$(optVoloR).attr('selected','selected').appendTo('#volo_g_r');
		else
			$(optVoloR).appendTo('#volo_g_r');

		//DATE x HOTEL
		if (ggA=='' && ggDomaniStr==day) {
			$(optA).attr('selected','selected').appendTo('#hotel_g_a');
			$('#h_g_a').val(day); 
		}
		else if(valday==ggA)
			$(optA).attr('selected','selected').appendTo('#hotel_g_a');
		else
			$(optA).appendTo('#hotel_g_a');
		
		if(valday==ggP)
			$(optP).attr('selected','selected').appendTo('#hotel_g_p');
		else
			$(optP).appendTo('#hotel_g_p');
	}
	//	mm/aaaa
	for (var t = 1; t < 14; t++) {
		var mese = mm;
		var anno = ((mese + t) > 12) ? (aaaa + 1) : (aaaa);
		mese = ((mese + t) > 12) ? (mese + t - 12) : (mese + t);
		if (mese < 10) mese = '0' + mese;
		var ma = mese  + '/' + anno;
		
		var optA = '<option value="' + anno  + mese + '">' + ma + '</option>';
		var optP = '<option value="' + anno  + mese + '">' + ma + '</option>';
		var optVoloA = '<option value="' + anno  + mese + '">' + ma + '</option>';
		var optVoloR = '<option value="' + anno  + mese + '">' + ma + '</option>';

		//DATE x VOLO
		if (aaVoloA=='' && mmVoloA=='' && maDomani==ma) {
			$(optVoloA).attr('selected','selected').appendTo('#volo_ma_a');
			$('#volo_m_a').val(mese); 
			$('#volo_a_a').val(anno); 
		}
		
		else if (anno==aaVoloA && mese==mmVoloA)
			$(optVoloA).attr('selected','selected').appendTo('#volo_ma_a');
		else
			$(optVoloA).appendTo('#volo_ma_a');
		
		if (anno==aaVoloR && mese==mmVoloR)
			$(optVoloR).attr('selected','selected').appendTo('#volo_ma_r');
		else
			$(optVoloR).appendTo('#volo_ma_r');

		//DATE x HOTEL
		if (aaA=='' && mmA=='' && maDomani==ma) {
			$(optA).attr('selected','selected').appendTo('#hotel_ma_a');
			$('#hotel_m_a').val(mese); 
			$('#hotel_a_a').val(anno); 
		}
		
		else if (anno==aaA && mese==mmA)
			$(optA).attr('selected','selected').appendTo('#hotel_ma_a');
		else
			$(optA).appendTo('#hotel_ma_a');
		
		if (anno==aaP && mese==mmP)
			$(optP).attr('selected','selected').appendTo('#hotel_ma_p');
		else
			$(optP).appendTo('#hotel_ma_p');		
	}
	
	//Se la data di arrivo in Hotel è vuota
	if (ggA=='' && aaA=='' && mmA=='')
		$('#hotel_h_data_a').val(ggDomani + "/" + mmDomani + "/" + aaaaDomani);
			
	//Se la data di partenza Volo è vuota
	if (ggVoloA=='' && aaVoloA=='' && mmVoloA=='')
		$('#volo_h_data_a').val(ggDomani + "/" + mmDomani + "/" + aaaaDomani);

	//Gestione popolamento tendine viaggiatori per RsOnlyModificaMotoreVOHO.jsp
	var keydVal =  document.forms['form_volohotel'].keyd.value;
	if (keydVal != '') {
		// per RsOnlyModificaMotoreVOHO.jsp
		try {
		pageLoad();
		}
		catch(e) {};
	}
	
	// Prevent selection of invalid dates through the select controls 
	function checkLinkedDays() {
		var mese = jQuery(this).val().substring(4);
		var anno = jQuery(this).val().substring(0,4);
		var daysInMonth = 32 - new Date(anno, mese - 1, 32).getDate(); 
		$(this).prev('select').find('option').attr('disabled', ''); 
		$(this).prev('select').find('option:gt(' + (daysInMonth) +')').attr('disabled', 'disabled'); 
		if ($(this).prev('select').val() > daysInMonth) { 
			$(this).prev('select').val(daysInMonth); 
		} 
		
	};
	
	$('#hotel_ma_a, #hotel_ma_p,#volo_ma_a, #volo_ma_r').change(checkLinkedDays);
			
	function readLinked() { 
		var _temp_id = this.id.split('_');
		var ma = jQuery('#' + _temp_id[0] + '_ma_' + _temp_id[3]).val();
		var g = jQuery('#' + _temp_id[0] + '_g_' + _temp_id[3]).val();

		if(ma!="" && g!=""){
			jQuery('#' + this.id).val( 
				g + '/' + ma.substr(4) + '/' + ma.substr(0,4)  
			); 
		}else{
			if(_temp_id[0]=='volo' && _temp_id[3]=='r' ||_temp_id[0]=='hotel' && _temp_id[3]=='p')
				jQuery('#' + this.id).val(jQuery('#' + _temp_id[0] + '_'+_temp_id[1]+'_' + _temp_id[2] + '_a').val());
		}
		return {}; 
	};
	
	function updateLinked(date) {
		var _temp_id = this.id.split('_');
		var _anno = + date.substring(6, 10);
		var _mese = date.substring(3, 5);

		jQuery('#' + _temp_id[0] + '_ma_' + _temp_id[3]).val(date.substring(3, 5) + '/' + date.substring(6, 10)).change(); 
		jQuery('#' + _temp_id[0] + '_g_' + _temp_id[3]).val(date.substring(0, 2));

		if (this.id == 'volo_h_data_a') {
			$('#volo_m_a').val(_mese);
			$('#volo_a_a').val(_anno)
		}
		if (this.id == 'volo_h_data_r') {
			$('#volo_m_r').val(_mese);
			$('#volo_a_r').val(_anno)
		}
		//Gestione cambio date voli
		if (this.id == 'volo_h_data_a') {
			if (($('#volo_g_r').val() == '' && $('#volo_ma_r').val() == ''))
			{
				var newDate = $('#volo_h_data_a').getDatepickerDate();
				$('#volo_h_data_r').changeDatepicker({minDate: newDate});
				$('#hotel_h_data_a').setDatepickerDate(newDate);
				$('#hotel_h_data_a').changeDatepicker({minDate: newDate});	
				if (newDate.getDate().toString().length == 1)
					var day = '0' + newDate.getDate();
				else
					var day = newDate.getDate();
				$('#hotel_g_a').val(day);
				var actualMonth = parseInt(newDate.getMonth()) + 1;
				if (actualMonth.toString().length == 1)
					var month = '0' + actualMonth;
				else
					var month = actualMonth;
				$('#hotel_ma_a').val(month + '/' + newDate.getFullYear());	
				$('#hotel_m_a').val(month);
				$('#hotel_a_a').val(newDate.getFullYear());
				$('#hotel_h_data_a').val($('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_ma_a').val().substr(0, 4));
			}
			else if ((($('#volo_g_r').val() != '' && $('#volo_ma_r').val() != '')))
			{
				var strDate = $('#volo_ma_r').val().substr(4,6) + '/' + $('#volo_g_r').val() + '/' + $('#volo_ma_r').val().substr(0, 4);
				$('#volo_h_data_r').setDatepickerDate(new Date(strDate));
				if ($('#volo_h_data_a').getDatepickerDate() >= $('#volo_h_data_r').getDatepickerDate())
				{
					var newDate = $('#volo_h_data_a').getDatepickerDate();
					newDate.setDate(newDate.getDate() + 1);
					$('#volo_h_data_r').changeDatepicker({minDate: newDate});
					$('#volo_h_data_r').setDatepickerDate(newDate);
					if (newDate.getDate().toString().length == 1)
						var day = '0' + newDate.getDate();
					else
						var day = newDate.getDate();
					$('#volo_g_r').val(day);
					var actualMonth = parseInt(newDate.getMonth()) + 1;
					if (actualMonth.toString().length == 1)
						var month = '0' + actualMonth;
					else
						var month = actualMonth;
					$('#volo_ma_r').val(month + '/' + newDate.getFullYear());					
					//if ($('#chk_hotel_parz').attr('value') == 'Y')
					//{					
						var newSelectedDate = $('#volo_h_data_a').getDatepickerDate();
						$('#hotel_h_data_a').changeDatepicker({minDate: newSelectedDate});
						$('#hotel_h_data_a').setDatepickerDate(newSelectedDate);				
						if (newSelectedDate.getDate().toString().length == 1)
							var day = '0' + newSelectedDate.getDate();
						else
							var day = newSelectedDate.getDate();
						$('#hotel_g_a').val(day);
						var actualMonth = parseInt(newDate.getMonth()) + 1;
						if (actualMonth.toString().length == 1)
							var month = '0' + actualMonth;
						else
							var month = actualMonth;
						$('#hotel_ma_a').val(month + '/' + newSelectedDate.getFullYear());			
						newSelectedDate.setDate(newSelectedDate.getDate() + 1);
						$('#hotel_h_data_p').changeDatepicker({minDate: newSelectedDate});						
						$('#hotel_h_data_p').setDatepickerDate(newSelectedDate);
						if (newSelectedDate.getDate().toString().length == 1)
							var day = '0' + newSelectedDate.getDate();
						else
							var day = newSelectedDate.getDate();
						$('#hotel_g_p').val(day);
						var actualMonth = parseInt(newSelectedDate.getMonth()) + 1;
						if (actualMonth.toString().length == 1)
							var month = '0' + actualMonth;
						else
							var month = actualMonth;
						$('#hotel_ma_p').val(month + '/' + newSelectedDate.getFullYear());	
					    $('#hotel_m_p').val(month);
						$('#hotel_a_p').val(newSelectedDate.getFullYear())
						$('#hotel_notti').val(1);
						$('#hotel_h_data_a').val($('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_ma_a').val().substr(0, 4));

					//}
				}
				else
				{
					//if ($('#chk_hotel_parz').attr('value') == 'Y')
					//{
						var newSelectedDate = $('#volo_h_data_a').getDatepickerDate();
						$('#hotel_h_data_a').changeDatepicker({minDate: newSelectedDate});
						$('#hotel_h_data_a').setDatepickerDate(newSelectedDate);				
						if (newSelectedDate.getDate().toString().length == 1)
							var day = '0' + newSelectedDate.getDate();
						else
							var day = newSelectedDate.getDate();
						$('#hotel_g_a').val(day);
						var actualMonth = parseInt(newSelectedDate.getMonth()) + 1;
						if (actualMonth.toString().length == 1)
							var month = '0' + actualMonth;
						else
							var month = actualMonth;
						$('#hotel_ma_a').val(month + '/' + newSelectedDate.getFullYear());											
					    $('#hotel_m_a').val(month);
						$('#hotel_a_a').val(newSelectedDate.getFullYear())
						var par = $('#hotel_h_data_p').getDatepickerDate();
						var and = $('#hotel_h_data_a').getDatepickerDate();
						var maxDay = parseInt((Math.max(par, and) - Math.min(par, and)) / 86400000);							
						$('#hotel_notti').val(maxDay);
						$('#hotel_h_data_a').val($('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_ma_a').val().substr(0, 4));
					//}
				}				
			}		
		}
		if (this.id == 'volo_h_data_r') {
			//if ($('#chk_hotel_parz').attr('value') == 'Y')
			//{
			if ($('#volo_g_a').val() != '' && $('#volo_ma_a').val() != '')
			{
				var strDate = $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(0, 4);
				$('#hotel_h_data_a').setDatepickerDate(new Date(strDate));
				var newDate = $('#hotel_h_data_a').getDatepickerDate();
				newDate.setDate(newDate.getDate() + 1);
				$('#hotel_h_data_p').changeDatepicker({minDate: newDate});
				var newSelectedDate = $('#hotel_h_data_a').getDatepickerDate();
				$('#hotel_h_data_p').setDatepickerDate(newSelectedDate);
				
				$('#hotel_h_data_p').val($('#volo_g_r').val() + '/' + $('#volo_ma_r').val().substr(4,6) + '/' + $('#volo_ma_r').val().substr(0, 4));
				
				if (newSelectedDate.getDate().toString().length == 1)
					var day = '0' + newSelectedDate.getDate();
				else
					var day = newSelectedDate.getDate();
				$('#hotel_g_a').val(day);
				var actualMonth = parseInt(newSelectedDate.getMonth()) + 1;
				if (actualMonth.toString().length == 1)
					var month = '0' + actualMonth;
				else
					var month = actualMonth;
				$('#hotel_ma_a').val(month + '/' + newSelectedDate.getFullYear());
				$('#hotel_m_a').val(month);
				$('#hotel_a_a').val(newSelectedDate.getFullYear())
				$('#hotel_notti').val('');
			}	
			if ($('#volo_g_r').val() != '' && $('#volo_ma_r').val() != '')
			{
				var strDate = $('#volo_ma_r').val().substr(4,6) + '/' + $('#volo_g_r').val() + '/' + $('#volo_ma_r').val().substr(0, 4);
				$('#hotel_h_data_a').changeDatepicker({maxDate: new Date(strDate)});
				$('#hotel_h_data_p').changeDatepicker({maxDate: new Date(strDate)});
				$('#hotel_h_data_p').setDatepickerDate(new Date(strDate));
				var newSelectedDate = $('#hotel_h_data_p').getDatepickerDate();
				if (newSelectedDate.getDate().toString().length == 1)
					var day = '0' + newSelectedDate.getDate();
				else
					var day = newSelectedDate.getDate();
				$('#hotel_g_p').val(day);
				var actualMonth = parseInt(newSelectedDate.getMonth()) + 1;
				if (actualMonth.toString().length == 1)
					var month = '0' + actualMonth;
				else
					var month = actualMonth;
				$('#hotel_ma_p').val(month + '/' + newSelectedDate.getFullYear());
				$('#hotel_m_p').val(month);
				$('#hotel_a_p').val(newSelectedDate.getFullYear())
				var par = $('#hotel_h_data_p').getDatepickerDate();
				var and = $('#hotel_h_data_a').getDatepickerDate();
				var maxDay = parseInt((Math.max(par, and) - Math.min(par, and)) / 86400000);									
				$('#hotel_notti').val(maxDay);			
			}
			//$('#chooseHotel').show();
			//$('#chk_hotel_parz').attr("value", "Y");
				
				/*var newSelectedDate = $('#volo_h_data_r').getDatepickerDate();
				$('#hotel_h_data_p').changeDatepicker({maxDate: newSelectedDate});
				$('#hotel_h_data_p').setDatepickerDate(newSelectedDate);				
				if (newSelectedDate.getDate().toString().length == 1)
					var day = '0' + newSelectedDate.getDate();
				else
					var day = newSelectedDate.getDate();
				$('#hotel_g_p').val(day);
				var actualMonth = parseInt(newSelectedDate.getMonth()) + 1;
				if (actualMonth.toString().length == 1)
					var month = '0' + actualMonth;
				else
					var month = actualMonth;
				$('#hotel_ma_p').val(month + '/' + newSelectedDate.getFullYear());											
				var par = $('#hotel_h_data_p').getDatepickerDate();
				var and = $('#hotel_h_data_a').getDatepickerDate();
				var maxDay = parseInt((Math.max(par, and) - Math.min(par, and)) / 86400000);													
				$('#hotel_notti').val(maxDay);		*/		
			//}						
		}
	};
	
	function updateLinkedNotti(date) {
		var _temp_id = this.id.split('_');
		var _anno = + date.substring(6, 10);
		var _mese = date.substring(3, 5);
		var _giorno = date.substring(0, 2);
		var sel = 'a';
		jQuery('#' + _temp_id[0] + '_ma_' + _temp_id[3]).val(_mese + '/' + _anno).change(); 
		jQuery('#' + _temp_id[0] + '_g_' + _temp_id[3]).val(_giorno);
		if (this.id == 'hotel_h_data_a') {
			$('#hotel_m_a').val(_mese);
			$('#hotel_a_a').val(_anno)
		}
		if (this.id == 'hotel_h_data_p') {
			$('#hotel_m_p').val(_mese);
			$('#hotel_a_p').val(_anno)
		}
		_mese = + _mese;
		if (_temp_id[3] == 'a') {
			sel = 'p';
		}
		else if (_temp_id[3] == 'p') {
			sel = 'a';
		}
		var _$_sel = $('#hotel_h_data_' + sel);
		if ( (_$_sel.val() != '' && _$_sel.val() != undefined) && (this.value != '' && this.value != undefined) ) {
			var _mese_p = $('#hotel_ma_' + sel).val().substr(4, 6);
			var _anno_p = $('#hotel_ma_' + sel).val().substr(0, 4);
			var _giorno_p = $('#hotel_g_' + sel).val();
			var temp = Date.parse(_mese + '/' + _giorno + '/' + _anno);
			var temp2 = Date.parse(_mese_p + '/' + _giorno_p + '/' + _anno_p);
			$('#hotel_notti').val(parseInt((Math.max(temp2, temp) - Math.min(temp2, temp)) / 86400000));
		} 
		//else if (this.value == '' || _$_sel.val() == '') {
		//	$('#hotel_notti').val('');
		//}
		
		//Aggiornamento il secondo DTPicker
		if (this.id == 'hotel_h_data_a') {
			if (($('#hotel_h_data_p').getDatepickerDate() == null) && $('#hotel_notti').attr('value') == '')
			{
				var newDate = $('#hotel_h_data_a').getDatepickerDate();
				newDate.setDate(newDate.getDate() + 1);
				$('#hotel_h_data_p').changeDatepicker({minDate: newDate});				
			}
			if (($('#hotel_h_data_p').getDatepickerDate() == null) && $('#hotel_notti').attr('value') != '')
			{
				var newDate = $('#hotel_h_data_a').getDatepickerDate();
				newDate.setDate(newDate.getDate() + 1);
				$('#hotel_h_data_p').changeDatepicker({minDate: newDate});
				var newSelectedDate = $('#hotel_h_data_a').getDatepickerDate();
				newSelectedDate.setDate(newSelectedDate.getDate() + parseInt($('#hotel_notti').attr('value')));
				$('#hotel_h_data_p').setDatepickerDate(newSelectedDate);
				if (newSelectedDate.getDate().toString().length == 1)
					var day = '0' + newSelectedDate.getDate();
				else
					var day = newSelectedDate.getDate();
				$('#hotel_g_p').val(day);
				var actualMonth = parseInt(newSelectedDate.getMonth()) + 1;
				if (actualMonth.toString().length == 1)
					var month = '0' + actualMonth;
				else
					var month = actualMonth;
				$('#hotel_ma_p').val(month + '/' + newSelectedDate.getFullYear());
			}
			if (($('#hotel_h_data_p').getDatepickerDate() != null) && $('#hotel_notti').attr('value') != '')
			{
				if ($('#hotel_h_data_a').getDatepickerDate() >= $('#hotel_h_data_p').getDatepickerDate())
				{
					var newDate = $('#hotel_h_data_a').getDatepickerDate();
					newDate.setDate(newDate.getDate() + 1);
					$('#hotel_h_data_p').changeDatepicker({minDate: newDate});
					var newSelectedDate = $('#hotel_h_data_a').getDatepickerDate();
					newSelectedDate.setDate(newSelectedDate.getDate() + parseInt($('#hotel_notti').attr('value')));
					$('#hotel_h_data_p').setDatepickerDate(newSelectedDate);
					if (newSelectedDate.getDate().toString().length == 1)
						var day = '0' + newSelectedDate.getDate();
					else
						var day = newSelectedDate.getDate();
					$('#hotel_g_p').val(day);
					var actualMonth = parseInt(newSelectedDate.getMonth()) + 1;
					if (actualMonth.toString().length == 1)
						var month = '0' + actualMonth;
					else
						var month = actualMonth;
					$('#hotel_ma_p').val(month + '/' + newSelectedDate.getFullYear());										
				}					
			}
		}		
	};
	
	try {
		$('#hotel_h_data_a').attachDatepicker({
			minDate: dopodomani,
			maxDate: fraUnAnno,
			setDate: dopodomani,
			buttonImage: '/ResEBook/img/sf/calendario.gif',
			beforeShow: readLinked,
			onSelect: updateLinkedNotti
		});
		$('#hotel_h_data_p').attachDatepicker({
			minDate: dopodomani,
			maxDate: fraUnAnno,
			setDate: dopodomani,
			buttonImage: '/ResEBook/img/sf/calendario.gif',
			beforeShow: readLinked,
			onSelect: updateLinkedNotti
		});
		$('#volo_h_data_a').attachDatepicker({
			minDate: dopodomani,
			maxDate: fraUnAnno,
			setDate: dopodomani,
			buttonImage: '/ResEBook/img/sf/calendario.gif',
			beforeShow: readLinked,
			onSelect: updateLinked
		});

		var minDateR;
		if ($('#volo_h_data_r').val() == '')
			minDateR = dopodomani;
		else {
			var _mese = $('#volo_ma_a').val().substr(4, 6);
			var _anno = $('#volo_ma_a').val().substr(0, 4);
			var _giorno = $('#volo_g_a').val();
			minDateR = new Date(_anno, _mese, _giorno);
			minDateR.setDate(minDateR.getDate()+1);
		}

		$('#volo_h_data_r').attachDatepicker({
			minDate: minDateR,
			maxDate: fraUnAnno,
			setDate: dopodomani,
			buttonImage: '/ResEBook/img/sf/calendario.gif',
			beforeShow: readLinked,
			onSelect: updateLinked
		});
		//	fine sezione datepicker
	}
	catch(e) {
		// non fare nulla
	}

	jQuery("select[name^='apta']").each(
		function()
		{
			chiamaWs(this.value);
		}
	)

	var $this = $('#eng_hotel');

	$('#hotel_sel_ricerca').click(function(){
		$('#hotel_ricerca_avanzata').toggle();
		if ($(this).text() == 'Ricerca base')
			//$(this).html('<u>Ricerca avanzata</u>');
			$(this).html('&nbsp;');
		else
			$(this).html('<u>Ricerca base</u>');
		return false;
	});

	$('#hotel_numero_camere').change(function(){
		var n = + $(this).val();
		$('div:gt(' + (n - 1) + ')', '.hotel_camera').each(function(){
			var $this = $(this);
			$('select:first', $this).val('0');
			$('select:last', $this).val('0');
			$this.hide();
		});
		$('div:lt(' + n + ')', '.hotel_camera').show();
//		$('div[id^=hotel_ebc_]:gt(' + (n - 1) + ')').hide();
		$('div[id^=hotel_ebc_]:gt(' + (n - 1) + ')').each(function(){
			var $this = $(this);
			$this.hide();
			$('select', $this).val('0');
		});
	});
	
	$('#hotel_camera_1_bambini, #hotel_camera_2_bambini, #hotel_camera_3_bambini').change(function(){
		var $this = $(this);
		var n = + $this.val();
		var camera = $this.attr('id').split('_')[2];
		var $div = $('#hotel_ebc_' + camera)
		if (n > 0) $div.show();
			else $div.hide();
		$('div:gt(' + n + ')', $div).hide();
		$('div:lt(' + (n + 1) + ')', $div).show();
	});
	
	$('#chk_hotel_parz').click(function(){
		if (this.checked)
		{
			var andata = $('#volo_ma_a').val() + $('#volo_g_a').val();
			var ritorno = $('#volo_ma_r').val() + $('#volo_g_r').val();
			if (andata != '' && ritorno != ''){
				$('#chooseHotel').show();
				$('#chk_hotel_parz').attr("value", "Y");			
			} else {
				alert("La scelta di un Hotel per parte del viaggio è possibile dopo avere selezionato le date del volo");
				$('#chk_hotel_parz').attr("checked", "");
			}
		}
		else
		{
			$('#chooseHotel').hide();
			$('#chk_hotel_parz').attr("value", "N");
		}
	});

	//Gestione press numero notti
	$('#hotel_notti').keyup(function(e){
		if (e.which == 8 || e.which == 46)
		{
			var Range = window.document.selection.createRange();			
			var SelText = Range.text;
			var len = parseInt(SelText.length);
			
			if (len == $('#hotel_notti').val().length)
				var write = '';
			else if (len == 0)
			{
				var write = $('#hotel_notti').val();				
			}				
			else
			{
				var pos = parseInt($('#hotel_notti').val().indexOf(SelText));
				var write = $('#hotel_notti').val().substring(0, pos) + $('#hotel_notti').val().substring(pos + len, $('#hotel_notti').val().length);
			}
			if (write == '' || write == '0')
			{
				if ($('#hotel_h_data_a').getDatepickerDate() == null) {
		  			var strDate = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
		  			$('#hotel_h_data_a').setDatepickerDate(new Date(strDate));
				}

				var newDate = $('#hotel_h_data_a').getDatepickerDate();
				newDate.setDate(newDate.getDate() + 1);
				$('#hotel_h_data_p').changeDatepicker({minDate: newDate});
				$('#hotel_g_p').val('gg');
				$('#hotel_ma_p').val('mm/aaaa');
			}
			else
			{
				if ($('#hotel_h_data_a').getDatepickerDate() == null) {
		  			var strDate = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
		  			$('#hotel_h_data_a').setDatepickerDate(new Date(strDate));
				}

				var newDate = $('#hotel_h_data_a').getDatepickerDate();
				newDate.setDate(newDate.getDate() + 1);
				$('#hotel_h_data_p').changeDatepicker({minDate: newDate});
				var newSelectedDate = $('#hotel_h_data_a').getDatepickerDate();
				
				if (($('#volo_g_r').val() != '' && $('#volo_ma_r').val() != '') && ($('#volo_g_a').val() != '' && $('#volo_ma_a').val() != ''))
				{
					if ($('#volo_h_data_a').getDatepickerDate() == null)
					{
						var dt = $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(0, 4);
						$('#volo_h_data_a').setDatepickerDate(new Date(dt));
					}				
					var rit = $('#volo_h_data_r').getDatepickerDate();
					var and = $('#volo_h_data_a').getDatepickerDate();
					var maxDate = parseInt((Math.max(rit, and) - Math.min(rit, and)) / 86400000);
					if (parseInt(write) > parseInt(maxDate))
						write = maxDate;
				}
				newSelectedDate.setDate(newSelectedDate.getDate() + parseInt(write));
				$('#hotel_h_data_p').setDatepickerDate(newSelectedDate);
				if (newSelectedDate.getDate().toString().length == 1)
					var day = '0' + newSelectedDate.getDate();
				else
					var day = newSelectedDate.getDate();
				$('#hotel_g_p').val(day);
				var actualMonth = parseInt(newSelectedDate.getMonth()) + 1;
				if (actualMonth.toString().length == 1)
					var month = '0' + actualMonth;
				else
					var month = actualMonth;
				$('#hotel_ma_p').val(month + '/' + newSelectedDate.getFullYear());				
			}
		}
		else if ((e.which >= 48 && e.which <= 57) || (e.which >= 97 && e.which <= 105))
		{
		  	if ($('#hotel_g_a').val() != 'gg' && $('#hotel_ma_a').val() != 'mm/aaaa')
		  	{
		  		var strDate = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
		  		$('#hotel_h_data_a').setDatepickerDate(new Date(strDate));
				var newDate = $('#hotel_h_data_a').getDatepickerDate();
				newDate.setDate(newDate.getDate() + 1);
				$('#hotel_h_data_p').changeDatepicker({minDate: newDate});
				var newSelectedDate = $('#hotel_h_data_a').getDatepickerDate();
				if (($('#volo_g_r').val() != '' && $('#volo_ma_r').val() != '') && ($('#volo_g_a').val() != '' && $('#volo_ma_a').val() != ''))
				{
					if ($('#volo_h_data_a').getDatepickerDate() == null)
					{
						var dt = $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(0, 4);
						$('#volo_h_data_a').setDatepickerDate(new Date(dt));
					}
					var rit = $('#volo_h_data_r').getDatepickerDate();
					var and = $('#volo_h_data_a').getDatepickerDate();
					var maxDate = parseInt((Math.max(rit, and) - Math.min(rit, and)) / 86400000);
					if (parseInt($('#hotel_notti').val()) > parseInt(maxDate))
						$('#hotel_notti').val(maxDate);
				}				
				newSelectedDate.setDate(newSelectedDate.getDate() + parseInt($('#hotel_notti').val()));
				$('#hotel_h_data_p').setDatepickerDate(newSelectedDate);
				if (newSelectedDate.getDate().toString().length == 1)
					var day = '0' + newSelectedDate.getDate();
				else
					var day = newSelectedDate.getDate();
				$('#hotel_g_p').val(day);
				var actualMonth = parseInt(newSelectedDate.getMonth()) + 1;
				if (actualMonth.toString().length == 1)
					var month = '0' + actualMonth;
				else
					var month = actualMonth;
				$('#hotel_ma_p').val(month + '/' + newSelectedDate.getFullYear());
				$('#hotel_m_p').val(month);
				$('#hotel_a_p').val(newSelectedDate.getFullYear());
		  	}
		}
	});
	
	$('#hotel_notti').keydown(function(e){
		if (!(e.which >= 48 && e.which <= 57) && !(e.which >= 97 && e.which <= 105) && (e.which != 8) && (e.which != 46) && (e.which != 37) && (e.which != 39) && (e.which != 16))
		{
		  	var keychar = String.fromCharCode(e.which);
		  	var numcheck = /\d/;
		  	return numcheck.test(keychar);
		}				
	});
	
	$('#hotel_g_p').change(function(){
		if ($('#hotel_g_p').val() == '') {
			$('#hotel_notti').val('');
		}	
		else if (($('#hotel_ma_p').val() != '') && ($('#hotel_g_a').val() != '') && ($('#hotel_ma_a').val() != ''))
		{
			var strDateA = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
			var strDateP = $('#hotel_ma_p').val().substr(4,6) + '/' + $('#hotel_g_p').val() + '/' + $('#hotel_ma_p').val().substr(0, 4);
			var dataA = new Date(strDateA);
			var dataP = new Date(strDateP);
			$('#hotel_notti').val(parseInt((Math.max(dataP, dataA) - Math.min(dataP, dataA)) / 86400000));
		}
	});
	
	$('#hotel_g_a').change(function(){
		if ($('#hotel_g_a').val() == '') {
			$('#hotel_notti').val('');
		}		
		else if (($('#hotel_ma_p').val() != '') && ($('#hotel_g_p').val() != '') && ($('#hotel_ma_a').val() != ''))
		{
			var strDateA = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
			var strDateP = $('#hotel_ma_p').val().substr(4,6) + '/' + $('#hotel_g_p').val() + '/' + $('#hotel_ma_p').val().substr(0, 4);
			var dataA = new Date(strDateA);
			var dataP = new Date(strDateP);
			$('#hotel_notti').val(parseInt((Math.max(dataP, dataA) - Math.min(dataP, dataA)) / 86400000));
		}
	});
	
	$('#hotel_ma_p').change(function(){
		if ($('#hotel_ma_p').val() == '') {
			$('#hotel_notti').val('');
		}		
		else if (($('#hotel_g_p').val() != '') && ($('#hotel_g_a').val() != '') && ($('#hotel_ma_a').val() != ''))
		{
			var strDateA = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
			var strDateP = $('#hotel_ma_p').val().substr(4,6) + '/' + $('#hotel_g_p').val() + '/' + $('#hotel_ma_p').val().substr(0, 4);
			var dataA = new Date(strDateA);
			var dataP = new Date(strDateP);
			$('#hotel_notti').val(parseInt((Math.max(dataP, dataA) - Math.min(dataP, dataA)) / 86400000));
		}
	});
	
	$('#hotel_ma_a').change(function(){
		if ($('#hotel_ma_a').val() == '') {
			$('#hotel_notti').val('');
		}		
		else if (($('#hotel_g_p').val() != '') && ($('#hotel_g_a').val() != '') && ($('#hotel_ma_p').val() != ''))
		{
			var strDateA = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
			var strDateP = $('#hotel_ma_p').val().substr(4,6) + '/' + $('#hotel_g_p').val() + '/' + $('#hotel_ma_p').val().substr(0, 4);
			var dataA = new Date(strDateA);
			var dataP = new Date(strDateP);
			$('#hotel_notti').val(parseInt((Math.max(dataP, dataA) - Math.min(dataP, dataA)) / 86400000));
		}
	});
		
	$('#volo_g_a').change(function(){
		if (($('#volo_g_a').val() != '') && ($('#volo_ma_a').val() != ''))
		{
			var strDateA = $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(0, 4);
			var dataA = new Date(strDateA);
			$('#hotel_h_data_a').setDatepickerDate(dataA);
			dataA.setDate(dataA.getDate());
			$('#volo_h_data_r').changeDatepicker({minDate: dataA});
			$('#hotel_h_data_p').changeDatepicker({minDate: dataA});

			if (dataA.getDate().toString().length == 1)
				var day = '0' + dataA.getDate();
			else
				var day = dataA.getDate();
			$('#hotel_g_a').val(day);
			var actualMonth = parseInt(dataA.getMonth()) + 1;
			if (actualMonth.toString().length == 1)
				var month = '0' + actualMonth;
			else
				var month = actualMonth;
			$('#hotel_ma_a').val(month + '/' + dataA.getFullYear());			
			dataA.setDate(dataA.getDate());
			$('#hotel_h_data_a').changeDatepicker({minDate: dataA});
			//calcolo numNotti
			var strDatehA = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
			var strDatehP = $('#hotel_ma_p').val().substr(4,6) + '/' + $('#hotel_g_p').val() + '/' + $('#hotel_ma_p').val().substr(0, 4);
			var datahA = new Date(strDatehA);
			var datahP = new Date(strDatehP);
			$('#hotel_notti').val(parseInt((Math.max(datahP, datahA) - Math.min(datahP, datahA)) / 86400000));	

		}
	});

	$('#volo_ma_a').change(function(){
		if (($('#volo_g_a').val() != '') && ($('#volo_ma_a').val() != ''))
		{
			var strDateA = $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(0, 4);
			var strDate_a = strDateA.substr(3,2) + '/'+ strDateA.substr(0,2) + '/' + strDateA.substr(6);
			var str_m_Date_a = strDateA.substr(0,2); 
			var str_a_Date_a = strDateA.substr(6);


			var dataA = new Date(strDateA);
			$('#hotel_h_data_a').setDatepickerDate(dataA);
			dataA.setDate(dataA.getDate());
			$('#volo_h_data_r').changeDatepicker({minDate: dataA});
			$('#hotel_h_data_p').changeDatepicker({minDate: dataA});

			$('#volo_h_data_a').val(strDate_a);
			$('#volo_m_a').val(str_m_Date_a);
			$('#volo_a_a').val(str_a_Date_a);

		
			if (dataA.getDate().toString().length == 1)
				var day = '0' + dataA.getDate();
			else
				var day = dataA.getDate();
				
			$('#hotel_g_a').val(day);
			var actualMonth = parseInt(dataA.getMonth()) + 1;
			if (actualMonth.toString().length == 1)
				var month = '0' + actualMonth;
			else
				var month = actualMonth;
			$('#hotel_ma_a').val(month + '/' + dataA.getFullYear());			
			dataA.setDate(dataA.getDate());
			$('#hotel_h_data_a').changeDatepicker({minDate: dataA});
			$('#hotel_a_a').val(dataA.getFullYear());
			$('#hotel_m_a').val(month);
			//calcolo numNotti
			var strDateA = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
			var strDateP = $('#hotel_ma_p').val().substr(4,6) + '/' + $('#hotel_g_p').val() + '/' + $('#hotel_ma_p').val().substr(0, 4);
			var dataA = new Date(strDateA);
			var dataP = new Date(strDateP);
			$('#hotel_notti').val(parseInt((Math.max(dataP, dataA) - Math.min(dataP, dataA)) / 86400000));	
		}
	});

	$('#volo_g_r').change(function(){
		if (($('#volo_g_r').val() != '') && ($('#volo_ma_r').val() != ''))
		{
			var strDateR = $('#volo_ma_r').val().substr(4,6) + '/' + $('#volo_g_r').val() + '/' + $('#volo_ma_r').val().substr(0, 4);
			var dataR = new Date(strDateR);
			
			var strDate_r = strDateR.substr(3,2) + '/'+ strDateR.substr(0,2) + '/' + strDateR.substr(6);
			var str_m_Date_r = strDateR.substr(0,2); 
			var str_a_Date_r = strDateR.substr(6);

			$('#volo_h_data_r').val(strDate_r);
			$('#volo_m_r').val(str_m_Date_r);
			$('#volo_a_r').val(str_a_Date_r);
			
			
			$('#hotel_h_data_a').setDatepickerDate(dataR);
			if (dataR.getDate().toString().length == 1)
				var day = '0' + dataR.getDate();
			else
				var day = dataR.getDate();
			$('#hotel_g_p').val(day);
			var actualMonth = parseInt(dataR.getMonth()) + 1;
			if (actualMonth.toString().length == 1)
				var month = '0' + actualMonth;
			else
				var month = actualMonth;
			$('#hotel_ma_p').val(month + '/' + dataR.getFullYear());			
			dataR.setDate(dataR.getDate() + 1);
			$('#hotel_h_data_p').changeDatepicker({minDate: dataR});
			$('#hotel_a_p').val(dataR.getFullYear());
			$('#hotel_m_p').val(month);
			//calcolo numNotti
			var strDateA = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
			var strDateP = $('#hotel_ma_p').val().substr(4,6) + '/' + $('#hotel_g_p').val() + '/' + $('#hotel_ma_p').val().substr(0, 4);
			var dataA = new Date(strDateA);
			var dataP = new Date(strDateP);
			$('#hotel_notti').val(parseInt((Math.max(dataP, dataA) - Math.min(dataP, dataA)) / 86400000));	
		}
	});

	$('#volo_ma_r').change(function(){
		if (($('#volo_g_r').val() != '') && ($('#volo_ma_r').val() != ''))
		{
			var strDateR = $('#volo_ma_r').val().substr(4,6) + '/' + $('#volo_g_r').val() + '/' + $('#volo_ma_r').val().substr(0, 4);
			var strDate_r = strDateR.substr(3,2) + '/'+ strDateR.substr(0,2) + '/' + strDateR.substr(6);
			var str_m_Date_r = strDateR.substr(0,2); 
			var str_a_Date_r = strDateR.substr(6);

			var dataR = new Date(strDateR);
			$('#hotel_h_data_a').setDatepickerDate(dataR);
			$('#hotel_h_data_a').changeDatepicker({maxDate: new Date(strDateR)});

			$('#volo_h_data_r').val(strDate_r);
			$('#volo_m_r').val(str_m_Date_r);
			$('#volo_a_r').val(str_a_Date_r);
			
			if (dataR.getDate().toString().length == 1)
				var day = '0' + dataR.getDate();
			else
				var day = dataR.getDate();
				
			$('#hotel_g_p').val(day);
			$('#h_g_p').val(day); 
			var actualMonth = parseInt(dataR.getMonth()) + 1;
			if (actualMonth.toString().length == 1)
				var month = '0' + actualMonth;
			else
				var month = actualMonth;
			$('#hotel_ma_p').val(month + '/' + dataR.getFullYear());
			dataR.setDate(dataR.getDate() + 1);
			$('#hotel_h_data_p').changeDatepicker({minDate: dataR});
			$('#hotel_a_p').val(dataR.getFullYear());
			$('#hotel_m_p').val(month);

			//calcolo numNotti
			var strDateA = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
			var strDateP = $('#hotel_ma_p').val().substr(4,6) + '/' + $('#hotel_g_p').val() + '/' + $('#hotel_ma_p').val().substr(0, 4);
			var dataA = new Date(strDateA);
			var dataP = new Date(strDateP);
			$('#hotel_notti').val(parseInt((Math.max(dataP, dataA) - Math.min(dataP, dataA)) / 86400000));	

			$('#hotel_h_data_p').changeDatepicker({minDate: dataA});
			$('#hotel_h_data_p').changeDatepicker({maxDate: dataP});
		}
	});

	function isDate(dateStr) {
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = dateStr.match(datePat);
		if (matchArray == null)
			return false;
	
		month = matchArray[1];
		day = matchArray[3];
		year = matchArray[5];
	
		if (month < 1 || month > 12)
			return false;
	
		if (day < 1 || day > 31)
			return false;
	
		if ((month==4 || month==6 || month==9 || month==11) && day==31)
			return false;
	
		if (month == 2) {
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day > 29 || (day==29 && !isleap))
				return false;
		}
		
		return true;
	};

	
	$('#form_volohotel').submit(function(){
		$('#form_check').remove();
		var messaggio = '<p>Errori nella compilazione del form:</p>'
		var g_a = + $('#volo_g_a').val();
		var ma_a = + $('#volo_ma_a').val();
		var g_r = + $('#volo_g_r').val();
		var ma_r = + $('#volo_ma_r').val();
		var strDateA = $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(0, 4);
		var strDateP = $('#volo_ma_r').val().substr(4,6) + '/' + $('#volo_g_r').val() + '/' + $('#volo_ma_r').val().substr(0, 4);
		
		var dataA = new Date(strDateA);
		var dataP = new Date(strDateP);
		var today = new Date();
		var today_mmggaa = (today.getMonth()+1) + '/' +  today.getDate() + '/' +  today.getFullYear(); 
		
		var w_dopodomani = new Date(today_mmggaa);
        w_dopodomani.setDate(w_dopodomani.getDate() + 2);

	    var w_dopodomani_ggmmaa = w_dopodomani.getDate() + '/' +  (w_dopodomani.getMonth()+1)  + '/' +  w_dopodomani.getFullYear(); 

        
		if (!isDate(strDateA))
			messaggio += '</BR> - Data del volo di partenza errata.';
		if (!isDate(strDateP))
			messaggio += '</BR> - Data del volo di ritorno errata.';	
				
//		if (dataA < today)
//			messaggio += 'Data del volo di andata antecedente a quella odierna';
//		if (dataP < today)
//			messaggio += 'Data del volo di ritorno antecedente a quella odierna';
		
		if (dataA < w_dopodomani)
			messaggio += '</BR> - &quot;ATTENZIONE: puoi prenotare un volo di linea + hotel fino a 2 giorni lavorativi prima della partenza&quot;.';
		if (dataP < dataA)
			messaggio += '</BR> - Data del volo di ritorno antecedente a quella del volo di partenza.';


		//Controllo HOTEL	
		if ($('#chk_hotel_parz').attr('value') == 'Y')
		{

			var g_a = + $('#volo_g_a').val();
			var ma_a = + $('#volo_ma_a').val();
			var g_r = + $('#volo_g_r').val();
			var ma_r = + $('#volo_ma_r').val();
			var strDateA = $('#volo_ma_a').val().substr(4,6) + '/' + $('#volo_g_a').val() + '/' + $('#volo_ma_a').val().substr(0, 4);
			var strDateR = $('#volo_ma_r').val().substr(4,6) + '/' + $('#volo_g_r').val() + '/' + $('#volo_ma_r').val().substr(0, 4);
			
			//data volo andata 
			var dataVA = new Date(strDateA);
			//data volo ritorno 
			var dataVR = new Date(strDateR);

			var strDateHA = $('#hotel_ma_a').val().substr(4,6) + '/' + $('#hotel_g_a').val() + '/' + $('#hotel_ma_a').val().substr(0, 4);
			var strDateHP = $('#hotel_ma_p').val().substr(4,6) + '/' + $('#hotel_g_p').val() + '/' + $('#hotel_ma_p').val().substr(0, 4);
			var dataHA = new Date(strDateHA);
			var dataHP = new Date(strDateHP);	
			
			var today = new Date();
			var today_mmggaa = (today.getMonth()+1) + '/' +  today.getDate() + '/' +  today.getFullYear(); 
			var today_ggmmaa = (today.getDate() + '/' +  today.getMonth()+1)  + '/' +  today.getFullYear(); 

			
			var w_dopodomani = new Date(today_mmggaa);
	        w_dopodomani.setDate(w_dopodomani.getDate() + 2);
	        
	        var w_dopodomani_ggmmaa = w_dopodomani.getDate() + '/' +  (w_dopodomani.getMonth()+1)  + '/' +  w_dopodomani.getFullYear(); 

			if (!isDate(strDateHA))
				messaggio += '</BR> - Data hotel arrivo errata.';
			if (!isDate(strDateHP))
				messaggio += '</BR> - Data hotel partenza errata.';	
					
//			if (dataHA < today)
//				messaggio += 'Data hotel arrivo antecedente a quella odierna';
//			if (dataHP < today)
//				messaggio += 'Data hotel partenza antecedente a quella odierna';					

			if (dataHA < w_dopodomani)
				messaggio += '</BR> - &quot;ATTENZIONE: puoi prenotare un volo di linea + hotel fino a 2 giorni lavorativi prima della partenza&quot;.';
			if (dataHP < dataHA)
				messaggio += '</BR> - Data partenza hotel  antecedente a quella di arrivo in hotel.';
				
			if (dataHA < dataVA)
				messaggio += '</BR> - Data arrivo hotel antecedente a quella del volo di partenza.';

			if (dataHP > dataVR)
				messaggio += '</BR> - Data partenza hotel successiva a quella del volo di ritorno.';				
				

		}
		
		if ($('#hotel_numero_camere').val() > 1)
		{
			if ($('#hotel_numero_camere').val() == 2 && $('#hotel_camera_2_adulti').val() == 0)
				messaggio += '</BR> - Inserire almeno 1 adulto per la camera 2.';
			if ($('#hotel_numero_camere').val() == 3 && ($('#hotel_camera_2_adulti').val() == 0 || $('#hotel_camera_3_adulti').val() == 0))	
				messaggio += '</BR> - Inserire almeno 1 adulto per la camera 2 o 3.';
		}

//		if (ma_a == ma_r && ma_a != '0' && ma_r != '0') {
//			if (g_r <= g_a && g_r != '0' && g_a != '0') messaggio += '<p></BR> - La data di partenza deve essere successiva a quella di ritorno del Volo</p>';
//		} else if (ma_r < ma_a && g_r != '0' && g_a != '0') messaggio += '<p></BR> - La data di partenza deve essere successiva a quella di ritorno del Volo</p>';
		
		
		if (messaggio == '<p>Errori nella compilazione del form:</p>') {
			messaggio = '<p>Form incompleto:</p>';
			if ($('#keydP').val() == '') messaggio += '</BR> - Devi selezionare l&rsquo;aeroporto di partenza.';
			if ($('#keyd').val() == '') messaggio += '</BR> - Devi selezionare l&rsquo;aeroporto di destinazione.';
			if ($('#volo_g_a').val() == '' || $('#volo_ma_a').val() == '') messaggio += '</BR> - Devi scegliere la data di partenza del Volo.';
			if ($('#volo_g_r').val() == '' || $('#volo_ma_r').val() == '') messaggio += '</BR> - Devi scegliere la data di ritorno del Volo.';
		}

		/*if (ma_h_a == ma_h_r && ma_h_a != '0' && ma_h_r != '0') {
			if (g_h_r <= g_h_a && g_h_r != '0' && g_h_a != '0') messaggio += '<p>La data di partenza deve essere successiva a quella di arrivo in Hotel</p>';
		} else if (ma_h_r < ma_h_a && g_h_r != '0' && g_h_a != '0') messaggio += '<p>La data di partenza deve essere successiva a quella di arrivo in Hotel</p>';
		if (messaggio == '<p>Errori nella compilazione del form:</p>') {
			messaggio = '<p>Form incompleto:</p>';

			if ($('#hotel_g_a').val() == '' || $('#hotel_ma_a').val() == '') messaggio += '<p>Devi scegliere la data di arrivo in Hotel</p>';
			if ($('#hotel_g_p').val() == '' || $('#hotel_ma_p').val() == '') messaggio += '<p>Devi scegliere la data di partenza dall&rsquo;Hotel</p>';
		}*/

		if (messaggio == '<p>Form incompleto:</p>'){
		
		    var nDivTrovaLayer = $("#trova_layer").length;
		    
		    if(nDivTrovaLayer > 0){
		    	HideDIV('alpitourPAGEcontent'); DisplayDIV('trova_layer');
		    }else{
			    viewPaginaAttesa(); 
			}   
			return true;
		}
		else {
			$('<div id="form_check" class="formBox"></div>')
			.html(messaggio)
			.insertAfter('#form_volohotel');
			return false;
		}
	});

		// Visualizzo Pagina di Attesa dopo aver premuto il tasto TROVA 
	function viewPaginaAttesa() {
	
	    var coordinate = jQuery('#searchBox').offset();
	    
	    if (coordinate.top>900) {
	    	coordinate.top=181;
	    }
	    
        if ($('#divContentDX').attr('id') !=undefined){
	        jQuery($('#divContentDX')).attr('style','display: none;');
	    }     

	    if ($('#footer').attr('moveFooter') != undefined){
	         var obj = $('#divContDXSearch');
	         var offset = obj.offset();
	         var h = obj.outerHeight();
	         jQuery($('#footer')).attr('style','position: absolute; top:'+ (220 + h) +'px;');    
	    }

        var style_params = null;
        var other_params = null;

		var styleBar = {
			'position': 'absolute',
			'top': coordinate.top + 'px',
			'left': coordinate.left + 300 + 'px',
			'border': '0',
			'padding': '10px',
			'width': '611px',
			'height': '10px',
			'cursor': 'pointer',
			'background-position': 'center',
			'background-image': 'url(/ResEBook/img/pb/progress_bar.gif)',
			'background-repeat': 'no-repeat'
		}
		
		var configBar = {
			'closeOnClick': false,
			'id': '#progressBarSOHO'
		}
		
		jQuery.extend(styleBar, style_params);
		jQuery.extend(configBar, other_params);
		jQuery('#' + configBar.id).remove();
		
		jQuery('<div id="' + configBar.id + '"></div>')
			.css(styleBar)
			.appendTo('body')
			.click(function(){
				if (configBar.closeOnClick) {
					jQuery(this).remove();
				}
			});
 
        document.body.scrollTop=0;
		return;
	}  

});