var DecPlaces = 2;

function PerformCalculation () {
	// Read the current input values - input box objects:
	var CreatinineBox = document.getElementById ('CreatinineInput');
	var AgeBox = document.getElementById ('AgeInput');
	// Read the current input values - input box values:
	var Creatinine = ReadInputBoxFloat (CreatinineBox);
	var Age = ReadInputBoxFloat (AgeBox);
	// Read the current input values - select boxes objects:
	var GenderBox = document.getElementById ('GenderInput');
	var RaceBox = document.getElementById ('RaceInput');
	var UnitBox = document.getElementById ('UnitInput');
	// Read the current input values - select boxes values:
	var Gender = GenderBox.options[GenderBox.selectedIndex].value;
	var Race = RaceBox.options[RaceBox.selectedIndex].value;
	var Unit = UnitBox.options[UnitBox.selectedIndex].value;
	// Perform validity / range checks:
	var ResultMsg = [];
	if (isNaN (Creatinine) || Creatinine == 0) {
		ResultMsg.push ('<li class="ResultsListError">Please enter the Serum Creatinine value.</li>');
	} else {
		if (Unit == '1') {
			if (Creatinine < 0.4 || Creatinine > 11.3) {
				ResultMsg.push (
					'<li class="ResultsListError">Serum Creatinine must be between 0.4 and 11.3 mg/dL.</li>'
				);
			}
		} else {
			if (Creatinine < 35.2 || Creatinine > 999) {
				ResultMsg.push (
					'<li class="ResultsListError">Serum Creatinine must be between 35.2 and 999 &micro;mol/L.</li>'
				);
			}
		}
	}
	if (isNaN (Age) || Age == 0) {
		ResultMsg.push ('<li class="ResultsListError">Please enter the patient\'s age.');
	} else if (Age < 18 || Age > 110) {
		ResultMsg.push ('<li class="ResultsListError">Age must be between 18 and 110 years.');
	}
	// Perform the calculations (if no errors above):
	if (ResultMsg.length == 0) {
		Creatinine *= parseFloat (Unit);
		var Value_MDRD = Get_eGFR_MDRD (Creatinine, Age, Gender, Race, false);
		var Value_MDRD_IDMS = Get_eGFR_MDRD (Creatinine, Age, Gender, Race, true);
		var Value_CKDEPI = Get_eGFR_CKDEPI (Creatinine, Age, Gender, Race);
		ResultMsg.push (
			'</ul><abbr title="Estimated Glomerular Filtration Rate">eGFR</abbr> ml/min per 1.73 M<sup>2</sup> &nbsp; &mdash;<ul class="ResultsList">',
			'<li class="ResultsListOK">' + Value_MDRD.toFixed (DecPlaces) + ' -- <abbr title="Modification of Diet in Renal Disease">MDRD</abbr>, non-<abbr title="Isotope Dilution Mass Spectrometry">IDMS</abbr> serum Cr</li>',
			'<li class="ResultsListOK"><b>' + Value_MDRD_IDMS.toFixed (DecPlaces) + ' -- MDRD, IDMS-normalized serum Cr</b></li>',
			'<li class="ResultsListOK">' + Value_CKDEPI.toFixed (DecPlaces) + ' -- <abbr title="Chronic Kidney Disease Epidemiology Collaboration">CKD-EPI</abbr>, (IDMS-normalized serum Cr only)</li>'
		);
		// if (Value_MDRD >= 60 || Value_MDRD_IDMS >= 60 || Value_CKDEPI >= 60) { ResultMsg.push ('<div class="FinePrint"><br />(NOTE: the <abbr title="National Kidney Disease Education Program">NKDEP</abbr> recommends reporting values 60 and above as <b>&ge;60</b>, not an exact number.)</div>'); }
	}
	var ResultsCell = document.getElementById ('ResultsCell');
	ResultsCell.innerHTML = '<ul class="ResultsList">' + ResultMsg.join ('') + '</ul>';
}

function Get_eGFR_MDRD (Creatinine, Age, Gender, Race, IsIDMS) {
	if (typeof (IsIDMS) != 'boolean') { IsIDMS = false; }
	var Value = (IsIDMS) ? 175 : 186;
	Value *= Math.pow (Creatinine, -1.154) * Math.pow (Age, -0.203);
	if (Gender == 'F') { Value *= 0.742; }
	if (Race == 'Black') { Value *= 1.21; }
	return Value;
}

function Get_eGFR_CKDEPI (Creatinine, Age, Gender, Race) {
	if (Race == 'Black') {
		if (Gender == 'F') {
			if (Creatinine <= 0.7) {
				return 166 * Math.pow (Creatinine / 0.7, -0.329) * Math.pow (0.993, Age);
			} else {
				return 166 * Math.pow (Creatinine / 0.7, -1.209) * Math.pow (0.993, Age);
			}
		} else {
			if (Creatinine <= 0.9) {
				return 163 * Math.pow (Creatinine / 0.9, -0.411) * Math.pow (0.993, Age);
			} else {
				return 163 * Math.pow (Creatinine / 0.9, -1.209) * Math.pow (0.993, Age);
			}
		}
	} else {
		if (Gender == 'F') {
			if (Creatinine <= 0.7) {
				return 144 * Math.pow (Creatinine / 0.7, -0.329) * Math.pow (0.993, Age);
			} else {
				return 144 * Math.pow (Creatinine / 0.7, -1.209) * Math.pow (0.993, Age);
			}
		} else {
			if (Creatinine <= 0.9) {
				return 141 * Math.pow (Creatinine / 0.9, -0.411) * Math.pow (0.993, Age);
			} else {
				return 141 * Math.pow (Creatinine / 0.9, -1.209) * Math.pow (0.993, Age);
			}
		}
	}
}

function ReadInputBoxFloat (InputObj) {
	// if (InputObj.disabled) { return undefined; }
	var RawValue = InputObj.value.replace (',', '.');
	var Value = parseFloat (RawValue);
	// Copy parsed number back into the input box (while making sure this isn't keeping the user from entering decimals):
	if (!RawValue.match (/^\d*[.]*[0-9]*/)) { InputObj.value = isNaN (Value) ? '' : Value; }
	return Value;
}

function InputCellClicked (CellObj) {
	var CellID = CellObj.id;
	var InputID = CellID.replace (/Cell$/, 'Input');
	var InputObj = document.getElementById (InputID);
	if (InputObj.disabled) {
		alert ('The currently selected formula doesn\'t use this input field.');
	} else {
		InputObj.focus();
	}
}
