$(document).ready(function() {
	$('#laskurit_link').click(function() {
		if($('#laskurit_section').is(':hidden')) {
			$('#laskurit_section').show();
		}
	});			$('#ip_link').click(function() {		if($('#ip_section').is(':hidden')) {			$('#ip_section').show();		}	});
	
	$('#imgClose').click(function() {
		if($('#laskurit_section').is(':visible')) {
			$('#laskurit_section').slideUp(200);
			$('#height').val('');
			$('#weight').val('');
			$('#errorHeight').html('').hide();
			$('#errorWeight').html('').hide();
			$('#BMIresult').html('').hide();
		}
	});			$('#imgCloseIP').click(function() {		if($('#ip_section').is(':visible')) {			$('#ip_section').slideUp(200);		}	});
	
	$('#btnLaske').click(function() {
		var txtHeight = $('#height').val();
		var txtWeight = $('#weight').val();
		var txtWeight = txtWeight.replace(',', '.');
			if(txtHeight == "" && txtWeight == "") {
				$('#errorHeight').html('*').show();
				$('#errorWeight').html('*').show();
				return;
			} else if(isNaN(txtHeight) || isNaN(txtWeight)) {
				alert("Painoindeksi, pituus ja paino voi laskea vain numeroilla!");
				return;
			} else if(txtHeight == "" && txtWeight != "") {
				$('#errorHeight').html('*').show();
				$('#errorWeight').html('').hide();
				return;
			} else if(txtHeight != "" && txtWeight == "") {
				$('#errorWeight').html('*').show();
				$('#errorHeight').html('').hide();
				return;
			} else {
				$('#errorHeight').html('').hide();
				$('#errorWeight').html('').hide();
			}
		/*Converts from KG to LB*/
		var weightLB = txtWeight * 2.20462262;
		var weightLBformatted = weightLB.toFixed(2);
		
		/*Converts from CM to FT*/
		var inches = txtHeight * 0.393700787;
		var meters = txtHeight / 100;
		
		var feet = parseInt(inches / 12);
		inches = inches % 12
		inches = inches.toFixed(1);
		
		/*Calculates BMI*/
		var height2 = txtHeight / 100
		var resultBMI = txtWeight / (height2 * height2);
		var resultBMIformatted = resultBMI.toFixed(1);
		var resultBMIformatted = resultBMIformatted.replace('.', ',');		
		$('#BMIresult').html('<span style="font-size: 9pt;">Painoindeksisi: </span>' + resultBMIformatted + '<br /><span style="font-size: 9pt;">Pituutesi: </span>' + +feet+' <span style="font-size: 9pt;">ft</span> '+inches+' <span style="font-size: 9pt;">in</span>' + '<br /><span style="font-size: 9pt;">Painosi: </span>' + weightLBformatted+' <span style="font-size: 9pt;"> lb</span>').fadeIn(500);
	});
	
	$('#btnReset').click(function() {
		$('#errorHeight').html('').hide();
		$('#errorWeight').html('').hide();
		$('#BMIresult').html('').fadeOut(100);
	});
});
