var 
myWindow = '';
cifre = "0123456789";


function popup(url) {
	if (!myWindow.closed && myWindow.location) {
		myWindow.location.href = url;
	}else {
		myWindow = window.open(url,'myWindow','scrollbars=1,resizable=1,height=720,width=650,left=0,top=0');
	}
	
	if (window.focus) {myWindow.focus()}
	return false;
}


function stampa(){
	if (valida()){
		with (document.modulo){
			stringa = ""
			stringa = stringa + "a=" + indiceMC.value
			stringa = stringa + "=" + indiceMCtesto.value
			stringa = stringa + "=" + biotipo.value
			stringa = stringa + "=" + biotipoTesto.value
			stringa = stringa + "=" + costCorporeaTesto.value
			stringa = stringa + "=" + pesoForma.value
			stringa = stringa + "=" + sovrappeso.value
			stringa = stringa + "=" + carboidrati.value
			stringa = stringa + "=" + calorieG1.value
			stringa = stringa + "=" + proteine.value
			stringa = stringa + "=" + calorieG2.value
			stringa = stringa + "=" + lipidi.value
			stringa = stringa + "=" + calorieG3.value
			stringa = stringa + "=" + calTOT.value
			
			stringa = stringa + "=" + nome.value
			stringa = stringa + "=" + sessoTesto.value
			stringa = stringa + "=" + eta.value
			stringa = stringa + "=" + altezza.value
			stringa = stringa + "=" + peso.value
			stringa = stringa + "=" + pesoDec.value
			stringa = stringa + "=" + circVita.value
			stringa = stringa + "=" + circFianchi.value
			stringa = stringa + "=" + circPolso.value
			
			popup("stampa.htm?"+stringa)
		}
	}
}

function controllaNumero(numero) {
	if (!check_num(numero.value)){
		alert("Inserisci solo cifre in questo campo");
		numero.value = numero.value.toString().replace(/[\D^0+]/g,"");
		numero.focus();
		return(false);
	}
}
function controllaDecimali(numero) {
	if (numero.value.length < 2) {
		numero.value=numero.value + "0";
		numero.focus();
		return(false);
	}
	azzera()
}
function check_empty(testo) {
  return (testo.length>0);
} //falso se vuoto

function check_num(stringa){
  i=0
  buona=true
  while(i<=stringa.length){
    car=stringa.substring(i,i+1);
    if(cifre.indexOf (car) == -1)
      {buona=false; break;}
  i++}
  return(buona);
}

function azzera(){
	with (document.modulo){
		indiceMC.value = ""
		indiceMCtesto.value = ""
		biotipo.value = ""
		biotipoTesto.value = ""
		costCorporeaTesto.value = ""
		pesoForma.value = ""
		sovrappeso.value = ""
		carboidrati.value = ""
		calorieG1.value = ""
		proteine.value = ""
		calorieG2.value = ""
		lipidi.value = ""
		calorieG3.value = ""
		calTOT.value = ""
	}
}

function cambiaSesso(sesso){
	azzera();
	if (sesso == "M") {
		document.modulo.coefficientePF.value = "22.5";
		document.modulo.coefficienteCOST.value = "2.0";
		document.modulo.coefficienteCarboidrati.value = "2.8";
		document.modulo.sessoTesto.value = "M";
	}else{
		document.modulo.coefficientePF.value = "20.8";
		document.modulo.coefficienteCOST.value = "1.8";
		document.modulo.coefficienteCarboidrati.value = "2.7";
		document.modulo.sessoTesto.value = "F";
	}

}
function calcola(interi,decimali) {

	var reale = interi+decimali;
	
	reale = parseInt(reale);
	reale = reale/100;
	return(reale)

}

function arrotonda(valore){
	var a=valore
	a=Math.round(a*100)/100;
	return(a)
}


function setta(){
	with (document.modulo){
		nome.value = "Massimo"
		eta.value = "62"
		altezza.value = "170"
		peso.value = "80"
		pesoDec.value = "00"
		circVita.value = "95"
		circFianchi.value = "101"
		circPolso.value = "18"
	}	
}

function valida(){
	
	with (document.modulo){
			
		if (nome.value.length == 0 ){
			alert("Inserisci il nome");
			nome.focus();
			return(false);
		}

		if (eta.value == 0 ){
			alert("Inserisci l'età");
			eta.focus();
			return(false);
		}
		
		if (altezza.value == 0 ){
			alert("Inserisci l'altezza");
			altezza.focus();
			return(false);
		}
		
		pesoReale=calcola(peso.value,pesoDec.value)	
		if (pesoReale==0){
			alert("Inserisci il peso");
			peso.focus();
			return(false);
		}
		
		if (circVita.value == 0 ){
			alert("Inserisci la circonferenza vita");
			circVita.focus();
			return(false);
		}
		
		if (circFianchi.value == 0 ){
			alert("Inserisci la circonferenza fianchi");
			circFianchi.focus();
			return(false);
		}
		altezzaAlQuadrato = (altezza.value/100*altezza.value/100)
		indiceMC.value = arrotonda(pesoReale/altezzaAlQuadrato)

		if (sessoTesto.value == "M") {
			
			if (indiceMC.value <=20 ){
				indiceMCtesto.value = "Sottopeso";
			}
			if (indiceMC.value > 20 && indiceMC.value <= 25  ){
				indiceMCtesto.value = "Peso Normale";			
			}
			if (indiceMC.value > 25 && indiceMC.value <= 30  ){
				indiceMCtesto.value = "Sovrappeso";			
			}
			if (indiceMC.value > 30 && indiceMC.value <= 40  ){
				indiceMCtesto.value = "Obesità di medio grado";			
			}
			if (indiceMC.value > 40 ){
				indiceMCtesto.value = "Obesità alto grado";			
			}

		
		}
		
		if (sessoTesto.value == "F") {
			
			if (indiceMC.value <=18.7 ){
				indiceMCtesto.value = "Sottopeso";
			}
			if (indiceMC.value > 18.7 && indiceMC.value <= 23.8  ){
				indiceMCtesto.value = "Peso Normale";			
			}
			if (indiceMC.value > 23.8 && indiceMC.value <= 28.6  ){
				indiceMCtesto.value = "Sovrappeso";			
			}
			if (indiceMC.value > 28.6 && indiceMC.value <= 40  ){
				indiceMCtesto.value = "Obesità di medio grado";			
			}
			if (indiceMC.value > 40 ){
				indiceMCtesto.value = "Obesità alto grado";			
			}

		
		}
		
		biotipo.value = arrotonda(circVita.value/circFianchi.value)
		if (biotipo.value >= 0.84 ){
			biotipoTesto.value = "Androide";			
		}else{
			biotipoTesto.value = "Ginoide";
		}
			
		
		PF = (altezzaAlQuadrato*coefficientePF.value)
		
		
		costCorporea = arrotonda(altezza.value/circPolso.value)
			if (sessoTesto.value == "M") {
				
				if (costCorporea >= 10.4){
					costCorporeaTesto.value = "Minuta";
					addizionalePesoForma = - arrotonda(PF/10);
								
				}
				if (costCorporea > 9.6 && costCorporea < 10.4  ){
					costCorporeaTesto.value = "Normale";
					addizionalePesoForma = 0;			
				}
				if (costCorporea <= 9.6){
					costCorporeaTesto.value = "Robusta";
					addizionalePesoForma = arrotonda(PF/10);
									
				}
			
			}
			if (sessoTesto.value == "F") {
				
				if (costCorporea >= 11){
					costCorporeaTesto.value = "Minuta";
					addizionalePesoForma = - arrotonda(PF/10);
								
				}
				if (costCorporea > 10.1 && costCorporea < 11 ){
						costCorporeaTesto.value = "Normale";
						addizionalePesoForma = 0;			
				}
				if (costCorporea <= 10.1){
					costCorporeaTesto.value = "Robusta";
					addizionalePesoForma = arrotonda(PF/10);
									
				}
			
			}

			pesoForma.value = arrotonda(PF + addizionalePesoForma);
			sovrappeso.value = arrotonda(pesoReale-pesoForma.value)
			carboidrati.value = arrotonda(pesoForma.value*coefficienteCarboidrati.value)
			calorieG1.value = arrotonda(carboidrati.value*coefficienteCalorieCarboidrati.value)
			proteine.value = arrotonda(carboidrati.value/coefficienteProteine.value)
			calorieG2.value = arrotonda(proteine.value*coefficienteCalorieProteine.value)
			lipidi.value = arrotonda(carboidrati.value/coefficienteLipidi.value)
			calorieG3.value = arrotonda(lipidi.value*coefficienteCalorieLipidi.value)
			calTOT.value = (calorieG1.value*100+calorieG2.value*100+calorieG3.value*100)/100
	}
		return(true);
}
