function calcVol() {
	if (!checkForm()) return false;
	var ktv = document.ktv;
	var height = ktv.height.value;
	var htUnits = ktv.htUnits.selectedIndex;
	var weight = ktv.weight.value;
	var wtUnits = ktv.wtUnits.selectedIndex;
	var age = ktv.age.value;
	var sex = ktv.sex.selectedIndex;
	var diabetes = ktv.diabetes.selectedIndex;
	var volChoice = ktv.volChoice.selectedIndex;

// Convert height to centimeters
	if (htUnits == 1) {height *= 100}
	if (htUnits == 2) {height *= 2.54}
	if (htUnits == 3) {height *= 30.48}

// Convert weight to kg
	if (wtUnits == 1) {weight *= 0.45359}

// Calculate Watson formula
	if (sex == 0) {wVol = -2.097 + (0.1069*height) + (0.2466*weight)}
		else {wVol = 2.447 - (0.09156*age) + (0.1074*height) + (0.3362*weight)}

// Calculate Hume-Weyers formula
	if (sex == 0) {hwVol = (0.34454*height) + (0.183809*weight) - 35.270121}
		else {hwVol = (0.194786*height) + (0.296785*weight) - 14.012934}

// Calculate Chertow's Bioelectrical Impedance formula
	cVol = height*(0.0186104*weight + 0.12703384) + weight*(0.11262857*sex + 0.00104135*age - 0.00067247*weight - 0.04012056) - age*(0.03486146*sex + 0.07493713) - sex*1.01767992 + diabetes*0.57894981;

/*	newcVol = -0.07493713*age - 1.01767992*sex + 0.12703384*height - 0.04012056*weight + 0.57894981*diabetes - 0.00067247*weight*weight - 0.03486146*age*sex + 0.11262857*sex*weight + 0.00104135*age*weight + 0.0186104*height*weight;

	alert("height = " + height + "\nweight = " + weight + "\nage = " + age + "\nsex = " + sex + "\ndiabetes = " + diabetes + "\n\ncVol = " + cVol + "\nnewcVol = " + newcVol);
*/

// Calculate Mellits-Cheek formula
	if (sex == 0 && height < 110.8) {kidsVol = 0.076 + 0.507*weight + 0.013*height}
	if (sex == 0 && height >= 110.8) {kidsVol = -10.313 + 0.252*weight + 0.154*height}
	if (sex == 1 && height < 132.7) {kidsVol = -1.927 + 0.465*weight + 0.045*height}
	if (sex == 1 && height >= 132.7) {kidsVol = -21.993 + 0.406*weight + 0.209*height}

// Calculate 0.6 x weight
	var regVol = 0.6*weight;

	ktv.wVol.value = wVol;
	ktv.hwVol.value = hwVol;
	ktv.cVol.value = cVol;
	ktv.kidsVol.value = kidsVol;
	ktv.regVol.value = regVol;

	if (volChoice == 0) {var vol = wVol;}
	if (volChoice == 1) {var vol = hwVol;}
	if (volChoice == 2) {var vol = cVol;}
	if (volChoice == 3) {if (ktv.volChoice[3].text == "Mellits-Cheek formula (kids)") {var vol = kidsVol;} else {var vol = regVol;}}
	if (volChoice == 4) {var vol = regVol;}

	// alert("volChoice = " + volChoice + "\nvol = " + vol);

	vol = roundNum(vol,1);
	ktv.vol.value = vol;
	return vol;
}

function checkAge(age) {
	checkError(age, "age", "patient's age");
	var volChoice = document.ktv.volChoice;
	//  alert (volChoice.options[3].text);
	if (age <= 20) {
		if (volChoice.options[3].text != "Mellits-Cheek formula (kids)") {
			volChoice.options[3].text = "Mellits-Cheek formula (kids)";
			volChoice.options[4] = new Option("0.6 x weight");
		}
	} else {
		if (volChoice.options[3].text == "Mellits-Cheek formula (kids)") {volChoice.options[3] = null}
	}

	return;
}

function changeVol() {
	var ktv = document.ktv;
	var volChoice = ktv.volChoice.selectedIndex;

	if (volChoice == 0) {var vol = ktv.wVol.value}
	if (volChoice == 1) {var vol = ktv.hwVol.value}
	if (volChoice == 2) {var vol = ktv.cVol.value}
	if (volChoice == 3) {if (ktv.volChoice[3].text == "Mellits-Cheek formula (kids)") {var vol = ktv.kidsVol.value} else {var vol = ktv.regVol.value}}
	if (volChoice == 4) {var vol = ktv.regVol.value}

/*	alert("wVol = " + ktv.wVol.value + "\nhwVol = " + ktv.hwVol.value +
		"\ncVol = " + ktv.cVol.value + "\nkidsVol = " + ktv.kidsVol.value +
		"\nregVol = " + ktv.regVol.value);
*/
	ktv.vol.value = roundNum(vol,1);

	return true;
}

function checkForm() {
	var ktv = document.ktv;
	var height = ktv.height.value;
	var weight = ktv.weight.value;
	var age = ktv.age.value;
	if (!checkError(height, "height", "patient's height")) return false;
	if (!checkError(weight, "weight", "patient's weight")) return false;
	if (!checkError(age, "age", "patient's age")) return false;
	return true;
}

function checkError(whatVar, varName, varText) {
	if ((whatVar == "") || (whatVar <= 0) || (isNaN(whatVar))) {
		alert("Please enter the " + varText + ".");
		eval("document.ktv." + varName + ".focus();");
		eval("document.ktv." + varName + ".select();");
		return false;
	}
	return true;
}

function quickVals() {
	var ktv = document.ktv;
	ktv.k.value = 2.3;
	ktv.t.value = 240;
	ktv.age.value = 53;
	ktv.height.value = 175;
	ktv.weight.value = 70;
	ktv.sex.selectedIndex = 1;
	return;
}

function roundNum(thisNum,dec) {
	thisNum = thisNum * Math.pow(10,dec)
	thisNum = Math.round(thisNum)
	thisNum = thisNum / Math.pow(10,dec)
	return thisNum
}
