--- regcode.module	2007-09-27 07:06:20.000000000 +0200
+++ regcode.module_changes	2008-12-10 10:15:48.000000000 +0100
@@ -78,14 +78,46 @@
  *   The settings form.
  */
 function regcode_admin_settings() {
+
+  $roles = user_roles();
+
+  $form['regcode_optional'] = array(
+      '#type' => 'checkbox',
+      '#title' => t("Make registration code optional"),
+      '#default_value' => variable_get('regcode_optional', 0),
+      '#description' => t('If checked, users can register without a registration code'),
+  );
+
+  $form['regcode_fieldtitle'] = array(
+      '#type' => 'textfield',
+      '#title' => t("Field title"),
+      '#description' => t('The title of the registration code textfield'),
+      '#default_value' => variable_get('regcode_fieldtitle',''),
+  );
+  $form['regcode_fielddescription'] = array(
+      '#type' => 'textfield',
+      '#title' => t("Field description"),
+      '#description' => t('The description of the registration code textfield'),
+      '#default_value' => variable_get('regcode_fielddescription',''),
+  );
+
   $form['regcode_codes'] = array(
-    '#type' => 'textarea',
-    '#title' => t('Registration code(s)'),
-    '#required' => TRUE,
-    '#default_value' => variable_get('regcode_codes', ''),
-    '#description' => t('If set, new users must correctly enter one of these
-      codes in order to register. Enter one code per line.'),
+    '#type' => 'fieldset',
+    '#title' => t('Registration codes'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
   );
+
+  foreach($roles as $rid => $role_name) {
+    $form['regcode_codes']["regcode_codes_$rid"] = array(
+      '#type' => 'textarea',
+      '#title' => t("Registration code(s) for $role_name"),
+      '#required' => FALSE,
+      '#default_value' => variable_get("regcode_codes_$rid", ''),
+      '#description' => t('If set, new users must correctly enter one of these
+      codes in order to register. Enter one code per line.'),
+    );
+  }
   $form = system_settings_form($form);
   return $form;
 }
@@ -106,35 +138,70 @@
  */
 function regcode_user($op, &$edit, &$account, $category = NULL) {
 
-  // Get the array of registration codes.
-  $regcodes = variable_get('regcode_codes', '');
+
+  // Retrieve the registration codes.
+  $roles = user_roles();
+  foreach($roles as $rid => $name) {
+    $rid_code = variable_get("regcode_codes_$rid", '');
+    if($rid_code) {
+	$regcodes[$rid] = $rid_code;
+    }
+  }
 
   // Only do the checking if the codes variable is set.
-  if (strlen(trim($regcodes))) {
+  if (!empty($regcodes)) {
     switch ($op) {
 
       case 'register':
+
         // Inject the registration code field into the registration form.
         $form['regcode_code'] = array(
           '#type' => 'textfield',
-          '#title' => t('Registration Code'),
-          '#required' => TRUE,
-          '#description' => t('Please enter your registration code.')
+          '#title' => t(variable_get('regcode_fieldtitle','Registration Code')),
+          '#description' => t(variable_get('regcode_fielddescription','Please enter your registration code.')),
+          '#required' => variable_get('regcode_optional', 0) ? FALSE : TRUE,
         );
         return $form;
 
       case 'validate':
+
         if (($category == 'account') && (!$account->uid)) {   
-          // Make sure that the entered code is in the list.
-          $regcodes = explode("\n", $regcodes);
-          array_walk($regcodes, create_function('&$a', '$a = trim($a);'));
-          if (!in_array(trim($edit['regcode_code']), $regcodes)) {
-            form_set_error('regcode_code', t('Invalid registration code'));
-            watchdog('regcode', t('User entered invalid registration code: ') .
-            $edit['regcode_code'], WATCHDOG_WARNING);
+          // Make sure that the entered code exists (if option is set)
+          if(!variable_get('regcode_optional', 0)) {
+            $valid = false;
+            foreach($regcodes as $rid => $codes) {
+              $codes = explode("\r\n", $codes);
+              array_walk($regcodes, create_function('&$a', '$a = trim($a);'));
+              if(in_array(trim($edit['regcode_code']), $codes)) {
+                  $valid = true;
+		  break;
+              }
+            }
+
+            if(!$valid) {
+              form_set_error('regcode_code', t('Invalid !title', 
+                              array( '!title' => variable_get('regcode_fieldtitle','registration code'))));
+              watchdog('regcode', t('User entered invalid registration code: ') .
+              $edit['regcode_code'], WATCHDOG_WARNING);
+            }
           }
         }
         break;
+
+      case  'insert':
+
+        // Give the right roles
+        $cur_roles = array();
+        foreach($regcodes as $rid => $codes) {
+          $codes = explode("\r\n", $codes);
+          array_walk($regcodes, create_function('&$a', '$a = trim($a);'));
+          if(in_array(trim($edit['regcode_code']), $codes)) {
+               //$cur_roles[] = $rid;
+               $edit['roles'][$rid] = array();
+          }
+       }
+       break;
+
     } // switch
   }
 }
