//JavaScript developed by Dr. TK Shiao, Office of Medical Informatics, KU Medical Center

//function roundToXDigits takes the number n and round it
//to the x-th decimal place.  For example:
//roundToXDigits(23.567,1) returns the value 23.6
//If x is less than 0, it is treated as 0 and the
//decimal gets rounded to the closest integer.
var teza1=0;
var teza2=0;
var teza3=0;
var teza4=0;
var teza5=0;




function roundToXDigits(n,x)
{
	if (x < 0) x = 0;
	temp = n;
	for (i=1; i<=x; i++) temp = temp *10;
   temp = Math.round(temp);
   strTemp = "" + temp;
	if (x == 0)
	{
      return strTemp;
	}
	else
	{
   	len = strTemp.length;
   	return strTemp.substring(0, len-x) + "." + strTemp.substring(len-x, len);
	}
}
//-----------------------------------------
//This function computes body mass index based on
//height, weight, and sex.
//It also reverse the formula to compute ideal body
//weight based on height, sex, and range of BMI.
function computeBMI(form)
{
	var Height = eval (form.e_height.value);
	//check weight unit and convert to kg if necessary
	var Weight = eval (form.e_weight.value);
	
	//compute BMI
	var BMI = Weight / Math.pow(Height/100, 2);
	
	document.getElementById('bmi').innerHTML = roundToXDigits(BMI,1);
	//determine body state based on BMI
	if (form.r_gender[0].checked)
	{ // male
		teza1=roundToXDigits(20*Math.pow(Height/100, 2),0);
    teza2=roundToXDigits(25*Math.pow(Height/100, 2),0);
    teza3=roundToXDigits(27*Math.pow(Height/100, 2),0);
    teza4=roundToXDigits(30*Math.pow(Height/100, 2),0);
    teza5=roundToXDigits(30.1*Math.pow(Height/100, 2),0);
    if (BMI<20)
			document.getElementById('bodystate').innerHTML = 'Podhranjenost';
		if ((BMI>=20)&&(BMI<=25))
			document.getElementById('bodystate').innerHTML='Normalna teža';
		if ((BMI>=25)&&(BMI<=27))
			document.getElementById('bodystate').innerHTML='Zmerna debelost';		
		if ((BMI>27)&&(BMI<=30))
			document.getElementById('bodystate').innerHTML='Debelost-prevelika teža';
		if (BMI>30)
			document.getElementById('bodystate').innerHTML='Zelo debeli';
	
		var FromWt = 20 * Math.pow(Height/100, 2);
		var ToWt = 25 * Math.pow(Height/100, 2);
	}
	else
	{ //female}
	  teza1=roundToXDigits(18.6*Math.pow(Height/100, 2),0);
    teza2=roundToXDigits(25*Math.pow(Height/100, 2),0);
    teza3=roundToXDigits(27*Math.pow(Height/100, 2),0);
    teza4=roundToXDigits(30*Math.pow(Height/100, 2),0);
    teza5=roundToXDigits(30.1*Math.pow(Height/100, 2),0);
		if (BMI<18.5)
			document.getElementById('bodystate').innerHTML='Podhranjenost';
		if ((BMI>=18.5)&&(BMI<=25))
			document.getElementById('bodystate').innerHTML='Normalna teža';
		if ((BMI>=25)&&(BMI<=27))
			document.getElementById('bodystate').innerHTML='Zmerna debelost';		
		if ((BMI>27)&&(BMI<=30))
			document.getElementById('bodystate').innerHTML='Debelost-prevelika teža';
		if (BMI>30)
			document.getElementById('bodystate').innerHTML='Zelo debeli';
	
		
		var FromWt = 18.5 * Math.pow(Height/100, 2);
		var ToWt = 25 * Math.pow(Height/100, 2) - 1;
	}
	document.getElementById('from').innerHTML = roundToXDigits(FromWt,0);
	document.getElementById('to').innerHTML = roundToXDigits(ToWt,0);
	form.h_to.value = roundToXDigits(ToWt,0);
}
