// Copyright 2009, whatismybmi.co.uk. All Rights Reserved.
// It is illegal copy our HTML content,
// CSS style sheets and JavaScript functions.
window.onload = init; 

function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}



function init() {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.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 (answs == 'optional') {
			form.weightstones.value = 0;
			answs = 0;
			}
		
			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 (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) {
	$.prompt(errMsg.message, { prefix: 'impromptu' });	
	//alert();
	}		
}

//General Array Function
function MakeArray(n) {
   this.length = n;
   for (var i = 1; i <=n; i++) {
     this[i] = 0;
   }
}

//Initialize Days of Week Array
days = new MakeArray(7);
days[0] = "Saturday"
days[1] = "Sunday"
days[2] = "Monday"
days[3] = "Tuesday"
days[4] = "Wednesday"
days[5] = "Thursday"
days[6] = "Friday"

//Initialize Months Array
months = new MakeArray(12);
months[1] = "January" 
months[2] = "February" 
months[3] = "March" 
months[4] = "April" 
months[5] = "May" 
months[6] = "June" 
months[7] = "July" 
months[8] = "August"
months[9] = "September" 
months[10] = "October"
months[11] = "November"
months[12] = "December"

//Day of Week Function
function compute(form) {

   var val1 = parseInt(startday, 10)
   if ((val1 < 0) || (val1 > 31)) {
      alert("Day is out of range")
   }
   var val2 = parseInt(startmonth, 10)
   if ((val2 < 0) || (val2 > 12)) {
      alert("Month is out of range")
   }  
   var val2x = parseInt(startmonth, 10)
   var val3 = parseInt(startyear, 10)
   if (val3 < 1900) {
      alert("You're that old!")
   }
   if (val2 == 1) {
      val2x = 13;
      val3 = val3-1
   }
   if (val2 == 2) {
      val2x = 14;
      val3 = val3-1
   }
   var val4 = parseInt(((val2x+1)*3)/5, 10)
   var val5 = parseInt(val3/4, 10)
   var val6 = parseInt(val3/100, 10)
   var val7 = parseInt(val3/400, 10)
   var val8 = val1+(val2x*2)+val4+val3+val5-val6+val7+2
   var val9 = parseInt(val8/7, 10)
   var val0 = val8-(val9*7)
  // form.result1.value = months[val2]+" "+startday +", "+startyear
   form.result2.value = days[val0]
   $('#update_button').attr('disabled', true);
   $('#clear_button').attr('disabled', false);
} 

var startday ;
var startmonth;
var startyear; 
  
$(document).ready(function() {

	$('#update_button').attr('disabled', true);
	$('#clear_button').attr('disabled', 'disabled');	

  	$(function() {
		$( "#datepicker" ).datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: 'dd-mm-yy',
			yearRange: '1900:2014',
			onSelect: function(dateText) {
				//alert("Parse dateText into your variables. dateText = " + dateText);
				var arr_dateText = dateText.split("-");
				startday = arr_dateText[0];
				startmonth = arr_dateText[1];
				startyear = arr_dateText[2]; 
				$('#update_button').attr('disabled', false);				
			}
							
		});
	});
});








