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 convertDir = civ.convertDir.value;
	var dose = civ.dose.value;
	if (convertDir == "mgVol") {var doseType = civ.doseType.selectedIndex;}
		else {var doseType = civ.doseVolType.selectedIndex;}
	var freqType = civ.freq.selectedIndex;
	var weight = civ.weight.value;
	var weightType = civ.weightType.selectedIndex;
	var cDose = civ.cDose.value;
	var cDoseType = civ.cDoseType.selectedIndex;
	var cVol = civ.cVol.value;
	var cVolType = civ.cVolType.selectedIndex;	//  Values for this can change!
	var freq;

	//  Convert Dose to mg
	if (convertDir == "mgVol" && (doseType == 2 || doseType == 3 || doseType == 6 || doseType == 7)) {dose = dose / 1000}
	
	//  Convert Dose to cc
	if (convertDir == "volMg" && (doseType == 1 || doseType == 5)) {dose *= 5;}
	if (convertDir == "volMg" && (doseType == 2 || doseType == 6)) {dose *= 15;}
	
	//  Define Frequency
	if ((convertDir == "mgVol" && (doseType == 0 || doseType == 2 || doseType == 4 || doseType == 6)) || 
		(convertDir == "volMg" && doseType <= 3)) {
		if (freqType == 0) {freq = 1}
		if (freqType == 1) {freq = 2}
		if (freqType == 2) {freq = 3}
		if (freqType == 3) {freq = 4}
		if (freqType == 4) {freq = 6}
		if (freqType == 5) {freq = 8}
		if (freqType == 6) {freq = 12}
		if (freqType == 7) {freq = 24}
	} else {freq = 1}
	
	//  Convert Weight
	if (convertDir == "mgVol" && doseType < 4) {weight = 1} else {
		if (weightType == 0) {weight = weight / 1000}
		if (weightType == 2) {weight *= 0.453597}
	}

	//  Convert Concentration Dose to mg
	if (cDoseType == 0) {cDose = cDose / 1000}
	if (cDoseType == 2) {cDose *= 1000}

	//  Convert Concentration Vol to cc
	//  if (cVolType == 1) {cVol *= 5}
	//  if (cVolType == 2) {cVol *= 15}
	if (civ.cVolType.options[cVolType].text == "teaspoon (tsp)") {cVol *= 5}
	if (civ.cVolType.options[cVolType].text == "tablespoon (tbsp)") {cVol *= 15}

	if (convertDir == "mgVol") {
		var ivRate = dose * weight * cVol / (cDose * freq);
		civ.cc.value = ivRate;
		civ.tsp.value = ivRate / 5;
		civ.tbsp.value = ivRate / 15;	
		showRate();
	} else {
		var ivRate = dose * cDose * freq / (cVol * weight);
		civ.mgDose.value = ivRate;		//  in mg/kg/day
		if (cDoseType == 0) {showMg("mcgKg", "day");}
		if (cDoseType == 1) {showMg("mgKg", "day");}
		if (cDoseType == 2) {showMg("gramsKg", "day");}
	}

	return true;
}

function showMg(mgWeight, doseDay) {			//  Show mg dose
	var civ = document.calcIV;
	var mgDose = civ.mgDose.value;				//  in mg/kg/day
	
	if (mgWeight == "mg" || mgWeight == "mcg" || mgWeight == "grams") {
		var weight = civ.weight.value;
		var weightType = civ.weightType.selectedIndex;
	
		//  Convert weight to kg
		if (weightType == 0) {weight = weight / 1000}
		if (weightType == 2) {weight *= 0.453597}

		mgDose *= weight;
	}
	
	if (mgWeight == "mcg" || mgWeight == "mcgKg") {mgDose *= 1000}
	if (mgWeight == "grams" || mgWeight == "gramsKg") {mgDose = mgDose / 1000}

	if (doseDay == "dose") {
		var freqType = civ.freq.selectedIndex;
	
		//  Determine frequency
		if (freqType == 0) {freq = 1}
		if (freqType == 1) {freq = 2}
		if (freqType == 2) {freq = 3}
		if (freqType == 3) {freq = 4}
		if (freqType == 4) {freq = 6}
		if (freqType == 5) {freq = 8}
		if (freqType == 6) {freq = 12}
		if (freqType == 7) {freq = 24}
	
		mgDose = mgDose / freq;
	}
	
	mgTypeText = "<select name=\"mgType\" onChange=\"showMg(this.options[this.selectedIndex].value, doseDayType.options[doseDayType.selectedIndex].value);\">\n";
	if (mgWeight == "mgKg") {mgTypeText += "<option selected value=\"mgKg\">mg/kg\n"}
		else {mgTypeText += "<option value=\"mgKg\">mg/kg\n"}
	if (mgWeight == "mcgKg") {mgTypeText += "<option selected value=\"mcgKg\">mcg/kg\n"}
		else {mgTypeText += "<option value=\"mcgKg\">mcg/kg\n"}
	if (mgWeight == "gramsKg") {mgTypeText += "<option selected value=\"gramsKg\">grams/kg\n"}
		else {mgTypeText += "<option value=\"gramsKg\">grams/kg\n"}
	if (mgWeight == "mg") {mgTypeText += "<option selected value=\"mg\">mg\n"}
		else {mgTypeText += "<option value=\"mg\">mg\n"}
	if (mgWeight == "mcg") {mgTypeText += "<option selected value=\"mcg\">mcg\n"}
		else {mgTypeText += "<option value=\"mcg\">mcg\n"}
	if (mgWeight == "grams") {mgTypeText += "<option selected value=\"grams\">grams\n"}
		else {mgTypeText += "<option value=\"grams\">grams\n"}
	mgTypeText += "</select>";
	
	doseDayText = "<select name=\"doseDayType\" onChange=\"showMg(mgType.options[mgType.selectedIndex].value, this.options[this.selectedIndex].value);\">\n";
	if (doseDay == "day") {doseDayText += "<option selected value=\"day\">day\n"}
		else {doseDayText += "<option value=\"day\">day\n"}
	if (doseDay == "dose") {doseDayText += "<option selected value=\"dose\">dose\n"}
		else {doseDayText += "<option value=\"dose\">dose\n"}
	doseDayText += "</select>";
	if (doseDay == "dose") {doseDayText += " " + civ.freq.options[freqType].text}
	
	mgDose = roundNum(mgDose,2);
	
	var ivRateText = mgDose + " " + mgTypeText + " / " + doseDayText;
	var ivRate2 = eval(getLabel("ivRate2"));
	ivRate2.innerHTML = ivRateText;
	ivRate2.style.display = '';
	
	return true;
}

function showRate() {
	var civ = document.calcIV;
	if (!civ.rateType) {
		var rateType = civ.cVolType.selectedIndex;
		rateType = civ.cVolType.options[rateType].text;
	} else {
		var rateType = civ.rateType.selectedIndex;
		rateType = civ.rateType.options[rateType].text;
	}
	var tabs, route;

	// if (rateType == 0) {
	if (rateType == "cc") {
		var cc = civ.cc.value;
		civ.ivRate.value = roundNum(cc,1);
		tabs = 0;
		rateType = 0;
	}
	// if (rateType == 1) {
	if (rateType == "tsp" || rateType == "teaspoon (tsp)") {
		var tsp = civ.tsp.value;
		civ.ivRate.value = roundNum(tsp,1);
		tabs = 0;
		rateType = 1;
	}
	// if (rateType == 2) {
	if (rateType == "tbsp" || rateType == "tablespoon (tbsp)") {
		var tbsp = civ.tbsp.value;
		civ.ivRate.value = roundNum(tbsp,1);
		tabs = 0;
		rateType = 2;
	}
	// if (rateType == 3) {
	if (rateType == "tabs" || rateType == "tablet") {
		var cc = civ.cc.value;
		civ.ivRate.value = roundNum(cc,1);
		tabs = 1;
		rateType = 3;
	}
	
	var ivRate2 = civ.ivRate.value;
	var whatFreq = civ.freq.selectedIndex;
	var doseFreq = civ.freq.options[whatFreq].text;

	if (tabs == 0) {
		if (rateType == 0) {routeCc = "<option selected>cc\n"}
			else {routeCc = "<option>cc\n"}
		if (rateType == 1) {routeTsp = "<option selected>tsp\n"}
			else {routeTsp = "<option>tsp\n"}
		if (rateType == 2) {routeTbsp = "<option selected>tbsp\n"}
			else {routeTbsp = "<option>tbsp\n"}

		route = "<select name=\"rateType\" onChange=\"showRate();\">\n" +
			routeCc + routeTsp + routeTbsp +
			"</select>";
	} else {
		route = "tabs"
	}

	var doseText = ivRate2 + " " + route + " per dose " + doseFreq;
	
/*	if (ie4 || ie5) {
		document.all["ivRate2"].innerHTML = ivRate2 + " ";
		document.all["doseFreq"].innerHTML = doseFreq;
	}
	if (w3c) {
		document.getElementById("ivRate2").innerHTML = ivRate2 + " ";
		document.getElementById("doseFreq").innerHTML = doseFreq;
	}
*/

	if (ie4 || ie5) {document.all["ivRate2"].innerHTML = doseText;}
	if (w3c) {document.getElementById("ivRate2").innerHTML = doseText;}
	if (!tabs) {civ.rateType.focus();}

	return true;
}

function clearDose() {
	var civ = document.calcIV;
	civ.cc.value = "";
	civ.tsp.value = "";
	civ.tbsp.value = "";

	if (civ.cVolType.selectedIndex == 3) {civ.cVol.value = 1}
	
	if (ns4) {
		civ.ivRate.value = "";
	}
	if (ie4 || ie5) {
		document.all["ivRate2"].innerHTML = "";
		// if (!pd2) document.all["doseFreq"].innerHTML = "";
	}
	if (w3c) {
		document.getElementById("ivRate2").innerHTML = "";
		// if (!pd2) document.getElementById("doseFreq").innerHTML = "";
	}

	return true;
}

function showWeight(doseType) {
//	var doseType = document.calcIV.doseType.selectedIndex;
//	alert ("doseType = " + doseType);
	var civ = document.calcIV;

	if (doseType < 4 && doseType != "volShow") {
		if (ie4 || ie5) {
			document.all["weightRow"].style.display = "none";
			document.all["concRow"].style.backgroundColor = "#cccccc";
//			document.weightRow.style.display = "none";
		} else if (ns6) {
			document.getElementById("weightRow").style.display = "none";
			document.getElementById("concRow").style.backgroundColor = "#cccccc";
		}
	} else {
		if (ie4 || ie5) {
			document.all["weightRow"].style.display = "";
			document.all["concRow"].style.backgroundColor = "#dddddd";
//			document.weightRow.style.display = "";
		} else if (ns6) {
			document.getElementById("weightRow").style.display = "";
			document.getElementById("concRow").style.backgroundColor = "#dddddd";
		}
	}

	return true;
}

function isTablet(volSelect) {
	var civ = document.calcIV;
	
	if (volSelect == 3 || volSelect == 7) {
		if (civ.cVolType.length != 1) {
			civ.cVolType.options[3] = null;
			civ.cVolType.options[2] = null;
			civ.cVolType.options[1] = null;
			civ.cVolType.options[0] = new Option("tablet");
		}
	} else if (volSelect != "no") {
		if (civ.cVolType.length != 3) {
			civ.cVolType.options[3] = null;
			civ.cVolType.options[0] = new Option("cc");
			civ.cVolType.options[1] = new Option("teaspoon (tsp)");
			civ.cVolType.options[2] = new Option("tablespoon (tbsp)");
		}
	} else {
		if (civ.cVolType.length != 4) {
			civ.cVolType.options[0] = new Option("cc");
			civ.cVolType.options[1] = new Option("teaspoon (tsp)");
			civ.cVolType.options[2] = new Option("tablespoon (tbsp)");
			civ.cVolType.options[3] = new Option("tablet");
		}
	}
	
	return;
}

function checkTab(x) {
	var civ = document.calcIV;
	
	if (x == 3) {
		civ.cVol.value = 1
		civ.rateType.options[2] = null;
		civ.rateType.options[1] = null;
		civ.rateType.options[0] = null;
		civ.rateType.options[0] = new Option("tabs");
		civ.rateType.selectedIndex = 0;
	} else {
		civ.rateType.options[0] = new Option("cc");
		civ.rateType.options[1] = new Option("tsp");
		civ.rateType.options[2] = new Option("tbsp");
		civ.rateType.selectedIndex = x;
	}
	
	return;
}

function whichConvert(whatVal) {
	var civ = document.calcIV;
	var topRow = eval(getLabel("convertTop"));
	var mgText = "<select name=\"doseType\" onChange=\"showWeight(this.selectedIndex); clearDose();\">\n" +
			"<option>mg/day\n" +
			"<option>mg/dose\n" +
			"<option>mcg/day\n" +
			"<option>mcg/dose\n" +
			"<option>mg/kg/day\n" +
			"<option>mg/kg/dose\n" +
			"<option>mcg/kg/day\n" +
			"<option>mcg/kg/dose\n" +
		"</select>";
	var volText = "<select name=\"doseVolType\" onChange=\"isTablet(this.selectedIndex); clearDose();\">\n" +
			"<option>cc/dose\n" +
			"<option>tsp/dose\n" +
			"<option>tbsp/dose\n" +
			"<option>tablets/dose\n" +
			"<option>cc/day\n" +
			"<option>tsp/day\n" +
			"<option>tbsp/day\n" +
			"<option>tablets/day\n" +
		"</select>";
	
	if (whatVal == "mgVol") {
		topRow.innerHTML = mgText;
		civ.convertDir.value = "mgVol";
		isTablet("no");
	}
	if (whatVal == "volMg") {
		topRow.innerHTML = volText;
		civ.convertDir.value = "volMg";
		showWeight("volShow");
	}
	
	return;
}

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

function checkForm() {
	var civ = document.calcIV;
	var dose = civ.dose.value;
	var convertDir = civ.convertDir.value;
	var weight = civ.weight.value;
	var cDose = civ.cDose.value;
	var cVol = civ.cVol.value;

	if (!checkError(dose, "dose", "desired dose")) return false;
	if (convertDir == "mgVol") {
		var doseType = civ.doseType.selectedIndex;
		if (doseType > 3) {
			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;
	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 roundNum(thisNum,dec) {
	thisNum = thisNum * Math.pow(10,dec);
	thisNum = Math.round(thisNum);
	thisNum = thisNum / Math.pow(10,dec);
	return thisNum;
}