--- logintoboggan/logintoboggan.module	2007-07-11 20:18:47.000000000 -0500
+++ logintoboggan_hacked/logintoboggan.module	2007-12-22 13:03:00.593750000 -0500
@@ -213,7 +213,28 @@ function logintoboggan_form_alter($form_
           $form['pass']['#description'] = t('Please choose a password for your account; it must be !length 30 characters and spaces are not allowed.', array('!length' => $length));
         }
       }
-      break;
+      //Should we hide the username field
+      if(variable_get('email_as_username', 0)) {
+        $form['account']['name']['#type'] = 'hidden';
+        //When the form is submitted, we populate the username value with the email address.
+        if($form['#post']['mail']) {
+          $form['#post']['name'] = $form['#post']['mail'];
+        }
+      }
+      //If the registration redirect is set to previous, we need to save the referring page
+      if(variable_get('toboggan_redirect_on_register', '') == '<previous>' ) {
+        if($form['#post']['referring_page']) {
+          $form['toboggan']['referring_page'] = array('#type' => 'hidden',
+            '#value' => $form['#post']['referring_page'],
+          );
+        }
+        elseif(!stristr($_SERVER['HTTP_REFERRER'], 'user/register')) {
+          $form['toboggan']['referring_page'] = array('#type' => 'hidden',
+            '#value' => urlencode(filter_xss_bad_protocol($_SERVER['HTTP_REFERER']))
+          );
+        }
+      }
+    break;
   }
 }
 
@@ -304,7 +325,7 @@ function logintoboggan_user_register_sub
   drupal_set_message($message);
 
   // where do we need to redirect after registration?
-  $redirect = _logintoboggan_process_redirect(variable_get('toboggan_redirect_on_register', ''), $account);
+  $redirect = _logintoboggan_process_redirect(variable_get('toboggan_redirect_on_register', ''), $account, $form_values['referring_page']);
 
   //log the user in if they created the account and immediate login is enabled.
   if($reg_pass_set) {
@@ -566,6 +587,12 @@ function logintoboggan_main_settings() {
     '#description' => t('Users will be able to enter EITHER their username OR their e-mail address to log in. NOTE: This will disallow users from registering using an e-mail address as their username.'),
     );
 
+  $form['login']['email_as_username'] = array('#type' => 'checkbox',
+    '#title' => t('Use email address as username'),
+    '#default_value' => variable_get('email_as_username', FALSE),
+    '#description' => t('This removes the username field from the user registration page, and set the username to the users email address'),
+    );
+  
   $form['registration'] = array('#type' => 'fieldset',
     '#title' => t('Registration'),
     );
@@ -604,7 +631,7 @@ function logintoboggan_main_settings() {
     '#type' => 'textfield',
     '#title' => t('Redirect path on Registration'),
     '#default_value' => variable_get('toboggan_redirect_on_register', ''),
-    '#description' => t('Normally, after a user registers a new account, they will be taken to the front page, or to their user page if you specify <cite>Immediate login</cite> above. Leave this setting blank if you wish to keep the default behavior. If you wish the user to go to a page of your choosing, then enter the path for it here. For instance, you may redirect them to a static page such as <cite>node/35</cite>, or to the <cite>&lt;front&gt;</cite> page. You may also use <em>%uid</em> as a variable, and the user\'s user ID will be substituted in the path. <em>NOTE: this feature will not work if you have the user password creation feature disabled.</em>'),
+    '#description' => t('Normally, after a user registers a new account, they will be taken to the front page, or to their user page if you specify <cite>Immediate login</cite> above. Leave this setting blank if you wish to keep the default behavior. If you wish the user to go to a page of your choosing, then enter the path for it here. For instance, you may redirect them to a static page such as <cite>node/35</cite>, or to the <cite>&lt;front&gt;</cite> page. If you would like them to return to the previous page, enter <cite>&lt;previous&gt;</cite>You may also use <em>%uid</em> as a variable, and the user\'s user ID will be substituted in the path. <em>NOTE: this feature will not work if you have the user password creation feature disabled.</em>'),
   );
 
   $form['registration']['redirect']['toboggan_redirect_on_confirm'] = array(
@@ -900,10 +927,16 @@ function _logintoboggan_protocol() {
   return (($_SERVER['HTTPS'] == 'on') ? 'https' : 'http');
 }
 
-function _logintoboggan_process_redirect($redirect, $account) {
-  $variables = array('%uid' => $account->uid);
-  $redirect = strtr($redirect, $variables);
-
+function _logintoboggan_process_redirect($redirect, $account, $referrer = NULL) {
+  switch($redirect) {
+    case '<previous>':
+      $redirect = urldecode($referrer);
+    break;
+    default:
+      $variables = array('%uid' => $account->uid);
+      $redirect = strtr($redirect, $variables);
+      break;
+  }
   return $redirect;
 }
 
