--- decisions.module	2009-08-05 12:04:14.000000000 +0200
+++ decisions_new.module	2009-08-05 13:08:28.000000000 +0200
@@ -997,7 +997,7 @@ function _decisions_form_prepare_datetim
       'month' => _decisions_date('n', $time),
       'year' => _decisions_date('Y', $time),
       'hour' => _decisions_date('H', $time),
-      'min' => _decisions_date('i', $time),
+      'minute' => _decisions_date('i', $time),
       'sec' => _decisions_date('s', $time),
     );
   }
@@ -1073,6 +1073,22 @@ function decisions_load($node) {
   $criteria['content_id'] = $node->nid;
   $decision->voted = count(votingapi_select_votes($criteria)) > 0;
 
+  // Respect TREE form
+  $decision->settings['algorithm']                  = $decision->algorithm;
+  $decision->settings['uselist']                    = $decision->uselist;
+  $decision->settings['active']                     = $decision->active;
+  $decision->settings['showvotes']                  = $decision->showvotes;
+  $decision->settings['maxchoices']                 = $decision->maxchoices;
+  $decision->settings['quorum']['quorum_abs']       = $decision->quorum_abs;
+  $decision->settings['quorum']['quorum_percent']   = $decision->quorum_percent;
+  $decision->settings['date']['startdate']['date']  = $decision->startdate;
+ 
+  // Calculate end date and intialize star date and runtime
+ 	$enddate = _decisions_enddate_calc($decision);
+
+  $decision->settings['date']['noenddate'] = (($decision->runtime == DECISIONS_RUNTIME_INFINITY) ? 1 : 0);
+  $decision->settings['date']['enddate']['date'] = $enddate;
+
   return $decision;
 }
 
@@ -1112,7 +1128,7 @@ function decisions_insert($node) {
   // just create an empty entry for now
   $mode = _decisions_get_mode($node);
 
-  db_query("INSERT INTO {decisions} (nid, mode, quorum_abs, quorum_percent, uselist, active, runtime, maxchoices, algorithm, startdate) VALUES (%d, '%s', %d, %f, %d, %d, %d, %d, '%s', %d)", $node->nid, $mode, $node->settings['quorum']['quorum_abs'], $node->settings['quorum']['quorum_percent'], $node->settings['uselist'], $node->settings['active'], $node->settings['runtime'], $node->settings['maxchoices'], $node->settings['algorithm'], $startdate);
+  db_query("INSERT INTO {decisions} (nid, mode, quorum_abs, quorum_percent, uselist, active, runtime, maxchoices, algorithm, startdate) VALUES (%d, '%s', %d, %f, %d, %d, %d, %d, '%s', %d)", $node->nid, $mode, $node->settings['quorum']['quorum_abs'], $node->settings['quorum']['quorum_percent'], $node->settings['uselist'], $node->settings['active'], $runtime, $node->settings['maxchoices'], $node->settings['algorithm'], $startdate);
 
   // create the electoral list if desired
 
@@ -1140,7 +1156,7 @@ function decisions_validate(&$node) {
   // Check for at least two choices
   $realchoices = 0;
   foreach ($node->choice as $i => $choice) {
-    if ($choice['label'] != '') {
+    if (is_array($choice) && $choice['label'] != '') {
       $realchoices++;
     }
   }
@@ -1163,12 +1179,22 @@ function decisions_validate(&$node) {
  * This is called upon node edition.
  */
 function decisions_update($node) {
+  // Format dates if hook_update processed without form
+  if (!is_array($node->settings['date']['startdate']['date'])) {
+    $node->settings['date']['startdate']['date'] = _decisions_form_prepare_datetime($node->settings['date']['startdate']['date']);
+	}
+
   // Compute startdate and runtime.
   $startdate = _decisions_translate_form_date($node->settings['date']['startdate']['date']);
   if ($node->settings['date']['noenddate']) {
     $runtime = DECISIONS_RUNTIME_INFINITY;
   }
   else {
+	  // Format dates if hook_update processed without form
+	  if (!is_array($node->settings['date']['enddate']['date'])) {
+		  $node->settings['date']['enddate']['date'] = _decisions_form_prepare_datetime($node->settings['date']['enddate']['date']);
+    }
+
     $enddate =  _decisions_translate_form_date($node->settings['date']['enddate']['date']);
     if ($enddate < $startdate) {
       form_set_error('enddate', t('The specified close date is less than the opening date, setting it to the same for now.'));
@@ -1488,33 +1514,27 @@ function decisions_form(&$node, &$form_s
     '#collapsed' => TRUE,
     '#collapsible' => TRUE,
   );
-  
-  $startdate = !is_null($node->startdate) ? $node->startdate : time();
-  $runtime = !is_null($node->runtime) ? $node->runtime : variable_get('decisions_default_runtime', 24 * 60 * 60);
-  if ($runtime == DECISIONS_RUNTIME_INFINITY) {
-    $enddate = $startdate; // by default
-  }
-  else {
-    $enddate = $startdate + $runtime;
-  }
+
+  // Calculate end date and intialize star date and runtime
+ 	$enddate = _decisions_enddate_calc($node);
   
   $form['settings']['date']['startdate'] = array(
     '#type' => 'fieldset',
     '#title' => t('Opening date')
   );  
-  $form['settings']['date']['startdate']['date'] = _decisions_form_date($startdate);
+  $form['settings']['date']['startdate']['date'] = _decisions_form_date($node->startdate);
   
   $form['settings']['date']['noenddate'] = array(
     '#type' => 'checkbox',
     '#title' => t('No closing date'),
-    '#default_value' => ($runtime != DECISIONS_RUNTIME_INFINITY),
+    '#default_value' => ($node->runtime == DECISIONS_RUNTIME_INFINITY),
     '#description' => t('Check this box if you do not want the vote to close on a specific date.'),
     '#attributes' => array('onClick' => 'Drupal.toggleFieldset($("#enddate"))'),
   );
 
   $form['settings']['date']['enddate'] = array(
     '#type' => 'fieldset',
-    '#collapsed' => ($runtime != DECISIONS_RUNTIME_INFINITY),
+    '#collapsed' => ($node->runtime == DECISIONS_RUNTIME_INFINITY),
     '#collapsible' => TRUE,
     '#attributes' => array('id' => 'enddate'),
     '#title' => t('Closing date')
@@ -1636,3 +1656,22 @@ function _decisions_date($format, $times
   $result = gmdate($format, $timestamp);
   return  $result;
 }
+
+/**
+ * Calculate enddate and initialize startdate and runtime
+ */
+function _decisions_enddate_calc($node) {
+  if (is_null($node->startdate)) {
+	  $node->startdate = time();
+	}
+	if (is_null($node->runtime)) {
+    $node->runtime = variable_get('decisions_default_runtime', 24 * 60 * 60);
+	}
+  if ($node->runtime == DECISIONS_RUNTIME_INFINITY) {
+    $enddate = $node->startdate; // by default
+  }
+  else {
+    $enddate = $node->startdate + $node->runtime;
+  }
+	return $enddate;
+}
