var formHotelVal = "";
var keydVal =  "";
var newDataAndata = '';

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;
};

function loadXMLDest(xmlData,tipoVg) {

	var messaggi	= [];
	
	if (formHotelVal == '' && keydVal == '') {
		// per SearchFormCHHO.jsp
		$('#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('partenza','','1',tipoVg);
			setComboPar('form_charter_volo');				
		});
		$('R000001', data).each(function(i){
			xmlDoc=this;
			// funzione per inserimento livello T = 1
			//loadLev(this);
			
			// DA modificare con allineamento WS MARY DESTINATION BOXES AEROPORTI ITALIANI
			if (tipoVg == 'OWR'){
				//var node=xmlDoc.getElementsByTagName("P001114")[0];
				var node=xmlDoc.getElementsByTagName("P000001")[0];
				if (document.forms['form_charter_volo'].keydP.value != null && document.forms['form_charter_volo'].keydP.value != "")
					nameNode = "P" + document.forms['form_charter_volo'].keydP.value.substring(7);
				else
					nameNode = "P";
			} else {
				//var node=xmlDoc.getElementsByTagName("P000384")[0];
				var node=xmlDoc.getElementsByTagName("P000276")[0];
				nameNode = node.nodeName;
			}
			loadAllDest('destin',node.nodeName,'1');
			setComboAllArr('form_charter_volo');	
			if (nameNode != "P")
				setHiddenKeydArr('destin','form_charter_volo');						
		
		});				
	}	//	fine if (messaggi.code && messaggi.text)
	
		//	se si ricevono messaggi diversi da "0" e "Completed." dalla servlet
	else alert("Errore nella ricezione dei dati.");
}

function popUpHelp(e,obj){

	var x;
	var y;
	var ev = (!e)?window.event:e;//IE:Moz
	if (ev.pageX){ //Moz
		//alert("MOZ " + ev.pageX);
		x = ev.pageX;
		y = ev.pageY;
	} else if (ev.clientX){ //IE
		//alert("IE " + ev.clientX);
		x = ev.clientX;
		y = ev.clientY;
	}
	
	document.getElementById(obj).style.top = y - 70 + 'px';
	document.getElementById(obj).style.left = x - 100 + 'px';
    document.getElementById(obj).style.display = 'block';	
}

function removeHelp(obj){

    obj.style.display = 'none';	
}

$(document).ready(function(){

	// popAlpi HELP
	jQuery('.popAlpiSearch').popAlpiSearch();
	jQuery('#poaltoSearch').livequery(function(){
		jQuery(this).bgiframe();
	});	

	formHotelVal = document.forms['form_charter_volo'].formHotel.value;
	keydVal =  document.forms['form_charter_volo'].keyd.value;

	//	variabili utilizzate nel parsing dell'xml
	var messaggi	= [];
	
		var chiamaWs = function(tipoVg){

		var param= basePath + "/EBookingWeb/SEACHHO?tipoVg=" + tipoVg + "&tipoDati=js";

		var js = document.createElement('script');
	    js.setAttribute('type', 'text/javascript');
	    js.setAttribute('encoding', 'UTF-8');
	    js.setAttribute('src', param);
	    document.body.appendChild(js);	
		
	    /*$('<script type="text/javascript" src="' + param + '" encoding="UTF-8" />')
		.bgiframe()
		.appendTo('body');*/

		/*$.ajax({
			url: '/EBookingWeb/SEACHHO',
			type: 'post',
			data: 'tipoVg='+tipoVg ,
			dataType: 'xml',
			success: function(xmlData){
				if (formHotelVal == '' && keydVal == '') {
					// per SearchFormCHHO.jsp
					$('#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('partenza','','1',tipoVg);
						setComboPar('form_charter_volo');				
					});
					$('R000001', data).each(function(i){
						xmlDoc=this;
						// funzione per inserimento livello T = 1
						//loadLev(this);
						
						// DA modificare con allineamento WS MARY DESTINATION BOXES AEROPORTI ITALIANI
						if (tipoVg == 'OWR'){
							//var node=xmlDoc.getElementsByTagName("P001114")[0];
							var node=xmlDoc.getElementsByTagName("P000001")[0];
							if (document.forms['form_charter_volo'].keydP.value != null && document.forms['form_charter_volo'].keydP.value != "")
								nameNode = "P" + document.forms['form_charter_volo'].keydP.value.substring(7);
							else
								nameNode = "P";
						} else {
							//var node=xmlDoc.getElementsByTagName("P000384")[0];
							var node=xmlDoc.getElementsByTagName("P000276")[0];
							nameNode = node.nodeName;
						}
						loadAllDest('destin',node.nodeName,'1');
						setComboAllArr('form_charter_volo');	
						if (nameNode != "P")
							setHiddenKeydArr('destin','form_charter_volo');						
					
					});				
				}	//	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
		});*/
	};
	
	//	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 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 = $('#charter_m_a').val(); 
	var aaVoloA = $('#charter_a_a').val(); 
	
	var domani = new Date();
	domani.setDate(laData.getDate() + 1);

	var fraUnAnno = new Date();
	fraUnAnno.setDate(laData.getDate() + 365);
		
	var dopodomani = new Date();
	dopodomani.setDate(laData.getDate() + 2);

	var ggDomani = domani.getDate();
	var mmDomani = domani.getMonth()+1;
	var aaaaDomani = domani.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;	
	
	var soloAndata = '';
	var newDataRitorno = '';
	if ($('#dataInizioSoggiorno').val() != undefined)
	{
		newDataAndata = $('#dataInizioSoggiorno').val();
		var new_g_a = newDataAndata.substr(6,8);
		var new_m_a = newDataAndata.substr(4,2);
		var new_a_a = newDataAndata.substr(0,4);
		if (new_g_a != $('#h_g_a').val() && $('#h_g_a').val() != 'null')
			new_g_a = $('#h_g_a').val();
		if (new_m_a != $('#hotel_m_a').val() && $('#hotel_m_a').val() != 'null')
			new_m_a = parseInt($('#hotel_m_a').val());
		if (new_a_a != $('#hotel_a_a').val() && $('#hotel_a_a').val() != 'null')
			new_a_a = $('#hotel_a_a').val();		
		var new_strDataAndata = new_m_a + '/' + new_g_a + '/' + new_a_a;		
	}

	if ($('#dataRitorno').val() != undefined)
	{
		var dtI = new Date(newDataAndata.substr(0,4), newDataAndata.substr(4,2)-1, newDataAndata.substr(6,8)); 
		var ggDur = parseInt($('#durataSoggiorno').val());		
		dtI.setDate(dtI.getDate() + ggDur);

		//newDataRitorno = $('#dataRitorno').val();
		var meseSog = "";
		var giornoSog = "";
		if ((dtI.getMonth()+1).toString().length == 1)
			meseSog = '0' + (dtI.getMonth()+1).toString();
		else
			meseSog = (dtI.getMonth()+1).toString();
		if (dtI.getDate().toString().length == 1)
			giornoSog = '0' + dtI.getDate().toString();
		else
			giornoSog = dtI.getDate().toString();
		
		newDataRitorno = dtI.getFullYear().toString() + meseSog + giornoSog;
		
		var new_g_r = newDataRitorno.substr(6,8);
		var new_m_r = newDataRitorno.substr(4,2);
		var new_a_r = newDataRitorno.substr(0,4);
		
		if (new_g_r != $('#h_g_p').val() && $('#h_g_p').val() != 'null')
			new_g_r = $('#h_g_p').val();
		if (new_m_r != $('#hotel_m_p').val() && $('#hotel_m_p').val() != 'null')
			new_m_r = parseInt($('#hotel_m_p').val());
		if (new_a_r != $('#hotel_a_p').val() && $('#hotel_a_p').val() != 'null')
			new_a_r = $('#hotel_a_p').val();						
		var new_strDataRitorno = new_m_r + '/' + new_g_r + '/' + new_a_r;	


	}
	
	jQuery("input[name^='tipoVg']").each(
		function(){
			if (this.checked) 
				soloAndata = this.value;
		}
	);	
	
	//	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>';
		
		if(valday==ggVoloA)
			$(optVoloA).attr('selected','selected').appendTo('#charter_g_a');
		else if (newDataAndata != '' && new_g_a == day)
		{
			$(optVoloA).attr('selected','selected').appendTo('#charter_g_a');
			$('#v_g_a').val(day);
			
		}
		else if ((newDataAndata == '') && (ggDomaniStr==day) && (ggVoloA == 'null'))
		{
			$(optVoloA).attr('selected','selected').appendTo('#charter_g_a');
			$('#v_g_a').val(day);
		}
		else
			$(optA).appendTo('#charter_g_a');
			
		if(valday==ggA)	
			$(optA).attr('selected','selected').appendTo('#hotel_g_a');		
		else if (newDataAndata != '' && new_g_a == day)
		{
			$(optA).attr('selected','selected').appendTo('#hotel_g_a');
			$('#h_g_a').val(day); 
		}				
		else	
			$(optA).appendTo('#hotel_g_a');
			
		if(valday==ggP)
			$(optP).attr('selected','selected').appendTo('#hotel_g_p');
		else if (newDataRitorno != '' && new_g_r == day)
		{
			$(optVoloA).attr('selected','selected').appendTo('#hotel_g_p');
			$('#h_g_p').val(day);
		}			
		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 am = anno + '/' + mese;
		var strAm = am.replace('/','');
		var optA = '<option value="' + anno  + mese + '">' + ma + '</option>';
		var optP = '<option value="' + anno  + mese + '">' + ma + '</option>';
		var optVoloA = '<option value="' + anno  + mese + '">' + ma + '</option>';

		if (anno==aaVoloA && mese==mmVoloA)
			$(optVoloA).attr('selected','selected').appendTo('#charter_ma_a');
		else if (newDataAndata != '' && (new_m_a + '/' + new_a_a) == ma)
		{
			$(optVoloA).attr('selected','selected').appendTo('#charter_ma_a');
			$('#charter_m_a').val(mese); 
			$('#charter_a_a').val(anno);
		}			
		else if (newDataAndata == '' && maDomani==ma && aaVoloA=='null' && mmVoloA=='null')
		{
			$(optVoloA).attr('selected','selected').appendTo('#charter_ma_a');
			$('#charter_m_a').val(mese); 
			$('#charter_a_a').val(anno); 			
		}
		else
			$(optVoloA).appendTo('#charter_ma_a');
		if (anno==aaA && mese==mmA)
			$(optA).attr('selected','selected').appendTo('#hotel_ma_a');
		else if ($('#charter_ma_a').val() == strAm)
		{	
			$(optA).attr('selected','selected').appendTo('#hotel_ma_a');
			$('#hotel_m_a').val(mese); 
			$('#hotel_a_a').val(anno); 		
		}			
		else
			$(optA).appendTo('#hotel_ma_a');
		if (anno==aaP && mese==mmP && newDataRitorno == '')
			$(optP).attr('selected','selected').appendTo('#hotel_ma_p');
		else if (newDataRitorno != '' && (new_m_r + '/' + new_a_r) == ma)
		{
			$(optVoloA).attr('selected','selected').appendTo('#hotel_ma_p');
			$('#hotel_m_p').val(mese); 
			$('#hotel_a_p').val(anno);
			UpdateHotelValue();		
		}
		else
			$(optP).appendTo('#hotel_ma_p');
	}
	
	//Aggiorno le notti
	function UpdateHotelValue() {
		var strDateA = new_m_a + '/' + new_g_a + '/' + new_a_a;
		var strDateP = new_m_r + '/' + new_g_r + '/' + new_a_r;
		var dataA = new Date(strDateA);
		var dataP = new Date(strDateP);
		$('#hotel_notti').val(parseInt((Math.max(dataP, dataA) - Math.min(dataP, dataA)) / 86400000));
		//Aggiorno i DTPicker
		$('#hotel_h_data_a').changeDatepicker({minDate: dataA});
		$('#hotel_h_data_p').changeDatepicker({maxDate: dataP});
	};
	
	//Se la data di partenza Volo è vuota
	if (ggVoloA=='' && aaVoloA=='' && mmVoloA=='')
		$('#volo_h_data_a').val(ggDomani + "/" + mmDomani + "/" + aaaaDomani);

	//Gestione popolamento tendine viaggiatori per RsOnlyModificaMotoreCHHO.jsp
	if (formHotelVal != '' || keydVal != '') {
		// per RsOnlyModificaMotoreCHHO.jsp
		pageLoad();
	}
								
	// 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,#charter_ma_a').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]=='charter' && _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 == 'charter_h_data_a') {
			$('#charter_m_a').val(_mese);
			$('#charter_a_a').val(_anno)
		}
		//Gestione cambio date voli
		if (this.id == 'charter_h_data_a') {							
			var newSelectedDate = $('#charter_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());			
			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($('#charter_g_a').val() + '/' + $('#charter_ma_a').val().substr(4,6) + '/' + $('#charter_ma_a').val().substr(0, 4));						
		}		
	};
	
	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));
			if ($('#hotel_notti').val() == 'NaN')
				$('#hotel_notti').val('');
			
		}
		// else if (this.value == '' || _$_sel.val() == '') {
		//	$('#hotel_notti').val('');
		//}
		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: domani,
			maxDate: fraUnAnno,
			buttonImage: '/ResEBook/img/sf/calendario.gif',
			beforeShow: readLinked,
			onSelect: updateLinkedNotti
		});
		$('#hotel_h_data_p').attachDatepicker({
			minDate: dopodomani,
			maxDate: fraUnAnno,
			buttonImage: '/ResEBook/img/sf/calendario.gif',
			beforeShow: readLinked,
			onSelect: updateLinkedNotti
		});
		$('#charter_h_data_a').attachDatepicker({
			minDate: oggi,
			maxDate: fraUnAnno,
			buttonImage: '/ResEBook/img/sf/calendario.gif',
			beforeShow: readLinked,
			onSelect: updateLinked
		});
		//	fine sezione datepicker
	}
	catch(e) {
		// non fare nulla
	}

	if(newDataRitorno != '')
	{
		var strDateA = new_m_a + '/' + new_g_a + '/' + new_a_a;
		var strDateP = new_m_r + '/' + new_g_r + '/' + new_a_r;
		var dataA = new Date(strDateA);
		var dataP = new Date(strDateP);
		//Aggiorno i DTPicker
		$('#hotel_h_data_a').changeDatepicker({minDate: dataA});
		$('#hotel_h_data_a').changeDatepicker({maxDate: dataP});
		$('#hotel_h_data_p').changeDatepicker({minDate: dataA});
		$('#hotel_h_data_p').changeDatepicker({maxDate: dataP});
	}
	
	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) + ')').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();
	});
	
	//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();
				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();				
				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_m_p').val($('#hotel_ma_p').val().substr(4,6));
			$('#hotel_a_p').val($('#hotel_ma_p').val().substr(0,4));
		}
	});
	
	$('#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));
			$('#hotel_m_a').val($('#hotel_ma_a').val().substr(4,6));
			$('#hotel_a_a').val($('#hotel_ma_a').val().substr(0,4));	
		}
	});
		
	$('#charter_g_a').change(function(){
		if (($('#charter_g_a').val() != '') && ($('#charter_ma_a').val() != ''))
		{

			var strDateA = $('#charter_ma_a').val().substr(4,6) + '/' + $('#charter_g_a').val() + '/' + $('#charter_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() + 1);
			$('#hotel_h_data_p').changeDatepicker({minDate: dataA});
			
			$('#charter_h_data_a').val(strDate_a);
			$('#charter_m_a').val(str_m_Date_a);
			$('#charter_a_a').val(str_a_Date_a);
		}
	});

	$('#charter_ma_a').change(function(){
		if (($('#charter_g_a').val() != '') && ($('#charter_ma_a').val() != ''))
		{
			var strDateA = $('#charter_ma_a').val().substr(4,6) + '/' + $('#charter_g_a').val() + '/' + $('#charter_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() + 1);
			$('#hotel_h_data_p').changeDatepicker({minDate: dataA});
			
			$('#charter_h_data_a').val(strDate_a);
			$('#charter_m_a').val(str_m_Date_a);
			$('#charter_a_a').val(str_a_Date_a);
			
		}
	});		
	
	//if (soloAndata != 'RT')
	//if (newDataRitorno == '')
	//	$('#hotel_notti').val('');
		
	jQuery("input[name^='tipoVg']").each(
		function()
		{
			if (this.checked) {
				chiamaWs(this.value);
			}
		}
	)

	$('#button_submit').click(function(){
		
		$('#form_check').remove();
		var messaggio = '<p>Errori nella compilazione del form:</p>'
		//Prima volta
		if(newDataAndata == '')
		{
			var g_a = + $('#charter_g_a').val();
			var ma_a = + $('#charter_ma_a').val();
			var strDateA = $('#charter_ma_a').val().substr(4,6) + '/' + $('#charter_g_a').val() + '/' + $('#charter_ma_a').val().substr(0, 4);
			var dataA = new Date(strDateA);
			var today = new Date();
	
			if (!isDate(strDateA))
				messaggio += '</BR> - Data partenza errata.';
			if (dataA < today)
				messaggio += '</BR> - Data partenza antecedente a quella odierna.';
				
			if (messaggio == '<p>Errori nella compilazione del form:</p>') {
				messaggio = '<p>Form incompleto:</p>';
				if ($('#partenza').val() == '') messaggio += '</BR> - Devi selezionare l&rsquo;aeroporto di partenza.';
				if ($('#charter_g_a').val() == '' || $('#charter_ma_a').val() == '') messaggio += '</BR> - Devi scegliere la data di partenza del volo.';
			}
			
			//Controllo viaggiatori Charter e Hotel
			var numCamere = $('#hotel_numero_camere').val();
			var numAdulti = 0;
			var numBambini = 0;
			var numInfanti = 0;
			var bambino = 0;
			for(var i=0; i< numCamere; i++)
			{
				numAdulti = numAdulti + parseInt($('#hotel_camera_' + (i+1) + '_adulti').val());
				bambino = parseInt($('#hotel_camera_' + (i+1) + '_bambini').val());
				if (bambino > 0)
				{
					for (var j=0;j < bambino;j++)
					{
						if ($("select[name^='e" + (j+1) + "b" + (i+1) + "']").val() > 11)
							numAdulti = numAdulti + 1;
						else if ($("select[name^='e" + (j+1) + "b" + (i+1) + "']").val() < 2)
							numInfanti = numInfanti + 1;
						else
							numBambini = numBambini + 1;
					}
				}
			}
			/*
			$('#charter_adulti').val(numAdulti)
			$('#charter_bambini').val(numBambini);
			$('#charter_neonati').val(numInfanti);
			*/
			
			$('#adu').val(numAdulti)
			$('#chd').val(numBambini);
			$('#inf').val(numInfanti);
		}
		else //Seconda volta
		{
			var g_h_a = + $('#hotel_g_a').val();
			var g_h_p = + $('#hotel_g_p').val();
			var ma_h_a = + $('#hotel_ma_a').val();
			var ma_h_p = + $('#hotel_ma_p').val();			
			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 strDateCA = $('#charter_ma_a').val().substr(4,6) + '/' + $('#charter_g_a').val() + '/' + $('#charter_ma_a').val().substr(0, 4);
			var strDateCR = $('#dataRitorno').val().substr(4,2) + '/' + $('#dataRitorno').val().substr(6, 2) + '/' + $('#dataRitorno').val().substr(0, 4);
			var dataCA = new Date(strDateCA);
			var dataCR = new Date(strDateCR);

			var dataMax = new Date(newDataRitorno.substr(0,4), newDataRitorno.substr(4,2)-1, newDataRitorno.substr(6,8)); 

			if (dataHA < dataCA)
				messaggio += '</BR> - Data arrivo in Hotel antecedente alla data di partenza del volo di andata.';
			
			if (dataHP > dataMax) {
				if (dataHP.getTime() == dataCR.getTime())
					messaggio += '</BR> - Data Hotel partenza non valida per il volo di ritorno notturno.';
				else					
					messaggio += '</BR> - Data Hotel partenza successiva alla data del volo di ritorno.';
			}
			
			if (!isDate(strDateHA))
				messaggio += '</BR> - Data Hotel arrivo errata.';			
			if (dataHA < today)
				messaggio += '</BR> - Data Hotel arrivo antecedente a quella odierna.';	
			if (!isDate(strDateHP))
				messaggio += '</BR> - Data Hotel partenza errata.';		
			if (dataHP < today)
				messaggio += '</BR> - Data Hotel partenza antecedente a quella odierna.';
				
			if (ma_h_a == ma_h_p && ma_h_a != '0' && ma_h_p != '0') {
				if (g_h_p <= g_h_a && g_h_p != '0' && g_h_a != '0') messaggio += '</BR> - La data di partenza deve essere successiva a quella di partenza hotel.';
			} else if (ma_h_p < ma_h_a && g_h_p != '0' && g_h_a != '0') messaggio += '</BR> - La data di partenza deve essere successiva a quella di partenza hotel.';
			if (messaggio == '<p>Errori nella compilazione del form:</p>') {
				messaggio = '<p>Form incompleto:</p>';
				if ($('#hotel_g_a').val() == '' || $('#hotel_ma_a').val() == '') messaggio += '</BR> - Devi scegliere la data di arrivo hotel.';
				if ($('#hotel_g_p').val() == '' || $('#hotel_ma_p').val() == '') messaggio += '</BR> - Devi scegliere la data di partenza hotel.';
			}
			else if ($('#hotel_g_p').val() == '' || $('#hotel_ma_p').val() == '')
				 messaggio += '</BR> - Devi scegliere la data di partenza hotel.';
			
			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.';
			}	
		}

		var formHotelVal = document.forms['form_charter_volo'].formHotel.value;
		if (formHotelVal == '2' || formHotelVal == '3') {
			messaggio += controlloCongruenzaPaxVoloHotel();
		}
		
		if (messaggio == '<p>Form incompleto:</p>'){
			
			createCookie("provenienzaNavigatore","Motore",1); 	
		
		    var nDivTrovaLayer = $("#trova_layer").length;
		    
		    if(nDivTrovaLayer > 0){
		    	HideDIV('alpitourPAGEcontent'); DisplayDIV('trova_layer');
		    }else{
			    viewPaginaAttesa(); 
			}     

			$('#form_charter_volo').submit();
			return true;
		
		}
		else {
			var keydVal =  document.forms['form_charter_volo'].keyd.value;
			if (formHotelVal == '' && keydVal == '') {
				// per SearchFormCHHO.jsp
				$('<div id="form_check" class="formBox"></div>')
				.html(messaggio)
				.insertAfter('#form_charter_volo');				
			}
			else {
				// per RsOnlyModificaMotoreCHHO.jsp
				$('<div id="form_check" class="formBox" style="margin:0px;"></div>')
				.html(messaggio)
				.insertAfter('#form_charter_volo');
			}
			return false;
		}
	});
	
	jQuery("input[name^='tipoVg']").bind('click',function(e){
    	chiamaWs(jQuery(this).attr("value"));
	});

	// Visualizzo Pagina di Attesa dopo aver premuto il tasto TROVA 
	function viewPaginaAttesa() {
	
	    var coordinate = jQuery('#searchBox').offset();

        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;
	} 	
	
	
});

function controlloCongruenzaPaxVoloHotel() {

	var msgErrore = "";
	var numCamere = 0;
    var aduCam1 = 0; var chdCam1 = 0; var ageb1c1 = 0; var ageb2c1 = 0; var ageb3c1 = 0;
    var aduCam2 = 0; var chdCam2 = 0; var ageb1c2 = 0; var ageb2c2 = 0; var ageb3c2 = 0;
    var aduCam3 = 0; var chdCam3 = 0; var ageb1c3 = 0; var ageb2c3 = 0; var ageb3c3 = 0;
    var totAduCam = 0; var totChdCam = 0; var totInfCam = 0;

	numCamere = parseInt($('#hotel_numero_camere').val(), 10);
	
	aduCam1 = parseInt($('#hotel_camera_1_adulti').val(), 10);
	chdCam1 = parseInt($('#hotel_camera_1_bambini').val(), 10);
	if (numCamere >= 2)
	{
		aduCam2 = parseInt($('#hotel_camera_2_adulti').val(), 10);
		chdCam2 = parseInt($('#hotel_camera_2_bambini').val(), 10);
	}
	if (numCamere == 3)
	{
		aduCam3 = parseInt($('#hotel_camera_3_adulti').val(), 10);
		chdCam3 = parseInt($('#hotel_camera_3_bambini').val(), 10);
	}

    totAduCam = 0 + eval(aduCam1)  +  eval(aduCam2) + eval(aduCam3);

	// Età bambini della camera 1
	if (chdCam1 >= 1) {
		agee1b1 = parseInt($('#e1b1').val(), 10);
		if (agee1b1 < 2) {
			totInfCam = totInfCam + 1;
		}
		else if(agee1b1 > 11) {
			totAduCam = totAduCam + 1;
		}
		else {
			totChdCam = totChdCam + 1; 
		}
	}
	if (chdCam1 >= 2) {
		agee2b1 = parseInt($('#e2b1').val(), 10);
		if (agee2b1 < 2) {
			totInfCam = totInfCam + 1;
		}
		else if(agee2b1 > 11) {
			totAduCam = totAduCam + 1;
		}
		else {
			totChdCam = totChdCam + 1; 
		}
	}
	if (chdCam1 == 3) {
		agee3b1 = parseInt($('#e3b1').val(), 10);
		if (agee3b1 < 2) {
			totInfCam = totInfCam + 1;
		}
		else if(agee3b1 > 11) {
			totAduCam = totAduCam + 1;
		}
		else {
			totChdCam = totChdCam + 1; 
		}
	}

	// Età bambini della camera 2
	if (chdCam2 >= 1) {
		agee1b2 = parseInt($('#e1b2').val(), 10);;
		if (agee1b2 < 2) {
			totInfCam = totInfCam + 1;
		}
		else if(agee1b2 > 11) {
			totAduCam = totAduCam + 1;
		}
		else {
			totChdCam = totChdCam + 1; 
		}
	}
	if (chdCam2 >= 2) {
		agee2b2 = parseInt($('#e2b2').val(), 10);
		if (agee2b2 < 2) {
			totInfCam = totInfCam + 1;
		}
		else if(agee2b2 > 11) {
			totAduCam = totAduCam + 1;
		}
		else {
			totChdCam = totChdCam + 1; 
		}
	}
	if (chdCam2 == 3) {
		agee3b2 = parseInt($('#e3b2').val(), 10);
		if (agee3b2 < 2) {
			totInfCam = totInfCam + 1;
		}
		else if(agee3b2 > 11) {
			totAduCam = totAduCam + 1;
		}
		else {
			totChdCam = totChdCam + 1; 
		}
	}
	
	// Età bambini della camera 3
	if (chdCam3 >= 1) {
		agee1b3 = parseInt($('#e1b3').val(), 10);
		if (agee1b3 < 2) {
			totInfCam = totInfCam + 1;
		}
		else if(agee1b3 > 11) {
			totAduCam = totAduCam + 1;
		}
		else {
			totChdCam = totChdCam + 1; 
		}
	}
	if (chdCam3 >= 2) {
		agee2b3 = parseInt($('#e2b3').val(), 10);
		if (agee2b3 < 2) {
			totInfCam = totInfCam + 1;
		}
		else if(agee2b3 > 11) {
			totAduCam = totAduCam + 1;
		}
		else {
			totChdCam = totChdCam + 1; 
		}
	}
	if (chdCam3 == 3) {
		agee3b3 = parseInt($('#e3b3').val(), 10);
		if (agee3b3 < 2) {
			totInfCam = totInfCam + 1;
		}
		else if(agee3b3 > 11) {
			totAduCam = totAduCam + 1;
		}
		else {
			totChdCam = totChdCam + 1; 
		}
	}

    // Controllo che l'occupazione delle camere sia congruente con i passeggeri sul volo
	var aduVolo = parseInt(document.forms['form_charter_volo'].adu.value, 10);
	var chdVolo = parseInt(document.forms['form_charter_volo'].chd.value, 10);
	var infVolo = parseInt(document.forms['form_charter_volo'].inf.value, 10);

    if (totAduCam != aduVolo  || totChdCam != chdVolo || totInfCam != infVolo) {
        msgErrore = '</BR> - Numero viaggiatori in hotel non congruente con passeggeri sul volo:';
        msgErrore += '</BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* passeggeri con meno di 2 anni: ' + infVolo;
        msgErrore += '</BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* passeggeri tra i 2 e gli 11 anni: ' + chdVolo;
        msgErrore += '</BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* passeggeri con almeno 12 anni: ' + aduVolo;
    }

	return msgErrore;    
}

function attesa() {
	$('#form_check').remove();
	var messaggio = '<p>Errori nella compilazione del form:</p>'
	//Prima volta
	if(newDataAndata == '')
	{
		var g_a = + $('#charter_g_a').val();
		var ma_a = + $('#charter_ma_a').val();
		var strDateA = $('#charter_ma_a').val().substr(4,6) + '/' + $('#charter_g_a').val() + '/' + $('#charter_ma_a').val().substr(0, 4);
		var dataA = new Date(strDateA);
		var today = new Date();

		if (!isDate(strDateA))
			messaggio += '</BR> - Data partenza errata.';
		if (dataA < today)
			messaggio += '</BR> - Data partenza antecedente a quella odierna.';
			
		if (messaggio == '<p>Errori nella compilazione del form:</p>') {
			messaggio = '<p>Form incompleto:</p>';
			if ($('#partenza').val() == '') messaggio += '</BR> - Devi selezionare l&rsquo;aeroporto di partenza.';
			if ($('#charter_g_a').val() == '' || $('#charter_ma_a').val() == '') messaggio += '</BR> - Devi scegliere la data di partenza del volo.';
		}
		
		//Controllo viaggiatori Charter e Hotel
		var numCamere = $('#hotel_numero_camere').val();
		var numAdulti = 0;
		var numBambini = 0;
		var numInfanti = 0;
		var bambino = 0;
		for(var i=0; i< numCamere; i++)
		{
			numAdulti = numAdulti + parseInt($('#hotel_camera_' + (i+1) + '_adulti').val());
			bambino = parseInt($('#hotel_camera_' + (i+1) + '_bambini').val());
			if (bambino > 0)
			{
				for (var j=0;j < bambino;j++)
				{
					if ($("select[name^='e" + (j+1) + "b" + (i+1) + "']").val() > 11)
						numAdulti = numAdulti + 1;
					else if ($("select[name^='e" + (j+1) + "b" + (i+1) + "']").val() < 2)
						numInfanti = numInfanti + 1;
					else
						numBambini = numBambini + 1;
				}
			}
		}
		/*
		$('#charter_adulti').val(numAdulti)
		$('#charter_bambini').val(numBambini);
		$('#charter_neonati').val(numInfanti);
		*/
		
		$('#adu').val(numAdulti)
		$('#chd').val(numBambini);
		$('#inf').val(numInfanti);
	}
	else //Seconda volta
	{
		var g_h_a = + $('#hotel_g_a').val();
		var g_h_p = + $('#hotel_g_p').val();
		var ma_h_a = + $('#hotel_ma_a').val();
		var ma_h_p = + $('#hotel_ma_p').val();			
		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 strDateCA = $('#charter_ma_a').val().substr(4,6) + '/' + $('#charter_g_a').val() + '/' + $('#charter_ma_a').val().substr(0, 4);
		var strDateCR = $('#dataRitorno').val().substr(4,2) + '/' + $('#dataRitorno').val().substr(6, 2) + '/' + $('#dataRitorno').val().substr(0, 4);
		var dataCA = new Date(strDateCA);
		var dataCR = new Date(strDateCR);

		var dataMax = new Date(newDataRitorno.substr(0,4), newDataRitorno.substr(4,2)-1, newDataRitorno.substr(6,8)); 

		if (dataHA < dataCA)
			messaggio += '</BR> - Data arrivo in Hotel antecedente alla data di partenza del volo di andata.';
		
		if (dataHP > dataMax) {
			if (dataHP.getTime() == dataCR.getTime())
				messaggio += '</BR> - Data Hotel partenza non valida per il volo di ritorno notturno.';
			else					
				messaggio += '</BR> - Data Hotel partenza successiva alla data del volo di ritorno.';
		}
		
		if (!isDate(strDateHA))
			messaggio += '</BR> - Data Hotel arrivo errata.';			
		if (dataHA < today)
			messaggio += '</BR> - Data Hotel arrivo antecedente a quella odierna.';	
		if (!isDate(strDateHP))
			messaggio += '</BR> - Data Hotel partenza errata.';		
		if (dataHP < today)
			messaggio += '</BR> - Data Hotel partenza antecedente a quella odierna.';
			
		if (ma_h_a == ma_h_p && ma_h_a != '0' && ma_h_p != '0') {
			if (g_h_p <= g_h_a && g_h_p != '0' && g_h_a != '0') messaggio += '</BR> - La data di partenza deve essere successiva a quella di partenza hotel.';
		} else if (ma_h_p < ma_h_a && g_h_p != '0' && g_h_a != '0') messaggio += '</BR> - La data di partenza deve essere successiva a quella di partenza hotel.';
		if (messaggio == '<p>Errori nella compilazione del form:</p>') {
			messaggio = '<p>Form incompleto:</p>';
			if ($('#hotel_g_a').val() == '' || $('#hotel_ma_a').val() == '') messaggio += '</BR> - Devi scegliere la data di arrivo hotel.';
			if ($('#hotel_g_p').val() == '' || $('#hotel_ma_p').val() == '') messaggio += '</BR> - Devi scegliere la data di partenza hotel.';
		}
		else if ($('#hotel_g_p').val() == '' || $('#hotel_ma_p').val() == '')
			 messaggio += '</BR> - Devi scegliere la data di partenza hotel.';
		
		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.';
		}	
	}

	var formHotelVal = document.forms['form_charter_volo'].formHotel.value;
	if (formHotelVal == '2' || formHotelVal == '3') {
		messaggio += controlloCongruenzaPaxVoloHotel();
	}
	
	if (messaggio == '<p>Form incompleto:</p>'){
	
		var nameWin = "wait";
		var win = "";
		win = window.open(basePath + "/ResEBook/html/attesa.html",nameWin,"width=960, height=650, top=30, left=150, status=yes,toolbar=yes,scrollbars=yes,resizable=yes,menubar=yes,location=yes");
		win.focus();
		setTimeout(function() {document.form_charter_volo.submit()},1000);
		document.form_charter_volo.target = nameWin;	
			
		//$('#form_charter_volo').submit();
		return true;
	
	}
	else {
		var keydVal =  document.forms['form_charter_volo'].keyd.value;
		if (formHotelVal == '' && keydVal == '') {
			// per SearchFormCHHO.jsp
			$('<div id="form_check" class="formBox"></div>')
			.html(messaggio)
			.insertAfter('#form_charter_volo');				
		}
		else {
			// per RsOnlyModificaMotoreCHHO.jsp
			$('<div id="form_check" class="formBox" style="margin:0px;"></div>')
			.html(messaggio)
			.insertAfter('#form_charter_volo');
		}
		return false;
	}
}


