
var W=window, D=document, F, E, EvSrc, Gec, Op, Ns, Saf, IE7, Nav, B;

var Gender, HeightInInches, Weight, BMI, Category, LowerIdealBMI, UpperIdealBMI, 
	CurrHorTab, CurrMetr='imperial', aHorTabs, aMetricTbl,
	
	LbInKg=2.20462262,	InchInCm=0.39370,	KmInMile=1.609344,
	LbsInStone=14,			InchesInFoot=12,		BMICoef=703,
	FeetInYard=3,

	aAdWeightTypes=['under', 'normal', 'over', 'obese'],
	aChWeightTypes=['under', 'normal', 'atrisk', 'over'],
	
	bHaveResults=[];

//-----

function getbyid(id){return D.getElementById(id);}
function SetText(o, Txt){o.firstChild.data=Txt;} 

function EventInit(e){
E=(!Gec ? event : e); EvSrc=E[Gec ? 'target' : 'srcElement'];
if (Ns && EvSrc.nodeType==3) EvSrc=EvSrc.parentNode;
} 

function ReadNum(Fld, bInt){
var Val;

Val=(!bInt ? parseFloat(Fld.value) : parseInt(Fld.value));
if (isNaN(Val)) return 0;
return Val;
}

function RoundToTenth(input){
return Math.round(input*10.0)/10.0;
}

function InitUA(){

Nav=W.navigator.userAgent; IE7=false; 
Gec=(Nav.indexOf('Gecko')!=-1); Op=(Nav.indexOf('Opera')!=-1);
Ns=(Gec && Nav.indexOf('Netscape')!=-1); Saf=(Gec && Nav.indexOf('Safari/')!=-1);

if (!Gec && !Op){
	Nav.match(/ MSIE (\d+)/);
	if (RegExp.$1>=7) IE7=true;
	}
}

function RadioGet(name, F){
var rb, opt, i;

rb=F[name];
for(i=0; i<rb.length; i++){
	opt=rb[i];
	if (!opt.disabled){
		if (opt.checked) return opt.value;
		}
	}
return null;
}

//----

function ShowBMI(){

if (CurrHorTab=='adult') CalcBMIAdult();
else{
	var Data;
	if (!(Data=ChildCheckSimple())) return;
	CalcBMIChild(Data);
	}
}

function ChildCheckSimple(){
var Msg, Gender, AgeY, AgeM, bErr;

if (!(Gender=RadioGet('ch_gender', F))) Msg='gender';
else if (!(AgeY=F.ch_age_y.value)) Msg='age';

if (Msg){
	alert('Please specify your '+Msg); return false;
	}

if (!(AgeM=F.ch_age_m.value)) AgeM=0;
return [Gender, +AgeY, +AgeM];
}

function ReadWeightHeight(Type, bChild){
var	wst, Weight, Height, StoneFld, bErr, bNoHeight;

StoneFld=F[Type+'_wst']; bHeight=(F[Type+'_hcm']!=null);

//Read weight/height from whatever fields as imperials
if (CurrMetr=='metric'){
	Weight=ReadNum(F[Type+'_wkg'])*LbInKg;
	if (bHeight) Height=ReadNum(F[Type+'_hcm'])*InchInCm;
	}
else{
	//Weight in stones
	if (StoneFld) wst=ReadNum(StoneFld); else wst=0;
	//Total weight in lbs
	Weight=wst*LbsInStone+ReadNum(F[Type+'_wlbs']);
	//Height in feet / inches / total
	if (bHeight) Height=ReadNum(F[Type+'_htf'])*InchesInFoot+ReadNum(F[Type+'_hti']);
	}

if (!bChild){
	//Range check for adults, in lbs/inches
	if (Weight<=65 || Weight>=500 || (bHeight && (Height<=48 || Height>=120))) bErr=true;
	}
//Range check for ch
else if (Weight<=7 || Weight>=330 || (bHeight && (Height<=12 || Height>=98))) bErr=true;

if (bErr){
	alert ("Invalid weight or height. Please check and re-enter!");
	return false;
	}

//Fill metric tab if not
if (CurrMetr!='metric'){
	F[Type+'_wkg'].value=RoundToTenth(Weight/LbInKg);
	if (bHeight) F[Type+'_hcm'].value=RoundToTenth((Height/InchInCm)+0.005);
	}

//On the imperial tab: always refill to normalize
if (StoneFld){
	//Stones
	wst=Math.floor(Weight/LbsInStone); StoneFld.value=(wst ? wst : '');
	F[Type+'_wlbs'].value=RoundToTenth(Weight % LbsInStone);
	}
else F[Type+'_wlbs'].value=RoundToTenth(Weight);

//Height
if (bHeight){
	F[Type+'_htf'].value=Math.floor(Height/InchesInFoot);
	F[Type+'_hti'].value=RoundToTenth(Height % InchesInFoot);
	}

return [Weight, Height];
}

function CalcBMIChild(Data){
var Gender, AgeY, AgeM, oDiv, oP, aSpans;

Gender=Data[0]; AgeY=Data[1]; AgeM=Data[2];

if (!(WH=ReadWeightHeight('ch', true))) return false;
Weight=WH[0]; Height=WH[1]; 
BMI=CalcBMI(Weight, Height);

Data=GetChildWeightType(BMI, Gender, AgeY*12+AgeM); 
//Set the ideal range

oDiv=getbyid('ns_ch_'+Data[0]); 
oP=oDiv.getElementsByTagName('P')[0]; 

aSpans=oP.getElementsByTagName('SPAN');
SetText(aSpans[0], RoundToTenth(Data[1])); SetText(aSpans[1], RoundToTenth(Data[2]));

ShowBMIResults('child', Data[0], RoundToTenth(BMI));
}

function ShowBMIResults(Type, WeightType, BMI){
var OtherType=GetOtherTab(Type), NextWeightType, i, DivPref, Pref, aWeightTypes;

Pref=Type.substr(0, 2)+'_'; DivPref='ns_'+Pref;

if (Type=='adult') aWeightTypes=aAdWeightTypes;
else aWeightTypes=aChWeightTypes;

//Hide the other results div
getbyid(OtherType+'_result').style.display='none';

for(i=0; i<4; i++){
	NextWeightType=aWeightTypes[i];
	getbyid(DivPref+NextWeightType).style.display=(NextWeightType==WeightType ? 'block' : 'none');
	getbyid(Pref+NextWeightType).style.backgroundColor=(NextWeightType==WeightType ? '#31529C' : '#5273B5');
	}

getbyid(Type+'_bmi').innerHTML=BMI;
//Show the results div
getbyid(Type+'_result').style.display='block';

bHaveResults[Type]=true;
}

function CalcBMIAdult(){
var WeightType, NextWeightType, i, BMI, WH, Weight, Height;

if (!(WH=ReadWeightHeight('ad', false))) return false;
Weight=WH[0]; Height=WH[1]; 
BMI=CalcBMI(Weight, Height);

if (BMI<18.5) WeightType='under';
else if (BMI>=18.5 && BMI<25) WeightType='normal';
else if (BMI>=25 && BMI<30) WeightType='over';
else if (BMI>=30) WeightType='obese';

ShowBMIResults('adult', WeightType, RoundToTenth(BMI));

return true;
}

function CalcBMI(Weight, Height){
return Weight*BMICoef/(Height*Height);
}

//----

function GetChildWeightType(BMI, Gender, AgeM){
var a5P, a85P, a95P, i, Type;
	
if (Gender=="m"){
	//male percentile arrays
	a5P=Array(
		14.71929257, 14.68360841, 14.64843329, 14.61378626, 14.57968578, 14.54614966, 14.51319492, 14.48083795,
		14.44909329, 14.41797561, 14.38749811, 14.35767293, 14.32851119, 14.30002256, 14.27221761, 14.24510202,
		14.21868433, 14.19297011, 14.16796472, 14.14367089, 14.12009269, 14.09723246, 14.07508966, 14.05366443,
		14.03295533, 14.01295984, 13.99367445, 13.97509464, 13.95721572, 13.94002947, 13.92353116, 13.90771281,
		13.89256678, 13.87808525, 13.86425959, 13.85108308, 13.83854804, 13.82664746, 13.81537489, 13.8047237,
		13.79469004, 13.78526882, 13.77645627, 13.76824936, 13.76064574, 13.75364373, 13.7472423, 13.74144099,
		13.73623987, 13.73163946, 13.72764072, 13.72424433, 13.72145289, 13.71926775, 13.71769094, 13.71672458,
		13.71637124, 13.71663233, 13.71751078, 13.71900887, 13.72112886, 13.72387293, 13.72724315, 13.7312415,
		13.73586983, 13.7411299, 13.74702309, 13.753551, 13.7607148, 13.76851558, 13.77695423, 13.78603145,
		13.79574779, 13.80610355, 13.81709878, 13.82873375, 13.84100784, 13.85392068, 13.86747174, 13.88166013,
		13.89648454, 13.91194439, 13.92803786, 13.94476358, 13.9621189, 13.98010297, 13.99871328, 14.01794738,
		14.03780282, 14.0582769, 14.07936677, 14.10106937, 14.1233815, 14.14629977, 14.16982063, 14.19394037,
		14.21865508, 14.24396081, 14.26985313, 14.29632789, 14.32338048, 14.35100623, 14.37920031, 14.40795771,
		14.43727329, 14.46714175, 14.49755765, 14.52851537, 14.56000917, 14.59203313, 14.62458122, 14.65764722,
		14.69122381, 14.72530601, 14.75988661, 14.79495883, 14.83051578, 14.86655039, 14.90305549, 14.94002374,
		14.97744768, 15.01531973, 15.05363215, 15.09237707, 15.13154651, 15.17113234, 15.2111263, 15.25152001,
		15.29230496, 15.33347251, 15.37501389, 15.41692023, 15.4591825, 15.50179159, 15.54473823, 15.58801305,
		15.63160658, 15.67550919, 15.71971118, 15.7642027, 15.80897382, 15.85401448, 15.89931451, 15.94486363,
		15.99065148, 16.03666757, 16.0829013, 16.129342, 16.17597888, 16.22280106, 16.26979755, 16.31695728,
		16.3642691, 16.41172173, 16.45930384, 16.50700399, 16.55481067, 16.60271228, 16.65069714, 16.69875348,
		16.74686947, 16.79503318, 16.84323264, 16.89145577, 16.93969043, 16.98792443, 17.03614547, 17.08434122,
		17.13249925, 17.18060709, 17.22865219, 17.27662259, 17.32450423, 17.37228509, 17.41995236, 17.46749315,
		17.51489451, 17.56214342, 17.60922679, 17.65613146, 17.70284417, 17.74935161, 17.79564039, 17.84169701,
		17.8875079, 17.9330594, 17.97833773, 18.02332903, 18.06801933, 18.11239453, 18.15644042, 18.20014256,
		18.24348662, 18.28645727, 18.32904183, 18.37122369, 18.41298696, 18.45431794, 18.49520062, 18.53561932,
		18.57555819, 18.61500111, 18.65393178, 18.69233437, 18.73019025, 18.7674838, 18.80419732, 18.84031318,
		18.87581348, 18.91067999, 18.9448942, 18.97843726, 19.0112887, 19.04343134, 19.07484425, 19.10550722,
		19.12055111
		); 

	a85P=Array(
		18.11954923, 18.03668013, 17.95700228, 17.88047101, 17.80704259, 17.73667414, 17.66932346, 17.6049486,
		17.54350898, 17.48496295, 17.42926933, 17.37638677, 17.32627356, 17.27888795, 17.23418554, 17.19212545,
		17.15266201, 17.11575098, 17.08134677, 17.04940504, 17.0198786, 16.99272087, 16.96788739, 16.94533215,
		16.92501028, 16.90687779, 16.89089179, 16.87701073, 16.86519374, 16.85540483, 16.8476049, 16.84175987,
		16.8378366, 16.83580365, 16.83563223, 16.83729272, 16.84075838, 16.84600319, 16.85300198, 16.86173124,
		16.87216519, 16.88428085, 16.89805459, 16.91346275, 16.93048153, 16.94908693, 16.96925461, 16.99095993,
		17.01417786, 17.03888299, 17.06504953, 17.09265223, 17.12166319, 17.15205629, 17.18380454, 17.21688072,
		17.25125697, 17.28690708, 17.32380254, 17.3619157, 17.40121891, 17.44168456, 17.48328513, 17.52599317,
		17.56978138, 17.61462266, 17.66048985, 17.70735632, 17.75519541, 17.80398077, 17.85368626, 17.90428602,
		17.95575444, 18.00806622, 18.06119653, 18.1151201, 18.16981303, 18.22525119, 18.28141055, 18.33826774,
		18.39579999, 18.45398355, 18.51279647, 18.57221621, 18.63222223, 18.69279128, 18.75390266, 18.81553565,
		18.87766958, 18.94028419, 19.00335954, 19.06687603, 19.13081436, 19.19515554, 19.25988094, 19.3249722,
		19.3904113, 19.45618042, 19.52226247, 19.58864006, 19.65529649, 19.72221531, 19.78938034, 19.85677572,
		19.92438591, 19.99219565, 20.06019002, 20.12835437, 20.19667437, 20.265136, 20.33372554, 20.40242956,
		20.4712365, 20.54013114, 20.60910189, 20.6781365, 20.74722303, 20.81634983, 20.88550554, 20.95467909,
		21.02385969, 21.09303687, 21.1622004, 21.23134038, 21.30044717, 21.36951141, 21.43852405, 21.5074763,
		21.57635964, 21.64516586, 21.71388701, 21.78251541, 21.85104367, 21.91946467, 21.98777156, 22.05595776,
		22.12401698, 22.19194317, 22.25973057, 22.32737368, 22.39486727, 22.46220636, 22.52938625, 22.5964025,
		22.66325092, 22.7299276, 22.79642886, 22.86275131, 22.92889179, 22.99484741, 23.06061552, 23.12619375,
		23.19157996, 23.25677226, 23.32176902, 23.38656886, 23.45117065, 23.5155735, 23.57977678, 23.6437801,
		23.70758332, 23.77118654, 23.83459013, 23.89779468, 23.96080104, 24.02361032, 24.08622386, 24.14864327,
		24.21087038, 24.2729073, 24.33475639, 24.39641931, 24.4579009, 24.51920324, 24.58032971, 24.64128395,
		24.70206989, 24.76269168, 24.82315378, 24.88346091, 24.94361805, 25.00363048, 25.06350375, 25.1232437,
		25.18285645, 25.24234842, 25.30172632, 25.36099716, 25.42016826, 25.47924723, 25.538242, 25.59716095, 
		25.65601244, 25.71480629, 25.77354888, 25.83225207, 25.89092687, 25.94958116, 26.00822631, 26.06687321, 
		26.1255331, 26.18421759, 26.24293861, 26.30170723, 26.36053932, 26.41944523, 26.47843912, 26.53753478, 
		26.59674639, 26.65608851, 26.71557609, 26.77522448, 26.83505132, 26.89506944, 26.95529697, 27.01575092, 
		27.04606818
		);

	a95P=Array(
		19.27889813, 19.16465965, 19.05567423, 18.9518675, 18.85316529, 18.75949359, 18.67077841, 18.58694589,
		18.50792131, 18.43363072, 18.3639996, 18.29895323, 18.23841666, 18.1823144, 18.13057244, 18.08311311,
		18.03986198, 18.00074262, 17.96567903, 17.9345935, 17.90741007, 17.88405183, 17.86443983, 17.84849622,
		17.8361423, 17.82729891, 17.8218865, 17.81982523, 17.82103559, 17.82543578, 17.83294744, 17.84349028,
		17.85698505, 17.87335318, 17.89251642, 17.91439894, 17.93892524, 17.96602144, 17.99561523, 18.02763543,
		18.06201396, 18.09868317, 18.13757754, 18.17863327, 18.22178824, 18.26698191, 18.31415529, 18.36325085,
		18.41421242, 18.46698511, 18.52151524, 18.57774996, 18.63563816, 18.69512904, 18.75617288, 18.81872078,
		18.88272474, 18.948137, 19.01491107, 19.08300081, 19.15236073, 19.22294589, 19.29471191, 19.36761493,
		19.44161165, 19.51665894, 19.59271539, 19.6697383, 19.74768666, 19.82651963, 19.90619689, 19.98667859,
		20.06792538, 20.1498984, 20.23255928, 20.31587026, 20.39979379, 20.48429323, 20.56933216, 20.6548748,
		20.74088584, 20.82733059, 20.9141748, 21.00138483, 21.08892746, 21.17677023, 21.26488107, 21.3532285,
		21.44178165, 21.53051015, 21.61938426, 21.70837477, 21.79745308, 21.88659115, 21.97576153, 22.06493737,
		22.15409238, 22.24320091, 22.33223786, 22.42117875, 22.5099997, 22.59867744, 22.68718928, 22.77551316,
		22.86362762, 22.95151182, 23.03914551, 23.12650908, 23.21358351, 23.30035043, 23.38679204, 23.4728912,
		23.55863129, 23.64399652, 23.72897155, 23.81354171, 23.89769296, 23.98141187, 24.06468566, 24.14750216,
		24.22984982, 24.31171774, 24.39309562, 24.47397382, 24.55434332, 24.63419572, 24.71352325, 24.79231881,
		24.87057587, 24.9482886, 25.02545175, 25.10206075, 25.17811163, 25.25360108, 25.32852641, 25.4028856,
		25.47667722, 25.54990054, 25.62255542, 25.6946424, 25.76616263, 25.83711794, 25.90751079, 25.97734428,
		26.04662217, 26.11534887, 26.18352944, 26.25116959, 26.31827569, 26.38485476, 26.45091448, 26.51646321,
		26.58150994, 26.64606434, 26.71013675, 26.77373817, 26.83688026, 26.89957537, 26.96183652, 27.02367739,
		27.08511234, 27.14615643, 27.20682538, 27.2671356, 27.32710417, 27.38674888, 27.4460882, 27.50514126,
		27.56392793, 27.62246873, 27.68078489, 27.73889854, 27.79683185, 27.85460837, 27.91225209, 27.96978771,
		28.02724061, 28.08463689, 28.1420033, 28.19936732, 28.25675709, 28.31420146, 28.37172995, 28.42937274,
		28.48716074, 28.54512548, 28.60329919, 28.66171476, 28.72040572, 28.77940629, 28.8387513, 28.89847621,
		28.9586172, 29.0192108, 29.08029503, 29.14190734, 29.20408599, 29.26687079, 29.33030127, 29.39441777,
		29.45926122, 29.52487306, 29.59129532, 29.65857091, 29.72674204, 29.79585303, 29.86594782, 29.93707103,
		30.00926778, 30.08258369, 30.15706484, 30.23275779, 30.30970901, 30.387967, 30.46757924, 30.54859414,
		30.58964285
		);
	}
else if (Gender=="f"){
	//Female percentile arrays
	a5P=Array(
		14.3801866, 14.34527262, 14.31096806, 14.27727686, 14.24420303, 14.21174909, 14.17991775, 14.14871111,
		14.11813062, 14.08817793, 14.0588539, 14.03015932, 14.00209476, 13.97466073, 13.94785767, 13.92168608,
		13.89614655, 13.87123981, 13.84696678, 13.82332865, 13.80032682, 13.77796299, 13.75623885, 13.73515717,
		13.71472021, 13.6949307, 13.67579162, 13.65730608, 13.63947739, 13.62230894, 13.60580421, 13.58996673,
		13.57480002, 13.56030761, 13.54649295, 13.53335941, 13.52091027, 13.50914853, 13.4980774, 13.48769977,
		13.47801782, 13.4690344, 13.46075175, 13.45317191, 13.44629667, 13.44012761, 13.43466608, 13.42991321,
		13.42587042, 13.42253788, 13.41991629, 13.41800595, 13.41680692, 13.41631905, 13.41654198, 13.41747514,
		13.41911775, 13.42146884, 13.42452722, 13.42829152, 13.43276016, 13.43793139, 13.44380325, 13.45037362,
		13.45764018, 13.46560044, 13.47425174, 13.48359125, 13.49361598, 13.50432278, 13.51570835, 13.52776922,
		13.54050176, 13.55390172, 13.56796556, 13.58268918, 13.59806788, 13.61409768, 13.63077388, 13.6480912,
		13.6660455, 13.68463148, 13.70384401, 13.72367745, 13.74412656, 13.7651862, 13.78684956, 13.80911155,
		13.83196589, 13.85540667, 13.87942647, 13.90402053, 13.92918106, 13.95490163, 13.98117536, 14.00799522,
		14.03535405, 14.06324456, 14.0916593, 14.12059071, 14.15003106, 14.17997251, 14.21040706, 14.2413266,
		14.27272286, 14.30458743, 14.33691178, 14.36968723, 14.40290497, 14.43655605, 14.47063181, 14.50512237,
		14.54001862, 14.57531107, 14.61099007, 14.64704584, 14.68346846, 14.72024789, 14.75737391, 14.7948362,
		14.83262428, 14.87072753, 14.90913518, 14.94783635, 14.98681999, 15.0260749, 15.06558978, 15.10535314,
		15.14535337, 15.18557871, 15.22601726, 15.26665697, 15.30748565, 15.34849095, 15.38966039, 15.43098133,
		15.47244099, 15.51402643, 15.55572457, 15.59752218, 15.63940588, 15.68136213, 15.72337724, 15.76543738,
		15.80752854, 15.84963659, 15.89174721, 15.93384595, 15.97591819, 16.01794916, 16.05992392, 16.10182739,
		16.14364432, 16.1853593, 16.22695676, 16.26842096, 16.30973602, 16.35088587, 16.3918543, 16.43262492,
		16.47318119, 16.5135064, 16.55358366, 16.59339594, 16.63292602, 16.67215653, 16.71106993, 16.74964852,
		16.78787442, 16.82572959, 16.86319583, 16.90025476, 16.93688787, 16.97307644, 17.00880162, 17.04404492,
		17.07878553, 17.11300574, 17.14668547, 17.17980508, 17.21234472, 17.24428517, 17.27560407, 17.30628297,
		17.33630159, 17.36563825, 17.39427286, 17.42218439, 17.44935184, 17.47575509, 17.50136974, 17.52617667,
		17.55015468, 17.57328018, 17.59553468, 17.61689275, 17.63733356, 17.6568349, 17.67537444, 17.69292976,
		17.70947829, 17.72499741, 17.73946435, 17.75285625, 17.76515018, 17.77632308, 17.78635182, 17.79521318,
		17.80288385, 17.80934044, 17.81455755, 17.81851675, 17.82119134, 17.82255737, 17.8225906, 17.82126653,
		17.82009046
		);
	
	a85P=Array(
		17.97371413, 17.88748812, 17.80489051, 17.72586396, 17.65035137, 17.57829774, 17.50964839, 17.44434994,
		17.38235043, 17.32359846, 17.26804436, 17.21563951, 17.16633643, 17.12008872, 17.07685104, 17.03657894,
		16.99922885, 16.96475788, 16.93312378, 16.90428477, 16.87819947, 16.85482681, 16.83412628, 16.81605638,
		16.80057671, 16.78764654, 16.77722512, 16.76927159, 16.76374505, 16.76060449, 16.75980884, 16.76131695,
		16.76508763, 16.77107966, 16.77925184, 16.78956298, 16.80197195, 16.81643792, 16.83291965, 16.85137625,
		16.87176785, 16.89405345, 16.91819285, 16.94414613, 16.97187362, 17.00133593, 17.03249399, 17.065309,
		17.09974172, 17.13575468, 17.17330968, 17.21236911, 17.25289572, 17.29485256, 17.33820304, 17.38291092,
		17.42894029, 17.4762556, 17.52482162, 17.57460349, 17.6255667, 17.67767708, 17.7309008, 17.78520438,
		17.8405547, 17.89691898, 17.95426475, 18.01255992, 18.07177271, 18.13187166, 18.19282565, 18.25460388,
		18.31717594, 18.38051241, 18.44458291, 18.50935798, 18.57480931, 18.6409074, 18.70762408, 18.77493214,
		18.84280276, 18.91120897, 18.98012366, 19.04952063, 19.11937316, 19.18965453, 19.26034062, 19.33140487,
		19.4028226, 19.47456869, 19.54662051, 19.6189515, 19.69153965, 19.7643612, 19.83739314, 19.91061278,
		19.98399779, 20.05752615, 20.13117619, 20.20492657, 20.27875629, 20.35264468, 20.42657139, 20.50051644,
		20.57446015, 20.64838321, 20.72226661, 20.79609169, 20.86984014, 20.94349396, 21.01703489, 21.09044653,
		21.16371155, 21.2368133, 21.30973548, 21.38246211, 21.45497756, 21.52726655, 21.5993141, 21.6711056,
		21.74262677, 21.81386368, 21.88480271, 21.95543061, 22.02573445, 22.09570166, 22.16531999, 22.23457754,
		22.30346276, 22.37196443, 22.44007167, 22.50777397, 22.57506113, 22.6419233, 22.70835101, 22.77433508,
		22.83986672, 22.90493747, 22.96953922, 23.03366421, 23.09730502, 23.16045458, 23.2231062, 23.28525349,
		23.34689045, 23.40801143, 23.46861112, 23.52868458, 23.5882272, 23.64723476, 23.70570337, 23.76362952,
		23.82101004, 23.87784214, 23.93412338, 23.98985168, 24.04502533, 24.09964299, 24.15370368, 24.20720678,
		24.26015204, 24.3125396, 24.36436993, 24.41564391, 24.46636278, 24.51652812, 24.56614194, 24.61520657,
		24.66372475, 24.71169957, 24.75913451, 24.80603342, 24.85240053, 24.89824041, 24.94355805, 24.98835785,
		25.03264836, 25.07643282, 25.11971866, 25.16251271, 25.20482218, 25.24665348, 25.28801794, 25.32892108,
		25.3693713, 25.40937929, 25.44895346, 25.48810377, 25.52684029, 25.56517184, 25.603114, 25.64067435,
		25.67786376, 25.714697, 25.75118107, 25.7873338, 25.82316611, 25.85869115, 25.89392241, 25.92887369,
		25.96355908, 25.99799301, 26.03219019, 26.06616563, 26.09993465, 26.13351285, 26.16691611, 26.20016061,
		26.23326279, 26.26623937, 26.29911039, 26.33188509, 26.36458581, 26.39723077, 26.42983873, 26.4624291,
		26.47871966
		);
	
	a95P=Array(
		19.05823845, 18.9659499, 18.87853388, 18.79590999, 18.71799839, 18.64471845, 18.5759903, 18.51173402,
		18.45186978, 18.39631848, 18.34500099, 18.29783876, 18.25475381, 18.21566879, 18.18050711, 18.14919304,
		18.12165176, 18.09780945, 18.07759336, 18.06093184, 18.04775438, 18.03799161, 18.03157513, 18.02843822,
		18.02851473, 18.03173977, 18.03804946, 18.04738091, 18.05967216, 18.0748621, 18.09289045, 18.11369768,
		18.13722499, 18.16341422, 18.19220786, 18.22354893, 18.25738105, 18.29364826, 18.33229525, 18.37326713,
		18.41650918, 18.46196754, 18.50958858, 18.55931907, 18.6111062, 18.6648976, 18.72064127, 18.77828567,
		18.83777954, 18.89907229, 18.96211354, 19.02685341, 19.09324243, 19.16123158, 19.23077228, 19.30181639,
		19.37431624, 19.44822461, 19.52349477, 19.60008044, 19.67793583, 19.75701564, 19.83727506, 19.91866977,
		20.00115598, 20.08469037, 20.16923016, 20.25473307, 20.34115737, 20.42846182, 20.51660573, 20.60554896,
		20.69525186, 20.78567525, 20.87678068, 20.96853014, 21.06088607, 21.15381165, 21.24727049, 21.34122666,
		21.43564505, 21.5304909, 21.62573005, 21.72132885, 21.8172543, 21.91347402, 22.00995586, 22.10666864,
		22.20358151, 22.30066429, 22.39788709, 22.49522108, 22.59263755, 22.69010855, 22.78760665, 22.88510501,
		22.98257733, 23.07999788, 23.17734149, 23.27458358, 23.3717001, 23.46866758, 23.56546313, 23.6620644,
		23.7584496, 23.85459754, 23.95048756, 24.04609957, 24.14141407, 24.23641208, 24.33107526, 24.4253857,
		24.51932616, 24.61287993, 24.70603086, 24.79876336, 24.89106241, 24.98291353, 25.07430281, 25.16521691,
		25.25564304, 25.34556897, 25.43498301, 25.52387405, 25.61223153, 25.70004544, 25.78730634, 25.87400532,
		25.96013405, 26.04568474, 26.13065016, 26.21502362, 26.298799, 26.38197072, 26.46453376, 26.54648363,
		26.62781643, 26.70852876, 26.78861781, 26.8680813, 26.94691749, 27.02512521, 27.10270382, 27.17965324,
		27.25597392, 27.33166687, 27.40673363, 27.4811763, 27.55499753, 27.62820049, 27.70078892, 27.77276709,
		27.84413981, 27.91491246, 27.98509093, 28.05468167, 28.12369169, 28.19212851, 28.26000023, 28.32731547,
		28.3940834, 28.46031375, 28.52601678, 28.59120331, 28.6558847, 28.72007286, 28.78378025, 28.84701989,
		28.90980533, 28.97215071, 29.03407069, 29.0955805, 29.15669594, 29.21743336, 29.27780966, 29.33784232,
		29.39754939, 29.45694948, 29.51606178, 29.57490604, 29.63350259, 29.69187236, 29.75003682, 29.80801808,
		29.8658388, 29.92352223, 29.98109225, 30.03857329, 30.09599042, 30.15336926, 30.21073618, 30.26811799,
		30.32554216, 30.38303689, 30.44063073, 30.49835324, 30.55623433, 30.61430463, 30.67259539, 30.73113849,
		30.78996648, 30.84911253, 30.90861047, 30.96849476, 31.02880054, 31.08956357, 31.1508203, 31.21260782,
		31.27496389, 31.33792691, 31.40153631, 31.46583097, 31.53085181, 31.59663995, 31.66323724, 31.73068625,
		31.76474311
		);
	}

//The arrays start with 2 years
i=AgeM-24;		

if (BMI<a5P[i]) Type="under";
else if (BMI<a85P[i]) Type="normal";
else if (BMI<a95P[i]) Type="atrisk";
else if (BMI>=a95P[i]) Type="over";

return [Type, a5P[i], a85P[i]];
}

//----

function SwitchHorTab(Tab){
var oForm=getbyid(Tab+'form'), OtherTab, oOtherForm, oPanelSt, oOtherPanelSt;

if (CurrHorTab==Tab) return;

OtherTab=GetOtherTab(Tab); oOtherForm=getbyid(OtherTab+'form');
oForm.style.display='block'; oOtherForm.style.display='none';

getbyid(Tab+'tab').className='CurrHorTab';
getbyid(OtherTab+'tab').className='HorTab';
CurrHorTab=Tab;

if (bHaveResults[OtherTab]) getbyid(OtherTab+'_result').style.display='none';
if (bHaveResults[Tab]) getbyid(Tab+'_result').style.display='block';

if (Tab=='activ') InitActivList();
}

function GetOtherTab(Tab){
var i;

//Find this
for(i=0; i<2; i++){
	if (aHorTabs[i]==Tab) break;
	}
return aHorTabs[1-i];
}

function SwitchMetrics(Metrics){
var oMetrics, OtherMetr, oOtherMetrics, i, Tab, Display;

if (CurrMetr==Metrics) return;

OtherMetr=(Metrics=='metric' ? 'imperial' : 'metric');
for(i=0; i<2; i++){
	Tab=aHorTabs[i];
	oMetrics=getbyid(Tab+Metrics); oOtherMetrics=getbyid(Tab+OtherMetr); 
	
	if (!aMetricTbl[i]) Display='block';
	else Display=(!Gec && !Op ? 'block' : 'table-row-group'); 
	oMetrics.style.display=Display; oOtherMetrics.style.display='none';
	
	getbyid(Metrics+'tab').className='MetTabCurr';
	getbyid(OtherMetr+'tab').className='MetTab';
	}

CurrMetr=Metrics;
if (CurrHorTab=='activ') InitActivList();
}

//-----
//Heart

function ShowHeart(){
var Age, Msg, MaxHRMin, MaxHR, Interval, IntName, RestPulse;

while(true){
	Age=ReadNum(F.Age, true);
	if (Age<1 || Age>125) Msg='Now please enter your age';
	break;
	}

if (Msg){
	alert(Msg); return;
	}

MaxHRMin=MaxHR=220-Age;

Interval=RadioGet('Interval', F);
if (Interval!=1){
	MaxHR=Math.round(MaxHR/Interval);
	IntName=(60/Interval)+' seconds';
	}
else IntName='1 minute';

SetText(getbyid('MaxHR'), MaxHR);
SetText(getbyid('IntName'), IntName);

RestPulse=ReadNum(F.RestPulse, true);
if (RestPulse>0 && (RestPulse<15 || RestPulse>200)){
	alert('The resting heart rate you entered seems incorrect'); RestPulse=0;
	}

ShowTrainZones(RestPulse, MaxHRMin, Interval);

getbyid('Results').style.display='block';
}

function ShowTrainZones(RestPulse, MaxHR, Interval){

getbyid('TrainZones').style.display=(RestPulse ? 'block' : 'none');
if (!RestPulse) return;

var aZones=[[50, 60], [60, 70], [70, 80], [80, 90], [90, 100]], i, aZone, HRR, aRows;

HRR=MaxHR-RestPulse; aRows=getbyid('TrainZonesT').rows;

for(i=0; i<5; i++){
	aZone=aZones[i];
	SetText(aRows[i+1].cells[2], 
		Math.round((RestPulse+HRR*aZone[0]/100)/Interval)+'-'+
		Math.round((RestPulse+HRR*aZone[1]/100)/Interval));
	}

}

function ZoneClick(e){
var ZoneRowNo;

EventInit(e);
if (!(EvSrc.tagName=='TD' && EvSrc.className=='ZoneLink')) return;

ZoneRowNo=EvSrc.parentNode.rowIndex;
for(RowNo=1; RowNo<=5; RowNo++){
	getbyid('Expl'+RowNo).style.display=(ZoneRowNo!=RowNo ? 'none' : 'block');
	}
}

function ShowCalories(){
if (CurrHorTab=='basal') ShowBMR(); else ShowActivCalor();
}

function CopyCaloryWeight(){
var FromPref=CurrHorTab.substr(0, 2), ToPref;

ToPref=GetOtherTab(CurrHorTab).substr(0, 2);
F[ToPref+'_wkg'].value=F[FromPref+'_wkg'].value;
F[ToPref+'_wlbs'].value=F[FromPref+'_wlbs'].value;
}

function ShowActivCalor(){
var Msg, Weight, Time, CalValue;

if (!(WH=ReadWeightHeight('ac', false))) return false;
Weight=WH[0]; CopyCaloryWeight();

Time=ReadNum(F.timeh)*60+ReadNum(F.timem);
if (Time<1){
	alert('Please specify correct time'); return false;
	}

CalValue=F.Activity.value;
if (!CalValue){
	alert('Select an activity'); return false;
	}

SetText(getbyid('Calories'), Math.round(Time*Weight*F.Activity.value/132.00595419));
ShowCalorieResults('activ');
}

function ShowBMR(){
var Gender, Age, Msg, Weight, Height, Calories;

if (!(Gender=RadioGet('gender', F))) Msg='gender';
else if (!(Age=ReadNum(F.age))) Msg='age';

if (Msg){
	alert('Please specify your '+Msg); return false;
	}
if (Age<4 || Age>225){
	alert('The age seems incorrect'); return false;
	}

if (!(WH=ReadWeightHeight('ba', Age<=15))) return false;
Weight=WH[0]; Height=WH[1]; CopyCaloryWeight();
if (Gender=='m') S=5; else if (Gender=='f') S=-161;
Calories=Math.round(Weight*4.53138778+Height*15.87503175-4.92*Age+S);

SetText(getbyid('BMR'), Calories);
ShowDailyCalNeeds(Calories);
ShowCalorieResults('basal');
}

function ShowDailyCalNeeds(Calories){
var aCoef=[1.2, 1.375, 1.55, 1.725, 1.9], i, aRows;

aRows=getbyid('CalNeeds').rows;
for(i=0; i<5; i++){
	SetText(aRows[i+1].cells[2], Math.round(Calories*aCoef[i]));
	}
}

function ShowCalorieResults(Type){
var OtherType=GetOtherTab(Type);

//Hide the other results div
getbyid(OtherType+'_result').style.display='none';
//Show the results div
getbyid(Type+'_result').style.display='block'; bHaveResults[Type]=true;
}

//-----

InitUA();