function autocomplete_pal(){
	 $.post("index.php?model=palinsesto_tag&show=all_events","",
	            function(data){  
	            	   	
				   var eventi_txt=new Array();
	               var split = data.split("@");
	               for(var j=0; j<split.length;j++){	               
	               		eventi_txt[j]={descrizione:split[j]};	               		
		            } 	    	
		    	
		            $("#tag_pal").autocomplete(eventi_txt, {
					formatItem: function(row, i, max) {
						return row.descrizione;
					},
					formatMatch: function(row, i, max) {
						return row.descrizione;
					},
					formatResult: function(row) {
						return row.descrizione;
					}

			}); 
        });	
      //  document.getElementById("tag_pal").focus();  
}
$(document).bind('keydown',"return",  function(){ 

	             
	               
	var pal = $('#tag_pal').val();
	var avv = $('#tag_avv').val();			
	var odd = $('#tag_odd').val();
	if((pal!='') && (avv!='')){
	
    $("#tag_odd").flushCache();
		
	var suggerimenti;
		   $.post("index.php?model=palinsesto_tag&show=event",{pal_sel: pal, avv_sel: avv},
	            function(data){
	            	var splitted = data.split("@");
	            	var descrizione_ev = splitted[0];
	            	var data_ev = splitted[1];
	            	var id_ev = splitted[2];
	            	var codpal_ev = splitted[3];
	            	var codavv_ev = splitted[4];
					
	            	if($('#page_id').val()!='TAG'){
	            		show_tag(descrizione_ev, id_ev, codpal_ev, codavv_ev);   
	            	}else{
	            		show_title(descrizione_ev);
		               	show_quote(id_ev,codpal_ev,codavv_ev,descrizione_ev,data_ev);        
	            	}  		
	            		            	
	            	$("#descr_eve_hid").val(descrizione_ev);
	            	$("#data_eve_hid").val(data_ev);
	            	$("#id_eve_hid").val(id_ev);
	            	$("#pal_hid").val(codpal_ev);
	            	$("#avv_hid").val(codavv_ev);
	            	$("#descr_eve").html(descrizione_ev+" "+data_ev);           
	            }
		  );
		   
	     $.post("index.php?model=palinsesto_tag&show=odds",{pal_sel: pal, avv_sel: avv},
	            function(data2){     	             
	               var finale=new Array();
	              // alert(data2);
	              
	               var splitted = data2.split("@");
	               var index=0;
	               for(var j=0; j<splitted.length;j++){
	               		var el=splitted[j].split("|");
	               		var tipo_val=el[0]; 
	               		var quota_val=el[1];
	               		var id_val=el[2];
	               		var gruppo_val=el[3];
	               		var cat1_legame = el[4];
	               		var cat2_legame = el[5];
	               		var eventi_legame = el[6];
	               		var cod_esi = el[7];
	               		var cod_eve = el[8];
	               		var categoria = el[9];
	               		var id_categoria = el[10];
	               		var handicap = el[11];
	               		
	               	finale[index]={tipo:tipo_val,quota:quota_val,id:id_val,gruppo:gruppo_val,cat1_legame:cat1_legame, cat2_legame:cat2_legame, eventi_legame:eventi_legame, codesi:cod_esi, codeve:cod_eve, categoria:categoria, id_categoria:id_categoria, handicap:handicap,tipo_replace:tipo_val};
	               		if((tipo_val.substr(0,1)=='x')||(tipo_val.substr(0,1)=='X')){
		               		if(tipo_val.substr(0,1)=='x')
								tipo_val_new=tipo_val.replace('x','*');
							else
								tipo_val_new=tipo_val.replace('X','*');
		               		index=index+1;
							finale[index]={tipo:tipo_val,quota:quota_val,id:id_val,gruppo:gruppo_val,cat1_legame:cat1_legame, cat2_legame:cat2_legame, eventi_legame:eventi_legame, codesi:cod_esi, codeve:cod_eve, categoria:categoria, id_categoria:id_categoria, handicap:handicap, tipo_replace:tipo_val_new};
	               		}
	               		index++; 
	               }

	                $("#tag_odd").autocomplete(finale, {
	                	minChars: 1,
	                	matchContains: false,
	                	selectFirst: true, 
						formatItem: function(row, i, max) {
							var tipologia=row.tipo_replace;
							if(tipologia.substr(0,1)=='*') tipologia=tipologia.replace('*','X');
							return  "<strong>"+converti_tipo(tipologia) + "</strong> [" + row.quota + "]";
						},
						formatMatch: function(row, i, max) {
							var tipologia=row.tipo;
							if (row.tipo_replace==row.tipo) {
								
							} 
							else {
								tipologia=row.tipo_replace;
							}
						
							return converti_tipo(tipologia) + "@" + row.quota+"@"+row.id+"@"+row.gruppo+"@"+row.cat1_legame+"@"+row.cat2_legame+"@"+row.eventi_legame+"@"+row.codesi+"@"+row.codeve+"@"+row.categoria+"@"+row.id_categoria+"@"+row.handicap;
						},
						formatResult: function(row) {
							return converti_tipo(row.tipo)+" ["+row.quota+"]";
						}		
					});    
	                $('#tag_odd').focus();
	            }
		  );   
		 $("#tag_odd").result(findValueCallback).next().click(function() {
			$(this).prev().search();
		}
	);   
  }
  else{
	  	if  ( (pal=='') ){	
	  	document.getElementById('tag_pal').focus();	
	  	} else{ 
	  	document.getElementById('tag_avv').focus();	  		
	  	}
  }
}); 