Index: /main/trunk/greenstone2/macros/document.dm
===================================================================
--- /main/trunk/greenstone2/macros/document.dm	(revision 27280)
+++ /main/trunk/greenstone2/macros/document.dm	(revision 27281)
@@ -105,5 +105,4 @@
 <table width=_pagewidth_ cellpadding=0 cellspacing=0 border=0>
 <tr><td align=left valign=top>
-_usercomment_
 _prevarrow_
 </td><td align=right valign=top>
@@ -125,4 +124,12 @@
 #######################################################################
 
+# associated javascript function loadUserComments is in style.dm's _globalscripts_ macro
+_usercomments_ {
+_If_(_cgiargd_,
+<div id="usercomments"></div>
+)
+}
+
+
 # Display the add-user-comment form on actual document pages and not when browsing/searching
 # This means the form should only be displayed on pages where the _cgiargd_ (the docid) is set
@@ -132,5 +139,5 @@
 # http://stackoverflow.com/questions/8869341/ajax-form-submit-with-submit-button
 
-_usercomment_ {
+_addusercomment_ {
 
 _If_(_cgiargd_,
@@ -232,4 +239,7 @@
 		doc.AddUserCommentForm.username.value = "";
 		doc.getElementById("usercommentfeedback").innerHTML = "_textcommentsubmitted_";
+
+		// update display of existing user comments to show the newly added comment
+		loadUserComments();
 	\}
 </script>
@@ -632,4 +642,15 @@
 _navarrowsbottom_
 </div>
+
+<center>
+<table width=_pagewidth_ cellpadding=0 cellspacing=0 border=0>
+<tr><td align=left valign=top>
+<div class="commentssection">
+_usercomments_
+_addusercomment_
+</div>
+</td></tr></table>
+</center>
+
 _endspacer__htmlfooter_
 }
Index: /main/trunk/greenstone2/macros/style.dm
===================================================================
--- /main/trunk/greenstone2/macros/style.dm	(revision 27280)
+++ /main/trunk/greenstone2/macros/style.dm	(revision 27281)
@@ -241,4 +241,136 @@
 
     var gsapi = new GSAjaxAPI("_gwcgi_","_cgiargc_");
+    
+    // http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-with-format-hhmmss
+    // Call as: alert(timestamp.printTime());
+    String.prototype.printTime = function () \{
+      var timestamp    = parseInt(this, 10); // don't forget the second param
+      var date = new Date(timestamp);
+      return date.toLocaleDateString() + " " + date.toLocaleTimeString();   
+   \}
+
+
+    function loadUserComments() \{
+
+        // don't bother loading comments if we're not on a document page (in which case there's no usercommentdiv)
+        var usercommentdiv = document.getElementById("usercomments");
+	if(usercommentdiv == undefined || usercommentdiv == null) \{
+	return;
+	\}
+
+	// else, if we have a usercommentdiv, we would have a docid. Get toplevel section of the docid
+	var doc_id = "_cgiargd_"; //escape("_cgiargd_");
+	var period = doc_id.indexOf(".");
+	if(period != -1) \{
+	    doc_id = doc_id.substring(0, period);
+	\}
+	
+	var metapos=0; // counter variable
+	var username = null;
+	var timestamp = null;
+	var comment = null;
+	
+	var comments_arraymap = []; // array of comment records with timestamp, username and comment fields
+
+	// First retrieve all the usercomments (usernames, associated usertimestamps and usercomments)
+	// from the archives folder, where it is arranged easier than in index because it's in sequence already.
+
+	username = gsapi.getArchivesMetadata(doc_id, "username", metapos);
+	
+	// Keep looping, retrieving username, comment and timestamp
+	// Store each triplet in the comments array. Stop when there's no further usernames in archives
+
+	while(username) \{ 
+	
+		timestamp = gsapi.getArchivesMetadata(doc_id, "usertimestamp", metapos);
+		comment = gsapi.getArchivesMetadata(doc_id, "usercomment", metapos);
+
+		//alert("username: " + username + "\\ntime: " + timestamp + "\\ncomment: " + comment);
+
+		// array of records (arraymap)
+		// http://stackoverflow.com/questions/8190658/sorting-objects-in-a-javascript-array-by-a-key-value
+		// http://stackoverflow.com/questions/4969121/in-javascript-is-there-an-easy-way-to-sort-key-value-pairs-by-the-value-and-re
+		// http://stackoverflow.com/questions/6298169/how-to-create-a-map-object-in-a-javascript
+
+		// object with member vars t, u and c.
+		var comment_record = \{
+		    t: timestamp,
+		    u: username,
+		    c: comment
+		\};
+
+		comments_arraymap.push(comment_record);
+			
+		metapos++;
+		username = gsapi.getArchivesMetadata(doc_id, "username", metapos);
+	\}
+
+	// No need to sort by time, 
+	// as the array is already stored sorted and hence retrieved in the right order by using metapost counter
+	// If sorting the array of comment records, which would be by timestamp, see
+	// https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort
+
+	//comments_arraymap.sort(function(rec1, rec2) \{
+	// return rec1.t-rec2.t; // sort by timestamp
+	//\});
+	
+	//alert("Found " + metapos + " number of comments in the archives folder for doc " + doc_id);
+	
+	// create the output HTML for all comments in one go now
+	// (we do this here altogether to hopefully avoid reloading the page for each comment)
+
+	for(var i = 0; i < metapos; i++) \{
+
+		var comment_record = comments_arraymap[i];
+
+		username = comment_record.u;
+		timestamp = comment_record.t;
+		comment = comment_record.c;
+
+		// for each usercomment, create a child div with the username, timestamp and comment
+
+		var divgroup=document.createElement("div");
+		var attr=document.createAttribute("class");
+		attr.nodeValue="usercomment";
+		divgroup.setAttributeNode(attr);
+
+		var divuser=document.createElement("div");
+		var divtime=document.createElement("div");
+		var divcomment=document.createElement("div");
+
+	
+		divgroup.appendChild(divuser);
+		var txt=document.createTextNode(username);
+		divuser.appendChild(txt);
+
+		divgroup.appendChild(divtime);
+		txt=document.createTextNode(timestamp.printTime()); // formate timestamp for date/time display
+		divtime.appendChild(txt);
+
+		divgroup.appendChild(divcomment);
+		txt=document.createTextNode(comment);
+		divcomment.appendChild(txt);
+
+		usercommentdiv.appendChild(divgroup);
+	\}
+
+	
+    \}
+
+
+    // http://stackoverflow.com/questions/807878/javascript-that-executes-after-page-load
+    // ensure we don't replace any other onLoad() functions, but append the loadUserComments() 
+    // function to the existing onLoad handlers
+
+    if(window.onload) {\
+        var curronload = window.onload;
+        var newonload = function() {\
+            curronload();
+            loadUserComments();
+        \};
+        window.onload = newonload;
+    \} else {\
+        window.onload = loadUserComments;
+    \}
   </script>
 
Index: /main/trunk/greenstone2/web/script/gsajaxapi.js
===================================================================
--- /main/trunk/greenstone2/web/script/gsajaxapi.js	(revision 27280)
+++ /main/trunk/greenstone2/web/script/gsajaxapi.js	(revision 27281)
@@ -254,4 +254,21 @@
     }
 
+    // New
+    this.getArchivesMetadata = function(docoid,metaname,metapos)
+    {
+        var mdserver = this.metadataserverURL();
+    
+        var url = mdserver + "?a=get-archives-metadata";
+        url += "&c="+collect_;
+        url += "&d="+docoid;
+        url += "&metaname=" + metaname;
+        if (metapos!=null) {
+            url += "&metapos=" + metapos;
+        }
+
+	//alert("In getArchivesMeta. URL: " + url)
+        return this.urlGetSync(url); //Once this works, make it POST
+    }
+
     this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode)
     {
