
var h_cms;
var w_kgs;
var h;
var w;
var bmi;


function dispnum(x) {
	x = Math.floor(x*10)/10; // rounded off to two decimal places
	return(x);
}

function clearvalues(){ // clears all the contents of the form
	document.data.centimetri.value=''
	document.data.weight_kgs.value=''
	document.data.bmi.value=''
	document.data.expl.value=''
	document.data.max_kgs.value=''
}

function initialise() { // initialise the variables
	if (document.data.centimetri.value == '') {
		document.data.centimetri.value = 0;
	}

	if (document.data.weight_kgs.value == '') {
		document.data.weight_kgs.value = 0;
	}
}

function compute() { // the main routine

	initialise();

	h_cms = parseFloat(document.data.centimetri.value);
	
	h = h_cms/100;

	if (h <= 1.22) {
		alert(dispnum(h)+" Metri sembra troppo basso\n\nPlease try again.");
	}
	if (h >= 2.13) {
		alert(dispnum(h)+" Metri sembra troppo alto\n\nPlease try again.");
	}

	w_kgs = parseFloat(document.data.weight_kgs.value);

	w=w_kgs;

	if (w <= 25) {
		alert(dispnum(w)+" Kgs? Troppo leggero\n\nPlease try again.");
	}
	if (w >= 227) {
		alert(dispnum(w)+" Kgs? Troppo pesante\n\nPlease try again.");
	}
	tempbmi = w / (h*h);
	bmi = Math.floor(tempbmi*10)/10;
	max_kgs = 25*h*h;

	document.data.bmi.value = dispnum(bmi);
	if (bmi < 18.5 )
		document.data.expl.value = "Sottopeso - potresti anche incrementarlo";
	if (bmi >= 18.5 && bmi <= 25 )
		document.data.expl.value = "OK, nel range del peso raccomandato, prova a mantenerlo";
	if (bmi > 25 && bmi <= 30 )
		document.data.expl.value = "Sovrappeso, assicurati che non salga ulteriormente";
	if (bmi > 30 )
		document.data.expl.value = "Obeso, è importante che perdi peso";

document.data.max_kgs.value = dispnum(max_kgs);
}
