//
//This form and the associated code are copyright 1999-2009 by Carl W. Meilahn    
//

function disclaimer()
{
message = "Warning!    This calculator is provided as a tool for Certified Nitrox Divers. The author of this software cannot accept responsibility for its accuracy when used as a dive planning device.  While great care has been taken to ensure accuracy in programming, the application of results from this Nitrox Calculator to real dives, without consultation with official tables or your own calculations, is not to be done under any circumstances. Several sources of error increase your risk of decompression sickness and/or Oxygen Toxicity and make this a hazardous practice.";
message1 = "Warning!	This calculator is provided as a tool for Certified Nitrox Divers. Do no use the calculator to determine values for in-water use.  Do not attempt to substitute any of these calculations for your own, verified, calculations.  Several sources of error increase your risk of decompression sickness and/or Oxygen Toxicity and make this a hazardous practice. Use of these calculations is at your own risk.";
alert(message);
}

function checkNumber(input, min, max, msg)
{
    msg = msg + " field has invalid data: " + input.value;
    var str = input.value;
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring(i, i + 1)
        if ((ch < "0" || "9" < ch) && ch != '.') {
            alert(msg);
            return false;
        }
    }
    var num = parseFloat(str)
    if (num < min || max < num) {
        alert(msg + " not in range [" + min + ".." + max + "]");
        return false;
    }
       return true;
}

function testField(input)
{
    if (input.value != null && input.value.length != 0)
        input.value = "" + input.value;
    computeForm(input.form);
}

function roundUp(fpnumber)
{
var x = parseFloat(fpnumber) * Math.pow(10,2);
x = x - (x % 1) + 1;
var y = Math.round(x) / Math.pow(10,2);
return y;
}

function roundDown(fpnumber)
{
var x = parseFloat(fpnumber) * Math.pow(10,2);
x = x - (x % 1);
var y = Math.round(x) / Math.pow(10,2);
return y;
}

function places(fpnumber)
{
var x = fpnumber * Math.pow(10,2);
x = x - (x % 1);
x = x / Math.pow(10,2);
return x;
}

function ADFocus(form)
{
form.PO2.value="";
form.PN2.value="";
form.BestMix.value="";
form.ActualDepth.focus();
form.ActualDepth.value=110;
form.ActualDepth.select();
form.ATA.value = "";
form.EAD.value = "";
}

function ATFocus(form)
{
form.PO2.value="";
form.PN2.value="";
form.BestMix.value="";
form.ATA.focus();
form.ATA.value=4.33;
form.ATA.select();
form.ActualDepth.value = "";
form.EAD.value = "";
}

function EDFocus(form)
{
form.PO2.value="";
form.PN2.value="";
form.BestMix.value="";
form.EAD.focus();
form.EAD.value=110;
form.EAD.select();
form.ActualDepth.value = "";
form.ATA.value = "";
}

function startFocus(form)
{
form.ATM[0].checked=1;
form.Changed[0].checked=1;
form.PercentO2.selectedIndex=11;
form.PO2_Max.selectedIndex=10;
form.PO2_CMax.selectedIndex=12;
form.ActualDepth.focus();
form.ActualDepth.value=110;
form.ActualDepth.select();
}

function clearForm(form)
{
form.ATA.value="";
form.EAD.value="";
form.MOD.value="";
form.CMD.value="";
form.PO2.value="";
form.PN2.value="";
form.BestMix.value="";
startFocus(form);
calculatePercentN2();
setDepth();
}

function calculatePercentN2()
{
var Percent =  (1.0 - parseFloat(document.EANxCalculator.PercentO2.options[document.EANxCalculator.PercentO2.selectedIndex].value) )
Percent = (Math.round( Percent * Math.pow(10,2) )) / Math.pow(10,2)
//document.EANxCalculator.PercentN2.value = Percent;
document.EANxCalculator.PercentN2.value = Math.round(100 * Percent) + "%";

}

function checkPO2s(form)
{
if (form.PO2_Max.selectedIndex <= form.PO2_CMax.selectedIndex)
{return true;}
else {
alert("Your Maximum PO2 must be less than your Contingency PO2.  Please re-select the values.");
form.MOD.value="";
form.CMD.value="";
return false;}
}

function setDepth()
{
if (document.EANxCalculator.ATM[0].checked) { D = 33; units = "ft";}
else { if (document.EANxCalculator.ATM[1].checked) { D = 34; units = "ft";}
else { if (document.EANxCalculator.ATM[2].checked) { D = 10; units = "m";}
else { if (document.EANxCalculator.ATM[3].checked) { D = 10.3; units = "m";}
}}};
document.EANxCalculator.Depth.value = D + units;
}


function computeForm(form)
{
var D = 0;
if (form.ATM[0].checked) { D = 33; units = "ft";}
else { if (form.ATM[1].checked) { D = 34; units = "ft";}
else { if (form.ATM[2].checked) { D = 10; units = "m";}
else { if (form.ATM[3].checked) { D = 10.3; units = "m";}
}}};
form.Depth.value = D + units;
calculatePercentN2();
N2P = (1.0 - parseFloat(document.EANxCalculator.PercentO2.options[document.EANxCalculator.PercentO2.selectedIndex].value) );
O2P = parseFloat(form.PercentO2.options[form.PercentO2.selectedIndex].value);
PO2MX = parseFloat(form.PO2_Max.options[form.PO2_Max.selectedIndex].text);
PO2CMX = parseFloat(form.PO2_Max.options[form.PO2_CMax.selectedIndex].text);

form.MOD.value = roundDown( ( ( PO2MX / O2P ) - 1 ) * D );
form.CMD.value = roundDown(( ( PO2CMX / O2P ) - 1 ) * D);

if ( form.Changed[0].checked )
  { AD = parseFloat(form.ActualDepth.value);
    AT  =   ( AD + D ) / D ;
    form.ATA.value = roundUp( AT );
    ED = ( ( 1- O2P ) * ( AD + D ) / 0.79) - D;
    form.EAD.value = roundUp( ED );
  }
else { if ( form.Changed[1].checked )
  { AT = parseFloat(form.ATA.value);
    AD  =   ( AT * D ) - D ;
    form.ActualDepth.value = roundUp( AD );
    ED = ( ( 1- O2P ) * ( AD + D ) / 0.79) - D;
    form.EAD.value = roundUp( ED );
  }
else { if ( form.Changed[2].checked )
  { ED = parseFloat(form.EAD.value);
    AD = ( ( 0.79 * ( ED + D ) ) / ( 1-O2P ) ) - D;
    form.ActualDepth.value = roundUp( AD );
    AT  =   ( AD + D ) / D ;
    form.ATA.value = roundUp( AT );
  }
}};
if ( (AT * O2P) < 1.0 )
{ form.PO2.value = "0" + roundUp( ( AT * O2P ) ); }
else { form.PO2.value = roundUp( AT * O2P ); }  

if ( ( AT * N2P ) < 1.0 )
{ form.PN2.value = "0" + roundUp( (AT * N2P) ); }
else { form.PN2.value = roundUp( AT * N2P ); }

//if ( (( PO2MX / AT ) < 1.0)  )
//{ if ( (( PO2MX / AT ) < 0.1) )
//  { form.BestMix.value = roundDown( (PO2MX / AT) ); }
//  else { form.BestMix.value = "0" + roundDown( (PO2MX / AT) ); }
//}
//else { form.BestMix.value = roundDown( PO2MX / AT ) ; }

form.BestMix.value = Math.round((100 * roundDown( (PO2MX / AT) ))) + "%";

if ( roundUp( AT * O2P ) > PO2MX )
{ alert("The calculated PO2 is greater than your selected Maximum PO2."); };
if ( roundUp( AT * O2P ) > PO2CMX )
{ alert("The calculated PO2 is greater than your selected Maximum Contingency PO2."); };

return;
}
