
function modale(titolo,testo){
	$(document).ready(function() {
                $("#dialog-modal").dialog({
						autoOpen: true,
						title: titolo,
                        height: 140,
						modal: true
					});
				$("#dialog-modal").html(testo);
	});
}

function debug(testoErrore){
	$(document).ready(function() {
				$("body").prepend('<div id="debugModal"></div>');			   
                $("#debugModal").dialog({
						autoOpen: true,
						title: 'Debug',
						position: ['right','top'],
						minHeight: 600
					});
				$("#debugModal").html(testoErrore);
	
	$(function() {
		$("#debug").accordion({
			fillSpace: true
		});
	});
	
	
	});	
}

function ajaxDebug(variabile){
	$("#ajaxDebug").append(variabile);
}

function confermaBuonfine(testoErrore){
		$(function() {
					// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
					$("#dialog").dialog("destroy");
				
					$("#dialog-modal").dialog({
						autoOpen: true,
						height: 140,
						modal: true
					});
					$("#dialog-modal").html(testoErrore, function(){
								
																				  });
					$("#dialog-modal").parent().delay(2000).fadeOut('slow');
					$(".ui-widget-overlay").delay(2000).fadeOut('slow', function(){
																		 $("#dialog-modal").dialog('close');
																				 });
					//
				});
}

jQuery.fn.conferma = function() {
    var o = $(this[0]) // It's your element
};

jQuery.fn.conferma = function() {
    var args = arguments[0] || {}; // It's your object of arguments
    var titolo = args.titolo;
    var testo = args.testo;
	var azione = args.azione;
	var deletingFile = args.deletingFile;
	var form = $(this).parent('form');
			$("#dialog-modal").dialog({
			title: titolo,
			resizable: false,
			height: 'auto',
			modal: true,
			buttons: {
				'Si' : function() {
							$.getJSON(form.attr("action") + "?" + form.serialize() +'&action='+azione+'&deletingFile='+deletingFile , function(json) {
							if(json.response.risultato == true){
								switch(json.response.transition){		
											case "fadeInWithClone":
												var mioclone = form.closest("li");
												var nuovo = mioclone.clone().insertAfter(mioclone);
											//Modifico l'id del li
											nuovo.attr('id',json.response.id);
											//Modifico l'id all'interno del form
											$('#id',nuovo).val(json.response.id);
											break;
											default:
												form.closest("li").fadeOut();
											break;
											}
								return json.response.risultato;
							} else {
								errore(json.response.erroreDB);
							}
							
							});
					
					$(this).dialog('close');
					
				},
				'No': function() {
					$(this).dialog('close');
				}
			}
		});
		$("#dialog-modal").html(testo);
};

function getOrder() {
                            	// save custom order to cookie
                            	var elenco = $("#sortable").sortable('toArray');
                                //tabella dove devo riodinare
                                var table = $("#sortable #table").val();
								$.getJSON("/WebXC/ReservedArea/Ajax-functions.php?action=riordina",{table:table,elenco:elenco},function(json)
					{
						if(json.response.erroreDB)
						{
							errore(json.response.erroreDB);
						}
					});
                            }

//Inverte il parametro isHome							
jQuery.fn.isHome = function() {
    var o = $(this[0]) // It's your element
};

jQuery.fn.isHome = function() {
		var id = $(this).closest('li').attr('id');
								//tabella dove devo riodinare
                                var table = $("#sortable #table").val();
								$.getJSON("/WebXC/ReservedArea/Ajax-functions.php?action=ishome",{table:table,id:id},function(json)
					{
						if(json.response.erroreDB)
						{
							errore(json.response.erroreDB);
						} else {
							
							var homeimg =  '#isHome'+id;
							
							var src = '/WebXC/img/PNG/Blue/18/isHome'+json.response.risultato+'.png';
							$(homeimg).attr('src',src);
						}
					});	
}

//Inverte il parametro isHome							
jQuery.fn.univocaMail = function() {
    var o = $(this[0]) // It's your element
};

jQuery.fn.univocaMail = function() {
								var dato = $(this).val();
								//tabella dove devo riodinare
                                var table = "viewusers";
								$.getJSON("/WebXC/ReservedArea/Ajax-functions.php?action=datounivoco",{table:table,dato:dato},function(json)
					{
						if(json.response.risultato==false)
						{	
							$('.mailunivoca').val('Ricompilare questo campo');
							alert('Questa e-mail è già presente nel nostro Database');
						}
					});	
}

//Inverte il parametro published							
jQuery.fn.isPublished = function() {
    var o = $(this[0]) // It's your element
};

jQuery.fn.isPublished = function() {
		var id = $(this).closest('li').attr('id');
								//tabella dove devo riodinare
                                var table = $("#sortable #table").val();
								$.getJSON("/WebXC/ReservedArea/Ajax-functions.php?action=ispublished",{table:table,id:id},function(json)
					{
						if(json.response.erroreDB)
						{
							errore(json.response.erroreDB);
						} else {
							
							var myimg =  '#isPublished'+id;
							
							var src = '/WebXC/img/PNG/Blue/18/isPublished'+json.response.risultato+'.png';
							$(myimg).attr('src',src);
						}
					});	
}


var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
//Controlla che i box con dimensione ridotta si allarghino se sono soli
function controlloBox50(selettore){
	var quanti = $(selettore).length;
	if(quanti==1){	
		$(selettore).css('width','100%');
	}
}


