// Body Fat Calculator
// copyright 23rd April 2006, by Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration

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 stripBlanks(fld) 
	{
	var result = "";
	var c = 0;

	for (i=0; i < fld.length; i++) 
		{if (fld.charAt(i) != " " || c > 0) 
			{result += fld.charAt(i);
			if (fld.charAt(i) != " ") 
				c = result.length;
			}
		}
	return result.substr(0,c);
	}

function maleFat(h,n,w) 
	{
	return thou(495/(1.0324-0.19077*(Math.log(w-n)/Math.LN10)+0.15456*(Math.log(h)/Math.LN10))-450);
	}

function femaleFat(h,n,w,r) 
	{
	return thou(495/(1.29579-0.35004*(Math.log(w+r-n)/Math.LN10)+0.22100*(Math.log(h)/Math.LN10))-450);
	}

function thou(n) 
	{
	return Math.round(n*10)/10+'%';
	}

function sex(thisform) 
	{
	var s = valButton(thisform.s);
	
	thisform.r.disabled = (s != 'f');
	}

function calc(thisform) 
	{
	var s = valButton(thisform.s);

	if (s == null) 
		{
		alert("Kérem adja meg a nemét!");
		thisform.s.focus();
		return false;
		}
//	var d = valButton(thisform.d);
	var d = 1;
	if (d == null) 
		{
		alert("Válasszon mértékegységet");
		thisform.d.focus();
		return false;
		}
	d = Number(d);
	var h = stripBlanks(thisform.h.value);
	if (h == '') 
		{
		alert("Kérem, adja meg a magasságát!");
		thisform.h.focus();
		return false;}
	if (h != Number(h) || (h = Number(h*d)) < 100 || h > 250) 
		{
		alert("Hibás magasság! (100cm-250cm közötti értéket kérek)");
		thisform.h.focus();
		return false;
		}
	var n = stripBlanks(thisform.n.value);
		if (n == '') 
			{
			alert("Kérem adja meg a nyak-körfogatát!");
			thisform.n.focus();
			return false;
			}
	if (n != Number(n) || (n = Number(n*d)) < 20 || n > 60) 
		{
		alert("Hibás nyakméretet adott meg (min 20 cm) !");
		thisform.n.focus();
		return false;
		}
	var w = stripBlanks(thisform.w.value);
		if (w == '') 
			{
			alert("Kérem Adja meg a derékbőségét!");
			thisform.w.focus();
			return false;
			}
		if (w != Number(w) || (w = Number(w*d)) < 30 || w > h) 
			{
			alert("Hibás derékbőség");
			thisform.w.focus();
			return false;
			}
	var r = 0;
	if (s == 'f') 
		{
		r = stripBlanks(thisform.r.value);
		if (r == '') 
			{
			alert("Kérem, adja meg a csípőméretét!");
			thisform.r.focus();
			return false;
			}
		if (r != Number(r) || (r = Number(r*d)) < 40 || h > 200) 
			{
			alert("Hibás csípőméret!");
			thisform.r.focus();
			return false;
			}
		}

	switch(s) 
		{
		case 'm': thisform.f.value = maleFat(h,n,w); break; 
		case 'f': thisform.f.value = femaleFat(h,n,w,r);
		}
	}
                    