-    if (!is_a($res, 'PEAR_Error')) {
-      $val = $res->fetchRow();
-      return $val;
-    }
-
-    return false;
-  }
-
-  // The insert function creates a new team.
-  static function insert($fields) {
-
-    $mdb2 = getConnection();
-
-    $lock_spec = $fields['lock_spec'];
-    if ($lock_spec !== null) {
-      $lockspec_f = ', lock_spec';
-      $lockspec_v = ', ' . $mdb2->quote($lock_spec);
-    } else {
-      $lockspec_f = '';
-      $lockspec_v = '';
-    }
-
-    $lang = $fields['lang'];
-    if (!$lang) {
-      global $i18n;
-      $lang = $i18n->lang;
-    }
-
-    $decimal_mark = $fields['decimal_mark'];
-    if ($decimal_mark !== null) {
-      $decimal_mark_f = ', decimal_mark';
-      $decimal_mark_v = ', ' . $mdb2->quote($decimal_mark);
-    } else {
-      $decimal_mark_f = '';
-      $decimal_mark_v = '';
-    }
-
-    $date_format = $fields['date_format'];
-    if ($date_format !== null) {
-      $date_format_f = ', date_format';
-      $date_format_v = ', ' . $mdb2->quote($date_format);
-    } elseif (defined('DATE_FORMAT_DEFAULT')) {
-      $date_format_f = ', date_format';
-      $date_format_v = ', ' . $mdb2->quote(DATE_FORMAT_DEFAULT);
-    } else {
-      $date_format_f = '';
-      $date_format_v = '';
-    }
-
-    $time_format = $fields['time_format'];
-    if ($time_format !== null) {
-      $time_format_f = ', time_format';
-      $time_format_v = ', ' . $mdb2->quote($time_format);
-    } elseif (defined('TIME_FORMAT_DEFAULT')) {
-      $time_format_f = ', time_format';
-      $time_format_v = ', ' . $mdb2->quote(TIME_FORMAT_DEFAULT);
-    } else {
-      $time_format_f = '';
-      $time_format_v = '';
-    }
-
-    $week_start = $fields['week_start'];
-    if ($week_start !== null) {
-      $week_start_f = ', week_start';
-      $week_start_v = ', ' . (int)$week_start;
-    } elseif (defined('WEEK_START_DEFAULT')) {
-      $week_start_f = ', week_start';
-      $week_start_v = ', ' . (int)WEEK_START_DEFAULT;
-    } else {
-      $week_start_f = '';
-      $week_start_v = '';
-    }
-
-    $plugins = $fields['plugins'];
-    if ($plugins !== null) {
-      $plugins_f = ', plugins';
-      $plugins_v = ', ' . $mdb2->quote($plugins);
-    } else {
-      $plugins_f = '';
-      $plugins_v = '';
-    }
-
-    $tracking_mode = $fields['tracking_mode'];
-    if ($tracking_mode !== null) {
-      $tracking_mode_f = ', tracking_mode';
-      $tracking_mode_v = ', ' . (int)$tracking_mode;
-    } else {
-      $tracking_mode_f = '';
-      $tracking_mode_v = '';
-    }
-
-    $record_type = $fields['record_type'];
-    if ($record_type !== null) {
-      $record_type_f = ', record_type';
-      $record_type_v = ', ' . (int)$record_type;
-    } else {
-      $record_type_f = '';
-      $record_type_v = '';
-    }
-
-    $sql = "insert into tt_teams (name, address, currency $lockspec_f, lang $decimal_mark_f $date_format_f $time_format_f $week_start_f $plugins_f $tracking_mode_f $record_type_f)
-      values(".$mdb2->quote(trim($fields['name'])).
-      ", ".$mdb2->quote(trim($fields['address'])).
-      ", ".$mdb2->quote(trim($fields['currency']))." $lockspec_v, ".$mdb2->quote($lang).
-      "$decimal_mark_v $date_format_v $time_format_v $week_start_v $plugins_v $tracking_mode_v $record_type_v)";
-    $affected = $mdb2->exec($sql);
-
-    if (!is_a($affected, 'PEAR_Error')) {
-      $team_id = $mdb2->lastInsertID('tt_teams', 'id');
-      return $team_id;
-    }
-
-    return false;
-  }
-
-  // The update function updates team information.
-  static function update($team_id, $fields)
-  {
-    // We'll require team name to be always set.
-    if (!isset($fields['name']) || $fields['name'] == "") return false;
-
-    $mdb2 = getConnection();
-    $name_part = 'name = '.$mdb2->quote($fields['name']);
-    $currency_part = '';
-    $addr_part = '';
-    $lang_part = '';
-    $decimal_mark_part = '';
-    $date_format_part = '';
-    $time_format_part = '';
-    $week_start_part = '';
-    $tracking_mode_part = '';
-    $record_type_part = '';
-    $plugins_part = '';
-    $lock_spec_part = '';
-    $working_hours_part = '';
-
-    if (isset($fields['address'])) $addr_part = ', address = '.$mdb2->quote($fields['address']);
-    if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
-    if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
-    if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
-    if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
-    if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
-    if (isset($fields['week_start'])) $week_start_part = ', week_start = '.intval($fields['week_start']);
-    if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.intval($fields['tracking_mode']);
-    if (isset($fields['record_type'])) $record_type_part = ', record_type = '.intval($fields['record_type']);
-    if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
-    if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
-    if (isset($fields['working_hours'])) $working_hours_part = ', workday_hours = '.$mdb2->quote($fields['working_hours']);
-
-    $sql = "update tt_teams set $name_part $addr_part $currency_part $lang_part $decimal_mark_part
-      $date_format_part $time_format_part $week_start_part $tracking_mode_part $record_type_part
-      $plugins_part $lock_spec_part $working_hours_part where id = $team_id";
-    $affected = $mdb2->exec($sql);
-    if (is_a($affected, 'PEAR_Error')) return false;
-
-    return true;
-  }
-
-  // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 25).
-  static function getInactiveTeams() {
-    $inactive_teams = array();
-    $mdb2 = getConnection();
-
-    // Get all team ids for teams created or modified more than 1 year ago.
-    $ts = date('Y-m-d', strtotime('-1 year'));
-    $sql =  "select id from tt_teams where timestamp < '$ts' order by id";
-    $res = $mdb2->query($sql);
-
-    $count = 0;