var xmlDocApt = null;
var collectionApt = new Array();
var myDays= ["Dom","Lun","Mar","Mer","Gio","Ven","Sab","Dom"];

function aeroportiPart(a,b,c){
	this.apt = a
	this.maPart = b
	this.gPart = c
}

function attesa () {
	
	if (document.getElementById('PARTENZE').style.display != 'none') {
		$('#descrAptPart').val(document.form_vacanze.aptPar.options[document.form_vacanze.aptPar.selectedIndex].text);
	}			

	$('#form_check').remove();
	var messaggio = '<p>Errori nella compilazione del form:</p>';
	
	var adt = parseInt($('#adulti').val());
	var und = parseInt($('#under18').val());
	var undeu1 = parseInt($('#eu1').val());
	var undeu2 = parseInt($('#eu2').val());

	if (document.getElementById('div_budget').style.display == 'block' && $('#budget').val() == '')
		messaggio += '</BR> - Devi selezionare il budget.';

	if ($('#destinazione').val() != '' && $('#keyd').val() == '') 
		messaggio += '</BR> - Devi selezionare una localit&agrave; di destinazione valida.';

	if ($('#destinazione').val() == '') 
		messaggio += '</BR> - Devi selezionare la localit&agrave; di destinazione.';

	//PROVVISORIO
	/*if (document.getElementById('PARTENZE').style.display == 'none') {
		if ($('#destinazione').val() != '' && ($('#liv2').val() == null || $('#liv2').val() == '-1') ) 
			messaggio += '</BR> - Devi selezionare almeno la Zona.';
	}*/
	
	if (document.getElementById('PARTENZE').style.display == 'none' && $('#giornoP').val() == '') {
		return false;
	}
	
	if (document.getElementById('PARTENZE').style.display == 'block') {
		if ( $('#aptPar').val() == '') 
			messaggio += '</BR> - Inserire l\'Aeroporto di partenza.';
		if ( $('#giornoP').val() == '') 
			messaggio += '</BR> - Inserire il Giorno di partenza.';
		if ( $('#meseAnnoP').val() == '') 
			messaggio += '</BR> - Inserire il Mese e l\'Anno di partenza.';
		if ( $('#durata').val() == '') 
			messaggio += '</BR> - Inserire la durata della vacanza.';		
		if ( adt+und > 4)
			messaggio += '</BR> - I viaggiatori non possono essere pi&ugrave; di 4.';
		if ( und==2 && (undeu1 < 0 || undeu2 < 0)) {
			if (undeu1 < 0 && undeu2 < 0) {
				messaggio += '</BR> - Inserire le et&agrave; mancanti.';
			} else {
				messaggio += '</BR> - Inserire l\'et&agrave; mancante.';
			}
		}
		if ( und==1 && undeu1 < 0)
			messaggio += '</BR> - Inserire l\'et&agrave; mancante.';				
	}
	
	if (messaggio == '<p>Errori nella compilazione del form:</p>') {
		messaggio = '<p>Form incompleto:</p>';
	}

	if (messaggio == '<p>Form incompleto:</p>') {
		messaggio = '<p>Form incompleto:</p>';

		var descLiv = document.form_vacanze.descliv1.value;
		if (descLiv.charAt(0) != '.' && descLiv != '' && descLiv != null) {
			messaggio += '</BR> - Devi selezionare uno Stato di destinazione.';
		}
	}
	if (messaggio == '<p>Form incompleto:</p>') {
		messaggio = '<p>Form incompleto:</p>';

		if (document.form_vacanze.liv2 != undefined) {
			var numOptLiv2 = document.form_vacanze.liv2.options.length;
			if (numOptLiv2 > 0 && document.form_vacanze.liv2.selectedIndex == 0)
				messaggio += '</BR> - Devi selezionare una Zona di destinazione.';
		}
	}

	if (messaggio == '<p>Form incompleto:</p>'){
	
		//Gestendo il scegli = -1 questo valore non posso passarlo alla form sucessiva 
		if ( und==0) {
			$('#eu1').selectOptions("0");
			$('#eu2').selectOptions("0");
		} else {
			if (und==1) {
				$('#eu2').selectOptions("0");
			}
		}	

		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_vacanze.submit()},1000);
		document.form_vacanze.target = nameWin;			
			
		return true;
	}
	else {

		$('<div id="form_check" class="formBox"></div>')
		.html(messaggio)
		.insertAfter('#form_vacanze');
		return false;
	}
}

function loadCollApt(n){
	
	collectionApt = new Array();
	var y = 0;
	
	for(var i=0;i<n.childNodes.length;i++){
		var x = n.childNodes[i];
		if (x.nodeName == 'APT') {
			var aeroporto = x.firstChild.nodeValue;
			var code = x.firstChild.nodeValue.substring(0,x.firstChild.nodeValue.indexOf(';')).trim();
			
			for(var p=0;p<n.childNodes.length;p++){
				if ( code == n.childNodes[p].nodeName) {
				    maArray = new Array();
				    gArray = new Array();
				    var count = 0;
				    for (var node=0;node<n.childNodes[p].childNodes.length;node++) {			    	
				    	//Workaround per Mozilla
				    	if (n.childNodes[p].childNodes[node].nodeName != '#text') {
					    	for(var z=0;z<n.childNodes[p].childNodes[node].attributes.length;z++){
					     		var atrib = n.childNodes[p].childNodes[node].attributes[z];
					     		if (atrib.nodeName == 'ID' || atrib.nodeName == 'id') {
					     			maArray[count] = atrib.nodeValue;
					     		}
					     		else if (atrib.nodeName == 'DD' || atrib.nodeName == 'dd') {
					     			gArray[count] = atrib.nodeValue;
					     		}
					    	}
					    	count++;
					    }
					}
				    var aptP = new aeroportiPart;
				    aptP.apt = aeroporto;
				    aptP.maPart = maArray;
				    aptP.gPart = gArray;
					collectionApt[y] = aptP;
					break;
				}
			}
			y++;
		}
	}

	$('#aptPar').removeOption(/./); 
	$('#giornoP').removeOption(/./); 
	$('#meseAnnoP').removeOption(/./); 
	$('#durata').removeOption(/./);

	for(var p=0;p<collectionApt.length;p++){
		var code = collectionApt[p].apt.substring(0,collectionApt[p].apt.indexOf(';')).trim();
		var text = collectionApt[p].apt.substring(collectionApt[p].apt.indexOf(';')+1);
		text = text.substr(0,1) + text.substr(1).toLowerCase();
		document.getElementById("aptPar").options[p] = new Option(text, code);

		if (code == 'ALL') {
			for(var k=0;k<collectionApt[p].maPart.length;k++){

				//Workaround per Mozilla
				if (collectionApt[p].maPart[k] == undefined)
					k++;
				else if (collectionApt[p].maPart[k] == '') {
					k++;
				}
					
				var code = collectionApt[p].maPart[k].substring(0,collectionApt[p].maPart[k].indexOf(';'));
				var text = collectionApt[p].maPart[k].substring(collectionApt[p].maPart[k].indexOf(';')+1);
				
				var sAnno = "20" + code.substring(2);
				
				document.getElementById("meseAnnoP").options[k] = new Option(text + " " + sAnno, code);
				var sMese = code.substring(0,2);
				
				code = collectionApt[p].gPart[k].substring(0,collectionApt[p].gPart[k].indexOf(';'));
				text = collectionApt[p].gPart[k].substring(collectionApt[p].gPart[k].indexOf(';')+1);
				
				if (k==0) {
					var dd = collectionApt[p].gPart[k].split("|");
					for (var h=0;h<dd.length;h++) {
						var d = dd[h].split(";");
						for (var m=0;m<d.length;m++) {
							if (m ==0) {
								var dtI = new Date(sAnno, sMese-1, d[m]); 						
								thisDay=myDays[dtI.getDay()];
								document.getElementById("giornoP").options[h] = new Option(thisDay + " " + d[m], d[m]);
							}
							else { 
								if (h==0) {
									document.getElementById("durata").options[m-1] = new Option(d[m] + " Giorni", d[m]);
								}
							}
						}
					}
				}
			}
		}
	}

}

function loadAptPart(){

	var messaggi = [];
	var $sb = $('#searchBox');
	var offset = $sb.offset();
	var w = $sb.innerWidth();
	var h = $sb.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(' + basePath + '/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');
			
	var param= basePath + "/EBookingWeb/SEAAPTVGTO?tipoScelto=" + $('#tipoVacanza').val() + "&keyd=" + document.form_vacanze.keyd.value + "&tipoFlusso=" + document.form_vacanze.flusso.value + "&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: param,
		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.') {
				$('RELATIONS_VGTO', data).each(function(i){
					xmlDocApt=this;
					loadCollApt(xmlDocApt);
				});
				jQuery($('#PARTENZE')).attr('style','display: block;');
				document.getElementById("VIAGGIATORI").style.display = 'block';
				if ($('#under18').val()==1) {
					document.getElementById("eta_under_1").style.display = 'block';
					document.getElementById("eta_under_2").style.display = 'none';
				} else if ($('#under18').val()==2) {
					document.getElementById("eta_under_1").style.display = 'block';
					document.getElementById("eta_under_2").style.display = 'block';
				} else {
					document.getElementById("eta_under_1").style.display = 'none';
					document.getElementById("eta_under_2").style.display = 'none';
				}
				jQuery($('#piuCamPax')).attr('style','display: block;');
				jQuery($('#campiObbli')).attr('class','spaceTop4');
			}
			else { 
				jQuery($('#PARTENZE')).attr('style','display: none;');
				jQuery($('#VIAGGIATORI')).attr('style','display: none;');
				document.getElementById("eta_under_1").style.display = 'none';
				document.getElementById("eta_under_2").style.display = 'none';
				jQuery($('#piuCamPax')).attr('style','display: none;');
				jQuery($('#campiObbli')).attr('class','spaceTop12');
				$('#aptPar').removeOption(/./); 
				$('#giornoP').removeOption(/./); 
				$('#meseAnnoP').removeOption(/./); 
				$('#durata').removeOption(/./);
				alert("Errore nella ricezione dei dati.");
			}
		},	
		error: function(){
			jQuery($('#PARTENZE')).attr('style','display: none;');
			jQuery($('#VIAGGIATORI')).attr('style','display: none;');
			document.getElementById("eta_under_1").style.display = 'none';
			document.getElementById("eta_under_2").style.display = 'none';
			jQuery($('#piuCamPax')).attr('style','display: none;');
			jQuery($('#campiObbli')).attr('class','spaceTop12');
			$('#aptPar').removeOption(/./); 
			$('#giornoP').removeOption(/./); 
			$('#meseAnnoP').removeOption(/./); 
			$('#durata').removeOption(/./);
			alert("ERRORE: Impossibile contattare il server. Provare a ricaricare la pagina.");
		}	
	});*/
}

function loadXMLDest (xmlData){
	
	var messaggi = [];
	
	$('#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.') {
		$('D000001', data).each(function(i){
			xmlDoc=this;
			collection = null;
			collection = new Array();
			
			loadColl(this);
			
			if (a0 == null)
				a0 = new actb($('#destinazione'),$('#keyd'),collection,false);
			else
				a0.setColl(collection);

			loadDest('liv1','','1','');
			setComboDest('form_vacanze');
		});
								
	}	//	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 loadXMLApt (xmlData){
	
	var messaggi = [];

	$('#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.') {
		$('RELATIONS_VGTO', data).each(function(i){
			xmlDocApt=this;
			loadCollApt(xmlDocApt);
		});
		jQuery($('#PARTENZE')).attr('style','display: block;');
		document.getElementById("VIAGGIATORI").style.display = 'block';
		if ($('#under18').val()==1) {
			document.getElementById("eta_under_1").style.display = 'block';
			document.getElementById("eta_under_2").style.display = 'none';
		} else if ($('#under18').val()==2) {
			document.getElementById("eta_under_1").style.display = 'block';
			document.getElementById("eta_under_2").style.display = 'block';
		} else {
			document.getElementById("eta_under_1").style.display = 'none';
			document.getElementById("eta_under_2").style.display = 'none';
		}
		jQuery($('#piuCamPax')).attr('style','display: block;');
		jQuery($('#campiObbli')).attr('class','spaceTop4');
	}
	else { 
		jQuery($('#PARTENZE')).attr('style','display: none;');
		jQuery($('#VIAGGIATORI')).attr('style','display: none;');
		document.getElementById("eta_under_1").style.display = 'none';
		document.getElementById("eta_under_2").style.display = 'none';
		jQuery($('#piuCamPax')).attr('style','display: none;');
		jQuery($('#campiObbli')).attr('class','spaceTop12');
		$('#aptPar').removeOption(/./); 
		$('#giornoP').removeOption(/./); 
		$('#meseAnnoP').removeOption(/./); 
		$('#durata').removeOption(/./);
		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();
	});	
	
	//	variabili utilizzate nel parsing dell'xml
	var messaggi = [];

	    $('#destinazione').attr("autocomplete","off");
	    var chiamaWs = function(){
			var $sb = $('#searchBox');
			var offset = $sb.offset();
			var w = $sb.innerWidth();
			var h = $sb.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(' + basePath + '/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');
				
			var param= basePath + "/EBookingWeb/SEAVGTO" + _sess +"?tipoScelto=" + $('#tipoVacanza').val() + "&tipoFlusso=" + document.form_vacanze.flusso.value + "&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: param,
				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.') {
						$('D000001', data).each(function(i){
							xmlDoc=this;
							collection = null;
							collection = new Array();
							
							loadColl(this);
							
							if (a0 == null)
								a0 = new actb($('#destinazione'),$('#keyd'),collection,false);
							else
								a0.setColl(collection);

							loadDest('liv1','','1','');
							setComboDest('form_vacanze');
						});
												
					}	//	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
			});*/
		};

	    var chiamaWsApt = function(){
			var $sb = $('#searchBox');
			var offset = $sb.offset();
			var w = $sb.innerWidth();
			var h = $sb.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(' + basePath + '/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');
			
			var param= basePath + "/EBookingWeb/SEAAPTVGTO?tipoScelto=" + $('#tipoVacanza').val() + "&keyd=" + document.form_vacanze.keyd.value + "&tipoFlusso=" + document.form_vacanze.flusso.value + "&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: param,
				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.') {
						$('RELATIONS_VGTO', data).each(function(i){
							xmlDocApt=this;
							loadCollApt(xmlDocApt);
						});
						jQuery($('#PARTENZE')).attr('style','display: block;');
						document.getElementById("VIAGGIATORI").style.display = 'block';
						if ($('#under18').val()==1) {
							document.getElementById("eta_under_1").style.display = 'block';
							document.getElementById("eta_under_2").style.display = 'none';
						} else if ($('#under18').val()==2) {
							document.getElementById("eta_under_1").style.display = 'block';
							document.getElementById("eta_under_2").style.display = 'block';
						} else {
							document.getElementById("eta_under_1").style.display = 'none';
							document.getElementById("eta_under_2").style.display = 'none';
						}
						jQuery($('#piuCamPax')).attr('style','display: block;');
						jQuery($('#campiObbli')).attr('class','spaceTop4');
					}	//	fine if (messaggi.code && messaggi.text)
						//	se si ricevono messaggi diversi da "0" e "Completed." dalla servlet
					else {
						jQuery($('#PARTENZE')).attr('style','display: none;');
						jQuery($('#VIAGGIATORI')).attr('style','display: none;');
						document.getElementById("eta_under_1").style.display = 'none';
						document.getElementById("eta_under_2").style.display = 'none';
						jQuery($('#piuCamPax')).attr('style','display: none;');
						jQuery($('#campiObbli')).attr('class','spaceTop12');
						//alert("Errore nella ricezione dei dati.");
						alert("Destinazione non valida.");
						$('#aptPar').removeOption(/./); 
						$('#giornoP').removeOption(/./); 
						$('#meseAnnoP').removeOption(/./); 
						$('#durata').removeOption(/./);
					}
				},	// fine success
				error: function(){
					jQuery($('#PARTENZE')).attr('style','display: none;');
					jQuery($('#VIAGGIATORI')).attr('style','display: none;');
					document.getElementById("eta_under_1").style.display = 'none';
					document.getElementById("eta_under_2").style.display = 'none';
					jQuery($('#piuCamPax')).attr('style','display: none;');
					jQuery($('#campiObbli')).attr('class','spaceTop12');
					alert("ERRORE: Impossibile contattare il server. Provare a ricaricare la pagina.");
					$('#aptPar').removeOption(/./); 
					$('#giornoP').removeOption(/./); 
					$('#meseAnnoP').removeOption(/./); 
					$('#durata').removeOption(/./);
				}	// fine error
			});*/
		};


	jQuery("div[id^='eta_under_']").each(
		function()
		{
			if ( jQuery($('#under18')).val() == 1 ) {
				jQuery($('#eta_under_1')).attr('style','display: block;');
				//jQuery($('#eta_under_1')).attr('style','margin: 0 0 0 10px;');
				}
			else if ( jQuery($('#under18')).val() == 2 ) {
				jQuery($('#eta_under_1')).attr('style','display: block;');
				//jQuery($('#eta_under_1')).attr('style','margin: 0 0 0 10px;');
				jQuery($('#eta_under_2')).attr('style','display: block;');
			}
		}
	)
	
	jQuery("input[name^='tipoRicerca']").each(
		function()
		{
			if (this.checked) {
				if (this.value == 'DESTINAZIONE') {
					document.getElementById("tipo_vacanza").style.display = 'block';
				} else if (this.value == 'BUDGET') {
					document.getElementById("div_budget").style.display = 'block';
				}
				chiamaWs();
			}
		}
	)
	
	jQuery("input[name^='tipoRicerca']").bind('click',function(e){
    	if (jQuery(this).attr("value") == 'DESTINAZIONE') {
			//document.getElementById("tipo_vacanza").style.display = 'block';
			document.getElementById("div_budget").style.display = 'none';
	   		jQuery($('#tipoVacanza')).val('');
	   		jQuery($('#budget')).val('');
	    	chiamaWs();
 		} else if (this.value == 'BUDGET') {
			document.getElementById("div_budget").style.display = 'block';
			//document.getElementById("tipo_vacanza").style.display = 'none';
	   		jQuery($('#tipoVacanza')).val('');
	    	chiamaWs();
	   	} else  {
			document.getElementById("div_budget").style.display = 'none';
	   		jQuery($('#budget')).val('');
			document.getElementById("tipo_vacanza").style.display = 'block';
	   	}
	});
	
	

	$('#liv1').change(function(){
    	var loadApt = false;
    	if (document.getElementById("liv1").value != -1) {
	    	/*for(var z=0;z<collection.length;z++){
		    	x = collection[z];
	     		if (x.getAttribute("value") == ("KEY"+document.getElementById("liv1").value)) {
	     			if (x.getAttribute("L") > 1) {
		     			if (x.childNodes.length == 0 ) {
			     			loadApt = true;
						}
		     		}
	     			break;
	     		}
	    	}*/
	    	//if (loadApt)
				chiamaWsApt();
			/*else {
				$('#aptPar').removeOption(/./); 
				$('#giornoP').removeOption(/./); 
				$('#meseAnnoP').removeOption(/./); 
				$('#durata').removeOption(/./); 
				document.getElementById("PARTENZE").style.display = 'none';
				document.getElementById("VIAGGIATORI").style.display = 'none';
				document.getElementById("eta_under_1").style.display = 'none';
				document.getElementById("eta_under_2").style.display = 'none';
				$('#aptPar').addOption("", "");
				$('#giornoP').addOption("", "gg");
				$('#meseAnnoP').addOption("", "mese anno");
				$('#durata').addOption("", "");
			}*/
		}				
	});

	$('#liv2').change(function(){
		//if ($('#liv2').val() != -1)
			chiamaWsApt();
		/*else {
			$('#aptPar').removeOption(/./); 
			$('#giornoP').removeOption(/./); 
			$('#meseAnnoP').removeOption(/./); 
			$('#durata').removeOption(/./); 
			document.getElementById("PARTENZE").style.display = 'none';
			document.getElementById("VIAGGIATORI").style.display = 'none';
			document.getElementById("eta_under_1").style.display = 'none';
			document.getElementById("eta_under_2").style.display = 'none';
			$('#aptPar').addOption("", "");
			$('#giornoP').addOption("", "gg");
			$('#meseAnnoP').addOption("", "mese anno");
			$('#durata').addOption("", "");		
		}*/
	});

	$('#liv3').change(function(){
		chiamaWsApt();
	});

	if ($('#keyd').val() != '' && $('#keyd').val() != 'KEYD') {
		chiamaWsApt();
	}
	
	$('#destinazione').keyup(function(e){
		if (e.which == 9 || e.which == 13) {
			/*if ( ($('#liv3').val() == null || $('#liv3').val() == '-1') ) {
				if ( ($('#liv2').val() != null && $('#liv2').val() != '-1') ) {
					chiamaWsApt();
				} else {

    				var loadApt = false;
			    	if (document.getElementById("liv1").value != -1) {
				    	for(var z=0;z<collection.length;z++){
					    	x = collection[z];
				     		if (x.getAttribute("value") == ("KEY"+document.getElementById("liv1").value)) {
				     			if (x.getAttribute("L") > 1) {
					     			if (x.childNodes.length == 0 ) {
						     			loadApt = true;
									}
					     		}
				     			break;
				     		}
				    	}
				    	if (loadApt)
							chiamaWsApt();
						else {
							$('#aptPar').removeOption(/./); 
							$('#giornoP').removeOption(/./); 
							$('#meseAnnoP').removeOption(/./); 
							$('#durata').removeOption(/./); 
							document.getElementById("PARTENZE").style.display = 'none';
							document.getElementById("VIAGGIATORI").style.display = 'none';
							document.getElementById("eta_under_1").style.display = 'none';
							document.getElementById("eta_under_2").style.display = 'none';
							$('#aptPar').addOption("", "");
							$('#giornoP').addOption("", "gg");
							$('#meseAnnoP').addOption("", "mese anno");
							$('#durata').addOption("", "");
						}
					}
				}
			} else {*/
				chiamaWsApt();
			//}
		}
	});

	$('#tipoVacanza').change(function(){
    	$('#destinazione').val('');
    	$('#keyd').val('');
    	if ($('#tipoVacanza').val() != '') {
    		if ($('#form_check').val() != null) {
    			document.getElementById("form_check").style.display = 'none';
    		}
    	}
		document.getElementById("PARTENZE").style.display = 'none';
		document.getElementById("VIAGGIATORI").style.display = 'none';
		document.getElementById("eta_under_1").style.display = 'none';
		document.getElementById("eta_under_2").style.display = 'none';
		document.getElementById("piuCamPax").style.display = 'none';
		jQuery($('#campiObbli')).attr('class','spaceTop12');
		$('#aptPar').removeOption(/./); 
		$('#giornoP').removeOption(/./); 
		$('#meseAnnoP').removeOption(/./); 
		$('#durata').removeOption(/./);		
    	chiamaWs();
	});

	$('#chk_budget').click(function(){
		if (this.checked)
			//jQuery($('#div_budget')).attr('style','display: inline;');
			document.getElementById("div_budget").style.display = 'block';
		else {
			//jQuery($('#div_budget')).attr('style','display: none;');
			document.getElementById("div_budget").style.display = 'none';
			$('#budget').val('');
		}
	});

	$('#aptPar').change(function(){
	
		$('#descrAptPart').val(document.form_vacanze.aptPar.options[document.form_vacanze.aptPar.selectedIndex].text);
		$('#giornoP').removeOption(/./); 
		$('#meseAnnoP').removeOption(/./); 
		$('#durata').removeOption(/./); 
		for(var p=0;p<collectionApt.length;p++){
			var code = collectionApt[p].apt.substring(0,collectionApt[p].apt.indexOf(';'));
			if (code == this.value){
				for(var k=0;k<collectionApt[p].maPart.length;k++){
					var code = collectionApt[p].maPart[k].substring(0,collectionApt[p].maPart[k].indexOf(';'));
					var text = collectionApt[p].maPart[k].substring(collectionApt[p].maPart[k].indexOf(';')+1);
					
					var sAnno = "20" + code.substring(2);
					var sMese = code.substring(0,2);
					
					$('#meseAnnoP').addOption(code, text + " " + sAnno,false);
					
					if (k==0) {
						var dd = collectionApt[p].gPart[k].split("|");
						for (var h=0;h<dd.length;h++) {
							var d = dd[h].split(";");
							for (var m=0;m<d.length;m++) {
								if (m ==0) {
									var dtI = new Date(sAnno, sMese-1, d[m]); 						
									thisDay=myDays[dtI.getDay()];
									$('#giornoP').addOption(d[m], thisDay + " " + d[m],false);
								}
								else { 
									if (h==0) {
										$('#durata').addOption(d[m], d[m] + " Giorni",false);
									}
								}
							}
						}
					}
				}
			}
		}
	});

	$('#giornoP').change(function(){
		$('#durata').removeOption(/./); 
		for(var p=0;p<collectionApt.length;p++){
			var code = collectionApt[p].apt.substring(0,collectionApt[p].apt.indexOf(';'));
			if (code == $('#aptPar').val() ){
				for(var k=0;k<collectionApt[p].maPart.length;k++){
					var code = collectionApt[p].maPart[k].substring(0,collectionApt[p].maPart[k].indexOf(';'));
					if (code == $('#meseAnnoP').val()) {			
						var dd = collectionApt[p].gPart[k].split("|");
						for (var h=0;h<dd.length;h++) {
							var d = dd[h].split(";");	
							for (var m=0;m<d.length;m++) {
								if (d[0]==this.value) {
									if (m>0)
										//document.getElementById("durata").options[m-1] = new Option(d[m] + " Giorni", d[m]);
										$('#durata').addOption(d[m], d[m] + " Giorni",false);
								}
							}
						}
					}
				}
			}
		}
	});

	$('#meseAnnoP').change(function(){
		$('#giornoP').removeOption(/./); 
		$('#durata').removeOption(/./); 
		for(var p=0;p<collectionApt.length;p++){
			var code = collectionApt[p].apt.substring(0,collectionApt[p].apt.indexOf(';'));
			if (code == $('#aptPar').val()){
				for(var k=0;k<collectionApt[p].maPart.length;k++){
					var code = collectionApt[p].maPart[k].substring(0,collectionApt[p].maPart[k].indexOf(';'));

					var sAnno = "20" + code.substring(2);
					var sMese = code.substring(0,2);

					if (code==this.value) {
						var dd = collectionApt[p].gPart[k].split("|");
						for (var h=0;h<dd.length;h++) {
							var d = dd[h].split(";");
							for (var m=0;m<d.length;m++) {
								if (m ==0) {
									var dtI = new Date(sAnno, sMese-1, d[m]); 						
									thisDay=myDays[dtI.getDay()];
									$('#giornoP').addOption(d[m], thisDay + " " + d[m],false);
								}
								else { 
									if (h==0) {
										$('#durata').addOption(d[m], d[m] + " Giorni",false);
									}
								}
							}
						}
					}
				}
			}
		}
	});

	$('#form_vacanze').submit(function(){
		
		if (document.getElementById('PARTENZE').style.display != 'none') {
			$('#descrAptPart').val(document.form_vacanze.aptPar.options[document.form_vacanze.aptPar.selectedIndex].text);
		}			

		$('#form_check').remove();
		var messaggio = '<p>Errori nella compilazione del form:</p>';
		
		var adt = parseInt($('#adulti').val());
		var und = parseInt($('#under18').val());
		var undeu1 = parseInt($('#eu1').val());
		var undeu2 = parseInt($('#eu2').val());
		var tipoVacanzaSel=false;

		if (document.getElementById('tipo_vacanza').style.display == 'block' && $('#tipoVacanza').val() == '')
			messaggio += '</BR> - Devi selezionare il tipo di vacanza.';
		else if ($('#tipoVacanza').val() != 'VILLAGGIO')
			tipoVacanzaSel=true;

		if (document.getElementById('div_budget').style.display == 'block' && $('#budget').val() == '')
			messaggio += '</BR> - Devi selezionare il budget.';

		if ($('#destinazione').val() != '' && $('#keyd').val() == '') 
			messaggio += '</BR> - Devi selezionare una localit&agrave; di destinazione valida.';

		if ($('#destinazione').val() == '') 
			messaggio += '</BR> - Devi selezionare la localit&agrave; di destinazione.';

		//PROVVISORIO
		/*if (document.getElementById('PARTENZE').style.display == 'none') {
			if ($('#destinazione').val() != '' && ($('#liv2').val() == null || $('#liv2').val() == '-1') ) 
				messaggio += '</BR> - Devi selezionare almeno la Zona.';
		}*/
		
		if (document.getElementById('PARTENZE').style.display == 'none' && $('#giornoP').val() == '') {
			return false;
		}
		
		if (document.getElementById('PARTENZE').style.display == 'block') {
			if ( $('#aptPar').val() == '') 
				messaggio += '</BR> - Inserire l\'Aeroporto di partenza.';
			if ( $('#giornoP').val() == '') 
				messaggio += '</BR> - Inserire il Giorno di partenza.';
			if ( $('#meseAnnoP').val() == '') 
				messaggio += '</BR> - Inserire il Mese e l\'Anno di partenza.';
			if ( $('#durata').val() == '') 
				messaggio += '</BR> - Inserire la durata della vacanza.';		
			if ( adt+und > 4)
				messaggio += '</BR> - I viaggiatori non possono essere pi&ugrave; di 4.';
			if ( und==2 && (undeu1 < 0 || undeu2 < 0)) {
				if (undeu1 < 0 && undeu2 < 0) {
					messaggio += '</BR> - Inserire le et&agrave; mancanti.';
				} else {
					messaggio += '</BR> - Inserire l\'et&agrave; mancante.';
				}
			}
			if ( und==1 && undeu1 < 0)
				messaggio += '</BR> - Inserire l\'et&agrave; mancante.';				
		}
		
		if (messaggio == '<p>Errori nella compilazione del form:</p>') {
			messaggio = '<p>Form incompleto:</p>';
		}

		if (messaggio == '<p>Form incompleto:</p>') {
			messaggio = '<p>Form incompleto:</p>';

			var descLiv = document.form_vacanze.descliv1.value;
			if (descLiv.charAt(0) != '.' && descLiv != '' && descLiv != null) {
				messaggio += '</BR> - Devi selezionare uno Stato di destinazione.';
			}
		}
		if (messaggio == '<p>Form incompleto:</p>') {
			messaggio = '<p>Form incompleto:</p>';
			
			//La zona è di destinazione è obbligatoria se non ho selezionato il brand

			if (document.form_vacanze.liv2 != undefined && !tipoVacanzaSel) 
			{
				var numOptLiv2 = document.form_vacanze.liv2.options.length;
				if (numOptLiv2 > 0 && document.form_vacanze.liv2.selectedIndex == 0)
					messaggio += '</BR> - Devi selezionare una Zona di destinazione.';
			}
		}

		if (messaggio == '<p>Form incompleto:</p>'){
		
			//Gestendo il scegli = -1 questo valore non posso passarlo alla form sucessiva 
			if ( und==0) {
				$('#eu1').selectOptions("0");
				$('#eu2').selectOptions("0");
			} else {
				if (und==1) {
					$('#eu2').selectOptions("0");
				}
			}	
			
			createCookie("provenienzaNavigatore","Motore",1); 			
			
		    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_vacanze');
			return false;
		}
	});
	
	$('#startProgress').click(function(){
	  var obj = $('#divContDXSearch');
	
	  if ($('#divContentDX').attr('id') !=undefined)
	   jQuery($('#divContentDX')).attr('style','display: none;'); 
	 
	  if ($('#footer').attr('moveFooter') != undefined){
	   var offset = obj.offset();
	   var h = obj.outerHeight();
	   
	   jQuery($('#footer')).attr('style','position: absolute; top:'+ (220 + h) +'px;');    
	  }
	  jQuery(obj).attr('style','display: "";'); 
	  //jQuery(obj).attr('style','display: ;');
	  document.body.scrollTop=0;
	 });	
	
	// 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': '#progressBarVGTO'
		}
		
		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;
	}  
	
	$('#adulti').change(function()
			{
				var $this = $(this);
				var n = + $this.val();
				
				if (n==3 || n==4)
				{
					optionUnder18=$('#under18').val();
					$("#under18 option[value=2]").remove(); //rimuovo la possibilità di selezionare 2 under18
					$('#eta_under_2').hide(); 
					$('#eu2').selectOptions("-1");
					if (n==4)
					{
						$("#under18 option[value=1]").remove(); //rimuovo la possibilità di selezionare under18
						$('#eta_under_1').hide();
						$('#eu1').selectOptions("-1");
					}
					else
					{
						if (optionUnder18==2)
							$('#under18').selectOptions("1");
						len=$('#under18 option').length;
						if (len==1)
						{
							$('#under18').append('<option value="1">1</option>');
						}
							
					}
				}	
				else
				{
					//devo eventualmente ripristinare le opzioni rimosse
					len=$('#under18 option').length;
					if (len==1)
					{
						$('#under18').append('<option value="1">1</option>');
						$('#under18').append('<option value="2">2</option>');
					}
					if (len==2)
					{
						$('#under18').append('<option value="2">2</option>');
					}
				}	
			});

	$('#under18').change(function(){
		var $this = $(this);
		var n = + $this.val();
		if (n == 0) {
			$('#eta_under_1').hide();
			$('#eta_under_2').hide();
			$('#eu1').selectOptions("-1");
			$('#eu2').selectOptions("-1");
		} else if (n==1) {
			$('#eta_under_1').show();
			$('#eta_under_2').hide();
			$('#eu2').selectOptions("-1");
		} else {
			$('#eta_under_1').show();
			$('#eta_under_2').show();
		}
	});
	
	//Gestendo il scegli = -1 questo valore viene risettato a -1 quando ritorno indietro col browser         
	var backunder18 = parseInt($('#under18').val());
	
	if ( backunder18==0) {
		$('#eu1').selectOptions("-1");
		$('#eu2').selectOptions("-1");
	} else {
		if (backunder18==1) {
			$('#eu2').selectOptions("-1");
		}
	}	
});
