/**
* Author:        Dan (http://thewebpageofdan.com)
* Description:   Javascript component for dive table calculator
*
* Some mods by Gary C. Kessler, 4/5/2009
*/

window.onload = init;

// ID of the element where output will be displayed
var output_id = "output";

// IDs of the input fields
var depth_id = "depth";
var time_id = "time";
var surface_hr_id = "surface_hr";
var surface_min_id = "surface_min";
var depth2_id = "depth2";

// IDs of the input labels (used for errors)
var depth_label_id = "depth_label";
var time_label_id = "time_label";
var surface_label_id = "surface_label";
var depth2_label_id = "depth2_label";

/**
* Main function
*/
function check()
{
    // Instantiate some variables
    var error = new Array();
    var first_group = "";
    var second_group = "";
    var rnt = 0;
    var amdt = 0;
    var printed_answer = "";

    var depth_field = document.getElementById(depth_id);
    var time_field = document.getElementById(time_id);
    var surface_hr_field = document.getElementById(surface_hr_id);
    var surface_min_field = document.getElementById(surface_min_id);
    var depth2_field = document.getElementById(depth2_id);
    var depth_label = document.getElementById(depth_label_id);
    var time_label = document.getElementById(time_label_id);
    var surface_label = document.getElementById(surface_label_id);
    var depth2_label = document.getElementById(depth2_label_id);
    output_div = document.getElementById(output_id);

    depth_label.className = time_label.className = surface_label.className = depth2_label.className = "";

    // Get user input
    var depth = parseInt(depth_field.value);
    var time = parseInt(time_field.value);
    var surface_hr = parseInt(surface_hr_field.value);
    var surface_min = parseInt(surface_min_field.value);
    var depth2 = parseInt(depth2_field.value);

    // Make sure we are dealing with numbers here
    depth = (isNaN(depth)) ? 0 : depth;
    time = (isNaN(time)) ? 0 : time;
    surface_hr = (isNaN(surface_hr)) ? 0 : surface_hr;
    surface_min = (isNaN(surface_min)) ? 0 : surface_min;
    depth2 = (isNaN(depth2)) ? 0 : depth2;

    // Make sure surface interval is in terms of minutes
    var surface = surface_hr * 60 + surface_min;

    // Error checks we can do before calculating
    if (depth <= 0 || depth > 130)
    {
        error.push("Your first dive must be between 1 and 130 ft.");
        depth_label.className = "error";
        depth_field.focus();
    }

    if (time <= 0)
    {
        error.push("You must enter the total time of your first dive.");
        time_label.className = "error";
        time_field.focus();
    }

    if (surface < 10 || surface > 1440)
    {
        error.push("Your surface interval must at least 10 minutes and no greater than 24 hours.");
        surface_label.className = "error";
        surface_hr_field.focus();
    }

    if (depth2 <= 0 || depth2 > 130)
    {
        error.push("Your second dive must be between 1 and 130 ft.");
        depth2_label.className = "error";
        depth2_field.focus();
    }

    if (error.length)
    {
        output_errors(error);
        return false;
    }

    // Determine the first group based on the time and depth of the first dive
    first_group = (depth <= 40 && time <= 5) ? "a" : first_group;
    first_group = ((depth <= 40 && time > 5 && time <= 15) || (depth > 40 && depth <= 50 && time <= 10) || (depth > 50 && depth <= 60 && time <= 10) || (depth > 60 && depth <= 70 && time <= 5) || (depth > 70 && depth <= 80 && time <= 5) || (depth > 80 && depth <= 90 && time <= 5) || (depth > 90 && depth <= 100 && time <= 5)) ? "b" : first_group;
    first_group = ((depth <= 40 && time > 15 && time <= 25) || (depth > 40 && depth <= 50 && time > 10 && time <= 15) || (depth > 50 && depth <= 60 && time > 10 && time <= 15) || (depth > 60 && depth <= 70 && time > 5 && time <= 10) || (depth > 70 && depth <= 80 && time > 5 && time <= 10) || (depth > 80 && depth <= 90 && time > 5 && time <= 10) || (depth > 90 && depth <= 100 && time > 5 && time <= 7) || (depth > 100 && depth <= 110 && time <= 5) || (depth > 110 && depth <= 120 && time <= 5) || (depth > 120 && depth <= 130 && time <= 5)) ? "c" : first_group;
    first_group = ((depth <= 40 && time > 25 && time <= 30) || (depth > 40 && depth <= 50 && time > 15 && time <= 25) || (depth > 50 && depth <= 60 && time > 15 && time <= 20) || (depth > 60 && depth <= 70 && time > 10 && time <= 15) || (depth > 70 && depth <= 80 && time > 10 && time <= 15) || (depth > 80 && depth <= 90 && time > 10 && time <= 12) || (depth > 90 && depth <= 100 && time > 7 && time <= 10) || (depth > 100 && depth <= 110 && time > 5 && time <= 10) || (depth > 110 && depth <= 120 && time > 5 && time <= 10) || (depth > 120 && depth <= 130 && time > 5 && time <= 8)) ? "d" : first_group;
    first_group = ((depth <= 40 && time > 30 && time <= 40) || (depth > 40 && depth <= 50 && time > 25 && time <= 30) || (depth > 50 && depth <= 60 && time > 20 && time <= 25) || (depth > 60 && depth <= 70 && time > 15 && time <= 20) || (depth > 70 && depth <= 80 && time > 15 && time <= 20) || (depth > 80 && depth <= 90 && time > 12 && time <= 15) || (depth > 90 && depth <= 100 && time > 10 && time <= 15) || (depth > 100 && depth <= 110 && time > 10 && time <= 13) || (depth > 110 && depth <= 120 && time > 10 && time <= 12)) ? "e" : first_group;
    first_group = ((depth <= 40 && time > 40 && time <= 50) || (depth > 40 && depth <= 50 && time > 30 && time <= 40) || (depth > 50 && depth <= 60 && time > 25 && time <= 30) || (depth > 60 && depth <= 70 && time > 20 && time <= 30) || (depth > 70 && depth <= 80 && time > 20 && time <= 25) || (depth > 80 && depth <= 90 && time > 15 && time <= 20) || (depth > 90 && depth <= 100 && time > 15 && time <= 20) || (depth > 100 && depth <= 110 && time > 13 && time <= 15)) ? "f" : first_group;
    first_group = ((depth <= 40 && time > 50 && time <= 70) || (depth > 40 && depth <= 50 && time > 40 && time <= 50) || (depth > 50 && depth <= 60 && time > 30 && time <= 40) || (depth > 60 && depth <= 70 && time > 30 && time <= 35) || (depth > 70 && depth <= 80 && time > 25 && time <= 30) || (depth > 80 && depth <= 90 && time > 20 && time <= 25) || (depth > 90 && depth <= 100 && time > 20 && time <= 22)) ? "g" : first_group;
    first_group = ((depth <= 40 && time > 70 && time <= 80) || (depth > 40 && depth <= 50 && time > 50 && time <= 60) || (depth > 50 && depth <= 60 && time > 40 && time <= 50) || (depth > 60 && depth <= 70 && time > 35 && time <= 40) || (depth > 70 && depth <= 80 && time > 30 && time <= 35)) ? "h" : first_group;
    first_group = ((depth <= 40 && time > 80 && time <= 100) || (depth > 40 && depth <= 50 && time > 60 && time <= 70) || (depth > 50 && depth <= 60 && time > 50 && time <= 55) || (depth > 60 && depth <= 70 && time > 40 && time <= 45)) ? "i" : first_group;
    first_group = ((depth <= 40 && time > 100 && time <= 110) || (depth > 40 && depth <= 50 && time > 70 && time <= 80)) ? "j" : first_group;
    first_group = (depth <= 40 && time > 110 && time <= 130) ? "k" : first_group;

    // Errors
    if (first_group == "")
    {
        error.push("The time of your first dive is outside of no-decompression table limits, please input a lesser time.");
        time_label.className = "error";
        time_field.focus();
        output_errors(error);
        return false;
    }

    // Determine the second group based on first group and surface interval
    switch (first_group)
    {
        case "a":
            second_group = "a";
        break;

        case "b":
            second_group = (surface >= 201) ? "a" : "b";
        break;

        case "c":
            second_group = (surface >= 290) ? "a" : second_group;
            second_group = (surface < 290 && surface >= 100) ? "b" : second_group;
            second_group = (surface < 100) ? "c" : second_group;
        break;

        case "d":
            second_group = (surface >= 349) ? "a" : second_group;
            second_group = (surface < 349 && surface >= 159) ? "b" : second_group;
            second_group = (surface < 159 && surface >= 70) ? "c" : second_group;
            second_group = (surface < 70) ? "d" : second_group;
        break;

        case "e":
            second_group = (surface >= 395) ? "a" : second_group;
            second_group = (surface < 395 && surface >= 205) ? "b" : second_group;
            second_group = (surface < 205 && surface >= 118) ? "c" : second_group;
            second_group = (surface < 118 && surface >= 55) ? "d" : second_group;
            second_group = (surface < 55) ? "e" : second_group;
        break;

        case "f":
            second_group = (surface >= 426) ? "a" : second_group;
            second_group = (surface < 426 && surface >= 238) ? "b" : second_group;
            second_group = (surface < 238 && surface >= 149) ? "c" : second_group;
            second_group = (surface < 149 && surface >= 90) ? "d" : second_group;
            second_group = (surface < 90 && surface >= 46) ? "e" : second_group;
            second_group = (surface < 46) ? "f" : second_group;
        break;

        case "g":
            second_group = (surface >= 456) ? "a" : second_group;
            second_group = (surface < 456 && surface >= 266) ? "b" : second_group;
            second_group = (surface < 266 && surface >= 179) ? "c" : second_group;
            second_group = (surface < 179 && surface >= 120) ? "d" : second_group;
            second_group = (surface < 120 && surface >= 76) ? "e" : second_group;
            second_group = (surface < 76 && surface >= 41) ? "f" : second_group;
            second_group = (surface < 41) ? "g" : second_group;
        break;

        case "h":
            second_group = (surface >= 480) ? "a" : second_group;
            second_group = (surface < 480 && surface >= 290) ? "b" : second_group;
            second_group = (surface < 290 && surface >= 201) ? "c" : second_group;
            second_group = (surface < 201 && surface >= 144) ? "d" : second_group;
            second_group = (surface < 144 && surface >= 102) ? "e" : second_group;
            second_group = (surface < 102 && surface >= 67) ? "f" : second_group;
            second_group = (surface < 67 && surface >= 37) ? "g" : second_group;
            second_group = (surface < 37) ? "h" : second_group;
        break;

        case "i":
            second_group = (surface >= 502) ? "a" : second_group;
            second_group = (surface < 502 && surface >= 313) ? "b" : second_group;
            second_group = (surface < 313 && surface >= 224) ? "c" : second_group;
            second_group = (surface < 224 && surface >= 165) ? "d" : second_group;
            second_group = (surface < 165 && surface >= 123) ? "e" : second_group;
            second_group = (surface < 123 && surface >= 90) ? "f" : second_group;
            second_group = (surface < 90 && surface >= 60) ? "g" : second_group;
            second_group = (surface < 60 && surface >= 34) ? "h" : second_group;
            second_group = (surface < 34) ? "i" : second_group;
        break;

        case "j":
            second_group = (surface >= 531) ? "a" : second_group;
            second_group = (surface < 531 && surface >= 341) ? "b" : second_group;
            second_group = (surface < 341 && surface >= 243) ? "c" : second_group;
            second_group = (surface < 243 && surface >= 185) ? "d" : second_group;
            second_group = (surface < 185 && surface >= 141) ? "e" : second_group;
            second_group = (surface < 141 && surface >= 108) ? "f" : second_group;
            second_group = (surface < 108 && surface >= 80) ? "g" : second_group;
            second_group = (surface < 80 && surface >= 55) ? "h" : second_group;
            second_group = (surface < 55 && surface >= 32) ? "i" : second_group;
            second_group = (surface < 32) ? "j" : second_group;
        break;

        case "k":
            second_group = (surface >= 539) ? "a" : second_group;
            second_group = (surface < 539 && surface >= 349) ? "b" : second_group;
            second_group = (surface < 349 && surface >= 260) ? "c" : second_group;
            second_group = (surface < 260 && surface >= 202) ? "d" : second_group;
            second_group = (surface < 202 && surface >= 159) ? "e" : second_group;
            second_group = (surface < 159 && surface >= 124) ? "f" : second_group;
            second_group = (surface < 124 && surface >= 96) ? "g" : second_group;
            second_group = (surface < 96 && surface >= 72) ? "h" : second_group;
            second_group = (surface < 72 && surface >= 50) ? "i" : second_group;
            second_group = (surface < 50 && surface >= 29) ? "j" : second_group;
            second_group = (surface < 29) ? "k" : second_group;
        break;

        case "l":
            second_group = (surface >= 553) ? "a" : second_group;
            second_group = (surface < 553 && surface >= 363) ? "b" : second_group;
            second_group = (surface < 363 && surface >= 276) ? "c" : second_group;
            second_group = (surface < 276 && surface >= 217) ? "d" : second_group;
            second_group = (surface < 217 && surface >= 174) ? "e" : second_group;
            second_group = (surface < 174 && surface >= 140) ? "f" : second_group;
            second_group = (surface < 140 && surface >= 110) ? "g" : second_group;
            second_group = (surface < 110 && surface >= 86) ? "h" : second_group;
            second_group = (surface < 86 && surface >= 65) ? "i" : second_group;
            second_group = (surface < 65 && surface >= 46) ? "j" : second_group;
            second_group = (surface < 46 && surface >= 27) ? "k" : second_group;
            second_group = (surface < 27) ? "l" : second_group;
        break;
    }

    // Finally determine the RNT and AMDT based on the second group and the depth of the second dive
    switch (second_group)
    {
        case "a":
            rnt = (depth2 <= 40) ? "7" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "6" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "5" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "4" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "4" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "3" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "3" : rnt;
            rnt = (depth2 > 100 && depth2 <= 110) ? "3" : rnt;
            rnt = (depth2 > 110 && depth2 <= 120) ? "3" : rnt;
            rnt = (depth2 > 120 && depth2 <= 130) ? "3" : rnt;

            amdt = (depth2 <= 40) ? "123" : amdt;
            amdt = (depth2 > 40 && depth2 <= 50) ? "74" : amdt;
            amdt = (depth2 > 50 && depth2 <= 60) ? "50" : amdt;
            amdt = (depth2 > 60 && depth2 <= 70) ? "41" : amdt;
            amdt = (depth2 > 70 && depth2 <= 80) ? "31" : amdt;
            amdt = (depth2 > 80 && depth2 <= 90) ? "22" : amdt;
            amdt = (depth2 > 90 && depth2 <= 100) ? "19" : amdt;
            amdt = (depth2 > 100 && depth2 <= 110) ? "12" : amdt;
            amdt = (depth2 > 110 && depth2 <= 120) ? "9" : amdt;
            amdt = (depth2 > 120 && depth2 <= 130) ? "5" : amdt;
        break;

        case "b":
            rnt = (depth2 <= 40) ? "17" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "13" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "11" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "9" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "8" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "7" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "7" : rnt;
            rnt = (depth2 > 100 && depth2 <= 110) ? "6" : rnt;
            rnt = (depth2 > 110 && depth2 <= 120) ? "6" : rnt;
            rnt = (depth2 > 120 && depth2 <= 130) ? "6" : rnt;

            amdt = (depth2 <= 40) ? "113" : amdt;
            amdt = (depth2 > 40 && depth2 <= 50) ? "67" : amdt;
            amdt = (depth2 > 50 && depth2 <= 60) ? "44" : amdt;
            amdt = (depth2 > 60 && depth2 <= 70) ? "36" : amdt;
            amdt = (depth2 > 70 && depth2 <= 80) ? "27" : amdt;
            amdt = (depth2 > 80 && depth2 <= 90) ? "18" : amdt;
            amdt = (depth2 > 90 && depth2 <= 100) ? "15" : amdt;
            amdt = (depth2 > 100 && depth2 <= 110) ? "9" : amdt;
            amdt = (depth2 > 110 && depth2 <= 120) ? "6" : amdt;
        break;

        case "c":
            rnt = (depth2 <= 40) ? "25" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "21" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "17" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "15" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "13" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "11" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "10" : rnt;
            rnt = (depth2 > 100 && depth2 <= 110) ? "10" : rnt;
            rnt = (depth2 > 110 && depth2 <= 120) ? "9" : rnt;
            rnt = (depth2 > 120 && depth2 <= 130) ? "8" : rnt;

            amdt = (depth2 <= 40) ? "105" : amdt;
            amdt = (depth2 > 40 && depth2 <= 50) ? "59" : amdt;
            amdt = (depth2 > 50 && depth2 <= 60) ? "38" : amdt;
            amdt = (depth2 > 60 && depth2 <= 70) ? "30" : amdt;
            amdt = (depth2 > 70 && depth2 <= 80) ? "22" : amdt;
            amdt = (depth2 > 80 && depth2 <= 90) ? "12" : amdt;
            amdt = (depth2 > 90 && depth2 <= 100) ? "12" : amdt;
            amdt = (depth2 > 100 && depth2 <= 110) ? "5" : amdt;
        break;

        case "d":
            rnt = (depth2 <= 40) ? "37" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "29" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "24" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "20" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "18" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "16" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "14" : rnt;
            rnt = (depth2 > 100 && depth2 <= 110) ? "13" : rnt;
            rnt = (depth2 > 110 && depth2 <= 120) ? "12" : rnt;
            rnt = (depth2 > 120 && depth2 <= 130) ? "11" : rnt;

            amdt = (depth2 <= 40) ? "93" : amdt;
            amdt = (depth2 > 40 && depth2 <= 50) ? "51" : amdt;
            amdt = (depth2 > 50 && depth2 <= 60) ? "31" : amdt;
            amdt = (depth2 > 60 && depth2 <= 70) ? "25" : amdt;
            amdt = (depth2 > 70 && depth2 <= 80) ? "17" : amdt;
            amdt = (depth2 > 80 && depth2 <= 90) ? "9" : amdt;
            amdt = (depth2 > 90 && depth2 <= 100) ? "8" : amdt;
        break;

        case "e":
            rnt = (depth2 <= 40) ? "49" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "38" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "30" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "26" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "23" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "20" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "18" : rnt;
            rnt = (depth2 > 100 && depth2 <= 110) ? "16" : rnt;
            rnt = (depth2 > 110 && depth2 <= 120) ? "15" : rnt;
            rnt = (depth2 > 120 && depth2 <= 130) ? "13" : rnt;

            amdt = (depth2 <= 40) ? "81" : amdt;
            amdt = (depth2 > 40 && depth2 <= 50) ? "42" : amdt;
            amdt = (depth2 > 50 && depth2 <= 60) ? "25" : amdt;
            amdt = (depth2 > 60 && depth2 <= 70) ? "19" : amdt;
            amdt = (depth2 > 70 && depth2 <= 80) ? "12" : amdt;
            amdt = (depth2 > 80 && depth2 <= 90) ? "5" : amdt;
            amdt = (depth2 > 90 && depth2 <= 100) ? "4" : amdt;
        break;

        case "f":
            rnt = (depth2 <= 40) ? "61" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "47" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "36" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "31" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "28" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "24" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "22" : rnt;
            rnt = (depth2 > 100 && depth2 <= 110) ? "20" : rnt;
            rnt = (depth2 > 110 && depth2 <= 120) ? "18" : rnt;
            rnt = (depth2 > 120 && depth2 <= 130) ? "16" : rnt;

            amdt = (depth2 <= 40) ? "69" : amdt;
            amdt = (depth2 > 40 && depth2 <= 50) ? "33" : amdt;
            amdt = (depth2 > 50 && depth2 <= 60) ? "19" : amdt;
            amdt = (depth2 > 60 && depth2 <= 70) ? "14" : amdt;
            amdt = (depth2 > 70 && depth2 <= 80) ? "7" : amdt;
        break;

        case "g":
            rnt = (depth2 <= 40) ? "73" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "56" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "44" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "37" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "32" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "29" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "26" : rnt;
            rnt = (depth2 > 100 && depth2 <= 110) ? "24" : rnt;
            rnt = (depth2 > 110 && depth2 <= 120) ? "21" : rnt;
            rnt = (depth2 > 120 && depth2 <= 130) ? "19" : rnt;

            amdt = (depth2 <= 40) ? "57" : amdt;
            amdt = (depth2 > 40 && depth2 <= 50) ? "24" : amdt;
            amdt = (depth2 > 50 && depth2 <= 60) ? "11" : amdt;
            amdt = (depth2 > 60 && depth2 <= 70) ? "8" : amdt;
        break;

        case "h":
            rnt = (depth2 <= 40) ? "87" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "66" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "52" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "43" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "38" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "33" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "30" : rnt;
            rnt = (depth2 > 100 && depth2 <= 110) ? "27" : rnt;
            rnt = (depth2 > 110 && depth2 <= 120) ? "25" : rnt;
            rnt = (depth2 > 120 && depth2 <= 130) ? "22" : rnt;

            amdt = (depth2 <= 40) ? "43" : amdt;
            amdt = (depth2 > 40 && depth2 <= 50) ? "14" : amdt;
        break;

        case "i":
            rnt = (depth2 <= 40) ? "101" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "76" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "61" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "50" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "43" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "38" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "34" : rnt;
            rnt = (depth2 > 100 && depth2 <= 110) ? "31" : rnt;
            rnt = (depth2 > 110 && depth2 <= 120) ? "28" : rnt;
            rnt = (depth2 > 120 && depth2 <= 130) ? "25" : rnt;

            amdt = (depth2 <= 40) ? "29" : amdt;
            amdt = (depth2 > 40 && depth2 <= 50) ? "4" : amdt;
        break;

        case "j":
            rnt = (depth2 <= 40) ? "116" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "87" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "70" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "57" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "48" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "43" : rnt;
            rnt = (depth2 > 90 && depth2 <= 100) ? "38" : rnt;

            amdt = (depth2 <= 40) ? "14" : amdt;
        break;

        case "k":
            rnt = (depth2 <= 40) ? "138" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "99" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "79" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "64" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "54" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "47" : rnt;
        break;

        case "l":
            rnt = (depth2 <= 40) ? "161" : rnt;
            rnt = (depth2 > 40 && depth2 <= 50) ? "111" : rnt;
            rnt = (depth2 > 50 && depth2 <= 60) ? "88" : rnt;
            rnt = (depth2 > 60 && depth2 <= 70) ? "72" : rnt;
            rnt = (depth2 > 70 && depth2 <= 80) ? "61" : rnt;
            rnt = (depth2 > 80 && depth2 <= 90) ? "53" : rnt;
        break;
    }

    // Final error check
    if (!rnt)
    {
        error.push("Avoid repetetive dives over 100 ft.");
        depth2_label.className = "error";
        depth2_field.focus();
        output_errors(error);
        return false;
    }

    // Print the appropriate output

    // GCK mods, 4/5/2009. Print the dive groups.

    printed_answer = "Dive group after the first dive is " + first_group.toUpperCase() +".<br />Dive group after the surface interval is " + second_group.toUpperCase() +".";

    if (rnt && amdt)
    {
        printed_answer = printed_answer + "<br />Residual Nitrogen Time is " + rnt + " minutes.<br />Adjusted maximum no-decompression bottom time is " + amdt + " minutes.";
    }
    else if (rnt)
    {
        printed_answer = printed_answer + "<br />Residual Nitrogen Time is " + rnt + " minutes.<br />No second no-decompression dive is possible.";
    }
    else
    {
        printed_answer = printed_answer + "<br />No limit";
    }

    output_div.innerHTML = printed_answer;
    output_div.className = "result";
}

/**
* Displays errors from an array
*/
function output_errors(errors)
{
    errors = errors.join("<br />");
    output_div.innerHTML = errors;
    output_div.className = "error";
}

/**
* Removes error messages
*/
function remove_errors()
{
    output_div.innerHTML = "";

    var depth_label = document.getElementById(depth_label_id);
    var time_label = document.getElementById(time_label_id);
    var surface_label = document.getElementById(surface_label_id);
    var depth2_label = document.getElementById(depth2_label_id);

    depth_label.className = time_label.className = surface_label.className = depth2_label.className = "";
}

/**
* Loading is done
*/
function init()
{
  document.getElementById("depth").focus();
}
