Index: /main/trunk/greenstone3/web/interfaces/default/js/documentedit_scripts_util.js
===================================================================
--- /main/trunk/greenstone3/web/interfaces/default/js/documentedit_scripts_util.js	(revision 32892)
+++ /main/trunk/greenstone3/web/interfaces/default/js/documentedit_scripts_util.js	(revision 32893)
@@ -330,4 +330,17 @@
 }
 
+function changesSaved() {
+	console.log("Replacing init states with current states");
+	// Clean changes. We're here because setting meta for all meta changes was successful, so
+	// - update doc's metadata initial states to current:
+	editableInitStates = editableLastStates;		
+	// - update doc's map editors' initial states to current:
+	var map_editors_array = Object.values(gsmap_store);
+	for(var i = 0; i < map_editors_array.length; i++) {
+		var map_editor = map_editors_array[i];
+		map_editor.savedOverlays = JSON.stringify(ShapesUtil.overlayToJSON(map_editor.overlays));
+	}
+}
+
 
 function saveAndRebuild(rebuild) 
@@ -345,16 +358,4 @@
 	var sendBuildRequest = function()
 	{
-		console.log("Replacing init states with current states");
-		// Clean changes. We're here because setting meta for all meta changes was successful, so
-		// - update doc's metadata initial states to current:
-		editableInitStates = editableLastStates;		
-		// - update doc's map editors' initial states to current:
-		var map_editors_array = Object.values(gsmap_store);
-		for(var i = 0; i < map_editors_array.length; i++) {
-			var map_editor = map_editors_array[i];
-			map_editor.savedOverlays = JSON.stringify(ShapesUtil.overlayToJSON(map_editor.overlays));
-		}
-		
-		
 		var request = "[";
 		for(var i = 0; i < _transactions.length; i++)
@@ -399,5 +400,9 @@
 				if (rebuild) {
 				    buildCollections(_collectionsToBuild, null, reloadUponRebuild);
-				} else {
+				} else { // no rebuilding
+					// We're past possible errors at this point. So changes have by now been definitely saved to archives.
+					// and as there is no rebuilding of the collection, we're now ready to set init states to current states					
+					changesSaved();
+					
 				  // reset the save button here
 				    enableSaveButtons(true);
@@ -537,4 +542,33 @@
 
 	
+	var errorCallback = function() {		
+		alert("A server side error occurred during setArchivesMetadata. (Is the server running?)\nNot proceeding further with saving and rebuilding.");
+		return true;
+		
+	}
+	
+	// called on success callback, to check for errors in response. Returns true if response contains the error status code 3
+	var hadErrorResponseOnSave = function(response) {
+		
+		// check response for error status code 3
+		//<status code="3"
+		
+		parser = new DOMParser();
+		xmlDoc = parser.parseFromString(response,"text/xml");
+		
+		// Response may have no status code if metadata changes made and the the server stopped and then restarted and docEditor's building button pressed:
+		// response message returned is that the user is not logged in. Don't handle this scenario here. This function solely checks for error status code===3 in responses.
+		if(xmlDoc.getElementsByTagName("status").length > 0) {
+		
+			var status_code = xmlDoc.getElementsByTagName("status")[0].getAttribute("code");
+			if(status_code === "3") { // status code 3 means error (see GSStatus.java::ERROR)
+				alert("An error occurred during setArchivesMetadata.\nNot proceeding further with saving and rebuilding.\nSee browser's console log for details.");
+				console.log("@@@ Error on setting archive metadata. Got error message: " + response);
+				return true;
+			}
+		}
+		return false;
+
+	}
 	
 	var processChangesLoop = function(index)
@@ -560,13 +594,19 @@
 				// collection, site, documentID, metadataName, metadataPosition, metadataValue, prevMetadataValue, metamode, responseFunction				
 				//console.log("@@@ metapos! change: ", change);
-				gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, change.metapos, encodeDelimiters(change.value), null, "override", function(){callbackFunction();});
+				gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, change.metapos, encodeDelimiters(change.value), null, "override",
+					function(response){ if(!hadErrorResponseOnSave(response)) callbackFunction(); },
+					errorCallback);					
 			}
 		  else if(change.orig)
 		    {
-			gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, null, encodeDelimiters(change.value), encodeDelimiters(change.orig), "override", function(){callbackFunction();});
+			gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, null, encodeDelimiters(change.value), encodeDelimiters(change.orig), "override",
+				function(response){ if(!hadErrorResponseOnSave(response)) callbackFunction(); },
+				errorCallback);
 		    }
 		  else
 		    {
-			gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, null, encodeDelimiters(change.value), null, "accumulate", function(){callbackFunction();});
+			gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, null, encodeDelimiters(change.value), null, "accumulate",
+				function(response){ if(!hadErrorResponseOnSave(response)) callbackFunction(); },
+				errorCallback);
 		    }
 		}
@@ -681,4 +721,9 @@
 									if(counter == collections.length - 1)
 									{
+										// We're here because rebuilding has now completed with no errors.
+										// This means changes were definitely successfully saved to archives AND have been rebuilt with NO errors,
+										// so set init states to current states:
+										changesSaved();
+										
 										removeCollectionsFromBuildList(collections);
 										if(callback)
Index: /main/trunk/greenstone3/web/interfaces/default/js/javascript-global-functions.js
===================================================================
--- /main/trunk/greenstone3/web/interfaces/default/js/javascript-global-functions.js	(revision 32892)
+++ /main/trunk/greenstone3/web/interfaces/default/js/javascript-global-functions.js	(revision 32893)
@@ -533,9 +533,9 @@
 		
 		if(successResponseFunction != null) {
-		    
+		    //console.log("XXXX callMetaServer - Got response: " + response);
 		    successResponseFunction(response);
 		}
 	    })
-	    .error(function() {
+	    .error(function(response) {
 			if(errorResponseFunction != null) { 
 				errorResponseFunction(response);
