	/*PERIODICITA'*/
function change_type_period(tipo){
	if (tipo=='Q'){
		$('frequenc_div').style.display = 'block';
		$('periodic_div').style.display = 'none';
	}else if (tipo=='P'){
		$('frequenc_div').style.display = 'none';
		$('periodic_div').style.display = 'block';
	}else{
		$('frequenc_div').style.display = 'none';
		$('periodic_div').style.display = 'none';
	}
}

function show_periodicity_spec(tipo){
	$('G_div').style.display = (tipo=='G') ? 'block' : 'none';
	$('S_div').style.display = (tipo=='S') ? 'block' : 'none';
	$('M_div').style.display = (tipo=='M') ? 'block' : 'none';
	$('A_div').style.display = (tipo=='A') ? 'block' : 'none';
}

	/*INDIPENDENTI*/
function openChat(id){
	var w = 400;
	var h = 400;
	var l = 0;
	var t = 0;
	if ($$('.prototip').size()>0){
		Ajax_Comix_close();
	}
	chat_window = window.open('chat-client.php?dest='+id,"chat_"+id,"width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
	chat_window.focus();
}

function chat_in(id){
	new Ajax.PeriodicalUpdater('unread', 'ajax.php?op=unread_msg', {
		method: 'get', 
		frequency: 10, 
		decay: 1,
		onCreate: function(transport){
			loading_alert = false;
		},
		onSuccess: function(transport){
			grand_tot = 0;
			document.title = document.title.replace(/\s\([0-9]+\)$/,"");
			var unread = transport.responseText.evalJSON(true);
			if ($('unread_msg_counter')!=undefined){
				if (unread['tot']!=undefined){
					grand_tot+=unread['tot']*1;
					$('unread_msg_counter').innerHTML=unread['tot'];
					$('unread_msg_counter').addClassName('unread_msg');
					var g = new k.Growler();
					for(var msg in unread.g_msg){
						g.growl(unread.g_msg[msg].message, {header: unread.g_msg[msg].title, className: "candybar", life: 20});	
					}
					if ($('memo_plannerbox')!=undefined){
						new Ajax.Updater('memo_plannerbox', 'index.php?module=Risorse&file=agenda&act=reload_box&tipo=memo&tipo_p=memo');
					}
				}else{
					$('unread_msg_counter').innerHTML="";
					$('unread_msg_counter').removeClassName('unread_msg');
				}
				for(var dest in unread.person){
					openChat(dest)
				}
			}
			
			if ($('rts_counter')!=undefined){
				if ((unread['rts']!=undefined) && (unread['rts']>0)){
					grand_tot+=unread['rts']*1;
					$('rts_counter').innerHTML=unread['rts'];
					$('rts_counter').addClassName('unread_msg');
				}else{
					$('rts_counter').innerHTML="";
					$('rts_counter').removeClassName('unread_msg');
				}
			}
				
			if (grand_tot>0){
				document.title = document.title + " ("+grand_tot+")";
			}
		}
	});
}

function chat_out(id){
	/*var w = 1;
	var h = 1;
	var l = 0;
	var t = 0;
	var TheNewWin = window.open('ajax.php?op=chat_logout&id='+id,"","width=" + w + ",height=" + h + "scrollbars=no,top=" + t + ",left=" + l);
	TheNewWin.blur();*/
}

function sendComm(){
	comet.doRequest($('msg').value, $('id_livello_hdd').value);
}

function answerComm(dest){
	openComix("ajax.php?op=form_message&dest="+dest,"msg_comix","msg_ancor");
}

function update_multils(){
	if (typeof(tlist2)=="object"){
		for (var i in tlist2){
			tlist2[i].update();
		};
	}
    return;
}
function update_files(id, fileName, result){
	var module = $('uploader_MODULE').value;
	var file = $('uploader_FILE').value;
	var id = $('uploader_ID').value;
	var url = "index.php?module="+module+"&file="+file+"&id="+id+"&act=uploader_file&tipo="+gup('tipo');
	new Ajax.Updater('files', url);
}

function update_files_description(){
	var module = $('uploader_MODULE').value;
	var file = $('uploader_FILE').value;
	var id = $('uploader_ID').value;
	new Ajax.Request('index.php?module='+module+'&file='+file+'&id='+id+'&act=saveFiles&tipo='+gup('tipo'), {
		parameters: $('form_files').serialize(true),
		onSuccess: function(transport){
			closer_support(1,'editor_files');		
		}
	});
}

function cancella_doc(file_num){
	var module = $('uploader_MODULE').value;
	var file = $('uploader_FILE').value;
	var id = $('uploader_ID').value;
	new Ajax.Updater('files', 'index.php?module='+module+'&file='+file+'&id='+id+'&act=del_file&file_id='+file_num);
}

function getQueryString4editor(){
	var get = getQueryString();
	var out = '';
	for (var i in get){
		if ((i!='module') &&
			(i!='file') &&
			(i!='act') &&
			(i!='row') &&
			(i!='in_array') &&
			(i!='clone') && 
			(i!='rand') &&
			(i!='filtri') &&
			(unescape(get[i]).search(/function/)<0) &&
			(get[i]!=undefined)
			){
			out+='&'+i+'='+unescape(get[i]);
		}
	}
	return out.replace(/#.*/,'');
}
function showLogStatus(row){
	openTooltip('index.php?module='+gup('module')+'&file='+gup('file')+'&act=log&row='+row, 'log_obj');
}
function showLogModule(){
	openTooltip('index.php?module='+gup('module')+'&file='+gup('file')+'&act=global_log', 'log_obj');
}
function ripristina(id){
	var cont = confirm("Il record sara' ripristinato, procedere?");
	if (cont){
		new Ajax.Request('ajax.php?op=restore_op&id='+id, {
			onSuccess: function(transport){
				if (transport.responseText=='OK'){
					alert('ripristino avvenuto con successo');
				}else{
					alert('si e\' verificato un errore durante il riprisitno');
				}
			}
		});
	}
}
function permanentDelete(row){
	var conf = confirm("Cancellare definitivamente?");
	if (conf){
		var extget = getQueryString4editor();
		new Ajax.Request('index.php?module='+gup('module')+'&file='+gup('file')+'&act=destroy&row='+row+'&start='+gup('start')+extget, {
			onSuccess: function(transport){
				filter_editor();
			}
		});
	}
}

function changeStatus(row){
	var stosend = 0;
	if (document.filter_hash!=undefined){
		if(document.filter_hash.status_sel.checked) stosend=1
	}
	var extget = getQueryString4editor();
	new Ajax.Request('index.php?module='+gup('module')+'&file='+gup('file')+'&act=status&row='+row+'&start='+gup('start')+'&status='+stosend+extget, {
		onSuccess: function(transport){
			filter_editor();
		}
	});
	return false;
}

function ordereditor(order,dir){
	$('order').value = order;
	$('dir').value = dir;
	paginate($('page').value);
}

function paginate(page){
	$('page').value = page;
	Effect.ScrollTo('navmenu');
	var url = getFilterUrl();
	new Ajax.Updater('main_obj', url+'&request=ajax');	
}

function getFilterUrl(){
	var base = 'index.php?module='+gup('module')+'&file='+gup('file');
	var collection = document.filter_hash.elements;
	var filter  = new Array;
	var extra   = new Object;
	var stosend = "";
	var ftosend = "";
	var otosent = "";
	var i = 0;
	var k = 0;
	for (var i  in collection){
		if ((collection[i] != undefined) && 
			(collection[i].name != undefined) &&
			(collection[i].name.substring(0,6)=="extra_")){
			extra[collection[i].name.replace('extra_','').replace('_sel','')] = collection[i].value;
		}else if ((collection[i] != undefined) && 
			(collection[i].name != undefined) && 
			(collection[i].name.replace("_sel","")!="") && 
			(collection[i].name.substring(0,7)!='picker_') &&
			(collection[i].name.substring(0,6)!='extra_') &&
			(collection[i].name!='sel_status_sel') &&
			(collection[i].name!='cy') &&
			(collection[i].name!='status_sel') &&
			(collection[i].name.substring(0,9)!='sel_extra') &&
			(collection[i].value!=0) && 
			(collection[i].value!='') && 
			(collection[i].value!=undefined) && 
			(collection[i].value!="NULL") && 
			(collection[i].value!="0000-00-00")){
				if ((collection[i].id.substring(0,7) == 'dataora') || (collection[i].id.substring(0,6) == 'orario')){
					if (collection[i].name.substring(0,9)=="mysql_da_"){
						name = collection[i].name.replace("_sel","").replace("mysql_da_","")
						filter[k] = "(FROM_UNIXTIME("+name+",'%Y-%m-%d')>='"+collection[i].value+"')";
						k++;
					}else if (collection[i].name.substring(0,8)=="mysql_a_"){
						name = collection[i].name.replace("_sel","").replace("mysql_a_","")
						filter[k] = "(FROM_UNIXTIME("+name+",'%Y-%m-%d')<='"+collection[i].value+"')"
						k++;
					}
				}else if (collection[i].type=="checkbox"){
					name = collection[i].name.replace("_sel","").replace("sel_","")
					if (collection[i].checked){
						filter[k] = "("+name+"='"+collection[i].value+"')";
						k++;
					}
				}else if ((collection[i].name=="da_fatturare_sel") || (collection[i].name=="da_preventivare_sel")){
					name = collection[i].name.replace("_sel","")
					filter[k] = "("+name+">='"+collection[i].value+"')";
					k++;
				}else if (collection[i].name.substring(0,3)=="da_"){
					name = collection[i].name.replace("_sel","").replace("da_","")
					filter[k] = "("+name+">='"+collection[i].value+"')";
					k++;
				}else if(collection[i].name.substring(0,2)=="a_"){
					name = collection[i].name.replace("_sel","").replace("a_","")
					filter[k] = "("+name+"<='"+collection[i].value+"')";
					k++;
				}else if ((collection[i].type == 'select-one')){
					filter[k] = '(' + collection[i].name.replace("_sel","").replace("sel_","") +' LIKE "%25,' + escape(collection[i].value) + ',%25" OR ' +collection[i].name.replace("_sel","") +' LIKE "%,' + escape(collection[i].value) + '" OR ' +collection[i].name.replace("_sel","") +' LIKE "' + escape(collection[i].value) + ',%" OR ' +collection[i].name.replace("_sel","") +' = "' + escape(collection[i].value) + '")';
					k++;
				}else{
					filter[k] = '(' + collection[i].name.replace("_sel","").replace("sel_","") +' LIKE "%25' + escape(collection[i].value) + '%25" OR ' +collection[i].name.replace("_sel","") +' LIKE "%' + escape(collection[i].value) + '" OR ' +collection[i].name.replace("_sel","") +' LIKE "' + escape(collection[i].value) + '%" OR ' +collection[i].name.replace("_sel","") +' = "' + escape(collection[i].value) + '")';
					k++;
				}
		}
	}
	extrasend = "&"+$H(extra).toQueryString()
	ftosend = filter.join(' and ');
	
	if(document.filter_hash.sel_status_sel!=undefined && document.filter_hash.sel_status_sel.checked) stosend=1
	else stosend=0;
	if($('order').value!="") otosent = otosent + '&orderby='+$('order').value;
	if($('dir').value!="") otosent = otosent + '&dir='+$('dir').value;
	if($('page').value!="") otosent = otosent + '&start='+$('page').value;
	var extget = getQueryString4editor();
	var url = base+'&act=filtro&filtri='+ftosend+'&status='+stosend+extget+otosent+'&request=ajax'+extrasend;
	/*Gruppo*/
	if (document.group_hash!=undefined){
		var gcollection = document.group_hash.elements;
		var group = new Array;
		var i = 0;
		var k = 0;
		for (var i  in gcollection){
			if ((gcollection[i] != undefined) && 
				(gcollection[i].name != undefined) && 
				(gcollection[i].value!=undefined))  {
				name = gcollection[i].name.replace("_sel","").replace("sel_","")
				if (gcollection[i].checked){
					group[k] = name;
					k++;
				}
			}
		}
		gtosend = group.join(',');
		url += '&gruppo='+gtosend;
	}
	return url;
}

function filter_editor(){
	var offset = Element.cumulativeOffset($('filter'));
	var height = $('filter').getHeight();
	var width  = $('filter').getWidth();
	var layer = document.createElement('div');
	layer.setAttribute('id', 'filter_layer')
	layer.setStyle('position:absolute; top: '+offset[1]+'px; left: '+offset[0]+'px; width:100%; height:'+height+'px;background-color:grey; opacity:.35; background-image: url(img/ajax-loader.gif);background-repeat:no-repeat;background-position:center;');
	document.body.appendChild(layer);
		
	url = getFilterUrl("");	
	new Ajax.Updater('main_obj',url,{
		onComplete: function(){
			$('filter_layer').remove();
			resizeIframe();
		}
	});
	return false;
}

function group_editor(){
	url = getFilterUrl("");
	new Ajax.Updater('main_obj',url);
}

function filter_editor_xls(){
	url = getFilterUrl();
	url = url.replace('&request=ajax','');
	location.href = url+'&act=xls';
}

function filter_editor_pdf(){
	url = getFilterUrl();	
	var collection = document.filter_hash.elements;
	var data   = new Array;
	for (var i  in collection){
		if ((collection[i] != undefined) && 
			(collection[i].name != undefined) && 
			(collection[i].name.replace("_sel","")!="") && 
			(collection[i].name.substring(0,7)!='picker_') &&
			(collection[i].value!=0) && 
			(collection[i].value!='') && 
			(collection[i].value!=undefined) && 
			(collection[i].value!="NULL") && 
			(collection[i].value!="0000-00-00"))  {
			if ((collection[i].id.substring(0,7) == 'dataora') || (collection[i].id.substring(0,6) == 'orario')){
				if (collection[i].name.substring(0,9)=="mysql_da_"){
					data[0] = collection[i].value;
				}else if (collection[i].name.substring(0,8)=="mysql_a_"){
					data[1] = collection[i].value;
				}
			}
		}
	}
	location.href = url.replace('&request=ajax','')+'&data_start='+data[0]+'&data_end='+data[1]+'&act=pdflist';
}

function showFilter(){
	var filterdiv = $('filter');
	if (filterdiv.style.display=='none')	
		filterdiv.style.display='block';
	else	
		filterdiv.style.display='none';
}
function showGroup(){
	var groupdiv = $('group');
	if (groupdiv.style.display=='none')	
		groupdiv.style.display='block';
	else	
		groupdiv.style.display='none';
}
function resetFilter(){
	location.href='index.php?module='+gup('module')+'&file='+gup('file')
}
	/*INPLACE*/
function newRowInplace(){
	var extget = getQueryString4editor();
	new Ajax.Updater('to_insert', 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=edit&row=0'+extget);
	$('to_insert').style.display = 'block';
}
function editRowInplace(row){
	var extget = getQueryString4editor();
	new Ajax.Updater('row_'+row, 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=edit&row='+row+'&start='+gup('start')+extget);
}
function updateRowInplace(row){
	update_multils(tlist2);
	if 	($('validator_'+gup('module')+'_'+gup('file'))!=undefined){
		eval($('validator_'+gup('module')+'_'+gup('file')).innerHTML);
		var test = validate(validator,validator_class);
	}else{
		var test = true;
	}
	if (test){
		var extget = getQueryString4editor();
		$('to_insert').style.display = 'none';
		new Ajax.Request('index.php?module='+gup('module')+'&file='+gup('file')+'&act=save&row='+row+'&new=1&start='+gup('start')+extget, {
			parameters: $('form_'+row).serialize(true),
			onSuccess: function(){
				//new Ajax.Updater('main_obj','index.php?module='+gup('module')+'&file='+gup('file')+'&act=cnt&start='+gup('start')+extget);
				filter_editor();
			}
		});
	}
}
	/*TOOLTIP*/
function newRowTooltip(){
	var extget = getQueryString4editor();
	openTooltip('index.php?module='+gup('module')+'&file='+gup('file')+'&act=edit&row=0'+extget, 'editor');
}
function editRowTooltip(row){
	var extget = getQueryString4editor();
	
	if (callback==undefined) var callback="noop"
	openTooltip('index.php?module='+gup('module')+'&file='+gup('file')+'&act=edit&row='+row+'&start='+gup('start')+extget, 'editor', callback);
}

function editRowTooltipFromParent(row){
	var extget = getQueryString4editor();
	var url = 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=edit&row='+row+'&start='+gup('start')+extget;
	url+=extget;
	new window.parent.Ajax.Request(url, {
		method: 'get',
		evalScripts: 'force',
		onSuccess: function(transport) {
			function_callback_close = "reloadWindow";
			content = transport.responseText;
			var vp = window.parent.document.viewport.getDimensions();
			window.parent.Shadowbox.open({
		        content:    content,
		        player:     "html",
		        title:		"Uno!",
		        height:     vp.height,
		        width:      vp.width-10
		    });
			window.parent.content_history.push(content);
			return false;
		}
	});
	return false;
}

function updateRowTooltip(row){
	update_multils(tlist2);
	if ($('validator_'+gup('module')+'_'+gup('file'))!=undefined){
		eval($('validator_'+gup('module')+'_'+gup('file')).innerHTML);
		var test = validate(validator,validator_class);
	}else{
		var test = true;
	}
	if (test){	
		var extget = getQueryString4editor();
		if (gup('fly')!=""){
			new Ajax.Request('index.php?module='+gup('module')+'&file='+gup('file')+'&act=save&row='+row+'&new=0&start='+gup('start')+extget, {
				evalScripts: true,
				parameters: $('form_'+row).serialize(true),
				onSuccess: function(transport){
					window_closer_support('editor');		
				}
			});
		}else if ($('main_obj')!=undefined){
			new Ajax.Request('index.php?module='+gup('module')+'&file='+gup('file')+'&act=save&row='+row+'&new=0&start='+gup('start')+extget, {
				evalScripts: true,
				parameters: $('form_'+row).serialize(true),
				onSuccess: function(transport){
					//new Ajax.Updater('main_obj','index.php?module='+gup('module')+'&file='+gup('file')+'&act=cnt&start='+gup('start')+extget);
					filter_editor();
					if ($('sb-player')!=null){
						Ajax_Tooltip_close('editor');
					}else{
						Tips.hideAll();
					}
				}
			});
		}else{
			new Ajax.Request('index.php?module='+gup('module')+'&file='+gup('file')+'&act=save&row='+row+'&new=0&start='+gup('start')+extget, {
				evalScripts: true,
				parameters: $('form_'+row).serialize(true),
				onSuccess: function(transport){
					window_closer_support('editor');		
				}
			});
		}
	}
}

function window_closer_support(content, id){
	window.close();
}

function closer_support(content,id){
	Ajax_Tooltip_close(id);
}

	/*WINDOW*/
function newRowWindow(){
	var extget = getQueryString4editor();
	var url = 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=edit&row=0'+extget;
	//window.open(url, 'editor', 'width=1024,height=768,scrollbars=yes,resize=yes');
	var vp = document.viewport.getDimensions();
	Shadowbox.open({
        content:    url,
        player:     "iframe",
        height:     vp.height,
        width:      vp.width
    });
}
function editRowWindow(row){
	var extget = getQueryString4editor();
	var url = 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=edit&row='+row+'&start='+gup('start')+extget;
	//window.open(url, 'editor', 'width=1024,height=768,scrollbars=yes,resize=yes');
	var vp = document.viewport.getDimensions();
	Shadowbox.open({
        content:    url,
        player:     "iframe",
        height:     vp.height,
        width:      vp.width
    });
}
function updateRowWindow(row){
	update_multils(tlist2);
	if ($('validator_'+gup('module')+'_'+gup('file'))!=undefined){
		eval($('validator_'+gup('module')+'_'+gup('file')).innerHTML);
		var test = validate(validator,validator_class);
	}else{
		var test = true;
	}
	if (test){
		var extget = getQueryString4editor();
		document.forms[0].action = 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=save&row='+row+'&new=0&start='+gup('start')+extget;
		document.forms[0].submit();
	}
}


	/*PAGE*/
function newRowPage(){
	var extget = getQueryString4editor();
	var url = 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=edit&row=0'+extget;
	//location.href = url;
	var vp = document.viewport.getDimensions();
	Shadowbox.open({
        content:    url,
        player:     "iframe",
        height:     vp.height,
        width:      vp.width
    });
}
function editRowPage(row){
	var extget = getQueryString4editor();
	var url = 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=edit&row='+row+'&start='+gup('start')+extget;
	//location.href = url;
	var vp = document.viewport.getDimensions();
	Shadowbox.open({
        content:    url,
        player:     "iframe",
        height:     vp.height,
        width:      vp.width
    });
	
}
function updateRowPage(row){
	update_multils(tlist2);
	if ($('validator_'+gup('module')+'_'+gup('file'))!=undefined){
		eval($('validator_'+gup('module')+'_'+gup('file')).innerHTML);
		var test = validate(validator,validator_class);
	}else{
		var test = true;
	}
	if (test){
		var extget = getQueryString4editor();
		$('form_'+row).action = 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=save&row='+row+'&new=0&start='+gup('start')+extget;
		$('form_'+row).submit();
	}
}
