/* JavaScripts condivisi */

// Box di caricamento
$("#loading").hide();
$(document).ajaxStart(function(){
	$("#loading").show();
});
$(document).ajaxStop(function(){
	$("#loading").hide();
});
$(document).ajaxComplete(function(){
	//alert("Elaborazione completata.");
});
$(document).ajaxError(function(event,request,settings){
	alert("Errore durante il caricamento della pagina.\n\nURL: "+settings.url+".");
});

// Alt e Title pulsanti di paginazione
function alt_paging_buttons() {
	$("#first").attr("alt","Prima pagina");
	$("#first").attr("title","Prima pagina");
	$("#prev").attr("alt","Pagina precedente");
	$("#prev").attr("title","Pagina precedente");
	$("#next").attr("alt","Pagina successiva");
	$("#next").attr("title","Pagina successiva");
	$("#last").attr("alt","Ultima pagina");
	$("#last").attr("title","Ultima pagina");
}

// Apertura finestre di dialogo
function open_dialog(width,height,title,resizable) {
	$("body").css("overflow", "hidden");

	$("#dialog").dialog({
		autoOpen:true,
		modal:true,
		width:width,
		height:height,
		title:title,
		resizable:resizable,
		bgiframe:true,
		close: function(event, ui) {
			$("#dialog").dialog("destroy");
			$("#dialog").empty();
			$("body").css("overflow", "auto");
		}
	});
	return false;
}

// Chiusura finestre di dialogo
function close_dialog() {
	$("#dialog").dialog("close");
	return false;
}
// Apertura finestre di dialogo
function open_dialog_iscrizione(width,height,title,resizable) {
	$("body").css("overflow", "hidden");

	$("#dialog_iscrizione").dialog({
		autoOpen:true,
		modal:true,
		width:width,
		height:height,
		title:title,
		resizable:resizable,
		bgiframe:true,
		close: function(event, ui) {
			$("#dialog_iscrizione").dialog("destroy");
			$("#dialog_iscrizione").empty();
			$("body").css("overflow", "auto");
		}
	});
	return false;
}

// Chiusura finestre di dialogo
function close_dialog_iscrizione() {
	$("#dialog_iscrizione").dialog("close");
	return false;
}

// Apertura finestre popup PDF
function pdf_window(url,name,width,height) {
	/*width += 32;
	height += 96;
	wleft = (screen.width - width) / 2;
	wtop = (screen.height - height) / 2;
	if (wleft < 0) {
		width = screen.width;
		wleft = 0;
	}
	if (wtop < 0) {
		height = screen.height;
		wtop = 0;
	}*/
	
	var win = window.open(url,name,'width='+width+',height='+height+',resizable=yes,location=no,toolbar=no,scrollbars=no,menubar=no,directories=no');
	
	/*win.resizeTo(width,height);
	win.moveTo(wleft,wtop);
	win.focus();*/
}

// Esegue il focus su input, select e textarea
function focus_input() {
	$("input,select,textarea").focus(function() {
		$(this).addClass("input-focus");
		/*var bg_image = $(this).css("background-image");
		if(bg_image.match("jscripts/themes/images/alert.gif")) {
			$(this).css({"background-image": "none"});
		}*/
	});
	$("input,select,textarea").blur(function() {
		$(this).removeClass("input-focus");
	});
}

// Disattiva il tasto invio sul form
function block_enter(form_name) {
	$(form_name).bind("keydown", function(e) {
		if (e.keyCode == 13) {  
			return false;
		}
	});
}

// Seleziona/deseleziona tutto nella griglia dati
function select_all(id) {
	if($("#select_all_status").val()==0) {
		//var id = "data."+id;
		$("#datagrid").resetSelection();
		var ids = $("#datagrid").getDataIDs();
		for(var i=0;i<ids.length;i++){
			var cl = ids[i];
			//var data = $("#datagrid").getRowData(cl);
			//$("#datagrid").setSelection(eval(id));
			$("#datagrid").setSelection(cl);
		}
		$("#select_all_status").val(1);
		$("#select_all").val("Rimuovi selezione");
	} else {
		$("#datagrid").resetSelection();
		$("#select_all_status").val(0);	
		$("#select_all").val("Seleziona tutto");
	}
}
function reset_select() {
	$("#select_all_status").val(0);	
	$("#select_all").val("Seleziona tutto");
}

// Converte la stringa con le iniziali maiuscole
function strCapitalize(string) {
	return string.replace(/\w+/g, function(a) {
		return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
	});
}

// Converte in minuscolo alcune parole
function strReplaceWords(str) {
	replace=new Array(" E "," Ed "," A "," Ad "," O "," Od "," Al "," All'"," Allo "," Alla "," Alle "," Agli "," In "," Nell'"," Nel "," Nella "," Nelle "," Negli "," Di "," Da "," Dal "," Dall'"," D'"," De "," Del "," Della "," Delle "," Dei "," Degli "," Dell'"," Sul "," Sull'"," Sui "," Sulla "," Sugli "," Su "," Con "," Per "," Tra "," Fra ");
	by=new Array(" e "," ed "," a "," ad "," o "," od "," al "," all'"," allo "," alla "," alle "," agli "," in "," nell'"," nel "," nella "," nelle "," negli "," di "," da "," dal "," dall'"," d'"," de "," del "," della "," delle "," dei "," degli "," dell'"," sul "," sull'"," sui "," sulla "," sugli "," su "," con "," per "," tra "," fra ");
	for (var i=0; i<replace.length; i++) {
		str = str.replace(replace[i], by[i]);
	}
	return str;
} 

// Rimuove gli spazi all'inizio e alla fine della stringa
function strTrim(string) {
    return string.replace(/^\s+/,'').replace(/\s+$/,'');
}

function strCapitalizeTrim(string) {
	//return strTrim(strCapitalize(string));
	return strReplaceWords(strTrim(strCapitalize(string)));
}

// Converte la stringa in maiuscolo
function strUpper(string) {
    return string.toUpperCase();
}

// Converte la stringa in minuscolo
function strLower(string) {
    return string.toLowerCase();
}

// Sostituisce tutte le occorrenze di una stringa
String.prototype.replaceAll=function(s1, s2) {
		return this.replace(new RegExp(s1,"g"), s2);
}
String.prototype.replaceAll=function(s1, s2) {
		var str = this;
		var pos = str.indexOf(s1);
		while (pos > -1){
				str = str.replace(s1, s2);
				pos = str.indexOf(s1);
		}
		return (str);
}

// Rimuove/sostituisce caratteri non compatibili
function removeMSWordChars(str) {
	var myReplacements = new Array();
	var myCode, intReplacement;
	myReplacements[9] = 32; // tab
	myReplacements[8216] = 39; // '
	myReplacements[8217] = 39; // '
	myReplacements[8218] = 39; // ‚
	myReplacements[8220] = 34; // "
	myReplacements[8221] = 34; // "
	myReplacements[8222] = 34; // "
	myReplacements[8211] = 45; // –
	myReplacements[8212] = 45; // —
	myReplacements[8364] = 69; // €
	myReplacements[8230] = 46; // …
	myReplacements[8226] = 45; // •
    for(c=0; c<str.length; c++) {
        var myCode = str.charCodeAt(c);
        if(myReplacements[myCode] != undefined) {
            intReplacement = myReplacements[myCode];
            str = str.substr(0,c) + String.fromCharCode(intReplacement) + str.substr(c+1);
        }
    }
	
	for(c=0; c<str.length; c++) { // rimuove tutti i caratteri non compatibili
		var myCode = str.charCodeAt(c);	
		if(myCode>255) {
			str = str.substr(0,c) + "" + str.substr(c+1);
		}
	}

    return str;
}

