// Common JavaScript code across your application goes here.

      $(document).ready(function() {

        // Display the appropriate registration fields
        // -------------------------------------------
        if ( $("#standard_reg").is(":checked") ) {
          $("#split_reg_fields").hide();
        }
        else {
          $("#std_reg_field").hide();
        }

        $("#standard_reg").click(function() {
          $("#std_reg_field").show(); $("#split_reg_fields").hide();
        });

        $("#split_reg").click(function() {
          $("#std_reg_field").hide(); $("#split_reg_fields").show();
        });

        // Display the appropriate location fields
        // -------------------------------------------
        if ( $("#regatta_select").is(":checked") ) {
          $("#to_location_fields").hide();
        }

        $("#regatta_select").click(function() {
          $("#to_location_fields").hide();
          $("#start_location_label").text('Location:');
        });

        $("#distance_select").click(function() {
          $("#to_location_fields").show();
          $("#start_location_label").text('Start Location:');
        });

        // Display the registration fields
        // -------------------------------
        if ( !$("#craw_event_select").is(":checked") ) {
          $("#registration_section").hide();
          $("#other_info_field").hide();
          $("#publish_detail_controls").hide();
        }

        $("#craw_event_select").click(function() {
          if ( $("#craw_event_select").is(":checked") ) {
            $("#registration_section").show();
            $("#other_info_field").show();
            $("#publish_detail_controls").show();
          } else {
            $("#registration_section").hide();
            $("#other_info_field").hide();
            $("#publish_detail_controls").hide();
          };
        });


        // Update the regatta schedule when a new year is selected
        // -------------------------------------------------------
        $("select#schedule_year").change(function () {
          $("form#schedule_select_form").submit();
        });


        // Toggle the national and regional event checkboxes
        // -------------------------------------------------
        $("#regional_event_select").click(function() {
          if ( $("#regional_event_select").is(":checked") ) {
            $("#national_event_select").attr('checked', false);
          };
        });

        $("#national_event_select").click(function() {
          if ( $("#national_event_select").is(":checked") ) {
            $("#regional_event_select").attr('checked', false);
          };
        });

        // Display the appropriate schedule entries
        // ----------------------------------------
        $("tr[class*=craw]").hide();
        $("tr[class*=regional]").hide();
        $("tr[class*=national]").hide();
        if ( $("#craw_event_display").is(":checked") ) {
          $("tr[class*=craw]").show();
        };
        if ( $("#regional_event_display").is(":checked") ) {
          $("tr[class*=regional]").show();
        };
        if ( $("#national_event_display").is(":checked") ) {
          $("tr[class*=national]").show();
        };

        $("#craw_event_display").click(function() {
          if ( $("#craw_event_display").is(":checked") ) {
            $("tr[class*=craw]").show();
          } else {
            $("tr[class*=craw]").hide();
            if ( $("#regional_event_display").is(":checked") ) {
              $("tr[class*=regional]").show();
            };
            if ( $("#national_event_display").is(":checked") ) {
              $("tr[class*=national]").show();
            };
          };
        });

        $("#regional_event_display").click(function() {
          if ( $("#regional_event_display").is(":checked") ) {
            $("tr[class*=regional]").show();
          } else {
            $("tr[class*=regional]").hide();
            if ( $("#craw_event_display").is(":checked") ) {
              $("tr[class*=craw]").show();
            };
            if ( $("#national_event_display").is(":checked") ) {
              $("tr[class*=national]").show();
            };
          };
        });

        $("#national_event_display").click(function() {
          if ( $("#national_event_display").is(":checked") ) {
            $("tr[class*=national]").show();
          } else {
            $("tr[class*=national]").hide();
            if ( $("#craw_event_display").is(":checked") ) {
              $("tr[class*=craw]").show();
            };
            if ( $("#regional_event_display").is(":checked") ) {
              $("tr[class*=regional]").show();
            };
          };
        });


        // Auto-submit the registration form
        // ---------------------------------
        $("form#paypal_form").submit();


        // Membership options
        // ------------------
        $("#full_membership").click(function() {
          if ( $("#full_membership").is(":checked") ) {
            $("#associate_membership").attr('checked', false);
          };
        });

        $("#associate_membership").click(function() {
          if ( $("#associate_membership").is(":checked") ) {
            $("#full_membership").attr('checked', false);
          };
        });


        // Submit an ajax request to delete a regatta image
        // ------------------------------------------------
        $("[id^='remove_image']").click( function() {
          if (confirm("You're about to delete an image from the server - ok?"))
            var photo_id = $(this).attr('id').replace(/remove_image_/,'');
            $.post('/Attachments/destroy/' + photo_id, function(data){
              $('#existing_photo_' + photo_id).remove();
            });
          return false;
        });


      });

