Futon: support document copying

Based on Patrick Antivackis work. Thanks!

COUCHDB-241
diff --git a/dialog/_copy_document.html b/dialog/_copy_document.html
new file mode 100644
index 0000000..fe49e81
--- /dev/null
+++ b/dialog/_copy_document.html
@@ -0,0 +1,29 @@
+<!--
+
+Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+this file except in compliance with the License.  You may obtain a copy of the
+License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software distributed
+under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+CONDITIONS OF ANY KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations under the License.
+
+-->
+<form action="" method="post">
+  <h2>Copy Document</h2>
+  <fieldset>
+    <p class="help help-copy">
+      Please enter a unique ID of the document:
+    </p>
+    <table summary="" class="help-copy"><tbody><tr>
+      <td><input type="text" name="docid" size="32"></td>
+    </tr></table>
+  </fieldset>
+  <div class="buttons">
+    <button type="submit">Copy</button>
+    <button type="button" class="cancel">Cancel</button>
+  </div>
+</form>
diff --git a/document.html b/document.html
index e041cd9..1509475 100644
--- a/document.html
+++ b/document.html
@@ -55,6 +55,7 @@
         $("#toolbar button.save").click(page.saveDocument);
         $("#toolbar button.add").click(page.addField);
         $("#toolbar button.load").click(page.uploadAttachment);
+        $("#toolbar button.copy").click(page.copyDocument);
         if (page.isNew) {
           $("#toolbar button.delete").hide();
         } else {
@@ -76,6 +77,7 @@
         <li><button class="save">Save Document</button></li>
         <li><button class="add">Add Field</button></li>
         <li><button class="load">Upload Attachment…</button></li>
+        <li><button class="copy">Copy Document…</button></li>
         <li><button class="delete">Delete Document…</button></li>
       </ul>
 
diff --git a/image/copy.png b/image/copy.png
new file mode 100644
index 0000000..07b4f79
--- /dev/null
+++ b/image/copy.png
Binary files differ
diff --git a/script/futon.browse.js b/script/futon.browse.js
index 241164c..75b1624 100644
--- a/script/futon.browse.js
+++ b/script/futon.browse.js
@@ -1094,6 +1094,37 @@
         });
       }
 
+      this.copyDocument = function() {
+        if (page.isDirty) {
+          alert("You need to save or revert any changes you have made to the " +
+                "document before you can copy it.");
+            return false;
+        }
+        $.showDialog("dialog/_copy_document.html", {
+          submit: function (data, callback) {
+            if (!data.docid || data.docid.length == 0) {
+              callback({docid: "Please enter a document ID."});
+              return;
+            }
+            db.copyDoc(
+              page.doc._id,
+              {
+                docid: data.docid
+              },
+              {
+                error: function (status, error, reason) {
+                  callback({docid: reason});
+              },
+                success: function (resp) {
+                  location.href = "document.html?"
+                                + encodeURIComponent(dbName)
+                                + "/" + $.couch.encodeDocId(resp.id);
+              }
+            });
+          }
+        });
+      };
+
       window.onbeforeunload = function() {
         if (page.isDirty) {
           return "You've made changes to this document that have not been " +
diff --git a/style/layout.css b/style/layout.css
index 54a183a..055e791 100644
--- a/style/layout.css
+++ b/style/layout.css
@@ -246,6 +246,7 @@
 #toolbar button.run { background-image: url(../image/run.png); }
 #toolbar button.save { background-image: url(../image/save.png); }
 #toolbar button.share { background-image: url(../image/compact.png); }
+#toolbar button.copy { background-image: url(../image/copy.png); }
 
 /* Dialogs */
 
@@ -282,7 +283,9 @@
   -webkit-border-bottom-right-radius: 7px;
 }
 #dialog p.help { color: #bbb; font-size: 95%; margin: 0 0 1em; }
+#dialog p.help-copy { margin-top: 1em; margin-bottom: 0; padding-left: 0.6em; }
 #dialog fieldset table { margin-top: 1em; }
+#dialog fieldset table.help-copy { margin-top: 0; }
 #dialog fieldset th, #dialog fieldset td { padding: .5em;
   vertical-align: top;
 }