$(document).ready(function() {

	

	// Defualt Imperial & Hide Metric by Default	
	$("label[for='height-cms']").hide();
	$("label[for='weight-kilo']").hide();
	$("#unit-imp").attr("checked", "checked");
	
	// On Click - Hide or Show Proper Form
	$("#unit-imp").click(function() {
		$("label[for='height-cms']").hide();
		$("label[for='weight-kilo']").hide();
		$("label[for='height-feet']").show();
		$("label[for='height-inches']").show();
		$("label[for='weight-pounds']").show();	
		$("#height-cms").val("");		
		$("#weight-kilo").val("");		
	});
	
	$("#unit-metric").click(function() {
		$("label[for='height-feet']").hide();
		$("label[for='height-inches']").hide();
		$("label[for='weight-pounds']").hide();
		$("label[for='height-cms']").show();
		$("label[for='weight-kilo']").show();
		$("#height-feet").val("");
		$("#height-inches").val("");		
		$("#weight-pounds").val("");				
	});
	
	// Clear Function
	$("#clear").click(function() {
		$("#height-feet").val("");
		$("#height-inches").val("");
		$("#height-cms").val("");
		$("#weight-pounds").val("");
		$("#weight-kilo").val("");
		$("#bmi div.output").empty();	
	});
	
	// Submit Function
	$("#calculate").click(function() {
	
		// Clear Things
		$("#bmi div.output").empty();
	
		// Rounding function
		function roundNumber(num, dec) {
			var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
			return result;
			}
					
	
	
		// Find out if it's metric or imperial
		var units = $("input[name='unit']:checked").val();
		
		
		if(units == 'imp')
			{											
			var weight_pounds = $('#weight-pounds').val();
			var feet = $('#height-feet').val();
			var inches = $('#height-inches').val();
			
			// Start Checks - is it empty?
			if(weight_pounds == '' || feet == '' || inches == '')
				{
				var message = "<p class='alert'>You are missing required information.</p>";
				$("#bmi div.output").append(message);
				return false;
				}
				
			// Is it a number?
			if(isNaN(weight_pounds) || isNaN(feet) || isNaN(inches))
				{
				var message = "<p class='alert'>You entered invalid data.</p>";
				$("#bmi div.output").append(message);
				return false;
				}	
									
			var feet_in_inches = feet * 12; 
			var height = (feet_in_inches*1 + inches*1);
			var weight = weight_pounds * 703;
			var bmi = weight  / (height * height); 
			var rounded = roundNumber(bmi,1)			
			var message = "<p class='success'>Your BMI is approximately "+ rounded +"</p>";
			$("#bmi div.output").append(message);			
			}
		
		if(units == 'metric')
			{	
			// Calculate Metric BMI
  			var weight = $('#weight-kilo').val();
  			var height = $('#height-cms').val();
  			
  			// Start Checks - is it empty?
			if(weight == '' || height == '')
				{
				var message = "<p class='alert'>You are missing required information.</p>";
				$("#bmi div.output").append(message);
				return false;
				}
				
			// Is it a number?
			if(isNaN(weight) || isNaN(height))
				{
				var message = "<p class='alert'>You entered invalid data.</p>";
				$("#bmi div.output").append(message);
				return false;
				}	
  			
  			var height2 = (height / 100);
  			var bmi = weight  / (height2 * height2);
  			var rounded = roundNumber(bmi,1)  			
  			var message = "<p class='success'>Your BMI is approximately "+ rounded +"</p>";
  			$("#bmi div.output").append(message);					
			}	
	
		
	
		
	return false;
	});
	

});