// Copyright 2009, whatismybmi.co.uk. All Rights Reserved.
// It is illegal copy our HTML content,
// CSS style sheets and JavaScript functions.

function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}

function load()
{
window.defaultStatus='';
document.bmifrm.heightimp.focus();
document.getElementById("bmisection").innerHTML= "Your BMI value is:<br/><br/><br/><br/>&nbsp;";
}

function ShowMenu() 
	{
 	if (document.bmifrm.measure[0].checked)
  		{
		document.bmifrm.heightimp.value = '';
		document.bmifrm.heightimppart.value = '';
		document.bmifrm.weightpounds.value = '';
		document.bmifrm.weightstones.value = 0;		
		document.getElementById("impmeasure").style.display = "block";
		document.getElementById("metricmeasure").style.display = "none";
		document.bmifrm.heightimp.focus();
		// document.bmifrm.bmi.value = 0;
		document.getElementById("bmisection").innerHTML= "Your BMI value is:<br/><br/><br/><br/>&nbsp;";
		}
	else {

		document.bmifrm.weightkg.value = '';
		document.bmifrm.heightmetric.value = 1;		
		document.bmifrm.heightmetricpart.value = '';		
		document.getElementById("impmeasure").style.display = "none";
		document.getElementById("metricmeasure").style.display = "block";
		document.bmifrm.heightmetricpart.focus();
		// document.bmifrm.bmi.value = 0;
		document.getElementById("bmisection").innerHTML= "Your BMI value is:<br/><br/><br/><br/>&nbsp;";
	}
}


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function calculate_bmi_imp(pounds,incs)
{
	var sqheight = (incs * incs) / 703;
	var bmi = (pounds/sqheight);
												
	var	weightToLose = pounds - (24.9 * sqheight);
	var	weightToGain = pounds - (18.5 * sqheight);
	
	var	rndWeightToLose = Math.ceil(weightToLose);
	var	rndWeightToGain = Math.floor(weightToGain); 
		
	var rnddownbmi = Math.floor(bmi);
	var decpart = bmi - rnddownbmi;
	decpart = decpart * 10;
	decpart = Math.round(decpart);  // Can use this method or roundNumber function to provide 1 decimal place answer.
	
	if (decpart == 10) {
	rnddownbmi = rnddownbmi + 1;
	decpart = 0;
	}
	
	bmi = rnddownbmi + "." + decpart;
	return {bmi : bmi,Lose : rndWeightToLose,Gain : rndWeightToGain};
}

function calculate_bmi_met(kilog,ht)
{
	var sqheight = (ht * ht) /10000;
	var bmi = (kilog/sqheight);
	
	var weightToLose = kilog - (24.9 * sqheight);
	var weightToGain = kilog - (18.5 * sqheight);
	
	var rndWeightToLose = Math.ceil(weightToLose);
	var rndWeightToGain = Math.floor(weightToGain); 
		
	var rnddownbmi = Math.floor(bmi);
	var decpart = bmi - rnddownbmi;
	decpart = decpart * 10;
	decpart = Math.round(decpart);  // Can use this method or roundNumber function to provide 1 decimal place answer.	
	
	if (decpart == 10) {
	rnddownbmi = rnddownbmi + 1;
	decpart = 0;
	}
	
	bmi = rnddownbmi + "." + decpart;

	return {bmi : bmi,Lose : rndWeightToLose,Gain : rndWeightToGain};
}
	
function cal_bmi_cat(bmivalue) {			
	var bmicat = "none" ;
	var bmivalue = parseFloat(bmivalue);
	
		if (bmivalue < 18.5) {
			bmicat = "UNDERWEIGHT" ;
		}
		else if (bmivalue >= 18.5 && bmivalue <=24.9)  {
			bmicat = "NORMAL WEIGHT" ;
		}
		else if (bmivalue >= 25.0 && bmivalue <=29.9)  {
			bmicat = "OVERWEIGHT" ;
		}
		else {
			bmicat = "OBESE" ;
		}
	return bmicat;	
	  
}


function mhrResults(form) {

	var mhrage = form.mhrageinput.value;
	
try {
	
	if (!mhrage || isNaN(mhrage) || mhrage<=0 ) {
		form.mhrageinput.value = '';
		form.mhrageinput.focus();
		throw new Error("Please enter a valid age");		
		}
	var hrmax =  parseInt(205.8 - (0.685 * mhrage));
	hrmax = roundNumber(hrmax,0);
	document.getElementById("mhrresult").
	innerHTML= "Your Maximum Heart Rate is " +  hrmax + "&nbsp;beats per minute";
	}
catch (errMsg) {
	alert(errMsg.message);
	}		
}

function bmiResults(form) {

var anshi = form.heightimp.value;
var anship = form.heightimppart.value;
var answp = form.weightpounds.value;
var answs = form.weightstones.value;

var anshm = form.heightmetric.value;
var anshmp = form.heightmetricpart.value;
var answk = form.weightkg.value;

try {


		if (document.bmifrm.measure[0].checked)
		{
		
			if (!anshi || isNaN(anshi) || anshi<=0 ) {
			form.heightimp.value = '';
			form.heightimp.focus();
			throw new Error("Please enter a valid height in feet");		
			}
			
			if (!anship || isNaN(anship) || anship<0) {
			form.heightimppart.value = '';
			form.heightimppart.focus();
			throw new Error("Please enter a valid inch part of your height");
			}
			
			if (!answp || isNaN(answp) || answp<0) {
			form.weightpounds.value = '';
			form.weightpounds.focus();
			throw new Error("Please enter a valid weight in pounds");
			}
			
			if (!answs || isNaN(answs) || answs<0) {
			form.weightstones.value = '';
			form.weightstones.focus();
			throw new Error("Please enter a valid weight in stones");
			}
			
			var	HeightPart =  parseInt(form.heightimppart.value) ;   
			var	HeightFeet =  parseInt(form.heightimp.value) * 12 ;
			var	TotalHeightInches = parseInt(HeightFeet + HeightPart);
			
			var WeightPoundsPart = parseInt(form.weightpounds.value) ;
			var WeightStonesPounds = parseInt((form.weightstones.value) * 14) ;
			var WeightPounds = parseInt(WeightPoundsPart + WeightStonesPounds);
			
			var bmidetails = calculate_bmi_imp(WeightPounds,TotalHeightInches);
			var categor = cal_bmi_cat(bmidetails.bmi);
			
			if (bmidetails.Gain < 0){
			bmidetails.Gain = bmidetails.Gain * -1;
			document.getElementById("bmisection").
			innerHTML= "Your BMI value is " +  bmidetails.bmi + " and your category is " + categor + "<br/>You need to gain " + bmidetails.Gain + " pounds to have a normal BMI value.";
			}
			else if (bmidetails.Lose > 0){
			document.getElementById("bmisection").
			innerHTML= "Your BMI value is " +  bmidetails.bmi + " and your category is " + categor + "<br/>You need to lose " + bmidetails.Lose + " pounds to have a normal BMI value.";
			}
			else 
			{
			document.getElementById("bmisection").
			innerHTML= "Your BMI value is " +  bmidetails.bmi + " and your category is " + categor ;
			bmidetails.bmi='<?php echo $bmi ; ?>';
			} 
		
		}
		else
		{

			if (!anshm || isNaN(anshm) || anshm<0 ) {
			form.heightmetric.value = '';
			form.heightmetric.focus();
			throw new Error("Please enter a valid height in metres");		
			}
			
			if (!anshmp || isNaN(anshmp) || anshmp<0) {
			form.heightmetricpart.value = '';
			form.heightmetricpart.focus();
			throw new Error("Please enter a valid centimetre part of your height");
			}
			
			if (!answk || isNaN(answk) || answk<=0) {
			form.weightkg.value = '';
			form.weightkg.focus();
			throw new Error("Please enter a valid weight in kg");
			}
						
			var	HeightPart =  parseInt(form.heightmetricpart.value) ;   
			var	HeightMet =  parseInt(form.heightmetric.value)  * 100 ;
			var	TotalHeightMet = parseInt(HeightMet + HeightPart);
			var WeightKilo = (form.weightkg.value) ;
			
			var bmidetails = calculate_bmi_met(WeightKilo,TotalHeightMet);
			var categor = cal_bmi_cat(bmidetails.bmi);								
			
			if (bmidetails.Gain < 0){
			bmidetails.Gain = bmidetails.Gain * -1;
			document.getElementById("bmisection").
			innerHTML= "Your BMI value is " +  bmidetails.bmi + " and your category is " + categor + "<br/>You need to gain " + bmidetails.Gain + " kg to have a normal BMI value.";
			}
			else if (bmidetails.Lose > 0){
			document.getElementById("bmisection").
			innerHTML= "Your BMI value is " +  bmidetails.bmi + " and your category is " + categor + "<br/>You need to lose " + bmidetails.Lose + " kg to have a normal BMI value.";
			}
			else 
			{
			document.getElementById("bmisection").
			innerHTML= "Your BMI value is " +  bmidetails.bmi + " and your category is " + categor ;
			} 
			
		}
}
	catch (errMsg) {
	alert(errMsg.message);
	}		
}
				


