Index: src/perl/register-dataset.cgi
===================================================================
--- src/perl/register-dataset.cgi	(revision 4743)
+++ src/perl/register-dataset.cgi	(revision 4744)
@@ -432,10 +432,14 @@
                 debug("NO SUCCESS");
                 debug("Message is: $docStatus");
                 push(@errorMessages, $docStatus);
+            } else {
+                deleteRemovedData();
             }
 
             debug("B3");
         } else {
+            debug("M1");
+
             # document is being modified
             $docid = incrementRevision($FORM::docid);
 
@@ -447,10 +451,11 @@
             if (! $response) {
                 push(@errorMessages, $metacat->getMessage());
                 push(@errorMessages, "Failed while updating.\n");  
-            }
-
+            } 
+                     
+            debug("M2, $docid");
             if (scalar(@errorMessages)) {
-                debug("ErrorMessages defined in modify.");
+                debug("Errors defined in modify.");
 
                 $$templateVars{'docid'} = $FORM::docid;
                 copyFormToTemplateVars();
@@ -458,6 +463,7 @@
                 $$templateVars{'errorMessages'} = \@errorMessages;
                 $error = 1;
             } else {
+                deleteRemovedData();
                 $$templateVars{'docid'} = $docid;
                 $$templateVars{'cfg'} = $skinName;
             }
@@ -884,9 +890,14 @@
     for (my $fileNum = 0; $fileNum <= $FORM::upCount; $fileNum++) {
         my $fn = 'upload_' . $fileNum;
         if (hasContent(param($fn))) {
-            debug("Retrieving metadata for file: $fn");
-            ($docid, $fileInfo) = fileMetadata($fileNum);
-            $uploadedFiles{ $docid } = $fileInfo;
+            # ignore data which is scheduled for deletion
+            if (grep {$_ eq ("uploadname_$fileNum")} @FORM::deletefile) {
+                debug("Not generating metadata for file scheduled for deletion: $fn");
+            } else {
+                debug("Retrieving metadata for file: $fn");
+                ($docid, $fileInfo) = fileMetadata($fileNum);
+                $uploadedFiles{ $docid } = $fileInfo;
+            }
         }
     }
 
@@ -905,9 +916,7 @@
 
     # process an _existing_ data file, which is already within Metacat.
     if ($fileHash =~ /ondisk/) {
-        my $finfo = $fileHash;
-        $finfo =~ s/ondisk://g;
-        ($docid, $fileHash) = split(":", $finfo);
+        ($docid, $fileHash) = datafileInfo($fileHash);
         $outFile = $dataDir . "/" . $docid;
     } else {
         # normalize input filenames; Windows filenames include full paths
@@ -960,6 +969,11 @@
     return ($docid, \%dataInfo);
 }
 
+sub datafileInfo {
+    my $finfo = shift;
+    $finfo =~ s/ondisk://g;
+    return my ($docid, $fileHash) = split (":", $finfo);
+}
 
 sub processFile {
     my $fileName = shift;
@@ -1025,34 +1039,65 @@
     return ($outputName, $digest);
 }
 
+sub deleteRemovedData {
+    # if we have any associated datafiles which are scheduled for deletion, remove them now
+    for (my $delNum = 0; $delNum <= $FORM::delCount; $delNum++) {
+        # need to look up the actual upload number, which is contained in the name
+        my $upNum = param("deletefile_$delNum");
+        $upNum =~ s/uploadname_//;
+        my $upn = param("upload_$upNum");
+        if (hasContent($upn)) {
+            debug("Deleting upload_$upNum, $upn");
+            if (grep {$_ eq ("uploadname_$upNum")} @FORM::deletefile) {
+                if (param("upload_$upNum") =~ /ondisk/) {
+                    debug("got a file which is ondisk, proceeding with deletion");
+                    deleteFileData(param("upload_$upNum"));
+                } else {
+                    debug("got an old reference, not yet in EML, remove from tempdir");
+                    deleteFile(param("uploadname_$upNum"));
+                }
+            } else {
+                debug("Name didn't match in deletefile list");
+            }
+        }
+    }
+}
+
 sub deleteFile {
     my $input = shift;
-    
-    if ($input =~ /ondisk/) { 
-        my $docid = $input;
-        $docid =~ s/ondisk://g;
-        # XXX FIXME once ondisk support is enabled
-        #my $response = $metacat->delete($docid);
-        my $response;
+    my $fileName = $tempDir . "/" . $input;
+
+    if (-e $fileName) {
+        unlink $fileName or
+            debug("Failed to delete file $fileName.");
+    } else {
+        debug("Unable to find file $fileName");
+    }
+    if (!-e $fileName) {
+        debug("Successfully deleted $fileName");
+    }
+}
+
+sub deleteFileData {
+    my $input = shift;
+    my ($docid, $fileHash) = datafileInfo($input);
+    my $metacat = Metacat->new($metacatUrl);
+
+    my ($username, $password) = getCredentials();
+    my $response = $metacat->login($username, $password);
+    if (!$response) {
+        my $msg = $metacat->getMessage();
+        push(@errorMessages, "Failed to login with credentials for `$username`. Error was $msg");
+        debug("Failed to login with given credentials for username $username, Error is: $msg");
+    } else {
+        $response = $metacat->delete($docid);
         if (!$response) {
             my $msg = $metacat->getMessage();
-            push(@errorMessages, "Failed to delete existing file. Error was: $msg\n");
+            push(@errorMessages, "Failed to delete existing file. Error was: $msg");
             debug("Delete -- Error is: $msg");
         } else {
             debug("Delete -- Success! Removed docid $docid");
         }
-    } else {        
-        my $fileName = $tempDir . "/" . $input;
-
-        if (-e $fileName) {
-            unlink $fileName or
-                debug("Failed to delete file $fileName.");
-        } else {
-            debug("Unable to find file $fileName");
-        }
-        if (!-e $fileName) {
-            debug("Successfully deleted $fileName");
-        }
     }
 }
 
@@ -1933,7 +1978,7 @@
     my $node;
     my $tempResult;
     my $tempNode;
-    my $aoCount = 1;
+    my $aoCount = 0;
     my $foundDSO;
 
     # set variable values
@@ -2239,7 +2284,7 @@
             my $temp = findValue($tempNode, "mediumName");
             if (substr($temp, 0, 5) eq "other") {
                 $$templateVars{'dataMedium'} = substr($temp, 0, 5);
-                $$templateVars{'dataMediumOther'} = substr($temp, 5);
+                $$templateVars{'dataMediumOther'} = substr($temp, 6);
             } else {
                 $$templateVars{'dataMedium'} = $temp;
             }
@@ -2838,12 +2883,10 @@
     my $userPass = $FORM::password;
     my $dname = "uid=$userDN,o=$userOrg,dc=ecoinformatics,dc=org";
 
-    if ($FORM::userLoggedIn eq 'true') {
-        my $session = CGI::Session->load();
-        if (!($session->is_empty || $session->is_expired)) {
-            $dname = $session->param("username");
-            $userPass = $session->param("password");
-        }
+    my $session = CGI::Session->load();
+    if (!($session->is_empty || $session->is_expired)) {
+        $dname = $session->param("username");
+        $userPass = $session->param("password");
     }
 
     return ($dname, $userPass);
@@ -3016,7 +3059,6 @@
             }
             
             $emldoc .= EMLStart();
-            # TODO SCW: make sure this replacement works as expected
             $emldoc .= accessElement("true");
             $emldoc .= $dataElem;
             $emldoc .= EMLEnd();
@@ -3026,7 +3068,7 @@
             my $newDocid = incrementRevision($docid);
 
             $emldoc =~ s/packageId=\"$docid\"/packageId=\"$newDocid\"/;
-            $response = $metacat->update($newDocid, $emldoc);
+            $response = $metacat->update($docid, $emldoc);
 
             if (! $response) {
                 debug("Error while updating in handleModAccept.");
@@ -3741,19 +3783,22 @@
     $$templateVars{'taxaCount'} = $taxonCount-1;
     $$templateVars{'taxaAuth'} = normalizeCD($FORM::taxaAuth);
     my $deleteCount = 0;
+
+    for (my $i = 0; $i <= scalar(@FORM::deletefile); $i++) {
+        my $delfile = pop(@FORM::deletefile);
+        $$templateVars{"deletefile_$deleteCount"} = $delfile;
+        debug(" creating deletefile_$deleteCount = $delfile"); 
+        $deleteCount++;
+    }
+    $$templateVars{"delCount"} = $deleteCount;
     for (my $upNum = 0; $upNum <= $FORM::upCount; $upNum++) {
         my $upn = "upload_$upNum";
         if (hasContent(param($upn))) {
-            if (grep {$_ eq ("uploadname_$upNum")} @FORM::deletefile) {
-                deleteFile(param("uploadname_$upNum"));
-                $deleteCount++;
-            } else {
-                debug("Processing existing file: $upn");
-                $$templateVars{"upload_$upNum"} = param("upload_$upNum");
-                $$templateVars{"uploadname_$upNum"} = param("uploadname_$upNum");
-                $$templateVars{"uploadtype_$upNum"} = param("uploadtype_$upNum");
-                $$templateVars{"uploadperm_$upNum"} = param("uploadperm_$upNum");
-            }
+            debug("Processing existing file: $upn");
+            $$templateVars{"upload_$upNum"} = param("upload_$upNum");
+            $$templateVars{"uploadname_$upNum"} = param("uploadname_$upNum");
+            $$templateVars{"uploadtype_$upNum"} = param("uploadtype_$upNum");
+            $$templateVars{"uploadperm_$upNum"} = param("uploadperm_$upNum");
         }
     }
 
@@ -3763,8 +3808,7 @@
         if (hasContent(param($fn))) {
             my $fileName = eval "\$FORM::file_$fileNum";
             debug("Processing file: $fn");
-            # Upload the file object itself to a temporary file, copy file
-            # metadata to form
+            # Upload the file object itself to a temporary file, copy file metadata to form
             my $fileInfo = processFile($fileName);
             # if we have existing uploads, increment the counter to avoid overwriting
             my $totalFileNum = $fileNum + $FORM::upCount;
@@ -3776,7 +3820,7 @@
         }
     }
     # total uploads are: new uploads - deleted files + original uploads
-    $$templateVars{'upCount'} = $uploadCount - $deleteCount + $FORM::upCount;
+    $$templateVars{'upCount'} = $uploadCount + $FORM::upCount;
     $$templateVars{'methodTitle'} = normalizeCD($FORM::methodTitle);
 
     my @tempMethodPara;
@@ -3787,12 +3831,10 @@
     $$templateVars{'studyExtentDescription'} = normalizeCD($FORM::studyExtentDescription);
     $$templateVars{'samplingDescription'} = normalizeCD($FORM::samplingDescription);
     $$templateVars{'origStateContact'} = $FORM::origState;
-
     $$templateVars{'modules'} = $modules;
     $$templateVars{'required'} = $required;
     $$templateVars{'show'} = $show;
     $$templateVars{'site'} = $FORM::site;
-
     $$templateVars{'docid'} = $FORM::docid;
 
     # Check if the session exists
@@ -3800,23 +3842,25 @@
     if (!($session->is_empty || $session->is_expired)) {
         $$templateVars{'userLoggedIn'} = 'true';
     }
+    
+    # Errors from validation function. print the errors out using the response template
+    if (scalar(@errorMessages)) {
+        debug("Error messages found when confirming data.");
+        $$templateVars{'status'} = 'failure';
+        $$templateVars{'errorMessages'} = \@errorMessages;
+        $error = 1;
+    }
 
-    if (! $error) {
+    if (!$error) {
         # If no errors, then print out data in confirm Data template
         $$templateVars{'section'} = "Confirm Data";
         $template->process($templates->{'confirmData'}, $templateVars);
 
     } else {    
-    # Errors from validation function. print the errors out using the response template
-    if (scalar(@errorMessages)) {
-        $$templateVars{'status'} = 'failure';
-        $$templateVars{'errorMessages'} = \@errorMessages;
-        $error = 1;
-    }
         # Create our HTML response and send it back
-    $$templateVars{'function'} = "submitted";
-    $$templateVars{'section'} = "Submission Status";
-    $template->process($templates->{'response'}, $templateVars);
+        $$templateVars{'function'} = "submitted";
+        $$templateVars{'section'} = "Submission Status";
+        $template->process($templates->{'response'}, $templateVars);
     }
 }
 
