﻿// global vars
var h_imperial = 0; //flags for input modes
var w_imperial = 0;

function initCalc () {
	// give initial values for weight / height values
	document.getElementById("ft").value = 5;
	document.getElementById("in").value = 6;
	document.getElementById("mt").value = 1;
	document.getElementById("cm").value = 50;
	
	document.getElementById("kg").value = 100;
	document.getElementById("st").value = 9;
	document.getElementById("lb").value = 0;
	
	// hide rows which are required with initial settings
	hide("rwResult")
	hide("rwHeightImperial")
	hide("rwWeightImperial")
}

function calc() {
	if(!document.getElementById) {
		alert("Please upgrade your browser.");
		return
	}
	
	// clear any previous result
	clearResults();
	
	// declare vars
	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;

	// get height values
	h_ft = parseFloat(getValue('ft'));
	h_ins = parseFloat(getValue('in'));
	h_m = parseFloat(getValue('mt'));
	h_cms = parseFloat(getValue('cm'));
	
	// get weight values
	w_st = parseFloat(getValue('st'));
	w_pds = parseFloat(getValue('lb'));
	w_kgs = parseFloat(getValue('kg'));
	
	// validate height
	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)+" - Height too short. Please check your height and reinput it.");
	}
	if (h >= 2.5) {
		alert(dispnum(h)+" - Height too tall. Please check your height and reinput it.");
	}
	
	// validate weight
	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)+" Too light. Please check your weight entries and reinput them.");
	}
	if (w >= 227) {
		alert(dispnum(w)+" Too heavy. Please check your weight entries and reinput them.");
	}
	
	tempbmi = w / (h*h);
	bmi = Math.floor(tempbmi*10)/10;
	max_kgs = 25*h*h;
	weight_in_pounds = (max_kgs*2.2);
	document.getElementById('BMIResult').innerHTML = 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);
	*/
	
	// success so show results
	show("rwResult");
}

function dispNum(x) {
	x = Math.floor(x*10)/10; // rounded off to two decimal places
	return(x);
}


function clearResults() {
	document.getElementById("BMIResult").innerHtml = '';
}

function hide(eID) {
	document.getElementById(eID).style.display = 'none';
}

function show(eID) {
	document.getElementById(eID).style.display = 'block';
}

function getValue(eID) {
	return document.getElementById(eID).value;
}

