
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function checkbox_state(id)
{	
	alert("disabled = "+this.document.getElementById(id).getAttribute("disabled"));
		//elem=this.document.getElementById(id);
		//alert ("CCC  "+this.document.getElementById(id).getAttribute('checked'));
	if (this.document.getElementById(id).getAttribute("disabled") == null)
			{
				alert("RAMO IF");
			this.document.getElementById(id).setAttribute("disabled","");
			}
	else {
			alert("RAMO ELSE");
			this.document.getElementById(id).disabled=true;
		  }
}

function cambia_tab(tab){ 
	for(i=1;i<=2;i++){
		this.document.getElementById('tab_'+i).className='tab_invisibile';
		this.document.getElementById('barra_tab_'+i).className='tab_no_sel';
	}
	this.document.getElementById('tab_'+tab).className='tab_visibile';
	this.document.getElementById('barra_tab_'+tab).className='tab_sel';
}

//E' usata nell'assistiti_form.tpl
//es. riempi_campi('citt_resi=citt_domi|cap__resi=cap__domi|tele_resi=tele_domi');
function riempi_campi(campi){
	elem=campi.split("|");
	for(i=0;i<elem.length;i++){
		aux=elem[i].split("=");
		this.document.getElementById(aux[0]).value=this.document.getElementById(aux[1]).value;
		}
}

function pres_ass(id,n){
	
	valore=this.document.getElementById(id).value;
	valore_old=this.document.getElementById(id+'_old').value;
	if(valore==valore_old) return true;
	
	if(valore=='P' || valore=='A' || valore=='F' || valore=='E' || valore=='S' || trim(valore)=='')
		  {			
		  }
	else  {
			alert("Errore!il valore immesso non è corretto!");
			this.document.getElementById(id).value=this.document.getElementById(id+'_old').value;
			//this.document.getElementById(id).focus();
			return false;
		  }

		
	if(valore=='P')
		this.document.getElementById('num_pres_'+n).value=
			parseInt(this.document.getElementById('num_pres_'+n).value)+1;
	if(valore=='A')
		this.document.getElementById('num_ass_'+n).value=
			parseInt(this.document.getElementById('num_ass_'+n).value)+1;
	if(valore_old=='P')
		this.document.getElementById('num_pres_'+n).value=
			parseInt(this.document.getElementById('num_pres_'+n).value)-1;
	if(valore_old=='A')
		this.document.getElementById('num_ass_'+n).value=
			parseInt(this.document.getElementById('num_ass_'+n).value)-1;
	this.document.getElementById(id+'_old').value=valore;
	return true;
}

//variabile globale
var last_function='generico';

function edit_td(id)
	{
	last_function='edit_td';
	//NASCONDE LA TABELLA DELLA GRIGLIA E VISUALIZZA IL CAMPO DI INPUT
	if(this.document.getElementById(id).style.display=='none')
		{
		this.document.getElementById(id).style.display='block';
		this.document.getElementById('label_'+id).style.display='none';
		this.document.getElementById(id).focus();			
		}
	}


function close_td(id_cur,n,giorno_mese,giorno_mese_max,e){
	
	if(last_function=='edit_next_td'){
			last_function='close_td';
			return;
	}
	last_function='close_td';
	id=id_cur+'_'+giorno_mese;
	if(this.document.getElementById(id).style.display=='block'){
	
			browsername=navigator.appName;
			if (browsername.indexOf("Microsoft")!=-1)
						browsername="microsoft";//IL BROWSER E' INTERNET EXPLORER
			else 		browsername="netscape";//IL BROWSER E' FIREFOX ALTRIMENTI
		 
			if(pres_ass(id,n)){
					if(browsername=="netscape")
							this.document.getElementById('label_'+id).textContent=
													this.document.getElementById(id).value;
					else  this.document.getElementById('label_'+id).nodeValue=
													this.document.getElementById(id).value;
			}
			else {
					if(browsername=="netscape")
						   this.document.getElementById('label_'+id).textContent=
												this.document.getElementById(id+'_old').value;
					else  this.document.getElementById('label_'+id).nodeValue=
												this.document.getElementById(id+'_old').value;
					
			}
			//NASCONDE IL CAMPO DI INPUT E VISUALIZZA LA CELLA DELLA TABELLA
			this.document.getElementById(id).style.display='none';	
			this.document.getElementById('label_'+id).style.display='block';
	}
}

function edit_next_td(id_cur,n,giorno_mese,giorno_mese_max){
		last_function='edit_next_td';
		id=id_cur+'_'+giorno_mese;
		if(this.document.getElementById(id).style.display=='block'){
			browsername=navigator.appName;
			if (browsername.indexOf("Microsoft")!=-1)
						browsername="microsoft";//IL BROWSER E' INTERNET EXPLORER
			else 		browsername="netscape";//IL BROWSER E' FIREFOX ALTRIMENTI
		 
			if(pres_ass(id,n)){
					if(browsername=="netscape")
							this.document.getElementById('label_'+id).textContent=
													this.document.getElementById(id).value;
					else  this.document.getElementById('label_'+id).nodeValue=
													this.document.getElementById(id).value;
			}
			else {
					if(browsername=="netscape")
						   this.document.getElementById('label_'+id).textContent=
												this.document.getElementById(id+'_old').value;
					else  this.document.getElementById('label_'+id).nodeValue=
												this.document.getElementById(id+'_old').value;
			}
			this.document.getElementById(id).style.display='none';	
			this.document.getElementById('label_'+id).style.display='block';
		}
		giorno_mese=parseInt(giorno_mese);
		giorno_mese_max=parseInt(giorno_mese_max);
		if(giorno_mese<giorno_mese_max){
			giorno_mese++;
domenica=(this.document.getElementById(id_cur+'_'+giorno_mese).getAttribute('domenica')=='si')?true:false;
			if(domenica && giorno_mese<giorno_mese_max) 
							giorno_mese++;																							
			edit_td(id_cur+'_'+giorno_mese);				
			}		
}
	
function simple_close_td(id){
	var v_input=this.document.getElementById(id);
	var v_label=this.document.getElementById('label_'+id);
	var v_NewValue=v_input.value;
	//AGGIORNA IL CONTENUTO DELLA CELLA
	if(v_input.style.display=='block')
		{	
		//AGGIORNA IL CONTENUTO DELLA CELLA
		v_node=v_label.firstChild;
		v_node.nodeValue=v_NewValue;
		}
	//NASCONDE LA CASELLA DI INPUT E VISUALIZZA LA CELLA DELLA TABELLA
	v_input.style.display='none';	
	v_label.style.display='block';	
}
	
	
	// Funzione di validazione campi
function validate(p_flds,p_parm)
{
		if (this.document.getElementById("fsub").value=="true")
			return;
		a_flds=p_flds.split('|');
		var f_validate=true;
		var v_warn="";
		var n_flds=0;
		str=this.document.getElementById("warnings");
		for(i=0; i<a_flds.length; i++)
			{
				if(this.document.getElementById("label_"+a_flds[i]))
					{
					var o_flds=this.document.getElementById(a_flds[i]);
					var o_etic=this.document.getElementById("label_"+a_flds[i]);
					// Imposta il formato dell'etichetta a normale
					o_etic.className="label";
					o_etic.innerHTML = o_etic.innerHTML.replace('*','');
					if (trim(o_flds.value)=="")
						{
						v_warn+=o_etic.innerHTML.replace(':','')+", ";
						o_etic.className="label_err";
						o_etic.innerHTML+="*";
						n_flds++;
						f_validate=false;
						}
					}
			}
		if(v_warn!=""){ 
			v_warn=v_warn.slice(0,-2); //elimina gli ultimi 2 caratteri ", " 
			if (n_flds==1)
				v_warn+=" &egrave; un campo obbligatorio!";
			else
				v_warn+=" sono campi obbligatori!";
			this.document.getElementById("warnings").className="warnings";
			this.document.getElementById("warnings").innerHTML=v_warn;
			}
		else 
			{
			this.document.getElementById("warnings").className="";
			this.document.getElementById("warnings").innerHTML="<br>";//Nessun messaggio
			}
		if(!f_validate) return;
		if(!chkPassword()) return;
		if (f_validate) 
			{
				chk_select();
				this.document.getElementById("fmod").value='false';
				this.document.getElementById("fsub").value="true";
				doaction(p_parm);
			}
	
}

// Funzione di validazione campi per la funzione salva_dati()
function validsav(p_flds)
{	
	a_flds=p_flds.split('|');
	if(a_flds.length < 1) return true;
	for(i=0; i<a_flds.length; i++)
		{	
			if (trim(this.document.getElementById(a_flds[i]).value)=="")
				return false;
		}
	return true;
}

function chkPassword()
{
		if(!this.document.getElementById('UserPass') || !this.document.getElementById('chk_Pass'))
			return true;
		var f_pass=this.document.getElementById('UserPass');
		var f_conf=this.document.getElementById('chk_Pass');
		if(f_pass.value != f_conf.value)
		{
			this.document.getElementById("warnings").className="warnings";
			this.document.getElementById("warnings").innerHTML="I campi password non coincidono";
			return false;
		}
		else 
		{
			this.document.getElementById("warnings").className="";
			this.document.getElementById("warnings").innerHTML="<br>";//Nessun messaggio
			return true;
		}
}

function showtabs(p_tabs)
{
	n_tabs=this.document.getElementById("n_tabs").value;
	for(c_tabs=1; c_tabs<=n_tabs; c_tabs++)
	{
		c_divtab="divtab"+c_tabs;
		c_tittab="tittab"+c_tabs;
		if(c_tabs==p_tabs) {
			this.document.getElementById(c_divtab).setAttribute("style", "display:block;")
			this.document.getElementById(c_tittab).className="tab_sel";
		}
		else {
			this.document.getElementById(c_divtab).setAttribute("style", "display:none;")
			this.document.getElementById(c_tittab).className="tab";			
		}
		
	}
}

//SPOSTA LE OPZIONI SELEZIONATE DALLA SELECT DI ORIGINE A QUELLA DI DESTINAZIONE
function spostasel(p_orig, p_dest)
{
	var v_orig=this.document.getElementById(p_orig);
	var v_dest=this.document.getElementById(p_dest);	
	var n_dest=v_dest.options.length
	
	for(i=0; i<v_orig.options.length; i++){
		
		if(v_orig.options[i].selected){
			//aggiunge il valore selezionato alla destinazione
			v_opts=v_orig.options[i].value;
			v_text=v_orig.options[i].innerHTML;			
			v_dest.options[n_dest]=new Option('',escape(v_opts),false,false);
			v_dest.options[n_dest].innerHTML = v_text;
			n_dest+=1;
			
			//rimuove i valori selezionati dall'origine
			v_orig.options[i]=null;
		}
	}
	// Ordina la select di destinazione
	var arrOptions = [];
	for (var i = 0; i < v_dest.options.length; i++)
	{        
		arrOptions[i] = [];
        arrOptions[i][1] = v_dest.options[i].value;
        arrOptions[i][0] = v_dest.options[i].text;
        arrOptions[i][2] = v_dest.options[i].selected;
	}    
	arrOptions.sort();
	for (var i = 0; i < v_dest.options.length; i++)
	{        
		v_dest.options[i].value = arrOptions[i][1];        				
		v_dest.options[i].text = arrOptions[i][0];        							
		v_dest.options[i].selected = arrOptions[i][2];  
	}	
	 		
}
// Controlla le select che hanno l'attributo "all" impostato a "yes" e seleziona tutte le options
function chk_select()
{
	var frm_flds = this.document.getElementById("content");
	fields=frm_flds.getElementsByTagName("select");
	for (l_count=0; l_count<fields.length; l_count++)
		{
			v_select=fields[l_count];
			if(v_select.getAttribute("all")=="yes")
			{
				for(i=0; i<v_select.options.length;i++){
					v_select.options[i].selected=true;
				} 
			}
		}
}

function show_fields(p_chk, p_fields)
{
	var v_label="label_"+p_fields;
	if(this.document.getElementById(p_chk).checked)
	{
			if(this.document.getElementById(p_fields))
				this.document.getElementById(p_fields).setAttribute("style", "display:block");
			if(this.document.getElementById(v_label))
				this.document.getElementById(v_label).setAttribute("style", "display:block");		
	}
	else {
		if(this.document.getElementById(p_fields))
		{
				this.document.getElementById(p_fields).setAttribute("style", "display:none");
				this.document.getElementById(p_fields).value="";
		}
		if(this.document.getElementById(v_label))
			this.document.getElementById(v_label).setAttribute("style", "display:none");
	}
}

