function doOnLoad(theForm) {

    setDefaultValues(theForm);

    var count = 0;

    while (eval("theForm.byear" + count)) {

        updateAge (theForm, count);
        count++;
    }

    theForm.first_name.focus();
}

function updateAge (theForm, count) {

    var cutOffYear = theForm.cutOffYear.value;
    var cutOffMonth = theForm.cutOffMonth.value;
    var cutOffDay = theForm.cutOffDay.value;
    var bYear = eval("theForm.byear" + count).value;

    if (bYear != "") {

        var bMonth = eval("theForm.bmonth" + count).value - 1;
        var bDay = eval("theForm.bday" + count).value;
        var age = cutOffYear - bYear;

        if (cutOffMonth < bMonth) {

             age--;
        }

        if (cutOffMonth == bMonth && cutOffDay < bDay) {

             age--;
        }

        if (age > 0 && age < 99) {

            eval("theForm.age" + count).value = age;

        } else {

            eval("theForm.age" + count).value = "";
        }

        updateLeague(theForm, count, age);
    }
}

function updateLeague (theForm, count, age) {

    var league;

    // Set this by using age: 4,5 inst; 6,7 4v4; 8,9,10 6v6; 11+ senior
    if (age == 4 || age == 5) {

        league = "Instructional";

    } else if (age == 6 || age == 7) {

        league = "4v4";

    } else if (age >= 8 && age <= 10) {

        league = "6v6";

    } else if (age >= 11 & age < 99) {

        league = "Senior";

    } else {

        league = "";
    }

    eval("theForm.league" + count).value = league;
}

function setDefaultValues (theForm) {

    if (theForm.city.value == "") {

        theForm.city.value = "Litchfield";
    }

    if (theForm.state.value == "") {

        theForm.state.value = "NH";
    }

    if (theForm.zip.value == "") {

        theForm.zip.value = "03052";
    }

    if (theForm.last_name.value != "") {

        setLastNames (theForm, theForm.last_name.value);
    }
}

function setLastNames (theForm, name) {

    if (theForm.last_name2.value == "") {

        theForm.last_name2.value = name;
    }

    var count = 0;

    while (eval("theForm.lname" + count)) {

        var lname = eval("theForm.lname" + count);

        if (lname.value == "") {

            lname.value = name;
        }

        count++;
    }
}

function validateEntries (theForm) {

    if (!(theForm.first_name.value != "" && theForm.last_name.value != "") &&
        !(theForm.first_name2.value != "" && theForm.last_name2.value != "")) {

        alert("Need at least one parent's first and last name");
        return;
    }

    if (theForm.phone.value == "" && theForm.phone2.value == "") {

        alert("Need at least one phone number");
        return;
    }

    if (theForm.address1.value == "") {

        alert("Need a street");
        return;
    }

    if (theForm.city.value == "") {

        alert("Need a city/town");
        return;
    }

    if (theForm.state.value == "") {

        alert("Need a state");
        return;
    }

    if (theForm.zip.value == "") {

        alert("Need a zip");
        return;
    }

    if (theForm.econtact.value == "") {

        alert("Need an emergency contact");
        return;
    }

    if (theForm.ephone.value == "") {

        alert("Need an emergency contact phone number");
        return;
    }

    if (theForm.physician.value == "") {

        alert("Need a physician contact");
        return;
    }

    if (theForm.pphone.value == "") {

        alert("Need a physician contact phone number");
        return;
    }

    var count = 0;

    while (eval("theForm.league" + count)) {

        if (eval("theForm.fname" + count).value == "") {

            alert("Player " + (count + 1) + ": needs a first name");
            return;
        }

        if (eval("theForm.lname" + count).value == "") {

            alert("Player " + (count + 1) + ": needs a last name");
            return;
        }

        gender = null;

        for (var i = eval("theForm.gender" + count).length - 1; i > -1; i--) {

            if (eval("theForm.gender" + count)[i].checked) {

                gender = i;
                break;
            }
        }

        if (gender == null) {

            alert("Player " + (count + 1) + ": select a gender");
            return;
        }

        if (eval("theForm.league" + count).value == "") {

            alert("Player " + (count + 1) + ": needs a valid birth date");
            return;
        }

        count++;
    }

    if (!theForm.release.checked) {

        alert("Must check box for LYSL Release");
        return;
    }

    if (!theForm.medical.checked) {

        alert("Must check box for Medical Consent");
        return;
    }

    theForm.submit();
}

function addingPlayer(theForm) {

    theForm.command.value = "addPlayer";
    theForm.submit();
}

function removingPlayer(theForm, player) {

    var str = "theForm.fname" + player;
    var first = eval(str).value;
    str = "theForm.lname" + player;
    var last = eval(str).value;

    if (confirm("Remove player " + first + " " + last + "?")) {

        theForm.command.value = "removePlayer";
        theForm.remove.value = player;
        theForm.submit();
    }
}