--- comment_upload/comment_upload.module	2007-05-13 16:54:01.000000000 -0300
+++ comment_upload.module	2007-12-15 06:19:19.000000000 -0200
@@ -1,5 +1,4 @@
 <?php
-// $Id: comment_upload.module,v 1.6.2.11 2007/05/13 19:54:01 heine Exp $
 
 /**
  * Implementation of hook_comment.
@@ -8,16 +7,58 @@
   $cid = is_object($comment) ? $comment->cid : $comment['cid'];
   $cid = is_array($cid) ? $cid['#value'] : $cid;
   switch ($op) {
+    case 'validate':
+      break;
+
+    case 'insert':
+    case 'update':
+      $node = node_load($comment['nid']);
+      if (user_access('upload files') && variable_get('comment_upload_'. $node->type, 1)) {
+        _comment_upload_save_files($comment);
+      }
+      break;
+
+    case 'delete':
+      _comment_upload_delete($cid);
+      break;
+
+    case 'view':
+      if (!user_access('view uploaded files')) {
+        break;
+      }
+      if (!isset($comment->files)) {
+        $comment->files = _comment_upload_load_files($cid, $comment->nid);
+      }
+      elseif (is_array($comment->files) && variable_get('comment_upload_single', 0)) {
+        // Simulate overwrite for preview
+        foreach ($comment->files as $file) {
+          if (strpos($file['fid'], 'upload') !== false) {
+            unset($comment->files[0]);
+            break;
+          }
+        }
+      }
+      if ($comment->files) {
+        $comment->comment .= theme('comment_attachments', $comment->files);
+      }
+      break;
+  }
+}
 
-    case 'form':
-      $node = node_load($comment['nid']['#value']);
+/**
+ * Hook into node type and upload settings forms.
+ */
+function comment_upload_form_alter(&$form, $form_state, $form_id) {
+  if ($form_id == 'comment_form') {
+    $cid = is_object($form) ? $form->cid : $form['cid'];
+      $cid = is_array($cid) ? $cid['#value'] : $cid;
+      $node = node_load($form['nid']['#value']);
       if (!user_access('upload files') || !variable_get('comment_upload_'. $node->type, 1)) {
         break;
       }
       $cobj->cid = $cid;
       $cobj->files = _comment_upload_load_files($cid);
-      _upload_prepare($cobj);
-      $form = array('#attributes' => array('enctype' => 'multipart/form-data'));
+
       if (variable_get('comment_upload_single', 0)) {
         $form['upload'] = array(
         '#type' => 'file',
@@ -27,12 +68,11 @@
         );
       }
       else {
-        drupal_add_js('misc/progress.js');
-        drupal_add_js('misc/upload.js');
-
+        
         // Attachments fieldset
         $form['attachments'] = array(
           '#type' => 'fieldset',
+	  '#access' => user_access('upload_files'),
           '#title' => t('File attachments'),
           '#collapsible' => TRUE,
           '#collapsed' => empty($cobj->files),
@@ -42,67 +82,28 @@
           '#weight' => 10,
         );
 
-        // Wrapper for fieldset contents (used by upload JS).
+        // Wrapper for fieldset contents (used by ahah JS).
         $form['attachments']['wrapper'] = array(
           '#prefix' => '<div id="attach-wrapper">',
           '#suffix' => '</div>',
         );
-        $form['attachments']['wrapper'] += _upload_form($cobj);
+	  $form['attachments']['wrapper'] += _upload_form($cobj);
+	  $form['#attributes']['enctype'] = 'multipart/form-data';
+
 
         // Enable the upload_preview module (when enabled) to modify the attachment display.
         if (module_exists('upload_preview')) {
           _upload_preview_node_form($form['attachments']['wrapper']['files'], 0);
         }
 
-        $form['attachments']['wrapper']['attach-url']['#value'] = url('comment_upload/js', NULL, NULL, TRUE);
+
+        $form['attachments']['wrapper']['new']['attach']['#ahah']['path'] = 'comment_upload/js';
         $form['current']['cid'] = array('#type' => 'hidden', '#value' => $cid);
         unset($form['attachments']['wrapper']['current']['vid']);
       }
-      return $form;
-
-    case 'validate':
-      _upload_validate($comment);
-      break;
-
-    case 'insert':
-    case 'update':
-      $node = node_load($comment['nid']);
-      if (user_access('upload files') && variable_get('comment_upload_'. $node->type, 1)) {
-        _comment_upload_save_files($comment);
-      }
-      break;
-
-    case 'delete':
-      _comment_upload_delete($cid);
-      break;
-
-    case 'view':
-      if (!user_access('view uploaded files')) {
-        break;
-      }
-      if (!isset($comment->files)) {
-        $comment->files = _comment_upload_load_files($cid, $comment->nid);
-      }
-      elseif (is_array($comment->files) && variable_get('comment_upload_single', 0)) {
-        // Simulate overwrite for preview
-        foreach ($comment->files as $file) {
-          if (strpos($file['fid'], 'upload') !== false) {
-            unset($comment->files[0]);
-            break;
-          }
-        }
-      }
-      if ($comment->files) {
-        $comment->comment .= theme('comment_attachments', $comment->files);
-      }
-      break;
+      $form['#submit'][] = 'upload_node_form_submit';
   }
-}
 
-/**
- * Hook into node type and upload settings forms.
- */
-function comment_upload_form_alter($form_id, &$form) {
   if ($form_id == 'node_type_form') {
     $form['workflow']['comment_upload'] = array(
       '#type' => 'radios',
@@ -129,16 +130,13 @@
   }
 }
 
-function comment_upload_menu($may_cache) {
+function comment_upload_menu() {
   $items = array();
-  if ($may_cache) {
-    $items[] = array(
-      'path' => 'comment_upload/js',
-      'callback' => 'comment_upload_js',
-      'access' => user_access('upload files'),
-      'type' => MENU_CALLBACK
-    );
-  }
+  $items['comment_upload/js'] = array(
+    'page callback' => 'comment_upload_js',
+    'access arguments' => array('upload files'),
+    'type' => MENU_CALLBACK
+  );
   return $items;
 }
 
@@ -148,29 +146,54 @@
 function comment_upload_js() {
   // We only do the upload.module part of the node validation process.
   $comment = (object)$_POST;
+  
+  $form_state = array();
 
   // Load existing files.
-  $comment->files = _comment_upload_load_files($comment->cid);
 
   // Handle new uploads, and merge tmp files into node-files.
-  _upload_prepare($comment);
-  _upload_validate($comment);
+  upload_node_form_submit(array(), $form_state);
 
-  $form = _upload_form($comment);
 
+  /*  if(isset($comment->cid))
+   $comment->files = _comment_upload_load_files($comment->cid);*/
+  
+
+  if (!empty($form_state['values']['files'])) {
+    foreach ($form_state['values']['files'] as $fid => $file) {
+      $comment->files[$fid] = $file;
+    }
+  }
+
+  $form = _upload_form($comment);
+    $form += array(
+    '#post' => $_POST,
+    '#programmed' => FALSE,
+    '#tree' => FALSE,
+    '#parents' => array(),
+  );
+    drupal_alter('form', $form, array(), 'upload_js');
+    $form_state = array('submitted' => FALSE);
+    
   // Swap upload/js for the comment_upload callback.
-  $form['attach-url']['#value'] = url('comment_upload/js', NULL, NULL, TRUE);
+  
+    $form['new']['attach']['#ahah']['path'] = 'comment_upload/js';
+  
+    $form = form_builder('upload_js', $form, $form_state);
+
 
-  foreach (module_implements('form_alter') as $module) {
-    $function = $module .'_form_alter';
-    $function('upload_js', $form);
-  }
-  $form = form_builder('upload_js', $form);
-  $output = theme('status_messages') . drupal_render($form);
-
-  // We send the updated file attachments form.
-  print drupal_to_js(array('status' => TRUE, 'data' => $output));
-  exit;
+    $files = isset($_POST['files']) ? $_POST['files'] : array();
+    foreach ($files as $fid => $file) {
+      if (is_numeric($fid)) {
+	$form['files'][$fid]['list']['#value'] = isset($file['list']) ? 1 : 0;
+	$form['files'][$fid]['remove']['#value'] = isset($file['remove']) ? 1 : 0;
+      }
+    }
+    $output = theme('status_messages') . drupal_render($form);
+    
+    // We send the updated file attachments form.
+    print drupal_to_js(array('status' => TRUE, 'data' => $output));
+    exit;
 }
 
 /**
@@ -205,7 +228,7 @@
 }
 
 function _comment_upload_save_files($comment) {
-  if (!is_array($comment['files'])) {
+  if (empty($comment['files']) ||  !is_array($comment['files'])) {
     return;
   }
 
@@ -280,6 +303,17 @@
 }
 
 /**
+ * Implementation of hook_theme().
+ */
+function comment_upload_theme() {
+  return array(
+    'comment_attachments' => array(
+      'arguments' => array('files' => NULL)
+    )
+  );
+}
+
+/**
  * Style the attachment display.
  *
  * Images are displayed inline when Inline display has been set.
