function cal_bmi(kg, htc){
   m = htc;
   h2 = m * m;

   bmi = kg/h2;


   f_bmi = Math.floor(bmi);

   diff  = bmi - f_bmi;
   diff = diff * 10;

   diff = Math.round(diff);
   if (diff == 10){
      f_bmi += 1;
      diff = 0;
   }
   bmi = f_bmi + "." + diff;

   return bmi;
}
function compute(){
   var f = document.bmi_input;

   w = f.kg.value;
   i = f.htc.value;

   if ((i=="") || (i=="0") || isNaN(i) ){
     alert("Please enter a number for your height.");
     f.htc.focus();
     return;
   }
   if ((w=="") || (w=="0") || isNaN(w) ){
     alert("Please enter a number for your weight.");
     f.kg.focus();
     return;
   }

   f.bmi.value = cal_bmi(w, i);
   f.bmi.focus();
}

function compute2(){
   var f2 = document.bmi_input2;

   w2 = f2.lb.value;
   i2 = f2.hti.value;

   if ((i2=="") || (i2=="0") || isNaN(i2) ){
     alert("Please enter a number for your height.");
     f2.hti2.focus();
     return;
   }
   if ((w2=="") || (w2=="0") || isNaN(w2) ){
     alert("Please enter a number for your weight.");
     f2.lb2.focus();
     return;
   }

   f2.bmi2.value = cal_bmi2(w2, i2);
   f2.bmi2.focus();
}

function cal_bmi2(lb, hti){

   m2 = hti;
   h22 = m2 * m2;

   bmi2 = (lb * 703) / h22;

   f_bmi2 = Math.floor(bmi2);

   diff2  = bmi2 - f_bmi2;
   diff2 = diff2 * 10;

   diff2 = Math.round(diff2);
   if (diff2 == 10){
      f_bmi2 += 1;
      diff2 = 0;
   }
   bmi2 = f_bmi2 + "." + diff2;

   return bmi2;
}

function calcmetric() { 
   var val1 = document.metric.stones.value * 1;
   var val2 = document.metric.pounds.value * 1;
	if (isNaN(val1) || isNaN(val2)) {
	     alert("Please enter a number for your weight.");
	     return;
	}
   var pounds = "0";
	if (val1>"0") {
	pounds = val1 * 14;
	}
   pounds = pounds+val2;
   pounds = Math.round((eval(pounds/2.2))*100)/100;
   document.metric.kg1.value=pounds;

   var val3=document.metric.feet.value * 1;
   var val4=document.metric.inches.value * 1;
	if (isNaN(val3) || isNaN(val4)) {
	     alert("Please enter a number for your height.");
	     return;
	}
   var cm = "0"
	if (val3>"0") {
	cm=val3 * 12;
	}
   cm = cm + val4;
   cm = Math.round((eval(cm*2.54))*100)/100;
   document.metric.cm1.value=cm;
}

function Nitrox(type) {
  if(document.nitrox_form.nitroxagree.checked==true){
   var maxmix=0;
   var maxdepth=0;
	if (type=="MaxDepth"){
	//Calculate max depth allowed for mixture
	  var ppo2 = document.nitrox_form.ppo2_1.value * 1;
	  var mix = document.nitrox_form.mix.value/100;
		if ((isNaN(ppo2) || isNaN(mix)) || ((ppo2=="0") || (mix=="0")) || (mix<"0.21") || (ppo2>"1.6")){
		document.nitrox_form.maxdepth.value="ERROR";
		alert("Please enter valid numbers for mix and ppo2\nMix must be 21 or above\nppo2 must be 1.6 or below");
		}
		else{
		  var maxdepth = Math.round(eval(((10*ppo2)/mix)-10)*100)/100;
		  document.nitrox_form.maxdepth.value=maxdepth;
		  document.nitrox_form.EADdepth.value=maxdepth;
		  document.nitrox_form.EADmix.value=(mix*100);
		  Nitrox("EAD");
		}
	}
	if (type=="MaxMix"){
	//Calculate max mix allowed for depth
	  var ppo2 = document.nitrox_form.ppo2_2.value *1;
	  var depth = document.nitrox_form.depth.value *1;
		if ((isNaN(ppo2) || isNaN(depth)) || ((ppo2=="0") || (depth=="0")) || (ppo2>"1.6")){
		alert("Please enter valid numbers for depth and ppo2\nppo2 must be 1.6 or below");
		}
		else{
		  var maxmix = Math.round(eval((ppo2/((depth/10)+1))*100)*100)/100;
		  document.nitrox_form.maxmix.value=maxmix;
			if ((maxmix>="21")){
			  document.nitrox_form.EADdepth.value=depth;
			  document.nitrox_form.EADmix.value=maxmix;
			  Nitrox("EAD");
			}
			else{
			//document.nitrox_form.EAD.value="ERROR";
			alert("Invalid mix percentage for this depth and ppo2\nNitrox mix must be 21% or above");
			}
		}
	}
	if (type=="EAD"){
	//Calculate EAD for given depth and mixture
	  var depth = document.nitrox_form.EADdepth.value * 1;
	  var mix = document.nitrox_form.EADmix.value * 1;
		if ((isNaN(depth) || isNaN(mix)) || ((depth=="0") || (mix=="0")) || (mix<"21")){
		document.nitrox_form.EAD.value="ERROR";
		alert("Please enter valid numbers for mix and depth\nMix must be 21 or above");
		}
		else{
		  var EAD = Math.round(eval(((((100-mix)/100)*(depth+10))/0.79)-10)*100)/100;
		  document.nitrox_form.EAD.value=EAD;
		}
	}
	}
  else {
	alert("You must signify your understanding of and qualification in the use of\nnitrox before using these calculators")
  }
}