--- advuser_old.module	2007-02-22 00:56:25.000000000 +0000
+++ advuser.module	2007-02-23 01:22:30.000000000 +0000
@@ -24,20 +24,30 @@
  * Implementation of hook_menu().
  */
 function advuser_menu($may_cache) {
   // code which should run exactly once per page view
   if (!$may_cache) {
-    theme_add_style(drupal_get_path('module', 'advuser') .'/advuser.css');
+    drupal_add_css(drupal_get_path('module', 'advuser') .'/advuser.css');
   }
   $items = array();
   if ($may_cache) {
-    $items[] = 
-      array('path' => 'admin/user/advuser', 
-            'title' => t('advanced managment'),
+    $items[] = array(
+      'path' => 'admin/settings/advuser',
+      'title' => t('Advanced User'),
+      'description' => t('Advanced User Settings'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('advuser_settings'),
+      'access' => user_access('administer site configuration'),
+      'type' => MENU_NORMAL_ITEM,
+    );
+
+    $items[] = array(
+            'path' => 'admin/user/advuser', 
+            'title' => t('Advanced managment'),
             'callback' => 'advuser_admin_users',
-            'type' => MENU_LOCAL_TASK,
-            'weight' => 7,
+            'type' => MENU_NORMAL_ITEM,
+//            'weight' => 7,
             'access' => user_access('administer users'),
             );
   }
 
   return $items;
@@ -61,26 +71,26 @@
   
   $filters = array();
   
   // Regular filters
   $filters['email'] = array(
-      'title' =>t('E-mail'),
+      'title' => t('E-mail'),
       'where' => "u.mail = '%s'",
     );
   
-		  $filters['uid'] = array(
-      'title' =>t('User Id'),
+   $filters['uid'] = array(
+      'title' => t('User Id'),
       'where' => "u.uid = '%s'",
     );
 
-		  $filters['access'] = array(
-      'title' =>t('Last Access'),
+    $filters['access'] = array(
+      'title' => t('Last Access'),
       'where' => "u.access = '%s'",
     );
-				$filters['status'] = array(
-      'title' =>t('Status'),
-      'type'  =>t('selection'),
+    $filters['status'] = array(
+      'title' => t('Status'),
+      'type'  => t('selection'),
       'options' => array(0 => t('Blocked'), 1 => ('Active')),
       'where' => 'u.status = %s',
     );
 		
   $fields = variable_get('advuser_profile_fields', ADVUSER_DEFAULT_PROFILE_FIELDS);
@@ -96,48 +106,47 @@
           }
         }
         
         // Regular filters
         $filters[$field->fid] = array(
-            'title' =>$field->title,
+            'title' => $field->title,
             'type' => $field->type,
             'options' => $options,
-            'field_where' => 'pf.fid = ' . $field->fid .' AND pv.value = ' . "'%s'",
+            'field_where' => 'pf.fid = '. $field->fid .' AND pv.value = '."'%s'",
           );
       }
-      
     }
   }
   
-  
   advuser_module_invoke('filter', $filters);
   return $filters;
 }
 
 function advuser_admin_filter_form_validate($form_id, $form) {
-  $op = $_POST['op'];
-  if(  $op == 'Filter' || $op == 'Refine' ) {
+  $op = $form_values['op'];
+  if ( $op == 'Filter' || $op == 'Refine' ) {
     if ( ! isset($form[$form['filter']]) || $form[$form['filter']] == '' ) {
       form_set_error($form['filter'], t('Please fill some value to correctely set up the filter'));
     }
   }
 }
 
 
 function advuser_admin_filter_form_submit() {
   global $form_values;
-  $op = $_POST['op'];
+  $op = $form_values['op'];
   $filters = advuser_admin_filters();
   switch ($op) {
     case t('Filter'): case t('Refine'):
       if (isset($form_values['filter'])) {
         $filter = $form_values['filter'];
         if ( $filters[$filter]['type']  == 'selection' ) {
           if (isset($filters[$filter]['options'][$form_values[$filter]])) {
             $_SESSION['advuser_user_filter'][] = array($filter, $form_values[$filter]);
           }
-        } else {
+        }
+         else {
           $_SESSION['advuser_user_filter'][] = array($filter, $form_values[$filter]);
         }
       }
       break;
     case t('Undo'):
@@ -173,11 +182,11 @@
     $original = $filters[$type];
     $em = 'and';
     if ( array_search($type, $types_used) !== FALSE ) {
       $em = 'or';
     }
-    $string = ($i++ ? '<em>'.$em.'</em> where <strong>%a</strong> is <strong>%b</strong>' : '<strong>%a</strong> is <strong>%b</strong>');
+    $string = ($i++ ? '<em>'. $em .'</em> where <strong>%a</strong> is <strong>%b</strong>' : '<strong>%a</strong> is <strong>%b</strong>');
     $txt_value = $value;
     if ($original['type'] == 'selection') {
       $txt_value = $original['options'][$value];
     }
     $form['filters']['current'][] = array('#value' => t($string, array('%a' => $filters[$type]['title'] , '%b' => $txt_value)));
@@ -186,11 +195,12 @@
 
   foreach ($filters as $key => $filter) {
     $names[$key] = $filter['title'];
     if ( $filter['type'] == 'selection' ) {
       $form['filters']['status'][$key] = array('#type' => 'select', '#options' => $filter['options']);
-    } else {
+    }
+     else {
       $form['filters']['status'][$key] = array('#type' => 'textfield');
     }
   }
 
   $names_keys = array_keys($names);
@@ -199,27 +209,49 @@
   if (count($session)) {
     $form['filters']['buttons']['undo'] = array('#type' => 'submit', '#value' => t('Undo'));
     $form['filters']['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset'));
   }
 
-  return drupal_get_form('advuser_admin_filter_form', $form);
+//  return drupal_get_form('advuser_admin_filter_form', $form);
+  return $form;
+}
+
+/*
+** User operations
+*/
+function advuser_user_operations() {
+  $external = array();
+  $operations = array(
+      'email' => array(
+      'label' => t('Email users'),
+      'callback' => 'advuser_multiple_email_confirm_page',
+     ),
+  );
+  
+  advuser_module_invoke('actions', $external);
+  
+  foreach ($external as $k => $v ) {
+    $operations['external_'. $k] = $v;
+  }
+  
+  return $operations;
 }
 
 /*
-** Node operations
+** AdvUser operations
 */
 function advuser_admin_users_operations() {
   $external = array();
   $operations = array(
-    'email' => array(t('Email users'), 'advuser_multiple_email_confirm', 'confirm'),
+    'email' => array(t('Email users'), 'advuser_multiple_email_confirm_page', 'confirm'),
     'delete' =>    array(t('Delete users'), 'advuser_multiple_delete_confirm' , 'confirm')
   );
   
   advuser_module_invoke('actions', $external);
   
   foreach ($external as $k => $v ) {
-    $operations['external_'.$k] = $v;
+    $operations['external_'. $k] = $v;
   }
   
   return $operations;
 }
 
@@ -240,15 +272,14 @@
     $where_key = TRUE ;
     $where_keys = array_keys($where, $filters[$key]['where']);
     if ( count((array) $where_keys) > 0 ) {
       // check for same value to on arg array, should have the same key
       foreach ( $where_keys as $k => $v ) {
-      	if ( $where_args[$v] == $value ) {
-      	   $where_key = FALSE;
-      	}
+        if ( $where_args[$v] == $value ) {
+          $where_key = FALSE;
+        }
       }//foreach;
-      
     }
     
     if ( isset($filters[$key]['where']) && $where_key )  {
       $where[] = $filters[$key]['where'];
       $where_args[] = $value;
@@ -258,16 +289,16 @@
     $field_where_key = TRUE;
     $field_where_keys = array_keys($field_where, $filters[$key]['field_where']);
     if ( count((array) $field_where_keys) > 0 ) {
       // check for same value to on arg array, should have the same key
       foreach ( $field_where_keys as $k => $v ) {
-      	if ( $having_args[$v] == $value ) {
-      	   $field_where_key = FALSE;
-      	}
+        if ( $having_args[$v] == $value ) {
+          $field_where_key = FALSE;
+        }
       }//foreach;
-      
-    } else {
+    }
+     else {
       if (  isset($filters[$key]['field_where']) ) {
         $cnt++;
       }
     }
 
@@ -275,28 +306,30 @@
       $field_where[] = $filters[$key]['field_where'];
       $having_args[] = $value;
     }
     
     // Proces join just once
-    if ( ! strstr($join, $filters[$key]['join'] . ' ') ) {
-      $join .= $filters[$key]['join'] . ' ';
+    if ( ! strstr($join, $filters[$key]['join'] .' ') ) {
+      $join .= $filters[$key]['join'] .' ';
     }
   }
   
   //print_r($field_where);
   
   // build query smartly
   $where_sql = 'WHERE u.uid > 1 AND ';
   foreach ( (array) $where as $w ) {
     if ( $lastwhere ) {
       if ( $lastwhere == $w ) {
-        $where_sql .= ' OR ' . $w ;
-      } else {
-        $where_sql .= ') AND (' . $w ;
+        $where_sql .= ' OR '. $w ;
+      }
+       else {
+        $where_sql .= ') AND ('. $w ;
       }
-    } else {
-      $where_sql .= '(' . $w ;
+    } 
+     else {
+      $where_sql .= '('. $w ;
     }
     $lastwhere = $w;
   }
   if ( $lastwhere ) {
     $where_sql .= ') AND ';
@@ -304,53 +337,62 @@
   $where_sql .= '1';
   
   $where = count($where) ? 'WHERE u.uid > 1 AND '. implode(' AND ', $where) : '';
   $field_where_cnt = count($field_where);
   $field_where = $field_where_cnt ? implode(' OR ', $field_where) : '0';
-  $having[] = 'SUM(IF(' . $field_where . ',1,0)) = ' . $cnt;
+  $having[] = 'SUM(IF('. $field_where .',1,0)) = '. $cnt;
   $having = count($having) ? 'HAVING '. implode(' AND ', $having) : '';
   
   $args = array_merge($where_args, $having_args);
 
-  return array( 'where' => $where_sql, 'join' => $join, 'args' => $args, 'having'=> $having);
+  return array( 'where' => $where_sql, 'join' => $join, 'args' => $args, 'having' => $having);
 }
 
-function advuser_multiple_email_confirm($all, &$form) {
+function advuser_multiple_email_confirm_page($all, $form) {
+  return drupal_get_form('advuser_multiple_email_confirm', $all, $form);
+}
+
+function advuser_multiple_email_confirm($all, $form) {
   $form['mailsubject'] = array(
       '#type' => 'textfield', 
       '#title' => t('Subject'),
       '#required' => TRUE,
     );
   $form['mailbody'] = array(
       '#type' => 'textarea', 
       '#title' => t('Mail body'),
       '#required' => TRUE,
     );
+
+  return confirm_form($form,
+                      t('Are you sure you want to mail these users?'),
+                      'admin/user/advuser', t('This action cannot be undone.'),
+                      t('Mail all'), t('Cancel'));
 }
 
 
 function advuser_multiple_email_confirm_submit($form_id, $edit) {
   if ($edit['confirm']) {
     foreach ($edit['users'] as $uid => $value) {
-      $account = user_load(array('uid'=>$uid));
-      if ( module_exist('lightcrm') ) {
+      $account = user_load(array('uid' => $uid));
+      if ( module_exists('lightcrm') ) {
         _lightcrm_comment_add($uid, $edit['mailsubject'], $edit['mailbody']);
-      } else {
+      }
+       else {
         $from = variable_get("site_mail", ini_get("sendmail_from")); // http://drupal.org/node/77689
-        user_mail($account->mail, $edit['mailsubject'], $edit['mailbody'], "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
-        //user_mail($account->mail, $edit['mailsubject'], $edit['mailbody'], "Reply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
+        drupal_mail('advance-user-mail', $account->mail, $edit['mailsubject'], $edit['mailbody'], $from);
       }
     }
     drupal_set_message(t('The users have been mailed.'));
   }
   drupal_goto('admin/user/advuser');
 }
 
 function advuser_multiple_delete_confirm_submit($form_id, $edit) {
   if ($edit['confirm']) {
     foreach ($edit['users'] as $uid => $value) {
-      $account = user_load(array('uid'=>$uid));
+      $account = user_load(array('uid' => $uid));
       db_query('DELETE FROM {users} WHERE uid = %d', $account->uid);
       db_query('DELETE FROM {sessions} WHERE uid = %d', $account->uid);
       db_query('DELETE FROM {users_roles} WHERE uid = %d', $account->uid);
       db_query('DELETE FROM {authmap} WHERE uid = %d', $account->uid);
       watchdog('user', t('Deleted user: %name %email.', array('%name' => theme('placeholder', $account->name), '%email' => theme('placeholder', '<'. $account->mail .'>'))), WATCHDOG_NOTICE);
@@ -368,15 +410,16 @@
   $form = array();
   $form['users'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
   // array filter returns only elements with true values
   if ( $all ) {
     $filter = advuser_admin_user_build_filter_query();
-    $result = db_query('SELECT u.* FROM {users} u '. $filter['join'] .' LEFT JOIN {profile_values} pv ON u.uid = pv.uid LEFT JOIN {profile_fields} pf on pv.fid = pf.fid '. $filter['where'] .' GROUP BY u.uid ' . $filter['having'] . ' ORDER BY u.login DESC', $filter['args']);
+    $result = db_query('SELECT u.* FROM {users} u '. $filter['join'] .' LEFT JOIN {profile_values} pv ON u.uid = pv.uid LEFT JOIN {profile_fields} pf on pv.fid = pf.fid '. $filter['where'] .' GROUP BY u.uid '. $filter['having'] .' ORDER BY u.login DESC', $filter['args']);
     while ( $usr = db_fetch_object($result)) {
       $user_array[$usr->uid] = '';
     }
-  } else {
+  }
+   else {
     $user_array = array_filter($edit['users']);
   }
   
   foreach ($user_array as $uid => $value) {
     $name = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid));
@@ -394,14 +437,23 @@
                       'admin/user/advuser', t('This action cannot be undone.'),
                       $operations[$action][0], t('Cancel'));
 }
 
 function advuser_admin_users() {
+  $output = drupal_get_form('advuser_admin_filter_form');
+
+  // Call the form first, to allow for the form_values array to be populated.
+//  $output .= drupal_get_form('user_admin_account');
+  $output .= drupal_get_form('advuser_admin_users_form');
+  
+  return $output;
+}
+
+function advuser_admin_users_form() {
   global $form_values;
-  $output = advuser_admin_filter_form();
 
-  $op = $_POST['op'];
+  $op = $form_values['op'];
   if ( $op == 'Update all' ) {
     $all = TRUE;
   }
   
   foreach (advuser_admin_users_operations() as $key => $value) {
@@ -419,11 +471,11 @@
     }
   }
   
   $filter = advuser_admin_user_build_filter_query();
   $listno = variable_get('advuser_listno', ADVUSER_DEFAULT_LISTNO);
-  $result = pager_query('SELECT u.* FROM {users} u '. $filter['join'] .' LEFT JOIN {profile_values} pv ON u.uid = pv.uid LEFT JOIN {profile_fields} pf on pv.fid = pf.fid '. $filter['where'] .' GROUP BY u.uid ' . $filter['having'] . ' ORDER BY u.login DESC', $listno, 0, NULL, $filter['args']);
+  $result = pager_query('SELECT u.* FROM {users} u '. $filter['join'] .' LEFT JOIN {profile_values} pv ON u.uid = pv.uid LEFT JOIN {profile_fields} pf on pv.fid = pf.fid '. $filter['where'] .' GROUP BY u.uid '. $filter['having'] .' ORDER BY u.login DESC', $listno, 0, NULL, $filter['args']);
   //$result = 'SELECT u.* FROM {users} u '. $filter['join'] .' LEFT JOIN {profile_values} pv ON u.uid = pv.uid LEFT JOIN {profile_fields} pf on pv.fid = pf.fid '. $filter['where'] .' GROUP BY u.uid ' . $filter['having'] . ' ORDER BY u.login DESC', $listno, 0, $filter['args'];
   //$result .= tablesort_sql($header);
 
   $form['options_all'] = array(
     '#type' => 'fieldset', '#title' => t('Mass update on every record'),
@@ -432,91 +484,117 @@
   );
   $options = array();
   foreach (advuser_admin_users_operations() as $key => $value) {
     $options[$key] = $value[0];
   }
-  $form['options_all']['operation'] = array('#type' => 'select', '#options' => $options);
-  $form['options_all']['submit'] = array('#type' => 'submit', '#value' => t('Update all'));
+  $form['options_all']['operation'] = array(
+    '#type' => 'select',
+    '#options' => $options,
+  );
+  $form['options_all']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Update all')
+  );
   
   $form['options_sel'] = array(
     '#type' => 'fieldset', '#title' => t('Update on selected records only'),
     '#prefix' => "<div class=\"container-inline\">" , '#suffix' => "</div>",
     '#tree' => TRUE,
   );
   $options = array();
   foreach (advuser_admin_users_operations() as $key => $value) {
     $options[$key] = $value[0];
   }
-  $form['options_sel']['operation'] = array('#type' => 'select', '#options' => $options);
-  $form['options_sel']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
+  $form['options_sel']['operation'] = array(
+    '#type' => 'select',
+    '#options' => $options,
+   );
+  $form['options_sel']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Update')
+   );
+
+    $destination = drupal_get_destination();
+
+    $status = array(t('blocked'), t('active'));
 
-  $destination = drupal_get_destination();
   while ($usr = db_fetch_object($result)) {
     $users[$usr->uid] = '';
-    $form['name'][$usr->uid] = array('#value' => l($usr->name, 'user/'. $usr->uid));
-    $form['status'][$usr->uid] =  array('#value' =>  ($usr->status ? t('active') : t('blocked')));
-	$form['access'][$usr->uid] = array('#value' => $usr->access ? t('%time ago', array('%time' => format_interval(time() - $usr->access))) : t('never'));
-	//$form['access'][$usr->access] = array('#value' => ($usr->access ? format_interval(time() - $usr->access)));
-	$form['operations'][$usr->uid] = array('#value' => l(t('edit'), 'user/'. $usr->uid .'/edit', array(), $destination));
+    $form['name'][$usr->uid] = array('#value' => theme('username', $usr));
+
+    $form['status'][$account->uid] =  array('#value' => $status[$account->status]);
+    $form['access'][$usr->uid] = array('#value' => $usr->access ? t('%time ago', array('%time' => format_interval(time() - $usr->access))) : t('never'));
+    //$form['access'][$usr->access] = array('#value' => ($usr->access ? format_interval(time() - $usr->access)));
+    $form['operations'][$usr->uid] = array('#value' => l(t('edit'), 'user/'. $usr->uid .'/edit', array(), $destination));
     $form['refcode'][$usr->uid] = array('#value' => $usr->uid);
   }
-  $form['users'] = array('#type' => 'checkboxes', '#options' => $users);
+
+  $form['users'] = array(
+    '#type' => 'checkboxes',
+    '#options' => $users
+   );
+
   $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
 
   $form['#method'] = 'post';
   $form['#action'] =  url('admin/user/advuser/action');
 
-  // Call the form first, to allow for the form_values array to be populated.
-  $output .= drupal_get_form('advuser_admin_users', $form);
-  
-  return $output;
+  return $form;
 }
 
 
-function theme_advuser_admin_users($form) {
+function theme_advuser_admin_users_form($form) {
   // Overview table:
-  $header = array(NULL, t('Username'), t('Status'), t('Last Access'), t('User ID'), t('Operation'));
+  $header = array(
+    theme('table_select_header_cell'),
+    array('data' => t('Username'), 'field' => 'u.name'),
+    array('data' => t('Status'), 'field' => 'u.status'),
+    array('data' => t('Last access'), 'field' => 'u.access'),
+    array('data' => t('User ID'), 'field' => 'u.uid'),
+    t('Operations')
+  );
+
 
-  $output .= form_render($form['options_all']);
-  $output .= form_render($form['options_sel']);
+  $output .= drupal_render($form['options_all']);
+  $output .= drupal_render($form['options_sel']);
   if (isset($form['name']) && is_array($form['name'])) {
     foreach (element_children($form['name']) as $key) {
-      $row = array();
-      $row[] = form_render($form['users'][$key]);
-      $row[] = form_render($form['name'][$key]);
-      $row[] = form_render($form['status'][$key]);
-						$row[] = form_render($form['access'][$key]);
-						$row[] = form_render($form['refcode'][$key]);
-      $row[] = form_render($form['operations'][$key]);
-      $rows[] = $row;
+      $row = array(
+      drupal_render($form['users'][$key]),
+      drupal_render($form['name'][$key]),
+      drupal_render($form['status'][$key]),
+      drupal_render($form['access'][$key]),
+      drupal_render($form['refcode'][$key]),
+      drupal_render($form['operations'][$key]),
+      );
     }
 
   }
   else  {
     $rows[] = array(array('data' => t('No posts available.'), 'colspan' => '4'));
   }
 
   $output .= theme('table', $header, $rows);
-		if ($form['pager']['#value']) {
-  $output .= form_render($form['pager']);
+  if ($form['pager']['#value']) {
+    $output .= drupal_render($form['pager']);
   }
 
-  $output .= form_render($form);
+  $output .= drupal_render($form);
   $output .= theme('pager', NULL, 50, 0);
   return $output;
 }
 
 function theme_advuser_admin_filter_form(&$form) {
   $output .= '<div id="advuser-admin-filter">';
-  $output .= form_render($form['filters']);
+  $output .= drupal_render($form['filters']);
   $output .= '</div>';
-  $output .= form_render($form);
+  $output .= drupal_render($form);
   return $output;
 }
 
 function advuser_admin_users_validate($form_id, $edit) {
-  $op = $_POST['op'];
+  $op = $form_values['op'];
   if ( $op == 'Update' ) {
     $edit['users'] = array_diff($edit['users'], array(0));
     if (count($edit['users']) == 0) {
       form_set_error('', t('Please select some items to perform the update on.'));
     }
@@ -558,39 +636,44 @@
 
 function theme_advuser_filters(&$form) {
   $output .= '<ul>';
   if (sizeof($form['current'])) {
     foreach (element_children($form['current']) as $key) {
-      $output .= '<li>' . form_render($form['current'][$key]) . '</li>';
+      $output .= '<li>'. drupal_render($form['current'][$key]) .'</li>';
     }
   }
 
-  $output .= '<li><dl class="multiselect">' . (sizeof($form['current']) ? '<dt><em>'. t('and/or') .'</em> '. t('where') .'</dt>' : '') . '<dd class="a">';
-  foreach(element_children($form['filter']) as $key) {
-    $output .= form_render($form['filter'][$key]);
+  $output .= '<li><dl class="multiselect">'. (sizeof($form['current']) ? '<dt><em>'. t('and/or') .'</em> '. t('where') .'</dt>' : '') .'<dd class="a">';
+  foreach (element_children($form['filter']) as $key) {
+    $output .= drupal_render($form['filter'][$key]);
   }
   $output .= '</dd>';
 
-  $output .= '<dt>'. t('is') .'</dt>' . '<dd class="b">';
+  $output .= '<dt>'. t('is') .'</dt><dd class="b">';
 
-  foreach(element_children($form['status']) as $key) {
-    $output .= form_render($form['status'][$key]);
+  foreach (element_children($form['status']) as $key) {
+    $output .= drupal_render($form['status'][$key]);
   }
   $output .= '</dd>';
 
   $output .= '</dl>';
-  $output .= '<div class="container-inline" id="advuser-admin-buttons">'. form_render($form['buttons']) .'</div>';
+  $output .= '<div class="container-inline" id="advuser-admin-buttons">'. drupal_render($form['buttons']) .'</div>';
   $output .= '</li></ul><br class="clear" />';
 
   return $output;
 }
 
 function advuser_settings() {
-  $form['module_banner']
-    = array('#type' => 'markup', 
-            '#value' => '<div style="border: solid 1px #eee; margin: .5em; padding: .5em;" <strong>Module maintenance and development sponsored by <a href="http://exodusdev.com">Exodus Development</a></strong><br/>');
-  $form['module_id'] = array('#type' => 'markup', '#value' =>  ADVUSER_MODULE_VERSION .'<br/></div>');
+  $form['module_banner'] = array(
+      '#type' => 'markup', 
+      '#value' => '<div style="border: solid 1px #eee; margin: .5em; padding: .5em;" <strong>Module maintenance and development sponsored by <a href="http://exodusdev.com">Exodus Development</a></strong><br/>'
+    );
+
+  $form['module_id'] = array(
+      '#type' => 'markup',
+      '#value' =>  ADVUSER_MODULE_VERSION .'<br/></div>'
+    );
 
   $form['advuser_mail'] = array(
       '#type' => 'fieldset',
       '#title' => t('Mail notifications on user account activity.'),
       '#collapsible' => FALSE,
@@ -602,28 +685,29 @@
       '#title' => t('Send notifications on new user registration'),
       '#description' => t('Notify selected roles when new users register.'),
       '#default_value' => variable_get('advuser_new_notify', ADVUSER_DEFAULT_NEW_NOTIFY),
     );
 				
-		$form['advuser_mail']['advuser_modify_notify'] = array(
+   $form['advuser_mail']['advuser_modify_notify'] = array(
       '#type' => 'checkbox',
       '#title' => t('Send notifications on user profile updates'),
       '#description' => t('Notify selected roles when users update their profiles.'),
       '#default_value' => variable_get('advuser_modify_notify', ADVUSER_DEFAULT_MODIFY_NOTIFY),
     );
 
-  $form['advuser_mail']['variables']
-    = array('#type' => 'markup', 
-            '#value' => '<div class="advuser-inset-panel"><strong>Substitution variables</strong> available in subject and email body<br/><em> %username, %site, %uri, %user_email, %google_user (search google for user email), %yahoo_user (search yahoo for user email)</em></div>');
+  $form['advuser_mail']['variables'] = array(
+      '#type' => 'markup', 
+      '#value' => '<div class="advuser-inset-panel"><strong>Substitution variables</strong> available in subject and email body<br/><em> %username, %site, %uri, %user_email, %google_user (search google for user email), %yahoo_user (search yahoo for user email)</em></div>'
+    );
 				
   $form['advuser_mail']['advuser_new_subject'] = array(
       '#type' => 'textfield',
       '#title' => t('Mail subject'),
       '#description' => t('The subject of the mail that is going to be sent to the user.  You may insert substitution variables within this item.'),
       '#default_value' => variable_get('advuser_new_subject', ADVUSER_DEFAULT_NEW_SUBJECT),
     );
-		$form['advuser_mail']['advuser_modify_subject'] = array(
+  $form['advuser_mail']['advuser_modify_subject'] = array(
       '#type' => 'textfield',
       '#title' => t('Mail subject'),
       '#description' => t('The subject of the mail that is going to be sent when a user modifies their profiles.  You may insert substitution variables within this item.'),
       '#default_value' => variable_get('advuser_modify_subject', ADVUSER_DEFAULT_MODIFY_SUBJECT),
     );
@@ -632,20 +716,20 @@
       '#type' => 'textarea',
       '#title' => t('Mail body'),
       '#description' => t('The mail that is going to be sent to the selected roles.  You may insert substitution variables within this item.'),
       '#default_value' => variable_get('advuser_new_mail', ADVUSER_DEFAULT_NEW_MAIL),
     );
-		$form['advuser_mail']['advuser_modify_mail'] = array(
+  $form['advuser_mail']['advuser_modify_mail'] = array(
       '#type' => 'textarea',
       '#title' => t('Mail body'),
       '#description' => t('The mail that is going to be sent to the selected roles when a user modifies their account.  You may insert substitution variables within this item.'),
       '#default_value' => variable_get('advuser_modify_mail', ADVUSER_DEFAULT_MODIFY_MAIL),
     );
 				
-			$form['advuser_mail']['advuser_listno'] = array(
+    $form['advuser_mail']['advuser_listno'] = array(
       '#type' => 'select',
-						'#options' => drupal_map_assoc(array(0, 10, 25, 50, 75, 100, 125, 150, 175, 200)),
+      '#options' => drupal_map_assoc(array(0, 10, 25, 50, 75, 100, 125, 150, 175, 200)),
       '#title' => t('Number of users in listing'),
       '#description' => t('Sets how many users to display in table view'),
       '#default_value' => variable_get('advuser_listno', ADVUSER_DEFAULT_LISTNO),
     );
     
@@ -676,11 +760,11 @@
     $form['advuser_mailonnew']['no_roles_sel_warning']
       = array('#type' => 'markup', 
               '#value' => '<div class="advuser-settings-warning"><strong>WARNING: No roles selected!</strong> - no email notifications will be sent.</div>');
   }
     
-  if ( module_exist('profile') ) {
+  if ( module_exists('profile') ) {
     $form['advuser_profile'] = array(
         '#type' => 'fieldset',
         '#title' => t('Profile module special settings'),
         '#collapsible' => TRUE,
         '#collapsed' => TRUE,
@@ -707,11 +791,11 @@
         '#options' => $fields,
         '#default_value' => $values,
       );    
       
   }
-  return $form;
+  return system_settings_form($form);
 }
 
 /**
  * Get a list of substitution variables for the user account
  * @param $user the user account
@@ -720,15 +804,15 @@
 function _advuser_get_variables(&$user) {
   $variables = 
     array(
           '%username' => $user->name,
           '%site' => variable_get("site_name", "drupal"), 
-          '%uri' => url('user/'.$user->uid, NULL, NULL, TRUE), 
+          '%uri' => url('user/'. $user->uid, NULL, NULL, TRUE), 
           '%user_email' => $user->mail,
           /* FUTURE: '%user_signature' => $user->signature, */
-          '%google_user'=> "http:/www.google.com/search?q=%22$user->mail%22",
-          '%yahoo_user'=> "http://search.yahoo.com/search/?p=%22$user->mail%22", 
+          '%google_user' => "http:/www.google.com/search?q=%22$user->mail%22",
+          '%yahoo_user' => "http://search.yahoo.com/search/?p=%22$user->mail%22", 
           );
   return $variables;
 }
 
 /**
@@ -741,22 +825,23 @@
   if (is_array($roles) ) {
     foreach ((array)$roles as $role_k => $role_v) {
       if ( $role_v > 0 ) {
         if ( $role_v == DRUPAL_AUTHENTICATED_RID ) {
           $role_where = ' OR 1';
-        } else {
+        }
+         else {
           $role_where = ' OR ur.rid = '. $role_v;
         }
       }
     }
   }
   return $role_where;
 }
 
 function _advuser_dbquery_users_to_notify($roles) {
   $role_where = _advuser_get_roles_query_fragment($roles);
-  return db_query('SELECT u.mail, u.name FROM {users} u LEFT JOIN {users_roles} ur on u.uid = ur.uid WHERE 0 ' . $role_where );
+  return db_query('SELECT u.mail, u.name FROM {users} u LEFT JOIN {users_roles} ur on u.uid = ur.uid WHERE 0 '. $role_where );
 }
 
 
 /**
  * Handle user insertion (new users)
@@ -778,11 +863,11 @@
     watchdog('advuser', "Sending user account mail: subj='$user_subject' body='$user_body'");
 
     $roles = variable_get('advuser_new_roles', ADVUSER_DEFAULT_NEW_ROLES);
     $result = _advuser_dbquery_users_to_notify($roles);
     while ($row = db_fetch_object($result)) {
-      user_mail($row->mail, $user_subject, $user_body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
+      drupal_mail('advanced-user-mail', $row->mail, $user_subject, $user_body, $from);
     }
   }
 }
 
 /**
@@ -808,12 +893,11 @@
     watchdog('advuser', "Sending user account mail: subj='$user_subject' body='$user_body'");
 
     $roles = variable_get('advuser_new_roles', ADVUSER_DEFAULT_NEW_ROLES);
     $result = _advuser_dbquery_users_to_notify($roles);
     while ($row = db_fetch_object($result)) {
-      //user_mail($row->mail, $user_subject, $user_body, ADVUSER_MAIL_MESSAGE_TEMPLATE);
-      user_mail($row->mail, $user_subject, $user_body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
+      drupal_mail('advanced-user-mail', $row->mail, $user_subject, $user_body, $from);
     }
   }
 }
 
 
