=== modified file 'uc_cart/uc_cart.module'
--- uc_cart/uc_cart.module	2009-02-06 21:41:38 +0000
+++ uc_cart/uc_cart.module	2009-02-13 15:26:39 +0000
@@ -1113,16 +1113,7 @@
     else {
       // Get a valid new username.
       if (empty($order->data['new_user']['name'])) {
-        // Default to the first part of the e-mail address.
-        $name = substr($order->primary_email, 0, strpos($order->primary_email, '@'));
-
-        // Trim that value for spaces and length.
-        $name = trim(substr($name, 0, USERNAME_MAX_LENGTH));
-
-        // Make sure we don't hand out a duplicate username.
-        while (db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE LOWER(name) = LOWER('%s')", $name)) > 0) {
-          $name .= rand(0, 9);
-        }
+        $name = uc_store_email_to_username($order->primary_email);
       }
       else {
         $name = $order->data['new_user']['name'];
@@ -1144,7 +1135,7 @@
         // Manually set the password so it appears in the e-mail.
         $account->password = $fields['pass'];
 
-        // Send the e-mail through the
+        // Send the e-mail through the user module.
         drupal_mail('user', 'register_no_approval_required', $order->primary_email, NULL, array('account' => $account), uc_store_email_from());
       }
 
@@ -1174,8 +1165,8 @@
   }
 
   $output = '<p>'. check_markup(variable_get('uc_msg_order_submit', uc_get_message('completion_message')),
-                         variable_get('uc_msg_order_submit_format', 3), FALSE) .'</p>';
-  $show_message = check_markup(variable_get('uc_msg_order_'. $message_type, uc_get_message('completion_'. $message_type)), variable_get('uc_msg_order_'. $message_type .'_format', 3), FALSE);
+                         variable_get('uc_msg_order_submit_format', FILTER_FORMAT_DEFAULT), FALSE) .'</p>';
+  $show_message = check_markup(variable_get('uc_msg_order_'. $message_type, uc_get_message('completion_'. $message_type)), variable_get('uc_msg_order_'. $message_type .'_format', FILTER_FORMAT_DEFAULT), FALSE);
 
   if ($show_message != '') {
     $variables['!new_username'] = check_plain($_SESSION['new_user']['name']);
@@ -1183,7 +1174,7 @@
     $output .= '<p>'. strtr($show_message, $variables) .'</p>';
   }
   $output .= '<p>'. check_markup(variable_get('uc_msg_continue_shopping', uc_get_message('continue_shopping')),
-                      variable_get('uc_msg_continue_shopping_format', 3),
+                      variable_get('uc_msg_continue_shopping_format', FILTER_FORMAT_DEFAULT),
                       FALSE) .'</p>';
 
   $output = token_replace_multiple($output, array('global' => NULL, 'order' => $order));

=== modified file 'uc_cart/uc_cart.pages.inc'
--- uc_cart/uc_cart.pages.inc	2009-02-12 18:07:59 +0000
+++ uc_cart/uc_cart.pages.inc	2009-02-13 15:27:12 +0000
@@ -201,7 +201,7 @@
 function theme_uc_cart_checkout_form($form) {
   drupal_add_css(drupal_get_path('module', 'uc_cart') .'/uc_cart.css');
 
-  $output = '<div>'. check_markup(variable_get('uc_checkout_instructions', ''), variable_get('uc_checkout_instructions_format', 3), FALSE) .'</div>';
+  $output = '<div>'. check_markup(variable_get('uc_checkout_instructions', ''), variable_get('uc_checkout_instructions_format', FILTER_FORMAT_DEFAULT), FALSE) .'</div>';
 
   if (arg(1) == 'checkout2') {
     foreach (element_children($form['panes']) as $pane_id) {
@@ -339,7 +339,7 @@
 function theme_uc_cart_checkout_review($panes, $form) {
   drupal_add_css(drupal_get_path('module', 'uc_cart') .'/uc_cart.css');
 
-  $output = '<p>'. check_markup(variable_get('uc_checkout_review_instructions', uc_get_message('review_instructions')), variable_get('uc_checkout_review_instructions_format', 3), FALSE)
+  $output = '<p>'. check_markup(variable_get('uc_checkout_review_instructions', uc_get_message('review_instructions')), variable_get('uc_checkout_review_instructions_format', FILTER_FORMAT_DEFAULT), FALSE)
            .'</p><table class="order-review-table">';
 
   foreach ($panes as $title => $data) {

=== modified file 'uc_order/uc_order.admin.inc'
--- uc_order/uc_order.admin.inc	2009-02-10 21:51:13 +0000
+++ uc_order/uc_order.admin.inc	2009-02-13 15:09:57 +0000
@@ -798,22 +798,31 @@
                . $user->mail .'\');" value="'. t('Apply') .'" /><br /><br /><hr><br/>';
     }
     else {
-      $data = array(
-        'name' => $email,
+      $name = uc_store_email_to_username($email);
+
+      $fields = array(
+        'name' => $name,
         'mail' => $email,
         'pass' => user_password(6),
         'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0,
       );
-      $user = user_save(NULL, $data);
+
+      $account = user_save('', $fields);
+
       if ($_POST['sendmail'] == 'true') {
-        $params = array('account' => $user);
-        drupal_mail('uc_order', 'user-register-welcome', $user->mail, user_preferred_language($user), $params);
-        $output .= t('Account details sent to e-mail provided.<br /><br /><strong>Username:</strong> !username<br /><strong>Password:</strong> !password', array('!username' => $user->name, '!password' => $data['pass'])) .'<br /><br />';
+        // Manually set the password so it appears in the e-mail.
+        $account->password = $fields['pass'];
+
+        // Send the e-mail through the user module.
+        drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), uc_store_email_from());
+
+        $output .= t('Account details sent to e-mail provided.<br /><br /><strong>Username:</strong> @username<br /><strong>Password:</strong> @password', array('@username' => $fields['name'], '@password' => $fields['pass'])) .'<br /><br />';
       }
+
       $output .= '<strong>'. t('Use this account now?') .'</strong><br />'
-               . t('User !uid - !mail', array('!uid' => $user->uid, '!mail' => $user->mail)) .' <input type="button" '
-                .'onclick="select_existing_customer('. $user->uid .', \''
-               . $user->mail .'\');" value="'. t('Apply') .'" /><br /><br /><hr><br/>';
+               . t('User @uid - @mail', array('@uid' => $account->uid, '@mail' => $account->mail)) .' <input type="button" '
+                .'onclick="select_existing_customer('. $account->uid .', \''
+               . $account->mail .'\');" value="'. t('Apply') .'" /><br /><br /><hr><br/>';
     }
   }
 

=== modified file 'uc_store/uc_store.module'
--- uc_store/uc_store.module	2009-02-12 20:45:25 +0000
+++ uc_store/uc_store.module	2009-02-13 15:03:21 +0000
@@ -1535,6 +1535,37 @@
 }
 
 /**
+ * Derive a valid username from an e-mail address.
+ *
+ * @param $email
+ *   An e-mail address.
+ * @return
+ *   A username derived from the e-mail address, using the part of the address
+ *     up to the @ with integers appended to the end if needed to avoid a
+ *     duplicate username.
+ */
+function uc_store_email_to_username($email) {
+  // Default to the first part of the e-mail address.
+  $name = substr($email, 0, strpos($email, '@'));
+
+  // Trim that value for spaces and length.
+  $name = trim(substr($name, 0, USERNAME_MAX_LENGTH - 4));
+
+  // Make sure we don't hand out a duplicate username.
+  while (db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE LOWER(name) = LOWER('%s')", $name)) > 0) {
+    // If the username got too long, trim it back down.
+    if (strlen($name) == USERNAME_MAX_LENGTH) {
+      $name = substr($name, 0, USERNAME_MAX_LENGTH - 4);
+    }
+
+    // Append a random integer to the name.
+    $name .= rand(0, 9);
+  }
+
+  return $name;
+}
+
+/**
  * Trimmed down version of GPL class by Tony Marston.  Details available at
  * http://www.tonymarston.co.uk/php-mysql/encryption.html
  *

