var isdebug=false;

$(document).ready(function(){
	if(GetUrlParam("locOrario")!=''){
		loadSelectLocalita();
		if(GetUrlParam("tab")==1){
			$("#formOrarioPartenze select[name=locOrario] option[value='"+GetUrlParam("locOrario")+"']").attr("selected", "selected");
			searchOrarioPartenze(); 
		}
		if(GetUrlParam("tab")==2){
		$("#formOrarioArrivi select[name=locOrario] option[value='"+GetUrlParam("locOrario")+"']").attr("selected", "selected");
			$('.box_tab_tab li').eq(1).addClass('on');
			$('.box_tab_tab li').eq(0).removeClass('on');
			$(".tab_first_content").hide();
			$(".tab_second_content").show();
				searchOrarioArrivi();
			}	
		}
	else if($('#resultOrarioGenerale').is('div')){
		//template pagina co3
		loadSelectLocalita();
	}	
		   
	if($('.voli_co3').is('iframe')){
		//passa i parametri al link se in get nella pagina ed  aggiunge la lingua della pagina per la chiamata nell'iframe
		//setLinkIframe();
	}
	 if($("#searchDestination").is("input")){
		  $('#btnReset').click(function(){	
				$("#searchDestination").val("");
			 	$("#searchDestinationCode").val("");
			 	$("#searchFly").val("");
			 	$("#searchH").val("");
					findPartenze("","");
				  loadAutocompletePartenzeDestination();
		    	loadAutocompletePartenzeFly();
					writeAggiornamentiPartenze();
						return false;
			});
		}
	
	
	if($('#scriptiframePartenzetable').is('div')){
		//template pagina iframe tabella
		getPartenzeTableIframe();
	}
	/////////////////GESTIONE VOLI IN PARTENZA
	if($('#resultPartenze').is('div')){
	//template pagina risultati ricerca
		getPartenzeResult();
	}
	if($('.scriptiframePartenzelist').is('div')){
	//template pagina iframe accordion
		getPartenzeAccordionIframe();
	}
	
	if($("#searchFrom").is("input")){
		$('#btnReset').click(function(){		
			$("#searchFrom").val("");
		 $("#searchFromCode").val("");
		 $("#searchFly").val("");
		 $("#searchH").val("");
				findArrivi("","");
			  loadAutocompleteArriviFrom();
	    	loadAutocompleteArriviFly();
				writeAggiornamentiArrivi();
					return false;
		});  
  }
	////////////////GESTIONE VOLI IN ARRIVO
	if($('#resultArrivi').is('div')){
		//template pagina risultati ricerca
		getArriviResult();
	}
	if($('#scriptiframeArrivitable').is('div')){
		//template pagina iframe tabella
		getArriviTableIframe();
	}
	if($('.scriptiframeArrivilist').is('div')){
		//template pagina iframe accordion
		getArriviAccordionIframe();
	}


});

//end ready!

function setLinkIframe(){
	$('.voli_co3').each(function(){
		var linkiframe=$(this).attr("src");
		$(this).attr("src",linkiframe+"&lang="+langXml);		
		});

}



function getPartenzeAccordionIframe(){
			iRow=rowFrame;
			loadAutocompletePartenzeDestination();
    	loadAutocompletePartenzeFly();
    	findPartenze("loadData","");
}



function getPartenzeTableIframe(){
			iRow=rowFrame;
			loadAutocompletePartenzeDestination();
    	loadAutocompletePartenzeFly();
    	findPartenze("","");
}




function getPartenzeResult(){
	 langXml=langXml;
	//data e ora ultimo aggiornamento
	writeAggiornamentiPartenze();
	//aggancia l'evento onclick sul bottone aggiorna
	$('#aggUpdate').click(function(){
			findPartenze("","");
		  loadAutocompletePartenzeDestination();
    	loadAutocompletePartenzeFly();
			writeAggiornamentiPartenze();
			return false;
	});
	
	var  sH=GetUrlParam("searchH");
		if(sH!=''){
			$("#searchH").val(sH);
		}
	var  sFly=GetUrlParam("searchFly");
	if(sFly!=''){
			$("#searchFly").val(sFly);
	}
	//load tabella risultati eventualmente con parametri
	var sDest =GetUrlParam("searchDestinationCode");
	if(sDest==''){
	  sDest=GetUrlParam("searchDestination");
		}
	var  sName=GetUrlParam("searchName");
		if(sDest!=''){
		 		$("#searchDestinationCode").val(sDest);
		 		$.ajax({
					type: "GET",
					url: "/sagat/filterPartenze?q="+sDest+"&operation=code&debug="+isdebug+"&marquee="+marquee,
					dataType: "text",
					cache: false,
					success: function(msg){
						if($.trim(msg)!=''){
						$("#searchDestination").val(msg);
						}else{
						$("#searchDestination").val(sName);	
							}
					},complete: function(){
							loadAutocompletePartenzeDestination();
					  loadAutocompletePartenzeFly();
					  findPartenze("","");		
						}
				});
		}else{
			loadAutocompletePartenzeDestination();
		  loadAutocompletePartenzeFly();
		  findPartenze("","");		
			}
}



function writeAggiornamentiPartenze(){
		       		$.ajax({
							type: "GET",
							url: '/sagat/filterPartenze?debug='+isdebug+'&lang='+langXml+'&operation=hour'+'&marquee='+marquee,
							dataType: "text",
							cache: false,
							success: function(msg){
							if(msg.indexOf("notAvaiable")<0){
								var aggiornamenti=msg.split("-");
								$("#aggHour").html(aggiornamenti[1]);
								$("#aggDate").html(aggiornamenti[0]);
							}
					}
			});
}

function findPartenze(li,labelD) {
		if($("#searchDestination").val()==""){		
				$("#searchDestinationCode").val("");
			}
		var destDDD=$("#searchDestinationCode").val();
		if(operationVoli=='result'){
       	var percorso='/sagat/filterPartenze?mode=1&debug='+isdebug+'&lang='+langXml+'&operation=list&searchFly='+$("#searchFly").val()+'&searchDestination='+destDDD+'&searchH='+$("#searchH").val()+'&marquee='+marquee;
       		$.ajax({
					type: "GET",
					url: percorso,
					dataType: "text",
					cache: false,
					success: function(msg){
						$(".result").html(msg);
						if(msg.indexOf("notAvaiable")>=0){
 							 $("#updateresult").hide();
								$(".flight_content").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
								
						}
						else if(msg.indexOf("numberResult")>0){
									var numberRows=parseInt($("#numberResult").text());
									var classRows=$("#numberResult").attr("class");
									if(numberRows<=0){
											 		if(numberRows==0)
											 		$(".result").append("<tr class=\""+classRows+"\"><td colspan=\"6\" class=\"empty first\">"+txtnoResult+"</td><td colspan=\"2\" class=\"last\"></td></tr>");
											 		else if(numberRows<0){
											 				$(".result").append("<tr class=\""+classRows+"\"><td colspan=\"6\" class=\"empty first\">"+txtnoVoloPartenza+"</td><td colspan=\"2\" class=\"last\"></td></tr>");
											 					numberRows=0;
											 			}
										}
						}
							else{//generic error
								$("#updateresult").hide();
								$(".flight_content").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
						}
				}
					,complete: function(){				
					}
			});
		}else if(operationVoli=='table'){
				var hasResult=false;
				var hasAfter=false;
		       	var percorso='/sagat/filterPartenze?debug='+isdebug+'&lang='+langXml+'&row='+iRow+'&operation=list&searchFly='+$("#searchFly").val()+'&searchDestination='+destDDD+'&searchH='+$("#searchH").val()+'&marquee='+marquee;
		       		$.ajax({
							type: "GET",
							url: percorso,
							dataType: "text",
							cache: false,
							success: function(msg){
								$(".result").html(msg);
								if(msg.indexOf("notAvaiable")>=0){
										$(".flight_content").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
								}
								else if(msg.indexOf("numberResult")>0){
											var numberRows=parseInt($("#numberResult").text());
											var classRows=$("#numberResult").attr("class");
											var hasResulttxt=$("#hasResult").text();
											var aftertxt=$("#after").text();
											
											if(hasResulttxt=='true'){
												hasResult=true;
											}
											if(aftertxt=='true'){
												hasAfter=true;
											}

											if(numberRows<=0){
													if(numberRows==0){
													 		$(".result").append("<tr class=\""+classRows+"\"><td colspan=\"5\" class=\"empty\">"+txtnoResult+"</td></tr>");
													 	}else if(numberRows<0){
													 		
													 			$(".result").append("<tr class=\""+classRows+"\"><td colspan=\"5\" class=\"empty\">"+txtnoVoloPartenza+"</td></tr>");
													 			numberRows=0;
													 		}
													 		numberRows=numberRows+1;
												}
											for(r=numberRows;r<iRow;r++){
												if(classRows=='odd'){
													classRows="even";
													}else{
													classRows="odd";
														}
											 		$(".result").append("<tr class=\""+classRows+" emptyrow\"><td colspan=\"5\" class=\"empty\"></td></tr>");
											}
										}
											else{//generic error
												$(".flight_content").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
											}
						}
							,complete: function(){
								if(hasResult && !hasAfter){
										$(".notEmpty:last").html("<td colspan=\"5\" class=\"showAll\" >"+txtShowAll+"</td>")
									}
								if(hasAfter && !hasResult){
									if($(".emptyrow").is("tr")){
											$(".emptyrow:last").html("<td colspan=\"5\" class=\"tomorrow\" ><img src=\"/www.aeroportoditorino.it/img/day_after.gif\"/>&nbsp;"+txtTomorrow+"</td>");
										}else{
											$(".notEmpty:last").html("<td colspan=\"3\" class=\"showtomorrowleft\" ><img src=\"/www.aeroportoditorino.it/img/day_after.gif\"/>&nbsp;"+txtTomorrow+"</td><td colspan=\"2\" class=\"showtomorrowright\" >"+txtShowAll+"</td>")	
										}
										
									}
								if(hasAfter && hasResult){								
									if($(".result").find("img").size()>1){
										$(".notEmpty:last").html("<td colspan=\"3\" class=\"showtomorrowleft\" ><img src=\"/www.aeroportoditorino.it/img/day_after.gif\"/>&nbsp;"+txtTomorrow+"</td><td colspan=\"2\" class=\"showtomorrowright\" >"+txtShowAll+"</td>")
									}else{
											$(".notEmpty:last").html("<td colspan=\"5\" class=\"showAll\" >"+txtShowAll+"</td>")
										}
									}
									
									
									
									var linkres=$("#linktable").attr("href");
									if(linkres.indexOf(".jsp")>=0){
											linkres=linkres.substring(0,linkres.indexOf(".jsp"))+".jsp?searchDestination="+destDDD+"&searchH="+$("#searchH").val()+"&searchFly="+$("#searchFly").val();
									}else{
										linkres=linkres.substring(0,linkres.indexOf(".html"))+".html?searchDestination="+destDDD+"&searchH="+$("#searchH").val()+"&searchFly="+$("#searchFly").val();
									}
									$("#linktable").attr("href",linkres);
									handleStatusScroll(".partenzetable ");
							}
					});
		}else if(operationVoli=='list' && li=='loadData'){
		       	var percorso='/sagat/filterPartenze?debug='+isdebug+'&mode=2&lang='+langXml+'&row='+iRow+'&operation=list&searchFly='+$("#searchFly").val()+'&searchDestination='+destDDD+'&searchH='+$("#searchH").val()+'&marquee='+marquee;
		       		$.ajax({
							type: "GET",
							url: percorso,
							dataType: "text",
							cache: false,
							success: function(msg){		
								$(".listacarouseliframe").html(msg);
								if(msg.indexOf("notAvaiable")>=0){
										$("#departureInt_wrap").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
								}
								else if(msg.indexOf("numberResult")>0){
											var numberRows=parseInt($("#numberResult").text());
											var classRows=$("#numberResult").attr("class");
										
											if(numberRows<=0){
										
													if(numberRows==0){
													 		$(".listacarouseliframe").append("<p class=\""+classRows+"\"><p>"+txtnoResult+"</p></tr>");
													 	} else		if(numberRows<0){
													 		
													 		$(".listacarouseliframe").parent().append("<p class=\""+classRows+"\"><p>"+txtnoVoloPartenza+"</p></tr>");
													 	
													 	}
												}
										}
											else{//generic error
												$("#departureInt_wrap").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
											}
								if(msg.indexOf('li style')>=0)	{		
									msg=msg.substring(0,msg.indexOf('li style')-1);
								}
								$(".listacarouseliframe").html(msg);
								handleFlightCarousel();										
						}
							,complete: function(){
								
								handleStatusScroll(".listacarouseliframe ");
							}
					});
			}
}

  function selectPartenze(li) {
  	$("#searchDestinationCode").val(li.extra[0]);
    findPartenze("",li.extra[0]);
  }
	function selectPartenzeFly(li) {
      findPartenze("",li.extra[0]);
  }
  function formatPartenze(row)
  {
      return row[0];
  }

function loadAutocompletePartenzeDestination(){
        $("#searchDestination").autocomplete(
		    "/sagat/filterPartenze",
		    {
			    delay:10,
			    minChars:1,
			    matchSubset:1,
			    matchContains:1,
			    cacheLength:1,
			    onItemSelect:selectPartenze,
			    onFindValue:findPartenze,
			    formatItem:formatPartenze,
			    autoFill:true,
			    extraParams : {"searchFly":"||","searchH":"||",lang: langXml,operation:"destination",debug:isdebug,"marquee":marquee}
		    }
	    );
	   }

 function loadAutocompletePartenzeFly(){
        $("#searchFly").autocomplete(
		    "/sagat/filterPartenze",
		    {
			    delay:10,
			    minChars:1,
			    matchSubset:1,
			    matchContains:1,
			    cacheLength:1,
			    onItemSelect:selectPartenzeFly,
			    onFindValue:findPartenze,
			    formatItem:formatPartenze,
			    autoFill:true,
			    extraParams : {"searchDestination":"||","searchH":"||",lang: langXml,operation:"fly",debug:isdebug,"marquee":marquee}
		    }
	    );
}

/////////////////////////fine partenze inizio arrivi
function getArriviAccordionIframe(){
			iRow=rowFrame;
			loadAutocompleteArriviFrom();
    	loadAutocompleteArriviFly();
    	findArrivi("loadData","");
}

function getArriviTableIframe(){
			iRow=rowFrame;
			loadAutocompleteArriviFrom();
    	loadAutocompleteArriviFly();
    	findArrivi("","");
}

function getArriviResult(){
	 langXml=langXml;
	//data e ora ultimo aggiornamento
	writeAggiornamentiArrivi();
	//aggancia l'evento onclick sul bottone aggiorna
	$('#aggUpdate').click(function(){
			findArrivi("","");
		  loadAutocompleteArriviFrom();
    	loadAutocompleteArriviFly();
			writeAggiornamentiArrivi();
				return false;
	});
	//load tabella risultati eventualmente con parametri
	var  sDest=GetUrlParam("searchFromCode");
	if(sDest==''){
		sDest=GetUrlParam("searchFrom");
		}
	var  sName=GetUrlParam("searchName");	
	var  sH=GetUrlParam("searchH");
		if(sH!=''){
			$("#searchH").val(sH);
		}
	var  sFly=GetUrlParam("searchFly");
	if(sFly!=''){
			$("#searchFly").val(sFly);
	}
		if(sDest!=''){
		 		$("#searchFromCode").val(sDest); 	
		 		$.ajax({
					type: "GET",
					url: "/sagat/filterArrivi?q="+sDest+"&operation=code&debug="+isdebug+"&marquee="+marquee,
					dataType: "text",
					cache: false,
					success: function(msg){
					if($.trim(msg)!=''){
						$("#searchFrom").val(msg);
					}else{
						$("#searchFrom").val(sName);	
					
					}
				},complete: function(){
				loadAutocompleteArriviFrom();
				  loadAutocompleteArriviFly();
				  findArrivi("","");	
						}
				});
		}
	else{
	loadAutocompleteArriviFrom();
  loadAutocompleteArriviFly();
  findArrivi("","");	
		}
}

function renderLinkwithParameteIframe(){
	$('.voli_co3').each(function(){
			var linkiframe="";
			var strhost="";
			strhost=""+window.location.search;
			if(strhost.length>0){
				strhost=strhost.substring(1);
			}
			var linkiframe=$(this).attr("src");
			if(linkiframe.indexOf(".jsp?")>0){
				strhost+=linkiframe.substring(linkiframe.indexOf(".jsp?")+5);
				linkiframe=linkiframe.substring(0,linkiframe.indexOf(".jsp?")+4);
			}
			else{
			}
			$(this).attr("src",linkiframe+"?lang="+langXml+"&"+strhost);
		});
}




function writeAggiornamentiArrivi(){
		       		$.ajax({
							type: "GET",
							url: '/sagat/filterArrivi?debug='+isdebug+'&lang='+langXml+'&operation=hour&marquee='+marquee,
							dataType: "text",
							cache: false,
							success: function(msg){
							if(msg.indexOf("notAvaiable")<0){
								var aggiornamenti=msg.split("-");
								$("#aggHour").html(aggiornamenti[1]);
								$("#aggDate").html(aggiornamenti[0]);
							}
					}
			});
}

function findArrivi(li,labelFrom) {

			if($("#searchFrom").val()==""){
			$("#searchFromCode").val("");
			}
		var fromDDD=$("#searchFromCode").val();		
		if(operationVoli=='result'){
       	var percorso='/sagat/filterArrivi?mode=1&debug='+isdebug+'&lang='+langXml+'&operation=list&searchFly='+$("#searchFly").val()+'&searchFrom='+fromDDD+'&searchH='+$("#searchH").val()+'&marquee='+marquee;
       		$.ajax({
					type: "GET",
					url: percorso,
					dataType: "text",
					cache: false,
					success: function(msg){
						$(".result").html(msg);
						if(msg.indexOf("notAvaiable")>=0){
							$("#updateresult").hide();
								$(".flight_content").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
						}
						else if(msg.indexOf("numberResult")>0){
									var numberRows=parseInt($("#numberResult").text());
									var classRows=$("#numberResult").attr("class");

										if(numberRows<=0){
											 		if(numberRows==0)
											 		$(".result").append("<tr class=\""+classRows+"\"><td colspan=\"6\" class=\"empty first\">"+txtnoResult+"</td><td colspan=\"2\" class=\"last\"></td></tr>");
											 		else if(numberRows<0){
											 				$(".result").append("<tr class=\""+classRows+"\"><td colspan=\"6\" class=\"empty first\">"+txtnoVoloArrivo+"</td><td colspan=\"2\" class=\"last\"></td></tr>");
											 					numberRows=0;
											 			}
										}
										
						}
							else{//generic error
								$("#updateresult").hide();
								$(".flight_content").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
						}
				}
					,complete: function(){
					}
			});

		}else if(operationVoli=='table'){
			var hasResult=false;
					var hasAfter=false;
		       	var percorso='/sagat/filterArrivi?debug='+isdebug+'&lang='+langXml+'&row='+iRow+'&operation=list&searchFly='+$("#searchFly").val()+'&searchFrom='+fromDDD+'&searchH='+$("#searchH").val()+'&marquee='+marquee;
		       		$.ajax({
							type: "GET",
							url: percorso,
							dataType: "text",
							cache: false,
							success: function(msg){
								$(".result").html(msg);
								if(msg.indexOf("notAvaiable")>=0){
										$(".flight_content").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);

								}
								else if(msg.indexOf("numberResult")>0){
											var numberRows=parseInt($("#numberResult").text());
											var classRows=$("#numberResult").attr("class");
											var hasResulttxt=$("#hasResult").text();
											var aftertxt=$("#after").text();
											if(hasResulttxt=='true'){
												hasResult=true;
											}
											if(aftertxt=='true'){
												hasAfter=true;
											}
											if(numberRows<=0){
												if(numberRows==0){
													$(".result").append("<tr class=\""+classRows+"\"><td colspan=\"5\" class=\"empty\">"+txtnoResult+"</td></tr>");
												}else if(numberRows<0){
													 		$(".result").append("<tr class=\""+classRows+"\"><td colspan=\"5\" class=\"empty\">"+txtnoVoloArrivo+"</td></tr>");
													 		numberRows=0;
													 	}
													 		numberRows=numberRows+1;
												}
											for(r=numberRows;r<iRow;r++){
												if(classRows=='odd'){
													classRows="even"
													}else{
													classRows="odd"
														}
											 		$(".result").append("<tr class=\""+classRows+" emptyrow\"><td colspan=\"5\" class=\"empty\"></td></tr>");
											}
										}
											else{//generic error
												$(".flight_content").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
											}
						}
							,complete: function(){
				
							  if(hasResult && !hasAfter){
										$(".notEmpty:last").html("<td colspan=\"5\" class=\"showAll\" >"+txtShowAll+"</td>")
									}
								if(hasAfter && !hasResult){
									if($(".emptyrow").is("tr")){
											$(".emptyrow:last").html("<td colspan=\"5\" class=\"tomorrow\" ><img src=\"/www.aeroportoditorino.it/img/day_after.gif\"/>&nbsp;"+txtTomorrow+"</td>");
										}else{
											$(".notEmpty:last").html("<td colspan=\"3\" class=\"showtomorrowleft\" ><img src=\"/www.aeroportoditorino.it/img/day_after.gif\"/>&nbsp;"+txtTomorrow+"</td><td colspan=\"2\" class=\"showtomorrowright\" >"+txtShowAll+"</td>")	
										}
										
									}
								if(hasAfter && hasResult){								
									if($(".result").find("img").size()>1){
										$(".notEmpty:last").html("<td colspan=\"3\" class=\"showtomorrowleft\" ><img src=\"/www.aeroportoditorino.it/img/day_after.gif\"/>&nbsp;"+txtTomorrow+"</td><td colspan=\"2\" class=\"showtomorrowright\" >"+txtShowAll+"</td>")
									}else{
											$(".notEmpty:last").html("<td colspan=\"5\" class=\"showAll\" >"+txtShowAll+"</td>")
										}
									}
									
									
									var linkres=$("#linktable").attr("href");
									if(linkres.indexOf(".jsp")>=0){
											linkres=linkres.substring(0,linkres.indexOf(".jsp"))+".jsp?searchFrom="+fromDDD+"&searchH="+$("#searchH").val()+"&searchFly="+$("#searchFly").val();
									}else{
										linkres=linkres.substring(0,linkres.indexOf(".html"))+".html?searchFrom="+fromDDD+"&searchH="+$("#searchH").val()+"&searchFly="+$("#searchFly").val();
									}
									$("#linktable").attr("href",linkres);
									handleStatusScroll(".arrivitable ");
							}
					});
		}else if(operationVoli=='list' && li=='loadData'){
		       	var percorso='/sagat/filterArrivi?debug='+isdebug+'&mode=2&lang='+langXml+'&row='+iRow+'&operation=list&searchFly='+$("#searchFly").val()+'&searchFrom='+fromDDD+'&searchH='+$("#searchH").val()+'&marquee='+marquee;
		       		$.ajax({
							type: "GET",
							url: percorso,
							dataType: "text",
							cache: false,
							success: function(msg){
								$(".listacarouseliframe").html(msg)						;
								if(msg.indexOf("notAvaiable")>=0){
											$("#departureInt_wrap").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
								}
								else if(msg.indexOf("numberResult")>0){
											var numberRows=parseInt($("#numberResult").text());
											var classRows=$("#numberResult").attr("class");
											
											if(numberRows==0){
													 		$(".listacarouseliframe").append("<p class=\""+classRows+"\"><p>"+txtnoResult+"</p></tr>");
													 	} else		if(numberRows<0){
													 		$(".listacarouseliframe").parent().append("<p class=\""+classRows+"\"><p>"+txtnoVoloArrivo+"</p></tr>");
													 		numberRows=0;
													 	}
											
									}
									else{//generic error
												$("#departureInt_wrap").html('<h3>'+$(".flight_content h3").html()+'</h3>'+txtUnAvaiable);
									}
								if(msg.indexOf('li style')>=0)	{		
									msg=msg.substring(0,msg.indexOf('li style')-1);
								}
								$(".listacarouseliframe").html(msg);
									handleFlightCarousel();
						}
							,complete: function(){
						handleStatusScroll(".listacarouseliframe ");
							}
					});
			}
}


function selectArrivi(li) {
    $("#searchFromCode").val(li.extra[0]);
	    findArrivi("",li.extra[0]);
}

function formatArrivi(row){
    return row[0];
}

function selectArriviFly(li) {
	    findArrivi("",li.extra[0]);
}

function loadAutocompleteArriviFrom(){
        $("#searchFrom").autocomplete(
		    "/sagat/filterArrivi",
		    {
			    delay:10,
			    minChars:1,
			    matchSubset:1,
			    matchContains:1,
			    cacheLength:1,
			    onItemSelect:selectArrivi,
			    onFindValue:findArrivi,
			    formatItem:formatArrivi,
			    autoFill:true,
			    extraParams : {"searchFly":"||","searchH":"||",lang: langXml,operation:"from",debug:isdebug,"marquee":marquee}
		    }
	    );
	   }

 function loadAutocompleteArriviFly(){
        $("#searchFly").autocomplete(
		    "/sagat/filterArrivi",
		    {
			    delay:10,
			    minChars:1,
			    matchSubset:1,
			    matchContains:1,
			    cacheLength:1,
			    onItemSelect:selectArriviFly,
			    onFindValue:findArrivi,
			    formatItem:formatArrivi,
			    autoFill:true,
			    extraParams : {"searchFrom":"||","searchH":"||",lang: langXml,operation:"fly",debug:isdebug,"marquee":marquee}
		    }
	    );
}
/*
function filterXml(docXml,idform){
	var oLocalita=$('#'+idform+' select[name=locOrario] option:selected').val();
	var oGiorno=$('#'+idform+' select[name=dayOrario] option:selected').val();
	var oOra=$('#'+idform+' select[name=oraOrario] option:selected').val();
	var olineaOrario=$('#'+idform+' input:checkbox[name=lineaOrario]').attr("checked");
	var ocharterOrario=$('#'+idform+' input:checkbox[name=charterOrario]').attr("checked");
	var oflyNationalOrario=$('#'+idform+' input:checkbox[name=flyNationalOrario]').attr("checked");
	var oflyInternational=$('#'+idform+' input:checkbox[name=flyInternationalOrario]').attr("checked");
	
	
	docXml=$(docXml).find("Item");
	
	if(oLocalita!=''){
	    docXml=$(docXml.find("localita:contains('"+oLocalita+"')").parent());
	}
	if(oOra!=''){
		docXml=$(docXml.find("fasciaoraria:contains('"+oOra+"')").parent());		 			       
	}
	if(oGiorno!=''){
		docXml=$(docXml.find("giorni:contains('"+oGiorno+"')").parent());		 			       
	}
	if(ocharterOrario && ! olineaOrario){
 	    docXml=$(docXml.find("charter:contains(True)").parent());
	}
	if(olineaOrario && ! ocharterOrario){
 	    docXml=$(docXml.find("charter:contains(False)").parent());
	}
	if(oflyInternational && !oflyNationalOrario){
  	    docXml=$(docXml.find("internazionale:contains(True)").parent());
	}
	if(oflyNationalOrario && !oflyInternational){
		
   	    docXml=$(docXml.find("internazionale:contains(False)").parent());
   	    alert($(docXml).length);
   	    alert($(docXml).text());
   	    alert($(docXml).eq(0).text());
   	    
	}
	return docXml;
}
*/

function searchOrarioPartenze(){
	$("#tableOrarioPartenze").show();
	$(".resultOrarioPartenze").html("Loading...");
     	$.ajax({
		type: "GET",
		url: '/STORE/XML/PARTENZE/data.xml',
		dataType: "xml",
		success: function(msg){
			var totris=0;
			var oLocalita=$('#formOrarioPartenze select[name=locOrario] option:selected').val();
			var oGiorno=$('#formOrarioPartenze select[name=dayOrario] option:selected').val();
			var oOra=$('#formOrarioPartenze select[name=oraOrario] option:selected').val();
			var olineaOrario=$('#formOrarioPartenze input:checkbox[name=lineaOrario]').attr("checked");
			var ocharterOrario=$('#formOrarioPartenze input:checkbox[name=charterOrario]').attr("checked");
			var oflyNationalOrario=$('#formOrarioPartenze input:checkbox[name=flyNationalOrario]').attr("checked");
			var oflyInternational=$('#formOrarioPartenze input:checkbox[name=flyInternationalOrario]').attr("checked");
				$(".resultOrarioPartenze").html("");
			 $(msg).find("Item").each(function(){
						var html="";
						var zLocalita=$("localita", this).text();
						var zGiorno=$("giorni", this).text();
						var zOra=$("fasciaoraria", this).text();
						var zcharter=$("charter", this).text();
						var zinternazionale=$("internazionale", this).text();


            if (zLocalita.length>0)
              {
              	if(((oLocalita!='' && zLocalita==oLocalita) || oLocalita=='') && 
              		 ((oGiorno!='' && zGiorno.indexOf(oGiorno)>=0) || oGiorno=='') && 
              		 ((oOra!='' && zOra.indexOf(oOra)>=0) || oOra=='') ){
              		 
              		 if((((oflyInternational && zinternazionale.indexOf("True")>=0) || !oflyInternational) && ((oflyNationalOrario && zinternazionale.indexOf("False")>=0) || !oflyNationalOrario)) || oflyNationalOrario && oflyInternational ) {
              		 	
              		 	 if((((ocharterOrario && zcharter.indexOf("True")>=0) || !ocharterOrario) && ((olineaOrario && zcharter.indexOf("False")>=0) || !olineaOrario)) || olineaOrario && ocharterOrario ) {
		            		  	totris++;
				              	var strimnote=$("note", this).find("textblock[language="+langXml+"]").text().replace(/(<([^>]+)>)/ig,"").replace(/^\s+|\s+$/g,"").replace("&#160;","");
				              	var strimcompagnia=$("compagnia", this).find("textblock[language="+langXml+"]").text().replace(/(<([^>]+)>)/ig,"").replace(/^\s+|\s+$/g,"").replace("&#160;","");
				              	var scompagnia=$("compagnia", this).find("textblock[language="+langXml+"]").text();
				              	var snote=$("note", this).find("textblock[language="+langXml+"]").text();
								
								if(zcharter.indexOf("True")>=0){
									html+='<tr class="volo_charter">';
								}else{
									html+='<tr>';
								}
				              	
												html+='<td class="general_timetable_blue">'+zLocalita+'</td>';
												html+='<td class="general_timetable_grey">'+$("volo", this).text()+'</td>';
												html+='<td class="general_timetable_grey"><span class="bold">'+$("ora_partenza", this).text()+'</span></td>';
												html+='<td class="general_timetable_grey">'+$("ora_arrivo", this).text()+'</td>';
												html+='<td>'+zGiorno+'</td>';
												html+='</tr>';
												var compnote=strimnote+strimcompagnia
												if (compnote.length>0){
													if(zcharter.indexOf("True")>=0){
														html+='<tr class="volo_charter">';
													}else{
														html+='<tr>';
													}
													html+='<td class="general_timetable_blue_bottom">'+scompagnia+'</td>';
													html+='<td colspan="4" class="general_timetable_note">'+snote+'</td>';
													html+='</tr>';
												}
												$(".resultOrarioPartenze").append(html);
									}
								}
							}
						}
						
					});
					if(totris==0){
										var html="<tr><td colspan=\"5\" class=\"empty\">"+txtnoResult+"</td></tr>";
									$(".resultOrarioPartenze").append(html);
									}
				},
				error: function(msg)
				{
					alert("errore="+msg);
         }
		});
	return false;
}

function searchOrarioArrivi(){
	$("#tableOrarioArrivi").show();
	$(".resultOrarioArrivi").html("Loading...");
     	$.ajax({
		type: "GET",
		url: '/STORE/XML/ARRIVI/data.xml',
		dataType: "xml",
		/*dataFilter:function(data,type) {
			var messageList=filterXml(data,"formOrarioArrivi");
		return messageList
		},*/
		success: function(msg){
						var totris=0;
			var oLocalita=$('#formOrarioArrivi select[name=locOrario] option:selected').val();
			var oGiorno=$('#formOrarioArrivi select[name=dayOrario] option:selected').val();
			var oOra=$('#formOrarioArrivi select[name=oraOrario] option:selected').val();
			var olineaOrario=$('#formOrarioArrivi input:checkbox[name=lineaOrario]').attr("checked");
			var ocharterOrario=$('#formOrarioArrivi input:checkbox[name=charterOrario]').attr("checked");
			var oflyNationalOrario=$('#formOrarioArrivi input:checkbox[name=flyNationalOrario]').attr("checked");
			var oflyInternational=$('#formOrarioArrivi input:checkbox[name=flyInternationalOrario]').attr("checked");
				$(".resultOrarioArrivi").html("");
						 $(msg).find("Item").each(function(){
						var html="";
						var zLocalita=$("localita", this).text();
						var zGiorno=$("giorni", this).text();
						var zOra=$("fasciaoraria", this).text();
						var zcharter=$("charter", this).text();
						var zinternazionale=$("internazionale", this).text();


            if (zLocalita.length>0)
              {
              	if(((oLocalita!='' && zLocalita==oLocalita) || oLocalita=='') && 
              		 ((oGiorno!='' && zGiorno.indexOf(oGiorno)>=0) || oGiorno=='') && 
              		 ((oOra!='' && zOra.indexOf(oOra)>=0) || oOra=='') ){
              		 
              		 if((((oflyInternational && zinternazionale.indexOf("True")>=0) || !oflyInternational) && ((oflyNationalOrario && zinternazionale.indexOf("False")>=0) || !oflyNationalOrario)) || oflyNationalOrario && oflyInternational ) {
              		 	
              		 	 if((((ocharterOrario && zcharter.indexOf("True")>=0) || !ocharterOrario) && ((olineaOrario && zcharter.indexOf("False")>=0) || !olineaOrario)) || olineaOrario && ocharterOrario ) {
		            		  	totris++;
								if(zcharter.indexOf("True")>=0){
									html+='<tr class="volo_charter">';
								}else{
									html+='<tr>';
								}
								html+='<td class="general_timetable_blue">'+$("localita", this).text()+'</td>';
								html+='<td class="general_timetable_grey">'+$("volo", this).text()+'</td>';
								html+='<td class="general_timetable_grey">'+$("ora_partenza", this).text()+'</td>';
								html+='<td class="general_timetable_grey"><span class="bold">'+$("ora_arrivo", this).text()+'</span></td>';
								html+='<td>'+$("giorni", this).text()+'</td>';
								html+='</tr>';
								var strimnote=$("note", this).find("textblock[language="+langXml+"]").text().replace(/(<([^>]+)>)/ig,"").replace(/^\s+|\s+$/g,"").replace("&#160;","");
              	var strimcompagnia=$("compagnia", this).find("textblock[language="+langXml+"]").text().replace(/(<([^>]+)>)/ig,"").replace(/^\s+|\s+$/g,"").replace("&#160;","");
              	var scompagnia=$("compagnia", this).find("textblock[language="+langXml+"]").text();
              	var snote=$("note", this).find("textblock[language="+langXml+"]").text();
              	var compnote=strimnote+strimcompagnia
								if (compnote.length>0){
									if(zcharter.indexOf("True")>=0){
										html+='<tr class="volo_charter">';
									}else{
										html+='<tr>';
									}
								html+='<td class="general_timetable_blue_bottom">'+scompagnia+'</td>';
								html+='<td colspan="4" class="general_timetable_note">'+snote+'</td>';
								html+='</tr>';				
							}
						$(".resultOrarioArrivi").append(html);
					}
								}
							}
						}
						
					});
												if(totris==0){
						var html="<tr><td colspan=\"5\" class=\"empty\">"+txtnoResult+"</td></tr>";
						$(".resultOrarioArrivi").append(html);
				}
				},
				error: function(msg)
				{
					alert("errore="+msg);
         }
		});
	return false;
}




function loadSelectLocalita(){
			var arrnew=new Array();
     	$.ajax({
		type: "GET",
		url: '/STORE/XML/PARTENZE/data.xml',
		dataType: "xml",
		async: false,
		success: function(msg){
			$(msg).find("Item").each(function(){		
					if(!arrnew.contains($("localita", this).text())){
								arrnew.push($("localita", this).text());
							}
					});
				}
				,complete: function(){
					for (var k=0;k<arrnew.length;k++) {
						$("#locPartenze").append("<option value=\""+arrnew[k]+"\">"+arrnew[k]+"</option>");					
					}
				}
		});
		var arrnewa=new Array();
     	$.ajax({
		type: "GET",
		url: '/STORE/XML/ARRIVI/data.xml',
		dataType: "xml",
		async: false,
		success: function(msg){
				$(msg).find("Item").each(function(){		
					if(!arrnewa.contains($("localita", this).text())){
								arrnewa.push($("localita", this).text());
							}
					});
				},
				complete: function(){
					for (var ka=0;ka<arrnewa.length;ka++) {
						$("#locArrivi").append("<option value=\""+arrnewa[ka]+"\">"+arrnewa[ka]+"</option>");					
					}		
				}
		});
			return false;
}


function handleStatusScroll(divcontainer){ 
	//if(!simplifiedLayout)
		$(divcontainer+'.status span.marquee').marquee();
	/*if(simplifiedLayout == false){
		var chst=23;
		if($(".intpage").is("div")){
			chst=33;
		}
		if(divcontainer.indexOf("listacarouseliframe")>=0){
			chst=17;
		}
		handleMarquee(divcontainer+'.status span',chst);
	}*/
	
}


/*
function handleMarquee(el,num){
		$(el).each(function(){
		var statusLenght = $(this).text().length;
		if(statusLenght>num){
 			$(this).marquee();
		} 
	})
}*/
Array.prototype.contains = function (elementf) {
for (var i = 0; i < this.length; i++) {
	if (this[i] == elementf) {
		return true;
	}
}
return false;
}
