function value_exist_check(a,b,c,d){
  //a,c: weight and height input values;
  //b,d: unit of mesurement for b;
	var result = ((a !=="" ) && (c !=="" ));
  	if (!result){
		  var	errormsg = "";	
    	if (a ==""){
  		  errormsg += "Height: Value not entered (" + b + "). <br />";
  		}	
    	if (c ==""){
    		errormsg += "Weight: Value not entered (" + d + ").";
  		}
    document.getElementById("result").innerHTML = "<p class="+"error"+">" + errormsg + "</p>";
	} 	
	return result;
}

function h_imperialToMetric(a,b){
  //a:height in feet; b: height in inches;
	return ((a*12)+b)*2.54; // 1inch = 2.54cm	;		
}	

function w_imperialToMetric(c,d){
  //c: weight in stones; d: weight in pounds;
	return ((c*14)+d)/2.204623;		// 1kg = 2.204623pounds
}	

function corect_value(h,w){
	a = (h<250 && h>100);
	b = (w<250 && w>20);
	var result = (a && b);
	if (!result){ 
	 	var	errormsg = "";
 		if (!a){
      errormsg += 'Height: please check and re-enter that value.<br />';
		}
		if (!b){	
			errormsg += 'Weight: please check and re-enter that value.';
		}	
    document.getElementById("result").innerHTML = "<p class="+"error"+">" + errormsg + "</p>";			 
	}	 
	return result;	
}

function bmicalc(h,w){
  return Math.round(((w*10000)/(h*h))*100)/100; //1m = 100cm;
}

function results(bmi){
  var interp = ""
  if (bmi < 18.5) {
    interp = "If your BMI is less than 18.4 you are underweight for your height. This is not ideal as people who are underweight may be at risk from health problems, including loss of bone density, malnutrition and irregular periods (women).</p><p>Try to get up to your ideal weight if you can. You can up your calorie intake but make sure you're eating a healthy, well-balanced diet. Some types of exercise burn more calories than others so make sure you're eating more calories than you're using. <a href="+"contact_details.html"+"><span class="+"useful_links"+"><strong>Contact me now</strong></span></a> and I can help you come up with a healthy eating plan and an appropriate exercise routine to ensure you are fit and healthy."
  } else {
      if (bmi < 25.0) {
      	 interp = "Congratulations you are in the healthy range for the height and weight entered. If your BMI is between 18.5 and 24.9 you're an ideal weight for your height which is fantastic. To stay in shape and keep in this range, make sure you eat a healthy, well-balanced diet and keep active with regular exercise.</p><p>Try to get at least 30 minutes of exercise every day. To improve your health further, stop smoking, cut down on salt and high-fat foods, and limit your alcohol intake. <a href="+"contact_details.html"+"><span class="+"useful_links"+"><strong>Contact me now</strong></span></a> if you want to further improve your health and fitness. If you are interested in staying in shape and toning up then I can help you with this now. By embarking on a regular exercise programme and eating healthily you can ALSO have the body shape that you deserve."
      } else {
          if (bmi < 30.0) {
          	 interp = "Unfortunately you are considered overweight, based on the height and weight you entered. If your BMI is between 25 and 29.9 you're over the ideal weight for your height. This is a good time to think about changing your diet and exercise routine.</p><p>There are many benefits to losing weight; such as increased energy levels, increased self-esteem, being able to wear the clothes that you want to and looking and feeling good about yourself. Take the next step NOW to look and feel good, by reducing the amount of calories you eat a day by 500 and taking daily exercise. By doing this you can lose 5-10kg (11-22lbs) in about six months. Eat a well balanced diet, cut down on salt and alcohol. Try to eat more fresh fruit and vegetables. Also, eat smaller portions at mealtimes and avoid snacking between meals. <a href="+"contact_details.html"+"><span class="+"useful_links"+"><strong>Contact me NOW</strong></span></a> so that I can help you with a healthy eating plan and a fun regular exercise programme to help you lose the weight and have the body that you deserve."
          } else {
					          if (bmi < 40.0) {
          	 					 interp = "If your BMI is between 30 and 39.9 you're obese. This means you're well over the ideal weight for your height. This could cause serious health problems and affect your life expectancy. You need to lose weight NOW. You must reduce the amount of calories you eat a day by a minimum of 500 and take daily exercise. By doing this you can lose 5-10kg (11-22lbs) in about six months.  Eat a well balanced diet, cut down on salt and alcohol. Try to eat more fresh fruit and vegetables. Also, eat smaller portions at mealtimes and avoid snacking between meals. <a href="+"contact_details.html"+"><span class="+"useful_links"+"><strong>Contact me NOW</strong></span></a> so that I can help you with a healthy eating plan and a fun regular exercise programme to help you lose the weight and have the body that you deserve."
										} else {
													 interp = "If your BMI is over 40 you're very obese. This means you're seriously over the ideal weight for your height.  In order to lose this extra weight you need to make positive changes to your lifestyle and stick to them. Losing weight will help prevent severe health problems such as heart disease and diabetes, and extend your life expectancy. You need to lose weight NOW. You must reduce the amount of calories you eat a day by a minimum of 500 and take daily exercise. By doing this you can lose 5-10kg (11-22lbs) in about six months.  Eat a well balanced diet, cut down on salt and alcohol. Try to eat more fresh fruit and vegetables. Also, eat smaller portions at mealtimes and avoid snacking between meals. <a href="+"contact_details.html"+"><span class="+"useful_links"+"><strong>Contact me NOW</strong></span></a> so that I can help you with a healthy eating plan and a fun regular exercise programme to help you lose the weight and have the body that you deserve."
											} 			
            }
    	 }			
		}
		return interp;
}

function imperialBMI(){
	var wst = document.bmicalci.wst.value;
	var wpd = document.bmicalci.wpd.value;	
	var hft = document.bmicalci.hft.value;
	var hin = document.bmicalci.hin.value;
  if (hin == ""){hin = 0;} else {if (hft == ""){hft = 0;}}
  if (wpd == ""){wpd = 0;}	else {if (wst == ""){wst = 0;}}	
	var pst=parseInt(wst);
	var ppd=parseInt(wpd);	
	var pft=parseInt(hft);
	var pin=parseInt(hin);

  if (value_exist_check(hft,'feet / inches',wst,'stones / pounds')){
    var h = h_imperialToMetric(pft,pin);
  	var w = w_imperialToMetric(pst,ppd);
   	if (corect_value(h,w)){
    	var bmi = bmicalc(h,w);
    	document.getElementById("result").innerHTML = '<p class='+"bolded"+'>Your BMI result is: ' + bmi+'</p>'+ "<p>"+results(bmi)+"</p>";	
  	}
	}
}

function metricBMI(){
	var hcm = document.bmicalcm.hcm.value;
	var wkg = document.bmicalcm.wkg.value;	
	var pcm = parseFloat(hcm);
	var pkg = parseFloat(wkg);

	if (value_exist_check(hcm,'cm',wkg,'kg')){	
    var h = hcm;
  	var w = wkg;	
  	if (corect_value(h,w)){
    	var bmi = bmicalc(h,w);
    	document.getElementById("result").innerHTML = '<p class='+"bolded"+'>Your BMI result is: ' + bmi+'</p>'+ "<p>"+results(bmi)+"</p>";	
  	}
	}
}

function clear_error_msg(){ 
 return document.getElementById("result").innerHTML = "";
}