$(function() {
    $('.required').hide();
    $("#CacheLocation").select().focus();

    $('#submit_image').click(function() {
        $('.required').hide();

        var valid = true;
        var CacheLocation = $('#CacheLocation').val(); // get the cache location value
        var DateFound = $('#DateFound').val(); // get the date found value
        var Comments = $('#Comments').val() // Get the comments

        // validate cache location
        if (CacheLocation == '') {
            $('#cacheRequired').show(); // show the required label
            $('input#CacheLocation').focus();
            valid = false;
        }

        // validate date found
        if (DateFound == '') {
            $('#dateRequired').show(); // show the required label
            if (valid) { // Only set the focus to the date if the cache location is valid.
                $('input#DateFound').focus();
            }
            valid = false;
        }

        if (valid) {
            $('#geocache_form').submit();
        } else {
            return false;
        }
    });
});
