var ns4 = (navigator.appName.indexOf("Netscape")>=0 && !document.getElementById)? true : false;

var ie4 = (document.all && !document.getElementById)? true : false;

var ie5 = (document.getElementById && document.all)? true : false;

var ns6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;

var w3c = (document.getElementById)? true : false;



function bmiCalc(form) {

	var weight = Number(form.wt.value);

	var height = Number(form.ht.value);



	if (!checkNum(weight,"WEIGHT")) {

		form.wt.select();

		form.wt.focus();

		return false

	}



	if (!checkNum(height,"HEIGHT")) {

		form.ht.select();

		form.ht.focus();

		return false

	}



        if (form.wu.selectedIndex == 0) {

                                        //  What units for weight?

                                        //  0 = lbs

                                        //  1 = kg

                weight = weight * 0.45359237;	//  Convert wt (lbs to kg)

        }



        if (form.sex[1].checked) {      //  Is the patient female?

                                        //  0 = male

                                        //  1 = female

		leanConvert = 148;

		idealConvert = 45.5;	//  conversion factors for women

        } else {

		leanConvert = 128;

		idealConvert = 50;	//  conversion factors for men

	}



	var huNum = form.hu.selectedIndex;

	var hu = form.hu.options[huNum].text;



	if (hu == "inches") {  

				//  if height units are "inches"

		heightInches = height;

		heightMeters = height * 2.54 / 100;

	}



	if (hu == "cm") {  

				//  if height units are "cm"

		heightInches = height / 2.54;

		heightMeters = height / 100;

	}



	if (hu == "feet") {  

				//  if height units are "cm"

		var inch = Number(form.inch.value);

		if (!checkNum(inch,"inches")) {

			form.inch.select();

			form.inch.focus();

			return false

		}

		heightInches = height*12 + inch;

		heightMeters = heightInches * 2.54 / 100;

	}



	if (hu == "meters") {  

				//  if height units are "meters"

		heightInches = height * 100 / 2.54;

		heightMeters = height;

	}



	var whatBSA = form.whatBSA.selectedIndex;

	calcBSA(whatBSA, 1);

	// var bsa = 0.20247 * Math.pow(heightMeters,0.725) * Math.pow(weight,0.425);

	var leanKg = 1.1 * weight - leanConvert * (Math.pow(weight,2) / Math.pow((100 * heightMeters),2));

	var leanLbs = leanKg * 2.2046226;

	var idealKg = idealConvert + 2.3 * (heightInches - 60);

	var idealLbs = idealKg * 2.2046226;

	var bmi = weight / Math.pow(heightMeters,2);



	// bsa = rounding(bsa,2);

	leanKg = Math.round(leanKg);

	leanLbs = Math.round(leanLbs);

	idealKg = Math.round(idealKg);

	idealLbs = Math.round(idealLbs);

	bmi = rounding(bmi,1);



	if (bmi < 18.5) {var interp = "Underweight"}

		else if (bmi < 25.0) {var interp = "Normal"}

			else if (bmi < 30.0) {var interp = "Overweight"}

				else {var interp = "Obese"}



	/* form.bsa.value = bsa;

	form.leanKg.value = leanKg;

	form.leanLbs.value = leanLbs;

	form.idealKg.value = idealKg;

	form.idealLbs.value = idealLbs;

	form.bmi.value = bmi;

	form.interp.value = interp;

	*/

	

	var lbw = eval(getLabel("lbw"));

	var ibw = eval(getLabel("ibw"));

	var bmiText = eval(getLabel("bmiText"));

	

	lbw.innerHTML = leanKg + " kg = " + leanLbs + " lbs";

	ibw.innerHTML = idealKg + " kg = " + idealLbs + " lbs";

	bmiText.innerHTML = bmi + " kg/m<sup>2</sup> = " + interp;

	

	lbw.style.display = '';

	ibw.style.display = '';

	bmiText.style.display = '';



	return true;

}



function calcBSA(whichOne, calcButt) {

	var bmi = document.bmi;

	// var oldBSA = bmi.bsa.value;

	// if (oldBSA != parseFloat(oldBSA) && !calcButt) {return false}

	var ht = bmi.ht.value;

	var wt = bmi.wt.value;

	var huNum = bmi.hu.selectedIndex;

	var hu = bmi.hu.options[huNum].text;

	

	if (!calcButt) {

		if (!checkError(ht, "ht", "patient's height")) return false;

		if (!checkError(wt, "wt", "patient's weight")) return false;

	}

	

	if (bmi.wu.selectedIndex == 0) {wt = wt * 0.45359237;}	//  Convert wt (lbs to kg)

	if (hu == "inches") {ht = ht * 2.54;}		//  Convert inches to cm

	if (hu == "meters") {ht = ht * 100;}		//  Convert meters to cm

	if (hu == "feet") {					//  Convert feet to cm

		var inch = Number(bmi.inch.value);

		ht = (ht*12 + inch) * 2.54;

	}

	

	if (whichOne == 0) {	//  Mosteller formula

		tempVar = ht * wt / 3600;

		bsaVal = Math.sqrt(tempVar);

	}

	if (whichOne == 1) {	//  DuBois & DuBois formula

		bsaVal = 0.20247 * Math.pow((ht/100),0.725) * Math.pow(wt,0.425);

	}

	if (whichOne == 2) {	//  Haycock formula

		bsaVal = 0.024265 * Math.pow(ht,0.3964) * Math.pow(wt,0.5378);

	}	

	if (whichOne == 3) {	//	Gehan & George formula

		bsaVal = 0.0235 * Math.pow(ht,0.42246) * Math.pow(wt,0.51456);

	}

	if (whichOne == 4) {	//  Boyd formula

		tempVar = 0.7285 - (0.0188 * Math.log(wt*1000) / Math.log(10));

		bsaVal = 0.0003207 * Math.pow(ht,0.3) * Math.pow((wt*1000),tempVar);

	}

	bsaVal = rounding(bsaVal,3);

	

	var bsaText = eval(getLabel("bsa"));

	var bsaSelect = eval(getLabel("bsaSelect"));

	bsaText.innerHTML = bsaVal + " m<sup>2</sup>";

	bsaText.style.display = '';

	bsaSelect.style.display = '';

	return true;

}



function isFeet() {

	var bmi = document.bmi;

	var huNum = bmi.hu.selectedIndex;

	var hu = bmi.hu.options[huNum].text;

	var inchRow = eval(getLabel("inchRow"));



	if (hu == "feet") {inchRow.style.display = "";}

		else {inchRow.style.display = "none"}

	

	return true;

}



function checkNum(val,text) {

	if ((val == null) || (isNaN(val)) || (val == "") || (val < 0) || (text=="inches" && val>=12)) {

		if (text=="inches" && val==0) {return true}

		alert("Please enter a value for " + text + ".");

		return false

	}

	return true;

}



function checkError(whatVar, varName, varText) {

	if ((whatVar == "") || (whatVar <= 0) || (isNaN(whatVar))) {

		alert("Please enter the " + varText + ".");

		eval("document.bmi." + varName + ".focus();");

		eval("document.bmi." + varName + ".select();");

		return false;

	}

	return true;

}



function getLabel(nameID) {

	var whatTag;

	

	if (ie4 || w3c) {whatTag = "document.all[\"" + nameID + "\"]";}

	if (ns6) {whatTag = "document.getElementById(\"" + nameID + "\")";}

	return whatTag;	

}



function rounding(number,decimal) {

	multiplier = Math.pow(10,decimal);

	number = Math.round(number * multiplier) / multiplier;

        return number

}


