﻿function Reset(){
    document.forms[0].reset();
    var trBMI = document.getElementById('trBMI');
    var trMessage = document.getElementById('trMessage');
        trBMI.style.visibility = "hidden";
//        trMessage.style.display = "none";
        trMessage.style.visibility = "hidden";
       document.getElementById('tdhh').style.visibility = "hidden";
    }

//Blank Check
function CheckBlank(objID,strControlName)
{
   if (Ltrim_string(objID.value)=="")
   {
		alert(strControlName+": Cannot be left blank");
		objID.value="";
		objID.focus();
		return false;
	}
	return true;
}

//For LTrim Function
function Ltrim_string(objID)
{			
var j = 0,k=0;
if(objID.length>0)
{
	for(var i=0;i<objID.length && k<objID.length;i++)
	{
		if(objID.charAt(i)==" ")
		{
		j++;
		}
		else
		{
		k = objID.length;
		}
	}
	objID =objID.substring(j,objID.length);
}
	return objID;
}

function calc()
{
    var trBMI = document.getElementById('trBMI');
    var trMessage = document.getElementById('trMessage');
    var lblMessage = document.getElementById('lblMessage');
    //var lblBuyNowMessage = document.getElementById('lblBuyNowMessage');
    var lblResult = document.getElementById('lblResult');
    var WT = document.forms[0].wg;
    var HT = document.forms[0].ht;
    
    if(!CheckBlank(HT,"Height")||!CheckAmount(HT,"Height"))
	    return false;
	    
	var wt = document.forms[0].wg.value;
	var ht = document.forms[0].ht.value;
	
	var op=document.forms[0].opt1.value;
	
	
	
	
	//SI value
	var h = ht/100;
	var si = Math.round((wt/(h*h))*100)/100;
	//lblResult.value = si;

	//lblResult.innerHTML = si;
    document.getElementById('tdhh').style.visibility = "hidden";
	//If given height is less than 33cms
	if(ht<33)
	{
		lblResult.innerHTML = 0;
		alert("Height should be taller than 33 cms");
		HT.focus();
 			HT.select();
			return false;
	
	}
	 if(ht>242)
	    {
		    lblResult.innerHTML = 0;
		    alert("Height : should be proper");
		    HT.focus();
 			HT.select();
		    return false;
	    }
	
	 if(! CheckBlank(WT,"Weight") || !CheckAmount(WT,"Weight"))
	    return false;
	
	
	if(op=="pounds" && wt<=22)
	{
		//lblResult.innerHTML = 0;
		si = 0;
		alert("Weight should be greater than 22 lbs");
		WT.focus();
 			WT.select();
		return false;
	}
	if((wt<=10)&& (op!="pounds"))
	{
		lblResult.innerHTML = 0;
		alert("Weight should be greater than 10 kgs");
		WT.focus();
 			WT.select();
		return false;
	}
	
	if(op=="pounds")
	{
		//lblResult.innerHTML=Math.round((si/2.2)*100)/100;
		si = Math.round((si/2.2)*100)/100;
	}
	
	lblResult.innerHTML = si;
	var d = "Your category is "
	
	if(si<18.5)
	{
		lblMessage.innerHTML= d + "Underweight. Your BMI indicating your weight is in the under weight category for the adults of your height. Being under weight may weaken your immune system and put you at greater risk for nutritional deficiency disease and osteoporosis. To achieve healthy weight, embrace healthy eating by choosing a variety of calorie and nutrient rich foods and strength training exercises to promote lean muscle development.";
		lblMessage.style.color='#ce1127';
		//lblBuyNowMessage.innerHTML = "Content for Underweigth";
	}
	else if(si>18.5 && si<=24.9)
	{
		lblMessage.innerHTML= d + "Healthy Weight. Congratulations! Your healthy weight is well worth the effort. It reduces your risk of developing serious health conditions such as blood pressure, heart disease, stroke and diabities.";
		lblMessage.style.color='#479300';
		//lblBuyNowMessage.innerHTML = "Content for Healthy Weight";
	}
	else if(si>25 && si<=29.9)
	{	
		lblMessage.innerHTML = d + "Overweight. Your BMI indicating your weight is in the over weight category for the adults of your height. Being over weight may increase the risk of health problems such as high BP, high blood cholestrol, heart disease.";
		lblMessage.style.color='#ce1127';
		//lblBuyNowMessage.innerHTML = "Content for Overweight";
	}
     else if(si>30 && si<=34.9)
    {
        lblMessage.innerHTML=d + "Obese. Your BMI indicating your weight is in the obese category for adults of your height. Consider the benefits of achieving a healthy weight - a reduced risk of health conditions such as high blood pressure, diabetes and heart disease with improved lifestyle and self esteem.";
        lblMessage.style.color='#ce1127';
        //lblBuyNowMessage.innerHTML = "Content for Obese";
    }
	else if(si>35 && si<=39.9)	
    {
	    lblMessage.innerHTML= d + "Very Obese. Your BMI indicating your weight is in the obese category for adults of your height. Consider the benefits of achieving a healthy weight - a reduced risk of health conditions such as high blood pressure, diabetes and heart disease with improved lifestyle and self esteem.";
		lblMessage.style.color='#ce1127';
		//lblBuyNowMessage.innerHTML = "Content for Very Obese";
	}
    else if(si>40)
    {
        lblMessage.innerHTML=d + "Morbid Obesity. Your BMI indicating your weight is in the obese category for adults of your height. Consider the benefits of achieving a healthy weight - a reduced risk of health conditions such as high blood pressure, diabetes and heart disease with improved lifestyle and self esteem.";
        lblMessage.style.color='#ce1127';
        //lblBuyNowMessage.innerHTML = "Content for Morbid Obese";
    }
        //trBMI.style.display = "block";
        trBMI.style.visibility = "visible";
//        trMessage.style.display= "block";
        trMessage.style.visibility = "visible";
}

//Allow only Numbers (in amount format) having decimal in TextBox
function CheckAmount(objID,strControlName)
	{	
		if (objID.value!="")
		{
			var num=/[0-9.0-9]+/
			numflag=objID.value.match(num);	
			if(numflag !=objID.value){
			alert(strControlName+ ": Should be Numeric only.")
			objID.focus();
 			objID.select();
			return false;
		}
	}
	return true;
}

function conv(aa)//Height conversion
{
	var ft=0, inc=0, ht=0;
	if(aa==1 || aa==2)
	{
		ft = document.forms[0].opt2.value;
		inc = document.forms[0].opt3.value;
		var ss = ft*12;
		var tot = ss+parseInt(inc);
       	var val= tot*2.54;
		document.forms[0].ht.value = Math.round(val);
	}

	else{
		ht = document.forms[0].ht.value;
		if(ht!="")
		{
			var cm=Math.round(ht/2.54);
			var div=parseInt(cm/12);
			var md=cm%12;
			document.forms[0].opt2.value=div;
			document.forms[0].opt3.value=md;
		}
         }

}


function unit()  //Weight conversion
{
	var pp=document.forms[0].opt1.value;
	var ww = document.forms[0].wg.value;
	
	//Kilogram to pounds
	if(pp=="pounds")	
	{
		document.forms[0].wg.value = Math.round((ww*2.2)*100)/100;
	}

	//Pounds to kilograms
	else
	{
		document.forms[0].wg.value=Math.round(ww/2.2);
	}
}

