/* functions.js, 06/01/2005
   
   Funzioni JavaScript utilizzate nel progetto
*/

function Ricerca(IDtabella,voce,valore){
tdh=document.getElementById(IDtabella).tHead.rows[0].cells;
for(i=0;i<tdh.length;i++){
    htext=tdh[i].firstChild.nodeValue;
    if(htext==voce)
        colonna=i;
    }
trs=document.getElementById(IDtabella).tBodies[0].rows;
for(i=0;i<trs.length;i++){
    testo=trs[i].cells[colonna].firstChild.nodeValue;
    if (!(testo === valore)) trs[i].style.display="none";
    }
}

function Ripristina(IDtabella){
trs=document.getElementById(IDtabella).tBodies[0].rows;
for(i=0;i<trs.length;i++){
    trs[i].style.display="";
    }
}

function NascondiColonna(IDtabella,voce){
ModificaColonna(IDtabella,voce,"none");
}

function MostraColonna(IDtabella,voce){
ModificaColonna(IDtabella,voce,"");
}

function ModificaColonna(IDtabella,voce,display){
ths=document.getElementById(IDtabella).tHead.rows[0].cells;
for(i=0;i<ths.length;i++){
    htext=ths[i].firstChild.nodeValue;
    if(htext==voce)
        colonna=i;
    }
if(colonna>=ths.length) return;
ths[colonna].style.display=display;
trs=document.getElementById(IDtabella).tBodies[0].rows;
for(i=0;i<trs.length;i++){
    tds=trs[i].cells;
    tds[colonna].style.display=display;
    }
}

function RipristinaColonne(IDtabella){
var trs=document.getElementById(IDtabella).tHead.rows;
ths=trs[0].cells;
for(i=0;i<ths.length;i++) ths[i].style.display="";
trs=document.getElementById(IDtabella).tBodies[0].rows;
for(i=0;i<trs.length;i++){
    tds=trs[i].cells;
    for(j=0;j<tds.length;+j++)
        tds[j].style.display="";
    }
}


function CambiaTitolo(IDtabella, valore) {
  ths=document.getElementById(IDtabella).tHead.rows[0].cells;
  ths[1].firstChild.nodeValue = valore;
}


/**
 * Questionario P.F
 */
function questionariopf(block) {
  switch (block) {
  case 0:
	if (document.formIndagine.Q0.checked) {
		document.formIndagine.Q1.checked = false;
		document.formIndagine.Q1.disabled = true;
		document.formIndagine.Q2.checked = false;
		document.formIndagine.Q2.disabled = true;
		document.formIndagine.Q3.checked = false;
		document.formIndagine.Q3.disabled = true;
		document.formIndagine.Q4.checked = false;
		document.formIndagine.Q4.disabled = true;
		document.formIndagine.Q5.checked = false;
		document.formIndagine.Q5.disabled = true;
		document.formIndagine.Q6.checked = false;
		document.formIndagine.Q6.disabled = true;
		document.formIndagine.Q7.checked = false;
		document.formIndagine.Q7.disabled = true;
		document.formIndagine.Q8.checked = false;
		document.formIndagine.Q8.disabled = true;			 
	}
	else
	{
		document.formIndagine.Q1.disabled = false;
		document.formIndagine.Q2.disabled = false;
		document.formIndagine.Q3.disabled = false;
		document.formIndagine.Q4.disabled = false;
		document.formIndagine.Q5.disabled = false;
		document.formIndagine.Q6.disabled = false;
		document.formIndagine.Q7.disabled = false;
		document.formIndagine.Q8.disabled = false;
	}
	break;
  case 1:
	if (document.formIndagine.Q9.checked) {
		document.formIndagine.Q10.checked = false;
		document.formIndagine.Q10.disabled = true;
		document.formIndagine.Q11.checked = false;
		document.formIndagine.Q11.disabled = true;
		document.formIndagine.Q12.checked = false;
		document.formIndagine.Q12.disabled = true;
		document.formIndagine.Q13.checked = false;
		document.formIndagine.Q13.disabled = true;
		document.formIndagine.Q14.checked = false;
		document.formIndagine.Q14.disabled = true;
		document.formIndagine.Q15.checked = false;
		document.formIndagine.Q15.disabled = true;		
	}
	else
	{
		document.formIndagine.Q10.disabled = false;
		document.formIndagine.Q11.disabled = false;
		document.formIndagine.Q12.disabled = false;
		document.formIndagine.Q13.disabled = false;
		document.formIndagine.Q14.disabled = false;
		document.formIndagine.Q15.disabled = false;				
	} 
   break;
  } 
}


/**
 * Questionario MUS
 */
function mus() {
	if (document.formIndagine.S14.checked) {
		if (document.formIndagine.S0) {
            // Versione 1.4
            document.formIndagine.S0.checked = false;
		    document.formIndagine.S0.disabled = true;
		    document.formIndagine.S1.checked = false;
		    document.formIndagine.S1.disabled = true;
		    document.formIndagine.S2.checked = false;
		    document.formIndagine.S2.disabled = true;
		    document.formIndagine.S3.checked = false;
		    document.formIndagine.S3.disabled = true;
		    document.formIndagine.S4.checked = false;
		    document.formIndagine.S4.disabled = true;
		    document.formIndagine.S5.checked = false;
		    document.formIndagine.S5.disabled = true;
		    document.formIndagine.S6.checked = false;
		    document.formIndagine.S6.disabled = true;
		    document.formIndagine.S7.checked = false;
		    document.formIndagine.S7.disabled = true;
		    document.formIndagine.S8.checked = false;
		    document.formIndagine.S8.disabled = true;
		    document.formIndagine.S9.checked = false;
		    document.formIndagine.S9.disabled = true;
        } else {
            // Versione 1.5
            document.formIndagine.S20.checked = false;
            document.formIndagine.S20.disabled = true;
            document.formIndagine.S21.checked = false;
            document.formIndagine.S21.disabled = true;
            document.formIndagine.S22.checked = false;
            document.formIndagine.S22.disabled = true;
            document.formIndagine.S23.checked = false;
            document.formIndagine.S23.disabled = true;
            document.formIndagine.S24.checked = false;
            document.formIndagine.S24.disabled = true;
            document.formIndagine.S25.checked = false;
            document.formIndagine.S25.disabled = true;
            document.formIndagine.S26.checked = false;
            document.formIndagine.S26.disabled = true;
            document.formIndagine.S27.checked = false;
            document.formIndagine.S27.disabled = true;
            document.formIndagine.S28.checked = false;
            document.formIndagine.S28.disabled = true;
            document.formIndagine.S29.checked = false;
            document.formIndagine.S29.disabled = true;
            document.formIndagine.S30.checked = false;
            document.formIndagine.S30.disabled = true;
            document.formIndagine.S31.checked = false;
            document.formIndagine.S31.disabled = true;
            document.formIndagine.S32.checked = false;
            document.formIndagine.S32.disabled = true;
            document.formIndagine.S33.checked = false;
            document.formIndagine.S33.disabled = true;
            document.formIndagine.S34.checked = false;
            document.formIndagine.S34.disabled = true;            
        }
	}
	else
	{
        if (document.formIndagine.S0) {
            // versione 1.4
		    document.formIndagine.S0.disabled = false;	
		    document.formIndagine.S1.disabled = false;	
		    document.formIndagine.S2.disabled = false;	
		    document.formIndagine.S3.disabled = false;	
		    document.formIndagine.S4.disabled = false;	
		    document.formIndagine.S5.disabled = false;	
		    document.formIndagine.S6.disabled = false;	
		    document.formIndagine.S7.disabled = false;	
		    document.formIndagine.S8.disabled = false;	
		    document.formIndagine.S9.disabled = false;	
        } else {
            // versione 1.5
            document.formIndagine.S20.disabled = false;    
            document.formIndagine.S21.disabled = false;    
            document.formIndagine.S22.disabled = false;    
            document.formIndagine.S23.disabled = false;    
            document.formIndagine.S24.disabled = false;    
            document.formIndagine.S25.disabled = false;    
            document.formIndagine.S26.disabled = false;    
            document.formIndagine.S27.disabled = false;    
            document.formIndagine.S28.disabled = false;    
            document.formIndagine.S29.disabled = false;    
            document.formIndagine.S30.disabled = false;    
            document.formIndagine.S31.disabled = false;    
            document.formIndagine.S32.disabled = false;    
            document.formIndagine.S33.disabled = false;    
            document.formIndagine.S34.disabled = false;                
        }
	}
}
 

/**
 * Estende il questionario per i vegetariani
 */
function vegetariani() {
	if (!document.formIndagine.S10.checked) {
       document.formIndagine.S11.checked = false;
	   document.formIndagine.S12.checked = false;
	   document.formIndagine.S13.checked = false;
	   document.formIndagine.S11.disabled = true;
	   document.formIndagine.S12.disabled = true;
	   document.formIndagine.S13.disabled = true;
    } else {
	   document.formIndagine.S11.disabled = false;
	   document.formIndagine.S12.disabled = false;
	   document.formIndagine.S13.disabled = false;
    }
}
	 

function checkAlimento(dose1,dose2,dose3) {
    if (document.getElementById(dose3).value == "0") {
        document.getElementById(dose1).style.display=""; 
        document.getElementById(dose2).style.display="none"; 
        document.getElementById(dose3).value = "1";
    } else if (document.getElementById(dose3).value == "1") {
        document.getElementById(dose1).style.display=""; 
        document.getElementById(dose2).style.display=""; 
        document.getElementById(dose3).value = "2";
    } else {
        document.getElementById(dose1).style.display="none";
        document.getElementById(dose2).style.display="none";
        document.getElementById(dose3).value = "0";
    }
}


function resetBia() {
    document.formIndagine.tbw.value="";
    document.formIndagine.ecw.value="";
    document.formIndagine.icw.value="";
    document.formIndagine.ffm.value="";
    document.formIndagine.fm.value="";
    document.formIndagine.mb.value="";
    document.formIndagine.pa.value="";
}


/**
 * Permette di inviare i dati immessi al web server 
 */
function submitform(value) {
	document.formIndagine.PD.value = value;
	document.formIndagine.submit();
}


/**
 * Determina la dimensione della barra principale
 */
function sizeMainBar() {
    if (navigator.appName == "Microsoft Internet Explorer") {
        return document.getElementById("mainbar").offsetWidth-150;
    } else
        return document.getElementById("mainbar").offsetWidth;
}

               
/**
 * Permette di massimizzare la finestra sul desktop per ottimizzare
 *	l'aspetto alla risoluzione 1024x768. 
 */
function max_window() {
	top.window.moveTo(0,0);
	if (document.all) {
		top.window.resizeTo(800 /*screen.availWidth*0.8 */,screen.availHeight);
	}
	else if (document.layers||document.getElementById) {
		if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
			top.window.outerHeight = screen.availHeight;
			top.window.outerWidth = 800;   //screen.availWidth*0.8;
		}
	}
}



/**
 * Crea una nuova finestra a comparsa visualizzando all'interno l'url
 * specificato. E' utilizzato per visualizzare il risultato di un test
 * o di una indagine o un pdf
 */
function show(pageURL, typeURL) {
	/* ricavo la larghezza della finestra principale */
	if (document.all) {
		var x = window.screen.availWidth - 210;
		var h = window.screen.availHeight - 30;
	}	else {
		var x = top.window.outerWidth; /* netscape */
		var h = window.screen.availHeight;
	}
	
	switch (typeURL) {
    case 1: idWT=window.open(pageURL,'windowValuation','width=550,height='+ h.toString() +
                             ',top=0,left=0,scrollbars=yes,'+
                             'resizable=yes,toolbar=no,status=no');
            idWT.window.focus();
            break;
    case 2: idWT=window.open(pageURL,'windowTest','width=550,outerheight='+ h.toString() +
                             ',top=0,left=0,scrollbars=yes,'+
                             'resizable=yes,toolbar=no,status=no');
            /* top.window.focus();*/
            idWT.window.focus();
            break;  
    case 3: h = Math.round(h*0.7);
            idWT=window.open(pageURL,'windowTerminal','location=no,width=580,height='+h.toString()+
                ',fullscreen=no,scrollbars=1,resizable=yes,toolbar=no,status=no');
            idWT.window.focus();
            break;
    case 4: h = Math.round(h*0.7);
            idWT=window.open(pageURL,'windowTerminal','location=no,width=800,height='+h.toString()+
                ',fullscreen=no,scrollbars=1,resizable=yes,toolbar=no,status=no');
            idWT.window.focus();
            break;
    default:idWT=window.open(pageURL,'windowPrint','width=550,height='+ h.toString() +
                             ',top=0,left=0,scrollbars=yes,'+
                             'resizable=yes,toolbar=no,status=no');
            idWT.window.focus();
            break;
 	}
    if (this.idt) {
        if (idt.opener == null) idt.opener = self;
    }
}



/**
 caricamento url con rilevamento della larghezza di pagina
 */
function redirect(url){
    window.location = url+"&W="+sizeMainBar();
}



/**
 * Richiede la conferma di annullamento dell'indagine
 */
function annulla_indagine(URLAnnulla) {
	if (URLAnnulla) {
		if (window.confirm("Sei sicuro di annullare l'indagine?"))
			location=URLAnnulla;
	}
	else 
		return window.confirm("Sei sicuro di annullare l'indagine?");
}



/**
 * Richiede la conferma di annullamento dell'indagine
 */
function annulla_eq5d(URLAnnulla) {
	if (URLAnnulla) {
		if (window.confirm("Sei sicuro di annullare il questionario EQ-5D?"))
			location=URLAnnulla;
	}
	else 
		return window.confirm("Sei sicuro di annullare il questionario EQ-5D?");
}

/**
 * Richiede di entrare in modalità correzione della sequenza
 */
function confirmValuation() {
    if (window.confirm("Sei sicuro di salvare l'indagine?")) {
        document.formIndagine.SaveExit.value="T";
        return true;
    } else
        return false;
    /*
    Versione 1.4: dopo il salvataggio si poteva proseguire con la simulazione. Dalla 1.5 non è più possibile !
    if (window.confirm("Indagine salvata.\nVuoi proseguire con la correzione della sequenza  nutrizionale?"))
        document.formIndagine.SaveExit.value="F";
    else
        document.formIndagine.SaveExit.value="T";

    return true;
    */
}


function exitValuation(URLExit) {
    if (URLExit) {
        if (window.confirm("Uscire dalla modalità di Correzione sequenza nutrizionale?\nAttenzione: L'elaborazione simulata non viene salvata."))
            location=URLExit;
    }
    else
        return window.confirm("Uscire dalla modalità di Correzione sequenza nutrizionale?\nAttenzione: L'elaborazione simulata non viene salvata.");
}

/**
 * Visualizza un box di conferma prima di confermare la cancellazione di un
 * record 
 *
 * @param   object   the link
 * @param   object   messaggio da confermare
 *
 * @return  boolean  ritorna se il messaggio è stato confermato o meno
 */
function confirmLink(theLink, theConfirmMsg)
{
    if (confirmMsg == '')
        return true;

    var is_confirmed = confirm(confirmMsg + theConfirmMsg);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';         
    }
    is_confirmed;
} 



/**
 * Visualizza l'url specificato nella barra di destra
 */
function showTest(url) {
	testFrame.location.href = url;
}

function stampa(url) {
	window.open(url,'windowPrint','width=300,height=500,top=50,left=50,scrollbars=yes,'+
	                'resizable=yes,titlebar=no,toolbar=no,status=no,menubar=no,location=no,directories=no');
}


/**
 * Rilevamento della pressione del mouse nell'indice di salute
 */
function barEQ5D(img,e) {
  var posX = 0;
  var posY = 0;
  if (!e) var e = window.event;

  if (navigator.appName == "Microsoft Internet Explorer") {
    posY = e.clientY - 132;
  }
  else {
    posY = e.pageY - eval("document."+img+".y") - 105;
  }
   
  var barHeight = eval("document."+img+".height") - 10;	
	
  document.formEQ5D.barY.value = (barHeight - posY)/barHeight*100;	
  document.formEQ5D.submit();		
}


function editbiatom(biaid, serial, state) {
    document.getElementById("BiaTomID").value=biaid;
    document.getElementById("SerialNumber").value=serial;
    if (state=="Active")
        document.getElementById("State").checked=true;
    else
        document.getElementById("State").checked=false;
}

function editbiaacc(biaid, serial, state) {
    document.getElementById("BiaID").value=biaid;
    document.getElementById("SerialNumber").value=serial;
    if (state=="Active")
        document.getElementById("State").checked=true;
    else
        document.getElementById("State").checked=false;
    
}