function rounding(number,decimal) 
{
	multi = Math.pow(10,decimal);
	number = Math.round(number * multi) / multi;
	return number;
}


var feet_units = new Array();
feet_units[0]= new Array(0, 0, '(Choose units)');
feet_units[1]= new Array(2.54, 12, 'Feet / Inches');
feet_units[2]= new Array(1, 100, 'Meter / Centimeter');

var pounds_units = new Array();
pounds_units[0]= new Array(0, '(Choose units)');
pounds_units[1]= new Array(2.2, 'Pounds');
pounds_units[2]= new Array(1, 'Kilos');

var sex = new Array();
sex[0]= new Array(0, 0, 0, 0, '(Please select)');
sex[1]= new Array(128, 50, 0, 0, 'Male');
sex[2]= new Array(148, 45.5, 0, 0, 'Female');

function calculate(form)
{
	/*alert(form.height.value);
	alert(form.height2.value);
	alert(form.height_unit.value);
	alert(form.weight.value);
	alert(form.weight2.value);
	alert(form.weight_unit.value);
	alert(form.years.value);
	alert(document.getElementById('sex').value + '***');*/

	if (document.getElementById('sex').value!=1 && document.getElementById('sex').value!=2) {alert("man or woman?"); return false;}
	
	if (form.height_unit.value!=1 && form.height_unit.value!=2) {alert("You must choose the height units!"); form.height_unit.focus(); return false;}
	
	if (form.weight_unit.value!=1 && form.weight_unit.value!=2 && form.weight_unit.value!=3) {alert("You must choose the weight units!"); form.weight_unit.focus(); return false;}

	//if ((isNaN(inches)) || (inches == null)  || (inches == "") || (inches < 0)) inches = 0;
	
	var Kilos = form.weight.value;

	if(form.weight_unit.value == 2)
	{
		Kilos = (eval(form.weight.value * 14) + eval(form.weight2.value))/2.2;
	}
	if(form.weight_unit.value == 3)
	{
		Kilos = form.weight.value/2.2
	}
	if (isNaN(Kilos) || Kilos < 10) { alert("Weights should be heavier than 10 kilograms (22 pounds)."); form.weight.select(); return false;}

	if (Kilos > 200) { alert("Weights should be lighter than 200 kilograms (441 pounds).");	form.weight.select(); return false; }

 	var Age = form.years.value;
 	var AgeCat = 25; 

 	if ((isNaN(Age)) || (Age == null)  || (Age == "")) { alert("Please enter a specific age"); return false; }
	else 
	{
		if ( Age < 1 ) { alert("Ages younger than 1 year old are too young for this calculation. Sorry."); form.years.select(); return false; } 
		if (Age > 120) { alert("All ages from 70 to 120 are treated as age 75."); form.years.select(); return false;	}
	}	

	if (Age > 18) {  AgeCat = 25 } 
	else { AgeCat = 26	}

 /* TotalInches  = eval(feet*feet_units[feet_conversion][1]) + eval(inches);
 Height       = TotalInches * feet_units[feet_conversion][0];
 HeightMeters = Height/100;
 HeightInches = Height/2.54;
 */
 	
 
	var HeightMeters = form.height.value/100;
	
 	if(form.height_unit.value == 2)
	{
		HeightMeters = (eval(12 * form.height.value) + eval(form.height2.value)) / 39.37;
	}
 	var HeightInches = HeightMeters*100/2.54;

 	if (isNaN(HeightMeters) || HeightMeters < 0.33) { alert("Heights should be taller than 33 centimeters (31.5 inches)."); form.height.select(); return false; }
	if (HeightMeters > 2.41) { alert("Heights should be shorter than 241 centimeters ( 7 feet, 11 inches)."); form.height.select(); return false; }

	// Calculate BSA
	var bsa = Math.sqrt((HeightMeters * 100 * Kilos) / 3600);	// Mosteller method.
	var bsa = rounding(bsa,2);

	// Calculate BMI
	var bmi = Kilos / Math.pow(HeightMeters,2);
 	var bmi = rounding(bmi,1);

	// SHOWING RESULTS
	document.getElementById('bmi').innerHTML = bmi;
	document.getElementById('bsa').innerHTML = bsa

	
	// Calculate Lean Body Weight and Ideal Body Weight
	
	var LeanConvert = 128;
	var IdealConvert = 50.5;
		
	if(document.getElementById('sex').value == 2)
	{
		LeanConvert = 148;
		IdealConvert = 45.5;
	}
	//LeanConvert = sex[sex_conversor][0];
	//IdealConvert = sex[sex_conversor][1];

	var LeanKg = 1.1 * Kilos - LeanConvert * (Math.pow(Kilos,2) / Math.pow((100 * HeightMeters),2));
	var IdealKg = IdealConvert + 2.3 * (HeightInches - 60);
	
	var lbw = Math.round(LeanKg) + " Kg";

	if(form.weight_unit.value == 3)
	{
		lbw = Math.round(LeanKg * 2.2) + " lbs";
	}
	if(form.weight_unit.value == 2)
	{
		lbw = Math.floor(LeanKg/6.35) + " st " + Math.round((LeanKg%6.35)*100/45) + " lbs";
	}
	document.getElementById('lbw').innerHTML = lbw;
	//form.lean.value = Math.round(LeanKg * pounds_units[pounds_conversor][0]) + " " + pounds_units[pounds_conversor][1];

	// 1 = weight in kg.
	
	var IdealMin = 1;
	var IdealMax = 1;
	if (document.getElementById('sex').value == 2) 
	{  // sex is female. (sex_conversor=2)
		if (Age > 18) 
		{
			IdealMin = Math.round((1.4106 * HeightInches) - 34.304);
			IdealMax = Math.round((1.4674 * HeightInches) - 31.837);;
		} 
		else 
		{  // age < 18
			Ideal = Math.round(0.031*HeightInches*HeightInches - 1.7307*HeightInches + 35.446);
			IdealMin = Ideal;
			IdealMax = Ideal;
		}
	} 
	if  (document.getElementById('sex').value == 1) 
	{										// sex is male.
		if (Age > 18) 
		{
			IdealMin = Math.round((1.3122 * HeightInches) - 22.955);
			IdealMax = Math.round((1.4774 * HeightInches) - 28.792);
		} 
		else 
		{ // age < 18
			Ideal = Math.round(0.0336*HeightInches*HeightInches - 1.9844*HeightInches + 40.93);
			IdealMin = Ideal;
			IdealMax = Ideal;

		}
	}

	//form.ideal.value = IdealMin*pounds_units[pounds_conversor][0] + " - " + IdealMax*pounds_units[pounds_conversor][0] + " " + pounds_units[pounds_conversor][1];
	var ibw = Math.round(IdealMin) + " Kg - " + Math.round(IdealMax) + "Kg";

	if(form.weight_unit.value == 3)
	{
		ibw = Math.round(IdealMin * 2.2) + " lbs - " + Math.round(IdealMax * 2.2) + " lbs";
	}
	if(form.weight_unit.value == 2)
	{
		ibw = Math.floor(IdealMin/6.35) + " st " + Math.round((IdealMin%6.35)*100/45) + " lbs - " + Math.floor(IdealMax/6.35) + " st " + Math.round((IdealMax%6.35)*100/45) + " lbs";
	}

	document.getElementById('ibw').innerHTML = ibw;
	// Set Body Description.
	var Interpret = "in Normal Range";
	var OldAdj = 0;
	var WeightKids = Kilos / Math.pow(HeightMeters,2.88);

	if (Age > 64.9) { OldAdj = 3;}
	else 
		if (Age > 54.9) { OldAdj = 2; } 
		else 
			if (Age > 44.9) { OldAdj = 1; }
			else OldAdj = 1;

	
// sex is female. (sex_conversor=2)

	if (Age < 20) {	// Use W/H^2.88 instead of bmi for children.  
		var WeightKids = Kilos / Math.pow(HeightMeters,2.88);
		if (document.getElementById('sex').value == 2) {	// sex is female.
			if (Age < 12) {
				if (WeightKids > 15.82) {		// over 95th percentile. 97%ile = 16.33
					Interpret = "Obese";
				} else {
					if (WeightKids > 14.17) {		// over 85th percentile. 90%ile = 14.55
						Interpret = "Overweight";				// 75%ile = 13.4
					} else {
						if (WeightKids < 10.23) {		// under 5th percentile.  97%ile = 10.08
							Interpret = "Underweight";
						} else {
							if (WeightKids < 10.85) {		// under 15th percentile. 90%ile = 10.61
								Interpret = "Thin";    // under 75%ile = 11.34
							}
						}
					}
				}
			} else {	// post-pubertal girls.
				if (WeightKids > 15.80) {		// over 95th percentile. 97%ile = 15.89
					Interpret = "Obese";
				} else {
					if (WeightKids > 15.21) {		// over 85th percentile.  90%ile = 15.58
						Interpret = "Overweight";		// 75%ile = 14.46
					} else {
						if (WeightKids < 11.23) {		// under 5th percentile.  97%ile = 11.06
							Interpret = "Underweight";
						} else {
							if (WeightKids < 12.01) {		// under 15th percentile.  90%ile = 11.64
								Interpret = "Thin";		// 25%ile = 12.74
							}
						}
					}
				}
			} 
		} else {									// sex is male.
			if (WeightKids > 15.77) {		// over 95th percentile.  97%ile = 16.16
				Interpret = "Obese";
			} else {
				if (WeightKids > 14.45) {		// over 85th percentile.  90%ile = 14.81
					Interpret = "Overweight";		// 75%ile = 13.73
				} else {
					if (WeightKids < 10.65) {		// under 5th percentile.  97%ile = 10.46
						Interpret = "Underweight";
					} else {
						if (WeightKids < 11.36) {		// under 15th percentile.  90%ile = 11.14
							Interpret = "Thin";		// 25%ile = 11.81
						}
					}
				}
			}
		}
	} else {		// Use bmi for adults over 19 year old.
	if (document.getElementById('sex').value == 2)  {  // sex is female.
		if (bmi < (19.1+OldAdj)) {
			Interpret = "Underweight";
		} else {
			if (bmi < (25.8+OldAdj)) {
				Interpret = "in Normal Range";
			} else {
				if (bmi < (27.3+OldAdj)) {
					Interpret = "Marginally Overweight";
				} else {
					if (bmi < (32.2+OldAdj)) {
						Interpret = "Overweight";
					} else {
						Interpret = "Obese";
					}
				}
			}
		}
	} else {										// sex is male.
		if (bmi < (20.7+OldAdj)) {
			Interpret = "Underweight";
		} else {
			if (bmi < (26.4+OldAdj)) {
				Interpret = "in Normal Range";
			} else {
				if (bmi < (27.8+OldAdj)) {
					Interpret = "Marginally Overweight";
				} else {
					if (bmi < (31.1+OldAdj)) {
						Interpret = "Overweight";
					} else {
						Interpret = "Obese";
					}
				}
			}
		}
 	}
	if (bmi < 10 || bmi > 50) Interpret = "Check your numbers";
	}

	//form.Interpret.value = Interpret;
	//document.getElementById('interpret').innerHTML = Interpret;

	return false;
}

function weight_units_changed(a)
{
	if(a.value==1)
	{
		document.getElementById('weight').innerHTML = '<input class="small" name="weight" type="text" value="" autocomplete="off"> Kgs';
	}
	if(a.value==2)
	{
		document.getElementById('weight').innerHTML = '<input class="small" name="weight" type="text" value="" autocomplete="off"> stones &nbsp; <input class="small" name="weight2" type="text" value="" autocomplete="off"> lbs'
	}
	if(a.value==3)
	{
		document.getElementById('weight').innerHTML = '<input class="small" name="weight" type="text" value="" autocomplete="off"> lbs';
	}
}

function height_units_changed(a)
{
	if(a.value==1)
	{
		document.getElementById('height').innerHTML = '<input class="small" name="height" type="text" value="" autocomplete="off"> cm';
	}
	if(a.value==2)
	{
		document.getElementById('height').innerHTML = '<input class="small" name="height" type="text" value="" autocomplete="off"> feet &nbsp;<input class="small" name="height2" type="text" value="" autocomplete="off"> inches';
	}
}
