Index: /main/trunk/greenstone3/web/interfaces/default/js/documentedit_scripts.js
===================================================================
--- /main/trunk/greenstone3/web/interfaces/default/js/documentedit_scripts.js	(revision 32801)
+++ /main/trunk/greenstone3/web/interfaces/default/js/documentedit_scripts.js	(revision 32802)
@@ -1,5 +1,4 @@
 /** Javascript file for editing a single document's content - metadata and text */
 /** uses other functions in documentedit_scripts_util.js */
-
 
 /* some vars for document editing */
@@ -15,8 +14,8 @@
 
 /* What kind of metadata element selection do we provide?
-   plain: just a text input box
-   fixedlist: a drop down menu with a fixed list of options (provided by the availableMetadataElements list)
-   autocomplete: a text input box with a list of suggestions to choose from (provided by the availableMetadataElements list). Allows additional input other than the fixed list 
-*/
+plain: just a text input box
+fixedlist: a drop down menu with a fixed list of options (provided by the availableMetadataElements list)
+autocomplete: a text input box with a list of suggestions to choose from (provided by the availableMetadataElements list). Allows additional input other than the fixed list
+ */
 var new_metadata_field_input_type = "plain";
 /* add all metadata button? only valid with fixedlist or autocomplete metadata element selection */
@@ -31,5 +30,5 @@
 var autocompleteMetadata = ["dc.Subject"];
 var dcSubject_values = ["Kings", "Queens", "others"];
-*/
+ */
 
 /* The metadata specified in multiValuedMetadata array will be treated as a delimited list, using mvm_delimiter. On saving, the values will be separated and saved individually */
@@ -38,25 +37,39 @@
 var mvm_delimiter = ";";
 var mapsIdsArray = []; //An array for the IDs of all the maps created
+
 /************************
-* METADATA EDIT SCRIPTS *
-************************/
-
-function addEditMetadataLink(cell)
-{
+ * METADATA EDIT SCRIPTS *
+ ************************/
+
+function addEditMetadataLink(cell) {
 	cell = $(cell);
 	var id = cell.attr("id").substring(6);
 	var metaTable = gs.jqGet("meta" + id);
-	var row = cell.parent();	
-	var newCell = $("<td>", {"style": "font-size:0.7em; padding:0px 10px", "class": "editMetadataButton"});
-	var linkSpan = $("<span>", {"class": "ui-state-default ui-corner-all", "style": "padding: 2px; float:left;"});
-	
-    var linkLabel = $("<span>"+gs.text.de.edit_metadata+"</span>");		
- 	var linkIcon = $("<span>", {"class": "ui-icon ui-icon-folder-collapsed"});
+	var row = cell.parent();
+	var newCell = $("<td>", {
+			"style": "font-size:0.7em; padding:0px 10px",
+			"class": "editMetadataButton"
+		});
+	var linkSpan = $("<span>", {
+			"class": "ui-state-default ui-corner-all",
+			"style": "padding: 2px; float:left;"
+		});
+
+	var linkLabel = $("<span>" + gs.text.de.edit_metadata + "</span>");
+	var linkIcon = $("<span>", {
+			"class": "ui-icon ui-icon-folder-collapsed"
+		});
 	newCell.linkIcon = linkIcon;
 	newCell.linkLabel = linkLabel;
-	
-	var uList = $("<ul>", {"style": "outline: 0 none; margin:0px; padding:0px;"});
-	var labelItem = $("<li>", {"style": "float:left; list-style:none outside none;"});
-	var iconItem = $("<li>", {"style": "float:left; list-style:none outside none;"});
+
+	var uList = $("<ul>", {
+			"style": "outline: 0 none; margin:0px; padding:0px;"
+		});
+	var labelItem = $("<li>", {
+			"style": "float:left; list-style:none outside none;"
+		});
+	var iconItem = $("<li>", {
+			"style": "float:left; list-style:none outside none;"
+		});
 
 	uList.append(iconItem);
@@ -64,30 +77,28 @@
 	labelItem.append(linkLabel);
 	iconItem.append(linkIcon);
-	
-	var newLink = $("<a>", {"href": "javascript:;"});
-	newLink.click(function()
-	{
+
+	var newLink = $("<a>", {
+			"href": "javascript:;"
+		});
+	newLink.click(function () {
 		console.log(metaTable.metaNameField);
-		if(metaTable.css("display") == "none")
-		{
-		    linkLabel.html(gs.text.de.hide_metadata);
+		if (metaTable.css("display") == "none") {
+			linkLabel.html(gs.text.de.hide_metadata);
 			linkIcon.attr("class", "ui-icon ui-icon-folder-open");
 			metaTable.css("display", "block");
 			metaTable.metaNameField.css("display", "inline");
-		    metaTable.addRowButton.css("display", "inline");
-		    if (enable_add_all_metadata_button == true) {
-			    metaTable.addAllButton.css("display", "inline");
-		    }
-		}
-		else
-		{
-		    linkLabel.html(gs.text.de.edit_metadata);			
+			metaTable.addRowButton.css("display", "inline");
+			if (enable_add_all_metadata_button == true) {
+				metaTable.addAllButton.css("display", "inline");
+			}
+		} else {
+			linkLabel.html(gs.text.de.edit_metadata);
 			linkIcon.attr("class", "ui-icon ui-icon-folder-collapsed");
 			metaTable.css("display", "none");
 			metaTable.metaNameField.css("display", "none");
-		    metaTable.addRowButton.css("display", "none");
-		    if (enable_add_all_metadata_button == true) {
-			    metaTable.addAllButton.css("display", "none");
-		    }
+			metaTable.addRowButton.css("display", "none");
+			if (enable_add_all_metadata_button == true) {
+				metaTable.addAllButton.css("display", "none");
+			}
 		}
 	});
@@ -97,33 +108,46 @@
 	newCell.append(linkSpan);
 	row.append(newCell);
-	
+
 	addFunctionalityToTable(metaTable);
 	metaTable.metaNameField.css("display", "none");
-    metaTable.addRowButton.css("display", "none");
-    if (enable_add_all_metadata_button == true) {
-	metaTable.addAllButton.css("display", "none");
-    }
-}
-
-
-function addEditMapGPSLink(cell)
-{
+	metaTable.addRowButton.css("display", "none");
+	if (enable_add_all_metadata_button == true) {
+		metaTable.addAllButton.css("display", "none");
+	}
+}
+
+var gsmap_store = {};
+
+function addEditMapGPSLink(cell) {
 	cell = $(cell);
 	var id = cell.attr("id").substring(6);
 	//console.log(id);
 	var mapGPScontainer = gs.jqGet("map-and-controls-" + id);
-	console.log(mapGPScontainer);
 	var row = cell.parent();
-	var newCell = $("<td>", {"style": "font-size:0.7em; padding:0px 10px", "class": "editMapGPSButton"});
-	var linkSpan = $("<span>", {"class": "ui-state-default ui-corner-all", "style": "padding: 2px; float:left;"});
-	
-    var linkLabel = $("<span>"+gs.text.de.edit_map_gps+"</span>");		
- 	var linkIcon = $("<span>", {"class": "ui-icon ui-icon-folder-collapsed"});
+	var newCell = $("<td>", {
+			"style": "font-size:0.7em; padding:0px 10px",
+			"class": "editMapGPSButton"
+		});
+	var linkSpan = $("<span>", {
+			"class": "ui-state-default ui-corner-all",
+			"style": "padding: 2px; float:left;"
+		});
+
+	var linkLabel = $("<span>" + gs.text.de.edit_map_gps + "</span>");
+	var linkIcon = $("<span>", {
+			"class": "ui-icon ui-icon-folder-collapsed"
+		});
 	newCell.linkIcon = linkIcon;
 	newCell.linkLabel = linkLabel;
-	
-	var uList = $("<ul>", {"style": "outline: 0 none; margin:0px; padding:0px;"});
-	var labelItem = $("<li>", {"style": "float:left; list-style:none outside none;"});
-	var iconItem = $("<li>", {"style": "float:left; list-style:none outside none;"});
+
+	var uList = $("<ul>", {
+			"style": "outline: 0 none; margin:0px; padding:0px;"
+		});
+	var labelItem = $("<li>", {
+			"style": "float:left; list-style:none outside none;"
+		});
+	var iconItem = $("<li>", {
+			"style": "float:left; list-style:none outside none;"
+		});
 
 	uList.append(iconItem);
@@ -131,110 +155,101 @@
 	labelItem.append(linkLabel);
 	iconItem.append(linkIcon);
+
+	var mapEditor = new MapEditor(id);
+	gsmap_store["map-" + id] = mapEditor;
 	
-	var newLink = $("<a>", {"href": "javascript:;"});
-	newLink.click(function()
-	{
+	var newLink = $("<a>", {
+			"href": "javascript:;"
+		});
+	newLink.click(function () {
 		console.log("Show/Hide Map Editor");
+
+		// Only if this 'id' element hasn't been initialied, then do the following 2 lines
+		if (mapEditor.map !== null) {
+			console.log(mapEditor.map.mapId);
+			console.log(mapsIdsArray.includes(id));
+		}
+
+		var clicked_mapEditor = gsmap_store["map-" + id];
 		
-		// Only if this 'id' element hasn't been initialied, then do the following 2 lines
-		if(map !== null){
-			console.log(map.mapId);
-			console.log(mapsIdsArray.includes(id));
-		}
-		
-		
-		if(map == null){
-			console.log(id);
-			initMapEditorControls(id);
-			initMapEditor(id);
-			mapsIdsArray.push(map.mapId)
-		} else if(mapsIdsArray.includes(id) == false){
-			initMapEditorControls(id);
-			initMapEditor(id);
-			mapsIdsArray.push(map.mapId)
-		}
-
-		
-		
-		if(mapGPScontainer.css("display") == "none")
-		{
-			
-		    linkLabel.html(gs.text.de.hide_map_gps);
+		if (clicked_mapEditor.map == null) {
+			console.log("Initializing map for id: " + id);
+
+			clicked_mapEditor.initMapEditorControls();
+			clicked_mapEditor.initMapEditor();
+			//mapsIdsArray.push(clicked_mapEditor.map.mapId);
+		} /*
+		else if (mapsIdsArray.includes(id) == false) {
+			//mapEditor.initMapEditorControls();
+			//mapEditor.initMapEditor();
+			mapsIdsArray.push(mapEditor.map.mapId)
+		}
+*/
+		if (mapGPScontainer.css("display") == "none") {
+
+			linkLabel.html(gs.text.de.hide_map_gps);
 			linkIcon.attr("class", "ui-icon ui-icon-folder-open");
 			mapGPScontainer.css("display", "block");
 
-			
-		}
-		else
-		{
-		    linkLabel.html(gs.text.de.edit_map_gps);
-			
+		} else {
+			linkLabel.html(gs.text.de.edit_map_gps);
+
 			linkIcon.attr("class", "ui-icon ui-icon-folder-collapsed");
 			mapGPScontainer.css("display", "none");
-	
-			
+
 		}
 	});
 
-	newLink.append(uList);	
+	newLink.append(uList);
 	linkSpan.append(newLink);
 	newCell.append(linkSpan);
 	row.append(newCell);
-	
-	mapGPScontainer.css("display","none");
-	
-	
+
+	mapGPScontainer.css("display", "none");
+
 	addFunctionalityToTable(mapGPScontainer);
 	mapGPScontainer.metaNameField.css("display", "none");
-    mapGPScontainer.addRowButton.css("display", "none");
-    if (enable_add_all_metadata_button == true) {
-	  mapGPScontainer.addAllButton.css("display", "none");
-    }
-	
-}
-
-function setEditingFeaturesVisible(visible)
-{
-	if(visible)
-	{
-	    $("#editContentButton").html(gs.text.de.hide_editor); 
+	mapGPScontainer.addRowButton.css("display", "none");
+	if (enable_add_all_metadata_button == true) {
+		mapGPScontainer.addAllButton.css("display", "none");
+	}
+
+}
+
+function setEditingFeaturesVisible(visible) {
+	if (visible) {
+		$("#editContentButton").html(gs.text.de.hide_editor);
 		$("#editContentButtonDiv").attr("class", "ui-state-default ui-corner-all");
-	}
-	else
-	{
-	    $("#editContentButton").html(gs.text.de.edit_content); 
+	} else {
+		$("#editContentButton").html(gs.text.de.edit_content);
 		$("#editContentButtonDiv").attr("class", "");
 	}
-	
+
 	var visibility = (visible ? "" : "none");
-    if (display_metadata_set_selector == true) {
-	$("#metadataListLabel, #metadataSetList").css("display", visibility);
-    }
-	
-	$(".editMetadataButton").each(function()
-	{
+	if (display_metadata_set_selector == true) {
+		$("#metadataListLabel, #metadataSetList").css("display", visibility);
+	}
+
+	$(".editMetadataButton").each(function () {
 		$(this).css("display", visibility);
-	    $(this.linkLabel).html(gs.text.de.edit_metadata); 
-		$(this.linkIcon).attr("class", "ui-icon ui-icon-folder-collapsed");
-	});	
-	/*
-	$(".editMapGPS").each(function()
-	{
-		$(this).css("display", visibility);
-	    $(this.linkLabel).html(gs.text.de.edit_map_gps); 		
+		$(this.linkLabel).html(gs.text.de.edit_metadata);
 		$(this.linkIcon).attr("class", "ui-icon ui-icon-folder-collapsed");
 	});
-	*/
-	
-	$("table").each(function()
-	{
-		if($(this).attr("id") && $(this).attr("id").search(/^meta/) != -1)
-		{
+	/*
+	$(".editMapGPS").each(function(){
+	$(this).css("display", visibility);
+	$(this.linkLabel).html(gs.text.de.edit_map_gps);
+	$(this.linkIcon).attr("class", "ui-icon ui-icon-folder-collapsed");
+	});
+	 */
+
+	$("table").each(function () {
+		if ($(this).attr("id") && $(this).attr("id").search(/^meta/) != -1) {
 			$(this).css("display", "none");
 			$(this.metaNameField).css("display", "none");
-		    $(this.addRowButton).css("display", "none");
-		    if (enable_add_all_metadata_button == true) {
-			$(this.addAllButton).css("display", "none");
-		    }
+			$(this.addRowButton).css("display", "none");
+			if (enable_add_all_metadata_button == true) {
+				$(this.addAllButton).css("display", "none");
+			}
 		}
 	});
@@ -242,26 +257,21 @@
 
 /* override this function in other interface/site/collection if you want
-   a different set of metadata sets 
-  Use in conjunction with the dynamic_metadata_set_list variable. */
+a different set of metadata sets
+Use in conjunction with the dynamic_metadata_set_list variable. */
 function setStaticMetadataSets(list) {
-  addOptionToList(list, "All", gs.text.de.all_metadata);
-}
-
-function readyPageForEditing()
-{
-
- //   CKEDITOR.on('instanceReady', function(evt) {
-//	addCKEEditableState(evt,editableInitStates);
- //   });
-
-	if($("#metadataSetList").length)
-	{
+	addOptionToList(list, "All", gs.text.de.all_metadata);
+}
+
+function readyPageForEditing() {
+
+	//   CKEDITOR.on('instanceReady', function(evt) {
+	//	addCKEEditableState(evt,editableInitStates);
+	//   });
+
+	if ($("#metadataSetList").length) {
 		var setList = $("#metadataSetList");
-		if(!setList.css("display") || setList.css("display") == "")
-		{
+		if (!setList.css("display") || setList.css("display") == "") {
 			setEditingFeaturesVisible(false);
-		}
-		else
-		{
+		} else {
 			setEditingFeaturesVisible(true);
 		}
@@ -269,55 +279,72 @@
 	}
 
-    $("#editContentButton").html(gs.text.de.hide_editor);
-	//wait for 0.5 sec to let ckeditor up 
-	setTimeout(function(){ $(".sectionText").each(function(){addEditableState(this,editableInitStates);}); }, 500);	
+	$("#editContentButton").html(gs.text.de.hide_editor);
+	//wait for 0.5 sec to let ckeditor up
+	setTimeout(function () {
+		$(".sectionText").each(function () {
+			addEditableState(this, editableInitStates);
+		});
+	}, 500);
 	var editBar = $("#editBarLeft");
-	
-
-	var visibleMetadataList = $("<select>", {"id": "metadataSetList", "class": "ui-state-default"});
+
+	var visibleMetadataList = $("<select>", {
+			"id": "metadataSetList",
+			"class": "ui-state-default"
+		});
 	setStaticMetadataSets(visibleMetadataList);
-    
-    if (display_metadata_set_selector == true) {
-	var metadataListLabel = $("<span>", {"id": "metadataListLabel", "style": "margin-left:20px;"});
-	metadataListLabel.html(gs.text.de.visible_metadata); 
-	editBar.append(metadataListLabel);
-    } else {
-	visibleMetadataList.css ("display", "none");
-    }
+
+	if (display_metadata_set_selector == true) {
+		var metadataListLabel = $("<span>", {
+				"id": "metadataListLabel",
+				"style": "margin-left:20px;"
+			});
+		metadataListLabel.html(gs.text.de.visible_metadata);
+		editBar.append(metadataListLabel);
+	} else {
+		visibleMetadataList.css("display", "none");
+	}
 	editBar.append(visibleMetadataList);
 	visibleMetadataList.change(onVisibleMetadataSetChange);
 	editBar.append("<br>");
-    
-	for (var i=0; i< save_and_rebuild_buttons.length; i++) {
-	  var button_type = save_and_rebuild_buttons[i];
-	  if (button_type == "save") {
-	    var saveButton = $("<button>", {"id": "saveButton", "class": "ui-state-default ui-corner-all"});
-	    saveButton.click(save);
-	    saveButton.html(gs.text.de.save);
-	    editBar.append(saveButton);
-	  } else if(button_type == "rebuild") {
-	    var rebuildButton = $("<button>", {"id": "rebuildButton", "class": "ui-state-default ui-corner-all"});
-	    rebuildButton.click(rebuildCurrentCollection);
-	    rebuildButton.html(gs.text.de.rebuild);
-	    editBar.append(rebuildButton);
-	  } else if (button_type == "saveandrebuild") {
-	    var saveAndRebuildButton = $("<button>", {"id": "saveAndRebuildButton", "class": "ui-state-default ui-corner-all"});
-	    saveAndRebuildButton.click(saveAndRebuild);
-	    saveAndRebuildButton.html(gs.text.de.saverebuild);
-	    editBar.append(saveAndRebuildButton);
-
-	  }
+
+	for (var i = 0; i < save_and_rebuild_buttons.length; i++) {
+		var button_type = save_and_rebuild_buttons[i];
+		if (button_type == "save") {
+			var saveButton = $("<button>", {
+					"id": "saveButton",
+					"class": "ui-state-default ui-corner-all"
+				});
+			saveButton.click(save);
+			saveButton.html(gs.text.de.save);
+			editBar.append(saveButton);
+		} else if (button_type == "rebuild") {
+			var rebuildButton = $("<button>", {
+					"id": "rebuildButton",
+					"class": "ui-state-default ui-corner-all"
+				});
+			rebuildButton.click(rebuildCurrentCollection);
+			rebuildButton.html(gs.text.de.rebuild);
+			editBar.append(rebuildButton);
+		} else if (button_type == "saveandrebuild") {
+			var saveAndRebuildButton = $("<button>", {
+					"id": "saveAndRebuildButton",
+					"class": "ui-state-default ui-corner-all"
+				});
+			saveAndRebuildButton.click(saveAndRebuild);
+			saveAndRebuildButton.html(gs.text.de.saverebuild);
+			editBar.append(saveAndRebuildButton);
+
+		}
 	}
 	var statusBarDiv = $("<div>");
 	editBar.append(statusBarDiv);
 	_statusBar = new StatusBar(statusBarDiv[0]);
-	
+
 	var titleDivs = $(".sectionTitle");
-	for(var i = 0; i < titleDivs.length; i++)
-	{
+	for (var i = 0; i < titleDivs.length; i++) {
 		addEditMetadataLink(titleDivs[i]);
 		addEditMapGPSLink(titleDivs[i]);
 	}
-	
+
 	_baseURL = gs.xsltParams.library_name;
 	onVisibleMetadataSetChange(); // make sure that the selected item in the list is active
@@ -327,103 +354,108 @@
 // currently not used if other one is present. need to get the js include order right
 function enableSaveButtons(enabled) {
-  if (enabled) {
-    $("#saveButton, #rebuildButton, #saveAndRebuildButton").removeAttr("disabled");
-  } else {
-    $("#saveButton, #rebuildButton, #saveAndRebuildButton").attr("disabled", "disabled");
-  }
-}
-
-/* this is a cut down version of save() from documentmaker_scripts_util.js 
- going back to using save, will delete this once everything working*/
+	if (enabled) {
+		$("#saveButton, #rebuildButton, #saveAndRebuildButton").removeAttr("disabled");
+	} else {
+		$("#saveButton, #rebuildButton, #saveAndRebuildButton").attr("disabled", "disabled");
+	}
+}
+
+/* this is a cut down version of save() from documentmaker_scripts_util.js
+going back to using save, will delete this once everything working*/
 function saveMetadataChangesOld() {
 
-  console.log("Saving metadata changes");
- 
-  // get collection name
-  var collection = gs.cgiParams.c;;
-
-  // get document id
-  var docID = gs.cgiParams.d;
-
-  var metadataChanges = new Array();
-  if (_deletedMetadata.length > 0) {
-
-    for(var i = 0; i < _deletedMetadata.length; i++) {
-      
-      var currentRow = _deletedMetadata[i];
-      
-      //Get metadata name
-      var cells = currentRow.getElementsByTagName("TD");
-      var nameCell = cells[0];
-      var name = nameCell.innerHTML;
-      var valueCell = cells[1];
-      var value = valueCell.innerHTML;
-      metadataChanges.push({type:'delete', docID:docID, name:name, value:value});
-      removeFromParent(currentRow);
-    }
-  }
-
-  /*var changes = null;
+	console.log("Saving metadata changes");
+
+	// get collection name
+	var collection = gs.cgiParams.c; ;
+
+	// get document id
+	var docID = gs.cgiParams.d;
+
+	var metadataChanges = new Array();
+	if (_deletedMetadata.length > 0) {
+
+		for (var i = 0; i < _deletedMetadata.length; i++) {
+
+			var currentRow = _deletedMetadata[i];
+
+			//Get metadata name
+			var cells = currentRow.getElementsByTagName("TD");
+			var nameCell = cells[0];
+			var name = nameCell.innerHTML;
+			var valueCell = cells[1];
+			var value = valueCell.innerHTML;
+			metadataChanges.push({
+				type: 'delete',
+				docID: docID,
+				name: name,
+				value: value
+			});
+			removeFromParent(currentRow);
+		}
+	}
+
+	/*var changes = null;
 	//var changes = de.Changes.getChangedEditableSections();
-  for(var i = 0; i < changes.length; i++) {
-    
-    var changedElem = changes[i];
-		
-    //Get metadata name
-    var row = changedElem.parentNode;
-    var cells = row.getElementsByTagName("TD");
-    var nameCell = cells[0];
-    var name = nameCell.innerHTML;
-    var value = changedElem.innerHTML;
-    value = value.replace(/&nbsp;/g, " ");
-    
-    var orig = changedElem.originalValue;
-    if (orig) {
-      orig = orig.replace(/&nbsp;/g, " ");
-    }
-    metadataChanges.push({collection:collection, docID:docID, name:name, value:value, orig:orig});
-    changedElem.originalValue = changedElem.innerHTML;
-    
-  }
-*/
-  if (metadataChanges.length ==0) {
-      console.log(gs.text.de.no_changes);
-    return;
-  }
-
-  var processChangesLoop = function(index)
-    {
-      var change = metadataChanges[index];
-      
-      var callbackFunction;
-      if(index + 1 == metadataChanges.length)
-	{
-	  callbackFunction = function(){console.log("Completed saving metadata changes. You must rebuild the collection for the changes to take effect.");};
-	}
-      else
-	{
-	  callbackFunction = function(){processChangesLoop(index + 1)};
-	}
-      if (change.type == "delete") {
-	gs.functions.removeArchivesMetadata(collection, gs.xsltParams.site_name, change.docID, change.name, null, change.value, function(){callbackFunction();});
-      } else {
-	if(change.orig)
-	  {
-	    gs.functions.setArchivesMetadata(collection, gs.xsltParams.site_name, docID, change.name, null, change.value, change.orig, "override", function(){callbackFunction();});
-	  }
-	else
-	  {
-	    gs.functions.setArchivesMetadata(collection, gs.xsltParams.site_name, docID, change.name, null, change.value, null, "accumulate", function(){callbackFunction();});
-	  }
-      }
-    }
-  processChangesLoop(0);
-  /* need to clear the changes from the page */
-  while (_deletedMetadata.length>0) {
-    _deletedMetadata.pop();
-  }
-    
-}
-
-
-
+	for(var i = 0; i < changes.length; i++) {
+
+	var changedElem = changes[i];
+
+	//Get metadata name
+	var row = changedElem.parentNode;
+	var cells = row.getElementsByTagName("TD");
+	var nameCell = cells[0];
+	var name = nameCell.innerHTML;
+	var value = changedElem.innerHTML;
+	value = value.replace(/&nbsp;/g, " ");
+
+	var orig = changedElem.originalValue;
+	if (orig) {
+	orig = orig.replace(/&nbsp;/g, " ");
+	}
+	metadataChanges.push({collection:collection, docID:docID, name:name, value:value, orig:orig});
+	changedElem.originalValue = changedElem.innerHTML;
+
+	}
+	 */
+	if (metadataChanges.length == 0) {
+		console.log(gs.text.de.no_changes);
+		return;
+	}
+
+	var processChangesLoop = function (index) {
+		var change = metadataChanges[index];
+
+		var callbackFunction;
+		if (index + 1 == metadataChanges.length) {
+			callbackFunction = function () {
+				console.log("Completed saving metadata changes. You must rebuild the collection for the changes to take effect.");
+			};
+		} else {
+			callbackFunction = function () {
+				processChangesLoop(index + 1)
+			};
+		}
+		if (change.type == "delete") {
+			gs.functions.removeArchivesMetadata(collection, gs.xsltParams.site_name, change.docID, change.name, null, change.value, function () {
+				callbackFunction();
+			});
+		} else {
+			if (change.orig) {
+				gs.functions.setArchivesMetadata(collection, gs.xsltParams.site_name, docID, change.name, null, change.value, change.orig, "override", function () {
+					callbackFunction();
+				});
+			} else {
+				gs.functions.setArchivesMetadata(collection, gs.xsltParams.site_name, docID, change.name, null, change.value, null, "accumulate", function () {
+					callbackFunction();
+				});
+			}
+		}
+	}
+	processChangesLoop(0);
+	/* need to clear the changes from the page */
+	while (_deletedMetadata.length > 0) {
+		_deletedMetadata.pop();
+	}
+
+}
