--- email_registration.module	2008-11-27 19:01:36.000000000 -0500
+++ email_registration.module	2009-01-29 21:39:47.281250000 -0500
@@ -8,19 +8,29 @@
 
 function email_registration_user($op, &$edit, &$account, $category = NULL) {
   switch ($op) {
-    case 'insert':
-    case 'update':
-    $namenew = variable_get('email_registration_eq_email',FALSE) ? $edit['mail'] : preg_replace('/@.*$/', '', $edit['mail']);
-    // if username generated from email record already exists, append underscore and number eg:(chris_123)
-    if (db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) {
-      // find the next number available to append to the name
-      $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '^%s_[0-9]+$' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1";
-      $nameidx = db_result(db_query($sql, $namenew));
-      $namenew .= '_'. ($nameidx + 1);
-    }
-    // replace with generated username
-    db_query("UPDATE {users} SET name = '%s' WHERE uid = '%s'", $namenew, $account->uid);
-  break;
+    case 'validate':
+      global $form_values;
+      // skips validation for super-admin (uid=1)
+      if ($form_values['_account']->uid != 1) {
+        // if the flag is TRUE, then just set $namenew to the email and move on
+        if (variable_get('email_registration_eq_email',FALSE)) {
+          $namenew = $form_values['mail'];
+        }
+        // if the flag is FALSE, then proceed with the necessary DB queries
+        else {
+          $namenew = preg_replace('/@.*$/', '', $form_values['mail']);
+          // if username generated from email record already exists, append underscore and number eg:(chris_123)
+          if (db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) {
+            // find the next number available to append to the name
+            $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '^%s_[0-9]+$' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1";
+            $nameidx = db_result(db_query($sql, $namenew));
+            $namenew .= '_'. ($nameidx + 1);
+          }
+        }
+        // replace with generated username
+        $form_values['name'] = $namenew;
+      }
+    break;
   }
   return;
 }
@@ -32,8 +42,11 @@ function email_registration_user($op, &$
 
 function email_registration_form_alter($form_id, &$form) {
   switch ($form_id) {
+    case 'user_edit':
+      if (!variable_get('email_registration_eq_email',FALSE))
+        break;
     case 'user_register':
-//      if (module_exists('profile')) {
+    if ($form['_account']['#value']->uid != 1) {
       if ((!isset($form['name'])) && (isset($form['account']))) {
         $form['account']['name']['#type'] = 'hidden';
         $form['account']['name']['#value'] = user_password();
@@ -44,6 +57,7 @@ function email_registration_form_alter($
         $form['name']['#value'] = user_password();
         $form['mail']['#title'] = t('E-mail');
       }
+      }
       break;
 
     case 'user_pass':
