$(document).ready(function(){
	
	$("#bmi-result").hide();
		$('div.field.imperial').show();
		$('div.field.metric').hide();
		
	jQuery.fn.inputFocus = function() {
		jQuery(this).each(function() {
			jQuery(this).focus()
		});
	} // end plugin


	$('#imperial, #imperial-height').click(function(){
		$('div.field.imperial').show();
		$('div.field.metric').hide();
		
		if($("#cms_show").val()==parseInt($("#cms").val()))
		{
			val = $("#cms").val();
		}
		else
		{
			val = $("#cms_show").val();
		}
		
		$.post("includes/to_metric.php",{
			value: val,
			action: "height-metric"
		}, function(xml) {
			feet = parseInt($("height",xml).text() / 12);
			inches = Math.round($("height",xml).text() - (feet * 12));
			if (feet > 0)
			{
				$("#feet").val(feet);
				$("#feet_show").val(feet);
			}
			if (inches > 0)
			{
				$("#inches").val(inches);
				$("#inches_show").val($("height",xml).text() - (feet * 12));
			}
		});
		
		if($("#kgs_show").val()==parseInt($("#kgs").val()))
		{
			val = $("#kgs").val();
		}
		else
		{
			val = $("#kgs_show").val();
		}
		
		$.post("includes/to_metric.php",{
			value: val,
			action: "weight-metric"
		}, function(xml) {
			stone = parseInt($("weight",xml).text() / 14);
			pounds = Math.round($("weight",xml).text() - (stone * 14));
			if (pounds == 14)
			{
				stone = stone + 1;
				pounds = 0;
			}
			if (stone > 0)
			{
				$("#stone").val(stone);
				$("#stone_show").val(stone);
			}
			if (pounds > 0)
			{
				$("#pounds").val(pounds);
				$("#pounds_show").val($("weight",xml).text() - (stone * 14));
			}
		});
	});
	
	$('#metric, #metric-height').click(function(){
		$('div.field.imperial').hide();
		$('div.field.metric').show();
		
		if(Math.round($("#inches_show").val())==$("#inches").val() && Math.round($("#feet_show").val())==$("#feet").val())
		{
			val = parseInt($("#feet_show").val() * 12) + parseFloat($("#inches_show").val());
			
		}
		else
		{
			if(Math.round($("#inches_show").val())!=$("#inches").val() && Math.round($("#feet_show").val())!=$("#feet").val())
				val = parseInt($("#feet").val() * 12) + parseFloat($("#inches").val());
			else if(Math.round($("#inches_show").val())!=$("#inches").val() && Math.round($("#feet_show").val())==$("#feet").val())
				val = parseInt($("#feet_show").val() * 12) + parseFloat($("#inches").val());
			else if(Math.round($("#inches_show").val())==$("#inches").val() && Math.round($("#feet_show").val())!=$("#feet").val())
				val = parseInt($("#feet").val() * 12) + parseFloat($("#inches_show").val());
		}
		
		$.post("includes/to_metric.php",{
			value: val,
			action: "height-imperial"
		}, function(xml) {
			if ($("height",xml).text() > 0)
			{
				$("#cms").val($("height",xml).text());
				$("#cms_show").val(Math.round(parseFloat($("height",xml).text())*100)/100);
			}
		});
		
		if(Math.round($("#pounds_show").val())==$("#pounds").val() && Math.round($("#stone_show").val())==$("#stone").val())
		{
			val = parseInt($("#stone_show").val() * 14) + parseFloat($("#pounds_show").val());
			jQuery("input#cms_show").inputFocus();
		}
		else
		{
			if(Math.round($("#pounds_show").val())!=$("#pounds").val() && Math.round($("#stone_show").val())!=$("#stone").val())
				val = parseInt($("#stone").val() * 14) + parseFloat($("#pounds").val());
			else if(Math.round($("#pounds_show").val())!=$("#pounds").val() && Math.round($("#stone_show").val())==$("#stone").val())
				val = parseInt($("#stone_show").val() * 14) + parseFloat($("#pounds").val());
			else if(Math.round($("#pounds_show").val())==$("#pounds").val() && Math.round($("#stone_show").val())!=$("#stone").val())
				val = parseInt($("#stone").val() * 14) + parseFloat($("#pounds_show").val());
		}
		
		$.post("includes/to_metric.php",{
			value: val,
			action: "weight-imperial"
		}, function(xml) {
			if ($("weight",xml).text() > 0)
			{
				$("#kgs").val($("weight",xml).text());
				$("#kgs_show").val(Math.round(parseFloat($("weight",xml).text())*100)/100);
			}
		});
	});
	
	$("form#bmi-form").submit(function(){
		var element = document.getElementsByName('imperial-height');
		var bt_count = element.length; // can't use element.length in the loop, as it would decrement
		
		for (var i = 0; i <bt_count; i++)
		{
			if (element[i].checked == true)
			{
				var option_selected = element[i].value;
			}
		}
			
		if($("#inches").val()=='')
		{
			$("#inches").val(0);
		}
		if($("#inches_show").val()=='')
		{
			$("#inches_show").val(0);
		}
		
		if($("#pounds").val()=='')
		{
			$("#pounds").val(0);
		}
		if($("#pounds_show").val()=='')
		{
			$("#pounds_show").val(0);
		}
			
		$.post("includes/bmi_calculation.php",{
			imperial_selected: option_selected,
			heightm: $("#cms_show").val(),
			weightm: $("#kgs_show").val(),
			heighti: parseInt($("#feet").val() * 12) + parseInt($("#inches").val()),
			weighti: parseInt($("#stone").val() * 14) + parseInt($("#pounds").val())
		}, function(xml) {
			$("#form-fields").hide();
			$("#bmi-result").show();
			$("#bmi-value").text($("bmi",xml).text());
			bmi_val = parseInt($("#bmi-value").text() * 100);

			$("p#below").hide();
			$("p#normal").hide();
			$("p#above").hide();
			$("p#high").hide();
			if (bmi_val < 1850)
			{
				$("p#below").show();
			}
			if ((bmi_val >= 1850) && (bmi_val < 2500))
			{
				$("p#normal").show();
			}
			if ((bmi_val >= 2500) && (bmi_val < 3000))
			{
				$("p#above").show();
			}
			if (bmi_val >= 3000)
			{
				$("p#high").show();
			}
		});
		return false;
	});

	$("#bmi-button2").click(function(){
		$("#form-fields").show();
		$("#bmi-result").hide();
	});
});