$(document).ready(function(){
						   
	// CALCULATORS
	
	// bmi
	$('#bmi-form #bmi-submit').click(function(){
		$('.result').html('&nbsp;');
		$('table.bmi-table tr').removeClass('active');
		
		height = $('#bmi-form #height').val();
		weight = $('#bmi-form #weight').val();
		
		if(isNaN(height) || height=='') { 
			$('.result').html('<span class="errormsg">Please enter your height!</span>'); 
		} else if(isNaN(weight) || weight=='') { 
			$('.result').html('<span class="errormsg">Please enter your weight!</span>'); 
		} else {
			height = height / 100;
			bmi = weight / ( height * height );
			bmi = (Math.round(bmi*10)/10);
			$('.result').html('Your BMI is <strong>'+bmi+'</strong>');
			
			if(bmi<18.5) {
				$('#bmi-under').addClass('active');
			} else if(bmi>=18.5 && bmi<25) {
				$('#bmi-normal').addClass('active');
			} else if(bmi>=25 && bmi<30) {
				$('#bmi-over').addClass('active');
			} else if(bmi>=30) {
				$('#bmi-obese').addClass('active');
			}
		}		
		return false;									  
	});
	
	// waist to hip
	$('#waist-to-hip-form #waist-to-hip-submit').click(function(){
		$('.result').html('&nbsp;');
		$('table.waist-to-hip-table td').removeClass('active');
		
		waist = $('#waist-to-hip-form #waist').val();
		hip = $('#waist-to-hip-form #hip').val();
		
		if(isNaN(waist) || waist=='') { 
			$('.result').html('<span class="errormsg">Please enter your waist circumference!</span>'); 
		} else if(isNaN(hip) || hip=='') { 
			$('.result').html('<span class="errormsg">Please enter your hip circumference!</span>'); 
		} else {
			ratio = Math.round((waist/hip)*100)/100;
			
			$('.result').html('Your waist to hip ratio is <strong>'+ratio+'</strong>');
			
			if(ratio<=0.8) { $('#f_low').addClass('active'); }
			else if(ratio>=0.85) { $('#f_high').addClass('active'); }
			else { $('#f_med').addClass('active'); }			

			if(ratio<=0.95) { $('#m_low').addClass('active'); }
			else if(ratio>=1) { $('#m_high').addClass('active'); }
			else { $('#m_med').addClass('active'); }
			
		}		
		return false;									  
	});
	
	// bmr
	$('#bmr-form #bmr-submit').click(function(){
		$('.result').html('&nbsp;');
		
		gender = $('#bmr-form input.radio:checked').val();
		age = $('#bmr-form #age').val();
		height = $('#bmr-form #height').val();
		weight = $('#bmr-form #weight').val();
		activity = $('#bmr-form #activity').val();
		
		if(isNaN(age) || age=='') { 
			$('.result').html('<span class="errormsg">Please enter your age!</span>'); 
		} else if(isNaN(height) || height=='') { 
			$('.result').html('<span class="errormsg">Please enter your height!</span>'); 
		} else if(isNaN(weight) || weight=='') { 
			$('.result').html('<span class="errormsg">Please enter your weight!</span>'); 
		} else {
			if( gender == 'male' ) {
				bmr = 66 + (13.7*weight) + (5*height) - (6.8*age);
			} else {
				bmr = 655 + (9.6*weight) + (1.8*height) - (4.7*age);
			}
			den = bmr * activity;
			
			$('.result').html('<p>Your BMR is <strong>'+Math.round(bmr)+' calories ('+Math.round(bmr*4.1868)+' kJ)</strong>.</p><p>Your daily energy needs: <strong>'+Math.round(den)+' calories ('+Math.round(den*4.1868)+' kJ)</strong></p>');		
			
		}		
		return false;									  
	});
	
	// heart rate
	$('#thr-form #thr-submit').click(function(){
		$('.result').html('&nbsp;');
		
		age = $('#thr-form #age').val();
		rhr = $('#thr-form #rhr').val();
		
		if(isNaN(age) || age=='') { 
			$('.result').html('<span class="errormsg">Please enter your age!</span>'); 
		} else if(isNaN(rhr) || rhr=='') { 
			$('.result').html('<span class="errormsg">Please enter your resting heart rate!</span>'); 
		} else {
			mhr = Math.round(220 - age);
			rhr = parseInt(rhr);
			
			$('.result').html('Your Maximum Heart Rate (MHR) is <strong>'+mhr+' bpm</strong>');	
			
			mhr85 = Math.round((mhr - rhr) * 0.85 + rhr);
			mhr75 = Math.round((mhr - rhr) * 0.75 + rhr);
			mhr70 = Math.round((mhr - rhr) * 0.70 + rhr);
			mhr60 = Math.round((mhr - rhr) * 0.60 + rhr);
			mhr50 = Math.round((mhr - rhr) * 0.50 + rhr);
			
			$('#low').text(mhr50+' - '+mhr60);
			$('#average').text(mhr60+' - '+mhr70);
			$('#high').text(mhr75+' - '+mhr85);
			
		} 		
		return false;									  
	});
	
	// hydration
	$('#hydration-form #hydration-submit').click(function(){
		$('.result').html('&nbsp;');
		
		weight = $('#hydration-form #weight').val();
		
		if(isNaN(weight) || weight=='') { 
			$('.result').html('<span class="errormsg">Please enter your weight!</span>'); 
		} else {
			
			litres = Math.round(weight * 14.2 / 113 * 250 / 1000 * 100)/100;
			$('.result').html('You should drink at least <strong>'+litres+' litres</strong> a day.');		
			
		} 		
		return false;									  
	});


});