function html_entity_decode(str) {
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}

var options, a;
jQuery(function(){
	
	var onAutocompleteSelect = function(value, data) {
		var c = $('#citta').val();
		$('#citta').val(html_entity_decode(c.substr(0,(c.length-5))));
		$("input#prov").val(value.substr(-3, 2));
		$.get(
			'inc/autocomplete_comuni.php?cap=' + value ,
			function(txt){
				$("input#cap").val(txt);
			}
		)
		$.get(
			'inc/autocomplete_comuni.php?regione=' + $("#prov").val() ,
			function(txt){
				$("#regione").val(txt);
			}
		)
    	//alert(value);
    }
	
    options = {
        serviceUrl: 'inc/autocomplete_comuni.php',
		onSelect: onAutocompleteSelect
    };
	
    a = $('#citta').autocomplete(options);
	
});

//$( "#citta" ).autocomplete({
//			source: function( request, response ) {
//				$.ajax({
//					url: "js/ajax/search_nato.php",
//					dataType: "json",
//					data: {
//						l: 6,
//						c: request.term
//					},
//					success: function( data ) {
//						response( $.map( data, function( item ) {
//							return {
//								label: item.com+", "+item.prov,
//								value: item.com,
//								prov: item.prov,
//                                                                cap: item.cap
//							}
//						}));
//					}
//				});
//			},
//			minLength: 1,
//			select: function( event, ui ) {
//			  $('input#prov').val(ui.item.prov);
//                          $('input#cap').val(ui.item.cap);
//			},
//			open: function() {
//				$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
//			},
//			close: function() {
//				$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
//			}
//		});

