/*
Filename:	webJS.js
Authors:	Ross Schalmo, Tyler Rand, Jeff Schwab
Purpose:	JavaScript used on front-end pages. 
Templates:	None
*/

function cal_bmi(lbs, ins){
   h2 = ins * ins;
   bmi = lbs/h2 * 703
   f_bmi = Math.floor(bmi);
   diff  = bmi - f_bmi;
   diff = diff * 10;
   diff = Math.round(diff);
   if (diff == 10){
      // Need to bump up the whole thing instead
      f_bmi += 1;
      diff = 0;
   }
   bmi = f_bmi + "." + diff;
   return bmi;
}

function compute(){
   var f = self.document.BMIform;

   w = f.weight.value;
   v = f.HeightDropDown.value;
   u = f.InchDropDown.value;

   // Format values for the BMI calculation

   if (!chkw(u)){
     var ii = 0;
     f.InchDropDown.value = 0;
   } else {
     var it = f.InchDropDown.value*1;
     var ii = parseInt(it);
       }

   var fi = parseInt(f.HeightDropDown.value * 12);
   var i = fi + ii;

  // Do validation of remaining fields to check for existence of values

   if (!chkw(v)){
     alert("Please enter a number for your height.");
     f.HeightDropDown.focus();
     return;
   }
   if (!chkw(w)){
     alert("Please enter a number for your weight.");
     f.weight.focus();
     return;
   }

   // Perform the calculation

   f.BMI.value = cal_bmi(w, i);
   f.BMI.focus();
}

function chkw(w){
   if (isNaN(parseInt(w))){
      return false;
   } else if (w < 0){
  return false;
  }
  else{
  return true;
  }
}

function validate(){
	var myForm = document.getElementById('userLogin');
	if(myForm.uniqueid.value == '' || myForm.password.value == ''){
		var loginError = document.getElementById('loginError');
		loginError.innerHTML = 'You must enter a username and password.';
		return false;
	}else{
		return true;
	}		
}



function adjustDivsAdmin(){	
	var divs = new Array(4);
	var maxHeight;

	divs[0] = document.getElementById('leftlinkbar');
	//divs[1] = document.getElementById('picturebar');
	divs[1] = document.getElementById('contentbar');
	divs[2] = document.getElementById('rightlinkbar');
	
	picbarheight = document.getElementById('topgraphic').offsetHeight;
		
	maxHeight = divs[0].offsetHeight;
		
	for (var x = 1; x<=2; x++)
	{
		if (divs[x].offsetHeight > maxHeight)
		{
			maxHeight = divs[x].offsetHeight;
		}
	}
		

	if(maxHeight < 327){
		maxHeight=327;
	}	

	for (var x=0; x<=2; x++)
	{
		divs[x].style.height = maxHeight + "px";
	}
	
	document.getElementById('bottomgraphic').style.top = (maxHeight + picbarheight + 43) + "px";
	document.getElementById('bottomspace').style.top = (maxHeight + picbarheight + 68) + "px";
}

function resize(){
try{
	var x, y, max;
	x = document.getElementById('img').width;
	y = document.getElementById('img').height;
	max = 150;
	if (x > y || x == y){
		if (x > 150){
			document.getElementById('img').width=max;
		}
	} 
	else{
		if (y > 150){
			document.getElementById('img').height=max;
		}
	}
}
catch(err){
}

}


