=== modified file 'django/contrib/admin/media/js/SelectBox.js'
--- django/contrib/admin/media/js/SelectBox.js	2006-12-19 04:35:09 +0000
+++ django/contrib/admin/media/js/SelectBox.js	2007-01-21 20:35:30 +0000
@@ -3,18 +3,19 @@
     init: function(id) {
         var box = document.getElementById(id);
         var node;
-        SelectBox.cache[id] = new Array();
-        var cache = SelectBox.cache[id];
-        for (var i = 0; (node = box.options[i]); i++) {
-            cache.push({ value: node.value, text: node.text, displayed: 1 });
+        SelectBox.cache[id] = new Object();
+        var boxCache = SelectBox.cache[id];
+        for (var i=0; (node=box.options[i]); i++) {
+            boxCache[node.value] = {value: node.value, text: node.text, displayed: 1};
         }
     },
     redisplay: function(id) {
         // Repopulate HTML select box from cache
         var box = document.getElementById(id);
         box.options.length = 0; // clear all options
-        for (var i = 0, j = SelectBox.cache[id].length; i < j; i++) {
-            var node = SelectBox.cache[id][i];
+        var elementsArray = SelectBox.sort(id);
+        for (var i=0; i<elementsArray.length; i++ ) {
+            var node = elementsArray[i]; //SelectBox.cache[id][nodeValue];
             if (node.displayed) {
                 box.options[box.options.length] = new Option(node.text, node.value, false, false);
             }
@@ -25,9 +26,10 @@
         // the words in text. (It's an AND search.)
         var tokens = text.toLowerCase().split(/\s+/);
         var node, token;
-        for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
+        for (var nodeValue in SelectBox.cache[id]) {
+            node = SelectBox.cache[id][nodeValue];
             node.displayed = 1;
-            for (var j = 0; (token = tokens[j]); j++) {
+            for (var j=0; (token=tokens[j]); j++) {
                 if (node.text.toLowerCase().indexOf(token) == -1) {
                     node.displayed = 0;
                 }
@@ -36,39 +38,21 @@
         SelectBox.redisplay(id);
     },
     delete_from_cache: function(id, value) {
-        var node, delete_index = null;
-        for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
-            if (node.value == value) {
-                delete_index = i;
-                break;
-            }
-        }
-        var j = SelectBox.cache[id].length - 1;
-        for (var i = delete_index; i < j; i++) {
-            SelectBox.cache[id][i] = SelectBox.cache[id][i+1];
-        }
-        SelectBox.cache[id].length--;
+        delete SelectBox.cache[id][value];
     },
     add_to_cache: function(id, option) {
-        SelectBox.cache[id].push({ value: option.value, text: option.text, displayed: 1 });
+        SelectBox.cache[id][option.value] = {value: option.value, text: option.text, displayed: 1};
     },
     cache_contains: function(id, value) {
-        // Check if an item is contained in the cache
-        var node;
-        for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
-            if (node.value == value) {
-                return true;
-            }
-        }
-        return false;
+        return SelectBox.cache[id].hasOwnProperty(value);
     },
     move: function(from, to) {
         var from_box = document.getElementById(from);
         var to_box = document.getElementById(to);
         var option;
-        for (var i = 0; (option = from_box.options[i]); i++) {
+        for (var i=0; (option=from_box.options[i]); i++) {
             if (option.selected && SelectBox.cache_contains(from, option.value)) {
-                SelectBox.add_to_cache(to, { value: option.value, text: option.text, displayed: 1 });
+                SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1});
                 SelectBox.delete_from_cache(from, option.value);
             }
         }
@@ -79,15 +63,21 @@
         var from_box = document.getElementById(from);
         var to_box = document.getElementById(to);
         var option;
-        for (var i = 0; (option = from_box.options[i]); i++) {
-            SelectBox.add_to_cache(to, { value: option.value, text: option.text, displayed: 1 });
+        for (var i=0; (option=from_box.options[i]); i++) {
+            SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1});
             SelectBox.delete_from_cache(from, option.value);
         }
         SelectBox.redisplay(from);
         SelectBox.redisplay(to);
     },
     sort: function(id) {
-        SelectBox.cache[id].sort( function(a, b) {
+        var cache = SelectBox.cache[id];
+        var array = new Array();
+        for (optionValue in cache) {
+            var option = cache[optionValue];
+            array.push(option);
+        }
+        array.sort(function(a, b) {
             a = a.text.toLowerCase();
             b = b.text.toLowerCase();
             try {
@@ -98,11 +88,12 @@
                 // silently fail on IE 'unknown' exception
             }
             return 0;
-        } );
+        });
+        return array;
     },
     select_all: function(id) {
         var box = document.getElementById(id);
-        for (var i = 0; i < box.options.length; i++) {
+        for (var i=0; i<box.options.length; i++) {
             box.options[i].selected = 'selected';
         }
     }

