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

function calcPK(form) {
	var pk = document.pk;
	var weight = form.weight.value;
	var weightType = form.weightType.selectedIndex;
	var aweight = pk.aweight.value;
	var aweightType = pk.aweightType.selectedIndex;
	var gender = form.gender.selectedIndex;
	var cr = form.cr.value;
	var age = form.age.value;
	var height = form.height.value;
	var hTNum = pk.heightType.selectedIndex;
	var heightType = pk.heightType.options[hTNum].text;
	// var heightType = form.heightType.selectedIndex;
	var vancClear = pk.vancClear.value;
	// 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 ""

	var tInf = pk.infuseTime.value;
	var iTime = pk.iTime.selectedIndex;
	
	var whatDrug = pk.whatDrug.selectedIndex;
	var whatCalc = pk.whatCalc.selectedIndex;
	var drugPeak = pk.drugPeak.value;
	var drugTrough = pk.drugTrough.value;
	var peakTime = pk.peakTime.value;
	var troughTime = pk.troughTime.value;
	var pTime = pk.pTime.selectedIndex;
	var tTime = pk.tTime.selectedIndex;
	
	var params = eval(getLabel("params"));
	var revParams = eval(getLabel("revParams"));
	var dosingParams = eval(getLabel("dosingParams"));
	
	if (!errorAlert("the patient's current weight","weight",weight)) {return false};
	if (!checkError(aweight,"aweight","patient's admit weight")) {return false};

	if (whatCalc == 0) {
		//  Initial Calculations
		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 ((vancClear != "") && (!errorAlert("a vancomycin clearance","vancClear",vancClear))) {return false};
	} else {
		//  Revisions
		if (!checkError(drugTrough, "drugTrough", "serum trough level")) {return false}
		if (!checkError(drugTrough, "troughTime", "time 'trough' was drawn")) {return false}
		if (whatDrug != 0) {
			if (!checkError(drugPeak, "drugPeak", "serum peak level")) {return false}
			if (!checkError(drugPeak, "peakTime", "time 'peak' was drawn")) {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;
	}
*/
	if (!checkError(tInf, "infuseTime", "infusion time")) {return false}
	
	//  Convert pounds to kilograms
	if (weightType == 1) {weight = weight * 0.4535924}
	if (aweightType == 1) {aweight *= 0.4535924}
	
	//  Convert infusion time to hours
	if (iTime == 0) {tInf = tInf / 60}

	//  Convert dosing interval to hours	
	if (doseIntType == 1) {doseInt *= 24}
	
if (whatCalc == 0) {
	//  convert height to inches
	if (heightType == "cm") {height = height / 2.54}
	if (heightType == "feet") {
		var inch = Number(pk.inch.value);
		if (!checkError(inch, "inch", "proper inches")) {return false}
		height = height*12 + inch;
	}
	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);
	var formIBW = eval(getLabel("ibw"));
	formIBW.innerHTML = roundNum(ibw,1) + " kg";
	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
	if (whatDrug == 0) {
		var crcllhr = vancClear * 0.06 * aweight * (crclmlmin / ibw)
	} else {
		var crcllhr = crclmlmin * 0.06;
	}
	// form.crclmlmin.value = roundNum(crclmlmin,1);
	// form.crcllhr.value = roundNum(crcllhr,1);
	// var formCrClhr = eval(getLabel("crcllhr"));
	// formCrClhr.innerHTML = roundNum(crcllhr,1) + " L/hr"
	var formCrClmin = eval(getLabel("crclmlmin"));
	var vClearRow = eval(getLabel("vClearRow"));
	if (whatDrug == 0) {
		//  Creatinine Clearance
		formCrClmin.innerHTML = roundNum(crclmlmin,1) + " ml/min";
	
		//  Vancomycin Clearance
		vClearRow.style.display = "";
		var vClearVal = eval(getLabel("vClearVal"));
		vClearVal.innerHTML = roundNum(crcllhr,1) + " L/hr";
	} else {	
		formCrClmin.innerHTML = roundNum(crclmlmin,1) + " ml/min = " + roundNum(crcllhr,1) + " L/hr";
		vClearRow.style.display = "none";
	}

	//  Vd - volume of distribution
	if (whatDrug == 0) {var vd = 0.7 * weight;}
		else {
			var vd = (0.25*ibw);
			//  Adjust for obese patients
			if (aweight > (1.2*ibw)) {vd = vd + 0.1*(aweight - ibw);}
			if (weight > aweight) {vd += (weight - aweight);}
		}
	// form.vd.value = roundNum(vd,1);
	var formvd = eval(getLabel("vd"));
	formvd.innerHTML = roundNum(vd,1) + " L";

	//  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);
	var formkel = eval(getLabel("kel"));
	var formthalf = eval(getLabel("thalf"));
	formkel.innerHTML = roundNum(kel,3) + " hr<sup>-1</sup>";
	formthalf.innerHTML = roundNum(thalf,1) + " hr";

	//  Infusion times
/*	var tInf = 0.5;
	if (whatDrug == 0) {
		if (drugDose > 500 && drugDose <= 1250) {tInf = 1}
		if (drugDose > 1250 && drugDose <= 1750) {tInf = 1.5}
		if (drugDose > 1750) {tInf = 2}
	}
	parseFloat(tInf);
*/

	var basedOn = eval(getLabel("basedOn"));
	// var predPeak = eval(getLabel("predPeak"));
	// var predTrough = eval(getLabel("predTrough"));
	
	//  Select dosing model: bolus vs. short infusion
	if ((6 * tInf) > thalf) {
		//  Short infusion model
		//  Cpss max : predicted Peak, sampled at 30 minutes after 30-minute infusion
		//  var cpssmax = (drugDose / (tInf * crcllhr)) * (1 - Math.exp(-kel * tInf)) * (Math.exp(-kel * tInf)) / (1 - Math.exp(-kel * doseInt));
		// var cpssmax = siLevel(drugDose,tInf,crcllhr,kel,doseInt,tInf);
		// predPeak.innerHTML = roundNum(cpssmax,1) + " mg/L";
		//  document.pk.cpssmax.value = roundNum(cpssmax,1);
			
		//  Cpss min : predicted Trough
		//  var cpssmin = (drugDose / (tInf * crcllhr)) * (1 - Math.exp(-kel * tInf)) * (Math.exp(-kel * (doseInt - tInf))) / (1 - Math.exp(-kel * doseInt));
		// var cpssmin = siLevel(drugDose,tInf,crcllhr,kel,doseInt,(doseInt - tInf));
		// predTrough.innerHTML = roundNum(cpssmin,1) + " mg/L";
		//  document.pk.cpssmin.value = roundNum(cpssmin,1);
		
		pk.bolusShort.value = "short";
		basedOn.innerHTML = "Based on short-infusion model :";
	} else {
		//  Bolus model
		//  Cpss max : predicted Peak, sampled at 1 hour after 1-hour infusion
		//  var cpssmax = (drugDose / vd) * (Math.exp(-kel * (parseFloat(tInf) + 1))) / (1 - Math.exp(-kel * doseInt));
		// var cpssmax = bLevel(drugDose,vd,kel,doseInt,(parseFloat(tInf) + 1));
		// predPeak.innerHTML = roundNum(cpssmax,1) + " mg/L";
		//  document.pk.cpssmax.value = roundNum(cpssmax,1);
	
		//  Cpss min : predicted Trough
		//  var cpssmin = (drugDose / vd) * (Math.exp(-kel * doseInt)) / (1 - Math.exp(-kel * doseInt));
		// var cpssmin = bLevel(drugDose,vd,kel,doseInt,doseInt);
		// predTrough.innerHTML = roundNum(cpssmin,1) + " mg/L";
		//  document.pk.cpssmin.value = roundNum(cpssmin,1);
		
		pk.bolusShort.value = "bolus";
		basedOn.innerHTML = "Based on bolus model :";
	}
	
	if (whatDrug == 0) {
		var drugDisp = eval(getLabel("revVanc"));
		var dontDisp = eval(getLabel("revAmino"));
		var drugInt = pk.rvInt;
		drugInt.value = roundNum(thalf,0);
	} else {
		var drugDisp = eval(getLabel("revAmino"));
		var dontDisp = eval(getLabel("revVanc"));
		var drugInt = pk.raInt;
		drugInt.value = roundNum(2*thalf, 0);
	}
	var revDose = eval(getLabel("revDose"));
	var revDose2 = eval(getLabel("revDose2"));
	var revDose3 = eval(getLabel("revDose3"));
	var revDose4 = eval(getLabel("revDose4"));
	
	params.style.display = "";
	revParams.style.display = "none";
	dosingParams.style.display = "";
	drugDisp.style.display = "";
	dontDisp.style.display = "none";
	revDose.style.display = "none";
	revDose2.style.display = "none";
	revDose3.style.display = "none";
	revDose4.style.display = "none";
	
	pk.revCl.value = crcllhr;
	pk.revK.value = kel;
	pk.revT.value = thalf;
	pk.revVd.value = vd;
	drugInt.focus();
	drugInt.select();
	
	/*  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 (whatCalc == 1) {
	var revK, revT, revVd, revCl;
	var revAmino = eval(getLabel("revAmino"));
	var revVanc = eval(getLabel("revVanc"));

	var drugDose = pk.drugDose.value;
	var drugDoseType = pk.drugDoseType.selectedIndex;
	var doseInt = pk.doseInt.value;
	var doseIntType = pk.doseIntType.selectedIndex;
	
	if (!checkError(drugDose,"drugDose","drug dose")) {return false};
	if (!checkError(doseInt,"doseInt","dosing interval")) {return false};

	//  Convert drug dose to milligrams
	if (drugDoseType == 1) {drugDose *= 1000}	//  convert to milligrams
	
	if (tTime == 1) {troughTime = doseInt - troughTime}
	if (pTime == 0)	{peakTime = doseInt - peakTime}
		
	revParams.style.display = "";
	dosingParams.style.display = "";
	params.style.display = "none";

	//  Revision k, t1/2
	if (whatDrug == 0) {
		//  Vancomycin
		var vd = 0.7 * weight;
		var topNum = ((drugDose / vd) + parseFloat(drugTrough)) / drugTrough;
		topNum = Math.log(topNum);
		revK = topNum / (doseInt - troughTime);
		revVd = vd;
		revT = 0.693 / revK;
		revCl = revK * revVd;
		
		revVanc.style.display = "";
		revAmino.style.display = "none";
		pk.rvInt.value = roundNum(revT,0);
		pk.rvInt.focus();
		pk.rvInt.select();
	} else {
		//  Aminoglycosides
		revK = Math.log(drugPeak / drugTrough) / (doseInt - tInf - troughTime - peakTime);
		revT = 0.693 / revK;
		if ((6*tInf > revT) && (whatDrug != 0)) {
			//  Short Infusion Model
			revCl = (drugDose / (tInf * drugPeak)) * (1 - Math.exp(-revK * tInf)) * (Math.exp(-revK * (parseFloat(tInf) + parseFloat(peakTime)))) / (1 - Math.exp(-revK * doseInt));
			revVd = revCl / revK;
			pk.bolusShort.value = "short";
		} else {
			//  Bolus Model
			revVd = (drugDose / drugPeak) * (Math.exp(-revK * (parseFloat(tInf) + parseFloat(peakTime)))) / (1 - Math.exp(-revK * doseInt));
			revCl = revK * revVd;
			pk.bolusShort.value = "bolus";
		}
		
		revVanc.style.display = "none";
		revAmino.style.display = "";
		pk.raInt.value = 2 * roundNum(revT,0);
		pk.raInt.focus();
		pk.raInt.select();
	}

	var rCl = eval(getLabel("rCl"));
	var rT = eval(getLabel("rT"));
	var rK = eval(getLabel("rK"));
	var rVd = eval(getLabel("rVd"));
	var revButton = eval(getLabel("revButton"));
	revButton.style.display = "";
	
	rCl.innerHTML = roundNum(revCl,1) + " L/hr";
	rT.innerHTML = roundNum(revT,1) + " hours";
	rK.innerHTML = roundNum(revK,3) + " hr<sup>-1</sup>";
	rVd.innerHTML = roundNum(revVd,1) + " L";
	
	pk.revCl.value = revCl;
	pk.revK.value = revK;
	pk.revT.value = revT;
	pk.revVd.value = revVd;
}

	var revDose = eval(getLabel("revDose"));
	var revDose2 = eval(getLabel("revDose2"));
	var revDose3 = eval(getLabel("revDose3"));
	var revDose4 = eval(getLabel("revDose4"));
	
	revDose.style.display = "none";
	revDose2.style.display = "none";
	
/* else if (weight != "") {
	//  Vd - volume of distribution
	var vd = 0.7 * weight;
	// form.vd.value = roundNum(vd,1);
	formvd.innerHTML = 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;
}*/
	var ptVal = pk.ptVal.value;
	if (ptVal == 1) {
		pk.rDose.value = drugDose;
		pk.nInt.value = doseInt;
		newPT(0);
		var revButton = eval(getLabel("revButton"));
		
		revAmino.style.display = "none";
		revVanc.style.display = "none";
		revButton.style.display = "none";
		revDose3.style.display = "";
		revDose4.style.display = "";
		revDose3.style.backgroundColor = "#dddddd";
		revDose4.style.backgroundColor = "#dddddd";
	} else {
		revDose3.style.display = "none";
		revDose4.style.display = "none";
	}

return true;
}


function newDose() {
	var pk = document.pk;
	var revCl = pk.revCl.value;
	var revK = pk.revK.value;
	var revT = pk.revT.value;
	var revVd = pk.revVd.value;
	var nDose, doseText;
	
	var revVanc = eval(getLabel("revVanc"));
	var revAmino = eval(getLabel("revAmino"));
	
	if (revVanc.style.display == "") {
		//  Vancomycin
		var rvInt = pk.rvInt.value;
		var rvIntType = pk.rvIntType.selectedIndex;
		var newTrough = pk.newTrough.value;
		
		if (!checkError(rvInt, "rvInt", "new vancomycin dosing interval")) {return false}
		if (!checkError(newTrough, "newTrough", "target serum trough level")) {return false}
		
		//  Convert dosing interval to hours
		if (rvIntType == 1) {
			doseText = rvInt + " days";
			rvInt *= 24;
		} else {doseText = rvInt + " hours";}
		pk.nInt.value = rvInt;
		
		//  nDose = (newTrough * revVd * (1 - Math.exp(-revK * rvInt))) / Math.exp(-revK * rvInt);
		nDose = bDose(newTrough,revVd,revK,rvInt,rvInt);
	} else {
		//  Aminoglycosides
		var bolusShort = pk.bolusShort.value;
		var raInt = pk.raInt.value;
		var raIntType = pk.raIntType.selectedIndex;
		var newPeak = pk.newPeak.value;
		var tInf = pk.infuseTime.value;
		var iTime = pk.iTime.selectedIndex;
		
		if (!checkError(raInt, "raInt", "new aminoglycoside dosing interval")) {return false}
		if (!checkError(newPeak, "newPeak", "target serum peak level")) {return false}
		if (!checkError(tInf, "infuseTime", "drug infusion time")) {return false}
		
		//  Convert infusion time to hours
		if (iTime == 0) {tInf = tInf / 60}

		//  Convert dosing interval to hours
		if (raIntType == 1) {
			doseText = raInt + " days";
			raInt *= 24;
		} else {doseText = raInt + " hours"}
		pk.nInt.value = raInt;
		
		if (bolusShort == "short") {
			//  Short infusion model
			//  nDose = (newPeak * revCl * tInf * (1 - Math.exp(-revK * raInt))) / ((1 - Math.exp(-revK * tInf)) * Math.exp(-revK * 0.5));
			nDose = siDose(newPeak,revCl,tInf,revK,raInt,0.5);
		} else {
			//  Bolus model
			//  nDose = (newPeak * revVd * (1 - Math.exp(-revK * raInt))) / Math.exp(-revK * (0.5 + parseFloat(tInf)));
			nDose = bDose(newPeak,revVd,revK,raInt,(0.5 + parseFloat(tInf)));
		}
	}
	
	pk.nDose.value = nDose;
	pk.doseText.value = doseText;
	
	var revDose = eval(getLabel("revDose"));
	var revDose2 = eval(getLabel("revDose2"));
	var revDose3 = eval(getLabel("revDose3"));
	var calcDose = eval(getLabel("calcDose"));
	calcDose.innerHTML = roundNum(nDose,0) + " mg";
	revDose.style.display = "";
	revDose2.style.display = "";
	revDose3.style.display = "none";
	pk.rDose.value = roundNum(nDose,0);
	pk.rDose.focus();
	pk.rDose.select();

	return;
}

function newPT(fromWhere) {
	var pk = document.pk;
	var revCl = pk.revCl.value;
	var revK = pk.revK.value;
	var revT = pk.revT.value;
	var revVd = pk.revVd.value;
	var nInt = pk.nInt.value;
	var bolusShort = pk.bolusShort.value;
	var tInf = pk.infuseTime.value;
	var iTime = pk.iTime.selectedIndex;
	var doseText = pk.doseText.value;
	var rDose = pk.rDose.value;
	var rDoseType = pk.rDoseType.selectedIndex;
	var nPeak, nTrough, peakText, basedonText, intType;
	var revPeak = eval(getLabel("revPeak"));
	// var startEnd = eval(getLabel("startEnd"));

	if (fromWhere == 1) {
		var pDraw = pk.pDraw.value;
		var pDrawType = pk.pDrawType.selectedIndex;
		if (pDrawType == 0) {pDraw = pDraw / 60}	//  Convert peak draw time to hours
		if (!checkError(pDraw, "pDraw", "time for serum peak level to be drawn")) {return false}
	}
	
	if (!checkError(tInf, "infuseTime", "drug infusion time")) {return false}
	if (!checkError(rDose, "rDose", "new rounded dose")) {return false}

	//  Convert infusion time to hours
	if (iTime == 0) {tInf = tInf / 60}
	
	//  Convert rounded dose to milligrams
	if (rDoseType == 1) {
		basedonText = rDose + " gram";
		rDose *= 1000;
	} else {basedonText = rDose + " mg"}
	
	if (revVanc.style.display == "") {
		//  Vancomycin
		//  nPeak = (rDose * Math.exp(-revK * (parseFloat(tInf) + 1))) / (revVd * (1 - Math.exp(-revK * nInt)));
		//  nTrough = (rDose * Math.exp(-revK * nInt)) / (revVd * (1 - Math.exp(-revK * nInt)));
		if (fromWhere == 0) {var pDraw = 1}
		nPeak = bLevel(rDose,revVd,revK,nInt,(parseFloat(tInf) + parseFloat(pDraw)));
		nTrough = bLevel(rDose,revVd,revK,nInt,nInt);
		peakText = "sampled at 1 hour after " + tInf + " min infusion";
		// startEnd.innerHTML = "start";
		
		intType = pk.rvIntType.selectedIndex;
		if (intType == 1) {
			basedonText = "Based on new dose of " + basedonText + " q" + (nInt/24) + " days, using bolus model :";
		} else {
			basedonText = "Based on new dose of " + basedonText + " q" + nInt + " hours, using bolus model :";
		}
	} else {
		//  Aminoglycoside
		intType = pk.raIntType.selectedIndex;
		if (intType == 1) {
			basedonText = "Based on new dose of " + basedonText + " q" + (nInt/24) + " days,";
		} else {
			basedonText = "Based on new dose of " + basedonText + " q" + nInt + " hours,";
		}
		
		if (bolusShort == "short") {
			//  Short infusion model
			//  nPeak = (rDose * (1 - Math.exp(-revK * tInf)) * Math.exp(-revK * 0.5)) / (tInf * revCl * (1 - Math.exp(-revK * nInt)));
			//  nTrough = (rDose * (1 - Math.exp(-revK * tInf)) * Math.exp(-revK * (nInt - tInf))) / (tInf * revCl * (1 - Math.exp(-revK * nInt)));
			if (fromWhere == 0) {var pDraw = 0.5}
			nPeak = siLevel(rDose,tInf,revCl,revK,nInt,pDraw);
			nTrough = siLevel(rDose,tInf,revCl,revK,nInt,(nInt - tInf));
			basedonText = basedonText + " using short-infusion model :";
			//  startEnd.innerHTML = "start";
		} else {
			//  Bolus model
			//  nPeak = (rDose * Math.exp(-revK * (parseFloat(tInf) + 0.5))) / (revVd * (1 - Math.exp(-revK * nInt)));
			//  nTrough = (rDose * Math.exp(-revK * nInt)) / (revVd * (1 - Math.exp(-revK * nInt)));
			if (fromWhere == 0) {var pDraw = 0.5}
			nPeak = bLevel(rDose,revVd,revK,nInt,(parseFloat(tInf) + parseFloat(pDraw)));
			nTrough = bLevel(rDose,revVd,revK,nInt,nInt);
			basedonText = basedonText + " using bolus model :";
			//  startEnd.innerHTML = "end";
		}
		peakText = "sampled at 30 min after " + tInf + " min infusion";
	}

	if (fromWhere == 0) {
		pDraw *= 60;	//  Convert peak draw time to minutes
		pk.pDraw.value = pDraw;
		pk.pDrawType.selectedIndex = 0;
	}
	
	var revDose3 = eval(getLabel("revDose3"));
	var revDose4 = eval(getLabel("revDose4"));
	var revBased = eval(getLabel("revBased"));
	var revPeak = eval(getLabel("revPeak"));
	var revTrough = eval(getLabel("revTrough"));
	
	//  "Based on new dose of 1 gram q24 hours, using bolus model :"
	revBased.innerHTML = basedonText;
	revPeak.innerHTML = roundNum(nPeak,1) + " mg/L";
	revTrough.innerHTML = roundNum(nTrough,1) + " mg/L";
	revDose3.style.display = "";
	revDose4.style.display = "";
	revDose3.style.backgroundColor = "#cccccc";
	revDose4.style.backgroundColor = "#cccccc";
	
	return;
}

function siLevel(drugDose,tInf,crcllhr,kel,doseInt,whatTime) {
	//  Short infusion model
	var si = (drugDose / (tInf * crcllhr)) * (1 - Math.exp(-kel * tInf)) * (Math.exp(-kel * whatTime)) / (1 - Math.exp(-kel * doseInt));
	/*alert ("drugDose = " + drugDose +
		"\ntInf = " + tInf +
		"\ncrcllhr = " + crcllhr +
		"\nkel = " + kel +
		"\ndoseInt = " + doseInt +
		"\nwhatTime = " + whatTime +
		"\nsi = " + si
	);*/
	return si;
}

function bLevel(drugDose,vd,kel,doseInt,whatTime) {
	//  Bolus dose model
	var bolus = (drugDose / vd) * (Math.exp(-kel * whatTime)) / (1 - Math.exp(-kel * doseInt));
	return bolus;
}

function siDose(level,cl,tInf,k,dInt,whatTime) {
	//  Short infusion model
	var shortDose = (level * cl * tInf * (1 - Math.exp(-k * dInt))) / ((1 - Math.exp(-k * tInf)) * Math.exp(-k * whatTime));
	/* alert ("level = " + level +
		"\ntInf = " + tInf +
		"\ncl = " + cl +
		"\nk = " + k +
		"\ndInt = " + dInt +
		"\nwhatTime = " + whatTime +
		"\nshortDose = " + shortDose
	);*/
	return shortDose;
}

function bDose(level,vd,k,dInt,whatTime) {
	//  Bolus model
	var bolusDose = (level * vd * (1 - Math.exp(-k * dInt))) / Math.exp(-k * whatTime);
	return bolusDose;
}




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 pk = document.pk;
	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 initRev(drug, iR) {
	var initDose = eval(getLabel("initDose"));
	var init1 = eval(getLabel("init1"));
	var init2 = eval(getLabel("init2"));
	var revLevels = eval(getLabel("revLevels"));
	var peak1 = eval(getLabel("peak1"));
	var peak2 = eval(getLabel("peak2"));
	var vClear1 = eval(getLabel("vClear1"));
	var vClear2 = eval(getLabel("vClear2"));
	var gender = eval(getLabel("gender"));
	var ptButton = eval(getLabel("ptButton"));

	if (iR == 0) {		//  Initial Dosing
		initDose.style.display = "none";	//  for revisions
		init1.style.display = "";
		init2.style.display = "";
		gender.style.display = "";
		revLevels.style.display = "none";
		peak1.style.display = "none";
		ptButton.style.display = "none";
		if (drug == 0) {		//  Vancomycin clearance
			vClear1.style.display = "";
			vClear2.style.display = "";
		} else {
			vClear1.style.display = "none";
			vClear2.style.display = "none";
		}
	}
	if (iR == 1) {		//  Revisions
		initDose.style.display = "";
		init1.style.display = "none";
		init2.style.display = "none";
		gender.style.display = "none";
		revLevels.style.display = "";
		ptButton.style.display = "";

		//  If vancomycin, don't need peaks
		if (drug == 0) {
			peak1.style.display = "none";
			peak2.style.display = "none";
		} else {
			peak1.style.display = "";
			peak2.style.display = "";
		}
	}
	//  infuseOver(drug);
	clearBoxes();

	return;
}

function infuseOver(whatDrug) {
	var pk = document.pk;
	var drugDose = pk.drugDose.value;
	var drugDoseType = pk.drugDoseType.selectedIndex;
	var infuseTime = pk.infuseTime.value;
	
	if (infuseTime) {return}
	if ((drugDose == "") || (drugDose <= 0) || (isNaN(drugDose))) {return}
	if (drugDoseType == 1) {drugDose *= 1000}
	
	//  Infusion times
	var tInf = 0.5;
	if (whatDrug == 0) {
		if (drugDose > 500 && drugDose <= 1250) {tInf = 1}
		if (drugDose > 1250 && drugDose <= 1750) {tInf = 1.5}
		if (drugDose > 1750) {tInf = 2}
	}
	pk.infuseTime.value = tInf;
	pk.iTime.selectedIndex = 1;
	
	return;
}

function testVals(whatVal) {
	if (whatVal == "v") {
		var pk = document.pk;
		pk.drugDose.value = 1000;
		pk.doseInt.value = 12;
		pk.infuseTime.value = 1;
		pk.weight.value = 70;
		pk.aweight.value = 65;
		pk.age.value = 25;
		pk.cr.value = 0.7;
		pk.height.value = 60;
	}
	if (whatVal == "g") {
		var pk = document.pk;
		pk.drugDose.value = 120;
		pk.doseInt.value = 8;
		pk.infuseTime.value = 0.5;
		pk.weight.value = 70;
		pk.aweight.value = 65;
		pk.age.value = 25;
		pk.cr.value = 0.7;
		pk.height.value = 60;
	}
	return true;
}

function clearBoxes() {
	var params = eval(getLabel("params"));
	var revParams = eval(getLabel("revParams"));
	var dosingParams = eval(getLabel("dosingParams"));
	var revDose = eval(getLabel("revDose"));
	var revDose2 = eval(getLabel("revDose2"));
	var revDose3 = eval(getLabel("revDose3"));
	var revDose4 = eval(getLabel("revDose4"));
	params.style.display = "none";
	revParams.style.display = "none";
	dosingParams.style.display = "none";
	revDose.style.display = "none";
	revDose2.style.display = "none";
	revDose3.style.display = "none";
	revDose4.style.display = "none";
	return;
}

function awFill(wt) {
	if (!checkError(wt,"weight","patient's current weight")) {return false}
	document.pk.aweight.value = wt;
	return;
}

function awtFill(wtType) {
	document.pk.aweightType.selectedIndex = wtType;
	return;
}

function negTime(peakTrough) {
	var pk = document.pk;
	var checkNum;
	
	if (peakTrough == "peak") {checkNum = pk.peakTime.value}
		else {checkNum = pk.troughTime.value}
	if (checkNum < 0) {checkNum = Math.abs(checkNum)}
	if (peakTrough == "peak") {pk.peakTime.value = checkNum}
		else {pk.troughTime.value = checkNum}

	return;
}

function isFeet() {
	var pk = document.pk;
	var hTNum = pk.heightType.selectedIndex;
	var hT = pk.heightType.options[hTNum].text;
	var inchRow = eval(getLabel("inchRow"));

	if (hT == "feet") {inchRow.style.display = "";}
		else {inchRow.style.display = "none"}
	return true;
}

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

function getLabel(nameID) {
	var whatTag;
	
	if (ie4) {whatTag = "document.all[\"" + nameID + "\"]";}
	if (ns6) {whatTag = "document.getElementById(\"" + nameID + "\")";}
	return whatTag;	
}

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