MAX_HEALTHY_BMI = 25;
MIN_HEALTHY_BMI = 18.5;
OBESE_BMI		  = 30;
VERY_OBESE_BMI  = 40;

var preferred_weight_unit;
var preferred_height_unit;
var BMI = 0;

/////////////////////////////////////////////////////////////////////////////////////////////
function weight_as_string(weight_in_kg)
{
	if(document.main_form.weight_units[0].checked == true)	// kg
	{
		return weight_in_kg.toFixed(1)+" kg";
	}

	if(document.main_form.weight_units[1].checked == true)	// pounds
	{
		return (weight_in_kg*2.20462262).toFixed(1)+" pounds";
	}
	if(document.main_form.weight_units[2].checked == true)	// stone
	{
		var pounds = weight_in_kg*2.20462262;
		var stone = 0;
		while(pounds >=14)
		{
			stone++;
			pounds -= 14;
		}
		return ( (stone.toFixed(0))+" st "+(pounds.toFixed(0))+" pounds");
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////

function calculate()
{
	var height_m;
	var weight_kg;

	// read in height and, if needed, convert to metres

	if(document.main_form.height_units[0].checked == true)	// metres
	{
		height_m = parseFloat(document.main_form.the_height.value);
	}

	if(document.main_form.height_units[1].checked == true)	// centimetres
	{
		height_m = 0.01 * parseFloat(document.main_form.the_height.value);

	}
	if(document.main_form.height_units[2].checked == true)	// feet, inches
	{
		var height_feet = document.main_form.the_height.value;

		height_feet = height_feet.replace(",", "'")	// allow comma to be used as feet, inches separator
		height_feet = height_feet.replace(" ", "'")	// allow space to be used as feet, inches separator
		height_feet = height_feet.replace(".", "'")	// allow stop to be used as feet, inches separator

		height_feet = height_feet.replace(/'+/, "'")	// remove duplicate separators


		var height_inches= 0;

		var heightArray = height_feet.split("'");

		var feet = 0;
		var inches = 0;

		if(heightArray.length > 0)
			feet = parseInt(heightArray[0]);

		if(heightArray.length > 1)
			inches = parseInt(heightArray[1]);

		if( isNaN(inches) )
		{
			inches = 0;
		}

		if( isNaN(feet) )
		{
			feet = 0;
		}

		height_inches = (12 * feet) + inches;


		var height_cm = height_inches * 2.54;
		height_m = height_cm * 0.01;
	}

// read in weight and, if needed, convert to kg

	if(document.main_form.weight_units[0].checked == true)	// kg
	{
		weight_kg = parseFloat(document.main_form.weight.value);
		if(isNaN(weight_kg))
			weight_kg = 0;
	}

	if(document.main_form.weight_units[1].checked == true)	// pounds
	{
		var weight_pounds = parseFloat(document.main_form.weight.value);
		if(isNaN(weight_pounds))
			weight_pounds = 0;

		weight_kg =  weight_pounds / 2.20462262;
	}
	if(document.main_form.weight_units[2].checked == true)	// stone
	{
		var weight_stone = document.main_form.weight.value;

		weight_stone = weight_stone.replace(" ", ",")	// allow space to be used as stone pounds separator

		weight_stone = weight_stone.replace(/,+/, ",")	// remove duplicate separators


		var weight_pounds = 0;

		var weightArray = weight_stone.split(",");

		var stone = 0;
		var pounds = 0;

		if(weightArray.length > 0)
			stone = parseInt(weightArray[0]);

		if(weightArray.length > 1)
			pounds= parseInt(weightArray[1]);

		if( isNaN(pounds) )
		{
			pounds = 0;
		}

		if( isNaN(stone) )
		{
			stone = 0;
		}

		weight_pounds = (14 * stone) + pounds;

		weight_kg =  weight_pounds / 2.20462262;
	}


	var height_squared = height_m*height_m;
	BMI = weight_kg / height_squared;

	var max_weight = MAX_HEALTHY_BMI*height_squared;

	var min_weight = MIN_HEALTHY_BMI*height_squared;

	var outputString;

	outputString = "Your BMI is "+(BMI.toFixed(1))+".\n";
	outputString += "This suggests that you are ";


	if(BMI < MIN_HEALTHY_BMI)
	{
		outputString += "underweight.\n";
	}

	if( (BMI >= MIN_HEALTHY_BMI) && (BMI <= MAX_HEALTHY_BMI) )
	{
		outputString += "a healthy weight.\n";
	}

	if( (BMI > MAX_HEALTHY_BMI) && (BMI <= OBESE_BMI) )
	{
		outputString += "overweight.\n";
	}

	if(BMI > OBESE_BMI)
	{
		outputString += "obese.\n";
	}
	if(BMI > MAX_HEALTHY_BMI)
	{
		// calculate max weight in users preferred units
		outputString += "\nThe maximum healthy weight for someone of your height is "+weight_as_string(max_weight)+".\n";
	}

	if(BMI < MIN_HEALTHY_BMI)
	{
		// calculate max weight in users preferred units
		outputString += "\nThe minimum healthy weight for someone of your height is "+weight_as_string(min_weight)+".\n";
	}
// write values to textarea

	document.main_form.output_text.value  = outputString;


}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
