
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=''
	document.data.statm1.value=''
	document.data.statm2.value=''
	document.data.statm3.value=''
	document.data.statm4.value=''
	document.data.statm5.value=''
	document.data.statm6.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 == 0.00) {
		alert("YOU MUST BE INVISIBLE? PLEASE TRY AGAIN!");
	}
	if (h <= 1.22) {
		alert("You have entered your height as "+dispnum(h)+" Meter "+(h_cms)+" centimeters"+" ?\n\n If this is correct, please consult your Doctor regarding your ideal weight or\n\nenter a height of at least 1 metre 23 cms then try again.");
   }
	if (h >= 2.13) {
		alert("You have entered your height as "+dispnum(h)+" Metres "+(h_cms)+" centimeters"+"?\n\n If this is correct, please consult your Doctor regarding your ideal weight or\n\nenter a height of less than 2 metres 12 cms then 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("You have entered your weight as "+dispnum(w)+" Kgs? If this is correct, you are seriously underweight!\n\nWe would advise you to see a doctor regarding putting on some healthy weight.\n\nIf you made a mistake, please click the clear button and try again.");
	}
	if (w >= 227) {
		alert("You have entered your weight as "+dispnum(w)+" Kgs? That is much too heavy\n\nPlease try again.");
	}
	bmi = w / (h*h);
	max_kgs = 25*h*h;
	weight_in_pounds = (max_kgs*2.2);
	document.data.bmi.value = dispnum(bmi);
   if (bmi )
		document.data.statm1.value = "Your body mass index is";
   if (bmi )
		document.data.statm2.value = "Your Maximum Healthy Weight is";
   if (bmi )
		document.data.statm3.value = "stone,";
   if (bmi )
		document.data.statm4.value = "pounds (";
   if (bmi )
		document.data.statm5.value = ") kgs.";
   if (bmi )
		document.data.statm6.value = "These figures are based on a healthy B M I of 25 (MAXIMUM).";
	if (bmi < 18.5 )
		document.data.expl.value = "You are UNDERWEIGHT - We suggest you put some healthy weight on. Our products can help you do this.";
	if (bmi >= 18.5 && bmi <= 24.9 )
		document.data.expl.value = "You are an IDEAL WEIGHT, maintain it with a healthy diet and regular exercise.";
	if (bmi > 25 && bmi <= 29.9 )
		document.data.expl.value = "You are OVERWEIGHT - Try losing weight making sure it doesn't rise! Try reducing the quantity of food you eat.";
	if (bmi > 30 )
		document.data.expl.value = "You are OBESE - It is very important to lose weight A.S.A.P We advise you to contact your Doctor first!";
   if (bmi > 40 )
		document.data.expl.value = "You are VERY OBESE - You must lose weight NOW!! We would strongly advise you to consult your doctor first.";
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);
}
