// Validate that the text field is a number within the range
function validateNumber(txtField, min, max) {
	var val = txtField.value;
	if (isNaN(val) || val < min || val > max) {
		// Invalid input - alert user and reset value
		alert("'" + txtField.name + "' must be in the range: " + min + " to " + max + ".\nThe value you entered (" + val + ") is not valid.");
		if(val > max) {
			txtField.value = max;
		} else {
			txtField.value = min;
		}
		return false;
	}
	return true;
}

// Reset the input and output fields
function resetForm(form) {
	// Reset input fields to default values
	form.Enclosure.value = 1000;
	form.Protecting.selectedIndex = 0;
	form.Elevation.value = 500;
	form.Temperature.value = 72;

	// Reset Inergen fields
	form.ConcentrationInergen.value = "";
	form.CommentsInergen.value = "";
	form.AmountInergen.value = "";

	// Reset FM200 fields
	form.ConcentrationFM200.value = "";
	form.CommentsFM200.value = "";
	form.AmountFM200.value = "";

	// Reset FE13 fields
	form.ConcentrationFE13.value = "";
	form.CommentsFE13.value = "";
	form.AmountFE13.value = "";

	// Reset CO2 fields
	form.ConcentrationCO2.value = "";
	form.CommentsCO2.value = "";
	form.AmountCO2.value = "";
}

// Calculate the output fields based on the input fields
function calculateResults(form) {
	// Set concentration and comment defaults for Inergen
	form.ConcentrationInergen.value = 37.5;
	form.CommentsInergen.value = ""

	// Set concentration and comment defaults for FM200
	form.ConcentrationFM200.value = 7;
	form.CommentsFM200.value = ""

	// Set concentration and comment defaults for FE13
	form.ConcentrationFE13.value = 18;
	form.CommentsFE13.value = ""

	// Set concentration and comment defaults for CO2
	form.ConcentrationCO2.value = 34;
	form.CommentsCO2.value = ""

	// Adjust values based on what we're protecting
	switch (form.Protecting.options[form.Protecting.selectedIndex].value) {
		case "AC":
			form.ConcentrationFM200.value = 8.1;
			break;
		case "IS":
			form.ConcentrationFM200.value = 8.7;
			break;
		case "ER":
		case "SG":
		case "TE":
			form.ConcentrationCO2.value = 50;
			break;
		case "TC":
		case "MR":
		case "UP":
			form.CommentsFE13.value = "Not Recommended.";
			break;
		case "DP":
		case "CS":
			form.CommentsFE13.value = "Not Recommended.";
			form.CommentsCO2.value = "Not Recommended.\n";
			break;
		case "PC":
			form.CommentsCO2.value = "Not Recommended.\n";
			break;
	}


	// Figure amounts
	var altCorrect = altitudeCorrection(form.Elevation.value);
	form.AmountFM200.value = Math.round(altCorrect * ((form.Enclosure.value/(1.8854 + (form.Temperature.value * 0.004574))) * (form.ConcentrationFM200.value/(100 - form.ConcentrationFM200.value))));
	form.AmountFE13.value = Math.round(1.11 * altCorrect * ((form.Enclosure.value/(4.731 + (form.Temperature.value * 0.0107))) * (form.ConcentrationFE13.value/(100 - form.ConcentrationFE13.value))));
	form.AmountCO2.value = Math.round(form.Enclosure.value/(30.75 - (.375 * form.ConcentrationCO2.value)));
	if (form.Elevation.value <= 3000) altCorrect = 1;
	form.AmountInergen.value = Math.round(altCorrect * (((2.303 * form.Enclosure.value)/(9.7261 + (form.Temperature.value * 0.0211))) * (Math.log(100/(100 - form.ConcentrationInergen.value))/Math.LN10) * 11.2093));


	// Adjust values based on temperature
	if (form.Temperature.value < 10) {
		form.CommentsFM200.value += " Temperature out of Range.";
		form.AmountFM200.value = ""
		form.ConcentrationFM200.value = ""
	}
	if (form.Temperature.value > 130) {
		form.CommentsFM200.value += " Temperature out of Range.";
		form.AmountFM200.value = ""
		form.ConcentrationFM200.value = ""
		form.CommentsFE13.value += " Temperature out of Range.";
		form.AmountFE13.value = ""
		form.ConcentrationFE13.value = ""
	}
	if (form.Temperature.value != 72 || form.Elevation.value != 500) {
		form.CommentsCO2.value += "Full Calculations must be Performed. Values listed are for 500 Ft. and 72F."
	}
}

// Return a correction factor based on altitude and type of system
function altitudeCorrection(elevation) {
	// Not quite linear
	if (elevation >= 6000) {
		correctionFactor = 0.66 + (0.03 * (10000 - elevation) / 1000);
	} else if (elevation >= 0) {
		correctionFactor = 0.78 + (0.03667 * (6000 - elevation) / 1000);
	} else {
		correctionFactor = 1 - (0.035 * elevation / 1000);
	}

	return correctionFactor;
}