var delay = "2";  //############# How long before window appears (seconds)
var winw = "400"; //############# How wide should your window be (pixels)
var winh = "300"; //############# How tall should the window be (pixels)
var repeat = "1"; //Do you want visitor to be able to re-open window after closing?


//############# published at: scripts.tropicalpcsolutions.com


var ie=(document.all);
var ns=(document.layers);
var ns6=(document.getElementById && !ie);
var calculate=ns? "" : "px"
function loadElement(){
if(!ns && !ie && !ns6) 
{
return;
}
if(ie) 
{
popup=eval('document.all.elementDiv.style');
}
else if(ns) 
{
popup=eval('document.layers["elementDiv"]');
}
else if(ns6) 
{
popup=eval('document.getElementById("elementDiv").style');
}
if (ie||ns6) 
{
popup.visibility="visible";
}
else 
{
popup.visibility ="show";
}
displayElement();
}
function displayElement(){
var agent=navigator.userAgent.toLowerCase();
if (ie){
	documentWidth = (centerElement().offsetWidth)/2+centerElement().scrollLeft-(winw/2);
	documentHeight = (centerElement().offsetHeight)/2+centerElement().scrollTop-(winh/2);
}
else if (ns){
	documentWidth=window.innerWidth/2+window.pageXOffset-(winw/2);
	documentHeight=window.innerHeight/2+window.pageYOffset-(winh/2);
}
else if (ns6){
	documentWidth=self.innerWidth/2+window.pageXOffset-(winw/2);
	documentHeight=self.innerHeight/2+window.pageYOffset-(winh/2);
}
popup.left = documentWidth+calculate;
popup.top = documentHeight+calculate;
setTimeout("displayElement()",100);
}
function centerElement(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function closeElement(){
	if (ie||ns6) { popup.display="none"; } 
	else { popup.visibility ="hide"; }
        
		if (repeat == 1) { 
		location.replace; 
		}
}
function close_divs()
	{
	if(ie) popup=eval('document.all.elementDiv.style');
else if(ns) popup=eval('document.layers["elementDiv"]');
else if(ns6) popup=eval('document.getElementById("elementDiv").style');

if (ie||ns6) popup.visibility="hidden";
else popup.visibility ="hide";
	
	}

function rounding(number,decimal) {
	multi = Math.pow(10,decimal);
	number = Math.round(number * multi) / multi;
	return number;
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}
function calBMI()
{

var frm = document.frmBMICal;

if(frm.bmi_height_ft.value=="")
{
alert("Please enter your height in feet.");
frm.bmi_height_ft.focus();
return false;
}
if(isNaN(frm.bmi_height_ft.value))
{
alert("Please enter numeric value in your height in feet .");
frm.bmi_height_ft.focus();
return false;
}

if(frm.bmi_height_in.value=="")
{
alert("Please enter your height in inches.");
frm.bmi_height_in.focus();
return false;
}

if(isNaN(frm.bmi_height_in.value))
{
alert("Please enter  numeric value in your height in inches.");
frm.bmi_height_in.focus();
return false;
}

if(frm.bmi_c_weight.value=="")
{
alert("Please enter your current weight.");
frm.bmi_c_weight.focus();
return false;
}

if(isNaN(frm.bmi_c_weight.value))
{
alert("Please enter numeric value in your current weight.");
frm.bmi_c_weight.focus();
return false;
}
/*
if(frm.bmi_g_weight.value=="")
{
alert("Please enter your goal weight.");
frm.bmi_g_weight.focus();
return false;
}

if(isNaN(frm.bmi_g_weight.value))
{
alert("Please enter numeric value in your goal weight.");
frm.bmi_g_weight.focus();
return false;
}
*/

/*
English BMI Formula 
BMI = ( Weight in Pounds / ( Height in inches ) x ( Height in inches ) ) x 703
 
Metric BMI Formula 
BMI = ( Weight in Kilograms / ( Height in Meters ) x ( Height in Meters ) ) 
*/

var bmi_c_val=0;
if (frm.bmi_wu.selectedIndex == 0) {	// 0 = weight in lbs
var total_inc=( Number(frm.bmi_height_in.value) + Number(frm.bmi_height_ft.value*12) );
bmi_c_val=  (Number( frm.bmi_c_weight.value) * 703) / (total_inc * total_inc) ;
}
else
{
var total_inc=( Number(frm.bmi_height_in.value) + Number(frm.bmi_height_ft.value*12) );
var heightMeters = total_inc * 2.54 / 100;
bmi_c_val=  Number( frm.bmi_c_weight.value) / (heightMeters * heightMeters) ;
}
//alert("You have a current Weight BMI of "+bmi_c_val.toFixed(2));
loadElement();

document.getElementById('result_txt').innerHTML='<br/><center><b><font color="#000000">Your BMI (Body Mass Index) is  '+bmi_c_val.toFixed(2)+'</b></font><br/><br/><table border="0" cellpadding="0" width="98%"><tr><td><table width="100%" height="0" border="0" cellpadding="4" cellspacing="1" bgcolor="#FDDD46"><tr><td colspan="2" bgcolor="#F9FAC3"><div align="center" class="postmetadata"><strong>Body Mass Index Table</strong></div></td></tr><tr><td bgcolor="#FFFFFF"><strong>Underweight</strong></td><td bgcolor="#FFFFFF">Below 18.5</td></tr><tr><td bgcolor="#FFFFFF"><strong>Normal</strong><strong></strong></td><td bgcolor="#FFFFFF">18.5 - 24.9 </td></tr><tr><td bgcolor="#FFFFFF"><strong>Overweight</strong></td><td bgcolor="#FFFFFF">25.0 - 29.9</td></tr><tr><td bgcolor="#FFFFFF"><strong>Obesity</strong></td><td bgcolor="#FFFFFF">30.0 and Above</td></tr></table></td></tr></table></center><br/>';
return false;
}


function calBMR()
{
var frm = document.frmBMRCal;

if(frm.bmr_height_ft.value=="")
{
alert("Please enter your height in feet.");
frm.bmr_height_ft.focus();
return false;
}
if(isNaN(frm.bmr_height_ft.value))
{
alert("Please enter numeric value in your height in feet .");
frm.bmr_height_ft.focus();
return false;
}

if(frm.bmr_height_in.value=="")
{
alert("Please enter your height in inches.");
frm.bmr_height_in.focus();
return false;
}
if(isNaN(frm.bmr_height_in.value))
{
alert("Please enter  numeric value in your height in inches.");
frm.bmr_height_in.focus();
return false;
}

if(frm.bmr_weight.value=="")
{
alert("Please enter your weight.");
frm.bmr_weight.focus();
return false;
}

if(isNaN(frm.bmr_weight.value))
{
alert("Please enter numeric value in your weight.");
frm.bmr_weight.focus();
return false;
}

if(frm.bmr_age.value=="")
{
alert("Please enter your age.");
frm.bmr_age.focus();
return false;
}

if(isNaN(frm.bmr_age.value))
{
alert("Please enter numeric value in your age.");
frm.bmr_age.focus();
return false;
}

/*
English BMR Formula 
Women: BMR = 655 + ( 4.35 x weight in pounds ) + ( 4.7 x height in inches ) - ( 4.7 x age in years )
Men: BMR = 66 + ( 6.23 x weight in pounds ) + ( 12.7 x height in inches ) - ( 6.8 x age in year ) 
  
Metric BMR Formula 
Women: BMR = 655 + ( 9.6 x weight in kilos ) + ( 1.8 x height in cm ) - ( 4.7 x age in years )
Men: BMR = 66 + ( 13.7 x weight in kilos ) + ( 5 x height in cm ) - ( 6.8 x age in years ) 
*/

var bmr_val=0;
var total_inc=( Number(frm.bmr_height_in.value) + Number(frm.bmr_height_ft.value*12) );
if (frm.bmr_wu.selectedIndex == 0) {	// 0 = weight in lbs
if(valButton(frm.bmr_gender)=="male")
{
bmr_val=66 + ( 6.23 * Number(frm.bmr_weight.value) ) + ( 12.7 * (total_inc)) - ( 6.8 * Number(frm.bmr_age.value) ) ;
}
else
{
bmr_val=655 + ( 4.35 * Number(frm.bmr_weight.value) ) + ( 4.7 * (total_inc)) - ( 4.7 * Number(frm.bmr_age.value) ) ;
}
}
else
{
var heightCm = total_inc * 2.54;
if(valButton(frm.bmr_gender)=="male")
{
bmr_val=66 + ( 13.7 * Number(frm.bmr_weight.value) ) + ( 5 * (heightCm)) - ( 6.8 * Number(frm.bmr_age.value) ) ;
}
else
{
bmr_val=655 + ( 9.6 * Number(frm.bmr_weight.value) ) + ( 1.8 * (heightCm)) - ( 4.7 * Number(frm.bmr_age.value) ) ;
}
}

//alert("You have a BMR of "+bmr_val.toFixed(2));
loadElement();
document.getElementById('result_txt').innerHTML='<br/><center><b><font color="#000000">You have a BMR of  '+bmr_val.toFixed(2)+'</b></font><br/></center><br/>';
return false;
}

function calBW()
{
var frm = document.frmBWCal;

if(frm.bw_height_ft.value=="")
{
alert("Please enter your height in feet.");
frm.bw_height_ft.focus();
return false;
}
if(isNaN(frm.bw_height_ft.value))
{
alert("Please enter numeric value in your height in feet .");
frm.bw_height_ft.focus();
return false;
}

if(frm.bw_height_in.value=="")
{
alert("Please enter your height in inches.");
frm.bw_height_in.focus();
return false;
}
if(isNaN(frm.bw_height_in.value))
{
alert("Please enter  numeric value in your height in inches.");
frm.bw_height_in.focus();
return false;
}

if(frm.bw_weight.value=="")
{
alert("Please enter your weight.");
frm.bw_weight.focus();
return false;
}

if(isNaN(frm.bw_weight.value))
{
alert("Please enter numeric value in your weight.");
frm.bw_weight.focus();
return false;
}

if(frm.bw_age.value=="")
{
alert("Please enter your age.");
frm.bw_age.focus();
return false;
}

if(isNaN(frm.bw_age.value))
{
alert("Please enter numeric value in your age.");
frm.bw_age.focus();
return false;
}

var bw_val=0;
var heightInches=(Number(frm.bw_height_in.value) + Number(frm.bw_height_ft.value*12));
var heightMeters = heightInches * 2.54 / 100;
var total_kg_weight=(Number(frm.bw_weight.value)/2.20462262);

var weight =  Number(frm.bw_weight.value);

if (frm.bw_wu.selectedIndex == 0) {	// 0 = weight in lbs
		kg = weight * 0.45359237;
	} else { // 1 = weight in kg.
		kg = weight;
	}

// Calculate BMI
var bmi=0;
bmi = kg / Math.pow(heightMeters,2);
//bmi = rounding(bmi,1);

Age=Number(frm.bw_age.value);

var PeopBMI;
	if(valButton(frm.bw_gender)=="male")
	{
		PeopBMI = 0.5*bmi + 11.5;
	} else {
		PeopBMI = 0.4*bmi + 0.03*Age + 11;
	}

    //alert("Height Meters :"+heightMeters);
	//alert("Height Inches :"+heightInches);
	//alert("BMI :"+bmi);
	//alert("P BMI :"+PeopBMI);
	//alert("Age :"+Age);
	
if (Age > 18.5 || heightInches > 69) {	// Use Peoples Choice for Adults
		if (frm.bw_wu.selectedIndex == 0) {  // 0 = weight in lbs
			bw_val = Math.round( (PeopBMI * (heightMeters*heightMeters)) * 2.2046 ) + " lbs";
		} else {  // 1 = weight in kg.
			bw_val = Math.round( (PeopBMI * (heightMeters*heightMeters)) ) + " kgs";
		}
	} else {	// Age < 18,  use 50th percentile.
		if(valButton(frm.bw_gender)=="male") {  // Male
			if (frm.bw_wu.selectedIndex == 0) {  // Weight in lbs
				bw_val = Math.round(0.0982*heightInches*heightInches - 6.6836*heightInches + 144.6) + " lbs";
			} else { 
				bw_val = Math.round(0.0445*heightInches*heightInches - 3.0317*heightInches + 65.591) + " kgs";
			}
		} else {		// Female
			if ( Age < 12 || heightInches < 63) {
				if ( frm.bw_wu.selectedIndex == 0) {  // Weight in lbs  
					bw_val = Math.round(0.0697*heightInches*heightInches - 3.9646*heightInches + 81.378) + " lbs";
				} else {  // y = 0.0316x2 - 1.7983x + 36.913
					bw_val = Math.round(0.0316*heightInches*heightInches - 1.7983*heightInches + 36.913) + " kgs";
				}
			} else {
				if ( frm.bw_wu.selectedIndex == 0) {  // Weight in lbs  y = 0.0167x2 + 2.8845x - 135.65
					bw_val = Math.round(0.0167*heightInches*heightInches + 2.8845*heightInches - 135.65) + " lbs";
				} else { // y = -0.0237x2 + 5.0882x - 174.91
					bw_val = Math.round(-0.0237*heightInches*heightInches + 5.0882*heightInches - 174.91) + " kgs";
				}
			}
		} 
	}
//alert("Ideal Body Weight : "+bw_val);
loadElement();
document.getElementById('result_txt').innerHTML='<br/><center><b><font color="#000000">Ideal Body Weight : '+bw_val+'</b></font><br/></center><br/>';
return false;
}

function bmr_poundsAndKilos(form) {
	var weight = Number(form.bmr_weight.value);
	if ( weight > 0) {
		if (form.bmr_wu.selectedIndex == 0) {	// 0 = pounds.
			form.bmr_weight.value = rounding( weight / 0.45359237,0);
		} else {								// 1 = kilograms.
			if (weight > 219) {
				form.bmr_weight.value = rounding( weight * 0.45359237,0);
			} else {
				form.bmr_weight.value = rounding( weight * 0.45359237,1);
			}
		}
		form.bmr_weight.select()
		form.bmr_weight.focus()
	}
	return true;
}

function bw_poundsAndKilos(form) {
	var weight = Number(form.bw_weight.value);
	if ( weight > 0) {
		if (form.bw_wu.selectedIndex == 0) {	// 0 = pounds.
			form.bw_weight.value = rounding( weight / 0.45359237,0);
		} else {								// 1 = kilograms.
			if (weight > 219) {
				form.bw_weight.value = rounding( weight * 0.45359237,0);
			} else {
				form.bw_weight.value = rounding( weight * 0.45359237,1);
			}
		}
		form.bw_weight.select()
		form.bw_weight.focus()
	}
	return true;
}

function bmi_poundsAndKilos(form) {
	var weight = Number(form.bmi_c_weight.value);
	if ( weight > 0) {
		if (form.bmi_wu.selectedIndex == 0) {	// 0 = pounds.
			form.bmi_c_weight.value = rounding( weight / 0.45359237,0);
		} else {								// 1 = kilograms.
			if (weight > 219) {
				form.bmi_c_weight.value = rounding( weight * 0.45359237,0);
			} else {
				form.bmi_c_weight.value = rounding( weight * 0.45359237,1);
			}
		}
		form.bmi_c_weight.select()
		form.bmi_c_weight.focus()
	}
	return true;
}
