Index: Dev/trunk/src/client/qed-client/index.js
===================================================================
--- Dev/trunk/src/client/qed-client/index.js	(revision 465)
+++ Dev/trunk/src/client/qed-client/index.js	(revision 466)
@@ -4,4 +4,5 @@
     "./app/Router",
     "./routes",
+    "./session",
     "./store",
     "./store/request",
@@ -9,24 +10,18 @@
     "dojo/_base/array",
     "dojo/parser",
-    "dojo/request/registry",
     "./stddeps",
     "dojo/domReady!"
-], function(Content, Page, Router, routes, store, request, MainMenu, array, parser, registry) {
+], function(Content, Page, Router, routes, session, store, request, MainMenu, array, parser) {
 
-    registry.register(/.*/,request);
+    parser.parse();
+
+    new MainMenu({},"menu").startup();
+
+    array.forEach(routes,function(route){
+        Router.register(route);
+    });
+    Router.startup();
+
+    session.restore();
     
-    parser.parse();
-    store.info()
-    .then(function(){
-        new MainMenu({},"menu").startup();
-        array.forEach(routes,function(route){
-            Router.register(route);
-        });
-        Router.startup();
-    },function(){
-        Content.startup();
-        Content.set(new Page({
-            templateString: "<div>Application database is not properly configured. Contact your system administrator.</div>"
-        }));
-    });
 });
Index: Dev/trunk/src/client/qed-client/pages/surveyRun.js
===================================================================
--- Dev/trunk/src/client/qed-client/pages/surveyRun.js	(revision 465)
+++ Dev/trunk/src/client/qed-client/pages/surveyRun.js	(revision 466)
@@ -86,5 +86,5 @@
         },
         _link: function(url,label) {
-            return '<a target="_black" href="'+url+'">'+(label || url)+'</a>';
+            return '<a target="_blank" href="'+url+'">'+(label || url)+'</a>';
         },
         _onSave: function(evt) {
@@ -111,8 +111,4 @@
             if ( evt ) { event.stop(evt); }
             return false;
-        },
-        _onExport: function(evt) {
-            if ( evt ) { event.stop(evt); }
-            return false;
         }
     });
Index: Dev/trunk/src/client/qed-client/pages/templates/question.html
===================================================================
--- Dev/trunk/src/client/qed-client/pages/templates/question.html	(revision 465)
+++ Dev/trunk/src/client/qed-client/pages/templates/question.html	(revision 466)
@@ -1,3 +1,3 @@
-<div class="orange">
+<form class="orange">
     <div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'top'">
         <h2>
@@ -15,3 +15,3 @@
         </div>
     </div>
-</div>
+</form>
Index: Dev/trunk/src/client/qed-client/pages/templates/surveyRun.html
===================================================================
--- Dev/trunk/src/client/qed-client/pages/templates/surveyRun.html	(revision 465)
+++ Dev/trunk/src/client/qed-client/pages/templates/surveyRun.html	(revision 466)
@@ -45,8 +45,5 @@
             <div>
                 <div class="qedLabel">Export results</div>
-                <button data-dojo-type="dijit/form/Button"
-                        class="qedField blue"
-                        data-dojo-props="baseClass: 'rftBlockButton', iconClass: 'rftIcon rftIconSave'"
-                        data-dojo-attach-event="onClick:_onExport">To CSV</button>
+                <a target="_blank" href="/surveyRun/${surveyRunId}/csv" class="qedField">To CSV</a>
             </div>
         </fieldset>
Index: Dev/trunk/src/client/qed-client/session.coffee
===================================================================
--- Dev/trunk/src/client/qed-client/session.coffee	(revision 466)
+++ Dev/trunk/src/client/qed-client/session.coffee	(revision 466)
@@ -0,0 +1,44 @@
+define [
+    "dojo/_base/declare",
+    "dojo/_base/json",
+    "dojo/Evented",
+    "dojo/request"
+], (declare, json, Evented, request) ->
+    Session = declare [Evented],
+        info: null
+        get: () ->
+            @info
+        restore: () ->
+            request '/api/login',
+                method: "GET"
+                handleAs: "json"
+            .then (res) =>
+                @_set res
+            , () =>
+                @_set null
+        login: (username, password) ->
+            request '/api/login',
+                method: "POST"
+                handleAs: "json"
+                data:
+                    username: username
+                    password: password
+            .then (res) =>
+                @_set res
+            , () =>
+                @_set null
+        logout: () ->
+            request '/api/logout',
+                method: "POST"
+                handleAs: "json"
+            .then ((res) =>
+                console.log "Logged out"
+                @_set null), () =>
+                console.log "Error logging out"
+        
+        _set: (newInfo) ->
+            @info = newInfo
+            @emit 'change', @info
+            @info
+
+    new Session()
Index: Dev/trunk/src/client/qed-client/stddeps.js
===================================================================
--- Dev/trunk/src/client/qed-client/stddeps.js	(revision 465)
+++ Dev/trunk/src/client/qed-client/stddeps.js	(revision 466)
@@ -33,7 +33,9 @@
     './model/widgets/SurveyWidget',
 
+    './ui/LoginDialog',
     './ui/MainMenu',
     './ui/MenuBarLink',
     './ui/MenuLink',
+    './ui/SessionMenu',
 
     './widgets/LineWithActionsWidget',
Index: Dev/trunk/src/client/qed-client/store.js
===================================================================
--- Dev/trunk/src/client/qed-client/store.js	(revision 465)
+++ Dev/trunk/src/client/qed-client/store.js	(revision 466)
@@ -9,5 +9,5 @@
     
     var couchStore = new CouchStore({
-        target: 'data/couch/' /*,
+        target: 'api/data/' /*,
         validate: function(object) {
             var result = jsonSchema.validate(object,schema);
Index: Dev/trunk/src/client/qed-client/store/CouchStore.js
===================================================================
--- Dev/trunk/src/client/qed-client/store/CouchStore.js	(revision 465)
+++ Dev/trunk/src/client/qed-client/store/CouchStore.js	(revision 466)
@@ -5,5 +5,5 @@
     "dojo/_base/json",
     "dojo/_base/lang",
-    "dojo/request/registry",
+    "dojo/request",
     "dojo/store/util/QueryResults"
 ], function(Deferred, array, declare, json, lang, request, QueryResults) {
Index: Dev/trunk/src/client/qed-client/ui/LoginDialog.coffee
===================================================================
--- Dev/trunk/src/client/qed-client/ui/LoginDialog.coffee	(revision 466)
+++ Dev/trunk/src/client/qed-client/ui/LoginDialog.coffee	(revision 466)
@@ -0,0 +1,32 @@
+define [
+    "../session",
+    "dijit/_WidgetsInTemplateMixin",
+    "dijit/Dialog",
+    "dijit/form/ValidationTextBox",
+    "dojo/_base/declare",
+    "dojo/_base/event",
+    "dojo/dom-construct",
+    "dojo/text!./templates/LoginDialog.html"
+], (session, _WidgetsInTemplateMixin, Dialog,
+    ValidationTextBox, declare, event,
+    domConstruct, template) ->
+    declare [Dialog,_WidgetsInTemplateMixin],
+        templateString: template
+        title: "Login to QED"
+        show: () ->
+            @loginForm.reset()
+            @inherited arguments
+        onLogin: (evt) ->
+            if @loginForm.validate
+                value = @loginForm.get 'value'
+                session.login value.username,
+                              value.password
+                .then (() => @hide()),
+                      () => alert "Login failed!"
+            @hide()
+            event.stop evt if evt
+            false
+        onCancel: (evt) ->
+            @hide()
+            event.stop evt if evt
+            false
Index: Dev/trunk/src/client/qed-client/ui/MenuLink.js
===================================================================
--- Dev/trunk/src/client/qed-client/ui/MenuLink.js	(revision 465)
+++ Dev/trunk/src/client/qed-client/ui/MenuLink.js	(revision 466)
@@ -1,10 +1,16 @@
-define(['dojo/_base/declare','dijit/MenuItem','../app/Router'
-], function(declare,MenuItem,Router) {
+define([
+    "../app/Router",
+    "dijit/MenuItem",
+    "dojo/_base/declare",
+    "dojo/_base/event"
+], function(Router, MenuItem, declare, event) {
     return declare([MenuItem],{
         options:{
             path: null
         },
-        onClick: function() {
+        onClick: function(evt) {
             if ( this.path ) { Router.go(this.path); }
+            if ( evt ) { event.stop(evt); }
+            return false;
         }
     });
Index: Dev/trunk/src/client/qed-client/ui/SessionMenu.coffee
===================================================================
--- Dev/trunk/src/client/qed-client/ui/SessionMenu.coffee	(revision 466)
+++ Dev/trunk/src/client/qed-client/ui/SessionMenu.coffee	(revision 466)
@@ -0,0 +1,33 @@
+define [
+    "../session",
+    "dijit/DropDownMenu",
+    "dijit/registry",
+    "dijit/MenuBarItem",
+    "dojo/_base/declare",
+    "dojo/_base/event",
+    "dojo/on"
+], (session, DropDownMenu, registry, MenuBarItem, declare, event, _on) ->
+    declare [MenuBarItem],
+        info: null
+        postCreate: () ->
+            @loginDialog = registry.byId 'loginDialog'
+            console.error "Cannot find loginDialog" if not @loginDialog
+            @set 'label', "Login"
+        startup: () ->
+            if @_started then return
+            @inherited arguments
+            @_update session.get()
+            _on session, 'change', (info) => @_update info
+        _update: (info) ->
+            @info = info
+            if info
+                @set 'label', "Logout, "+info.username
+            else
+                @set 'label', "Login"
+        onClick: (evt) ->
+            if @info
+                session.logout()
+            else
+                @loginDialog.show()
+            event.stop evt if evt
+            false
Index: Dev/trunk/src/client/qed-client/ui/templates/LoginDialog.html
===================================================================
--- Dev/trunk/src/client/qed-client/ui/templates/LoginDialog.html	(revision 466)
+++ Dev/trunk/src/client/qed-client/ui/templates/LoginDialog.html	(revision 466)
@@ -0,0 +1,51 @@
+<div class="dijitDialog"
+     role="dialog"
+     aria-labelledby="${id}_title">
+    <div data-dojo-attach-point="titleBar"
+         class="dijitDialogTitleBar">
+        <span data-dojo-attach-point="titleNode"
+              class="dijitDialogTitle"
+              id="${id}_title" role="heading" level="1"></span>
+        <span data-dojo-attach-point="closeButtonNode"
+              class="dijitDialogCloseIcon"
+              data-dojo-attach-event="ondijitclick: onCancel"
+              title="${buttonCancel}" role="button" tabindex="0">
+            <span data-dojo-attach-point="closeText"
+                  class="closeText" title="${buttonCancel}">x</span>
+        </span>
+    </div>
+    <div class="dijitDialogPaneContent">
+        <form data-dojo-type="dijit/form/Form"
+              data-dojo-attach-point="loginForm"
+              data-dojo-attach-event="onSubmit:onLogin">
+            <div>
+              <label for="username"
+                     class="qedLabel">Username</label>
+              <div name="username"
+                   class="qedField"
+                   data-dojo-type="dijit/form/ValidationTextBox"
+                   data-dojo-props="required: true"></div>
+            </div>
+            <div>
+              <label for="password" class="qedLabel">Password</label>
+              <div name="password"
+                   class="qedField"
+                   data-dojo-type="dijit/form/ValidationTextBox"
+                   data-dojo-props="required: true"></div>
+            </div>
+            <div data-dojo-type="dijit/form/Button"
+                 type="submit"
+                 data-dojo-attach-event="onLogin">Login</div>
+            <div data-dojo-type="dijit/form/Button"
+                 data-dojo-attach-event="onCancel">Cancel</div>
+        </form>
+        <!-- Container node is required, otherwise
+             root is taken and emptied
+         -->
+        <div data-dojo-attach-point="containerNode"></div>
+    </div>
+</div>
+
+
+
+
Index: Dev/trunk/src/client/qed-client/ui/templates/MainMenu.html
===================================================================
--- Dev/trunk/src/client/qed-client/ui/templates/MainMenu.html	(revision 465)
+++ Dev/trunk/src/client/qed-client/ui/templates/MainMenu.html	(revision 466)
@@ -12,5 +12,5 @@
         </div>
         <div class="rftMainMenuButton" data-dojo-type="./MenuBarLink" data-dojo-props="path:'/results'">Results</div>
-        <div class="rftMainMenuButton" data-dojo-type="./MenuBarLink">Logout</div>
+        <div class="rftMainMenuButton" data-dojo-type="./SessionMenu"></div>
     </div>
 </div>
Index: Dev/trunk/src/client/qed-client/ui/templates/SessionBar.html
===================================================================
--- Dev/trunk/src/client/qed-client/ui/templates/SessionBar.html	(revision 465)
+++ 	(revision )
@@ -1,5 +1,0 @@
-<div class="${baseClass}">
-    <span class="rftIcon rftIconSession" data-dojo-attach-point="iconNode"></span>
-    <div data-dojo-type="../widgets/LineWithActionsWidget" data-dojo-attach-point="upperNode" data-dojo-props="actions: {'inspect':{callback: inspect, properties: {blockButton: true}}}"></div>
-    <div data-dojo-type="../widgets/LineWithActionsWidget" data-dojo-attach-point="lowerNode"></div>
-</div>
Index: Dev/trunk/src/client/qed-client/ui/templates/SessionMenu.html
===================================================================
--- Dev/trunk/src/client/qed-client/ui/templates/SessionMenu.html	(revision 466)
+++ Dev/trunk/src/client/qed-client/ui/templates/SessionMenu.html	(revision 466)
@@ -0,0 +1,6 @@
+<div>
+  <div class="rftMainMenuButton" data-dojo-type="dijit/MenuBarItem" data-dojo-props="label: 'Login'"></div>
+  <div class="rftMainMenuButton" data-dojo-type="dijit/PopupMenuBarItem" data-dojo-props="label: 'Logged in as user!'">
+    <div data-dojo-type="dijit/DropDownMenu"></div>
+  </div>
+</div>
Index: Dev/trunk/src/client/qed-client/widgets/_ComplexValueMixin.coffee
===================================================================
--- Dev/trunk/src/client/qed-client/widgets/_ComplexValueMixin.coffee	(revision 465)
+++ Dev/trunk/src/client/qed-client/widgets/_ComplexValueMixin.coffee	(revision 466)
@@ -15,9 +15,13 @@
             @inherited arguments
             if @domNode.tagName.toLowerCase() isnt "form"
-                console.warn "Not scoping a _ComplexValueMixin in a form element can cause name clashes. E.g. radio buttons might stop working correctly. It is recommended to use <form> as the root element in your template for", @declaredClass
+                console.warn "Not scoping a _ComplexValueMixin in a
+                        form element can cause name clashes. E.g.
+                        radio buttons might stop working correctly.
+                        It is recommended to use <form> as the root
+                        element in your template for", @declaredClass
 
         _setDisabledAttr: (value) ->
             @_set "disabled", value
-            array.forEach @_getDescendantFormWidgets(), (child) => 
+            array.forEach @_getDescendantFormWidgets(), (child) =>
                 child.set "disabled", value
 
