function switch_res(){
	if ($('res_tecniche').style.display=='none'){
		$('res_tecniche').show();
		$('res_umane').hide();
		$('boxres_title').innerHTML = 'tecniche';
		$('int_planner_tcn').show();
		$('int_planner').hide();
	}else{
		$('res_tecniche').hide();
		$('res_umane').show();
		$('boxres_title').innerHTML = 'umane';
		$('int_planner_tcn').hide();
		$('int_planner').show();
	}
}

function select_all_tipo(sel, div_id){
	$$('.'+div_id+' input[type=checkbox]').each(function(el){
		el.checked = sel.checked;
		$$('.'+el.id).each(function(int_el){
			int_el.checked = sel.checked;
		})
	});
	update_session_planner();
}

function updateRowTooltip_permessi(row){
	update_multils();
	if ($('validator_Risorse_richieste_personale_permessi')!=undefined){
		eval($('validator_Risorse_richieste_personale_permessi').innerHTML);
		var test = validate(validator,validator_class);
	}else{
		var test = true;
	}
	if (test){	
		var extget = getQueryString4editor();
		new Ajax.Request('index.php?module=Risorse&file=richieste_personale_permessi&act=save&row='+row+'&new=0&start='+gup('start')+extget, {
			evalScripts: true,
			parameters: $('form_'+row).serialize(true),
			onSuccess: function(transport){
				Tips.hideAll();
			}
		});
	}
}

function updateRowTooltip_ferie(row){
	update_multils();
	if ($('validator_Risorse_richieste_personale_ferie')!=undefined){
		eval($('validator_Risorse_richieste_personale_ferie').innerHTML);
		var test = validate(validator,validator_class);
	}else{
		var test = true;
	}
	if (test){	
		var extget = getQueryString4editor();
		new Ajax.Request('index.php?module=Risorse&file=richieste_personale_ferie&act=save&row='+row+'&new=0&start='+gup('start')+extget, {
			evalScripts: true,
			parameters: $('form_'+row).serialize(true),
			onSuccess: function(transport){
				Tips.hideAll();
			}
		});
	}
}


function resize_attWindow(el){
/*	var pianificazione 	= el.up().up().up().up().up()
	var sidebar 		= pianificazione.next();
	var firstContent 	=  pianificazione.up().up().up().up();
	var secondContent 	= firstContent.up().up().up();
	
	var w1 = pianificazione.getWidth() + sidebar.getWidth() + 40;
	var w2 = w1 + 12;
	
	firstContent.setStyle('width:'+w1+'px');
	secondContent.setStyle('width:'+w2+'px');
*/
	return false;
}

function newActivity(day,h_init,h_final,id){
	dalle_h = Math.floor(h_init);
	alle_h  = Math.floor(h_final);
	
	dalle_m = (h_init - Math.floor(h_init))*60;
	alle_m  = (h_final - Math.floor(h_final))*60;
	
	dalle_h = (dalle_h<10) ? "0"+dalle_h : dalle_h;
	alle_h  = (alle_h<10) ? "0"+alle_h : alle_h;
	
	dalle_m = (dalle_m<10) ? "0"+dalle_m : dalle_m;
	alle_m  = (alle_m<10) ? "0"+alle_m : alle_m;
	
	var dalle = dalle_h + "" + dalle_m;
	var  alle = alle_h + "" + alle_m;
	
	openComix('index.php?module=Risorse&file=attivita_form&act=edit&day='+day+'&dalle='+dalle+'&alle='+alle+'&tecn_id='+id, 'new_att_'+'_'+day+'_'+h_init+'_'+id, 'ancora_'+day+'_'+h_init+'_'+id, false)
}

function update_post_rl(rapporto){
	if ($('resoconti')!=undefined){
		tecn = $('tecnico_0').value;
	 	data = $('data').value
		esistente = $('esistente').checked;
		nuova = $('nuova').checked;
		load_rapportolav(tecn);
	 	var query = "SELECT SUBSTRING(alle,1,2) AS alle_h, SUBSTRING(alle,3,2) AS alle_m, alle FROM tsk_reportlavori WHERE id_tecn="+$('tecnico_0').value+" AND data_i='"+$('dataora_mysql_data').value+"' ORDER BY alle DESC LIMIT 1"
		new Ajax.Request('ajax.php?op=query&query='+query, {
			onComplete: function(transport){
				eval(transport.responseText);
				rapporto.reset();
				//att_mode('E');
				$('esistente').checked = esistente;
				$('nuova').checked = nuova;
				$('id_report').value = "";
				$('anagrafica_1').value = "";
				$('ora_pianifica_dalle').value = result['1']['alle_h'];
				$('min_pianifica_dalle').value = result['1']['alle_m'];
				$('ora_final_pianifica_dalle').value = result['1']['alle'];
	 			$('tecnico_0').value = tecn;
	 			updateDateField('picker_data', data);
			}
		})
	}else{
		Tips.hideAll();
	}
	reload_planner();
}

function stampa_att_rl(){
	var dal = $('dataora_data_da').value;
	var  al = $('dataora_data_a').value;
	window.open('index.php?module='+gup('module')+'&file='+gup('file')+'&act=print_rl&data_start='+dal+'&data_end='+al, '');
}

function setDays(day){
	$('days').value=day;
	return;
}

function expand_planner(element_id){
	if ((element_id=='memo') || (element_id=='fatt') || (element_id=='np')){
		if ($(element_id).style.width!='700px'){	
			$(element_id).morph('width:700px', {
				duration: 0.8,
				afterFinish: function(){
					$('arrow_'+element_id).src = 'img/freccia_left.png';
					primo_piano(element_id);
				}
			});
		}else{
			$(element_id).morph('width:180px', {
				duration: 0.8,
				afterFinish: function(){
					$('arrow_'+element_id).src = 'img/freccia_right.png';
					secondo_piano(element_id);
				}
			});
		}
	}else{
		if ($(element_id).style.width!='700px'){	
			$(element_id).morph('width:700px', {
				duration: 0.8,
				afterFinish: function(){
					$('arrow_'+element_id).src = 'img/freccia_right.png';
					primo_piano(element_id);
				}
			});
		}else{
			$(element_id).morph('width:180px', {
				duration: 0.8,
				afterFinish: function(){
					$('arrow_'+element_id).src = 'img/freccia_left.png';
					secondo_piano(element_id);
				}
			});
		}
	}
	
}

function chiudi(element_id){
	if ((element_id=='memo') || ((element_id=='fatt')) ){
		if ($(element_id)){
			$(element_id).style.width='180px';
			$('arrow_'+element_id).src = 'img/freccia_right.png';
			secondo_piano(element_id);
		}
	}else{
		if ($(element_id)){
			$(element_id).style.width='180px';
			$('arrow_'+element_id).src = 'img/freccia_left.png';
			secondo_piano(element_id);
		}
	}
	
}

function chiudi_tutti(){
	resetta_primo_piano();
	chiudi('memo');
	chiudi('fatt');
	chiudi('oggi');
	chiudi('scadute');
	chiudi('future');
}

function resetta_primo_piano(){
	$('memo').style.zIndex = 0;
	$('memo').style.border = '1px solid #DDD';

	$('oggi').style.zIndex = 0;
	$('oggi').style.border = '1px solid #DDD';
	
	$('scadute').style.zIndex = 0;
	$('scadute').style.border = '1px solid #DDD';
	
	if ($('fatt')){
		$('fatt').style.zIndex = 0;
		$('fatt').style.border = '1px solid #DDD';
	}
}

function primo_piano(element_id){
	resetta_primo_piano();
	$(element_id).style.zIndex = 10;
	//$(element_id).style.border =  '2px outset #DDD';
}

function secondo_piano(element_id){
	$(element_id).style.zIndex = 0;
	//$(element_id).style.border =  '1px solid #DDD';
}

function show_planner(){
	var days = $('days').value;
	var gg   = $('datagg_start_day').value;
	var mm	 = $('datamm_start_day').value;
	var aa	 = $('dataaaaa_start_day').value;
	new Ajax.Updater('agende','index.php?module=Risorse&file=agenda&act=getPlanner&max_day='+days+'&gg='+gg+'&mm='+mm+'&aa='+aa)
}

function updateDate(diff){
 	var days = $('days').value;
	var gg   = $('datagg_start_day').value*1 + diff;
	var mm	 = $('datamm_start_day').value;
	var aa	 = $('dataaaaa_start_day').value;
	
	if ($('orario_start_day').value!=""){	
	 	var orario = ($('orario_start_day').value).split(":");
  	}else{
  		var orario = ("00:00:00").split(":");
  	}
 	var ggmysql = gg;
  	var mmmysql = mm;
  	var aamysql = aa;
  	$('datagg_start_day').value = gg;
  	$('datamm_start_day').value = mm;
  	$('dataaaaa_start_day').value = aa;
  	var time = new Date();
  	if ((gg>0) && (mm>=0) && (aa>0)){
		time.setFullYear(aa);
	 	time.setMonth(mm);
	 	time.setDate(gg);
	 	time.setHours(orario[0], orario[1], 0);
	 	var timestamp = (time.getTime() / 1000);
	 	var mmstring = ((mmmysql+1)<10) ? '0'+(mmmysql+1) : (mmmysql+1);
	 	var ggstring = (ggmysql<10) ? '0'+ggmysql : ggmysql;
	 	$('dataora_start_day').value = Math.round(timestamp);
	 	$('dataora_mysql_start_day').value = aamysql+'-'+mmstring+'-'+ggstring;
	 
	 	eval($('function_start_day').value);
		
		reload_planner()
  	}
}

function reload_planner(){
	var boxres  = $('boxres_title').innerHTML;
	var giorno 	= $('datagg_start_day').value;
	var mese 	= $('datamm_start_day').value;
	var anno	= $('dataaaaa_start_day').value;
	new Ajax.Updater('master_cnt', 'index.php?module=Risorse&file=agenda&act=cnt&gg='+giorno+'&mm='+mese+'&aa='+anno+'&boxres='+boxres, {
		onComplete: function(transport){
			if (boxres=='tecniche'){
				$('res_tecniche').show();
				$('res_umane').hide();
				$('boxres_title').innerHTML = 'tecniche';
			}else{
				$('res_tecniche').hide();
				$('res_umane').show();
				$('boxres_title').innerHTML = 'umane';
			}
		}
	});
	if ($$('.prototip').size()>0){
		Ajax_Comix_close();
	}else if ($('editor')!=undefined){
		Ajax_Tooltip_close('editor');
	}
}

function update_session_planner(el){
	if (el!=undefined){
		$$('.'+el.id).each(function(int_el){
			int_el.checked = el.checked;
		})
	}
	var multiple_selector = new Array();
	$$('.multipleselector').each(function(el){
		if (el.checked) multiple_selector.push(el.id)
	})
	
	var check = document.filter_hash.risorse_umane;
	var sel = new Array();
	if(check.length!=undefined){
		for (var i=0; i<check.length; i++){
			if (check[i].checked) sel.push(check[i].value);
		}
	}else{
		if (check.checked) sel.push(check.value);
	}
	//Memorizzo sessione
	var url = 'index.php?module=Risorse&file=agenda&act=store_ids&ids_cal='+sel.join(',')+'&multipleselector='+multiple_selector.join(',');
	new Ajax.Request(url, { 
		onSuccess: function(transport){
			reload_planner()
		} 
	});
}

function update_r_session_planner(rids){
	//Memorizzo sessione
	var url = 'index.php?module=Risorse&file=agenda&act=store_r_ids&r_ids_cal='+rids;
	new Ajax.Request(url, { 
		onSuccess: function(transport){
			reload_planner();
		} 
	});
}


function update_session(giorno,mese,anno,id,w,h){
	if (document.filter_hash.sel_risorse_umane!=undefined){	
		var check = document.filter_hash.sel_risorse_umane;
	}else{
		var check = document.filter_hash2.sel_risorse_umane;
	}
	var sel = getMultipleCK(check);
	//Memorizzo sessione
	var url = 'index.php?module=Risorse&file=agenda&act=store_ids&ids_cal='+sel.join(',');
	new Ajax.Request(url, { onSuccess: function(transport){update_cal(transport.responseText,giorno,mese,anno,id,w,h);} });
}

function update_r_session(giorno,mese,anno,id,w,h){
	var check = document.filter_hash_tcn.risorse_tecniche;
	var sel = new Array();
	if(check.length!=undefined){
		for (var i=0; i<check.length; i++){
			if (check[i].checked) sel.push(check[i].value);
		}
	}else{
		if (check.checked) sel.push(check.value);
	}
	//Memorizzo sessione
	var url = 'index.php?module=Risorse&file=agenda&act=store_r_ids&r_ids_cal='+sel.join(',');
	new Ajax.Request(url, { onSuccess: function(transport){update_cal(transport.responseText,giorno,mese,anno,id,w,h);} });
}

function cancella_elemento(prot,tipo){
	switch (tipo){
		case 'A':
			desc = 'l\'attivitˆ N¡'+prot;
			break;
		case 'F':
			desc = 'la fattura N¡'+prot;
			break;
		case 'C':
			desc = 'il controllo N¡'+prot;
			break;
	}
	var conf = confirm("Vuoi cancellare "+desc+"?\nL'azione e' irreversibile");
	if (conf)
		new Ajax.Request('index?module='+gup('module')+'&file='+gup('file')+'&id='+prot+'&tipo='+tipo, { onSuccess: function() { window.location.reload(); } });
}

function update_cal(content,giorno,mese,anno,id,w,h){
	new Ajax.Request('index.php?module=Risorse&file=agenda&act=mesi_cal&giorno='+giorno+'&mese='+mese+'&anno='+anno+'&id='+id+'&w='+w+'&h='+h, {
		onSuccess: function(transport){
			update_attivita(transport.responseText,id,giorno,mese,anno);
		}
	});
}

function update_attivita(content,id,giorno,mese,anno){
	$(id).innerHTML = content;
	new Ajax.Updater('agende','index.php?module=Risorse&file=agenda&act=agende&giorno='+giorno+'&mese='+mese+'&anno='+anno)
}

function load_agenda_dett(tecn,time){
	if (tecn!=0){
		Ajax_Tooltip_close('giornaliero');
		openTooltip('index.php?module=Risorse&file=agenda&act=day_ag&data='+time+'&tecn='+tecn,'giornaliero');
	}
}

function fill_hidden_res(res){
	$('risorse').value = getMultiple(res);
}

function showtodo(){
	var todo = $('todo');
	if (todo.style.display=='none'){
		todo.style.display='block'
	}else{
		todo.style.display='none'
	}
}
function showtodo_home(){
	var todo = $('todo_home');
	if (todo.style.display=='none'){
		todo.style.display='block'
	}else{
		todo.style.display='none'
	}
}

function load_agenda(tecnico){
	datareport = $('dataora_mysql_data').value;
	location.href='index.php?module=Risorse&file=agenda&id='+ tecnico +'&data='+datareport; 
}

function load_agenda_data(gg,mm,aa,timestamp){
	gg = (gg<10) ? "0"+gg : gg;
	mm = (mm<10) ? "0"+mm : mm;
	$('dataora_data').value = timestamp;
	$('dataora_mysql_data').value = aa+'-'+mm+'-'+gg;
	tecnico = $('tecnico_0').value;
	datareport = $('dataora_mysql_data').value;
	location.href='index.php?module=Risorse&file=agenda&id='+ tecnico +'&data='+aa+'-'+mm+'-'+gg;
}

function storna(id){
	var bool = confirm("Vuoi cancellare la fattura?\nL'azione e' irreversibile");
	if (bool){
		new Ajax.Request('index.php?module='+((version=='v1') ? 'Contabilita' : 'Amministrazione')+'&file=fatturazione&id='+id+'&act=storna', {
			onSuccess: reload_planner
		});
	}
	return false;
}

function stampa_doc(module,file,movimento,causale,ask){
	if (ask==1){
		var conf = confirm("Rendere definitivo il documento?");
		var def = (conf) ? "SI" : "NO";
	}
	var pop = window.open("index.php?module="+module+"&file="+file+"&id="+movimento+"&defcausale="+causale+"&act=print"+"&def="+def,"doc");
	if (def="SI") {
		window.setTimeout(function(){
			var url = getFilterUrl();
			new Ajax.Updater('main_obj', url+'&request=ajax');	
		}, 500);
	}
}
