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 cIV(form) {

	if (!checkForm()) return false;

	var civ = document.calcIV;

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

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

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

	

	var dose = civ.dose.value;

	var doseTypeNum = civ.doseType.selectedIndex;

	var doseType = civ.doseType.options[doseTypeNum].text;

	var sIV = civ.sIV.value;

	var sIVtypeNum = civ.sIVtype.selectedIndex;

	var sIVtype = civ.sIVtype.options[sIVtypeNum].text;

	var timeTypeNum = civ.timeType.selectedIndex;

	var timeType = civ.timeType.options[timeTypeNum].text;

	var isWt = civ.isWt.checked;

	var weight = civ.weight.value;

	var weightType = civ.weightType.selectedIndex;

	var cDose = civ.cDose.value;

	var cDoseTypeNum = civ.cDoseType.selectedIndex;

	var cDoseType = civ.cDoseType.options[cDoseTypeNum].text;

	var cVol = civ.cVol.value;

	var cVolType = civ.cVolType.selectedIndex;

	var tInf;

	

	if (finalIV.style.display == "") {

		//  Convert Dose to mg

		if (doseType == "mcg") {dose = dose / 1000}

		if (doseType == "ng") {dose = dose / (1000 * 1000)}

	

		//  Convert Weight

		if (isWt) {

			if (weightType == 0) {weight = weight / 1000}

			if (weightType == 2) {weight *= 0.453597}

		} else {weight = 1}

	

		//  Convert Concentration Dose to mg

		if (cDoseType == "nanograms") {cDose = cDose / (1000 * 1000)}

		if (cDoseType == "mcg") {cDose = cDose / 1000}

		if (cDoseType == "grams") {cDose *= 1000}

	

		//  Convert Concentration Vol to cc

		if (cVolType == 1) {cVol *= 1000}

	

		//  Convert "infusion time" to min

		if (timeType == "min") {tInf = 60}

		if (timeType == "hr") {tInf = 1}

		if (timeType == "day") {tInf = 1/24}

	

		var ivRate = dose * weight * tInf * cVol / cDose;

		civ.ivVal.value = ivRate;



		civ.ccMin.value = ivRate / 60;

		civ.ccHr.value = ivRate;

		civ.ccDay.value = ivRate * 24;

		showRate();

	}

	

	if (finalDose.style.display == "") {

		//  Convert IV rate to cc/hr, indirectly

		if (sIVtype == "cc/min") {tInf = 1}

		if (sIVtype == "cc/hr") {tInf = 60}

		if (sIVtype == "cc/day") {tInf = 60*24}



		//  Convert weight to kg

		if (isWt) {

			if (weightType == 0) {weight = weight / 1000}

			if (weightType == 2) {weight *= 0.453597}

		} else {weight = 1}



		//  Convert Concentration Dose to mg

		if (cDoseType == "nanograms") {cDose = cDose / (1000 * 1000)}

		if (cDoseType == "mcg") {cDose = cDose / 1000}

		if (cDoseType == "grams") {cDose *= 1000}

	

		//  Convert Concentration Vol to cc

		if (cVolType == 1) {cVol *= 1000}

		

		//  Calculate final dose in mg/kg/min

		var fDose = sIV * (cDose / cVol) / (weight * tInf);

		civ.doseVal.value = fDose;

		showDose();

	}

	

	if (finalConc.style.display == "") {

		//  Convert dose to mg

		if (doseType == "mcg") {dose = dose / 1000}

		if (doseType == "ng") {dose = dose / (1000 * 1000)}



		//  Convert doseTime to hr, indirectly

		if (timeType == "min") {tInf = 60}

		if (timeType == "hr") {tInf = 1}

		if (timeType == "day") {tInf = 1/24}



		//  Convert weight to kg

		if (isWt) {

			if (weightType == 0) {weight = weight / 1000}

			if (weightType == 2) {weight *= 0.453597}

		} else {weight = 1}



		//  Convert IV rate to cc/hr

		if (sIVtype == "cc/min") {sIV *= 60}

		if (sIVtype == "cc/day") {sIV = sIV / 24}

		

		//  Calculate final concentration in mg/cc

		var fcDose = dose * weight * tInf / sIV;

		civ.concVal.value = fcDose;

		civ.fcVol.value = 100;

		civ.fcVolType.selectedIndex = 0;

		showConc();

	}

	

	return true;

}



function showRate() {

	var civ = document.calcIV;

	var rateType = civ.rateType.selectedIndex;

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



	if (rateType == 0) {

		var ccMin = civ.ccMin.value;

		// civ.ivRate.value = roundNum(ccMin,1);

		ivFinal.innerHTML = roundNum(ccMin,1);

	}

	if (rateType == 1) {

		var ccHr = civ.ccHr.value;

		// civ.ivRate.value = roundNum(ccHr,1);

		ivFinal.innerHTML = roundNum(ccHr,1);

	}

	if (rateType == 2) {

		var ccDay = civ.ccDay.value;

		// civ.ivRate.value = roundNum(ccDay,1);

		ivFinal.innerHTML = roundNum(ccDay,1);

	}

	ivFinal.style.display = "";

	return true;

}



function showDose() {

	var civ = document.calcIV;

	var doseVal = civ.doseVal.value;	//  in mg/kg/min	

	// var fDose = civ.fDose.value;

	var fDoseNum = civ.fDoseType.selectedIndex;

	var fDoseType = civ.fDoseType.options[fDoseNum].text;

	var fTimeNum = civ.fTimeType.selectedIndex;

	var fTimeType = civ.fTimeType.options[fTimeNum].text;

	var weight = civ.weight.value;

	var weightType = civ.weightType.selectedIndex;

	var isWt = civ.isWt.checked;

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



	if (fDoseType == "ng") {doseVal *= (1000*1000)}

	if (fDoseType == "mcg") {doseVal *= 1000}

	if (fDoseType == "grams") {doseVal = doseVal / 1000}



	if (fTimeType == "hr") {doseVal *= 60}

	if (fTimeType == "day") {doseVal *= (60*24)}



	// civ.fDose.value = roundNum(doseVal,1);

	doseFinal.innerHTML = roundNum(doseVal,1);

	doseFinal.style.display = "";

	return true;

}



function showConc() {

	var civ = document.calcIV;

	var concVal = civ.concVal.value;	//  in mg/cc

	// var fcDose = civ.fcDose.value;

	var fcDoseNum = civ.fcDoseType.selectedIndex;

	var fcDoseType = civ.fcDoseType.options[fcDoseNum].text;

	var fcVol = civ.fcVol.value;

	var fcVolNum = civ.fcVolType.selectedIndex;

	var fcVolType = civ.fcVolType.options[fcVolNum].text;

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



	if (fcDoseType == "nanograms") {concVal *= (1000*1000)}

	if (fcDoseType == "mcg") {concVal *= 1000}

	if (fcDoseType == "grams") {concVal = concVal / 1000}



	if (fcVolType == "liters") {concVal = concVal * 1000}

	concVal *= fcVol;



	if (concVal >= 1) {

		// civ.fcDose.value = roundNum(concVal,1)

		concFinal.innerHTML = roundNum(concVal,1)

	} else {

		// civ.fcDose.value = roundNum(concVal,2)

		concFinal.innerHTML = roundNum(concVal,2)

	}

	concFinal.style.display = "";

	return true;

}



function showWeight2() {

	var civ = document.calcIV;

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

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

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

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

	

	if (civ.isWt.checked) {

		weightRow1.style.display = "";

		weightRow2.style.display = "";

		doseWt.style.display = "";

		fDoseWt.style.display = "";

	} else {

		weightRow1.style.display = "none";

		weightRow2.style.display = "none";

		doseWt.style.display = "none";

		fDoseWt.style.display = "none";

	}



	return true;

}



function dispRow(row) {

	var civ = document.calcIV;

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

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

	var startConc = eval(getLabel("concRow"));

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

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

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

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

	

	if (row == 0) {

		//  Convert dose to IV rate

		startDose.style.display = "";

		finalIV.style.display = "";

		weightRow.style.backgroundColor = "#dddddd";

		startConc.style.display = "";

		

		startIV.style.display = "none";

		finalDose.style.display = "none";

		finalConc.style.display = "none";

	}

	if (row == 1) {

		//  Convert IV rate to dose

		startIV.style.display = "";

		startIV.style.backgroundColor = "#cccccc";

		startConc.style.display = "";

		weightRow.style.backgroundColor = "#dddddd";

		finalDose.style.display = "";



		

		startDose.style.display = "none";

		finalIV.style.display = "none";

		finalConc.style.display = "none";

	}

	if (row == 2) {

		//  Find concentration of solution

		startDose.style.display = "";

		startIV.style.display = "";

		startIV.style.backgroundColor = "#dddddd";

		weightRow.style.backgroundColor = "#cccccc";

		finalConc.style.display = "";

		

		startConc.style.display = "none";

		finalIV.style.display = "none";

		finalDose.style.display = "none";

	}

	

	return true;

}



function showWeight(doseType) {

	var civ = document.calcIV;

	if (doseType == 0 || doseType == 2 || doseType == 4) {

		if (ie4 || ie5) {

			document.all["weightRow"].style.display = "none";

			document.all["concRow"].style.backgroundColor = "#dddddd";

//			document.weightRow.style.display = "none";

		} else if (ns6) {

			document.getElementById("weightRow").style.display = "none";

			document.getElementById("concRow").style.backgroundColor = "#dddddd";

		}

	} else {

		if (ie4 || ie5) {

			document.all["weightRow"].style.display = "";

			document.all["concRow"].style.backgroundColor = "#cccccc";

//			document.weightRow.style.display = "";

		} else if (ns6) {

			document.getElementById("weightRow").style.display = "";

			document.getElementById("concRow").style.backgroundColor = "#cccccc";

		}

	}

	

	if (doseType == 4 || doseType == 5) {

		civ.cDoseType.selectedIndex = 3;

	} else {civ.cDoseType.selectedIndex = 1;}

	return true;

}



function checkForm() {

	var civ = document.calcIV;

	var dose = civ.dose.value;

	var sIV = civ.sIV.value;

	var isWt = civ.isWt.checked;

	var weight = civ.weight.value;

	var cDose = civ.cDose.value;

	var cVol = civ.cVol.value;

	

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

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

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



	if (finalIV.style.display == "") {

		if (!checkError(dose, "dose", "desired dose")) return false;

		if (isWt) {

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

		}

		if (!checkError(cDose, "cDose", "concentration dosage")) return false;

		if (!checkError(cVol, "cVol", "concentration volume")) return false;

	}

	if (finalDose.style.display == "") {

		if (!checkError(sIV, "sIV", "IV rate")) return false;

		if (isWt) {

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

		}

		if (!checkError(cDose, "cDose", "concentration dosage")) return false;

		if (!checkError(cVol, "cVol", "concentration volume")) return false;

	}

	if (finalConc.style.display == "") {

		if (!checkError(dose, "dose", "desired dose")) return false;

		if (!checkError(sIV, "sIV", "IV rate")) return false;

		if (isWt) {

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

		}

	}

	

	return true;

}



function checkError(whatVar, varName, varText) {

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

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

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

		eval("document.calcIV." + 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 roundNum(thisNum,dec) {

	thisNum = thisNum * Math.pow(10,dec);

	thisNum = Math.round(thisNum);

	thisNum = thisNum / Math.pow(10,dec);

	return thisNum;

}