function calcPK(form) {
	var weight = form.weight.value;
	var weightType = form.weightType.selectedIndex;
	var gender = form.gender.selectedIndex;
	var cr = form.cr.value;
	var age = form.age.value;
	var height = form.height.value;
	var heightType = form.heightType.selectedIndex;
	var tau = form.tau.value;		//  ok for tau to be ""
	var cpsstrough = form.cpsstrough.value;
	var time = form.time.value;
	var dose = form.dose.value;		// ok for dose to be ""
	var trough = form.trough.value;	// ok for trough to be ""
	var timetrough = form.timetrough.value;	// ok for timetrough to be ""
	
	if (!errorAlert("the patient's weight","weight",weight)) {return false};
	if ((cr != "") && (!errorAlert("the patient's serum creatinine","cr",cr))) {return false};
	if ((age != "") && (!errorAlert("the patient's age","age",age))) {return false};
	if ((height != "") && (!errorAlert("the patient's height","height",height))) {return false};
	if ((tau != "") && (!errorAlert("a tau value","tau",tau))) {return false};
	if (!errorAlert("a trough value","cpsstrough",cpsstrough)) {return false};
	if (!errorAlert("a time","time",time)) {return false};
	if ((dose != "") && (!errorAlert("a valid dose","dose",dose))) {return false};
	if ((trough != "") && (!errorAlert("a valid trough","trough",trough))) {return false};
//	if ((timetrough != "") && (!errorAlert("the trough time (hours after dose)","timetrough",timetrough))) {return false};
	if ((timetrough != "") && (isNaN(parseFloat(timetrough)))) {
		alert("Please enter the trough time.");
		eval("document.pk.timetrough.focus();");
		eval("document.pk.timetrough.select();");
		return false;
	}
	
	if ((trough != "") && (timetrough == "")) {
		errorAlert("the trough time (hours after dose)","timetrough",timetrough);
		return false;
	}
	if ((trough == "") && (timetrough != "0") && (timetrough != "")) {
		errorAlert("a valid trough","trough",trough);
		return false;
	}
	
	//  convert pounds to kilograms
	if (weightType == 1) {weight = weight * 0.4535924}
	
if ((height!="") && (cr!="") && (age!="")) {
	//  convert cm to inches
	if (heightType == 1) {height = height / 2.54}
	if (height < 60) {height = 0} else {height = height - 60}

	//  Ideal Body Weight
	if (gender == 0) {var ibw = 45 + 2.3 * height} else {var ibw = 50 + 2.3 * height}
	form.ibw.value = roundNum(ibw,1);
	if (weight < ibw) {var wt = weight} else {var wt = ibw}

/*	
	//  Adjusted Body Weight - use only if actual weight is more than 20% IBW
	if (weight > (ibw * 1.2)) {
		var wt = ibw + 0.4 * (weight - ibw);
		form.abw.value = roundNum(wt,1);
	} else {
		var wt = weight;
		form.abw.value = "";	
	}
*/

	//  If Cr < 1, use Cr = 1
	if (cr < 1) {cr = 1};

	//  Cockcroft-Gault GFR
	var crclmlmin = (((140 - age) * wt)) / (cr * 72);
	if (gender == 0) {crclmlmin = crclmlmin * 0.85};		// Correction factor for women
	var crcllhr = crclmlmin * 0.06;
	form.crclmlmin.value = roundNum(crclmlmin,1);
	form.crcllhr.value = roundNum(crcllhr,1);
	
	//  Vd - volume of distribution
	var vd = 0.7 * weight;
	form.vd.value = roundNum(vd,1);
	
	//  kel and t1/2
	var kel = crcllhr / vd;

	var thalf = 0.693 / kel;
	form.kel.value = roundNum(kel,3);
	form.thalf.value = roundNum(thalf,1);

	//  set tau to be t1/2, for now
	if (tau == "") {
		var tau = thalf;
		form.tau.value = roundNum(thalf,1);
	}
	
	cp(tau,dose,cpsstrough,vd,kel,time);
	var dose = document.pk.dose.value;
	document.pk.dose1.value = document.pk.dose2.value = document.pk.dose3.value = dose;
}/* else if (weight != "") {
	//  Vd - volume of distribution
	var vd = 0.7 * weight;
	form.vd.value = roundNum(vd,1);

	//  set tau to be t1/2, for now
	if (tau == "") {
		var tau = thalf;
		form.tau.value = roundNum(thalf,1);
	}

	cp(tau,dose,cpsstrough,vd,kel,time);
	var dose = document.pk.dose.value;
	document.pk.dose1.value = document.pk.dose2.value = document.pk.dose3.value = dose;
}*/
return true;
}


function calcKt (trough,timetrough) {
//	if (document.pk.ontime[0].checked) {var onTime = "on"} else {var onTime = "off"}
	if (document.pk.timetrough.value == "0") {var onTime = "on"} else {var onTime = "off"}
	var dose = document.pk.dose.value;
	var vd = document.pk.vd.value;
	var tau = document.pk.tau.value;
	trough = parseFloat(trough);
	timetrough = parseFloat(timetrough);
	
	var weight = document.pk.weight.value;
	var weightType = document.pk.weightType.selectedIndex;
	//  convert pounds to kilograms
	if (weightType == 1) {weight = weight * 0.4535924}
	
	if ((vd == "") && (weight != "")) {
		//  Vd - volume of distribution
		var vd = 0.7 * weight;
		document.pk.vd.value = roundNum(vd,1);
	}
/*
	if ((vd == "") && (weight != "")) {
		//  Vd - volume of distribution
		var vd = 0.7 * weight;
		form.vd.value = roundNum(vd,1);
	
		//  set tau to be t1/2, for now
		if (tau == "") {
			var tau = thalf;
			form.tau.value = roundNum(thalf,1);
		}

		cp(tau,dose,cpsstrough,vd,kel,time);
		var dose = document.pk.dose.value;
		document.pk.dose1.value = document.pk.dose2.value = document.pk.dose3.value = dose;
	}
*/

		//  Revise k and t1/2
		var k = Math.log(((dose/vd) + trough) / trough) / tau;
		var thalf = 0.693 / k;
		document.pk.k1.value = roundNum(k,3);
		document.pk.thalf1.value = roundNum(thalf,1);
//		document.pk.tau.value = roundNum(thalf,1);
	if (onTime == "on") {
		document.pk.estimTrough.value = "";
		document.pk.k2.value = "";
		document.pk.k3.value = "";
		document.pk.thalf2.value = "";
		document.pk.thalf3.value = "";
	}

	if (onTime == "off") {
		//  Estimate trough
		if (document.pk.kel.value != "") {k = document.pk.kel.value;}
			else {document.pk.kel.value = k}
		var estimTrough = trough * Math.exp(-k*timetrough);
		document.pk.estimTrough.value = roundNum(estimTrough,1);
		
		var cpssmax = document.pk.cpssmax.value;
		cpssmax = parseFloat (cpssmax);

//		k = Math.log (trough/estimTrough) / timetrough;		
		k = Math.log (cpssmax/estimTrough) / (tau - 2);		
		var thalf = 0.693 / k;
		
		if (timetrough == 0) {
			document.pk.k2.value = document.pk.k1.value;
			document.pk.thalf2.value = document.pk.thalf1.value;
		} else {
			document.pk.k2.value = roundNum(k,3);
			document.pk.thalf2.value = roundNum(thalf,1);
		}
//		document.pk.tau.value = roundNum(thalf,1);

		//  Bolus model
//		var time = tau - timetrough;
//		k = Math.log ( (dose / (vd * trough)) / (1 - Math.exp(-tau)) ) - time;

		k = newDose(trough,k);
		thalf = 0.693 / k;
		if (timetrough == 0) {
			document.pk.k3.value = document.pk.k1.value;
			document.pk.thalf3.value = document.pk.thalf1.value;
		} else {
			document.pk.k3.value = roundNum(k,3);
			document.pk.thalf3.value = roundNum(thalf,1);
		}
	
//		document.pk.k1.value = "";
//		document.pk.thalf1.value = "";
	}

	return true;
}
		
function newDose(cpss,k) {
	cpss = parseFloat(cpss);
	k = parseFloat(k);
	var dose = document.pk.dose.value;
	var vd = document.pk.vd.value;
	var tau = document.pk.tau.value;
	var cpssmin = document.pk.cpssmin.value
	var timetrough = document.pk.timetrough.value;
	var time = tau - timetrough;
	var trough, prevTroughD

//	var testText = "";
//	if (cpss < cpssmin) {kTrymax = k+0.3} else {kTrymax = k+0.05}
//	for (var kTry = (k-0.06); kTry <= kTrymax; kTry+=0.01) {

	kTry = k - 1;
	if (kTry <=0) {kTry = 0.001}
	
	while (kTry < (k+2)) {
		trough = ((dose/vd) * Math.exp(-kTry * time)) / (1 - Math.exp(-kTry * tau));	
		troughDiff = trough - cpss;
		
		// 	testText += "k = " + roundNum(kTry,3) + "; trough = " + roundNum(trough,3) + "; troughDiff = " + roundNum(troughDiff,3) + "\n";
		//	if (troughDiff < 0) {testText += "break 1\n"; break;}
		if (troughDiff < 0) {break;}
		kTry += 0.01;
	}
	
	for (var kTry2 = (kTry-0.01); kTry2 <= (kTry+0.01); kTry2+=0.001) {
		trough = ((dose/vd) * Math.exp(-kTry2 * time)) / (1 - Math.exp(-kTry2 * tau));	
		troughDiff = trough - cpss;
		
		// testText += "k2 = " + roundNum(kTry2,3) + "; trough = " + roundNum(trough,3) + "; troughDiff = " + roundNum(troughDiff,3) + "\n";
		
		if (troughDiff < 0) {break;}
		prevTroughD = troughDiff;
	}

	if (Math.abs(prevTroughD) > Math.abs(troughDiff)) {kTry = kTry2;}
		else {kTry = kTry2 - 0.001;}
		
//	document.pk.test.value = testText;
	return kTry;
}

function revisionCalc(dose,trough,k,tau) {
/*	//  set tau to be t1/2, for now
	if (tau == "") {
		form.tau.value = roundNum(thalf,1);
	}*/
	var vd = document.pk.vd.value;
	var time = document.pk.time.value;
	k = parseFloat(k);
	document.pk.tau.value = tau;

	//  Revise creatinine clearance
	crcllhr = k * vd;
	document.pk.crcllhr.value = roundNum(crcllhr,1);
	document.pk.crclmlmin.value = "";
	
	//  Recalculate tau, dose, cpssmax, cpssmin
	cp(tau,"",trough,vd,k,time);

	document.pk.kel.value = k;
	document.pk.thalf.value = tau;
	
	var dose = document.pk.dose.value;
	var cpssmax = document.pk.cpssmax.value;
	var cpssmin = document.pk.cpssmin.value;
	var crcllhr = document.pk.crcllhr.value;

	var reviseText = "Revised calculations, based on a new k = " + k + ", and t 1/2 = " + tau + " :\n";
	reviseText += "\tCreatinine Clearance = " + crcllhr + " L/hr\n";
	reviseText += "\tRevised dose = " + dose + " mg\n\n";
	reviseText += "\tCpss max = " + cpssmax + " mg/L\n";
	reviseText += "\tCpss min = " + cpssmin + " mg/L\n\n";
	reviseText += "Return to Step 2 to select new vancomycin 'dose' and 'tau'.";
	alert (reviseText);

	document.pk.dose.select();
	document.pk.dose.focus();
	
	return true;
}
/*
	//  Revisions
	if (trough != "") {
		//  Revise k and t1/2
		kel = (Math.log ( ((dose/vd) + trough) / trough )) / timetrough;
		thalf = 0.693 / kel;
		form.kel.value = roundNum(kel,3);
		form.thalf.value = roundNum(thalf,1);
	}
	
	return true;
}
*/
function errorAlert(varText,varName,varNum) {
	if ((varNum <= 0) || (isNaN(parseFloat(varNum)))) {
		alert("Please enter " + varText + ".");
		eval("document.pk." + varName + ".focus();");
		eval("document.pk." + varName + ".select();");
		return false;
	}
	return true;
}

function reviseAlert(form) {
	var dose = form.dose.value;
	var crcllhr = form.crcllhr.value;

	alert("REVISION CALCULATIONS:\n\n" +
		"New Creatinine Clearance = " + crcllhr + " L/hr\n" +
		"New Dose = " + dose + " mg\n\n" +
		"Adjust new dose, and click Step 2 button.");
	form.dose.select();
	form.dose.focus();

	return true;
}

function cp(tau,dose,cpsstrough,vd,kel,time) {
	tau = parseFloat(tau);
	cpsstrough = parseFloat(cpsstrough);
	kel = parseFloat(kel);
	time = parseFloat(time);
	dose = document.pk.dose.value;
//	dose = parseFloat(dose);
//	vd = parseFloat(vd);
	
/*
	var testText = "tau = " + tau + "\n";
	testText += "dose = " + dose + "\n";
	testText += "cpsstrough = " + cpsstrough + "\n";
	testText += "vd = " + vd + "\n";
	testText += "kel = " + kel + "\n";
	testText += "time = " + time + "\n";
	alert(testText);
*/


	if (vd == "" || isNaN(parseFloat(vd))) {
		var weight = document.pk.weight.value;
		var weightType = document.pk.weightType.selectedIndex;
	
		//  convert pounds to kilograms
		if (weightType == 1) {weight = weight * 0.4535924}
	
		//  Vd - volume of distribution
		var vd = 0.7 * weight;
		document.pk.vd.value = roundNum(vd,1);
	} else {vd = parseFloat(vd);}


	
	//  Dose calculation
	if (dose == "" || isNaN(parseFloat(dose))) {
		//  Check for Vd, kel, Cpss trough, tau
		if ((vd != "") && (!errorAlert("the volume of distribution","vd",vd))) {return false};
		if ((kel != "") && (!errorAlert("the k el","kel",kel))) {return false};
		if ((tau != "") && (!errorAlert("tau (the dosing interval)","tau",tau))) {return false};
		if ((cpsstrough != "") && (!errorAlert("the target trough (Cpss trough)","cpsstrough",cpsstrough))) {return false};

		var dose = (cpsstrough * vd * (1 - Math.exp(-kel * tau))) / Math.exp(-kel * tau);
		var newCrCl = vd * kel;
		var crclmlmin = newCrCl * 1000 / 60;
		
		document.pk.crclmlmin.value = roundNum(crclmlmin,0);
		document.pk.crcllhr.value = roundNum(newCrCl,1);
		document.pk.dose.value = roundNum(dose,0);
	} else {parseFloat(dose);}
	
	//  Cpss max
	var cpssmax = (dose / vd) * (Math.exp(-kel * time)) / (1 - Math.exp(-kel * tau));
	document.pk.cpssmax.value = roundNum(cpssmax,1);
	
	//  Cpss min
	var cpssmin = (dose / vd) * (Math.exp(-kel * tau)) / (1 - Math.exp(-kel * tau));
	document.pk.cpssmin.value = roundNum(cpssmin,1);
	
	return true;
}


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