Index: main/trunk/greenstone2/macros/style.dm
===================================================================
--- main/trunk/greenstone2/macros/style.dm	(revision 27312)
+++ main/trunk/greenstone2/macros/style.dm	(revision 27313)
@@ -264,92 +264,80 @@
 	    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.getDocumentMetadata(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) \{  //&& !username.indexOf("ERROR") == -1) \{ 
-	
-		var errorIndex = username.indexOf("ERROR");
-		if(errorIndex != -1) \{
-		   var endIndex = username.indexOf("(");
-		   var error = username.substring(errorIndex,endIndex);
-		   var errormessage=document.createTextNode("Error retrieving comments. " + error);
-		   usercommentdiv.appendChild(errormessage);
-   		   //alert(username);
-		   break;
-		\}
-	
-		timestamp = gsapi.getDocumentMetadata(doc_id, "usertimestamp", metapos);
-		comment = gsapi.getDocumentMetadata(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.getDocumentMetadata(doc_id, "username", metapos);
+
+	var username_rec = \{
+	    metaname: "username",
+	    metapos: "all"
+	\};
+
+	var timestamp_rec = \{
+	    metaname: "usertimestamp",
+	    metapos: "all"
+	\};
+
+	var comment_rec = \{
+	    metaname: "usercomment",
+	    metapos: "all"
+	\};
+
+	var doc_rec = \{
+	    docid: doc_id,
+	    metatable: [username_rec, timestamp_rec, comment_rec]	    
+	\};
+
+	var docArray = [doc_rec];
+	//alert(JSON.stringify(docArray));
+
+	var json_result_str = gsapi.getMetadataArray(docArray, "index");
+//	alert(json_result_str);
+	var result = JSON.parse(json_result_str);
+	// result contains only one docrec (result[0]), since we asked for the usercomments of one docid
+	var metatable = result[0].metatable;
+//	alert(JSON.stringify(metatable));
+
+	var i = 0;
+	var looping = true;
+	var print_heading = true;
+
+	// metatable[0] = list of usernames, metatable[1] = list of timestamps, metatable[2] = list of comments	
+	// the 3 lists/arrays should be of even length. Assuming this, loop as long as there's another username
+	while(looping) \{
+		var metaval_rec = metatable[0].metavals[i];
+		if(metaval_rec == undefined) \{
+		    looping = false;
+		\} 
+		else \{
+
+		    if(print_heading) \{
+		         var heading=document.createElement("div");
+		       	 var attr=document.createAttribute("class");
+		       	 attr.nodeValue="usercommentheading";
+		       	 heading.setAttributeNode(attr);
+		       	 var txt=document.createTextNode("_text-usercomments-section_");
+		       	 heading.appendChild(txt);
+		      	 usercommentdiv.appendChild(heading);
+
+ 		         print_heading = false;
+		    \}
+
+    		    var username = metaval_rec.metavalue;
+		    var timestamp = metatable[1].metavals[i].metavalue;  
+		    var comment = metatable[2].metavals[i].metavalue; 
+
+		    //alert("Comment: " + username + " " + timestamp + " " + comment);
+
+		    // No need to sort by time, as the meta are already stored sorted 
+		    // and hence retrieved in the right order by using the i (metapos) 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
+
+		     // for each usercomment, create a child div with the username, timestamp and comment
+		     displayInUserCommentList(usercommentdiv, username, timestamp, comment);
+		     
+		     i++;
+	     \}		
 	\}
 
-	// No need to sort by time, 
-	// as the array is already stored sorted and hence retrieved in the right order by using metapos 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 for all values together to hopefully avoid reloading the page for each comment)
-
-	if(metapos > 0) \{ 
-		var heading=document.createElement("div");
-		var attr=document.createAttribute("class");
-		attr.nodeValue="usercommentheading";
-		heading.setAttributeNode(attr);
-		var txt=document.createTextNode("_text-usercomments-section_");
-		heading.appendChild(txt);
-		usercommentdiv.appendChild(heading);
-	\}
-
-	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
-		displayInUserCommentList(usercommentdiv, username, timestamp, comment);
-	\}
-
-	
     \}
+
 
     function displayInUserCommentList(usercommentdiv, username, timestamp, comment) \{
Index: main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm
===================================================================
--- main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27312)
+++ main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27313)
@@ -270,4 +270,6 @@
 
 # takes a JSON string and returns a JSON string
+# resulting string is of the form:
+# [{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metavals":[{"metapos":"0","metavalue":"me"},{"metapos":"1","metavalue":"admin"}]},{"metaname":"usertimestamp","metavals":[{"metapos":"0","metavalue":"1367900586888"},{"metapos":"1","metavalue":"1367900616574"}]},{"metaname":"usercomment","metavals":[{"metapos":"0","metavalue":"Hi"},{"metapos":"1","metavalue":"Hello"}]}]}]
 sub _get_index_metadata_array
 {
@@ -304,8 +306,8 @@
 	    $json_result_str .= ",";
 	}
-	$json_result_str = $json_result_str . "{docid:\"" . $docid . "\"";	
+	$json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\"";	
 
 	my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
-	$json_result_str = $json_result_str . ",metatable:[";
+	$json_result_str = $json_result_str . ",\"metatable\":[";
 
 	my $first_rec = 1;
@@ -318,9 +320,9 @@
 	    
 	    my $metaname  = $metatable_rec->{'metaname'};
-	    $json_result_str .= "{metaname:\"$metaname\",metavals:[";
+	    $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":[";
 
 	    my $metapos   = $metatable_rec->{'metapos'}; # 0... 1|all|undefined
 	    if(!defined $metapos) {
-		$metapos = "all";
+		$metapos = 0;
 	    }
 
@@ -344,5 +346,5 @@
 
 		$metavalue = $doc_rec->{$metaname}->[$metapos];		
-		$json_result_str .= "{metapos:\"$metapos\",metavalue:\"$metavalue\"}";
+		$json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
 
 	    } else {
@@ -358,5 +360,5 @@
 		    }
 	    
-		    $json_result_str .= "{metapos:\"$metapos\",metavalue:\"$metavalue\"}";
+		    $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
 
 		    $metapos++;
Index: main/trunk/greenstone2/web/script/gsajaxapi.js
===================================================================
--- main/trunk/greenstone2/web/script/gsajaxapi.js	(revision 27312)
+++ main/trunk/greenstone2/web/script/gsajaxapi.js	(revision 27313)
@@ -271,4 +271,24 @@
     }
 
+    this.getMetadataArray = function(docArray,where)
+    {
+	docArrayJSON = JSON.stringify(docArray);
+
+	var mdserver = this.metadataserverURL();
+	
+	var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array";
+	if(where != null) {
+	    params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
+	    //} else {
+	    //    params += "&where=import|archives|index";
+	}
+	params += "&c="+escape(collect_);
+	params += "&json="+escape(docArrayJSON);
+	
+	//this.urlGetSync(mdserver + "?" + params);
+	return this.urlPostSync(mdserver,params);	
+    }
+
+
     this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode)
     {
