$(function() {
	$('form').attr('autocomplete', 'off');
	$('a.external').attr('target','_blank');
	$('a.delete').click(function(){ return confirm('Cancellare il record selezionato?') });
	$('#q').focus(function() {
		if ($(this).val(sText)) {
			$(this).val('').removeClass('quiet');
		}
	})
	popup('popup', 400, 480);
	preload('../images/ajax-loader.gif');
});

// mostra una progress bar sotto forma di dialog
function progress() {
	if ($('input[type=file]').val()) {
		var html;
		html = '<div id="dialog"><strong>Caricamento in corso...</strong>\
		<p class="small">Non chiudere la finestra durante l\'operazione</p>\
		<img class="centered" src="../images/ajax-loader.gif" alt="" /></div>';
		$(html).dialog({
			dialogClass: 'progress',
			closeOnEscape: false,
			draggable: false,
			resizable: false,
			modal: true,
			show: 'fade',
			width: 'auto',
			height: 80
		})
	}
}

// precarica le immagini
function preload() { // http://www.texotela.co.uk/code/jquery/preload/
	for (i = 0; i < arguments.length; i++) {
		$('<img>').attr('src', arguments[i]);
	}
}

// seleziona/deseleziona tutti i checkbox
function checkAll() {
	var form = (arguments[0]) ? arguments[0] : "#form1";
	var trigger = (arguments[1]) ? arguments[1] : "#chkToggle";
	$(trigger).click(function(){
		$(form + " input[type='checkbox']").attr("checked", $(this).is(":checked"));
	});
}

// attiva/disattiva i records selezionati
function toggleSelected() {
	var url = window.location.pathname; // Request.ServerVariables("URL")
	var qs = window.location.search; // "?" & Request.QueryString()
	var append = qs ? qs + "&nAction=toggle" : "?nAction=toggle";
	var form = (arguments[0]) ? arguments[0] : "#form1";
	var btn = (arguments[1]) ? arguments[1] : "#btnToggle";
	$(btn).click(function() {
		$(form).attr("action",url + append).submit();
	});
}

// elimina i records selezionati
function deleteSelected() {
	var url = window.location.pathname; // Request.ServerVariables("URL")
	var qs = window.location.search; // "?" & Request.QueryString()
	var append = qs ? qs + "&nAction=delete" : "?nAction=delete";
	var form = (arguments[0]) ? arguments[0] : "#form1";
	var btn = (arguments[1]) ? arguments[1] : "#btnDelete";
	$(btn).click(function() {
		var x = confirm("Eliminare i record selezionati?");
		if (x) { $(form).attr("action",url + append).submit(); }
	});
}

// visualizza/nasconde il target
function toggleSlide() {
	var target = (arguments[0]) ? arguments[0] : "#form1";
	var trigger = (arguments[1]) ? arguments[1] : "#add";
	$(trigger).click(function(){ $(target).slideToggle(); });
}

// copia il valore da un campo ad un altro, anche per più coppie
function copy() {
	for (var i = 0; i < arguments.length; i++) {
		var temp = arguments[i].split("|");
		$(temp[0]).data("CopyTo", temp[1]);
		$(temp[0]).keyup(function(){ $($(this).data("CopyTo")).val($(this).val()) });
	}
}

// mostra/nasconde le righe
function toggleDisplay(){
	var checkbox = $(arguments[0]);
	var target = arguments[1];
	var action = arguments[2]; // = remove class "hidden"
	var targets = target.split("|");
	if ((typeof(checkbox)) == 'string') {	checkbox = $('#'+checkbox+''); } 
	for(var i=0; i<targets.length; i++) {
		$('#'+targets[i]).toggleClass('hidden');
	}
}

// apre una popup
function popup(classname, width, height) {
	$('.' + classname).click(function(e){
		e.preventDefault();
		//alert($(this).attr('href'))
		var page = window.open($(this).attr('href'),classname,'scrollbars=yes,resizable=yes,width='+width+',height='+height+'');
		if (window.focus) {page.focus()}
		//return false;
	})
}

// lega due datepicker per le prenotazioni
function customDateFunction(input) {   
	if (input.id == "checkin") {
		if ($("#checkout").datepicker("getDate") != null) {
			dateMax = $("#checkout").datepicker("getDate");
		} else {
			dateMax = '';
		}
		return {maxDate: dateMax};                     
	} else if (input.id == "checkout") { 
		if ($("#checkin").datepicker("getDate") != null) {
			dateMin = $("#checkin").datepicker("getDate");
		} else {
			dateMin = '';
		}
		return { minDate: dateMin  }; 
	}
}


