/*
 * Javascript to calculate BMI & ajax code to control jquery slide panel
 *

 */
// jquery slide panel code
 $(document).ready(function(){
    
	$(".btn-slide").click(function(){
	clearAll();
		$("#panel").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});
	 
});


// check that a value has been entered
function keyCheck(value, limitNum)
{
	if(isNaN(value))
	{
		return false;
	}
	else
	{
		return true;
	}
}

// calculate Body Mass Index
function  calculateBMI() {
  var weight = document.form.weight.value
  var height = document.form.height.value
  
  if(keyCheck(height, 300) == false)
	{
		alert('Please enter a numeric value for the height');
		document.form.height.focus();
		return;
	}

	if(keyCheck(weight, 300) == false)
	{
		alert('Please enter a numeric value for the weight');
		document.form.weight.focus();
		return;
	}
  
      // make sure there is data first
    if (weight != '' && height != '')
        {

        // see that they don't go over the limits
        if (parseInt(weight) > 300 || parseInt(height) > 300)
            {
            alert('Please make sure the Weight is below 300kg \n and the Height is below 3 metres.');
            return;
            }
        else
            {
            // calculate the result
            var height2 = height / 100
            var BMI = weight  / (height2 * height2)
            BMI=custRound(BMI,1);

            // check the range
            if(parseInt(BMI) <20 || parseInt(BMI) >=25)
                {

                // show the contents
                document.form.BodyMassIndex.value=BMI;
                }
            else
                {

                // show the contents
                document.form.BodyMassIndex.value=BMI;
                }
            }
        }
    else
        {
        alert('Please make sure the Weight and \n Height fields have been entered.');       
        return;
        }
}

//round calculated value
function custRound(x,places) {
  return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
}

// clear all text boxes
function clearAll() {
document.form.height.value='';
document.form.weight.value='';
document.form.BodyMassIndex.value='';
}