//--------- bmi section ---------//



function calc_bmi(type) {



	//var wg=$('#bmiwg').val();	
	var ht=0;
	var wt=0;
	if(type=="imperial")
	{
		lbs=parseInt($('#bmiwtlbs').val());
		if (isNaN(lbs)) {
			lbs=0;
		}
		inches=$('#bmihtin').val();
		if (isNaN(inches)) {
			inches=0;
		}
		ht=(($('#bmihtft').val()*12)+parseInt(inches))*2.54;
		wt=(($('#bmiwtstone').val()*14)+parseInt(lbs))/2.2;
	}
	else if(type=="metric")
	{
		ht=$('#bmihtcms').val();
		wt=$('#bmiwtkgs').val();	
	}
	
	//make sure weight is metric
	
	//$('#bmiresult').text("wwwww");
	//SI value
	var h = ht/100;
	var si = Math.round((wt/(h*h))*100)/100;
	$('#bmivalue').text("Your BMI is:- "+si);

	//If value is not given for weight
	if(wt=="")
	{
		alert("Enter the value for weight");
	}
	//If weight is less than 10
	else if(wt<=10)
	{
		alert("Weight should be greater than 10kgs");
	}
	//If given height is less than 33cms
	else if(ht<33)
	{
		alert("Height should be taller than 33cms");
	}
	
	
	var d = "Your BMI category is "
	if(si<15)
	{
		d=d + "Very Underweight: - If your BMI is less than 18.4 you are underweight for your height. Are you eating enough calories every day? People who are underweight may be at risk from health problems, including loss of bone density, malnutrition and irregular periods (women).";
		d=d + "However, you may be naturally slim and will not need to change your eating habits. The best way to ensure that your weight is healthy for your height is to speak with your GP.";
	}
	else if(si>=15 && si<=18.5)
	{
		d=d + "Underweight: - If your BMI is less than 18.4 you are underweight for your height. Are you eating enough calories every day? People who are underweight may be at risk from health problems, including loss of bone density, malnutrition and irregular periods (women).";
		d=d + "However, you may be naturally slim and will not need to change your eating habits. The best way to ensure that your weight is healthy for your height is to speak with your GP.";
	}
	else if(si>18.5 && si<=25)
	{	
		d=d + "Ideal Weight: - If your BMI is between 18.5 and 24.9 you're an ideal weight for your height. To maintain this, make sure you eat a healthy, well-balanced diet and keep active with regular exercise. This will lower your chances of developing heart disease, diabetes and other health problems.";
		d=d + "Even if your weight is ideal, you need to keep an eye on your waist size. People who are 'apple-shaped' rather than 'pear-shaped' are more at risk from health problems. ";	
	}
	else if(si>25 && si<=30)
	{
		d=d + "Overweight: - If your BMI is between 25 and 29.9 you're over the ideal weight for your height. Make sure you eat a healthy, well-balanced diet and don't eat more calories than you need to.";
		d=d + "If you're trying to lose weight, get more exercise and avoid snacking and 'crash' diets. If you carry your weight around your stomach, you're 'apple-shaped', rather than 'pear-shaped'. This means you're at more risk of health problems, so you really need to get your weight down. ";
	}
	else if(si>30)
	{
		d=d + "Seriously Overweight:- If your BMI is between 30 and 39.9 you're obese. This means you're well over the ideal weight for your height. This could cause serious health problems and affect your life expectancy.";
		d=d + "Are you eating more than the recommended amount of calories per day? Are you getting enough exercise? Once you gain the confidence to start exercising regularly you'll notice a huge difference.";
		d=d + "Speak with your GP for advice if you've tried to lose weight without success."; 
	}
	$('#bmiresult').text(d);
}
//--------- length/weight conversion section ---------//

function calc_weightlength(type) {

	if (type=='length')
	{
		quantity=$('#lengthquantity').val();
		convertfrom=$('#lengthfrom').val();
		convertto=$('#lengthto').val();
		$('#lengthvalue').text(quantity+" "+$("#lengthfrom option:selected").text()+" = "+calc_convert(quantity,convertfrom,convertto)+" "+$("#lengthto option:selected").text())
	}
	else if (type=='weight')
	{
		quantity=$('#weightquantity').val();
		convertfrom=$('#weightfrom').val();
		convertto=$('#weightto').val();
		$('#weightvalue').text(quantity+" "+$("#weightfrom option:selected").text()+" = "+calc_convert(quantity,convertfrom,convertto)+" "+$("#weightto option:selected").text())
	}
	else if (type=='volume')
	{
		quantity=$('#volumequantity').val();
		convertfrom=$('#volumefrom').val();
		convertto=$('#volumeto').val();
		$('#volumevalue').text(quantity+" "+$("#volumefrom option:selected").text()+" = "+calc_convert(quantity,convertfrom,convertto)+" "+$("#volumeto option:selected").text())
	}

}

function calc_convert(quantity,lengthfrom,lengthto) {
	
		//var lfrom= new Array();
		
		//lquantity=$('#lengthquantity').val();
		//d=$('#lengthfrom').val();
		
		lfrom1=lengthfrom.split("/");
		lfrom2=lengthfrom.split("*");		
		//alert(lengthfrom+ " "+lfrom1.length+" "+lfrom2.length);
		if(lfrom1.length==2)
		{
			lengthvalue=quantity*lfrom1[0];
			lengthvalue=lengthvalue/lfrom1[1];
			//alert(lengthvalue+" "+quantity);
		}
		else if(lfrom2.length==2)
		{
			lengthvalue=quantity*lfrom2[0];
			lengthvalue=lengthvalue*lfrom2[1];
			//alert(lengthvalue+" "+quantity+" "+lfrom2[0]+" "+lfrom2[1]);
		}
		else 
		{
			lengthvalue=quantity*lfrom1[0];
		}
		//d=$('#lengthto').val();
		
		lto1=lengthto.split("/");
		lto2=lengthto.split("*");		
		if(lto1.length==2)
		{
			lengthvalue=lengthvalue/lto1[0];
			lengthvalue=lengthvalue*lto1[1];
		}
		else if(lto2.length==2)
		{
			//alert("***"+lengthto+" "+lengthvalue);
			lengthvalue=lengthvalue/lto2[0];
			lengthvalue=lengthvalue/lto2[1];
		}
		else 
		{
		//alert (lengthvalue);
			lengthvalue=lengthvalue/lto1[0];
		}
		s=(lengthvalue.toFixed(2)).split(".");
		if (s[1]=="00")
		{
		return s[0];
		}
		else
		{
		return lengthvalue.toFixed(2)
		}
		

}


//--BMI Calc --//
function bmiCalcHide(measure){
	
	
	$(".bmiwrap").hide();
	if (measure=='imperial'){
		$("#bmiimperial").show();
		var cssObj = {backgroundColor: "#e0e0e0"};
		$("#bmibtnimperial").css(cssObj);
		cssObj = {backgroundColor: "#ffffff"};
		$("#bmibtnmetric").css(cssObj);
	}else{
		$("#bmimetric").show();
		var cssObj = {backgroundColor: "#e0e0e0"};
		$("#bmibtnmetric").css(cssObj);
		cssObj = {backgroundColor: "#ffffff"};
		$("#bmibtnimperial").css(cssObj);
	}
	
}


