Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.225
diff -u -p -r1.225 form.inc
--- includes/form.inc	14 Sep 2007 10:40:55 -0000	1.225
+++ includes/form.inc	18 Sep 2007 01:41:06 -0000
@@ -1962,7 +1962,8 @@ function _form_set_class(&$element, $cla
 }
 
 /**
- * Remove invalid characters from an HTML ID attribute string.
+ * Prepare an HTML ID attribute string by removing invalid characters and
+ * guaranteeing uniqueness.
  *
  * @param $id
  *   The ID to clean.
@@ -1970,8 +1971,19 @@ function _form_set_class(&$element, $cla
  *   The cleaned ID.
  */
 function form_clean_id($id = NULL) {
+  static $seen_ids = array(); //Maintain list of seen IDs.
   $id = str_replace(array('][', '_', ' '), '-', $id);
-  return $id;
+  //In order to guarantee uniqueness, we must check to see if we have seen
+  //this ID before. The first time we see an ID, we leave it alone. Subsequent
+  //sightings get a number appended to the end.
+  $unique = $id;
+  while(isset($seen_ids[$unique])) {
+    $seen_ids[$id]++;
+    $unique = $id .'-'. $seen_ids[$id];
+  }
+  //Track every single ID we return, so we're never tricked into issuing dupes.
+  $seen_ids[$unique] = 0;
+  return $unique;
 }
 
 /**
