/*
Copyright (c) 2009 Vertex42, LLC. All Rights Reserved.
isBlank function from: http://www.mattkruse.com/javascript/validations/source.html
Except for the isBlank function, which you can get from the above site, you may not copy this code without written permission from Vertex42.com
*/

function show_help() {
	var helpstr = "Enter your height. To solve for BMI, enter your weight and press the BMI button. To solve for a goal weight, enter the desired BMI, and press the Weight button. The BMI scale provides the standard categories for underweight, normal weight, overweight, and obese.";
	alert(helpstr);
}

function isBlank(val){
	if(val==null) { return true; }
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
	}
	return true;
}

function isNum(x){
	/* Tests for Numeric, and does not fail on blank */
	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.display = 'block';
    }
}

function hideID(idName)
{
    if (document.getElementById) {
		var targetElement = document.getElementById(idName);
		targetElement.style.display = 'none';
    }
}

function toggleUnits() {
	var unitLabel = document.getElementById("units_label").innerHTML;
	var form = document.forms['bmi_form']	
	
	if (unitLabel == "ENGLISH") {
		// Toggle TO English units FROM Metric
		document.getElementById("weight_units").innerHTML = "lbs";
		document.getElementById("height_units").innerHTML = "feet";
		if ( !isBlank(form.weight.value) ) {
			if ( isNum(form.weight.value) ) {
				var num = parseFloat(form.weight.value) / 0.45359237; // Convert kg to lbs 
				form.weight.value = num.toFixed(2);
			}
		}
		if ( !isBlank(form.height1.value) ) {
			if ( isNum(form.height1.value) ) {
				if ( form.height1.value != 0 ) {
					var num_tot_in = parseFloat(form.height1.value)/2.54; // Convert cm to in
					var num_in = num_tot_in % 12;
					var num_ft = (num_tot_in - num_in) / 12;
					form.height2.value = num_in.toFixed(2);
					form.height1.value = num_ft.toFixed(1);
				}
			}
		}
		document.getElementById("units_label").innerHTML = "METRIC";
		showID('inch_field');
	} else {
		// Toggle TO Metric units FROM English
		document.getElementById("weight_units").innerHTML = "kg";
		document.getElementById("height_units").innerHTML = "cm";
		if ( !isBlank(form.weight.value) ) {
			if ( isNum(form.weight.value) ) {
				var num = parseFloat(form.weight.value) * 0.45359237; // Convert kg to lbs 
				form.weight.value = num.toFixed(2);
			}
		}
		if ( !isBlank(form.height1.value) || !isBlank(form.height2.value) ) {
			if ( isNum(form.height1.value) && isNum(form.height2.value) ) {
				var num = ( parseFloat(form.height1.value) * 12 + parseFloat(form.height2.value) ) * 2.54; // Convert ft and in to cm
				form.height1.value = num.toFixed(2);
				form.height2.value = '';
			}
		}
		document.getElementById("units_label").innerHTML = "ENGLISH";
		hideID('inch_field');
	}
}

function calcHeight(form) {
	var unitLabel = document.getElementById("units_label").innerHTML;
	var height = null;
	
	// Calculate Height in INCHES or METERS
	if (unitLabel == "ENGLISH") {
		// Calculate Height in METERS
		if ( !isBlank(form.height1.value) ) {
			if ( isNum(form.height1.value) ) {
				var height = parseFloat(form.height1.value)/100; // Convert cm to m
				return height;
			} else {
				alert("Please enter a valid Height"); form.height1.focus(); return false;
			}
		} else {
			alert("Please enter a Height"); form.height1.focus(); return false;
		}
	} else {
		// Calculate Height in INCHES
		if ( isBlank(form.height1.value) && isBlank(form.height2.value) ) {
			alert("Please enter a Height"); form.height1.focus(); return false;
		}
		if ( !isBlank(form.height1.value) && !isNum(form.height1.value) ) {
			alert("Please enter a valid Height (feet)"); form.height1.focus(); return false;
		}
		if ( !isBlank(form.height2.value) && !isNum(form.height2.value) ) {
			alert("Please enter a valid Height (inches)"); form.height2.focus(); 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) {
	var unitLabel = document.getElementById("units_label").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 ) {
		alert("Error: Division by Zero (height is 0)"); return false;
	}
	
	// Validate BMI
	if (isBlank(form.bmi.value)) {
		alert("Please enter a valid BMI (10-40)"); form.bmi.focus(); return false;
	} else 	if (!isNum(form.bmi.value)) { 
		alert("Enter a valid BMI (10-40)"); form.bmi.focus(); return false;
	} else {
		var bmi = parseFloat(form.bmi.value);
	}

	if (unitLabel == "ENGLISH") {
		var weight = bmi *  (height*height);
	} else {
		var weight = (bmi / 703) * (height*height);
	}
	form.weight.value = weight.toFixed(2);
	updateChart(bmi);
}

function calcBMI(form) {
	var unitLabel = document.getElementById("units_label").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 ) {
		alert("Error: Division by Zero (height is 0)"); return false;
	}

	
	// Validate Weight
	if (isBlank(form.weight.value)) {
		alert("Please enter a valid Weight"); form.weight.focus(); return false;
	} else if (!isNum(form.weight.value)) { 
		alert("Enter a valid Weight"); form.weight.focus(); return false;
	} else {
		var weight = parseFloat(form.weight.value);
		form.weight.value = weight;
	}
	if ( weight < 0 ) {
		alert("Weight must be Positive"); form.weight.focus(); return false;
	}

	if (unitLabel == "ENGLISH") {
		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)
	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,F9F9F9&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;
}