var h_imperial = 1; //flags for input modes
var w_imperial = 1;
var pounds_to_kgs = 2.204623;
var inches_to_meters = 39.4
var h_ft, h_ins, h_m, h_cms;
var w_st, w_pds, w_kgs;
var h,w, w1;
var weight_in_pounds;

function dispnum(x) {
	x = Math.floor(x*10)/10; // rounded off to two decimal places
	return(x);
}

function clearvalues(){ // clears all the contents of the form
	document.data.height_feet.value=''
	document.data.height_inches.value=''
	document.data.height_meters.value=''
	document.data.height_cms.value=''
	document.data.weight_stones.value=''
	document.data.weight_pounds.value=''
	document.data.weight_kgs.value=''
	document.data.bmi.value=''
	document.data.expl.value=''
	document.data.max_stones.value=''
	document.data.max_pounds.value=''
	document.data.max_kgs.value=''
}

function initialise() { // initialise the variables
	if (document.data.height_feet.value == '') {
		document.data.height_feet.value = 0;
	}
	if (document.data.height_inches.value == '') {
		document.data.height_inches.value = 0;
	}
	if (document.data.height_meters.value == '') {
		document.data.height_meters.value = 0;
	}
	if (document.data.height_cms.value == '') {
		document.data.height_cms.value = 0;
	}
	if (document.data.weight_stones.value == '') {
		document.data.weight_stones.value = 0;
	}
	if (document.data.weight_pounds.value == '') {
		document.data.weight_pounds.value = 0;
	}
	if (document.data.weight_kgs.value == '') {
		document.data.weight_kgs.value = 0;
	}
}

function compute() { // the main routine
	initialise();
	h_ft = parseFloat(document.data.height_feet.value);
	h_ins = parseFloat(document.data.height_inches.value);
	h_m = parseFloat(document.data.height_meters.value);
	h_cms = parseFloat(document.data.height_cms.value);
	if (h_imperial) {
		h = ((h_ft*12)+h_ins)
		h /= inches_to_meters; }
	else {
		h = ((h_m*100)+h_cms)/100;
	}
	if (h <= 1.22) {
		alert(dispnum(h)+" Metres seems to be too short\n\nPlease try again.");
	}
	if (h >= 2.5) {
		alert(dispnum(h)+" Metres seems to be too tall\n\nPlease try again.");
	}
	w_st = parseFloat(document.data.weight_stones.value);
	w_pds = parseFloat(document.data.weight_pounds.value);
	w_kgs = parseFloat(document.data.weight_kgs.value);
	if (w_imperial) {
		w = ((w_st*14)+w_pds);
		w1 = w;
		w /= pounds_to_kgs;} // pounds to kgs }
	else {
		w=w_kgs;
	}
	if (w <= 25) {
		alert(dispnum(w)+" Kgs? Thats much too light\n\nPlease try again.");
	}
	if (w >= 227) {
		alert(dispnum(w)+" Kgs? Thats much too heavy\n\nPlease try again.");
	}
	tempbmi = w / (h*h);
	bmi = Math.floor(tempbmi*10)/10;
	max_kgs = 25*h*h;
	weight_in_pounds = (max_kgs*2.2);
	document.data.bmi.value = dispnum(bmi);
	if (bmi < 18.5 )
		document.data.expl.value = "Underweight - you may need to put weight on";
	if (bmi >= 18.5 && bmi <= 25 )
		document.data.expl.value = "OK - ideal weight range, try to maintain";
	if (bmi > 25 && bmi <= 30 )
		document.data.expl.value = "Overweight - make sure your weight doesn't rise";
	if (bmi > 30 )
		document.data.expl.value = "Obese - important to lose weight";
max_imperial = (weight_in_pounds/14)*10;
max_imperial = Math.round(max_imperial);
max_imperial = max_imperial/10;
document.data.max_stones.value = Math.floor(max_imperial);
temp1 = max_imperial - (Math.floor(max_imperial));
temp1 = Math.round(temp1*10);
document.data.max_pounds.value = Math.round(temp1*1.4);
document.data.max_kgs.value = dispnum(max_kgs);
}