Index: Dev/branches/rest-dojo-ui/client/db/db.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/db/db.js	(revision 328)
+++ Dev/branches/rest-dojo-ui/client/db/db.js	(revision 328)
@@ -0,0 +1,119 @@
+require([
+         'dojo/_base/json',
+         'dojo/_base/lang',
+         'dojo/_base/xhr',
+         'dojo/parser',
+         'dojo/query',
+         'dijit/registry',
+         'dojox/io/xhrPlugins',
+         'dojo/text!./docs/rft/_security.json',
+         'dojo/text!./docs/_users/rft_admin.json',
+         'dojo/text!./docs/rft/_design/default.json',
+         'dojo/domReady!',
+         'dijit/form/TextBox',
+         'dijit/form/Button',
+         'dijit/form/CheckBox',
+         ],
+    function(json,lang,xhr,parser,query,registry,xhrPlugins,
+        securityDoc,rft_adminDoc,design_defaultDoc){
+    parser.parse();
+
+    var logNode;
+    query("#log").forEach(function(n){ logNode = n; });
+
+    function log(text,overwrite) {
+        if ( overwrite ) logNode.innerHTML = text
+        else logNode.innerHTML = logNode.innerHTML + '\n' + text;
+    }
+
+    var urlInput = registry.byId('url');
+    var usernameInput = registry.byId('username');
+    var passwordInput = registry.byId('password');
+    var resetInput = registry.byId('reset');
+
+    urlInput.set('value','http://localhost/couchdb');
+    usernameInput.set('value','rft_admin');
+    passwordInput.set('value','Welkom01');
+
+    log("Give CouchDB admin username & password and click 'Configure' to start.\nIf the database already exists, rft_admin password will suffice.",true);
+
+    registry.byId('configure').on('click',function(){
+        log("Configuring CouchDB for RFT:",true);
+
+        var baseUrl = urlInput.get('value');
+        var username = usernameInput.get('value');
+        var password = passwordInput.get('value');
+        var reset = resetInput.get('value');
+
+        function req(method,url,body) {
+            args = {
+                url: baseUrl+url,
+                contentType: 'application/json',
+                handleAs: 'json',
+                sync: true,
+                error: function(err) {
+                    log("ERROR: "+err);
+                }
+            };
+            if ( !body || lang.isObject(body) ) {
+                body = json.toJson(body || {});
+            }
+            args.rawBody = body;
+            if ( username ) {
+                args.user = username;
+                args.password = password;
+            }
+            return xhr(method,args,true);
+        }
+
+        log("Checking CouchDB version");
+        req('GET','/')
+        .then(function(res){
+            if (res.version !== "1.2.0" ) {
+                log("Found "+res.version+", only tested with CouchDB 1.2.0")
+            } else {
+                log("CouchDB 1.2.0 found")
+            }
+        });
+
+
+        var exists = false;
+        log("Checking database 'rft'");
+        req('GET','/rft')
+        .then(function(res){
+            log("Database 'rft' found.\nLeaving security and users as they are.")
+            exists = true;
+        });
+        if ( exists && reset ) {
+            req('DELETE','/rft');
+            exists = false;
+        }
+        if ( !exists ) {
+            log("Creating database 'rft'")
+            req('PUT','/rft')
+            
+            log("Setting _security on database 'rft'");
+            req('PUT','/rft/_security', securityDoc);
+
+            log("Checking 'rft_admin' user");
+            req('GET','/_users/org.couchdb.user:rft_admin')
+            .then(function(){
+                log("User 'rft_admin' already exists.");
+            }, function() {
+                log("Creating user 'rft_admin' with password 'Welkom01'");
+                req('PUT','/_users/org.couchdb.user:rft_admin', rft_adminDoc);
+            });
+        };
+
+        log("Updating design documents");
+        newDoc = json.fromJson(design_defaultDoc);
+        req('GET','/rft/_design/default')
+        .then(function(doc){
+            newDoc['_rev'] = doc['_rev'];
+        });
+        req('PUT','/rft/_design/default', newDoc);
+
+        log("Done!");
+    });
+
+});
Index: Dev/branches/rest-dojo-ui/client/db/docs/_users/rft_admin.json
===================================================================
--- Dev/branches/rest-dojo-ui/client/db/docs/_users/rft_admin.json	(revision 328)
+++ Dev/branches/rest-dojo-ui/client/db/docs/_users/rft_admin.json	(revision 328)
@@ -0,0 +1,7 @@
+{
+    "_id": "org.couchdb.user:rft_admin",
+    "name": "rft_admin",
+    "password": "Welkom01",
+    "roles": [ "rft_admin" ],
+    "type": "user"
+}
Index: Dev/branches/rest-dojo-ui/client/db/docs/rft/_design/default.json
===================================================================
--- Dev/branches/rest-dojo-ui/client/db/docs/rft/_design/default.json	(revision 328)
+++ Dev/branches/rest-dojo-ui/client/db/docs/rft/_design/default.json	(revision 328)
@@ -0,0 +1,10 @@
+{
+    "_id": "_design/default",
+    "language": "javascript",
+    "validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) { if (oldDoc && oldDoc.published) { throw({forbidden: 'Published documents cannot be modified.'}); } }",
+    "views": {
+        "unpublished": {
+            "map": "function(doc) { if ( doc.type == 'Survey' && !doc.published ) { emit(doc._id, doc); } }"
+        }
+    }
+}
Index: Dev/branches/rest-dojo-ui/client/db/docs/rft/_security.json
===================================================================
--- Dev/branches/rest-dojo-ui/client/db/docs/rft/_security.json	(revision 328)
+++ Dev/branches/rest-dojo-ui/client/db/docs/rft/_security.json	(revision 328)
@@ -0,0 +1,10 @@
+{
+    "admins" : {
+        "names" : [],
+        "roles" : ["rft_admin"]
+    },
+    "readers" : {
+        "names" : [],
+        "roles" : ["rft_user"]
+    }
+}
Index: Dev/branches/rest-dojo-ui/client/db/index.html
===================================================================
--- Dev/branches/rest-dojo-ui/client/db/index.html	(revision 328)
+++ Dev/branches/rest-dojo-ui/client/db/index.html	(revision 328)
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+        <title>Research Facilitator Tool - CouchDB Configure</title>
+        <script type="text/javascript" src="../dojotoolkit/dojo/dojo.js" data-dojo-config="async: true, parseOnLoad: false, isDebug: true"></script>
+        <script type="text/javascript" src="db.js"></script>
+        <link rel="stylesheet" type="text/css" href="../dojotoolkit/dijit/themes/claro/claro.css" />
+        <style>
+            label {
+                width: 200px;
+                display: inline-block;
+            }
+        </style>
+    </head>
+    <body class="claro">
+        <h1>Configure CouchDB</h1>
+        <div>
+            <label for="url">CouchDB URL</label><input type="text" id="url" data-dojo-type="dijit.form.TextBox"/><br/>
+            <label for="username">Admin username</label><input type="text" id="username" data-dojo-type="dijit.form.TextBox"/><br/>
+            <label for="password">Admin password</label><input type="password" id="password" data-dojo-type="dijit.form.TextBox"/><br/>
+            <label for="reset">Full reset</label><input type="checkbox" id="reset" data-dojo-type="dijit.form.CheckBox"/><br/>
+            <button id="configure" data-dojo-type="dijit.form.Button">Configure</button><br/>
+        </div>
+        <pre id="log">Waiting for configurator to initialize...</pre>
+    </body>
+</html>
