var SET = "<ul type='circle'>";
var ERROR_MESSAGE_DIV = "error_message";
var ENGLISH_UNIT = "english";
var UNITS_LABEL_DIV = "units_label";
var DISPLAY_STYLE_NONE = "none";
var DISPLAY_STYLE_BLOCK = "block";

function isNum(x) {
	filter = /(^\-?\d+\.?$)|(^\-?\d*\.\d+$)/;
	if (filter.test(x)) {
		return true;
	}
	return false;
}

function showID(idName) {
	if (document.getElementById) {
		var targetElement = document.getElementById(idName);
		targetElement.style.visibility = "visible";
	}
}

function hideID(idName) {
	if (document.getElementById) {
		var targetElement = document.getElementById(idName);
		targetElement.style.visibility = "hidden";
		// targetElement.style.display = DISPLAY_STYLE_NONE;
	}
}

function calcHeight(form) {
	var unitLabel = document.getElementById(UNITS_LABEL_DIV).innerHTML;
	document.getElementById(ERROR_MESSAGE_DIV).innerHTML = BLANK;
	var height = null;

	if (unitLabel == ENGLISH_UNIT) {
		if (!isBlank(form.height1.value)) {
			if (isNum(form.height1.value)) {
				var height = parseFloat(form.height1.value) / 100;
				return height;
			} else {
				errorMessage = SET
						+ "<li>Please enter a valid Height</li></ul>";
				form.height1.focus();
				document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
				return false;
			}
		} else {
			errorMessage = SET + "<li>Please enter a Height</li></ul>";
			form.height1.focus();
			document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
			return false;
		}
	} else {
		// Calculate Height in INCHES
		if (isBlank(form.height1.value) && isBlank(form.height2.value)) {
			errorMessage = SET + "<li>Please enter a Height</li></ul>";
			form.height1.focus();
			document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
			return false;
		}
		if (!isBlank(form.height1.value) && !isNum(form.height1.value)) {
			errorMessage = SET
					+ "<li>Please enter a valid Height (feet)</li></ul>";
			form.height1.focus();
			document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
			return false;
		}
		if (!isBlank(form.height2.value) && !isNum(form.height2.value)) {
			errorMessage = SET
					+ "<li>Please enter a valid Height (inches)</li></ul>";
			form.height2.focus();
			document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
			return false;
		}
		if (isBlank(form.height1.value)) {
			var feet = 0;
			form.height1.value = 0;
		} else {
			var feet = parseFloat(form.height1.value);
		}
		if (isBlank(form.height2.value)) {
			var inches = 0;
			form.height2.value = 0;
		} else {
			var inches = parseFloat(form.height2.value);
		}
		var height = feet * 12 + inches;
		return height;
	}
}

function calcWeight(form) {
	document.getElementById(ERROR_MESSAGE_DIV).innerHTML = BLANK;
	var unitLabel = document.getElementById(UNITS_LABEL_DIV).innerHTML;
	var BMI = null;
	var weight = null;

	// Validate and Calculate Height in inches or meters depending on UNITS
	var height = calcHeight(form);
	if (height == 0) {
		errorMessage = SET
				+ "<li>Error: Division by Zero (height is 0)</li></ul>";
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	}

	// Validate BMI
	if (isBlank(form.bmi.value)) {
		errorMessage = SET + "<li>Please enter a valid BMI (10-40)</li></ul>";
		form.bmi.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else if (!isNum(form.bmi.value)) {
		errorMessage = SET + "<li>Enter a valid BMI (10-40)</li></ul>";
		form.bmi.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else {
		var bmi = parseFloat(form.bmi.value);
	}

	if (unitLabel == ENGLISH_UNIT) {
		var weight = bmi * (height * height);
	} else {
		var weight = (bmi / 703) * (height * height);
	}
	form.weight.value = weight.toFixed(2);
	updateChart(bmi);
}

function calcBMI(form) {
	document.getElementById(ERROR_MESSAGE_DIV).innerHTML = BLANK;
	var unitLabel = document.getElementById(UNITS_LABEL_DIV).innerHTML;
	var BMI = null;
	var weight = null;

	// Validate and Calculate Height in inches or meters depending on UNITS
	var height = calcHeight(form);
	if (height == 0) {
		errorMessage = SET
				+ "<li>Error: Division by Zero (height is 0)</li></ul>";
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	}

	if (isBlank(form.weight.value)) {
		errorMessage = SET + "<li>Please enter a valid Weight</li></ul>";
		form.weight.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else if (!isNum(form.weight.value)) {
		errorMessage = SET + "<li>Enter a valid Weight</li></ul>";
		form.weight.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	} else {
		var weight = parseFloat(form.weight.value);
		form.weight.value = weight;
	}
	if (weight < 0) {
		errorMessage = SET + "<li>Weight must be Positive</li></ul>";
		form.weight.focus();
		document.getElementById(ERROR_MESSAGE_DIV).innerHTML = errorMessage;
		return false;
	}

	if (unitLabel == ENGLISH_UNIT) {
		var bmi = weight / (height * height);
	} else {
		var bmi = 703 * weight / (height * height);
	}
	form.bmi.value = bmi.toFixed(2);
	updateChart(bmi);
}

function updateChart(bmi) {
	// Must determine scaled value for bmi
	var scaledBMI = (bmi - 15) / 25;
	var bmiStr1 = scaledBMI.toFixed(2);
	var bmiStr2 = (scaledBMI + 0.01).toFixed(2);
	if (bmiStr1 < 0 || bmiStr2 < 0) {
		bmiStr1 = 0;
		bmiStr2 = 0.01;
	} else if (bmiStr1 > 1 || bmiStr2 > 1) {
		bmiStr1 = 0.99;
		bmiStr2 = 1;
	}
	var uriString = 'http://chart.apis.google.com/chart?chs=340x110&cht=bhs&chco=DCDCC8CC,BFEFC4CC,FFFFB9CC,FABED1CC&chd=t:3.5|6.5|5|10&chds=0,25&chf=bg,s,FFFFFF&chtt=BMI+Scale&chts=333333,16&chbh=25,20,20&chdl=Underweight|Normal|Overweight|Obese&chdlp=b&chxt=x&chxl=0:|15|18.5|25|30|40&chxp=0,0,14,40,60,100&chm=r,FF0000,0,'
			+ bmiStr1 + ',' + bmiStr2;
	document.getElementById("bmichart").src = uriString;
}
