diff -urp ad/ad.info ad_d6/ad.info
--- ad/ad.info	2008-06-13 01:00:19.000000000 +0100
+++ ad_d6/ad.info	2008-06-17 21:15:12.043918800 +0100
@@ -1,8 +1,9 @@
 ; $Id: ad.info,v 1.1.2.2 2007/07/16 19:25:05 jeremy Exp $
 name = Ad
 package = Ad
-dependencies = taxonomy
+dependencies[] = taxonomy
 description = An advertising system for Drupal powered websites.
+core=6.x
 
 ; Information added by drupal.org packaging script on 2008-06-13
 version = "5.x-1.x-dev"
diff -urp ad/ad.install ad_d6/ad.install
--- ad/ad.install	2008-04-19 19:44:59.000000000 +0100
+++ ad_d6/ad.install	2008-06-17 23:40:38.491918800 +0100
@@ -7,51 +7,45 @@
  * All rights reserved.
  */
 
-function ad_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'pgsql':
-     /* The ad table stores administrative information about each ad.  The
-      * actual ad itself can be found in the appropriate ad type table.
-      */
-     db_query("CREATE TABLE {ads} (
-       aid INT NOT NULL UNIQUE DEFAULT '0' PRIMARY KEY,
-       uid INT NOT NULL DEFAULT '0',
- 
-       adstatus VARCHAR(255) NOT NULL DEFAULT '',
-       adtype VARCHAR(255) NOT NULL DEFAULT '',
-
-       redirect VARCHAR(255) NOT NULL DEFAULT '',
-
-       autoactivate INT NOT NULL DEFAULT '0',
-       autoactivated INT NOT NULL DEFAULT '0',
-       autoexpire INT NOT NULL DEFAULT '0',
-       autoexpired INT NOT NULL DEFAULT '0',
-
-       activated INT NOT NULL DEFAULT '0',
-       maxviews INT NOT NULL DEFAULT '0',
-       maxclicks INT NOT NULL DEFAULT '0',
-       expired INT NOT NULL DEFAULT '0'
-     );");
+function ad_schema() {
+
+ /* The ad table stores administrative information about each ad.  The
+     * actual ad itself can be found in the appropriate ad type table.
+     */
+  $schema['ads'] = array(
+      'fields' => array(
+           ),
+      'primary key' => array('aid'),
+      'indexes' => array(
+           'autoactivate' => array('autoactivate'),
+           'autoexpire' => array('autoexpire'),
+           'uid' => array('uid')),
+  );
 
-     /**
+ /**
       * Every ad can have one or more owners.
       */
-     db_query("CREATE TABLE {ad_owners} (
-       oid SERIAL NOT NULL PRIMARY KEY,
-       aid INT NOT NULL DEFAULT '0',
-       uid INT NOT NULL DEFAULT '0'
-     );");
+  $schema['ad_owners'] = array(
+      'fields' => array(
+           ),
+      'primary key' => array('oid'),
+      'indexes' => array(
+           'aid' => array('aid'),
+           'uid' => array('uid')),
+  );
+
 
-     /**
+ /**
       * Permissions can be granted to each owner of each ad.  The same owner
       * can own multiple ads, and can have different permissions for each ad.
       */
-     db_query("CREATE TABLE {ad_permissions} (
-       oid INT NOT NULL DEFAULT '0' PRIMARY KEY,
-       permissions TEXT NULL DEFAULT ''
-     );");
+  $schema['ad_permissions'] = array(
+      'fields' => array(
+           ),
+      'primary key' => array('oid'),
+  );
 
-     /**
+ /**
       * This table counts each time a given action occurs on an ad.  Actions
       * include when the ad is viewed, clicked, enabled and disabled.
       * Statistics are collected at an hourly granularity.
@@ -62,182 +56,56 @@ function ad_install() {
       * Actions:
       *  'view', 'click', 'enable', 'disable'
       */
-     db_query("CREATE TABLE {ad_statistics} (
-       sid SERIAL NOT NULL PRIMARY KEY,
-       aid INT NOT NULL DEFAULT '0',
-
-       date INT NOT NULL DEFAULT '0',
-       action VARCHAR(255) NOT NULL DEFAULT '',
-       adgroup VARCHAR(255) NULL DEFAULT '',
-       hostid VARCHAR(32) NULL DEFAULT '',
-       count INT NOT NULL DEFAULT '0'
-     );");
+  $schema['ad_statistics'] = array(
+      'fields' => array(
+           ),
+      'primary key' => array('sid'),
+      'indexes' => array(
+           'action' => array('action'),
+           'adgroup' => array('adgroup'),
+           'aid' => array('aid'),
+           'date' => array('date'),
+           'hostid' => array('hostid')),
+  );
 
-     /**
+ /**
       * The ad_clicks table tracks when a given advertisement was clicked, 
       * who clicked it (uid if any and IP address), and what page they were
       * on when they clicked it.
       */
-      db_query("CREATE TABLE {ad_clicks} (
-        cid SERIAL NOT NULL PRIMARY KEY,
-        aid INT NOT NULL DEFAULT '0',
-        uid INT NOT NULL DEFAULT '0',
-
-        status INT NOT NULL DEFAULT '0',
- 
-        hostname varchar(128) NOT NULL DEFAULT '',
-        user_agent varchar(255) NOT NULL DEFAULT '',
-        adgroup varchar(255) NOT NULL DEFAULT '',
-        hostid varchar(32) NOT NULL DEFAULT '',
-        url varchar(255) DEFAULT '',
-        timestamp INT NOT NULL DEFAULT '0',
-      );");
-
-      /**
-       * The ad_hosts table is used to configure users that can display ads
-       * remotely. 
-       */
-      db_query("CREATE TABLE {ad_hosts} (
-        uid INT NOT NULL DEFAULT '0' PRIMARY KEY,
- 
-        hostid varchar(32) DEFAULT '',
-        status INT NOT NULL DEFAULT '0',
-        description TEXT NOT NULL DEFAULT ''
-      );");
-      break;
-
-    case 'mysql':
-    case 'mysqli':
-    default:
-
-      /* The ad table stores administrative information about each ad.  The
-       * actual ad itself can be found in the appropriate ad type table.
-       */
-      db_query("CREATE TABLE {ads} (
-        aid INT(10) UNSIGNED NOT NULL DEFAULT '0',
-        uid INT(10) UNSIGNED NOT NULL DEFAULT '0',
-  
-        adstatus VARCHAR(255) NOT NULL DEFAULT '',
-        adtype VARCHAR(255) NOT NULL DEFAULT '',
-
-        redirect VARCHAR(255) NOT NULL DEFAULT '',
-
-        autoactivate INT UNSIGNED NOT NULL DEFAULT '0',
-        autoactivated INT UNSIGNED NOT NULL DEFAULT '0',
-        autoexpire INT UNSIGNED NOT NULL DEFAULT '0',
-        autoexpired INT UNSIGNED NOT NULL DEFAULT '0',
-
-        activated INT UNSIGNED NOT NULL DEFAULT '0',
-        maxviews INT UNSIGNED NOT NULL DEFAULT '0',
-        maxclicks INT UNSIGNED NOT NULL DEFAULT '0',
-        expired INT UNSIGNED NOT NULL DEFAULT '0',
-
-        PRIMARY KEY  (aid),
-        KEY (uid),
-        KEY (autoactivate),
-        KEY (autoexpire)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
+  $schema['ad_clicks'] = array(
+      'fields' => array(
+           ),
+      'primary key' => array('cid'),
+      'indexes' => array(
+           'adgroup' => array('adgroup'),
+           'aid' => array('aid'),
+           'hostid' => array('hostid'),
+           'hostname' => array('hostname'),
+           'status' => array('status'),
+           'url' => array('url'),
+           'user_agent' => array('user_agent')),
+  );
 
-      /**
-       * Every ad can have one or more owners.
-       */
-      db_query("CREATE TABLE {ad_owners} (
-        oid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
-        aid INT(10) UNSIGNED NOT NULL DEFAULT '0',
-        uid INT(10) UNSIGNED NOT NULL DEFAULT '0',
-
-        PRIMARY KEY  (oid),
-        KEY  (aid),
-        KEY  (uid)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-
-      /**
-       * Permissions can be granted to each owner of each ad.  The same owner
-       * can own multiple ads, and can have different permissions for each ad.
-       */
-      db_query("CREATE TABLE {ad_permissions} (
-        oid INT(11) UNSIGNED NOT NULL DEFAULT '0',
-        permissions LONGTEXT NULL,
-        PRIMARY KEY  (oid)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-
-      /**
-       * This table counts each time a given action occurs on an ad.  Actions
-       * include when the ad is viewed, clicked, enabled and disabled.
-       * Statistics are collected at an hourly granularity.
-       *
-       * The source column is used for tracking statistics for externally 
-       * hosted ads.
-       *
-       * Actions:
-       *  'view', 'click', 'enable', 'disable'
-       */
-      db_query("CREATE TABLE {ad_statistics} (
-        sid INT UNSIGNED NOT NULL AUTO_INCREMENT,
-        aid INT UNSIGNED NOT NULL DEFAULT '0',
-
-        date INT(10) UNSIGNED NOT NULL DEFAULT '0',
-        action VARCHAR(255) NOT NULL DEFAULT '',
-        adgroup VARCHAR(255) NULL DEFAULT '',
-        hostid VARCHAR(32) NULL DEFAULT '',
-        count INT(11) UNSIGNED NOT NULL DEFAULT '0',
-
-        PRIMARY KEY  (sid),
-        KEY (aid),
-        KEY (date),
-        KEY (action),
-        KEY (adgroup),
-        KEY (hostid)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-
-      /**
-       * The ad_clicks table tracks when a given advertisement was clicked, 
-       * who clicked it (uid if any and IP address), and what page they were
-       * on when they clicked it.
-       */
-      db_query("CREATE TABLE {ad_clicks} (
-        cid INT UNSIGNED NOT NULL AUTO_INCREMENT,
-        aid INT UNSIGNED NOT NULL DEFAULT '0',
-        uid int(10) UNSIGNED NOT NULL DEFAULT '0',
-
-        status INT(2) NOT NULL DEFAULT '0',
-
-        hostname varchar(128) NOT NULL DEFAULT '',
-        user_agent varchar(255) NOT NULL DEFAULT '',
-        adgroup varchar(255) NOT NULL DEFAULT '',
-        hostid varchar(32) NOT NULL DEFAULT '',
-        url varchar(255) DEFAULT '',
-        timestamp INT(11) UNSIGNED NOT NULL DEFAULT '0',
-
-        PRIMARY KEY  (cid),
-        KEY  (aid),
-        KEY  (status),
-        KEY  (hostname),
-        KEY  (user_agent),
-        KEY  (adgroup),
-        KEY  (hostid),
-        KEY  (url)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
 
-      /**
+ /**
        * The ad_hosts table is used to configure users that can display ads
        * remotely. 
        */
-      db_query("CREATE TABLE {ad_hosts} (
-        uid INT UNSIGNED NOT NULL DEFAULT '0',
-
-        hostid varchar(32) DEFAULT '',
-
-        status INT(2) UNSIGNED NOT NULL DEFAULT '0',
-        description TEXT NULL,
-
-        PRIMARY KEY  (uid),
-        KEY  (status),
-        KEY  (hostid)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-  }
+  $schema['ad_hosts'] = array(
+      'fields' => array(
+           ),
+      'primary key' => array('uid'),
+      'indexes' => array(
+           'hostid' => array('hostid'),
+           'status' => array('status')),
+  );
+}
 
-  drupal_set_message(t('The necessary ad module tables have been created.'));
+function mymodule_install() {
+  // Create my tables.
+  drupal_install_schema('ad');
+  drupal_set_message('The necessary ad module tables have been created.');
 }
 
 /**
@@ -245,19 +113,7 @@ function ad_install() {
  */
 function ad_uninstall() {
   // Delete all ad content.
-  $result = db_query("SELECT nid FROM {node} WHERE type = 'ad'");
-  while ($nid = db_result($result)) {
-    node_delete($nid);
-    variable_del("ad_autoactivate_warning_$nid");
-  }
-
-  // Drop all ad module tables.
-  db_query('DROP TABLE {ad_clicks}');
-  db_query('DROP TABLE {ad_hosts}');
-  db_query('DROP TABLE {ad_owners}');
-  db_query('DROP TABLE {ad_permissions}');
-  db_query('DROP TABLE {ad_statistics}');
-  db_query('DROP TABLE {ads}');
+  drupal_uninstall_schema('ad');
 
   // Delete all remaining ad module variables.
   $variables = array('ad_cron_timestamp', 'ad_link_target', 'ad_cache', 'ad_cache_file', 'adserve', 'ad_group_vid', 'ad_groups', 'ad_validate_url', 'ad_display');
@@ -265,215 +121,4 @@ function ad_uninstall() {
     variable_del($variable);
   }
   // TODO: "ad_block_quantity_$delta"
-}
-
-/**
- * Introduce fields for specifying an optional maximum number of view or clicks
- * for each ad.  Also introduce fields for tracking the last time the ad was
- * activated or expired.
- */
-function ad_update_1() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    case 'pgsql':
-      db_add_column($ret, 'ads', 'activated', 'int', array('not null' => TRUE, 'default' => "'0'"));
-      db_add_column($ret, 'ads', 'maxviews', 'int', array('not null' => TRUE, 'default' => "'0'"));
-      db_add_column($ret, 'ads', 'maxclicks', 'int', array('not null' => TRUE, 'default' => "'0'"));
-      db_add_column($ret, 'ads', 'expired', 'int', array('not null' => TRUE, 'default' => "'0'"));
-      break;
-    case 'mysql':
-    case 'mysqli':
-    default:
-      $ret[] = update_sql("ALTER TABLE {ads} ADD activated INT UNSIGNED NOT NULL DEFAULT '0' AFTER autoexpired");
-      $ret[] = update_sql("ALTER TABLE {ads} ADD maxviews INT UNSIGNED NOT NULL DEFAULT '0' AFTER activated");
-      $ret[] = update_sql("ALTER TABLE {ads} ADD maxclicks INT UNSIGNED NOT NULL DEFAULT '0' AFTER maxviews");
-      $ret[] = update_sql("ALTER TABLE {ads} ADD expired INT UNSIGNED NOT NULL DEFAULT '0' AFTER maxclicks");
-  }
-  return $ret;
-}
-
-/**
- * Ad notifications are being moved to an external module, so remove all
- * references from the core module.
- */
-function ad_update_2() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    default:
-      $ret[] = update_sql("DROP TABLE {ad_notifications}");
-      $ret[] = update_sql("ALTER TABLE {ads} DROP expire_notified");
-      $ret[] = update_sql("ALTER TABLE {ads} DROP renew_notified");
-  }
-  return $ret;
-}
-
-/**
- * Redirect urls need to be absolute to work when hosting ads remotely.
- */
-function ad_update_3() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    default:
-      $result = db_query('SELECT aid FROM {ads}');
-      while ($ad = db_fetch_object($result)) {
-        $ret[] = update_sql("UPDATE {ads} SET redirect = '". url("ad/redirect/$ad->aid", NULL, NULL, TRUE) ."' WHERE aid = $ad->aid");
-      }
-  }
-  return $ret;
-}
-
-/**
- * Move ad groups from custom tables into taxonomy tables.
- */
-function ad_update_4() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    default:
-      // Move groups from ad_groups into taxonomy.
-      $result = db_query('SELECT * FROM {ad_groups}');
-      while ($group = db_fetch_object($result)) {
-        // The default group is now no group at all.
-        if ($group->gid == 1)
-          continue;
-        $edit = array('vid' => _ad_get_vid(), 'name' => $group->name, 'description' => $group->description);
-        taxonomy_save_term($edit);
-        $tid[$group->gid] = $edit['tid'];
-      }
-      // Assign ads to new ad groups.
-      $result = db_query('SELECT aid,gid FROM {ads}');
-      while ($ad = db_fetch_object($result)) {
-        if ($tid[$ad->gid]) {
-          $ret[] = update_sql("INSERT INTO {term_node} (nid, tid) VALUES ($ad->aid, ". $tid[$ad->gid] .')');
-        }
-      }
-      // Fix the deltas of any group blocks.
-      $result = db_query("SELECT * from {blocks} WHERE module = 'ad'");
-      $ret[] = update_sql("DELETE FROM {blocks} WHERE module = 'ad'");
-      while ($block = db_fetch_object($result)) {
-        if ($block->delta == 1) {
-          // The old "default" group block now as a delta of 0.
-          $ret[] = update_sql("INSERT INTO {blocks} VALUES('ad', 0, '$block->theme', $block->status, $block->weight, '$block->region', $block->custom, $block->throttle, $block->visibility, '$block->pages', '$block->title')");
-        }
-        else {
-          // Switch from a delta of "gid" to a delta of "tid".
-          $ret[] = update_sql("INSERT INTO {blocks} VALUES('ad', ". $tid[$block->delta] .", '$block->theme', $block->status, $block->weight, '$block->region', $block->custom, $block->throttle, $block->visibility, '$block->pages', '$block->title')");
-        }
-      }
-      // Remove old groups table and column.
-      $ret[] = update_sql("DROP TABLE {ad_groups}");
-      $ret[] = update_sql("ALTER TABLE {ads} DROP gid");
-  }
-  return $ret;
-}
-
-/**
- * Convert to utf8 character set for all tables to allow for proper 
- * internationalization.
- */
-function ad_update_5() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    case 'pgsql':
-      // Not sure if anything needs to be done for utf8 support in PostgreSQL.
-      break;
-    default:
-      $tables = array('ads', 'ad_clicks', 'ad_hosts', 'ad_owners', 'ad_permissions', 'ad_statistics');
-      foreach ($tables as $table) {
-        $ret[] = update_sql('ALTER TABLE {'. $table. '} CONVERT TO CHARACTER SET utf8');
-      }
-  }
-  return $ret;
-}
-
-/**
- * Update embed-ad-#-count variable to use "-1" instead of "9999" to auto embed 
- * ads after the last paragraph.
- */
-function ad_update_6() {
-  $types = node_get_types();
-  foreach ($types as $key => $type) {
-    $count = variable_get("embed-ad-$key-count", 0);
-    if ($count == 9999) {
-      variable_set("embed-ad-$key-count", -1);
-    }
-  }
-  return array();
-}
-
-/**
- * Add a user_agent column to the ad_clicks table so it's possible to filter
- * out clicks based on the agent (ie, if it reports itself as a bot).
- */
-function ad_update_7() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      $ret[] = update_sql("ALTER TABLE {ad_clicks} ADD user_agent varchar(255) NOT NULL DEFAULT '' AFTER hostname");
-      $ret[] = update_sql("ALTER TABLE {ad_clicks} ADD KEY (user_agent)");
-      break;
-    case 'pgsql':
-      db_add_column($ret, 'ad_clicks', 'user_agent', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
-      break;
-  }
-  return $ret;
-}
-
-/**
- * Introduce "status" for remote hosts, allowing them to be disabled.
- */
-function ad_update_8() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      $ret[] = update_sql("ALTER TABLE {ad_hosts} ADD status INT NOT NULL DEFAULT '0' AFTER hostid");
-      break;
-
-    case 'pgsql':
-      db_add_column($ret, 'ad_hosts', 'status', 'int', array('not null' => TRUE, 'default' => "'0'"));
-      break;
-  }
-  return $ret;
-}
-
-/**
- * Introduce "status" for ad clicks, allowing duplicates to be filtered out.
- */
-function ad_update_9() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      $ret[] = update_sql("ALTER TABLE {ad_clicks} ADD status INT(2) NOT NULL DEFAULT '0' AFTER uid");
-      break;
-
-    case 'pgsql':
-      db_add_column($ret, 'ad_clicks', 'status', 'int', array('not null' => TRUE, 'default' => "'0'"));
-      break;
-  }
-  return $ret;
-}
-
-/**
- * Introduce "adgroup" for ad statistics and clicks, providing per-group 
- * granularity.
- */
-function ad_update_10() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      $ret[] = update_sql("ALTER TABLE {ad_statistics} ADD adgroup VARCHAR(255) NULL DEFAULT '' AFTER action");
-      $ret[] = update_sql("ALTER TABLE {ad_statistics} ADD KEY (adgroup)");
-      $ret[] = update_sql("ALTER TABLE {ad_clicks} ADD adgroup VARCHAR(255) NULL DEFAULT '' AFTER user_agent");
-      $ret[] = update_sql("ALTER TABLE {ad_clicks} ADD KEY (adgroup)");
-      break;
-
-    case 'pgsql':
-      db_add_column($ret, 'ad_statistics', 'adgroup', 'VARCHAR(255)', array('default' => "''"));
-      db_add_column($ret, 'ad_clicks', 'adgroup', 'VARCHAR(255)', array('default' => "''"));
-      break;
-  }
-  return $ret;
-}
+}
\ No newline at end of file
diff -urp ad/ad.module ad_d6/ad.module
--- ad/ad.module	2008-06-12 16:26:17.000000000 +0100
+++ ad_d6/ad.module	2008-06-17 23:50:01.569918800 +0100
@@ -119,6 +119,15 @@ function ad($group = FALSE, $quantity = 
  * Function to display the actual advertisement to the screen.  Wrap it in a 
  * theme function to make it possible to customize in your own theme.
  */
+ 
+function ad_theme() {
+  return array(
+    'ad_display' => array(
+      'arguments' => array('content'),
+    ),
+  );
+}
+
 function theme_ad_display($group, $display) {
   // The naming convention for the id attribute doesn't allow commas.
   $group = preg_replace('/[,]/', '', $group);
@@ -145,7 +154,7 @@ function ad_redirect($aid, $group = NULL
   if (!($url = $_GET['u']) || !valid_url($url)) {
     $url = referer_uri();
   }
-  db_query("INSERT INTO {ad_clicks} (aid, uid, status, hostname, user_agent, adgroup, hostid, url, timestamp) VALUES (%d, %d, %d, '%s', '%s', '%s', '%s', '%s', %d)", $aid, $user->uid, $status, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'], $group, $hostid, $url, time());
+  db_query("INSERT INTO {ad_clicks} (aid, uid, status, hostname, user_agent, adgroup, hostid, url, timestamp) VALUES (%d, %d, %d, '%s', '%s', '%s', '%s', '%s', %d)", $aid, $user->uid, $status, ip_address(), $_SERVER['HTTP_USER_AGENT'], $group, $hostid, $url, time());
 
   // Determine where we're supposed to redirect the user.
   $adtype = db_result(db_query('SELECT adtype FROM {ads} WHERE aid = %d', $aid));
@@ -154,11 +163,11 @@ function ad_redirect($aid, $group = NULL
   $node->hostid = $hostid;
   $url = module_invoke('ad_'. $adtype, 'adapi', 'redirect', $node);
   if (isset($url)) {
-    watchdog('ad', t('Clicked %type ad aid %aid hostid %hostid.', array('%type' => $adtype, '%aid' => $aid, '%hostid' => $hostid)));
+    watchdog('ad', 'Clicked %type ad aid %aid hostid %hostid.', array('%type' => $adtype, '%aid' => $aid, '%hostid' => $hostid));
     header('Location: '. $url);
   }
   else {
-    watchdog('ad', t('Ad redirection failed for aid %aid hostid %hostid, failed to load destination URL. ', array('%aid' => $aid, '%hostid' => $hostid)));
+    watchdog('ad', 'Ad redirection failed for aid %aid hostid %hostid, failed to load destination URL. ', array('%aid' => $aid, '%hostid' => $hostid));
     drupal_goto('');
   }
 }
@@ -380,7 +389,7 @@ function theme_ad_statistics_display($st
 /**
  * Implementation of hook_help().
  */
-function ad_help($path) {
+function ad_help($path, $arg) {
   switch ($path) {
     case 'admin/help#ad':
       $output = '<p>'. t('The ad module provides a complete advertising system for Drupal powered websites.  It does this through an API that allow other modules to handle various types of advertising content.  For example, if enabled together with the ad_image module you will be able to display image based advertisements such as banner ads.') .'</p>';
@@ -395,7 +404,7 @@ function ad_help($path) {
 function ad_cron() {
   if (time() - variable_get('ad_cron_timestamp', 0) >= 60) {
     // Locate ads that need to be activated or expired.
-    $result = db_query('SELECT aid, adstatus, adtype, autoactivate, autoactivated, autoexpire, autoexpired FROM {ads} WHERE autoactivate != 0 OR autoexpire != 0');
+    $result = db_query('SELECT aid, adstatus, adtype, autoactivate, autoactivated, autoexpire, autoexpired FROM {ads} WHERE autoactivate <> 0 OR autoexpire <> 0');
     while ($ad = db_fetch_object($result)) {
       switch ($ad->adstatus) {
         case 'approved': {
@@ -408,7 +417,7 @@ function ad_cron() {
             ad_statistics_increment($ad->aid, 'autoactivated');
             ad_statistics_increment($ad->aid, 'active');
 
-            watchdog('ad', t('Automatically activated ad %title with nid %nid.', array('%title' => $node->title, '%nid' => $node->nid)));
+            watchdog('ad', 'Automatically activated ad %title with nid %nid.', array('%title' => $node->title, '%nid' => $node->nid));
 
             // Allow modules to do special processing to automatically
             // activated advertisements.
@@ -417,7 +426,7 @@ function ad_cron() {
           else if (!$ad->autoactivate) {
             // Once daily warn that there's an ad stuck in approved state.
             if (time() - variable_get("ad_autoactivate_warning_$ad->aid", 0) >= 8600) {
-              watchdog('ad', t('Warning: ad %title with nid %nid in approved state has no autoactivate date set.', array('%title' => $node->title, '%nid' => $node->nid)));
+              watchdog('ad', 'Warning: ad %title with nid %nid in approved state has no autoactivate date set.', array('%title' => $node->title, '%nid' => $node->nid));
               variable_set("ad_autoactivate_warning_$ad->aid", time());
             }
           }
@@ -433,7 +442,7 @@ function ad_cron() {
             ad_statistics_increment($ad->aid, 'autoexpired');
             ad_statistics_increment($ad->aid, 'expired');
 
-            watchdog('ad', t('Automatically expired ad %title with nid %nid.', array('%title' => $node->title, '%nid' => $node->nid)));
+            watchdog('ad', 'Automatically expired ad %title with nid %nid.', array('%title' => $node->title, '%nid' => $node->nid));
 
             // Allow modules to do special processing to automatically
             // activated advertisements.
@@ -448,7 +457,7 @@ function ad_cron() {
         default:
           $node = node_load($ad->aid);
           db_query('UPDATE {ads} SET autoactivate = 0, autoexpire = 0 WHERE aid = %d', $ad->aid);
-          watchdog('ad', t('Warning: reset %type timestamp on advertisement %title with nid %nid because it is in %state state.', array('%title' => $node->title, '%nid' => $node->nid, '%type' => $ad->autoactivate ? 'autoactivate' : 'autoexpire', '%state' => $ad->adstatus)));
+          watchdog('ad', 'Warning: reset %type timestamp on advertisement %title with nid %nid because it is in %state state.', array('%title' => $node->title, '%nid' => $node->nid, '%type' => $ad->autoactivate ? 'autoactivate' : 'autoexpire', '%state' => $ad->adstatus));
       }
     }
     variable_set('ad_cron_timestamp', time());
@@ -482,7 +491,7 @@ function ad_node_info() {
 
 /**
  */
-function ad_access($op, $node) {
+function ad_access($op, $node, $account) {
   global $user;
 
   if ($op == 'create') {
@@ -736,7 +745,7 @@ function ad_form(&$node) {
 /**
  * Drupal _form_alter() hook.
  */
-function ad_form_alter($form_id, &$form) {
+function ad_form_alter(&$form, &$form_state, $form_id) {
   if ($form_id == 'taxonomy_form_vocabulary') {
     // Remove taxonomy form options not applicable for ad groups.
     if ($form['vid']['#value'] == _ad_get_vid()) {
@@ -798,7 +807,7 @@ function ad_nodeapi(&$node, $op, $teaser
           $node->adstatus = t('unpublished');
         }
         $activated = $node->adstatus == 'active' ? time() : 0;
-        db_query("INSERT INTO {ads} (aid, uid, adstatus, adtype, redirect, autoactivate, autoexpire, activated, maxviews, maxclicks) VALUES(%d, %d, '%s', '%s', '%s', %d, %d, %d, %d, %d)", $node->nid, $node->uid, $node->adstatus, $node->adtype, url("ad/redirect/$node->nid", NULL, NULL, TRUE), $node->autoactivate ? strtotime($node->autoactivate) : '', $node->autoexpire ? strtotime($node->autoexpire) : '', $activated, (int)$node->maxviews, (int)$node->maxclicks);
+        db_query("INSERT INTO {ads} (aid, uid, adstatus, adtype, redirect, autoactivate, autoexpire, activated, maxviews, maxclicks) VALUES(%d, %d, '%s', '%s', '%s', %d, %d, %d, %d, %d)", $node->nid, $node->uid, $node->adstatus, $node->adtype, url("ad/redirect/$node->nid", array('absolute' => TRUE)), $node->autoactivate ? strtotime($node->autoactivate) : '', $node->autoexpire ? strtotime($node->autoexpire) : '', $activated, (int)$node->maxviews, (int)$node->maxclicks);
         ad_owners_add($node->nid, $node->uid);
         ad_host_id_create($node->uid);
         ad_statistics_increment($node->nid, 'create');
@@ -1027,160 +1036,179 @@ function ad_adapi($op, $node = NULL) {
 /**
  * Implementation of hook_menu().
  */
-function ad_menu($may_cache) {
+function ad_menu() {
   global $user;
   $items = array();
 
-  if ($may_cache) {
-    // menu items
-    $items[] = array('path' => 'admin/content/ad',
-                     'title' => t('Ads'),
-                     'callback' => 'ad_admin_list',
-                     'access' => user_access('administer advertisements'),
-                     'description' => t('Configure and manage your advertising system.'));
-
-    // tabs
-    $items[] = array('path' => 'admin/content/ad/list',
-                     'title' => t('List'),
-                     'callback' => 'ad_admin_list',
-                     'type' => MENU_DEFAULT_LOCAL_TASK);
-    $items[] = array('path' => 'admin/content/ad/statistics',
-                     'title' => t('Statistics'),
-                     'callback' => 'drupal_get_form',
-                     'callback arguments' => array('ad_admin_statistics'),
-                     'type' => MENU_LOCAL_TASK,
-                     'weight' => 1);
-    $items[] = array('path' => 'admin/content/ad/configure',
-                     'title' => t('Settings'),
-                     'callback' => 'drupal_get_form',
-                     'callback arguments' => array('ad_admin_configure_settings'),
-                     'type' => MENU_LOCAL_TASK,
-                     'weight' => 3);
-    $items[] = array('path' => 'admin/content/ad/groups',
-                     'title' => t('Groups'),
-                     'callback' => 'ad_admin_groups_list',
-                     'type' => MENU_LOCAL_TASK,
-                     'weight' => 5);
-
-    // groups sub tabs
-    $items[] = array('path' => 'admin/content/ad/groups/list',
-                     'title' => t('List'),
-                     'callback' => 'ad_admin_groups_list',
-                     'type' => MENU_DEFAULT_LOCAL_TASK, 
-                     'weight' => 0);
-    $items[] = array('path' => 'admin/content/ad/groups/add', 
-                     'title' => t('Create group'),
-                     'callback' => 'drupal_get_form',
-                     'callback arguments' => array('ad_admin_group_form'),
-                     'type' => MENU_LOCAL_TASK,
-                     'weight' => 3);
-
-    // configure sub tabs
-    $items[] = array('path' => 'admin/content/ad/configure/global',
-                     'title' => t('Global settings'),
-                     'callback' => 'drupal_get_form',
-                     'callback arguments' => array('ad_admin_configure_settings'),
-                     'type' => MENU_DEFAULT_LOCAL_TASK,
-                     'weight' => 0);
-    $items[] = array('path' => 'node/add/ad',
-                     'title' => t('Ad'),
-                     'callback' => 'ad_add',
-                     'access' => user_access('create advertisements'));
-
-    $adtypes = module_invoke_all('adapi', 'type', array());
-    foreach ($adtypes as $adtype) {
-      $items[] = array(
-       'path' => 'node/add/ad/'. $adtype,
-       'title' => t('!type advertisement', array('!type' => t($adtype))),
-       'access' => user_access('create advertisements'),
+  // menu items
+  $items['admin/content/ad'] = array(
+    'title' => 'Ads',
+    'page callback' => 'ad_admin_list',
+    'access callback' => 'user_access',
+    'access arguments' => array('administer advertisements'),
+    'description' => 'Configure and manage your advertising system.'
+  );
+
+  // tabs
+  $items['admin/content/ad/list'] = array(
+    'title' => 'List',
+    'page callback' => 'ad_admin_list',
+    'type' => MENU_DEFAULT_LOCAL_TASK
+  );
+  $items['admin/content/ad/statistics'] = array(
+    'title' => 'Statistics',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ad_admin_statistics'),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 1
+  );
+  $items['admin/content/ad/configure'] = array(
+    'title' => 'Settings',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ad_admin_configure_settings'),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 3
+  );
+  $items['admin/content/ad/groups'] = array(
+    'title' => 'Groups',
+    'page callback' => 'ad_admin_groups_list',
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 5
+  );
+
+  // groups sub tabs
+  $items['admin/content/ad/groups/list'] = array(
+    'title' => 'List',
+    'page callback' => 'ad_admin_groups_list',
+    'type' => MENU_DEFAULT_LOCAL_TASK, 
+    'weight' => 0
+  );
+  $items['admin/content/ad/groups/add'] = array( 
+    'title' => 'Create group',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ad_admin_group_form'),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 3
+  );
+
+  // configure sub tabs
+  $items['admin/content/ad/configure/global'] = array(
+    'title' => 'Global settings',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ad_admin_configure_settings'),
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+    'weight' => 0
+  );
+  
+//according to coder, next two menu items should now be auto generated.
+//  $items['node/add/ad'] = array(
+//    'title' => 'Ad',
+//    'page callback' => 'ad_add',
+//    'access callback' => 'user_access',
+//    'access callback' => array('create advertisements')
+//    );
+
+//  $adtypes = module_invoke_all('adapi', 'type', array());
+//  foreach ($adtypes as $adtype) {
+//    $items['node/add/ad/'. $adtype] = array(
+//     'title' => '!type advertisement', array('!type' => t($adtype)),
+//     'access callback' => 'user_access',
+//     'access arguments' => array('create advertisements'),
+//    );
+//  }
+
+  // callbacks
+  if (arg(0) == 'ad' && arg(1) == 'redirect' && is_numeric(arg(2))) {
+    $aid = preg_replace('/[^0-9]/', '', arg(2));
+    $group = preg_replace('/[^0-9,nt]/', '', arg(3));
+    $hostid = preg_replace('/[^0-9a-f]/', '', arg(4));
+    $items["ad/redirect/%aid"] = array(
+      'access callback' => 'user_access',
+      'access arguments' => array('show advertisements'),
+      'type' => MENU_CALLBACK, 
+      'page callback' => 'ad_redirect',
+      'page arguments' => array($aid, $group, $hostid)
+    );
+  }
+  elseif (arg(2) == 'ad' && arg(3) == 'groups' && is_numeric(arg(4))) {
+    if ($term = taxonomy_get_term(arg(4))) {
+      $items["admin/content/ad/groups/%_taxonomy_term/edit"] = array( 
+        'title' => 'edit',
+        'page callback' => 'drupal_get_form',
+        'page arguments' => array('ad_admin_group_form', (array)$term),
+        'access callback' => 'user_access',
+        'access arguments' => array('administer advertisements'),
+        'type' => MENU_CALLBACK,
+        'weight' => 1);
+      $items["admin/content/ad/groups/$term->tid/delete"] = array(
+        'title' => 'delete',
+        'page callback' => 'drupal_get_form',
+        'page arguments' => array('ad_confirm_group_delete', (array)$term),
+        'access callback' => 'user_access',
+        'access arguments' => array('administer advertisements'),
+        'type' => MENU_CALLBACK,
+        'weight' => 2);
+    }
+  }
+  elseif (arg(0) == 'node' && is_numeric(arg(1)) && ad_adaccess(arg(1), 'manage owners')) {
+//    $node = node_load(arg(1));
+    $node = menu_get_object($type = 'node', $position = 1, $path = NULL);
+    if ($node->adtype) {
+      $items["node/$node->nid/adowners"] = array(
+        'access callback' => 'ad_adaccess',
+        'access arguments' => array($node->nid, 'manage owners'),
+        'title' => 'Ad owners',
+        'page callback' => 'ad_owners_overview',
+        'page arguments' => array($node),
+        'type' => MENU_LOCAL_TASK,
+        'weight' => 5
       );
-    }
-  }
-  else {
-    // callbacks
-    if (arg(0) == 'ad' && arg(1) == 'redirect' && is_numeric(arg(2))) {
-      $aid = preg_replace('/[^0-9]/', '', arg(2));
-      $group = preg_replace('/[^0-9,nt]/', '', arg(3));
-      $hostid = preg_replace('/[^0-9a-f]/', '', arg(4));
-      $items[] = array('path' => "ad/redirect/$aid",
-                       'access' => user_access('show advertisements'),
-                       'type' => MENU_CALLBACK, 
-                       'callback' => 'ad_redirect',
-                       'callback arguments' => array($aid, $group, $hostid));
-    }
-    elseif (arg(2) == 'ad' && arg(3) == 'groups' && is_numeric(arg(4))) {
-      if ($term = taxonomy_get_term(arg(4))) {
-        $items[] = array('path' => "admin/content/ad/groups/$term->tid/edit", 
-                         'title' => t('edit'),
-                         'callback' => 'drupal_get_form',
-                         'callback arguments' => array(
-                                                   'ad_admin_group_form', 
-                                                   (array)$term),
-                         'access' => user_access('administer advertisements'),
-                         'type' => MENU_CALLBACK,
-                         'weight' => 1);
-        $items[] = array('path' => "admin/content/ad/groups/$term->tid/delete", 
-                         'title' => t('delete'),
-                         'callback' => 'drupal_get_form',
-                         'callback arguments' => array(
-                                                   'ad_confirm_group_delete', 
-                                                   (array)$term),
-                         'access' => user_access('administer advertisements'),
-                         'type' => MENU_CALLBACK,
-                         'weight' => 2);
+      $items["node/$node->nid/adowners/list"] = array(
+        'access callback' => 'ad_adaccess',
+        'access arguments' => array($node->nid, 'manage owners'),
+        'title' => 'List',
+        'type' => MENU_DEFAULT_LOCAL_TASK,
+        'weight' => 0
+      );
+      if (is_numeric(arg(3))) {
+        $uid = arg(3);
+        $ad_user = user_load(array('uid' => $uid));
+        $items["node/$node->nid/adowners/$uid/permissions"] = array(
+          'title' => '!owner\'s permissions', array('!owner' => $ad_user->name),
+          'access callback' => 'ad_adaccess',
+          'access arguments' => array($node->nid, 'manage owners'),
+          'page callback' => 'drupal_get_form',
+          'page arguments' => array('ad_owner_permissions', $node->nid, $uid),
+          'type' => MENU_LOCAL_TASK,
+          'weight' => 2
+        );
+        $items["node/$node->nid/adowners/$uid/remove"] = array(
+          'page callback' => 'drupal_get_form',
+          'page arguments' => array('ad_owner_remove', $node->nid, $uid),
+          'type' => MENU_CALLBACK,
+          'weight' => 6
+        );
       }
-    }
-    elseif (arg(0) == 'node' && is_numeric(arg(1)) && ad_adaccess(arg(1), 'manage owners')) {
-      $node = node_load(arg(1));
-      if ($node->adtype) {
-        $items[] = array('path' => "node/$node->nid/adowners",
-                         'access' => ad_adaccess($node->nid, 'manage owners'),
-                         'title' => t('Ad owners'),
-                         'callback' => 'ad_owners_overview',
-                         'callback arguments' => array($node),
-                         'type' => MENU_LOCAL_TASK,
-                         'weight' => 5);
-        $items[] = array('path' => "node/$node->nid/adowners/list",
-                         'access' => ad_adaccess($node->nid, 'manage owners'),
-                         'title' => t('List'),
-                         'type' => MENU_DEFAULT_LOCAL_TASK,
-                         'weight' => 0);
-        if (is_numeric(arg(3))) {
-          $uid = arg(3);
-          $ad_user = user_load(array('uid' => $uid));
-          $items[] = array('path' => "node/$node->nid/adowners/$uid/permissions",
-                           'title' => t('!owner\'s permissions', array('!owner' => $ad_user->name)),
-                           'access' => ad_adaccess($node->nid, 'manage owners'),
-                           'callback' => 'drupal_get_form',
-                           'callback arguments' => array('ad_owner_permissions', $node->nid, $uid),
-                           'type' => MENU_LOCAL_TASK,
-                           'weight' => 2);
-          $items[] = array('path' => "node/$node->nid/adowners/$uid/remove",
-                           'callback' => 'drupal_get_form',
-                           'callback arguments' => array('ad_owner_remove', $node->nid, $uid),
-                           'type' => MENU_CALLBACK,
-                           'weight' => 6);
-        }
-        else {
-          $items[] = array('path' => "node/$node->nid/adowners/add",
-                           'access' => ad_adaccess($node->nid, 'manage owners'),
-                           'title' => t('Add'),
-                           'callback' => 'drupal_get_form',
-                           'callback arguments' => array('ad_owners_add_form', $node),
-                           'type' => MENU_LOCAL_TASK,
-                           'weight' => 4);
-        }
+      else {
+        $items["node/$node->nid/adowners/add"] = array(
+          'access callback' => 'ad_adaccess',
+          'access arguments' => array($node->nid, 'manage owners'),
+          'title' => 'Add',
+          'page callback' => 'drupal_get_form',
+          'page arguments' => array('ad_owners_add_form', $node),
+          'type' => MENU_LOCAL_TASK,
+          'weight' => 4
+        );
       }
     }
-    if (is_numeric($nid = arg(1)) && is_numeric($cid = arg(3))) {
-      $items[] = array(
-        'path' => "node/$nid/details/$cid",
-        'title' => t('Click details'),
-        'callback' => 'ad_click_details',
-        'callback arguments' => array($nid, $cid),
-        'type' => MENU_CALLBACK
-      );
-    }
+  }
+  if (is_numeric($nid = arg(1)) && is_numeric($cid = arg(3))) {
+    $items["node/$nid/details/$cid"] = array(
+      'title' => 'Click details',
+      'page callback' => 'ad_click_details',
+      'page arguments' => array($nid, $cid),
+      'type' => MENU_CALLBACK
+    );
   }
 
   return $items;
@@ -1342,29 +1370,29 @@ function ad_owners_add_form($node) {
   return $form;
 }
 
-function ad_owners_add_form_validate($form_id, $form_values) {
-  $owner = user_load(array('name' => $form_values['username']));
+function ad_owners_add_form_validate($form, &$form_state) {
+  $owner = user_load(array('name' => $form_state['values']['username']));
   if (!is_object($owner)) {
-    form_set_error('username', t('The specified username %username does not exist.', array('%username' => $form_values['username'])));
+    form_set_error('username', t('The specified username %username does not exist.', array('%username' => $form_state['values']['username'])));
   }
-  else if (db_result(db_query('SELECT oid FROM {ad_owners} WHERE uid = %d AND aid = %d', $owner->uid, $form_values['aid']))) {
-    form_set_error('username', t('The specified user %username is already an owner of this ad.', array('%username' => $form_values['username'])));
+  else if (db_result(db_query('SELECT oid FROM {ad_owners} WHERE uid = %d AND aid = %d', $owner->uid, $form_state['values']['aid']))) {
+    form_set_error('username', t('The specified user %username is already an owner of this ad.', array('%username' => $form_state['values']['username'])));
   }
   else if (!user_access('edit own advertisements', $owner) &&
            !user_access('administer advertisements', $owner)) {
-    form_set_error('username', t('The specified user %username does not have <em>edit own advertisements</em> nor <em>administer advertisements</em> permissions.  The user must be !assigned to a !role with these privileges before you can add them as an ad owner.', array('%username' => $form_values['username'], '!assigned' => l(t('assigned'), "user/$owner->uid/edit"), '!role' => l(t('role'), 'admin/user/access'))));
+    form_set_error('username', t('The specified user %username does not have <em>edit own advertisements</em> nor <em>administer advertisements</em> permissions.  The user must be !assigned to a !role with these privileges before you can add them as an ad owner.', array('%username' => $form_state['values']['username'], '!assigned' => l(t('assigned'), "user/$owner->uid/edit"), '!role' => l(t('role'), 'admin/user/access'))));
   }
-  module_invoke_all('adowners', 'validate', $owner, $form_values['aid']);
+  module_invoke_all('adowners', 'validate', $owner, $form_state['values']['aid']);
 }
 
-function ad_owners_add_form_submit($form_id, $form_values) {
-  $owner = user_load(array('name' => $form_values['username']));
-  if (!(ad_owners_add($form_values['aid'], $owner->uid, array('access statistics', 'access click history')))) {
+function ad_owners_add_form_submit($form, &$form_state) {
+  $owner = user_load(array('name' => $form_state['values']['username']));
+  if (!(ad_owners_add($form_state['values']['aid'], $owner->uid, array('access statistics', 'access click history')))) {
     form_set_error('username', t('The user is already an owner of the ad.'));
   }
   else {
-    drupal_set_message(t('The user %username has been added as an owner of this advertisement.', array('%username' => $form_values['username'])));
-    drupal_goto('node/'. $form_values['aid'] ."/adowners/$owner->uid/permissions");
+    drupal_set_message(t('The user %username has been added as an owner of this advertisement.', array('%username' => $form_state['values']['username'])));
+    drupal_goto('node/'. $form_state['values']['aid'] ."/adowners/$owner->uid/permissions");
   }
 }
 
@@ -1503,11 +1531,11 @@ function theme_ad_owner_permissions($for
 /**
  * Store the ad owner's updated permissions in the ad_permissions table.
  */
-function ad_owner_permissions_submit($form_id, $form_values) {
+function ad_owner_permissions_submit($form, &$form_state) {
   $permissions = module_invoke_all('adapi', 'permissions', array());
   $perms = array();
   foreach ($permissions as $permission) {
-    if ($form_values[str_replace(' ', '_', "$permission")]) {
+    if ($form_state['values'][str_replace(' ', '_', "$permission")]) {
       $perms[] = $permission;
     }
   }
@@ -1518,8 +1546,8 @@ function ad_owner_permissions_submit($fo
     // MySQL, MySQLi
     db_query('LOCK TABLES {ad_permissions} WRITE');
   }
-  db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $form_values['oid']);
-  db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $form_values['oid'], implode('|,|', $perms));
+  db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $form_state['values']['oid']);
+  db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $form_state['values']['oid'], implode('|,|', $perms));
   if ($GLOBALS['db_type'] == 'pgsql') {
     db_query('COMMIT;');
   }
@@ -1528,7 +1556,8 @@ function ad_owner_permissions_submit($fo
     db_query('UNLOCK TABLES');
   }
   drupal_set_message(t('The permissions have been saved.'));
-  return "node/$form_values[aid]/adowners";
+//  $form_state['redirect'] = "node/$form_state['values']['aid']/adowners";
+//  $form_state['nid'] = $node->nid;
 }
 
 /**
@@ -1728,14 +1757,15 @@ function ad_multiple_delete_confirm() {
 /**
  * Perform the actual ad deletions.
  */
-function ad_multiple_delete_confirm_submit($form_id, $form_values) {
-  if ($form_values['confirm']) {
-    foreach ($form_values['ads'] as $aid => $value) {
+function ad_multiple_delete_confirm_submit($form, &$form_state) {
+  if ($form_state['values']['confirm']) {
+    foreach ($form_state['values']['ads'] as $aid => $value) {
       node_delete($aid);
     }
     drupal_set_message(t('The ads have been deleted.'));
   }
-  return 'admin/content/ad';
+  $form_state['redirect'] = 'admin/content/ad';
+  $form_state['nid'] = $node->nid;
 }
 
 /**
@@ -1776,8 +1806,8 @@ function theme_ad_admin_ads($form) {
 /**
  * Must select an ad if performing an operation.
  */
-function ad_admin_ads_validate($form_id, $form_values) {
-  $ads = array_filter($form_values['ads']);
+function ad_admin_ads_validate($form, &$form_state) {
+  $ads = array_filter($form_state['values']['ads']);
   if (count($ads) == 0) {
     form_set_error('', t('No ads selected.'));
   }
@@ -1786,11 +1816,11 @@ function ad_admin_ads_validate($form_id,
 /**
  * Submit the ad administration update form.
  */
-function ad_admin_ads_submit($form_id, $form_values) {
+function ad_admin_ads_submit($form, &$form_state) {
   $operations = module_invoke_all('ad_operations');
-  $operation = $operations[$form_values['operation']];
+  $operation = $operations[$form_state['values']['operation']];
   // Filter out unchecked nodes
-  $ads = array_filter($form_values['ads']);
+  $ads = array_filter($form_state['values']['ads']);
   if ($function = $operation['callback']) {
     // Add in callback arguments if present.
     if (isset($operation['callback arguments'])) {
@@ -1985,19 +2015,19 @@ function theme_ad_filter_form($form) {
 /**
  * Process result from ad administration filter form.
  */
-function ad_filter_form_submit($form_id, $form_values) {
+function ad_filter_form_submit($form, &$form_state) {
   $filters = ad_filters();
-  switch ($form_values['op']) {
+  switch ($form_state['values']['op']) {
     case t('Filter'):
     case t('Refine'):
-      if (isset($form_values['filter'])) {
-        $filter = $form_values['filter'];
+      if (isset($form_state['values']['filter'])) {
+        $filter = $form_state['values']['filter'];
 
         // Flatten the options array to accommodate hierarchical/nested options.
         $flat_options = form_options_flatten($filters[$filter]['options']);
 
-        if (isset($flat_options[$form_values[$filter]])) {
-          $_SESSION['ad_overview_filter'][] = array($filter, $form_values[$filter]);
+        if (isset($flat_options[$form_state['values'][$filter]])) {
+          $_SESSION['ad_overview_filter'][] = array($filter, $form_state['values'][$filter]);
         }
       }
       break;
@@ -2257,9 +2287,9 @@ function ad_admin_configure_settings($ed
 /**
  * Validate form settings, calling attention to any illogical configurations.
  */
-function ad_admin_configure_settings_validate($form_id, $form_values) {
-  if ($form_values['ad_link_target'] == '_self' && 
-      $form_values['ad_display'] == 'iframe') {
+function ad_admin_configure_settings_validate($form, &$form_state) {
+  if ($form_state['values']['ad_link_target'] == '_self' && 
+      $form_state['values']['ad_display'] == 'iframe') {
     // We don't consider this an error, as this could be exactly what the
     // administrator is trying to do.  But as for most people it is likely
     // to be a misconfiguration, display a helpful warning...
@@ -2270,26 +2300,26 @@ function ad_admin_configure_settings_val
 /**
  * Save updated values from settings form.
  */
-function ad_admin_configure_settings_submit($form_id, $form_values) {
-  variable_set('ad_link_target', $form_values['ad_link_target']);
-  variable_set('ad_link_nofollow', $form_values['ad_link_nofollow']);
-  variable_set('ad_cache', $form_values['ad_cache']);
-  variable_set('ad_display', $form_values['ad_display']);
-  variable_set('ad_validate_url', $form_values['ad_validate_url']);
-  variable_set('ad_iframe_frameborder', $form_values['ad_iframe_frameborder']);
-  variable_set('ad_iframe_scroll', $form_values['ad_iframe_scroll']);
-  variable_set('ad_iframe_width', $form_values['ad_iframe_width']);
-  variable_set('ad_iframe_height', $form_values['ad_iframe_height']);
+function ad_admin_configure_settings_submit($form, &$form_state) {
+  variable_set('ad_link_target', $form_state['values']['ad_link_target']);
+  variable_set('ad_link_nofollow', $form_state['values']['ad_link_nofollow']);
+  variable_set('ad_cache', $form_state['values']['ad_cache']);
+  variable_set('ad_display', $form_state['values']['ad_display']);
+  variable_set('ad_validate_url', $form_state['values']['ad_validate_url']);
+  variable_set('ad_iframe_frameborder', $form_state['values']['ad_iframe_frameborder']);
+  variable_set('ad_iframe_scroll', $form_state['values']['ad_iframe_scroll']);
+  variable_set('ad_iframe_width', $form_state['values']['ad_iframe_width']);
+  variable_set('ad_iframe_height', $form_state['values']['ad_iframe_height']);
   if (($cache = variable_get('ad_cache', 'none')) != 'none') {
     // Allow external cache types to store their settings
-    module_invoke('ad_cache_'. $cache, 'adcacheapi', 'settings_submit', $form_values);
+    module_invoke('ad_cache_'. $cache, 'adcacheapi', 'settings_submit', $form_state['values']);
   }
 /*
  // TODO: Write an external display module and implement this.
   $display = variable_get('ad_display', 'javascript');
   if ($display != 'javascript' && $display != 'raw') {
     // Allow external display types to store their settings
-    module_invoke('ad_cache_'. $cache, 'adcacheapi', 'settings_submit', $form_values);
+    module_invoke('ad_cache_'. $cache, 'adcacheapi', 'settings_submit', $form_state['values']);
   }
 */
 }
@@ -2408,26 +2438,26 @@ function ad_owner_remove($aid, $uid) {
 /**
  * Don't allow the removal of the primary owner of the advertisement.
  */
-function ad_owner_remove_validate($form_id, $form_values) {
-  $node = node_load($form_values['aid']);
-  if ($node->uid == $form_values['uid']) {
-    $owner = user_load(array('uid' => $form_values['uid']));
+function ad_owner_remove_validate($form, &$form_state) {
+  $node = node_load($form_state['values']['aid']);
+  if ($node->uid == $form_state['values']['uid']) {
+    $owner = user_load(array('uid' => $form_state['values']['uid']));
     drupal_set_message(t('%name is the primary owner of this advertisement.  You cannot remove the primary owner.', array('%name' => $owner->name)), 'error');
-    drupal_goto('node/'. $form_values['aid'] .'/adowners');
+    drupal_goto('node/'. $form_state['values']['aid'] .'/adowners');
   }
 }
 
 /**
  * Remove the ad owner, and all associated permissions.
  */
-function ad_owner_remove_submit($form_id, $form_values) {
-  $oid = db_result(db_query('SELECT oid FROM {ad_owners} WHERE aid = %d AND uid = %d', $form_values['aid'], $form_values['uid']));
+function ad_owner_remove_submit($form, &$form_state) {
+  $oid = db_result(db_query('SELECT oid FROM {ad_owners} WHERE aid = %d AND uid = %d', $form_state['values']['aid'], $form_state['values']['uid']));
   db_query('DELETE FROM {ad_owners} WHERE oid = %d', $oid);
   db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $oid);
-  $owner = user_load(array('uid' => $form_values['uid']));
+  $owner = user_load(array('uid' => $form_state['values']['uid']));
   module_invoke_all('adowners', 'remove', $oid, $owner);
   drupal_set_message(t('The ad owner %name has been removed.', array('%name' => $owner->name)));
-  drupal_goto('node/'. $form_values['aid'] .'/adowners');
+  drupal_goto('node/'. $form_state['values']['aid'] .'/adowners');
 }
 
 /**
@@ -2654,19 +2684,20 @@ function ad_admin_group_form($group = ar
 /**
  * Save a newly created ad group.
  */
-function ad_admin_group_form_submit($form_id, $form_values) {
-  $status = taxonomy_save_term($form_values);
+function ad_admin_group_form_submit($form, &$form_state) {
+  $status = taxonomy_save_term($form_state['values']);
   switch ($status) {
     case SAVED_NEW:
       $groups = variable_get('ad_groups', array());
-      $groups[] = $form_values['tid'];
+      $groups[] = $form_state['values']['tid'];
       variable_set('ad_groups', $groups);
-      drupal_set_message(t('Created new ad group %term.', array('%term' => $form_values['name'])));
+      drupal_set_message(t('Created new ad group %term.', array('%term' => $form_state['values']['name'])));
       break;
     case SAVED_UPDATED:
-      drupal_set_message(t('The ad group %term has been updated.', array('%term' => $form_values['name'])));
+      drupal_set_message(t('The ad group %term has been updated.', array('%term' => $form_state['values']['name'])));
   }
-  return 'admin/content/ad/groups';
+  $form_state['redirect'] = 'admin/content/ad/groups';
+  $form_state['nid'] = $node->nid;
 }
 
 /**
@@ -2688,12 +2719,13 @@ function ad_confirm_group_delete($term) 
 /**
  * Delete ad group.
  */
-function ad_confirm_group_delete_submit($form_id, $form_values) {
-  taxonomy_del_term($form_values['tid']);
-  drupal_set_message(t('The ad group %term has been deleted.', array('%term' => $form_values['name'])));
-  watchdog('ad', t('mailarchive: deleted %term ad group.', array('%term' => $form_values['name'])));
+function ad_confirm_group_delete_submit($form, &$form_state) {
+  taxonomy_del_term($form_state['values']['tid']);
+  drupal_set_message(t('The ad group %term has been deleted.', array('%term' => $form_state['values']['name'])));
+  watchdog('ad', 'mailarchive: deleted %term ad group.', array('%term' => $form_state['values']['name']));
 
-  return 'admin/content/ad/groups';
+  $form_state['redirect'] = 'admin/content/ad/groups';
+  $form_state['nid'] = $node->nid;
 }
 
 /**
