--- og_mandatory_group.module	2008-09-06 13:50:27.000000000 -0700
+++ og_mandatory_groupNEW.module	2010-10-24 23:06:48.000000000 -0700
@@ -40,12 +40,49 @@ function og_mandatory_group_form_alter(&
         $form['og_register'] = array_reverse($form['og_register']);
       }
     }
-    if ($group_count > 0 && variable_get('og_mandatory_additional_group', FALSE)) {
+    $max_groups = variable_get('og_mandatory_maximum_groups', 0);
+    if ($group_count > 0 && $max_groups && !variable_get('og_mandatory_additional_group', FALSE)) {
+      if ($max_groups == 1) {
+        if ($mandatory_group && $mandatory_group_in_form) {
+        $form['og_register']['maximum'] = array('#value' => t('You may join one additional (non-mandatory) group.'),);
+        }
+        else {
+          $form['og_register']['maximum'] = array('#value' => t('You may join one group.'),);
+        }
+      }
+      else{
+        if ($mandatory_group && $mandatory_group_in_form) {
+          $form['og_register']['maximum'] = array('#value' => t('You may join up to @max_groups additional (non-mandatory) groups.', array('@max_groups' => $max_groups)),);
+        }
+        else {
+          $form['og_register']['maximum'] = array('#value' => t('You may join up to @max_groups groups.', array('@max_groups' => $max_groups)),);
+        }
+      }
+    }
+    elseif ($group_count > 0 && $max_groups && variable_get('og_mandatory_additional_group', FALSE)) {
+      if ($max_groups == 1) {
+        if ($mandatory_group && $mandatory_group_in_form) {
+          $form['og_register']['minimum'] = array('#value' => t('You must join one additional (non-mandatory) group.'),);
+        }
+        else {
+          $form['og_register']['minimum'] = array('#value' => t('You must join a group.'),);
+        }
+      }
+      else {
+        if ($mandatory_group && $mandatory_group_in_form) {
+          $form['og_register']['minimum'] = array('#value' => t('You must join between one and @max_groups additional (non-mandatory) groups.', array('@max_groups' => $max_groups)),);
+        }
+        else {
+          $form['og_register']['minimum'] = array('#value' => t('You must join between one and @max_groups groups.', array('@max_groups' => $max_groups)),);
+        }
+      }
+    }
+    elseif ($group_count > 0 && variable_get('og_mandatory_additional_group', FALSE)) {
       if ($mandatory_group && $mandatory_group_in_form) {
-        $form['og_register']['minimum'] = array ('#value' => t('You must join at least one additional (non-mandatory) group.'),);
+        $form['og_register']['minimum'] = array('#value' => t('You must join at least one additional (non-mandatory) group.'),);
       }
       else {
-        $form['og_register']['minimum'] = array ('#value' => t('You must join at least one group.'),);
+        $form['og_register']['minimum'] = array('#value' => t('You must join at least one group.'),);
       }
     }
   }
@@ -65,10 +102,21 @@ function og_mandatory_group_user($op, &$
       if (isset($edit['og_register']) && variable_get('og_mandatory_additional_group', FALSE)) { //only present during registration
         if (count($edit['og_register']) > 0 && count(array_filter($edit['og_register'])) < 1) {
           if ($edit['og_mandatory_in_form']) {
-            form_set_error('og_register', "You must join at least one group in addition to the mandatory group");
+            form_set_error('og_register', t('You must join at least one group in addition to the mandatory group');
+          }
+          else {
+            form_set_error('og_register', t('You must join at least one group'));
+          }
+        }
+      }
+      $max_groups = variable_get('og_mandatory_maximum_groups', 0);
+      if (isset($edit['og_register']) && $max_groups) {
+        if (count(array_filter($edit['og_register'])) > $max_groups) {
+          if ($max_groups == 1) {
+            form_set_error('og_register', t('You may only join one group in addition to the mandatory group'));
           }
           else {
-            form_set_error('og_register', "You must join at least one group");
+            form_set_error('og_register', t('You may only join a maximum of @max_groups groups', array('@max_groups' => $max_groups)));
           }
         }
       }
@@ -183,6 +231,21 @@ function og_mandatory_group_settings() {
       '#title' => t('Require new users to join at least one group in addition to any mandatory group'),
       '#default_value' => variable_get('og_mandatory_additional_group', FALSE),
   );
+  $form['og_mandatory_maximum_groups'] = array(
+    '#type' => 'select', '#title' => t('Maximum number of non-mandatory groups new users may join on the registration screen.'), '#default_value' => variable_get('og_mandatory_maximum_groups', 0),
+    '#options' => array(
+      0 => t('Unlimited'),
+      1 => t('1 group'),
+      2 => t('2 groups'),
+      3 => t('3 groups'),
+      4 => t('4 groups'),
+      5 => t('5 groups'),
+      6 => t('6 groups'),
+      7 => t('7 groups'),
+      8 => t('8 groups'),
+      9 => t('9 groups'),
+    ),
+  );
   $form['#validate'][] = 'og_mandatory_group_settings_validate';
   return system_settings_form($form);
 }
