From 1abe478410bda68435505643184f6bb102acb111 Mon Sep 17 00:00:00 2001
From: Chris Hood <chris@univate.com.au>
Date: Thu, 10 May 2012 16:38:25 +1000
Subject: [PATCH] Issue #1247538 by christianchristensen, mcpuddin,
 NROTC_Webmaster, mfb | Iulian Arcus: Port SMS User to 7.

---
 modules/sms_actions/sms_actions.info               |    2 +-
 modules/sms_blast/sms_blast.info                   |    2 +-
 modules/sms_blast/sms_blast.module                 |   28 ++-
 modules/sms_devel/js/sms_devel_virtualgw.js        |    4 +-
 modules/sms_devel/sms_devel.info                   |    9 +-
 modules/sms_devel/sms_devel.install                |   49 +++-
 modules/sms_devel/sms_devel.module                 |   39 ++--
 modules/sms_devel/sms_devel.send_form.inc          |   20 +-
 modules/sms_devel/sms_devel.virtualgw.admin.inc    |  124 ++++++-----
 modules/sms_devel/sms_devel.virtualgw.inc          |   85 +++++---
 modules/sms_international/sms_international.info   |    3 +-
 .../sms_international/sms_international.install    |   15 +-
 modules/sms_sendtophone/sms_sendtophone.info       |    4 +
 modules/sms_sendtophone/sms_sendtophone.js         |    6 +-
 modules/sms_sendtophone/sms_sendtophone.module     |   34 ++-
 modules/sms_track/sms_track.admin.inc              |    2 +-
 modules/sms_track/sms_track.info                   |   12 +-
 modules/sms_track/sms_track.install                |   21 ++-
 modules/sms_track/sms_track.module                 |  107 +++++----
 modules/sms_track/views/sms_track.views.inc        |    6 +-
 .../sms_track/views/sms_track.views_default.inc    |    2 +-
 .../views/sms_track_handler_field_direction.inc    |    4 +-
 .../views/sms_track_handler_field_gateway.inc      |    2 +-
 .../views/sms_track_handler_field_local_number.inc |    2 +-
 modules/sms_user/sms_user.admin.inc                |   33 +++-
 modules/sms_user/sms_user.info                     |    6 +
 modules/sms_user/sms_user.module                   |  104 ++++++---
 modules/sms_valid/sms_valid.admin.inc              |  230 ++++++++++--------
 modules/sms_valid/sms_valid.info                   |    7 +-
 modules/sms_valid/sms_valid.install                |  130 +++++++----
 modules/sms_valid/sms_valid.module                 |  246 +++++++++++++-------
 sms.admin.inc                                      |   18 +-
 sms.install                                        |    6 +-
 sms.module                                         |   23 ++-
 sms.tokens.inc                                     |    1 +
 35 files changed, 879 insertions(+), 507 deletions(-)

diff --git a/modules/sms_actions/sms_actions.info b/modules/sms_actions/sms_actions.info
index 32b553f..28c0489 100644
--- a/modules/sms_actions/sms_actions.info
+++ b/modules/sms_actions/sms_actions.info
@@ -3,4 +3,4 @@ description = "Provides an action for sending outgoing messages and the ability
 package = SMS Framework
 dependencies[] = sms
 dependencies[] = trigger
-core = 6.x
\ No newline at end of file
+core = 7.x
diff --git a/modules/sms_blast/sms_blast.info b/modules/sms_blast/sms_blast.info
index 3cd9949..1724697 100644
--- a/modules/sms_blast/sms_blast.info
+++ b/modules/sms_blast/sms_blast.info
@@ -2,4 +2,4 @@ name = SMS Blast
 description = Allows bulk text messages to be sent to registered users.
 package = SMS Framework
 dependencies[] = sms_user
-core = 6.x
\ No newline at end of file
+core = 7.x
diff --git a/modules/sms_blast/sms_blast.module b/modules/sms_blast/sms_blast.module
index dcecf19..f2683e3 100644
--- a/modules/sms_blast/sms_blast.module
+++ b/modules/sms_blast/sms_blast.module
@@ -6,21 +6,26 @@
  */
 
 /**
- * Implementation of hook_perm().
+ * Implements of hook_permission().
  */
-function sms_blast_perm() {
-  return array('send sms blasts');
+function sms_blast_permission() {
+  return array(
+    'Send SMS Blast' => array(
+      'title' => t('Send SMS Blast'),
+      'description' => t('This allows the user to send an SMS blast.'),
+    ),
+  );
 }
 
 /**
- * Implementation of hook_menu().
+ * Implements of hook_menu().
  */
 function sms_blast_menu() {
   $items['sms_blast'] = array(
     'title'            => 'SMS Blast',
     'page callback'    => 'drupal_get_form',
     'page arguments'   => array('sms_blast_form'),
-    'access arguments' => array('send sms blasts'),
+    'access arguments' => array('Send SMS Blast'),
     'type' => MENU_NORMAL_ITEM,
   );
   return $items;
@@ -50,15 +55,14 @@ function sms_blast_form() {
  */
 function sms_blast_form_submit(&$form, $form_state) {
   $result = db_query("SELECT uid FROM {sms_user} WHERE status = 2");
-  $confirmed_user_qty = db_affected_rows();
 
-  if ($confirmed_user_qty) {
-    while ($row = db_fetch_array($result)) {
-      sms_user_send($row['uid'], $form_state['values']['message']);
+  if ($result->rowCount() != 0) {
+    foreach ($result as $row) {
+      sms_user_send($row->uid, $form_state['values']['message']);
     }
-    drupal_set_message(t('The message was sent to %count users.', array('%count' => $confirmed_user_qty)));
+    drupal_set_message(t('The message was sent to %count users.', array('%count' => $result->rowCount())));
   }
   else {
-    drupal_set_message(t('There are 0 users with confirmed phone numbers. Message not sent.'));
+    drupal_set_message(t('There are 0 users with confirmed phone numbers. The message was not sent.'));
   }
-}
+}
\ No newline at end of file
diff --git a/modules/sms_devel/js/sms_devel_virtualgw.js b/modules/sms_devel/js/sms_devel_virtualgw.js
index d4a45a5..851f9fa 100644
--- a/modules/sms_devel/js/sms_devel_virtualgw.js
+++ b/modules/sms_devel/js/sms_devel_virtualgw.js
@@ -5,7 +5,7 @@
  */
 
 
-sms_devel.virtualgw.getactivity = function() {
+sms_devel.virtualgw.getactivity = (function() {
   var url = location.protocol +"//"+ location.host + Drupal.settings.basePath +"admin/smsframework/devel/virtualgw/getactivity";
   $.ajax({
     url: url,
@@ -24,7 +24,7 @@ sms_devel.virtualgw.getactivity = function() {
     }
   });
   $("#clean-url").addClass('clean-url-processed');
-};
+};)(jQuery);
 
 this.timer = setTimeout(sms_devel.virtualgw.getactivity, 1000);
 
diff --git a/modules/sms_devel/sms_devel.info b/modules/sms_devel/sms_devel.info
index 9565f1b..24ab716 100644
--- a/modules/sms_devel/sms_devel.info
+++ b/modules/sms_devel/sms_devel.info
@@ -1,5 +1,12 @@
 name = Devel
 description = Dev and test tools to assist module/site development with the SMS Framework.
-core = 6.x
+core = 7.x
 package = SMS Framework
 dependencies[] = sms
+scripts[] = /js/sms_devel_virtualgw.js
+files[] = sms_devel.install
+files[] = sms_devel.module
+files[] = sms_devel.send_form.inc
+files[] = sms_devel.virtualgw.admin.inc
+files[] = sms_devel.virtualgw.inc
+
diff --git a/modules/sms_devel/sms_devel.install b/modules/sms_devel/sms_devel.install
index e1cbf77..cd32713 100644
--- a/modules/sms_devel/sms_devel.install
+++ b/modules/sms_devel/sms_devel.install
@@ -5,11 +5,33 @@
  *
  * @package sms
  * @subpackage sms_devel
- */ 
+ */
+
+
+/**
+ * Implements hook_install().
+ *
+ * @ingroup hooks
+ */
+function sms_devel_install() {
+//  TODO The drupal_(un)install_schema functions are called automatically in D7.
+//  drupal_install_schema('sms_devel')
+}
 
 
 /**
- * Implement hook_schema()
+ * Implements hook_uninstall().
+ *
+ * @ingroup hooks
+ */
+function sms_devel_uninstall() {
+  // TODO The drupal_(un)install_schema functions are called automatically in D7.
+  // drupal_uninstall_schema('sms_devel')
+}
+
+
+/**
+ * Implements hook_schema().
  *
  * @return
  *   Drupal schema.
@@ -24,36 +46,43 @@ function sms_devel_schema() {
         'description' => 'The primary identifier.',
         'type' => 'serial',
         'unsigned' => TRUE,
-        'not null' => TRUE),
+        'not null' => TRUE,
+      ),
       'created' => array(
         'description' => 'The creation timestamp.',
-        'type' => 'int',        'unsigned' => TRUE,
-        'not null' => TRUE),
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
       'type' => array(
         'description' => '0=out 1=in, 2=receipt',
         'type' => 'int',
         'size' => 'tiny',
         'unsigned' => TRUE,
-        'not null' => TRUE),
+        'not null' => TRUE,
+      ),
       'number' => array(
         'description' => 'If type=0/2 this is recipient. If type=1 this is sender.',
         'type' => 'varchar',
         'length' => 20,
-        'not null' => TRUE),
+        'not null' => TRUE,
+      ),
       'message' => array(
         'description' => 'Message text.',
         'type' => 'text',
         'size' => 'normal',
-        'not null' => FALSE),
+        'not null' => FALSE,
+      ),
       'options' => array(
         'description' => 'Extended message options.',
         'type' => 'text',
         'size' => 'normal',
-        'not null' => FALSE),
+        'not null' => FALSE,
       ),
+    ),
     'unique keys' => array(),
     'primary key' => array('id'),
   );
-  
+
   return $schema;
 }
diff --git a/modules/sms_devel/sms_devel.module b/modules/sms_devel/sms_devel.module
index d9066be..e24ad51 100644
--- a/modules/sms_devel/sms_devel.module
+++ b/modules/sms_devel/sms_devel.module
@@ -5,14 +5,17 @@
  *
  * @package sms
  * @subpackage sms_devel
- */
+ *//*
+include DRUPAL_ROOT . '/' . 'sms_devel.send_form.inc';
+include DRUPAL_ROOT . '/' . 'sms_devel.virtualgw.inc';
+include DRUPAL_ROOT . '/' . 'sms_devel.virtualgw.admin.inc';*/
 include 'sms_devel.send_form.inc';
 include 'sms_devel.virtualgw.inc';
 include 'sms_devel.virtualgw.admin.inc';
 
 
 /**
- * Implement hook_menu()
+ * Implements hook_menu().
  *
  * @return
  *   Drupal menu item array.
@@ -23,49 +26,49 @@ function sms_devel_menu() {
   $items = array();
 
   $items['admin/smsframework/devel'] = array(
-    'title'  => 'Devel',
+    'title' => 'Devel',
     'description' => 'Development and testing tools.',
-    'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('sms_devel_send_form', NULL),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('sms_devel_send_form', NULL),
     'access arguments' => array('administer smsframework'),
   );
 
   $items['admin/smsframework/devel/send_form'] = array(
-    'title'  => 'Test send form',
-    'type'   => MENU_DEFAULT_LOCAL_TASK,
+    'title' => 'Test send form',
+    'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -10,
   );
 
   $items['admin/smsframework/devel/virtualgw'] = array(
-    'title'  => 'Virtual gateway',
-    'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('sms_devel_virtualgw_form', NULL),
+    'title' => 'Virtual gateway',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('sms_devel_virtualgw_form', NULL),
     'access arguments' => array('administer smsframework'),
-    'type'   => MENU_LOCAL_TASK,
+    'type' => MENU_LOCAL_TASK,
     'weight' => -9,
     'file' => 'sms_devel.virtualgw.admin.inc',
   );
 
   $items['admin/smsframework/devel/virtualgw/send'] = array(
-    'page callback'    => 'sms_devel_virtualgw_ahah_send',
+    'page callback' => 'sms_devel_virtualgw_ahah_send',
     'access arguments' => array('administer smsframework'),
-    'type'   => MENU_CALLBACK,
+    'type' => MENU_CALLBACK,
     'file' => 'sms_devel.virtualgw.inc',
   );
 
   $items['sms/virtualgw/receiver'] = array(
-    'page callback'    => 'sms_devel_virtualgw_receiver',
+    'page callback' => 'sms_devel_virtualgw_receiver',
     'page arguments' => array(),
     'access callback' => TRUE,
-    'type'   => MENU_CALLBACK,
+    'type' => MENU_CALLBACK,
     'file' => 'sms_devel.virtualgw.inc',
   );
 
   $items['admin/smsframework/devel/virtualgw/getactivity'] = array(
-    'page callback'    => 'sms_devel_virtualgw_js_getactivity',
+    'page callback' => 'sms_devel_virtualgw_js_getactivity',
     //'page arguments'   => array(NULL),
     'access arguments' => array('administer smsframework'),
-    'type'   => MENU_CALLBACK,
+    'type' => MENU_CALLBACK,
   );
 
   return $items;
@@ -73,7 +76,7 @@ function sms_devel_menu() {
 
 
 /**
- * Implement hook_gateway_info()
+ * Implements hook_gateway_info().
  *
  * @return
  *   SMS Franework gateway info array
diff --git a/modules/sms_devel/sms_devel.send_form.inc b/modules/sms_devel/sms_devel.send_form.inc
index 3b3bb93..796118f 100644
--- a/modules/sms_devel/sms_devel.send_form.inc
+++ b/modules/sms_devel/sms_devel.send_form.inc
@@ -16,10 +16,10 @@
  *
  * @ingroup forms
  */
-function sms_devel_send_form(&$form_state) {
+function sms_devel_send_form($form, &$form_state) {
   // Begin my form
   $form['about'] = array(
-    '#type'  => 'item',
+    '#type' => 'item',
     '#value' => 'This is a basic form that contains:<ul><li>include sms_send_form()</li><li>message text field</li><li>submit button</li></ul>The form validation includes sms_send_form_validate().<br/>The form submission includes sms_send_form_submit() which sends the message, and a little note that the form submitted ok.',
   );
 
@@ -28,27 +28,31 @@ function sms_devel_send_form(&$form_state) {
 
   // Message text field for my form
   $form['message'] = array(
-    '#type'  => 'textarea',
+    '#type' => 'textarea',
     '#title' => t('Message'),
-    '#rows'  => 4,
-    '#cols'  => 40,
+    '#rows' => 4,
+    '#cols' => 40,
     '#resizable' => FALSE,
   );
 
   // Submit button for my form
   $form['submit'] = array(
-    '#type'  => 'submit',
+    '#type' => 'submit',
     '#value' => 'Submit',
     '#validate' => array('sms_send_form_validate'),
-    '#submit'   => array('sms_send_form_submit', 'sms_devel_send_form_submit'),
+    '#submit' => array('sms_send_form_submit', 'sms_devel_send_form_submit'),
   );
 
   return $form;
 }
 
+/**
+ * @todo Please document this function.
+ * @see http://drupal.org/node/1354
+ */
 function sms_devel_send_form_submit($form, &$form_state) {
   // These is what happens after the SMS has been sent
   $number  = $form_state['values']['number'];
   $message = $form_state['values']['message'];
-  drupal_set_message("Form submitted ok for number $number and message: $message");
+  drupal_set_message(check_plain(t("Form submitted ok for number $number and message: $message")));
 }
diff --git a/modules/sms_devel/sms_devel.virtualgw.admin.inc b/modules/sms_devel/sms_devel.virtualgw.admin.inc
index 3379e0d..bf44e9e 100644
--- a/modules/sms_devel/sms_devel.virtualgw.admin.inc
+++ b/modules/sms_devel/sms_devel.virtualgw.admin.inc
@@ -18,8 +18,8 @@
  */
 function sms_devel_virtualgw_settings_form($configuration) {
   $form['sms_devel_virtualgw_link'] = array(
-    '#type'  => 'item',
-    '#value' => 'See also: '. l('Virtual Gateway dashboard', 'admin/smsframework/devel/virtualgw'),
+    '#type' => 'item',
+    '#value' => 'See also: ' . l(t('Virtual Gateway dashboard'), 'admin/smsframework/devel/virtualgw'),
   );
 
   return $form;
@@ -32,129 +32,133 @@ function sms_devel_virtualgw_settings_form($configuration) {
  * @return
  *   Drupal system settings form array.
  */
-function sms_devel_virtualgw_form() {
+function sms_devel_virtualgw_form($form, &$form_state) {
   // Include js
 
   $form['sms_devel_virtualgw_settings'] = array(
-    '#type'  => 'fieldset',
+    '#type' => 'fieldset',
     '#title' => t('Settings'),
     '#collapsible' => TRUE,
-    '#collapsed'   => TRUE,
+    '#collapsed' => TRUE,
   );
   $form['sms_devel_virtualgw_settings']['sms_devel_virtualgw_autoreply_enabled'] = array(
-    '#type'  => 'checkbox',
+    '#type' => 'checkbox',
     '#title' => t('Enable autoreply'),
     '#default_value' => variable_get('sms_devel_virtualgw_autoreply_enabled', FALSE),
   );
   $form['sms_devel_virtualgw_settings']['sms_devel_virtualgw_autoreply_format'] = array(
-    '#type'  => 'textarea',
-    '#rows'  => 4,
-    '#cols'  => 40,
+    '#type' => 'textarea',
+    '#rows' => 4,
+    '#cols' => 40,
     '#resizable' => FALSE,
     '#default_value' => variable_get('sms_devel_virtualgw_autoreply_format', 'echo: {message}'),
-    '#description'   => t('If enabled then the gateway will reply to your messages through sms_incoming()<br />You may use these tokens: {number} {gw_number} {message} {reference}'),
+    '#description' => t('If enabled then the gateway will reply to your messages through sms_incoming()<br />You may use these tokens: {number} {gw_number} {message} {reference}'),
   );
   $form['sms_devel_virtualgw_settings']['sms_devel_virtualgw_receipts_enabled'] = array(
-    '#type'  => 'checkbox',
+    '#type' => 'checkbox',
     '#title' => t('Enable receipts'),
     '#default_value' => variable_get('sms_devel_virtualgw_receipts_enabled', FALSE),
-    '#description'   => t('If enabled then the gateway will provide a message receipt through sms_receipt()'),
+    '#description' => t('If enabled then the gateway will provide a message receipt through sms_receipt()'),
   );
   $form['sms_devel_virtualgw_settings']['submit'] = array(
-    '#type'   => 'submit',
-    '#value'  => t('Save'),
+    '#type' => 'submit',
+    '#value' => t('Save'),
     '#submit' => array('sms_devel_virtualgw_form_save_settings'),
   );
 
   $form['sms_devel_virtualgw_send'] = array(
-    '#type'  => 'fieldset',
+    '#type' => 'fieldset',
     '#title' => t('Send a message from the gateway to SMS Framework'),
     '#collapsible' => TRUE,
-    '#collapsed'   => TRUE,
+    '#collapsed' => TRUE,
   );
   $form['sms_devel_virtualgw_send']['sms_devel_virtualgw_from'] = array(
-    '#type'  => 'textfield',
-    '#title' => t('From ($number)'),
-    '#size'  => 30,
+    '#type' => 'textfield',
+    '#title' => check_plain(t('From ($number)')),
+    '#size' => 30,
   );
   $form['sms_devel_virtualgw_send']['sms_devel_virtualgw_to'] = array(
-    '#type'  => 'textfield',
-    '#title' => t('To ($options[\'gw_number\'])'),
-    '#size'  => 30,
+    '#type' => 'textfield',
+    '#title' => check_plain(t('To ($options[\'gw_number\'])')),
+    '#size' => 30,
   );
   $form['sms_devel_virtualgw_send']['sms_devel_virtualgw_message'] = array(
-    '#type'  => 'textarea',
-    '#title' => t('Message ($message)'),
-    '#rows'  => 4,
-    '#cols'  => 40,
+    '#type' => 'textarea',
+    '#title' => check_plain(t('Message ($message)')),
+    '#rows' => 4,
+    '#cols' => 40,
     '#resizable' => FALSE,
   );
   $form['sms_devel_virtualgw_send']['send'] = array(
-    '#type'  => 'button',
+    '#type' => 'button',
     '#value' => t('Send'),
-    '#ahah'  => array(
-      'path'    => 'admin/smsframework/devel/virtualgw/send',
-      //'event'   => 'mousedown',
-      //'method'  => 'replace',
-      //'wrapper' => 'logfield',
+    // '#ahah' => array(
+      // 'page' => 'admin/smsframework/devel/virtualgw/send',
+    '#ajax' => array(
+      'callback' => 'sms_devel_virtualgw_send', // TODO Make sure this is equivalent to the lines above.
+      // 'event'   => 'mousedown',
+      // 'method'  => 'replace',
+      // 'wrapper' => 'logfield',
     ),
     // onclick="sendSmsMessage();"
   );
 
   $form['sms_devel_virtualgw_log'] = array(
-    '#type'  => 'fieldset',
+    '#type' => 'fieldset',
     '#title' => t('Activity log'),
     '#collapsible' => TRUE,
-    '#collapsed'   => FALSE,
+    '#collapsed' => FALSE,
   );
   $form['sms_devel_virtualgw_log']['sms_devel_virtualgw_logfield_refresh'] = array(
-    '#type'  => 'checkbox',
+    '#type' => 'checkbox',
     '#title' => t('Refresh the logfield'),
     '#default_value' => (variable_get('sms_devel_virtualgw_logfield_refresh', 1)) ? 1 : 0,
     // onchange="toggleRefresh(this.checked);"
   );
   $form['sms_devel_virtualgw_log']['sms_devel_virtualgw_logfield_showsent'] = array(
-    '#type'  => 'checkbox',
+    '#type' => 'checkbox',
     '#title' => t('Show sent messages'),
     '#default_value' => (variable_get('sms_devel_virtualgw_logfield_showsent', 1)) ? 1 : 0,
   );
   $form['sms_devel_virtualgw_log']['sms_devel_virtualgw_logfield_showreceived'] = array(
-    '#type'  => 'checkbox',
+    '#type' => 'checkbox',
     '#title' => t('Show received messages'),
     '#default_value' => (variable_get('sms_devel_virtualgw_logfield_showreceived', 1)) ? 1 : 0,
   );
   $form['sms_devel_virtualgw_log']['sms_devel_virtualgw_logfield_showreceipts'] = array(
-    '#type'  => 'checkbox',
+    '#type' => 'checkbox',
     '#title' => t('Show message receipts'),
     '#default_value' => (variable_get('sms_devel_virtualgw_logfield_showreceipts', 1)) ? 1 : 0,
   );
   $form['sms_devel_virtualgw_log']['sms_devel_virtualgw_logfield_lines'] = array(
-    '#type'  => 'textfield',
+    '#type' => 'textfield',
     '#title' => t('Number of recent activity records to pull'),
-    '#size'  => 4,
+    '#size' => 4,
   );
   $form['sms_devel_virtualgw_log']['sms_devel_virtualgw_logfield_refreshes'] = array(
-    '#type'  => 'item',
+    '#type' => 'item',
     '#title' => t('Refresh count'),
     '#value' => t(''),
   );
   $form['sms_devel_virtualgw_log']['sms_devel_virtualgw_logfield'] = array(
-    '#type'  => 'textarea',
+    '#type' => 'textarea',
     '#title' => t('Virtual gateway activity'),
-    '#rows'  => 20,
-    '#cols'  => 60,
+    '#rows' => 20,
+    '#cols' => 60,
     '#resizable' => TRUE,
     '#prefix' => '<div id="logfield">',
-    '#value'  => 'Sorry, this feature is not finished yet.',
+    '#value' => 'Sorry, this feature is not finished yet.',
     '#suffix' => '</div>',
   );
   $form['sms_devel_virtualgw_log']['get'] = array(
-    '#type'  => 'button',
+    '#type' => 'button',
     '#value' => t('Get activity'),
-    '#ahah'  => array(
-      'path'    => 'admin/smsframework/devel/virtualgw/getactivity',
-      //'event'   => 'mousedown',
-      'method'  => 'replace',
+    // '#ahah' => array(
+      // 'path' => 'admin/smsframework/devel/virtualgw/getactivity',
+    '#ajax' => array(
+      'callback' => 'sms_devel_virtualgw_js_getactivity', // TODO Make sure this is equivalent to the lines above.
+      // 'event'   => 'mousedown',
+      'method' => 'replace',
       'wrapper' => 'logfield',
     ),
     // onclick="sendSmsMessage();"
@@ -164,13 +168,17 @@ function sms_devel_virtualgw_form() {
 }
 
 
+/**
+ * @todo Please document this function.
+ * @see http://drupal.org/node/1354
+ */
 function sms_devel_virtualgw_form_save_settings($form, &$form_state) {
   variable_set('sms_devel_virtualgw_autoreply_enabled',
-               $form_state['values']['sms_devel_virtualgw_autoreply_enabled']);
+    $form_state['values']['sms_devel_virtualgw_autoreply_enabled']);
   variable_set('sms_devel_virtualgw_autoreply_format',
-               $form_state['values']['sms_devel_virtualgw_autoreply_format']);
+    $form_state['values']['sms_devel_virtualgw_autoreply_format']);
   variable_set('sms_devel_virtualgw_receipts_enabled',
-               $form_state['values']['sms_devel_virtualgw_receipts_enabled']);
+    $form_state['values']['sms_devel_virtualgw_receipts_enabled']);
 }
 
 
@@ -181,8 +189,8 @@ function sms_devel_virtualgw_form_save_settings($form, &$form_state) {
  *   HTML content string.
  */
 /*
-function sms_devel_virtualgw_admin_view() {
-  $content = views_embed_view('sms_devel_virtualgw', $display_id = 'default');
-  return $content;
-}
-*/
+ function sms_devel_virtualgw_admin_view() {
+ $content = views_embed_view('sms_devel_virtualgw', $display_id = 'default');
+ return $content;
+ }
+ */
diff --git a/modules/sms_devel/sms_devel.virtualgw.inc b/modules/sms_devel/sms_devel.virtualgw.inc
index 3edad83..9089402 100644
--- a/modules/sms_devel/sms_devel.virtualgw.inc
+++ b/modules/sms_devel/sms_devel.virtualgw.inc
@@ -8,17 +8,15 @@
  */
 
 /*
- A table is required to store activity, to be queried by the interactor page 
- Note: I have changed the "server-side number" nomenclature from "sender" & 
+ A table is required to store activity, to be queried by the interactor page
+ Note: I have changed the "server-side number" nomenclature from "sender" &
  "in_number" to simply "gw_number".
-*/
-
+ */
 
-define('SMS_DEVEL_VIRTUALGW_TYPE_OUT',     0);
-define('SMS_DEVEL_VIRTUALGW_TYPE_IN',      1);
+define('SMS_DEVEL_VIRTUALGW_TYPE_OUT',      0);
+define('SMS_DEVEL_VIRTUALGW_TYPE_IN',       1);
 define('SMS_DEVEL_VIRTUALGW_TYPE_RECEIPT', 2);
 
-
 /**
  * Callback for sending messages.
  *
@@ -54,7 +52,7 @@ function sms_devel_virtualgw_send($number, $message, $options) {
 
   // Always return success
   return array(
-    'status'      => TRUE,
+    'status' => TRUE,
     'status_code' => SMS_GW_OK,
     'gateway_status_code' => 'OK',
     'gateway_status_text' => 'sms_devel_virtualgw: send: OK',
@@ -62,6 +60,10 @@ function sms_devel_virtualgw_send($number, $message, $options) {
 }
 
 
+/**
+ * @todo Please document this function.
+ * @see http://drupal.org/node/1354
+ */
 function sms_devel_virtualgw_receiver($number = NULL, $message = NULL, $options = array()) {
   // Handle HTTP requests rather than direct function calls
   if ($number === NULL) {
@@ -79,24 +81,41 @@ function sms_devel_virtualgw_receiver($number = NULL, $message = NULL, $options
 }
 
 
+/**
+ * @todo Please document this function.
+ * @see http://drupal.org/node/1354
+ */
 function sms_devel_virtualgw_log_insert($type, $number = '', $message = '', $options = array()) {
   $options_z = serialize($options);
-  $result = db_query("INSERT INTO {sms_devel_virtualgw} (created, type, number,
-    message, options) VALUES (%d, %d, '%s', '%s', '%s')",
-    time(), $type, $number, $message, $options_z);
+  // @TODO Please review the conversion of this statement to the D7 database API syntax.
+  /* db_query("INSERT INTO {sms_devel_virtualgw} (created, type, number,
+   message, options) VALUES (%d, %d, '%s', '%s', '%s')", REQUEST_TIME, $type, $number, $message, $options_z) */
+  $result = $id = db_insert('sms_devel_virtualgw')
+  ->fields(array(
+    'created' => REQUEST_TIME,
+    'type' => $type,
+    'number' => $number,
+    'message' => $message,
+    'options' => $options_z,
+  ))
+  ->execute();
   return $result;
 }
 
 
+/**
+ * @todo Please document this function.
+ * @see http://drupal.org/node/1354
+ */
 function sms_devel_virtualgw_sendlogic($number, $message, $options) {
   $autoreply_enabled = variable_get('sms_devel_virtualgw_autoreply_enabled', FALSE);
-  $autoreply_format  = variable_get('sms_devel_virtualgw_autoreply_format',  '');
-  $receipts_enabled  = variable_get('sms_devel_virtualgw_receipts_enabled',  FALSE);
+  $autoreply_format  = variable_get('sms_devel_virtualgw_autoreply_format',   '');
+  $receipts_enabled  = variable_get('sms_devel_virtualgw_receipts_enabled',   FALSE);
 
   if ($autoreply_enabled) {
     // TODO Figure out the tokens logic
-    //$echo_message = $autoreply_format . $message;
-    $echo_message = 'echo: '. $message;
+    // $echo_message = $autoreply_format . $message;
+    $echo_message = 'echo: ' . $message;
 
     // Swap the numbers for the echo
     $echo_number  = $options['gw_number'];
@@ -118,6 +137,10 @@ function sms_devel_virtualgw_sendlogic($number, $message, $options) {
 }
 
 
+/**
+ * @todo Please document this function.
+ * @see http://drupal.org/node/1354
+ */
 function sms_devel_virtualgw_js_getactivity() {
   // We should not need to get a default limit - expect it in the request
   $limit = 50;
@@ -126,7 +149,9 @@ function sms_devel_virtualgw_js_getactivity() {
     $limit = $_REQUEST['rows'];
   }
   // Handle zero or null limit
-  if ( ! $limit ) { $limit = 1; }
+  if ( ! $limit ) {
+    $limit = 1;
+  }
 
   $lines = array();
 
@@ -137,14 +162,11 @@ function sms_devel_virtualgw_js_getactivity() {
       case SMS_DEVEL_VIRTUALGW_TYPE_OUT:
         $from = $options['sender'];
         $to   = $row->number;
-        ;;
       case SMS_DEVEL_VIRTUALGW_TYPE_IN:
         $from = $row->number;
         $to   = $options['sender'];
-        ;;
       case SMS_DEVEL_VIRTUALGW_TYPE_RECEIPT:
         // Receipt
-        ;;
     }
     $dir = $row->type;
     $msg = $row->message;
@@ -154,14 +176,17 @@ function sms_devel_virtualgw_js_getactivity() {
 
   $output = implode("\n", $lines);
 
-  $form_state = array('storage' => NULL, 'submitted' => FALSE);
-  $form_build_id = $_POST['form_build_id'];
+  $form_state = array(
+    'storage' => NULL,
+    'submitted' => FALSE,
+  );
+  $form_build_id = check_plain($_POST['form_build_id']);
   // Get the form from the cache.
   $form = form_get_cache($form_build_id, $form_state);
   $args = $form['#parameters'];
   $form_id = array_shift($args);
   // We will run some of the submit handlers so we need to disable redirecting.
-  $form['#redirect'] = FALSE;
+  $form_state['#redirect'] = FALSE;
   // We need to process the form, prepare for that by setting a few internals
   // variables.
   $form['#post'] = $_POST;
@@ -178,22 +203,22 @@ function sms_devel_virtualgw_js_getactivity() {
   unset($logfield['#prefix'], $logfield['#suffix']); // Prevent duplicate wrappers.
   $output = drupal_render($logfield);
 
-  return drupal_json(array('status' => TRUE, 'data' => $output));
+  return drupal_json_output(array('status' => TRUE, 'data' => $output));
 }
 
 
+/**
+ * @todo Please document this function.
+ * @see http://drupal.org/node/1354
+ */
 function sms_devel_virtualgw_ahah_send() {
   $result = drupal_http_request(
-    url('sms/virtualgw/receiver', array('absolute' => TRUE)),
-    array('Content-Type' => 'application/x-www-form-urlencoded'),
-    'POST',
-    http_build_query(array(
+    url('sms/virtualgw/receiver', array('absolute' => TRUE)), array('headers' => array('Content-Type' => 'application/x-www-form-urlencoded'), 'method' => 'POST', 'data' => http_build_query(array(
       'number' => $_POST['sms_devel_virtualgw_from'],
       'message' => $_POST['sms_devel_virtualgw_message'],
       'gw_number' => $_POST['sms_devel_virtualgw_to'],
-    ), '', '&')
-  );
+    ), '', '&')));
 
   // Return a good status
-  drupal_json(array('status' => TRUE));
+  drupal_json_output(array('status' => TRUE));
 }
diff --git a/modules/sms_international/sms_international.info b/modules/sms_international/sms_international.info
index 500064b..bb0621c 100644
--- a/modules/sms_international/sms_international.info
+++ b/modules/sms_international/sms_international.info
@@ -2,4 +2,5 @@ name = "SMS International"
 description = "Provides international number validation."
 dependencies[] = sms
 package = SMS Framework
-core = 6.x
\ No newline at end of file
+core = "7.x"
+
diff --git a/modules/sms_international/sms_international.install b/modules/sms_international/sms_international.install
index 95380eb..d7860d4 100644
--- a/modules/sms_international/sms_international.install
+++ b/modules/sms_international/sms_international.install
@@ -1,16 +1,18 @@
 <?php
 
 /**
- * Implementation of hook_install().
+ * Implements hook_install().
  */
 function sms_international_install() {
+//  drupal_install_schema('sms_international');
+
   db_query("INSERT INTO {sms_international} (iso, name, country_code, min_length, max_length, status) VALUES ('es', 'Spain', '34', '9', '9', 1)");
   db_query("INSERT INTO {sms_international} (iso, name, country_code, min_length, max_length, status) VALUES ('gb', 'United Kingdom', '44', '9', '10', 1)");
   db_query("INSERT INTO {sms_international} (iso, name, country_code, min_length, max_length, status) VALUES ('us', 'United States', '1', '10', '10', 1)");
 }
 
 /**
- * Implementation of hook_schema().
+ * Implements hook_schema().
  */
 function sms_international_schema() {
   $schema['sms_international'] = array(
@@ -25,6 +27,13 @@ function sms_international_schema() {
     'primary key' => array('iso'),
     'indexes'  => array('country_code' => array('country_code')),
   );
-  
+
   return $schema;
 }
+
+/**
+ * Implements hook_uninstall().
+ */
+function sms_international_uninstall() {
+//  drupal_uninstall_schema('sms_international');
+}
\ No newline at end of file
diff --git a/modules/sms_sendtophone/sms_sendtophone.info b/modules/sms_sendtophone/sms_sendtophone.info
index ec4c0d3..43484ba 100644
--- a/modules/sms_sendtophone/sms_sendtophone.info
+++ b/modules/sms_sendtophone/sms_sendtophone.info
@@ -3,4 +3,8 @@ description = Provides various tools for sending bits of information via SMS.
 dependencies[] = sms
 package = SMS Framework
 core = 7.x
+files[] = sms_sendtophone.css
 files[] = sms_sendtophone.module
+
+scripts[] = sms_sendtophone.js
+
diff --git a/modules/sms_sendtophone/sms_sendtophone.js b/modules/sms_sendtophone/sms_sendtophone.js
index a7ac2c8..83216be 100644
--- a/modules/sms_sendtophone/sms_sendtophone.js
+++ b/modules/sms_sendtophone/sms_sendtophone.js
@@ -1,4 +1,5 @@
-Drupal.behaviors.smssendtophone = function (context) {
+Drupal.behaviors.smssendtophone = {
+(attach: function (context) {
   $("a.sms-sendtophone").addClass('thickbox').each(function(i) {
     if ($(this).attr('href').search(/\?/) < 0) {
       $(this).attr('href', $(this).attr('href').concat('?'));
@@ -8,4 +9,5 @@ Drupal.behaviors.smssendtophone = function (context) {
     }
     $(this).attr('href', $(this).attr('href').concat('height=275&width=300&thickbox=1'));
   })
-}
+})(jQuery);
+};
\ No newline at end of file
diff --git a/modules/sms_sendtophone/sms_sendtophone.module b/modules/sms_sendtophone/sms_sendtophone.module
index fc2703a..d4a01e5 100644
--- a/modules/sms_sendtophone/sms_sendtophone.module
+++ b/modules/sms_sendtophone/sms_sendtophone.module
@@ -87,7 +87,7 @@ function sms_sendtophone_page($type = NULL) {
     }
   }
   $output = !empty($form) ? drupal_render($form) : $register;
-  
+
   if (isset($_GET['thickbox'])) {
     print $output;
     exit;
@@ -96,14 +96,14 @@ function sms_sendtophone_page($type = NULL) {
 }
 
 /**
- * Implements hook_filter_info()
+ * Implements hook_filter_info().
  */
 function sms_sendtophone_filter_info() {
   $filters['sms_sendtophone'] = array(
     'title' => t('Inline SMS'),
     'description' => t('Highlights text between [sms] tags and appends a "send to phone" button.'),
     'process callback' => '_sms_sendtophone_filter_inline',
-    'tips callback' => '_sms_sendtophone_filter_tips',
+    'tips callback' => '_sms_sendtophone_filter_sms_tips',
     'settings callback' => '_sms_sendtophone_filter_inline_settings',
     'cache' => FALSE,
   );
@@ -113,7 +113,7 @@ function sms_sendtophone_filter_info() {
 /**
  * SMS filter tips
  */
-function _sms_sendtophone_filter_tips($filter, $format, $long = FALSE) {
+function _sms_sendtophone_filter_sms_tips($filter, $format, $long = FALSE) {
   return t('Text between [sms][/sms] tags will be highted and appended with a "send to phone" button.');
 }
 
@@ -177,7 +177,10 @@ function _sms_sendtophone_filter_inline($text, $filter, $format, $langcode, $cac
 
   foreach ($matches as $match) {
     // TODO Please change this theme call to use an associative array for the $variables parameter.
-    $text = str_replace($match[0], theme('sms_sendtophone_filter_inline_' . $display, array('text' => $match[1], 'filter' => $filter)), $text);
+    $text = str_replace($match[0], theme('sms_sendtophone_filter_inline_' . $display, array(
+      'text' => $match[1],
+      'filter' => $filter)), $text
+    );
   }
   return $text;
 }
@@ -203,7 +206,8 @@ function sms_sendtophone_theme() {
  * @todo Please document this function.
  * @see http://drupal.org/node/1354
  */
-function theme_sms_sendtophone_filter_inline_text($variables) {
+function theme_sms_sendtophone_filter_inline_text() {
+  // TODO Number of parameters in this theme funcion does not match number of parameters found in hook_theme.
   $text = $variables['text'];
   $filter = $variables['filter'];
   $link_text = !empty($filter->settings['sms_sendtophone_filter_inline_display_text']) ? $filter->settings['sms_sendtophone_filter_inline_display_text'] : t('Send to phone');
@@ -216,7 +220,8 @@ function theme_sms_sendtophone_filter_inline_text($variables) {
  * @todo Please document this function.
  * @see http://drupal.org/node/1354
  */
-function theme_sms_sendtophone_filter_inline_icon($variables) {
+function theme_sms_sendtophone_filter_inline_icon() {
+  // TODO Number of parameters in this theme funcion does not match number of parameters found in hook_theme.
   $text = $variables['text'];
   $format = $variables['format'];
   if (variable_get("sms_sendtophone_filter_inline_default_icon_$format", 1)) {
@@ -226,7 +231,11 @@ function theme_sms_sendtophone_filter_inline_icon($variables) {
     $icon_path = variable_get("sms_sendtophone_filter_inline_custom_icon_path_$format", '');
   }
 
-  $icon = theme('image', array('path' => $icon_path, 'width' => t(variable_get("sms_sendtophone_filter_inline_display_text_$format", 'Send to phone')), 'height' => t('Send the highlighted text via SMS.')));
+  $icon = theme('image', array('path' => array(
+      'path' => $icon_path,
+      'width' => t(variable_get("sms_sendtophone_filter_inline_display_text_$format", 'Send to phone')),
+      'height' => t('Send the highlighted text via SMS.'),
+    )));
 
   $link = l($icon, 'sms/sendtophone/inline', array('attributes' => array('title' => t('Send the highlighted text via SMS.'), 'class' => 'sms-sendtophone'), 'query' => 'text=' . urlencode($text) . '&' . drupal_get_destination(), 'html' => TRUE));
 
@@ -339,7 +348,8 @@ function sms_sendtophone_field_formatter($field, $item, $formatter, $node) {
  * @todo Please document this function.
  * @see http://drupal.org/node/1354
  */
-function theme_sms_sendtophone_field($variables) {
+function theme_sms_sendtophone_field() {
+  // TODO Number of parameters in this theme funcion does not match number of parameters found in hook_theme.
   $text = $variables['text'];
   $link = l(t('Send to phone'), 'sms/sendtophone/field', array('attributes' => array('title' => t('Send this text via SMS.'), 'class' => 'sms-sendtophone'), 'query' => 'text=' . urlencode($text) . '&' . drupal_get_destination()));
 
@@ -375,7 +385,7 @@ function sms_sendtophone_form($form, $form_state, $type) {
           '#description' => t('This URL will be sent to the phone.'),
           '#cols' => 35,
           '#rows' => 2,
-          '#attributes' => array('disabled' => true),
+          '#attributes' => array('disabled' => TRUE),
           '#default_value' => url('node/' . $node->nid, array('absolute' => TRUE)),
         );
       }
@@ -425,7 +435,7 @@ function sms_sendtophone_form_submit($form, &$form_state) {
 }
 
 /**
- * Implementation of hook_node_view
+ * Implements hook_node_view().
  * previously implemented in hook_link
  */
 function sms_sendtophone_node_view($node, $view_mode, $langcode) {
@@ -455,7 +465,7 @@ function sms_sendtophone_node_view($node, $view_mode, $langcode) {
  * @todo Please document this function.
  * @see http://drupal.org/node/1354
  */
-function sms_sendtophone_form_alter(&$form, $form_state, $form_id) {
+function sms_sendtophone_form_alter(&$form, &$form_state, $form_id) {
   if ($form_id == 'sms_sendtophone_inline_form' || $form_id == 'sms_sendtophone_node_form' || $form_id == 'sms_sendtophone_cck_form') {
     if (!user_access('send to any number')) {
       // Makes number field plain text
diff --git a/modules/sms_track/sms_track.admin.inc b/modules/sms_track/sms_track.admin.inc
index 615d9d5..e0f2386 100644
--- a/modules/sms_track/sms_track.admin.inc
+++ b/modules/sms_track/sms_track.admin.inc
@@ -68,7 +68,7 @@ function sms_track_settings_form_submit($form, &$form_state) {
     watchdog('sms_track', 'SMS Tracking archive collector enabled');
   }
 
-  drupal_set_message('Settings saved.');
+  drupal_set_message(t('Settings saved.'));
 }
 
 
diff --git a/modules/sms_track/sms_track.info b/modules/sms_track/sms_track.info
index 624ec06..c31588a 100644
--- a/modules/sms_track/sms_track.info
+++ b/modules/sms_track/sms_track.info
@@ -1,6 +1,16 @@
 name = Tracking
 description = Message archiving and delivery tracking for the SMS Framework.
-core = 6.x
+core = 7.x
 package = SMS Framework
 dependencies[] = sms
 dependencies[] = views
+
+files[] = views/sms_track.views.inc
+files[] = views/sms_track.views_default.inc
+files[] = views/sms_track_handler_field_direction.inc
+files[] = views/sms_track_handler_field_gateway.inc
+files[] = views/sms_track_handler_field_local_number.inc
+files[] = sms_track.admin.inc
+files[] = sms_track.install
+files[] = sms_track.module
+
diff --git a/modules/sms_track/sms_track.install b/modules/sms_track/sms_track.install
index 2b31ad6..7593b9b 100644
--- a/modules/sms_track/sms_track.install
+++ b/modules/sms_track/sms_track.install
@@ -9,7 +9,26 @@
 
 
 /**
- * Implement hook_schema()
+ * Implement hook_install().
+ *
+function sms_track_install() {
+  drupal_install_schema('sms_track');
+}
+ * This should not be needed in D7
+ */
+
+/**
+ * Implement hook_uninstall().
+ *
+function sms_track_uninstall() {
+  drupal_uninstall_schema('sms_track');
+}
+ * This should not be needed in D7
+ */
+
+
+/**
+ * Implement hook_schema().
  *
  * @return
  *   Drupal schema array
diff --git a/modules/sms_track/sms_track.module b/modules/sms_track/sms_track.module
index 7f67f3b..38eb926 100644
--- a/modules/sms_track/sms_track.module
+++ b/modules/sms_track/sms_track.module
@@ -10,7 +10,7 @@
 
 
 /**
- * Implement hook_help()
+ * Implements hook_help().
  *
  * @param $path
  *   URI string
@@ -24,7 +24,7 @@ function sms_track_help($path, $arg) {
   $output = '';
   switch ($path) {
     case "admin/help#sms_track":
-      $output = '<p>'.  t("<i>TO BE RE-WRITTEN</i> This module simply records all incoming and outgoing SMS messages from the SMS Framework. This can be used to enable an audit trail for your system, so that you can investigate any issues and keen an eye on what is flowing in and out.<br /><br /><strong>Outgoing: </strong>It is important to be aware of your outgoing SMS traffic because user spamming can be expensive for you, and may also expose you to legal issues. A small code error can cause this kind of problem.<br /><br /><strong>Incoming: </strong>The proper handling of inbound messages depends heavily on the quality of your software code. You may find that someone is sending malformed messages to your gateway receiver, which could expose a vulnerability and potentially release sensitive data.<br /><br />This module uses a database table to store messages, and implements Views for reporting.") .'</p>';
+      $output = '<p>' . t("<i>TO BE RE-WRITTEN</i> This module simply records all incoming and outgoing SMS messages from the SMS Framework. This can be used to enable an audit trail for your system, so that you can investigate any issues and keen an eye on what is flowing in and out.<br /><br /><strong>Outgoing: </strong>It is important to be aware of your outgoing SMS traffic because user spamming can be expensive for you, and may also expose you to legal issues. A small code error can cause this kind of problem.<br /><br /><strong>Incoming: </strong>The proper handling of inbound messages depends heavily on the quality of your software code. You may find that someone is sending malformed messages to your gateway receiver, which could expose a vulnerability and potentially release sensitive data.<br /><br />This module uses a database table to store messages, and implements Views for reporting.") . '</p>';
       break;
   }
   return $output;
@@ -32,7 +32,7 @@ function sms_track_help($path, $arg) {
 
 
 /**
- * Implement hook_enable()
+ * Implements hook_enable().
  */
 function sms_track_enable() {
   if (variable_get('sms_track_archive_dir', SMS_DIR_NONE)) {
@@ -42,7 +42,7 @@ function sms_track_enable() {
 
 
 /**
- * Implement hook_disable()
+ * Implements hook_disable().
  */
 function sms_track_disable() {
   if (variable_get('sms_track_archive_dir', SMS_DIR_NONE)) {
@@ -52,22 +52,22 @@ function sms_track_disable() {
 
 
 /**
- * Implement hook_cron()
+ * Implements hook_cron().
  */
 function sms_track_cron() {
   // Ensure that we only run at most once per hour
   $last_run = variable_get('sms_track_archive_cron_last_run', 0);
-  $time_an_hour_ago = time() - 3600;
+  $time_an_hour_ago = REQUEST_TIME - 3600;
   if ($last_run <= $time_an_hour_ago) {
     // Purge the archive
     sms_track_archive_purge();
-    variable_set('sms_track_archive_cron_last_run', time());
+    variable_set('sms_track_archive_cron_last_run', REQUEST_TIME);
   }
 }
 
 
 /**
- * Implement hook_menu()
+ * Implements hook_menu().
  *
  * @return
  *   Drupal menu items array
@@ -76,26 +76,26 @@ function sms_track_menu() {
   $items = array();
 
   $items['admin/smsframework/sms_track'] = array(
-    'title'  => 'Message tracking',
+    'title' => 'Message tracking',
     'description' => 'Options and view for the message archive.',
-    'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('sms_track_settings_form'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('sms_track_settings_form'),
     'access arguments' => array('administer smsframework'),
-    'file'   => 'sms_track.admin.inc',
+    'file' => 'sms_track.admin.inc',
   );
   $items['admin/smsframework/sms_track/settings'] = array(
-    'title'  => 'Settings',
-    'type'   => MENU_DEFAULT_LOCAL_TASK,
+    'title' => 'Settings',
+    'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => 0,
   );
   $items['admin/smsframework/sms_track/view'] = array(
-    'title'  => 'View the archive',
-    'file'   => 'sms_track.admin.inc',
-    'page callback'    => 'sms_track_admin_view',
+    'title' => 'View the archive',
+    'file' => 'sms_track.admin.inc',
+    'page callback' => 'sms_track_admin_view',
     'access arguments' => array('administer smsframework'),
-    'type'   => MENU_LOCAL_TASK,
+    'type' => MENU_LOCAL_TASK,
     'weight' => 1,
-    'file'   => 'sms_track.admin.inc',
+    'file' => 'sms_track.admin.inc',
   );
 
   return $items;
@@ -103,7 +103,7 @@ function sms_track_menu() {
 
 
 /**
- * Implement hook_sms_send()
+ * Implements hook_sms_send().
  */
 function sms_track_sms_send($number, $message, &$options = array(), $gateway) {
   // Tracking
@@ -114,7 +114,7 @@ function sms_track_sms_send($number, $message, &$options = array(), $gateway) {
 }
 
 /**
- * Implement hook_sms_send_process()
+ * Implements hook_sms_send_process().
  *
  * @param $op
  *   Operation string (pre process, process, post process)
@@ -132,7 +132,7 @@ function sms_track_sms_send($number, $message, &$options = array(), $gateway) {
 function sms_track_sms_send_process($op, $number, $message, $options, $gateway, $result) {
   if ($op == 'post process') {
     // Archiving
-    $dir = 0;  // outgoing
+    $dir = 0; // outgoing
     $options['gateway_id'] = $gateway['identifier'];
     $options['result']  = $result;
     $result = sms_track_archive_write($dir, $number, $message, $options);
@@ -141,7 +141,7 @@ function sms_track_sms_send_process($op, $number, $message, $options, $gateway,
 
 
 /**
- * Implement hook_sms_incoming()
+ * Implements hook_sms_incoming().
  *
  * @param $op
  *   SMS incoming operation string (pre process, process, post process)
@@ -155,14 +155,14 @@ function sms_track_sms_send_process($op, $number, $message, $options, $gateway,
 function sms_track_sms_incoming($op, $number, $message, $options) {
   if ($op == 'pre process') {
     // Archiving
-    $dir = 1;  // incoming
+    $dir = 1; // incoming
     sms_track_archive_write($dir, $number, $message, $options);
   }
 }
 
 
 /**
- * Implement hook_sms_receipt()
+ * Implements hook_sms_receipt().
  *
  * @param $op
  *   Operation string
@@ -211,7 +211,7 @@ function sms_track_archive_write($dir, $number, $message, $options = array()) {
       // Or render a status code from a simple true/false result
       if (! $status) {
         if ($options['result']) {
-          $status = SWS_GW_OK;
+          $status = SMS_GW_OK;
         }
         else {
           $status = SMS_GW_ERR_OTHER;
@@ -235,22 +235,33 @@ function sms_track_archive_write($dir, $number, $message, $options = array()) {
   }
 
   $gw_number = (array_key_exists('gw_number', $options)) ? $options['gw_number'] : NULL;
-  $created   = time();
+  $created   = REQUEST_TIME;
   $options_z = serialize($options);
 
-  $result = db_query("INSERT INTO {sms_track} (reference, dir, number,
-    gw_number, message, status, created, options) VALUES
-    ('%s', %d, '%s', '%s', '%s', %d, %d, '%s')", $reference, $dir,
-    $number, $gw_number, $message, $status, $created, $options_z);
+  // @TODO Please review the conversion of this statement to the D7 database API syntax.
+  /* db_query("INSERT INTO {sms_track} (reference, dir, number,
+   gw_number, message, status, created, options) VALUES
+   ('%s', %d, '%s', '%s', '%s', %d, %d, '%s')", $reference, $dir, $number, $gw_number, $message, $status, $created, $options_z) */
+
+  $result = $id = db_insert('sms_track')
+  ->fields(array(
+    'reference' => $reference,
+    'dir' => $dir,
+    'number' => $number,
+    'gw_number' => $gw_number,
+    'message' => $message,
+    'status' => $status,
+    'created' => $created,
+    'options' => $options_z,
+  ))
+  ->execute();
 
   if (! $result) {
     $to_from = ($dir == 0) ? 'To' : 'From';
-    watchdog('sms_track', 'Failed to record message: '.
-      $to_from .' '. $number .': '. $message);
+    watchdog('sms_track', 'Failed to record message: ' . $to_from . ' ' . $number . ': ' . $message);
   }
 }
 
-
 /**
  * Update a message with a new status code
  *
@@ -262,27 +273,35 @@ function sms_track_archive_write($dir, $number, $message, $options = array()) {
  * @todo Log record handling
  */
 function sms_track_update_message($reference, $status) {
-  $updated = time();
+  $updated = REQUEST_TIME;
 
-  watchdog('sms_track', $reference.' '.$status.' '.$updated);
+  watchdog('sms_track', $reference . ' ' . $status . ' ' . $updated);
 
-  $result = db_query('UPDATE {sms_track} SET status = %d, updated = %d WHERE reference = \'%s\'', $status, $updated, $reference);
+  // @TODO Please review the conversion of this statement to the D7 database API syntax.
+  /* db_query('UPDATE {sms_track} SET status = %d, updated = %d WHERE reference = \'%s\'', $status, $updated, $reference) */
+  $result = db_update('sms_track')
+  ->fields(array(
+    'status' => $status,
+    'updated' => $updated,
+  ))
+  ->condition('reference', $reference)
+  ->execute();
 
   if ($result) {
-    watchdog('sms_track', 'Affected rows: '. db_affected_rows($result));
+    watchdog('sms_track', 'Affected rows: ' . db_affected_rows($result));
   }
 }
 
 
 /**
- * Implement hook_views_api()
+ * Implements hook_views_api().
  *
  * @return
  *   Views API required information array
  */
 function sms_track_views_api() {
   return array(
-    'api'  => 2,
+    'api' => 2,
     'path' => drupal_get_path('module', 'sms_track') . '/views',
   );
 }
@@ -302,7 +321,11 @@ function sms_track_archive_purge($max_age_days = NULL) {
   // Purge with no survivors
   if ($max_age_days > 0) {
     $max_age_secs = $max_age_days * 86400;
-    $oldest = time() - $max_age_secs;
-    $result = db_query("DELETE FROM {sms_track} WHERE created < %d", $oldest);
+    $oldest = REQUEST_TIME - $max_age_secs;
+    // @TODO Please review the conversion of this statement to the D7 database API syntax.
+    /* db_query("DELETE FROM {sms_track} WHERE created < %d", $oldest) */
+    $result = db_delete('sms_track')
+    ->condition('created', $oldest, '<')
+    ->execute();
   }
 }
diff --git a/modules/sms_track/views/sms_track.views.inc b/modules/sms_track/views/sms_track.views.inc
index bae4c20..5c8cc04 100644
--- a/modules/sms_track/views/sms_track.views.inc
+++ b/modules/sms_track/views/sms_track.views.inc
@@ -9,7 +9,7 @@
 
 
 /**
- * Implement hook_views_handlers()
+ * Implement hook_views_handlers().
  */
 function sms_track_views_handlers() {
   return array(
@@ -32,10 +32,10 @@ function sms_track_views_handlers() {
 
 
 /**
- * Implement hook_views_data()
+ * Implement hook_views_data().
  */
 function sms_track_views_data() {
-  //TODO: I have left id and options out of this until I can work out how the handlers work.
+  // TODO: I have left id and options out of this until I can work out how the handlers work.
   return array(
     'sms_track' => array(
       'table' => array(
diff --git a/modules/sms_track/views/sms_track.views_default.inc b/modules/sms_track/views/sms_track.views_default.inc
index 0cce7ec..2159e55 100644
--- a/modules/sms_track/views/sms_track.views_default.inc
+++ b/modules/sms_track/views/sms_track.views_default.inc
@@ -9,7 +9,7 @@
 
 
 /**
- * Implement hook_views_default_views()
+ * Implement hook_views_default_views().
  */
 function sms_track_views_default_views() {
   $view = new view;
diff --git a/modules/sms_track/views/sms_track_handler_field_direction.inc b/modules/sms_track/views/sms_track_handler_field_direction.inc
index e0d926c..58fef23 100644
--- a/modules/sms_track/views/sms_track_handler_field_direction.inc
+++ b/modules/sms_track/views/sms_track_handler_field_direction.inc
@@ -9,7 +9,7 @@
 
 
 /**
- * Implement hook_handler_field()
+ * Implement hook_handler_field().
  */
 class sms_track_handler_field_direction extends views_handler_field {
   function render($values) {
@@ -20,7 +20,7 @@ class sms_track_handler_field_direction extends views_handler_field {
       case 1:
         return t('In');
     }
-    //else
+    // else
     return $value;
   }
 }
diff --git a/modules/sms_track/views/sms_track_handler_field_gateway.inc b/modules/sms_track/views/sms_track_handler_field_gateway.inc
index b965a30..6d9e68b 100644
--- a/modules/sms_track/views/sms_track_handler_field_gateway.inc
+++ b/modules/sms_track/views/sms_track_handler_field_gateway.inc
@@ -9,7 +9,7 @@
 
 
 /**
- * Implement hook_handler_field()
+ * Implement hook_handler_field().
  */
 class sms_track_handler_field_gateway extends views_handler_field {
   function render($values) {
diff --git a/modules/sms_track/views/sms_track_handler_field_local_number.inc b/modules/sms_track/views/sms_track_handler_field_local_number.inc
index 7a66dcd..38fe127 100644
--- a/modules/sms_track/views/sms_track_handler_field_local_number.inc
+++ b/modules/sms_track/views/sms_track_handler_field_local_number.inc
@@ -9,7 +9,7 @@
 
 
 /**
- * Implement hook_handler_field()
+ * Implement hook_handler_field().
  */
 class sms_track_handler_field_local_number extends views_handler_field {
   function render($values) {
diff --git a/modules/sms_user/sms_user.admin.inc b/modules/sms_user/sms_user.admin.inc
index a12e8b4..9262db2 100644
--- a/modules/sms_user/sms_user.admin.inc
+++ b/modules/sms_user/sms_user.admin.inc
@@ -21,12 +21,24 @@ function sms_user_admin_settings($form, &$form_state) {
   $form['sms_user_confirmation_message'] = array(
     '#type' => 'textfield',
     '#title' => t('Confirmation message format'),
-    '#default_value' => variable_get('sms_user_confirmation_message', '[site-name] confirmation code: [confirm-code]'),
+    '#default_value' => variable_get('sms_user_confirmation_message', '[site:name] confirmation code: '),
     '#description' => t('Specify the format for confirmation messages. Keep this as short as possible.'),
-    '#size' => 40,
+    '#size' => 140,
     '#maxlength' => 255,
   );
 
+  // Add the token help to a collapsed fieldset at the end of the configuration page.
+  $form['tokens']['token_help'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Available Tokens List'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['tokens']['token_help']['content'] = array(
+    '#theme' => 'token_tree',
+    '#token_types' => array(),
+  );
+  /*
   $form['tokens'] = array(
     '#type' => 'fieldset',
     '#title' => t('Available replacement patterns'),
@@ -35,7 +47,7 @@ function sms_user_admin_settings($form, &$form_state) {
   );
 
   $form['tokens']['content']['#value'] = theme('token_tree', array('token_types' => array('sms_user')));
-
+  */
   $form['sms_user_sleep'] = array(
     '#type' => 'checkbox',
     '#title' => t('Enable sleep hours'),
@@ -68,6 +80,19 @@ function sms_user_admin_settings($form, &$form_state) {
     '#default_value' => variable_get('sms_user_new_account_message', ''),
     '#description' => t('The message that will be sent to newly registered users.  Leave empty for no message.'),
   );
+  
+  // Add the token help to a collapsed fieldset at the end of the registration page.
+  $form['registration']['tokens']['token_help'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Available Tokens List'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['registration']['tokens']['token_help']['content'] = array(
+    '#theme' => 'token_tree',
+    '#token_types' => array(),
+  );
+  /*
   $form['registration']['tokens'] = array(
     '#type' => 'fieldset',
     '#title' => t('Available replacement patterns'),
@@ -76,7 +101,7 @@ function sms_user_admin_settings($form, &$form_state) {
   );
 
   $form['registration']['tokens']['content']['#value'] = theme('token_tree', array('token_types' => array('sms_user')));
-
+  */
   $form['sms_user_max_chars'] = array(
     '#type' => 'textfield',
     '#default_value' => variable_get('sms_user_max_chars', SMS_USER_MAX_CHARS),
diff --git a/modules/sms_user/sms_user.info b/modules/sms_user/sms_user.info
index cd20a7e..925b21a 100644
--- a/modules/sms_user/sms_user.info
+++ b/modules/sms_user/sms_user.info
@@ -8,3 +8,9 @@ core = 7.x
 files[] = includes/sms_user_handler_field_number.inc
 files[] = includes/sms_user_handler_field_status.inc
 files[] = includes/sms_user_handler_filter_status.inc
+files[] = sms_user.actions.inc
+files[] = sms_user.install
+files[] = sms_user.module
+files[] = sms_user.rules.inc
+files[] = sms_user.views.inc
+files[] = sms_user.views_default.inc
diff --git a/modules/sms_user/sms_user.module b/modules/sms_user/sms_user.module
index 3b062de..f017b6b 100644
--- a/modules/sms_user/sms_user.module
+++ b/modules/sms_user/sms_user.module
@@ -19,11 +19,11 @@ function sms_user_permission() {
   return array(
     'receive sms' => array(
       'title' => t('receive sms'),
-      'description' => t('TODO Add a description for \'receive sms\''),
+      'description' => t('Receive SMS from the site.'),
     ),
     'edit own sms number' => array(
       'title' => t('edit own sms number'),
-      'description' => t('TODO Add a description for \'edit own sms number\''),
+      'description' => t('Edit own SMS number.'),
     ),
   );
 }
@@ -69,7 +69,6 @@ function sms_user_edit_access($account) {
  */
 function sms_user_send($uid, $message) {
   $account = user_load($uid);
-  
   //Check if the user is enabled to receive SMS
   if (user_access('receive sms', $account)) {
     if ($account->sms_user[0]['status'] == 2) {
@@ -145,7 +144,7 @@ function _sms_user_sleep_active($account) {
  * Menu callback; provides the forms for adding and confirming a user's mobile number.
  */
 function sms_user_settings($account) {
-  dsm($account);
+  //dsm($account);
   switch (isset($account->sms_user) ? $account->sms_user['status'] : 0) {
     case 0:
       $output = drupal_get_form('sms_user_settings_add_form', $account);
@@ -166,8 +165,7 @@ function sms_user_settings($account) {
 }
 
 /**
- * @todo Please document this function.
- * @see http://drupal.org/node/1354
+ * Displays the user settings form.
  */
 function sms_user_settings_add_form($form, &$form_state, $account) {
   $form = sms_send_form();
@@ -184,8 +182,7 @@ function sms_user_settings_add_form($form, &$form_state, $account) {
 }
 
 /**
- * @todo Please document this function.
- * @see http://drupal.org/node/1354
+ * Validate the users number.
  */
 function sms_user_settings_add_form_validate($form, &$form_state) {
   if ($error = sms_user_validate_number($form_state['values']['number'])) {
@@ -220,7 +217,7 @@ function sms_user_settings_confirm_form($form, &$form_state, $account) {
   $form['number'] = array(
     '#type' => 'item',
     '#title' => t('Mobile phone number'),
-    '#value' => $account->sms_user['number'],
+    '#markup' => $account->sms_user['number'],
   );
   $form['confirm_code'] = array(
     '#type' => 'textfield',
@@ -290,7 +287,8 @@ function sms_user_settings_reset_form($form, &$form_state, $account) {
   $form['sms_user']['number'] = array(
     '#type' => 'item',
     '#title' => t('Your mobile phone number'),
-    '#value' => $account->sms_user['number'],
+    '#markup' => $account->sms_user['number'],
+//    '#markup' => $account->sms_user['number'] . '@' . $account->sms_user['gateway']['carrier'],
     '#description' => t('Your mobile phone number has been confirmed.'),
   );
 
@@ -450,8 +448,7 @@ function sms_user_user_view($account, $view_mode) {
     $account->content['sms']['number'] = array(
       '#type' => 'user_profile_item',
       '#title' => t(''),
-      // @todo - theme function to format number.
-      '#value' => isset($account->sms_user['number']) ? $account->sms_user['number'] : '',
+      '#markup' => isset($account->sms_user['number']) ? $account->sms_user['number'] : '',
     );
   }
 }
@@ -518,6 +515,7 @@ function sms_user_user_load($users) {
 
   foreach ($result as $record) {
     if (!empty($record->uid)) {
+      $record->gateway = unserialize($record->gateway);
       $users[$record->uid]->sms_user = (array)$record;
 
       // @todo: the following is to support messaging that expect account details
@@ -534,13 +532,13 @@ function sms_user_user_load($users) {
  */
 function sms_user_save(&$edit, &$account, $category) {
   if (($category == 'mobile' || $category == 'account') && isset($edit['sms_user'])) {
-    dsm($edit);
+    //dsm($edit);
     $number = (object)$edit['sms_user'];
     $number->uid = $account->uid;
-    dsm($number);
+    //dsm($number);
 
     $primary_keys = array();
-    if ($account->sms_user['status'] == SMS_USER_PENDING && $edit['sms_user']['status'] == SMS_USER_CONFIRMED) { // is existing
+    if (isset($account->sms_user) && $account->sms_user['status'] == SMS_USER_PENDING && $edit['sms_user']['status'] == SMS_USER_CONFIRMED) { // is existing
       $primary_keys = array('number');
     }
     drupal_write_record('sms_user', $number, $primary_keys);
@@ -563,17 +561,12 @@ function sms_user_save(&$edit, &$account, $category) {
  * @see sms_user_user()
  */
 function sms_user_delete($uid, $number = FALSE) {
-  $db_args = array($uid);
-
-  if ($number == FALSE) {
-    $number_where = '';
-  }
-  else {
-    $number_where = "AND number = '%s'";
-    $db_args[] = $number;
+  $query = db_delete('sms_user')
+    ->condition('uid', $uid);
+  if ($number) {
+    $query->condition('number', $number);
   }
-  // TODO Please convert this statement to the D7 database API syntax.
-  db_query("DELETE FROM {sms_user} WHERE uid = %d $number_where", $db_args);
+  $num_deleted = $query->execute();
 }
 
 /**
@@ -587,9 +580,16 @@ function sms_user_delete($uid, $number = FALSE) {
  *   Created message, ready to send to user.
  */
 function _sms_user_confirm_message($code) {
-  $text_format = variable_get('sms_user_confirmation_message', '[site-name] confirmation code: [confirm-code]');
-  // @todo: d7 token support
-  //$text = token_replace_multiple($text_format, array('sms_user' => array('confirm-code' => $code)));
+  $text_format = variable_get('sms_user_confirmation_message', '[site:name] confirmation code: ') . $code;
+  // $text = token_replace($text_format, array('sms_user' => array('confirm-code' => $code)));
+//  $text_format = variable_get('sms_user_confirmation_message', '[site:name] confirmation code: ').$code;
+//  $text = token_replace($text_format, array(');token_replace('[node:created:since]', array('node' => $node));
+dsm("line 586 sms_user.module");
+dsm($text_format);
+  $text = token_replace($text_format, array('sms_user' => $sms_user), array('confirm-code' => $code));
+//  $text = token_replace($text_format);
+//  $text_format = variable_get('sms_user_confirmation_message', '[site:name] confirmation code: ').$code;
+//  $text = token_replace($text_format);
   return $text;
 }
 
@@ -618,9 +618,11 @@ function sms_user_register() {
 }
 
 /**
- * Implements hook_token_values().
- */
-function sms_user_token_values($type, $object = NULL, $options = array()) {
+ * Implements hook_tokens().
+ * TODO put this in upgrade patch.
+ * @see http://drupal.org/documentation/modules/token/update/6/7
+ *
+function sms_user_tokens($type, $object = NULL, $options = array()) {
   global $user;
   $values = array();
 
@@ -631,11 +633,32 @@ function sms_user_token_values($type, $object = NULL, $options = array()) {
       break;
   }
   return $values;
+}*/
+function sms_user_tokens($type, $tokens, array $data = array(), array $options = array()) {
+//function sms_user_tokens($type, $object = NULL, $options = array()) {
+  global $user;
+  $replacements = array();
+
+  if ($type == 'sms_user') {
+    foreach ($tokens as $name => $original) {
+      switch ($name) {
+        case 'confirm-code':
+            $replacements[$original] = $object['confirm-code'];
+          break;
+        case 'mobile-url':
+            $replacements[$original] = url("user/$user->uid/edit/mobile", array('absolute' => TRUE));
+          break;
+      }
+    }
+  }
+  return $replacements;
 }
 
 /**
- * Implements hook_token_list().
- */
+ * Implements hook_token_info().
+ *
+ * @see http://drupal.org/documentation/modules/token/update/6/7
+ *
 function sms_user_token_list($type = 'all') {
   $tokens = array();
   if ($type == 'sms_user') {
@@ -644,6 +667,19 @@ function sms_user_token_list($type = 'all') {
   }
 
   return $tokens;
+}*/
+function sms_user_token_info() {
+  $info['tokens']['sms_user']['confirm-code'] = array(
+    'name' => t('Confirmation code'),
+    'description' => t('The mobile confirmation code for the user.'),
+    'type' => 'sms_user',
+  );
+  $info['tokens']['sms_user']['mobile-url'] = array(
+    'name' => t('Mobile URL'),
+    'description' => t('The URL for the user\'s mobile settings page.'),
+    'type' => 'sms_user',
+  );
+  return $info;
 }
 
 /**
@@ -844,7 +880,7 @@ function sms_user_authenticate($number) {
 }
 
 /**
- *
+ * Log the current user out.
  */
 function sms_user_logout() {
   global $user;
diff --git a/modules/sms_valid/sms_valid.admin.inc b/modules/sms_valid/sms_valid.admin.inc
index fa33b03..5abd30a 100644
--- a/modules/sms_valid/sms_valid.admin.inc
+++ b/modules/sms_valid/sms_valid.admin.inc
@@ -5,8 +5,7 @@
  *
  * @package sms
  * @subpackage sms_valid
- */ 
-
+ */
 
 /**
  * Validation settings form
@@ -16,8 +15,8 @@
  *
  * @ingroup forms
  */
-function sms_valid_admin_settings_form(&$form_state, $prefix = 64) {
-  $use_rulesets       = variable_get('sms_valid_use_rulesets',       FALSE);
+function sms_valid_admin_settings_form($form, &$form_state, $prefix = 64) {
+  $use_rulesets = variable_get('sms_valid_use_rulesets', FALSE);
   $use_global_ruleset = variable_get('sms_valid_use_global_ruleset', FALSE);
 
   // Mode selector
@@ -28,57 +27,57 @@ function sms_valid_admin_settings_form(&$form_state, $prefix = 64) {
     $mode = 0;
   }
   $form['mode'] = array(
-    '#type'  => 'radios',
+    '#type' => 'radios',
     '#title' => 'Number validation',
     '#default_value' => $mode,
     '#options' => array(
-        0 => 'No rulesets. Only use validation hooks implemented by gateway or other modules. [default]',
-        1 => 'Use prefix-based validation rulesets.',
-        2 => 'Use one ruleset for all numbers.',
+      0 => 'No rulesets. Only use validation hooks implemented by gateway or other modules. [default]',
+      1 => 'Use prefix-based validation rulesets.',
+      2 => 'Use one ruleset for all numbers.',
     ),
     '#description' => t('Note that this will revert to the default option when the SMS Validation module is disabled.'),
   );
 
   // Global ruleset section
   $form['global'] = array(
-    '#type'  => 'fieldset',
+    '#type' => 'fieldset',
     '#title' => 'Global ruleset selection',
     '#collapsible' => TRUE,
-    '#collapsed'   => TRUE,
+    '#collapsed' => TRUE,
   );
 
   $form['global']['note'] = array(
-    '#type'  => 'item',
+    '#type' => 'item',
     '#value' => 'Identifies the ruleset that will be used for all numbers if the "Use one ruleset" option is selected.',
   );
 
   $form['global']['global_ruleset'] = array(
-    '#type'    => 'select',
-    '#title'   => t('Ruleset to use as the global ruleset'),
+    '#type' => 'select',
+    '#title' => t('Ruleset to use as the global ruleset'),
     '#options' => sms_valid_get_rulesets_for_form(),
     '#default_value' => variable_get('sms_valid_global_ruleset', $prefix),
   );
 
   // Local number ruleset section
   $form['local'] = array(
-    '#type'  => 'fieldset',
+    '#type' => 'fieldset',
     '#title' => 'Local number detection and handling',
     '#collapsible' => TRUE,
-    '#collapsed'   => TRUE,
+    '#collapsed' => TRUE,
   );
 
   $form['local']['note'] = array(
-    '#type'  => 'item',
+    '#type' => 'item',
     '#value' => 'You can this this to tell the number validation function that any number with this prefix should be considered a local number. The prefix will be stripped away and the number will be validated against the selected ruleset.',
   );
 
   $form['local']['local_number_prefix'] = array(
-    '#type'  => 'textfield',
+    '#type' => 'textfield',
     '#title' => t('Prefix that identifies a local number'),
-    '#size'          => 8,
-    '#maxlength'     => 20,
+    '#size' => 8,
+    '#maxlength' => 20,
     '#default_value' => variable_get('sms_valid_local_number_prefix', 0),
-    '#description'   => 'Set to blank to disable local number identification.',
+    '#description' => 'Set to blank to disable local number identification.',
     '#disabled' => ($use_global_ruleset) ? TRUE : FALSE,
   );
 
@@ -93,19 +92,19 @@ function sms_valid_admin_settings_form(&$form_state, $prefix = 64) {
 
   // Last resort ruleset section
   $form['last'] = array(
-    '#type'  => 'fieldset',
+    '#type' => 'fieldset',
     '#title' => 'Last resort for undetected ruleset prefixes',
     '#collapsible' => TRUE,
-    '#collapsed'   => TRUE,
+    '#collapsed' => TRUE,
   );
 
   $form['last']['note'] = array(
-    '#type'  => 'item',
+    '#type' => 'item',
     '#value' => 'If the number validation function cannot find a ruleset to use (ie. it cannot find a prefix match) you can tell it to try a last resort ruleset.<br /><strong>WARNING!</strong> Please be very careful when using this option to ensure that you do not have unexpected behavior in your rulesets.',
   );
 
   $form['last']['last_resort_enabled'] = array(
-    '#type'  => 'checkbox',
+    '#type' => 'checkbox',
     '#title' => t('Use last resort ruleset'),
     '#default_value' => variable_get('sms_valid_last_resort_enabled', FALSE),
     '#disabled' => ($use_global_ruleset) ? TRUE : FALSE,
@@ -128,7 +127,6 @@ function sms_valid_admin_settings_form(&$form_state, $prefix = 64) {
   return $form;
 }
 
-
 /**
  * @see sms_valid_admin_settings_form()
  */
@@ -151,67 +149,69 @@ function sms_valid_admin_settings_form_submit(&$form, $form_state) {
   variable_set('sms_valid_last_resort_enabled',  $last_resort_enabled);
   variable_set('sms_valid_last_resort_ruleset',  $last_resort_ruleset);
 
-  drupal_set_message('Settings saved.');
+  drupal_set_message(t('Settings saved.'));
 }
 
-
 /**
  * Validation rulesets list form
  *
  * @ingroup forms
  */
-function sms_valid_admin_rulesets_form(&$form_state) {
+function sms_valid_admin_rulesets_form($form, &$form_state) {
   $rulesets = sms_valid_get_all_rulesets();
-  
+
   $form['note'] = array(
-    '#type'  => 'item',
+    '#type' => 'item',
     '#value' => t('A ruleset is a number prefix with a set of deeper number prefixes, each with an allow/deny directive. For example, a ruleset prefix "64" and a rule like "21+" would allow a number like "64-21-123-4567". You can choose to have one big ruleset or you can split them into manageable rulesets by country, category, or whatever you decide.'),
   );
 
   foreach ($rulesets as $r) {
-    $prefix    = $r['prefix'];
+    $prefix = $r['prefix'];
     $qty_rules = count($r['rules']);
-    $rule_edit = '  ('. l('edit', "admin/smsframework/validation/ruleset/$prefix") .')';
+    $rule_edit = ' (' . l(t('edit'), "admin/smsframework/validation/ruleset/$prefix") . ')';
 
     $form[$prefix]['prefix'] = array(
-      '#type'  => 'textfield',
-      '#size'  => 5,
+      '#type' => 'textfield',
+      '#size' => 5,
       '#maxlength' => 5,
-      '#disabled'  => TRUE,
-      '#value' => $r['prefix']);
-    $form[$prefix]['name']   = array('#value' => $r['name']);
-    $form[$prefix]['iso2']   = array('#value' => $r['iso2']);
+      '#disabled' => TRUE,
+      '#value' => $r['prefix'],
+    );
+    $form[$prefix]['name'] = array('#value' => $r['name']);
+    $form[$prefix]['iso2'] = array('#value' => $r['iso2']);
     $form[$prefix]['qty_rules'] = array('#value' => $qty_rules . $rule_edit);
-    $form[$prefix][$prefix.'_out'] = array(
-      '#type'  => 'checkbox',
+    $form[$prefix][$prefix . '_out'] = array(
+      '#type' => 'checkbox',
       '#title' => 'Outbound',
       '#default_value' => sms_valid_ruleset_is_enabled($prefix, SMS_DIR_OUT),
     );
-    $form[$prefix][$prefix.'_in']  = array(
-      '#type'  => 'checkbox',
+    $form[$prefix][$prefix . '_in']  = array(
+      '#type' => 'checkbox',
       '#title' => 'Inbound',
       '#default_value' => sms_valid_ruleset_is_enabled($prefix, SMS_DIR_IN),
     );
-    $form[$prefix][$prefix.'_delete']  = array(
-      '#type'  => 'checkbox',
+    $form[$prefix][$prefix . '_delete']  = array(
+      '#type' => 'checkbox',
       '#title' => 'Delete',
       '#default_value' => FALSE,
     );
   }
 
   $form['submit'] = array(
-    '#type'  => 'submit',
+    '#type' => 'submit',
     '#value' => t('Save Changes'),
   );
 
   return $form;
 }
 
-
 /**
  * @see sms_valid_admin_rulesets_form()
  */
-function theme_sms_valid_admin_rulesets_form($form) {
+function theme_sms_valid_admin_rulesets_form($variables) {
+//  dsm($variables);
+  $variables = $variables['form'];
+  $form = $variables['form'];
   $rows = array();
   foreach ($form as $prefix => $element) {
     if (isset($element['prefix']) && is_array($element['prefix'])) {
@@ -220,25 +220,36 @@ function theme_sms_valid_admin_rulesets_form($form) {
         drupal_render($element['name']),
         drupal_render($element['iso2']),
         drupal_render($element['qty_rules']),
-        drupal_render($element[$prefix.'_out']),
-        drupal_render($element[$prefix.'_in']),
-        drupal_render($element[$prefix.'_delete']),
+        drupal_render($element[$prefix . '_out']),
+        drupal_render($element[$prefix . '_in']),
+        drupal_render($element[$prefix . '_delete']),
       );
       unset($form[$prefix]);
     }
   }
 
-  $output .= drupal_render($form['note']);
+  $output .= drupal_render_children($form['note']);
   unset($form['note']);
 
-  $header = array(t('Prefix'), t('Name'), t('Country'), t('Qty Rules'), array('data' => t('Allow messages'), 'colspan' => 2), t('Delete'));
-  $output .= theme('table', $header, $rows);
-  $output .= drupal_render($form);
+  $header = array(
+    t('Prefix'),
+    t('Name'),
+    t('Country'),
+    t('Qty Rules'), array(
+      'data' => t('Allow messages'),
+      'colspan' => 2,
+    ),
+    t('Delete'));
+  $output .= theme('table', array(
+    'header' => array(
+      'header' => $header,
+      'rows' => $rows,
+    )));
+  $output .= drupal_render_children($form);
 
   return $output;
 }
 
-
 /**
  * @see sms_valid_admin_rulesets_form()
  */
@@ -251,12 +262,12 @@ function sms_valid_admin_rulesets_form_submit($form, &$form_state) {
       if ($items[1] == 'out') {
         $prefix = $items[0];
 
-        $out = $form_state['values'][$prefix.'_out'];
-        $in  = $form_state['values'][$prefix.'_in'];
+        $out = $form_state['values'][$prefix . '_out'];
+        $in  = $form_state['values'][$prefix . '_in'];
         sms_valid_ruleset_set_status($prefix, sms_dir($out, $in));
 
         // Handle deletes
-        $delete = $form_state['values'][$prefix.'_delete'];
+        $delete = $form_state['values'][$prefix . '_delete'];
         if ($delete) {
           sms_valid_delete_ruleset($prefix);
         }
@@ -264,10 +275,9 @@ function sms_valid_admin_rulesets_form_submit($form, &$form_state) {
     }
   }
   drupal_set_message(t('Rulesets saved.'));
-  //$form_state['redirect'] = 'admin/smsframework/validation/countrycodes';
+  // $form_state['redirect'] = 'admin/smsframework/validation/countrycodes';
 }
 
-
 /**
  * Validation ruleset editing form
  *
@@ -276,7 +286,7 @@ function sms_valid_admin_rulesets_form_submit($form, &$form_state) {
  *
  * @ingroup forms
  */
-function sms_valid_admin_ruleset_form(&$form_state, $prefix = NULL) {
+function sms_valid_admin_ruleset_form($form, &$form_state, $prefix = NULL) {
   if ($prefix === NULL) {
     $ruleset = FALSE;
   }
@@ -287,7 +297,8 @@ function sms_valid_admin_ruleset_form(&$form_state, $prefix = NULL) {
   // Ruleset selection area
   $form['title'] = array(
     '#type' => 'item',
-    '#value' => t('Select a ruleset from the drop-down box and click Refresh to update the ruleset form below'),
+//    '#value' => t('Select a ruleset from the drop-down box and click Refresh to update the ruleset form below.'),
+    '#markup' => t('Select a ruleset from the drop-down box and click Refresh to update the ruleset form below.'),
   );
 
   $form['select_prefix'] = array(
@@ -304,24 +315,24 @@ function sms_valid_admin_ruleset_form(&$form_state, $prefix = NULL) {
 
   // Ruleset editor area
   $form['ruleset'] = array(
-    '#type'  => 'fieldset',
+    '#type' => 'fieldset',
     '#title' => 'Ruleset',
   );
 
   // If this is a new ruleset then this should be a textfield
   $form['ruleset']['prefix'] = array(
-    '#type'      => 'textfield',
-    '#title'     => t('Prefix'),
-    '#size'      => 5,
+    '#type' => 'textfield',
+    '#title' => t('Prefix'),
+    '#size' => 5,
     '#maxlength' => 5,
-    '#value'     => ($ruleset) ? $ruleset['prefix'] : '',
+    '#value' => ($ruleset) ? $ruleset['prefix'] : '',
     '#description' => 'Should be 4 digits or less. Highest allowed prefix is 65535.',
   );
 
   $form['ruleset']['name'] = array(
-    '#type'      => 'textfield',
-    '#title'     => t('Name'),
-    '#size'      => 80,
+    '#type' => 'textfield',
+    '#title' => t('Name'),
+    '#size' => 80,
     '#maxlength' => 200,
     '#default_value' => ($ruleset) ? $ruleset['name'] : '',
   );
@@ -331,17 +342,17 @@ function sms_valid_admin_ruleset_form(&$form_state, $prefix = NULL) {
     $options[''] = '(none)';
     $options = array_merge($options, countries_api_get_array());
     $form['ruleset']['iso2'] = array(
-      '#type'    => 'select',
-      '#title'   => t('Associated country (optional)'),
+      '#type' => 'select',
+      '#title' => t('Associated country (optional)'),
       '#options' => $options,
-      '#default_value' => ($ruleset) ? $ruleset['iso2']: '',
+      '#default_value' => ($ruleset) ? $ruleset['iso2'] : '',
     );
   }
   else {
     $form['ruleset']['iso2'] = array(
-      '#type'      => 'textfield',
-      '#title'     => t('Country code (ISO 3166-1 alpha-2) (optional)'),
-      '#size'      => 2,
+      '#type' => 'textfield',
+      '#title' => t('Country code (ISO 3166-1 alpha-2) (optional)'),
+      '#size' => 2,
       '#maxlength' => 2,
       '#default_value' => ($ruleset) ? $ruleset['iso2'] : '',
       '#description' => 'This would be a nice drop-down box if you had the Countries API module enabled.',
@@ -349,28 +360,28 @@ function sms_valid_admin_ruleset_form(&$form_state, $prefix = NULL) {
   }
 
   $form['ruleset']['out'] = array(
-    '#type'  => 'checkbox',
+    '#type' => 'checkbox',
     '#title' => 'Allow outbound communication',
     '#default_value' => ($ruleset) ? sms_valid_ruleset_is_enabled($prefix, SMS_DIR_OUT) : 0,
   );
 
   $form['ruleset']['in']  = array(
-    '#type'  => 'checkbox',
+    '#type' => 'checkbox',
     '#title' => 'Allow inbound commmunication',
     '#default_value' => ($ruleset) ? sms_valid_ruleset_is_enabled($prefix, SMS_DIR_IN) : 0,
   );
 
   $form['ruleset']['rules'] = array(
-    '#type'  => 'textarea',
+    '#type' => 'textarea',
     '#title' => 'Rules',
-    '#cols'  => 80,
-    '#rows'  => 15,
+    '#cols' => 80,
+    '#rows' => 15,
     '#default_value' => ($ruleset) ? sms_valid_rules_to_text($ruleset['rules']) : '',
     '#description' => t('One rule per line. Enter a number prefix (any length), not including the ruleset prefix.<br />Any prefix with a "-" at the end signifies an expicit deny.<br />Any prefix with a "+" at the end signifies an explicit allow.<br />All other rules are ignored.<br />Default is to deny any numbers that do not match.<br />Comments must be prefixed with a hash (#). You may place comments in-line only.<br />See the guide at %url', array('%url' => 'http://moo0.net/smsframework/?q=node/19')),
   );
 
   $form['ruleset']['submit'] = array(
-    '#type'  => 'submit',
+    '#type' => 'submit',
     '#value' => t('Save Ruleset'),
   );
 
@@ -381,25 +392,38 @@ function sms_valid_admin_ruleset_form(&$form_state, $prefix = NULL) {
 /**
  * @see sms_valid_admin_ruleset_form()
  */
-function theme_sms_valid_admin_ruleset_form($form) {
+function theme_sms_valid_admin_ruleset_form($variables) {
+//  dsm($variables['form']);
+  $variables = $variables['form'];
+  $form = $variables['form'];
   $header = array();
   $rows   = array();
   $rows[] = array(
-    array('data' => '<strong>'. drupal_render($form['title']) .'</strong>',
-          'colspan' => 3),
+    array(
+      'data' => '<strong>' . drupal_render($form['title']) . '</strong>',
+      'colspan' => 3,
+    ),
   );
   $rows[] = array(
     drupal_render($form['select_prefix']),
-    drupal_render($form['select']),
-    array('data' => '', 'width' => '80%'),
+    drupal_render_children($form['select']),
+    array(
+      'data' => '',
+      'width' => '80%',
+    ),
   );
 
   unset($form['title']);
   unset($form['select_prefix']);
   unset($form['select']);
 
-  $output .= theme('table', $header, $rows);
-  $output .= drupal_render($form);
+  $output = theme(
+    'table', array(
+      'header' => array(
+        'header' => $header,
+        'rows' => $rows,
+  )));
+  $output .= drupal_render_children($form);
 
   return $output;
 }
@@ -410,7 +434,7 @@ function theme_sms_valid_admin_ruleset_form($form) {
  */
 function sms_valid_admin_ruleset_form_select(&$form, $form_state) {
   $prefix = $form_state['values']['select_prefix'];
-  $form['#redirect'] = "admin/smsframework/validation/ruleset/$prefix";
+  $form_state['#redirect'] = "admin/smsframework/validation/ruleset/$prefix";
 }
 
 
@@ -431,7 +455,7 @@ function sms_valid_admin_ruleset_form_submit(&$form, $form_state) {
 
   sms_valid_save_ruleset($ruleset);
 
-  drupal_set_message('Ruleset saved.');
+  drupal_set_message(t('Ruleset saved.'));
 }
 
 
@@ -443,7 +467,7 @@ function sms_valid_admin_ruleset_form_submit(&$form, $form_state) {
  *
  * @ingroup forms
  */
-function sms_valid_admin_test_form(&$form_state, $last_test_validation_number = NULL) {
+function sms_valid_admin_test_form($form, &$form_state, $last_test_validation_number = NULL) {
   $last_number = variable_get('sms_valid_test_last_number', '');
   $test_mode   = variable_get('sms_valid_test_mode', 0);
 
@@ -452,8 +476,8 @@ function sms_valid_admin_test_form(&$form_state, $last_test_validation_number =
     '#title' => 'Validator(s)',
     '#default_value' => $test_mode,
     '#options' => array(
-        0 => 'Test against rulesets only. Verbose feedback.',
-        1 => 'Test against the main validation function.',
+      0 => 'Test against rulesets only. Verbose feedback.',
+      1 => 'Test against the main validation function.',
     ),
     '#description' => 'The main validation function includes rulesets (if enabled), the active gateway module and other modules that implement number validation hooks.',
   );
@@ -481,36 +505,38 @@ function sms_valid_admin_test_form(&$form_state, $last_test_validation_number =
  */
 function sms_valid_admin_test_form_submit(&$form, $form_state) {
   $number = $form_state['values']['number'];
-  drupal_set_message('You entered '. $number);
+  drupal_set_message(check_plain(t('You entered ') . $number));
   variable_set('sms_valid_test_last_number', $number);
 
   $test_mode = $form_state['values']['test_mode'];
   variable_set('sms_valid_test_mode', $test_mode);
 
   if (! variable_get('sms_valid_use_rulesets', FALSE)) {
-    drupal_set_message('Note: Rulesets are disabled.');
+    drupal_set_message(t('Note: Rulesets are disabled.'));
   }
 
   switch ($test_mode) {
-    case 0:  // Test only against rulesets
+    case 0: // Test only against rulesets
       $options = array('test' => TRUE);
       $result  = sms_valid_validate($number, $options);
       $pass    = $result['pass'];
       $log_msg = implode("<br />", $result['log']);
-      drupal_set_message($log_msg);
+      drupal_set_message(check_plain($log_msg));
       break;
-    case 1:  // Test main validation function
+    case 1: // Test main validation function
       $error = sms_validate_number($number);
       if ($error) {
-        drupal_set_message('Error message from validation function:<br />'.$error);
+        // TODO This is inserting the <br /> in the message instead of breaking the line.
+        drupal_set_message(check_plain('Error message from validation function:<br />' . $error));
       }
       $pass = (! $error);
       break;
   }
 
   if ($pass) {
-    drupal_set_message('Validation succeeded and returned number '. $number);
-  } else {
+    drupal_set_message(check_plain(t('Validation succeeded and returned number ') . $number));
+  }
+  else {
     form_set_error('number', 'Validation FAILED');
   }
 }
diff --git a/modules/sms_valid/sms_valid.info b/modules/sms_valid/sms_valid.info
index 4d442ac..f48e18a 100644
--- a/modules/sms_valid/sms_valid.info
+++ b/modules/sms_valid/sms_valid.info
@@ -1,6 +1,11 @@
 name = Validation
 description = Provides number validation features to the SMS Framework.
-core = 6.x
+core = 7.x
 
 package = SMS Framework
 dependencies[] = sms
+
+files[] = sms_valid.admin.inc
+files[] = sms_valid.install
+files[] = sms_valid.module
+
diff --git a/modules/sms_valid/sms_valid.install b/modules/sms_valid/sms_valid.install
index a5ce722..6ecaf98 100644
--- a/modules/sms_valid/sms_valid.install
+++ b/modules/sms_valid/sms_valid.install
@@ -5,32 +5,34 @@
  *
  * @package sms
  * @subpackage sms_valid
- */ 
+ */
 
 
 /**
- * Implementation of hook_install().
+ * Implements hook_install().
  *
  * @ingroup hooks
  */
 function sms_valid_install() {
-  drupal_install_schema('sms_valid');
+  // TODO The drupal_(un)install_schema functions are called automatically in D7.
+  // drupal_install_schema('sms_valid')
   sms_valid_create_example_ruleset();
 }
 
 
 /**
- * Implementation of hook_uninstall().
+ * Implements hook_uninstall().
  *
  * @ingroup hooks
  */
-function sms_valid_uninstall() {
-  drupal_uninstall_schema('sms_valid');
-}
+// function sms_valid_uninstall() {
+// TODO The drupal_(un)install_schema functions are called automatically in D7.
+// drupal_uninstall_schema('sms_valid')
+// }
 
 
 /**
- * Implementation of hook_schema().
+ * Implements hook_schema().
  *
  * @return
  *   Drupal schema.
@@ -42,36 +44,41 @@ function sms_valid_schema() {
   $schema['sms_valid_rules'] = array(
     'fields' => array(
       'prefix' => array(
-              'type'      => 'int',
-              'size'      => 'small',
-              'unsigned'  => TRUE,
-              'not null'  => TRUE,
-              'description' => 'Number prefix/code; 1-65535'),
+        'type' => 'int',
+        'size' => 'small',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'description' => 'Number prefix/code; 1-65535',
+      ),
       'name' => array(
-              'type'      => 'varchar',
-              'length'    => 100,
-              'not null'  => TRUE,
-              'description' => 'Descriptive name for this prefix/ruleset'),
+        'type' => 'varchar',
+        'length' => 100,
+        'not null' => TRUE,
+        'description' => 'Descriptive name for this prefix/ruleset',
+      ),
       'rules' => array(
-              'type'      => 'text',
-              'not null'  => FALSE,
-              'serialize' => TRUE,
-              'description' => 'Serialized ruleset'),
+        'type' => 'text',
+        'not null' => FALSE,
+        'serialize' => TRUE,
+        'description' => 'Serialized ruleset',
+      ),
       'dirs_enabled' => array(
-              'type'      => 'int',
-              'size'      => 'tiny',
-              'not null'  => TRUE,
-              'default'   => 4,
-              'description' => 'Active msg directions. See SMS_DIR_* constants.'),
+        'type' => 'int',
+        'size' => 'tiny',
+        'not null' => TRUE,
+        'default' => 4,
+        'description' => 'Active msg directions. See SMS_DIR_* constants.',
+      ),
       'iso2' => array(
-              'type'      => 'varchar',
-              'length'    => 2,
-              'not null'  => FALSE,
-              'description' => 'ISO 3166-1 alpha-2 country code'),
+        'type' => 'varchar',
+        'length' => 2,
+        'not null' => FALSE,
+        'description' => 'ISO 3166-1 alpha-2 country code',
+      ),
     ),
     'primary key' => array('prefix'),
   );
-  
+
   return $schema;
 }
 
@@ -87,19 +94,56 @@ function sms_valid_create_example_ruleset(&$ret = array()) {
   $name   = 'New Zealand';
   $dirs_enabled = 4;
   $iso2  = 'NZ';
-  $rules = array('21' => array('allow' => TRUE,  'comment' => 'Vodafone'),
-                 '22' => array('allow' => TRUE,  'comment' => 'Two Degrees Mobile'),
-                 '25' => array('allow' => FALSE, 'comment' => 'Telecom AMPS+TDMA (old)'),
-                 '26' => array('allow' => FALSE, 'comment' => 'Telecom pager network'),
-                 '27' => array('allow' => TRUE,  'comment' => 'Telecom CDMA'),
-                 '28' => array('allow' => TRUE,  'comment' => 'Slingshot'),
-                 '29' => array('allow' => TRUE,  'comment' => 'TelstraClear'),
-                );
+  $rules = array(
+    '21' => array(
+      'allow' => TRUE,
+      'comment' => 'Vodafone',
+    ),
+    '22' => array(
+      'allow' => TRUE,
+      'comment' => 'Two Degrees Mobile',
+    ),
+    '25' => array(
+      'allow' => FALSE,
+      'comment' => 'Telecom AMPS+TDMA (old)',
+    ),
+    '26' => array(
+      'allow' => FALSE,
+      'comment' => 'Telecom pager network',
+    ),
+    '27' => array(
+      'allow' => TRUE,
+      'comment' => 'Telecom CDMA',
+    ),
+    '28' => array(
+      'allow' => TRUE,
+      'comment' => 'Slingshot',
+    ),
+    '29' => array(
+      'allow' => TRUE,
+      'comment' => 'TelstraClear',
+    ),
+  );
   $rules_z = serialize($rules);
 
-  $sql = "INSERT INTO {sms_valid_rules} (prefix,name,rules,dirs_enabled,iso2)".
+  $sql = "INSERT INTO {sms_valid_rules} (prefix,name,rules,dirs_enabled,iso2)" .
                                 "VALUES (%d, '%s', '%s', %d, '%s')";
-  $result = db_query($sql, $prefix, $name, $rules_z, $dirs_enabled, $iso2);
-  $ret[]  = array('success' => $result !== FALSE, 'query' => check_plain($sql));
-}
+  // @TODO Please convert this statement to the D7 database API syntax.
+//  $result = db_query($sql, $prefix, $name, $rules_z, $dirs_enabled, $iso2);
 
+  $result = db_insert('sms_valid_rules')
+  ->fields(array(
+    'prefix' => $prefix,
+    'name' => $name,
+    'rules' => $rules_z,
+    'dirs_enabled' => $dirs_enabled,
+    'iso2' => $iso2,
+  ))
+//  $result => $sql;
+  ->execute();
+
+  $ret[] = array(
+    'success' => $result !== FALSE,
+    'query' => check_plain($sql),
+  );
+}
diff --git a/modules/sms_valid/sms_valid.module b/modules/sms_valid/sms_valid.module
index 3b15d4b..31629a3 100644
--- a/modules/sms_valid/sms_valid.module
+++ b/modules/sms_valid/sms_valid.module
@@ -9,7 +9,7 @@
 
 
 /**
- * Implement hook_sms_validate()
+ * Implements hook_sms_validate().
  *
  * @param $op
  *   Validation operation to work on.
@@ -40,7 +40,7 @@ function sms_valid_sms_validate($op, &$number, &$options) {
 
 
 /**
- * Implementation of hook_menu()
+ * Implements hook_menu().
  *
  * @return
  *   Drupal menu item array.
@@ -51,58 +51,58 @@ function sms_valid_menu() {
   $items = array();
 
   $items['admin/smsframework/validation'] = array(
-    'title'  => 'Number validation',
+    'title' => 'Number validation',
     'description' => 'Configure number validation and rulesets.',
-    'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('sms_valid_admin_settings_form', NULL),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('sms_valid_admin_settings_form', NULL),
     'access arguments' => array('administer smsframework'),
-    'file'   => 'sms_valid.admin.inc',
+    'file' => 'sms_valid.admin.inc',
   );
 
   $items['admin/smsframework/validation/settings'] = array(
-    'title'  => 'Settings',
-    'type'   => MENU_DEFAULT_LOCAL_TASK,
+    'title' => 'Settings',
+    'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -10,
   );
 
   $items['admin/smsframework/validation/rulesets'] = array(
-    'title'  => 'Rulesets',
-    'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('sms_valid_admin_rulesets_form', NULL),
+    'title' => 'Rulesets',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('sms_valid_admin_rulesets_form', NULL),
     'access arguments' => array('administer smsframework'),
-    'type'   => MENU_LOCAL_TASK,
+    'type' => MENU_LOCAL_TASK,
     'weight' => -9,
-    'file'   => 'sms_valid.admin.inc',
+    'file' => 'sms_valid.admin.inc',
   );
 
   $items['admin/smsframework/validation/ruleset'] = array(
-    'title'  => 'Add/Edit ruleset',
-    'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('sms_valid_admin_ruleset_form'),
+    'title' => 'Add/Edit ruleset',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('sms_valid_admin_ruleset_form'),
     'access arguments' => array('administer smsframework'),
-    'type'   => MENU_LOCAL_TASK,
+    'type' => MENU_LOCAL_TASK,
     'weight' => -8,
-    'file'   => 'sms_valid.admin.inc',
+    'file' => 'sms_valid.admin.inc',
   );
 
   $items['admin/smsframework/validation/ruleset/%'] = array(
-    'title'  => 'Edit ruleset',
-    'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('sms_valid_admin_ruleset_form', 4),
+    'title' => 'Edit ruleset',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('sms_valid_admin_ruleset_form', 4),
     'access arguments' => array('administer smsframework'),
-    'type'   => MENU_CALLBACK,
+    'type' => MENU_CALLBACK,
     'weight' => -7,
-    'file'   => 'sms_valid.admin.inc',
+    'file' => 'sms_valid.admin.inc',
   );
 
   $items['admin/smsframework/validation/test'] = array(
-    'title'  => 'Test validation',
-    'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('sms_valid_admin_test_form', NULL),
+    'title' => 'Test validation',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('sms_valid_admin_test_form', NULL),
     'access arguments' => array('administer smsframework'),
-    'type'   => MENU_LOCAL_TASK,
+    'type' => MENU_LOCAL_TASK,
     'weight' => -6,
-    'file'   => 'sms_valid.admin.inc',
+    'file' => 'sms_valid.admin.inc',
   );
 
   return $items;
@@ -110,7 +110,7 @@ function sms_valid_menu() {
 
 
 /**
- * Implementation of hook_theme()
+ * Implements hook_theme().
  *
  * @return
  *   Array of Drupal theme items.
@@ -120,11 +120,11 @@ function sms_valid_menu() {
 function sms_valid_theme() {
   return array(
     'sms_valid_admin_rulesets_form' => array(
-      'arguments' => array('form' => NULL),
+      'render element' => 'form',
       'file' => 'sms_valid.admin.inc',
     ),
     'sms_valid_admin_ruleset_form' => array(
-      'arguments' => array('form' => NULL),
+      'render element' => 'form',
       'file' => 'sms_valid.admin.inc',
     ),
   );
@@ -141,15 +141,19 @@ function sms_valid_get_all_rulesets() {
   $result = db_query("SELECT * FROM {sms_valid_rules}");
 
   $rulesets = array();
-  while ($row = db_fetch_array($result)) {
+  foreach($result as $row) {
+    $prefix = $row->prefix;
+    $rulesets[$prefix] = $row;
+    $rulesets[$prefix->rules] = unserialize($row->rules);
+  }
+  /*while ($row = db_fetch_array($result)) {
     $prefix = $row['prefix'];
     $rulesets[$prefix] = $row;
     $rulesets[$prefix]['rules'] = unserialize($row['rules']);
-  }
+  }*/
   return $rulesets;
 }
 
-
 /**
  * Get a ruleset for a given prefix
  *
@@ -160,10 +164,12 @@ function sms_valid_get_all_rulesets() {
  *   A ruleset array or FALSE.
  */
 function sms_valid_get_ruleset($prefix) {
-  $result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix = %d LIMIT 1",
-                     $prefix);
-  if (db_affected_rows()) {
-    $ruleset = db_fetch_array($result);
+//  $result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix = :prefix LIMIT 1", array(':prefix' => $prefix));
+  // @TODO Lets try this to see if it is a good converstion from D6 to D7
+  $result = db_query_range("SELECT * FROM {sms_valid_rules} WHERE prefix = :prefix", 0, 1, array(':prefix' => $prefix))->fetchAssoc();
+  //if (db_affected_rows()) {
+  foreach($result as $ruleset) {
+    //$ruleset = db_fetch_array($result);
     $ruleset['rules'] = unserialize($ruleset['rules']);
     return $ruleset;
   }
@@ -185,20 +191,50 @@ function sms_valid_get_ruleset_for_number($number) {
   $number = preg_replace('/[^0-9]/', '', $number);
 
   // Make an array of potential prefixes from the given number
-  for ($i = 0; $i < strlen($number); $i++) {
-    $potential_prefixes[] = substr($number, 0, $i + 1);
+  for ($i = 0; $i < drupal_strlen($number); $i++) {
+    $potential_prefixes[] = drupal_substr($number, 0, $i + 1);
   }
-
+//  $result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix IN (" . db_placeholders($potential_prefixes, 'int') . ")", $potential_prefixes);
   // Get the potential rulesets from the DB
-  $result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix IN ("
-                     . db_placeholders($potential_prefixes, 'int') . ")",
-                     $potential_prefixes);
-
+  // TODO Please convert this statement to the D7 database API syntax.
+  //$result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix IN (" . db_placeholders($potential_prefixes, 'int') . ")", $potential_prefixes);
+//  $result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix= %prefix IN ", array(%prefix => $potential_prefixes));
+//  $result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix = :prefix", array(':prefix' => $potential_prefixes));
+  // Drupal6 $result = db_query("SELECT nid, title FROM {node} WHERE uid= %d IN (" . db_placeholders(array(page,story), varchar) . ")", 5, page, story);
+  // Drupal7 $result = db_query("SELECT nid, title FROM {node} WHERE uid= :uid AND type IN (:type)", array(:uid => 5, :type => array(page, story),));
+//  $placeholders = implode(', ', array_fill(0, count($potential_prefixes), "%d"));
+//  $result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix IN ($placeholders)", $potential_prefixes);
+
+  // Drupal 6
+//$result = db_query("SELECT nid, title FROM {node} WHERE uid = %d AND type IN (" . db_placeholders(array('page', 'story'), 'varchar') . ")", 5, 'page', 'story');
+//$result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix IN (" . db_placeholders($potential_prefixes, 'int') . ")", $potential_prefixes);
+
+// Drupal 7
+//$result = db_query("SELECT * FROM {sms_valid_rules} WHERE prefix = :prefix AND placeholders IN (:placeholders)", array(
+//  ':prefix' => $potential_prefixes,
+//  ':placeholders' => $placeholders,
+//));
+//dsm($potential_prefixes);
   // Choose the ruleset with the best match (most chars = highest prefix)
+/*  while ($row = db_fetch_array($result)) {
+    if ($row['prefix'] > $last_prefix) {
+      $best_ruleset = $row;
+    }
+    $best_ruleset['rules'] = unserialize($best_ruleset['rules']);
+  }
+*/
   $best_ruleset = NULL;
   $last_prefix  = NULL;
-  while ($row = db_fetch_array($result)) {
-    if ($row['prefix'] > $last_prefix) { $best_ruleset = $row; }
+//  $result = db_query("SELECT prefix, name, rules, dirs_enabled, iso2 FROM {sms_valid_rules} WHERE prefix = :prefix IN", array(':prefix' => $potential_prefixes));
+  $query = db_select('sms_valid_rules', 'u')
+    ->fields('u', array('prefix', 'name', 'rules', 'dirs_enabled', 'iso2'))
+    ->condition('prefix', $potential_prefixes, 'IN');
+  $result = $query->execute();
+  foreach($result as $row) {
+dsm("line 234 sms_valid.module");
+    if ($row->prefix > $last_prefix) {
+      $best_ruleset = $row;
+    }
     $best_ruleset['rules'] = unserialize($best_ruleset['rules']);
   }
 
@@ -216,10 +252,10 @@ function sms_valid_get_ruleset_for_number($number) {
  *   Array of prefix numbers.
  */
 function sms_valid_get_prefixes_for_iso2($iso2) {
-  $result = db_query("SELECT prefix FROM {sms_valid_rules} WHERE iso2 = '%s'",
-                     $prefix);
+  $result = db_query("SELECT prefix FROM {sms_valid_rules} WHERE iso2 = :iso2", array(':iso2' => $iso2));
   $prefixes = array();
-  while ($row = db_fetch_object($result)) {
+//TODO  while ($row = db_fetch_object($result)) {
+  foreach($result as $row) {
     $prefixes[] = $row->prefix;
   }
   return $prefixes;
@@ -238,9 +274,10 @@ function sms_valid_get_prefixes_for_iso2($iso2) {
  *   Boolean. Whether the ruleset is enabled for this direction.
  */
 function sms_valid_ruleset_is_enabled($prefix, $dir = SMS_DIR_OUT) {
-  $result = db_query("SELECT dirs_enabled FROM {sms_valid_rules} WHERE prefix = %d LIMIT 1",
-                     $prefix);
-  $dirs_enabled = db_result($result);
+//  $result = db_query("SELECT dirs_enabled FROM {sms_valid_rules} WHERE prefix = :prefix LIMIT 1", array(':prefix' => $prefix));
+  // This should be very similar to line 195
+  $result = db_query_range("SELECT dirs_enabled FROM {sms_valid_rules} WHERE prefix = :prefix", 0, 1, array(':prefix' => $prefix));
+  $dirs_enabled = $result->fetchField();
 
   // There must be a better way of doing this, but this works ok
   if ($dirs_enabled == SMS_DIR_ALL) {
@@ -249,7 +286,7 @@ function sms_valid_ruleset_is_enabled($prefix, $dir = SMS_DIR_OUT) {
   if ($dirs_enabled == SMS_DIR_OUT && $dir == SMS_DIR_OUT) {
     return TRUE;
   }
-  if ($dirs_enabled == SMS_DIR_IN  && $dir == SMS_DIR_IN) {
+  if ($dirs_enabled == SMS_DIR_IN    && $dir == SMS_DIR_IN) {
     return TRUE;
   }
   return FALSE;
@@ -268,8 +305,12 @@ function sms_valid_ruleset_is_enabled($prefix, $dir = SMS_DIR_OUT) {
  *   Bollean. Result of the DB query.
  */
 function sms_valid_ruleset_set_status($prefix, $dir = SMS_DIR_ALL) {
-  return db_query("UPDATE {sms_valid_rules} SET dirs_enabled = %d WHERE prefix = %d",
-                  $dir, $prefix);
+  // @TODO Please review the conversion of this statement to the D7 database API syntax.
+  /* db_query("UPDATE {sms_valid_rules} SET dirs_enabled = %d WHERE prefix = %d", $dir, $prefix) */
+  return db_update('sms_valid_rules')
+  ->fields(array('dirs_enabled' => $dir))
+  ->condition('prefix', $prefix)
+  ->execute();
 }
 
 
@@ -291,18 +332,36 @@ function sms_valid_save_ruleset($ruleset) {
 
   if (sms_valid_get_ruleset($prefix)) {
     // The ruleset exists so we should update
-    return db_query("UPDATE {sms_valid_rules} SET
-                    name = '%s', rules = '%s', dirs_enabled = %d,
-                    iso2 = '%s' WHERE prefix = %d",
-                    $name, $rules_z, $dirs_enabled, $iso2, $prefix);
+    // @TODO Please review the conversion of this statement to the D7 database API syntax.
+    /* db_query("UPDATE {sms_valid_rules} SET
+     name = '%s', rules = '%s', dirs_enabled = %d,
+     iso2 = '%s' WHERE prefix = %d", $name, $rules_z, $dirs_enabled, $iso2, $prefix) */
+    return db_update('sms_valid_rules')
+    ->fields(array(
+        'name' => $name,
+        'rules' => $rules_z,
+        'dirs_enabled' => $dirs_enabled,
+        'iso2' => $iso2,
+    ))
+    ->condition('prefix', $prefix)
+    ->execute();
   }
   else {
     // The ruleset does not exist so we should create
-    return db_query("INSERT INTO {sms_valid_rules}
-                     (prefix, name, rules, dirs_enabled, iso2)
-                     VALUES
-                     (%d, '%s', '%s', %d, '%s')",
-                     $prefix, $name, $rules_z, $dirs_enabled, $iso2);
+    // @TODO Please review the conversion of this statement to the D7 database API syntax.
+    /* db_query("INSERT INTO {sms_valid_rules}
+     (prefix, name, rules, dirs_enabled, iso2)
+     VALUES
+     (%d, '%s', '%s', %d, '%s')", $prefix, $name, $rules_z, $dirs_enabled, $iso2) */
+    return $id = db_insert('sms_valid_rules')
+    ->fields(array(
+        'prefix' => $prefix,
+        'name' => $name,
+        'rules' => $rules_z,
+        'dirs_enabled' => $dirs_enabled,
+        'iso2' => $iso2,
+      ))
+    ->execute();
   }
 }
 
@@ -317,7 +376,11 @@ function sms_valid_save_ruleset($ruleset) {
  *   Boolean. Result of the DB query.
  */
 function sms_valid_delete_ruleset($prefix) {
-  return db_query("DELETE FROM {sms_valid_rules} WHERE prefix = %d", $prefix);
+  // @TODO Please review the conversion of this statement to the D7 database API syntax.
+  /* db_query("DELETE FROM {sms_valid_rules} WHERE prefix = %d", $prefix) */
+  return db_delete('sms_valid_rules')
+    ->condition('prefix', $prefix)
+    ->execute();
 }
 
 
@@ -350,7 +413,9 @@ function sms_valid_text_to_rules($text) {
   $rules = array();
 
   foreach ($lines as $line) {
-    if (empty($line)) { continue; }
+    if (empty($line)) {
+      continue;
+    }
 
     // Capture any comments and then strip them
     preg_match('/\#(.*)/', $line, $matches);
@@ -364,8 +429,10 @@ function sms_valid_text_to_rules($text) {
     $rule_prefix = trim(preg_replace('/[\D]/', '', $line));
 
     // Add to rules array
-    $rules[$rule_prefix] = array('allow'   => $allow,
-                                 'comment' => $comment);
+    $rules[$rule_prefix] = array(
+      'allow' => $allow,
+      'comment' => $comment,
+    );
   }
   return $rules;
 }
@@ -386,7 +453,7 @@ function sms_valid_rules_to_text($rules) {
   if ($rules && is_array($rules)) {
     foreach ($rules as $rule_prefix => $rule) {
       $allow       = ($rule['allow']) ? '+' : '-';
-      $comment     = ($rule['comment']) ? '    # '. $rule['comment'] : '';
+      $comment     = ($rule['comment']) ? '    # ' . $rule['comment'] : '';
       $lines[]     = $rule_prefix . $allow . $comment;
     }
   }
@@ -405,6 +472,7 @@ function sms_valid_rules_to_text($rules) {
  *   Options array that can be used in a form select element.
  */
 function sms_valid_get_rulesets_for_form($include_null_option = FALSE) {
+  $options = array();
   // We only really need a null option on the send form
   if ($include_null_option) {
     $options[-1] = '(auto select)';
@@ -413,8 +481,8 @@ function sms_valid_get_rulesets_for_form($include_null_option = FALSE) {
   // Other options
   $rulesets = sms_valid_get_all_rulesets();
   foreach ($rulesets as $prefix => $ruleset) {
-    $suffix = (!empty($ruleset['iso2'])) ? ' ('. $ruleset['iso2'] .')' : '';
-    $options[$prefix] = $prefix .' : '. $ruleset['name'] . $suffix;
+    $suffix = (!empty($ruleset['iso2'])) ? ' (' . $ruleset['iso2'] . ')' : '';
+    $options[$prefix] = $prefix . ' : ' . $ruleset['name'] . $suffix;
   }
   return $options;
 }
@@ -431,10 +499,11 @@ function sms_valid_get_rulesets_for_form($include_null_option = FALSE) {
  */
 function sms_valid_is_local_number($number) {
   $prefix = variable_get('sms_valid_local_number_prefix', '0');
-  // A blank prefix string makes this return false 
+  // A blank prefix string makes this return false
   if ($prefix !== '' && preg_match("/^$prefix/", $number)) {
     return TRUE;
-  } else {
+  }
+  else {
     return FALSE;
   }
 }
@@ -458,16 +527,19 @@ function sms_valid_is_local_number($number) {
  *   - log  : Array of log strings. The last record is the most significant.
  */
 function sms_valid_validate(&$number, &$options = array()) {
-  $result = array('pass' => NULL, 'log' => array());
+  $result = array(
+    'pass' => NULL,
+    'log' => array(),
+  );
 
   // Set the default direction if not specified in options
   $dir = (array_key_exists('dir', $options)) ? $options['dir'] : SMS_DIR_OUT;
 
-  $use_global_ruleset   = variable_get('sms_valid_use_global_ruleset',  FALSE);
+  $use_global_ruleset   = variable_get('sms_valid_use_global_ruleset',   FALSE);
   $global_ruleset       = variable_get('sms_valid_global_ruleset',       '64');
-  $local_number_prefix  = variable_get('sms_valid_local_number_prefix',   '0');
+  $local_number_prefix  = variable_get('sms_valid_local_number_prefix',  '0');
   $local_number_ruleset = variable_get('sms_valid_local_number_ruleset', '64');
-  $last_resort_enabled  = variable_get('sms_valid_last_resort_enabled', FALSE);
+  $last_resort_enabled  = variable_get('sms_valid_last_resort_enabled',  FALSE);
   $last_resort_ruleset  = variable_get('sms_valid_last_resort_ruleset',  NULL);
 
   // Check if we should use a specific ruleset prefix
@@ -506,7 +578,7 @@ function sms_valid_validate(&$number, &$options = array()) {
   elseif (sms_valid_is_local_number($number)) {
     $prefix  = $local_number_ruleset;
     $ruleset = sms_valid_get_ruleset($prefix);
-    $result['log'][] = 'Identified local number. Using ruleset prefix '. $prefix;
+    $result['log'][] = 'Identified local number. Using ruleset prefix ' . $prefix;
     // Strip the local prefix from number
     $num     = preg_replace("/^$local_number_prefix/", '', $number);
   }
@@ -515,16 +587,16 @@ function sms_valid_validate(&$number, &$options = array()) {
     $ruleset  = sms_valid_get_ruleset_for_number($number);
 
     if ($ruleset) {
-      $result['log'][] = 'Identified ruleset prefix '. $prefix;
+      $result['log'][] = 'Identified ruleset prefix ' . $prefix;
       $prefix = $ruleset['prefix'];
     }
     else {
       // Could not identify ruleset prefix
-      $result['log'][] = 'Could NOT identify the ruleset prefix for number '. $number;
+      $result['log'][] = 'Could NOT identify the ruleset prefix for number ' . $number;
 
       if ($last_resort_enabled && $last_resort_ruleset) {
         // We have a last resort to use
-        $result['log'][] = 'Using last resort ruleset prefix '. $last_resort_ruleset;
+        $result['log'][] = 'Using last resort ruleset prefix ' . $last_resort_ruleset;
         $prefix  = $last_resort_ruleset;
         $ruleset = sms_valid_get_ruleset($prefix);
       }
@@ -542,8 +614,11 @@ function sms_valid_validate(&$number, &$options = array()) {
   // Get the rules for this ruleset
   $rules = $ruleset['rules'];
 
-  // Sort the rules by prefix (key) in reverse order (largest to smallest)
-  krsort($rules);
+  // Lets make sure we have a rule before trying to sort it
+  if ($rules !== NULL) {
+    // Sort the rules by prefix (key) in reverse order (largest to smallest)
+    krsort($rules);
+  }
 
   // Check whether this ruleset is enabled for the direction of communication
   if (! sms_valid_ruleset_is_enabled($prefix, $dir)) {
@@ -555,7 +630,7 @@ function sms_valid_validate(&$number, &$options = array()) {
   // Test the number against each rule prefix until we get a match
   if (! empty($rules)) {
     foreach ($rules as $rule_prefix => $rule) {
-    $result['log'][] = 'Trying rule with prefix '. $rule_prefix .' on number '. $num;
+      $result['log'][] = 'Trying rule with prefix ' . $rule_prefix . ' on number ' . $num;
       if (preg_match("/^$rule_prefix/", $num)) {
         if ($rule['allow']) {
           // Set the full validated number and return
@@ -577,4 +652,3 @@ function sms_valid_validate(&$number, &$options = array()) {
   $result['log'][] = "Cannot validate this number. Denied by default.";
   return $result;
 }
-
diff --git a/sms.admin.inc b/sms.admin.inc
index 6725e53..74b620f 100644
--- a/sms.admin.inc
+++ b/sms.admin.inc
@@ -1,7 +1,5 @@
 <?php
 
-// $Id$
-
 /**
  * @file
  * The admin sms functions.
@@ -16,12 +14,12 @@ function sms_admin_default_form($form, &$form_state) {
   foreach ($gateways as $identifier => $gateway) {
     $active = ($identifier == variable_get('sms_default_gateway', 'log'));
     $options[$identifier] = '';
-    $form[$gateway['name']]['id'] = array('#value' => $identifier);
+    $form[$gateway['name']]['id'] = array('#markup' => $identifier);
     if ( isset($gateway['configure form'] ) and ( function_exists($gateway['configure form'] ))) {
-      $form[$gateway['name']]['configure'] = array('#value' => l(t('configure'), 'admin/smsframework/gateways/' . $identifier));
+      $form[$gateway['name']]['configure'] = array('#markup' => l(t('configure'), 'admin/smsframework/gateways/' . $identifier));
     }
     else {
-      $form[$gateway['name']]['configure'] = array('#value' => t('No configuration options'));
+      $form[$gateway['name']]['configure'] = array('#markup' => t('No configuration options'));
     }
   }
 
@@ -65,11 +63,10 @@ function theme_sms_admin_default_form($variables) {
   $rows = array();
   foreach ($form as $name => $element) {
     if (is_array($element) and isset($element['id']) and is_array($element['id'])) {
-        # dsm($element['configure']);
       $rows[] = array(
-        drupal_render($form['default'][$element['id']['#value']]),
+        drupal_render($form['default'][$element['id']['#markup']]),
         check_plain($name),
-        $element['configure']['#value'], // XXX D7 porting issue: why doesn't this work?  drupal_render($element['configure']),
+        $element['configure']['#markup'],
       );
       unset($form[$name]);
     }
@@ -182,7 +179,7 @@ function sms_carriers_admin_form($form, &$form_state) {
 
     $form['domain'][$css_safe_id] = array(
       '#type' => 'markup',
-      '#value' => $id,
+      '#markup' => check_plain($id),
     );
 
     $actions[] = l(t('Edit'), "admin/smsframework/carriers/{$id}");
@@ -196,7 +193,7 @@ function sms_carriers_admin_form($form, &$form_state) {
 
     $form['actions'][$css_safe_id] = array(
       '#type' => 'markup',
-      '#value' => implode(' | ', $actions),
+      '#markup' => check_plain(implode(' | ', $actions)),
     );
   }
 
@@ -365,4 +362,3 @@ function theme_sms_carriers_admin_form($variables) {
 
   return $output;
 }
-
diff --git a/sms.install b/sms.install
index d3cf951..8612f10 100644
--- a/sms.install
+++ b/sms.install
@@ -1,10 +1,8 @@
 <?php
 
-// $Id$
-
 /**
  * @file
- * Install, update and uninstall functions for the smsframework module.
+ * Install, update, and uninstall functions for the smsframework module.
  */
 
 /**
@@ -54,11 +52,9 @@ function sms_update_1() {
  * Implements hook_uninstall().
  */
 function sms_uninstall() {
-
   $variables = array();
   # XXX D7 porting issue: need to variable_del sms* variables on sms_uninstall()
   foreach ($variables as $variable) {
     variable_del($variable);
   }
-
 }
diff --git a/sms.module b/sms.module
index 25032d3..491a3d9 100644
--- a/sms.module
+++ b/sms.module
@@ -1,7 +1,5 @@
 <?php
 
-// $Id$
-
 /**
  * @file
  * The core of the SMS Framework. Provides gateway managment and API for
@@ -9,7 +7,7 @@
  */
 
 // Direction codes
-define('SMS_DIR_NONE', 0);
+define('SMS_DIR_NONE',  0);
 define('SMS_DIR_OUT',   1);
 define('SMS_DIR_IN',    2);
 define('SMS_DIR_ALL',   4);
@@ -18,7 +16,7 @@ define('SMS_DIR_ALL',   4);
 // 0=Unknown, 2xx=Positive, 3xx=Positive/Neutral (context-dependent), 4xx=Negative
 define('SMS_MSG_STATUS_UNKNOWN',      0);
 define('SMS_MSG_STATUS_OK',         200);
-define('SMS_MSG_STATUS_DELIVERED', 202);
+define('SMS_MSG_STATUS_DELIVERED',  202);
 define('SMS_MSG_STATUS_QUEUED',     302);
 define('SMS_MSG_STATUS_ERROR',      400);
 define('SMS_MSG_STATUS_NOCREDIT',   402);
@@ -29,7 +27,7 @@ define('SMS_MSG_STATUS_EXPIRED',    408);
 define('SMS_GW_UNKNOWN_STATUS',      0);
 define('SMS_GW_OK',                200);
 define('SMS_GW_ERR_AUTH',          401);
-define('SMS_GW_ERR_INVALID_CALL', 400);
+define('SMS_GW_ERR_INVALID_CALL',  400);
 define('SMS_GW_ERR_NOT_FOUND',     404);
 define('SMS_GW_ERR_MSG_LIMITS',    413);
 define('SMS_GW_ERR_MSG_ROUTING',   502);
@@ -468,8 +466,17 @@ function sms_validate_number(&$number, $options = array()) {
 
   // Allow the active gateway to provide number validation
   $gateway = sms_default_gateway();
-  if (function_exists($gateway['validate number']) && $error = $gateway['validate number']($number, $options)) {
-    return $error;
+  // TODO we neeed some sort of validation this isn't working properly.
+  if (isset($gateway['validate_number'])) {
+    if (function_exists($gateway['validate number']) && $error = $gateway['validate number']($number, $options)) {
+      return $error;
+    }
+    elseif ($error = sms_valid_validate($number, $options)) {
+      return $error;
+    }
+  }
+  else {
+    return NULL;
   }
 }
 
@@ -712,6 +719,4 @@ function sms_country_codes() {
     255 => "Zanzibar",
     263 => "Zimbabwe",
   );
-
 }
-
diff --git a/sms.tokens.inc b/sms.tokens.inc
index 97ed9d7..8147d29 100644
--- a/sms.tokens.inc
+++ b/sms.tokens.inc
@@ -14,6 +14,7 @@ function sms_token_info() {
   $info['types']['sms'] = array(
     'name' => t('SMS'),
     'needs-data' => 'sms',
+    'description' => t("SMS"),
   );
   $info['tokens']['sms']['number'] = array(
     'name' => t('Number'),
-- 
1.7.5.4

