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 dextroseCalc() {
	//  Vars: base, baseCc, mix, mixCc, targetConc, targetCc
	var dex = document.dex;
	var dexElem = dex.elements.length;
	// var dexElem = parseInt(dex.elements.length);
	var tempName, tempVal, tempSelect, tempTalk;
	
	for (var i = 0; i < dexElem; i++) {
		tempName = dex.elements[i].name;
		if (dex.elements[i].type == "text") {
			tempVal = dex.elements[i].value;
		} else if (dex.elements[i].type == "select-one") {
			tempSelect = dex.elements[i].selectedIndex;
			tempVal = dex.elements[i].options[tempSelect].value;
		} else if (dex.elements[i].type == "button" || dex.elements[i].type == "reset") {
			tempVal = "";
		}
		eval("var " + tempName + " = tempVal");
		//  eval("var " + tempName + "L = getLabel(tempName)");
		//  tempTalk += "i = " + i + ", and tempName = " + tempName + ", and tempType = "+ dex.elements[i].type +", and tempVal = " + tempVal + "\n";
	}
	var baseError = "volume of base solution";
	var mixError = "volume of stock solution";
	var tConcError = "concentration of final solution";
	var tCcError = "volume of final solution";
	//  if (baseCc != "" && !checkError(baseCc, "baseCc", baseError)) return false;
	//  if (mixCc != "" && !checkError(mixCc, "mixCc", mixError)) return false;
	//  if (!checkError(targetConc, "targetConc", tConcError)) return false;
	//  if (!checkError(targetCc, "targetCc", tCcError)) return false;
	if (mixCc=="" && baseCc=="" && checkError(targetConc, "targetConc", tConcError) && checkError(targetCc, "targetCc", tCcError)) {
		mixCc = targetCc * (targetConc - base) / (mix - base);
		baseCc = targetCc - mixCc;
		dex.mixCc.value = roundNum(mixCc,1);
		dex.baseCc.value = roundNum(baseCc,1);
	} else
	if ((targetCc=="" || targetCc == (parseFloat(baseCc) + parseFloat(mixCc))) && targetConc=="" && checkError(baseCc, "baseCc", baseError) && checkError(mixCc, "mixCc", mixError)) {
		baseCc = parseFloat(baseCc);
		mixCc = parseFloat(mixCc);
		targetConc = (mixCc * mix + baseCc * base) / (mixCc + baseCc);
		targetCc = (mixCc * mix + baseCc * base) / targetConc;
		dex.targetConc.value = roundNum(targetConc,1);
		dex.targetCc.value = roundNum(targetCc,1);
	} else
	if (baseCc=="" && targetCc=="" && checkError(targetConc, "targetConc", tConcError) && checkError(mixCc, "mixCc", mixError)) {
		targetCc = mixCc * (mix - base) / (targetConc - base);
		baseCc = targetCc - mixCc;
		dex.targetCc.value = roundNum(targetConc,1);
		dex.baseCc.value = roundNum(baseCc,1);
	} else
	if (mixCc=="" && targetCc=="" && checkError(targetConc, "targetConc", tConcError) && checkError(baseCc, "baseCc", baseError)) {
		targetCc = baseCc * (base - mix) / (targetConc - mix);
		mixCc = targetCc - baseCc;
		dex.targetCc.value = roundNum(targetConc,1);
		dex.mixCc.value = roundNum(mixCc,1);
	} else {
		alert("Oops! Error!\nMake sure you enter ONLY 2 boxes,\nand leave 2 boxes blank.");
	}
	//  alert("It works!\nbase = " + base);
	return;
}
function getLabel(nameID) {
	var whatTag;
	
	if (ie4) {whatTag = "document.all[\"" + nameID + "\"]";}
	if (ns6) {whatTag = "document.getElementById(\"" + nameID + "\")";}
	return whatTag;	
}
function checkForm() {
	var dex = document.dex;
	var height = dex.height.value;
	if (!checkError(height, "height", "patient's height")) return false;
	return true;
}
function checkError(whatVar, varName, varText) {
	if ((whatVar == "") || (whatVar <= 0) || (isNaN(whatVar))) {
		alert("Please enter the " + varText + ".");
		eval("document.dex." + varName + ".focus();");
		eval("document.dex." + 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;
}