﻿


var myArray = new Array();
var myArray2 = new Array();
var Counter = 0;

function CreateArray() {
    myArray = new Array();
    myArray[0] = document.getElementById("ls");
    myArray[1] = document.getElementById("lm");
    myArray[2] = document.getElementById("lh");
    myArray[3] = document.getElementById("ld");
    myArray[4] = document.getElementById("m3s");
    myArray[5] = document.getElementById("m3m");
    myArray[6] = document.getElementById("m3h");
    myArray[7] = null;
    myArray[8] = document.getElementById("ft3s");
    myArray[9] = document.getElementById("ft3m");
    myArray[10] = document.getElementById("ft3h");
    myArray[11] = null;
    myArray[12] = document.getElementById("USgals");
    myArray[13] = document.getElementById("USgalm");
    myArray[14] = document.getElementById("USgalh");

    myArray2 = new Array();
    myArray2[0] = document.getElementById("mi");
    myArray2[1] = document.getElementById("fi");

    Counter = 0;
}

function Calculate(frm_field, name, mult_str) {

    var mult = "";                             //Initialize single parameter string.
    var index = "";                             //Initialize location of comma.
    var roundvar;
    CreateArray();
    if (FieldCheck(frm_field, name) == true) {
        while (Counter < myArray.length) {
            if (myArray[Counter] != null) {
                index = mult_str.indexOf(",");
                mult = mult_str.substring(0, index);
                mult_str = mult_str.substring(index + 1);
                myArray[Counter].value = frm_field.value * parseFloat(mult);
                if (parseFloat(mult) == 1) {
                    myArray[Counter].style.backgroundColor = "yellow";
                }
                else {
                    myArray[Counter].style.backgroundColor = "white";
                }
            }
            else {
                index = mult_str.indexOf(",");
                mult = mult_str.substring(0, index);
                mult_str = mult_str.substring(index + 1);
            }
            Counter++;
        }
    }
}

function Calculate2(frm_field, name, mult_str) {

    var mult = "";                             //Initialize single parameter string.
    var index = "";                             //Initialize location of comma.
    var roundvar;
    CreateArray();
    if (FieldCheck(frm_field, name) == true) {
        while (Counter < myArray2.length) {
            index = mult_str.indexOf(",");
            mult = mult_str.substring(0, index);
            mult_str = mult_str.substring(index + 1);
            myArray2[Counter].value = frm_field.value * parseFloat(mult);
            if (parseFloat(mult) == 1) {
                myArray2[Counter].style.backgroundColor = "yellow";
            }
            else {
                myArray2[Counter].style.backgroundColor = "white";
            }

            Counter++;
        }
    }
}

function FieldCheck(frm_field, name) {
    var InputString = frm_field.value;
    var ValidChr = "1234567890-eE.";
    var Valid = true;
    var decimals = 0;
    var dashes = 0;
    var commas = 0;

    for (ctr_is = 0; ctr_is < InputString.length; ctr_is++) {
        chr = InputString.charAt(ctr_is);
        for (ctr_vc = 0; ctr_vc < ValidChr.length; ctr_vc++)
            if (chr == ValidChr.charAt(ctr_vc))
        // Character is valid.
            break;
        if (ctr_vc == ValidChr.length) {
            // Character is not found in the valid list.
            Valid = false;
            break;
        }
        if (chr == ".")
        // Count the number of decimals.
            decimals++;
        if (chr == "-")
        // Count the number of dashes.
            dashes++;
        if (chr == ",")
        // Count the number of commas.
            commas++;
    }

    if (!Valid) {
        alert("Please enter digits in the \"" + name + "\" field (ie. 0123456789-.e).");
        return (false);
    }

    if (decimals > 1) {
        alert("The \"" + name + "\" field contains more than one decimal.");
        return (false);
    }

    if (dashes > 1) {
        alert("The \"" + name + "\" field contains more than one dash.");
        return (false);
    }

    if (commas > 0) {
        alert("The \"" + name + "\" field cannot contain commas.");
        return (false);
    }

    if (InputString.length == 0) {
        alert("The \"" + name + "\" field is empty.");
        return (false);
    }
    return (true);
}

function Reset_onclick() {
    CreateArray();
    while (Counter < myArray.length) {
        if (myArray[Counter] != null) {
            myArray[Counter].style.backgroundColor = "white";
        }
        Counter++;
    }
    Counter = 0;
    while (Counter < myArray2.length) {
        myArray2[Counter].style.backgroundColor = "white";
        Counter++;
    }
}
