var josBMI;
var josBMIString;

function mod(div,base) {
	return Math.round(div - (Math.floor(div/base)*base));
}

function calcBmi(josw, josHeightFeetMeter, josHeightInchesCm, josMetric) {
	var UnitTrans = 703;
	if (josMetric > 0)
	UnitTrans = 1;
	 
	var w = josw * 1;
	var HeightFeetInt = josHeightFeetMeter * 1;
	var HeightInchesInt = josHeightInchesCm * 1;
	
	HeightFeetConvert = HeightFeetInt * 12;
	h = HeightFeetConvert + HeightInchesInt;

	if ( !(josMetric) && ((w <= 35) || (w >= 500)  || (h <= 48) || (h >= 120)) ) {
		alert ("Invalid data.  Please check and re-enter!");
		return false;
	}
	
	hm = josHeightFeetMeter + josHeightInchesCm / 100;	

	if ( (josMetric) && ((w <= 10) || (w >= 400)  || (hm <= 0.9) || (hm >= 3)) ) {
		alert ("Invalid data.  Please check and re-enter!");
		return false;
	}
	if (josMetric > 0)
	h=josHeightFeetMeter + josHeightInchesCm / 100;
	
	displaybmi = Math.round(((w * UnitTrans) / (h * h))*100)/100; //(Math.round((w * UnitTrans) / (h * h)));
	var rvalue = true;

	if (rvalue) {
	if (HeightInchesInt > 11) {
	reminderinches = mod(HeightInchesInt,12);
	josHeightInchesInt = reminderinches;
	josHeightFeetInt = HeightFeetInt + 
	((HeightInchesInt - reminderinches)/12);
	josBMI = displaybmi;
	}
	if (displaybmi <18.5) 
	josBMIString = "Underweight";
	if (displaybmi >=18.5 && displaybmi <=24.99) 
	josBMIString = "Normal Weight";
	if (displaybmi >=25 && displaybmi <=29.99) 
	josBMIString = "Overweight";
	if (displaybmi >=30 && displaybmi <=34.99) 
	josBMIString = "Obesity (Class 1)";
	if (displaybmi >=35 && displaybmi <=39.99) 
	josBMIString = "Obesity (Class 2)";
	if (displaybmi >=40) 
	josBMIString = "Morbid Obesity";
	josBMI = displaybmi; }
	return rvalue;
}

// MM DreamWeaver findObj() Function
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function josChange(obj) {
	var objlbfeet = findObj("joslbfeet");
	var objlbinches = findObj("joslbinches");
	var objlbw = findObj("joslbw");

	if (obj.value == "true") {
		objlbfeet.innerHTML = " Meters";
		objlbinches.innerHTML = " Cm";
		objlbw.innerHTML = " Kg";
	}
	else {
		objlbfeet.innerHTML = " Feet";
		objlbinches.innerHTML = " Inches";
		objlbw.innerHTML = " Lbs";
	}
}

function docalc() {
	var objjosHFM = findObj("josHFM");
	var objjosHIC = findObj("josHIC");
	var objjosw = findObj("josW");
	var objjosUnit = findObj("josUnit");
	var objjosResult = findObj("josResult");
	var x;
	if ( calcBmi(parseInt(objjosw.value), parseInt(objjosHFM.value), parseInt(objjosHIC.value), objjosUnit.value=="true") ) {
		objjosResult.innerHTML = josBMI + " " + josBMIString;
		if (josBMI<17) { josBMI=17; }
		if (josBMI>41) { josBMI=41; }
		x = Math.round((15.6)*parseInt(josBMI) - 252);
		moveInd(x);
	}
}

function moveInd(x) {
	objjosInd = findObj("josind");
	objjosInd.style.left = x + "px";
	objjosInd.style.visibility = "visible";
}