                    (function() {
                        var form = document.getElementById('paintops-pair-existing-form');
                        if (!form) return;
                        var beforeSel = form.querySelector('select[name="pair_before_photo_id"]');
                        var afterSel = form.querySelector('select[name="pair_after_photo_id"]');
                        if (!beforeSel || !afterSel) return;

                        // Whatever photo is picked on one side becomes
                        // un-pickable on the other, so the same photo can
                        // never be submitted as both Before and After.
                        function syncDisabled(changed, other) {
                            for (var i = 0; i < other.options.length; i++) {
                                var opt = other.options[i];
                                opt.disabled = opt.value !== '' && opt.value === changed.value;
                            }
                            if (other.value !== '' && other.value === changed.value) {
                                other.value = '';
                            }
                        }

                        beforeSel.addEventListener('change', function() { syncDisabled(beforeSel, afterSel); });
                        afterSel.addEventListener('change', function() { syncDisabled(afterSel, beforeSel); });
                    })();
