function toggleCheckbox(nome,number,div){
	if ($("#"+nome+"-"+number).attr("checked") == true){
    		$("#"+div).hide();
		}else{
			$("#"+div).show();
		}
	
}


function workprovinciaChange(){
	$('#worknazioneContattocompleteItem').hide();
	if($('#workprovinciaContattocomplete :selected').val()!='XX'){

	$.ajax({
		type: "GET",
		url: "/sip/strutture/provincia/"+$('#workprovinciaContattocomplete :selected').val(),
		dataType: "xml",
		success: function(xml) {
			$('#grpstrutture').html('Strutture della '+$('#workprovinciaContattocomplete :selected').text()+':<br />');
			$(xml).find('strutture').find('struttura').each(function(){
				var id = $(this).attr('id');
				var citta = $( "citta", this ).text();
				var strInfo = {
					id: $( "id", this ).text(),
					nominativo: $( "nominativo", this ).text(),
					address: $( "address", this ).text(),
					cap: $( "cap", this ).text(),
					citta: $( "citta", this ).text(),
					regione: $( "regione", this ).text(),
					nazione: $( "nazione", this ).text()
				};
	
				var div = $('<div class="btnStr" alt="'+id+'">').html('<span style="diaply:block;float:left;width:200px;">'+$( "citta", this ).text()+'</span>'+$( "nominativo", this ).text());
				$('#grpstrutture').append(div);
				div.click(function(){
					$('#strutturaContattocomplete').val(strInfo.id);
					$('#enteContattocomplete').val(strInfo.nominativo);
					$('#workaddressContattocomplete').val(strInfo.address);
					$('#workcapContattocomplete').val(strInfo.cap);
					$('#workcittaContattocomplete').val(strInfo.citta);
					$('#workregioneContattocomplete').val(strInfo.regione);
					$('#worknazioneContattocomplete').val(strInfo.nazione);
					entedisabled(true);	
					$('#grpstrutture').dialog('close');
				}); 
				//$('#grpstrutture').html($(this).attr("alt"));
				//$('#grpstrutture').append('<div class="btnStr" alt="'+id+'" /><span style="diaply:block;float:left;width:200px;">'+$( "citta", this ).text()+'</span>'+$( "nominativo", this ).text()+'</div>');
		
				//$('#grpstrutture').append('<div style="height:100px;"><input type="radio" name="adr" id="adr" value="'+id+'" /><b>'+$( "nominativo", this ).text()+'</b><br />'+$( "address", this ).text()+'<br />'+$( "cap", this ).text()+' '+$( "citta", this ).text()+'</div>');
				//$('#grpstrutture').append('<label ><input style="float:left;" type="radio" name="adr" id="adr" value="'+id+'" /><span style="diaply:block;float:left;width:200px;">'+$( "citta", this ).text()+'</span>'+$( "nominativo", this ).text()+'</label>');
				/*
				var title = $(this).find('title').text();
				var url = $(this).find('url').text();
				$('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
				$(this).find('desc').each(function(){
					var brief = $(this).find('brief').text();
					var long = $(this).find('long').text();
					$('<div class="brief"></div>').html(brief).appendTo('#link_'+id);
					$('<div class="long"></div>').html(long).appendTo('#link_'+id);
				});
				*/
			});
		
		$('#grpstrutture').dialog('open');
		
		//$('div.btnStr').click(function(){alert(99);}); 
		}
	});	
	
	}else{
		noente();
		$('#worknazioneContattocompleteItem').show();	
		entedisabled(false);	
	}
	
}
function workprovinciaLoad(){
	if($('#strutturaContattocomplete').val()>0){
		entedisabled(true);
	}
	if($('#workprovinciaContattocomplete :selected').val()!='XX'){
		$('#worknazioneContattocompleteItem').hide();
	}else {
		$('#worknazioneContattocompleteItem').show();
	}		
	
}
function noente(){
	$('#strutturaContattocomplete').val(0);
	$('#enteContattocomplete').val('');
	$('#workaddressContattocomplete').val('');
	$('#workcapContattocomplete').val('');		
	$('#workcittaContattocomplete').val('');
}
function entedisabled(disabled){
	if(disabled){
		$('#enteContattocomplete').attr('disabled',true);
		$('#workaddressContattocomplete').attr('disabled',true);
		$('#workcapContattocomplete').attr('disabled',true);
		$('#workcittaContattocomplete').attr('disabled',true);
	}else{
		$('#enteContattocomplete').removeAttr("disabled");	
		$('#workaddressContattocomplete').removeAttr("disabled");	
		$('#workcapContattocomplete').removeAttr("disabled");	
		$('#workcittaContattocomplete').removeAttr("disabled");	
	}
}

	
$(document).ready(function(){
	
	toggleCheckbox('noenteContattocomplete','1','grpworksi');$("#noenteContattocomplete-1").click(function(){toggleCheckbox('noenteContattocomplete','1','grpworksi');}); 
	workprovinciaLoad();$("#workprovinciaContattocomplete").change(function(){workprovinciaChange();}); 
	$('#grpstrutture').dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		width: 780,
		height: 360,
		title:"Selezionare una struttura!",

		buttons: {
			'Struttura non in lista': function() {
				noente();
				$(this).dialog('close');
				entedisabled(false);	
				//$("#accordion").accordion( "activate" , indexAccordion );
			}
		}

	});	
	
	/* GESTIONE INFORMAZIONI */
	$('div.informazioni').dialog({
		
		autoOpen: true,
		modal: true,
		resizable: false,
		width: 420,
		height: 240,
		title:"Informazioni!",

		buttons: {
			'Chiudi': function() {
				$(this).dialog('close');
				//$("#accordion").accordion( "activate" , indexAccordion );
			}
		}

	});	

	
	$('.datepicker').datepicker({
		showAnim: 		'fadeIn',
		dateFormat: 'dd-mm-yy'
	});	
	
	$('.natoilpicker').datepicker({
		changeMonth: true,
		changeYear: true,
		yearRange: '1900:2010',
		showAnim: 		'fadeIn',
		dateFormat: 'dd-mm-yy'
	});	
	
	
	
	
	$('#itemcancella').dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		title:"Operazione",

		buttons: {
			'Si sono sicuro': function() {
				$(this).dialog('close');
				document.location = pageInfo.deletelink;
			},
			'No': function() {
				$(this).dialog('close');
			}
		}

	});
	
	$('a.rinnovoTo').hide();
	$('a.rinnovoFrom').click(function() {
		$('a.rinnovoFrom').hide();
		$('a.rinnovoTo').show();
		$('a.rinnovoTo').removeClass('rinnovoCancel');
		$("a.rinnovoTo img").attr('src','/images/ok.png');
		//$("a.rinnovoTo[alt$='"+$('a.rinnovoFrom').attr("alt")+"/old/']").hide();
		
		var id = $(this).attr("alt");
		$('a.rinnovoTo').each(function() {$(this).attr("href", $(this).attr("alt")+id);});

		//var toshow = $(this).next('.rinnovoTo');
		//alert(toshow.attr("alt"));
		//$(this ' + a').attr("href", $(this ' + a').attr("alt")+$(this).attr("alt"));
		
		$("a.rinnovoTo[alt$='"+$(this).attr("alt")+"/old/'] img").attr('src','/images/nonok.png');
		$("a.rinnovoTo[alt$='"+$(this).attr("alt")+"/old/']").addClass('rinnovoCancel');
		$("a.rinnovoTo[alt$='"+$(this).attr("alt")+"/old/']").attr('href','#');
		
		$('a.rinnovoCancel').click(function() {
			$('a.rinnovoFrom').show();
			$('a.rinnovoTo').hide();
		return false;
		});
	
		return false;
	});
	
	

	
	
	
	
});

$(window).resize(function(){
});
