function cancelModeration(){
	//alert("cancel");
	doCloseInfoWindow();
	doCloseFormWindow();
	checkAdminUserLoggedIn();
	
	// clear the data from the map
	// and redisplay other data (depending on which layers are switched on or off)
	map.clearOverlays();
	updateMap(true);
	removeDiv('mapoverlay','mapwrapper');
}
function getPostString(tablename) {
	var postdata = formData("userDataCapture");
	var poststring = stripNonValidXMLCharacters(postdata);
	poststring = poststring.substr(0,poststring.length - 1);
	
	// now add the tablename
	poststring = poststring + "&tablename="+tablename;
	return poststring;
	
}
function updateModeratedData(tablename){
	// approved status is reset to 0
	if (checkData()) {
		poststring = getPostString(tablename);
		poststring = poststring+"&approved=0";
		sendModeratedDataToServer(poststring);
	}
	
}

function deleteModeratedData(tablename){
	if (confirm("You are about to delete this data - continue?")) {
		poststring = getPostString(tablename);
		poststring = poststring+"&approved=-2";
		sendModeratedDataToServer(poststring);
	}
}

function approveData(tablename) {
	if (checkData()) {
		var poststring = getPostString(tablename);
		poststring = poststring+"&approved=1";
		sendModeratedDataToServer(poststring);
	}
}

function refuseData(tablename) {
	poststring = getPostString(tablename);
	poststring = poststring+"&approved=-1";
	sendModeratedDataToServer(poststring);
}
function sendModeratedDataToServer(poststring) {
	xhReq = createXMLHttpRequest();
	
	// remove any dodgy characters
	
	
	
	poststring = poststring + "&action=saveModerate";
	 xhReq.open("POST", "processEditData.php", true);
	 xhReq.onreadystatechange = dataModerated;

	 //
	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
	 	// this only works in internet explorer
	 }
	xhReq.send(poststring);
}
function dataModerated() {
	if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
	if (result.indexOf("Your changes have been saved") > -1) {
		alert("Your changes have been saved");
	}
	// clear the map, alert the user, and refresh the displayed list of items for moderation ..
	doCloseInfoWindow();
	doCloseFormWindow();
	checkAdminUserLoggedIn();
	
	// clear the data from the map
	// and redisplay other data (depending on which layers are switched on or off)
	map.clearOverlays();
	updateMap(true);
	removeDiv('mapoverlay','mapwrapper');
	
}



function cancelModeratedCapture() {
	if (confirm("Cancel Data Edit?  You will lose all changes.")) {
		clearAllEditValues();
	}
}
function deleteCapture(tablename,id) {
// this is used where a record is first being created ...
	if (confirm("Cancel Data Edit?  You will lose all changes.")) {
		// delete the record from the server ...
			// in this case, the child record should be deleted from the database first
			// this is a PROPER delete as opposed to a 'delete' requested by the user / moderator, whic
			// only flags the data as deleted
			
			// so the CANCEL button should only appear for an 'add new' operation
		
			
			xhReq = createXMLHttpRequest();
			// remove any dodgy characters
			poststring = "";
			poststring = poststring + "&tablename="+tablename+"&id="+id;
			poststring = poststring + "&action=clearParent";
			 xhReq.open("POST", "processEditData.php", true);
			 xhReq.onreadystatechange = processCancelCapture;
			 //
			 try {
			   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			   //xhReq.setRequestHeader('Content-Type','text/html')
			 }
			 catch (e) {
			 	// this only works in internet explorer
			 }
			//alert(poststring);
		xhReq.send(poststring);
		//setupStep1();
	}

}
function processCancelCapture() {
if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
	clearAllEditValues();
	
	//alert("in cancel capture");
	addContentText3 = document.getElementById('controlcontent').innerHTML;
	document.getElementById('controlcontent').innerHTML = addContentText1; 	

	
}




function saveData(tablename) {
	if (checkData()) {
		saveToServer(tablename);
	}

}



function getApprovedData() {
	generateListsForAdmin(1);
}
function getUnApprovedData() {
	generateListsForAdmin(0);
}


function editExisting(tablename) {
	// this child has already been saved = we are editing/moderating
	var postdata = formData("userDataCapture");
	var poststring = stripNonValidXMLCharacters(postdata);
	poststring = poststring.substr(0,poststring.length - 1);




	var theLayerChoice = document.getElementsByName("childEditGroup");
	var j=theLayerChoice.length; 
	for (i=0; i<j; i++){
		if(theLayerChoice[i].checked) {
			var lookuptablename = theLayerChoice[i].value;
		}
	}

	if (document.getElementById('photoname')) {
		photoname = getPhotoName(document.getElementById('photoname').value);
		poststring = poststring + "&photoname="+ photoname;
	}

	minisitename = getMinisiteName();
	poststring = poststring+ "&minisitename="+minisitename
	
	// now add the tablename
	poststring = poststring + "&tablename="+tablename;
	poststring = poststring+"&approved=0";

	// as this is an add new record, also send the name of the lookup tablename, this will be used
	// to generate a new blank lookup table record to show to the user
	poststring = poststring + "&lookuptablename="+lookuptablename;
	poststring = poststring + "&action=showFirstChildExistingParent";
	xhReq = createXMLHttpRequest();
	// remove any dodgy characters
	
	 xhReq.open("POST", "processEditData.php", true);
	 xhReq.onreadystatechange = showChildEditForm; //showform;

	 //
	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
	 	// this only works in internet explorer
	 }
	xhReq.send(poststring);
}
function addlookuprecordExistingParent(tablename) {
	// get the ID from the main form - this is hidden
	
	
	var theLayerChoice = document.getElementsByName("childgroup");
	var j=theLayerChoice.length; 
	for (i=0; i<j; i++){
		if(theLayerChoice[i].checked) {
			var lookuptablename = theLayerChoice[i].value;
		}
	}

	
	
	//alert("addlookuprecordExistingParent");
	// save the main record, without any validation, then show a new sub record
	var postdata = formData("userDataCapture");
	var poststring = stripNonValidXMLCharacters(postdata);
	poststring = poststring.substr(0,poststring.length - 1);

	if (document.getElementById('photoname')) {
		photoname = getPhotoName(document.getElementById('photoname').value);
		poststring = poststring + "&photoname="+ photoname;
	}

	minisitename = getMinisiteName();
	poststring = poststring+ "&minisitename="+minisitename

	// now add the tablename
	poststring = poststring + "&tablename="+tablename;

	// as this is an add new record, also send the name of the lookup tablename, this will be used
	// to generate a new blank lookup table record to show to the user
	poststring = poststring + "&lookuptablename="+lookuptablename;
	poststring = poststring + "&action=createNewChildExistingParent";
	xhReq = createXMLHttpRequest();
	// remove any dodgy characters
	
	 xhReq.open("POST", "processEditData.php", true);
	 xhReq.onreadystatechange = showChildEditForm;  //showform;

	 //
	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
	 	// this only works in internet explorer
	 }
	xhReq.send(poststring);

}


function deleteNewAndReturnToExistingParent(lookuptablename, lookupid, parenttablename, parentid) {
	//alert("deleteNew and return to existing");
	// we have jsut created a new child but have changed our minds so need to delete
	// the temporary record, and then show the existing parent
	// happens during an EDIT SESSION

	// this method is called if the PARENT hasn't yet been saved

		// submit the form, save the changes and then re-create the main form
		// get the ID from the main form - this is hidden

	minisitename = getMinisiteName();
	poststring = poststring+ "&minisitename="+minisitename


		// save the main record, without any validation, then show a new sub record
		var postdata = formData("userDataCapture");
		var poststring = stripNonValidXMLCharacters(postdata);
		poststring = poststring.substr(0,poststring.length - 1);
		poststring = poststring+"&approved=0";


	if (document.getElementById('photoname')) {
		photoname = getPhotoName(document.getElementById('photoname').value);
		poststring = poststring + "&photoname="+ photoname;
	}


		xhReq = createXMLHttpRequest();
		// remove any dodgy characters

		poststring = poststring + "&parenttablename="+parenttablename+"&parentid="+parentid;
		poststring = poststring + "&lookuptablename="+lookuptablename+"&lookupid="+lookupid;
		poststring = poststring + "&action=DeleteNewChildExistingParent";
		 xhReq.open("POST", "processEditData.php", true);
		 xhReq.onreadystatechange = showEditForm;
	 //
		 try {
		   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   //xhReq.setRequestHeader('Content-Type','text/html')
		 }
		 catch (e) {
			// this only works in internet explorer
		 }
		//alert(poststring);
		xhReq.send(poststring);

}
function saveExistingAndReturnToExistingParent (lookuptablename, lookupid, parenttablename, parentid) {
	

	if (checkData()) {
		// submit the form, save the changes and then re-create the main form
		// get the ID from the main form - this is hidden

		// save the main record, without any validation, then show a new sub record
		var postdata = formData("userDataCapture");
		var poststring = stripNonValidXMLCharacters(postdata);
		poststring = poststring.substr(0,poststring.length - 1);
		poststring = poststring+"&approved=0";

		xhReq = createXMLHttpRequest();
		// remove any dodgy characters


		minisitename = getMinisiteName();
		poststring = poststring+ "&minisitename="+minisitename

		poststring = poststring + "&parenttablename="+parenttablename+"&parentid="+parentid;
		poststring = poststring + "&lookuptablename="+lookuptablename+"&id="+lookupid;
		poststring = poststring + "&action=saveExistingChildShowExistingParent";
		 xhReq.open("POST", "processEditData.php", true);
		 xhReq.onreadystatechange = showEditForm;
	 //
		 try {
		   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   //xhReq.setRequestHeader('Content-Type','text/html')
		 }
		 catch (e) {
			// this only works in internet explorer
		 }
		xhReq.send(poststring);
	}

	
}

function cancelExistingReturnToExistParent(lookuptablename, lookupid, parenttablename, parentid) {
	
	//alert("cancelExistingReturnToExistParent");
		// save the main record, without any validation, then show a new sub record
		xhReq = createXMLHttpRequest();
		// remove any dodgy characters
		
		
		
		poststring = "";
			//var sPath = window.location;
			//var paths = String(sPath).split("=");
			//minisitename = paths[1];
			//minisitename = minisitename.replace("#","");
			minisitename = getMinisiteName();
			poststring = poststring+ "&minisitename="+minisitename

		poststring = poststring + "&parenttablename="+parenttablename+"&parentid="+parentid;
		poststring = poststring + "&lookuptablename="+lookuptablename+"&id="+lookupid;
		poststring = poststring + "&action=cancelChildShowExistParent";
		 xhReq.open("POST", "processEditData.php", true);
		 xhReq.onreadystatechange = showEditForm;
	 //
		 try {
		   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   //xhReq.setRequestHeader('Content-Type','text/html')
		 }
		 catch (e) {
			// this only works in internet explorer
		 }
		//alert(poststring);
		xhReq.send(poststring);
}
function deleteExistingReturnToExistParent(lookuptablename, lookupid, parenttablename, parentid) {

		//alert("deleteExistingReturnToExistParent");
		
			// save the main record, without any validation, then show a new sub record
			xhReq = createXMLHttpRequest();
			// remove any dodgy characters
			poststring = "";
			
				//var sPath = window.location;
				//var paths = String(sPath).split("=");
				//minisitename = paths[1];
				//minisitename = minisitename.replace("#","");
				minisitename = getMinisiteName();
				poststring = poststring+ "&minisitename="+minisitename

			poststring = poststring + "&parenttablename="+parenttablename+"&parentid="+parentid;
			poststring = poststring + "&lookuptablename="+lookuptablename+"&lookupid="+lookupid;
			poststring = poststring + "&action=deleteChildShowExistParent";
			 xhReq.open("POST", "processEditData.php", true);
			 xhReq.onreadystatechange = showEditForm;
		 //
			 try {
			   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			   //xhReq.setRequestHeader('Content-Type','text/html')
			 }
			 catch (e) {
				// this only works in internet explorer
			 }
			//alert(poststring);
		xhReq.send(poststring);
}

function saveNewAndReturnToExistingParent(lookuptablename, lookupid, parenttablename, parentid) {
	

	if (checkData()) {
		// submit the form, save the changes and then re-create the main form
		// get the ID from the main form - this is hidden

		// save the main record, without any validation, then show a new sub record
		var postdata = formData("userDataCapture");
		var poststring = stripNonValidXMLCharacters(postdata);
		poststring = poststring.substr(0,poststring.length - 1);
		poststring = poststring+"&approved=0";
		
			minisitename = getMinisiteName();
			poststring = poststring+ "&minisitename="+minisitename


	if (document.getElementById('photoname')) {
		photoname = getPhotoName(document.getElementById('photoname').value);
		poststring = poststring + "&photoname="+ photoname;
	}


		xhReq = createXMLHttpRequest();
		// remove any dodgy characters

		poststring = poststring + "&parenttablename="+parenttablename+"&parentid="+parentid;
		poststring = poststring + "&lookuptablename="+lookuptablename+"&id="+lookupid;
		poststring = poststring + "&action=saveNewChildShowExistingParent";
		 xhReq.open("POST", "processEditData.php", true);
		 xhReq.onreadystatechange = showEditForm;
	 //
		 try {
		   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   //xhReq.setRequestHeader('Content-Type','text/html')
		 }
		 catch (e) {
			// this only works in internet explorer
		 }
		//alert(poststring);
		xhReq.send(poststring);
	}

		
}

function showChildEditForm() {
	
	  if (xhReq.readyState != 4)  { 
	   		return;
	   	}
		var response = xhReq.responseText;
		//alert("showing photo");
		//alert(response);
		var htmlscript = response.split("|XX|");
		var headID = document.getElementsByTagName("head")[0];
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.text = htmlscript[1];
		headID.appendChild(newScript);
		//alert(htmlscript[1]);
		// rather than have a new photo div, reuse the existing main form div
	
	  document.getElementById('describe').innerHTML = htmlscript[0]+generateFooter(); 
 

		  // create the black baground div
		 if (!document.getElementById('mapoverlay')) {
		    var ni = document.getElementById('mapwrapper');
		    var overlaydiv = document.createElement('div');
		   var divIdName = 'mapoverlay';
		   overlaydiv.setAttribute('id',divIdName);
		   overlaydiv.setAttribute('class','png');
		   ni.appendChild(overlaydiv);
		  }
}

function showEditForm() {
   if (xhReq.readyState != 4)  { 
   		return;
   	}
	var response = xhReq.responseText;
	//alert(response);
	var htmlscript = response.split("|XX|");
	var headID = document.getElementsByTagName("head")[0];
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.text = htmlscript[1];
	headID.appendChild(newScript);

	// find out if the 'describe' div already exists - if not,create it
	if (!document.getElementById('describe')) {
		  var ni = document.getElementById('mapwrapper');
		  var newdiv = document.createElement('div');
		  var divIdName = 'describe';
		  newdiv.setAttribute('id',divIdName);
		  newdiv.setAttribute('class','png');
		  ni.appendChild(newdiv);
	}
	document.getElementById('describe').innerHTML = htmlscript[0]+generateFooter();
	
	// remove the black background from the child photos/noise readings, if this exists
//	if (document.getElementById('mapoverlay')) {
//		 var d = document.getElementById('mapwrapper');
//		  var olddiv = document.getElementById('mapoverlay');
//		  d.removeChild(olddiv);
//
//	}

	  // create the black baground div
	  if (!document.getElementById('mapoverlay')) {
		    var ni = document.getElementById('mapwrapper');
		    var overlaydiv = document.createElement('div');
		   var divIdName = 'mapoverlay';
		   overlaydiv.setAttribute('id',divIdName);
		   overlaydiv.setAttribute('class','png');
		   ni.appendChild(overlaydiv);
	}
	
	// just in case there is a third element .. which may contain a message to the user
	// in this case, it may be the error message that the user tried to upload a photo > 2mb
	// october 2008
	try  {
		if (htmlscript[2]==undefined) {
		}
		else {
			alert(htmlscript[2]);
		}
	}
	catch (e) {
	}

}
function clearAllEditValues() {

	doCloseInfoWindow();
	doCloseFormWindow();
	
	startCapture = false;
	
	// make sure the date picker is closed
	try {
	  var pickerDiv = document.getElementById(datePickerDivID);
	  pickerDiv.style.visibility = "hidden";
	  pickerDiv.style.display = "none";
	} catch (e) {}

	
	// reset to the start of the data captuer process
	
	clearCaptureOverlays(); // this gets rid of any geometry and also of any values in pts[]
	
	// reset the cursor on the map just in case!
	document.getElementById("mapinterface").style.cursor="hand";	
	
	// clear all the hidden values
	
	// then setup the first step again
	setupStep1();
	removeDiv('mapoverlay','mapwrapper');	
	
}


	 


function showNextModerateLookup(lookuptablename, lookupid, parenttablename, parentid) {
	//alert("shownextlookupnewparent");
	// in this case, the child record should be deleted from the database first
	// this is a PROPER delete as opposed to a 'delete' requested by the user / moderator, whic
	// only flags the data as deleted

	// so the CANCEL button should only appear for an 'add new' operation


	xhReq = createXMLHttpRequest();
	// remove any dodgy characters
	var postdata = formData("userDataCapture");
	var poststring = stripNonValidXMLCharacters(postdata);
	poststring = poststring.substr(0,poststring.length - 1);
	
	poststring = poststring + "&parenttablename="+parenttablename+"&parentid="+parentid;
	poststring = poststring + "&lookuptablename="+lookuptablename+"&lookupid="+lookupid;
	poststring = poststring + "&action=showNextChildExistingParent";
	
		//var sPath = window.location;
		//var paths = String(sPath).split("=");
		//minisitename = paths[1];
		//minisitename = minisitename.replace("#","");
		minisitename = getMinisiteName();
		poststring = poststring+ "&minisitename="+minisitename

	 xhReq.open("POST", "processEditData.php", true);
	 xhReq.onreadystatechange = getNextModeratedLookup;

	 //
	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	//alert(poststring);
	xhReq.send(poststring);
}


function getNextModeratedLookup() {
   if (xhReq.readyState != 4)  { 
   		return;
   	}
	   
	   var response = xhReq.responseText;
	   //alert(response);
          var htmlscript = response.split("|XX|");
	   if (htmlscript[0].indexOf("No further records") > -1) {
	   	alert("No further records");
	   }
	   else {
		var headID = document.getElementsByTagName("head")[0];
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';

		newScript.text = htmlscript[1];
		headID.appendChild(newScript);
		
//		document.getElementById('opaquetext').innerHTML = htmlscript[0];
//		document.getElementById('opaquetext').innerHTML = htmlscript[0];
//		document.getElementById('buttonArea').innerHTML = generateFooter();
//		document.getElementById('opaquetext').style.height = "70%";
//		document.getElementById('buttonArea').style.height = "20%";
//		document.getElementById('topbuttonArea').innerHTML = generateHeader();

		if (!document.getElementById('describe')) {
			  var ni = document.getElementById('mapwrapper');
			  var newdiv = document.createElement('div');
			  var divIdName = 'describe';
			  newdiv.setAttribute('id',divIdName);
			  newdiv.setAttribute('class','png');
			  ni.appendChild(newdiv);
		}

		// put the black background
		if (!document.getElementById('mapoverlay')) {
			    var ni = document.getElementById('mapwrapper');
			    var overlaydiv = document.createElement('div');
			   var divIdName = 'mapoverlay';
			   overlaydiv.setAttribute('id',divIdName);
			   overlaydiv.setAttribute('class','png');
			   ni.appendChild(overlaydiv);
		}


		document.getElementById('describe').innerHTML = htmlscript[0]+generateFooter();


	   }
}


function getPreviousModeratedLookup() {
   if (xhReq.readyState != 4)  { 
   		return;
   	}
   	
	   var response = xhReq.responseText;
  	//alert(response);
  	var htmlscript = response.split("|XX|");
	   if (htmlscript[0].indexOf("No previous records") > -1) {
	   	alert("No previous records");
	   }
	   else {
		var headID = document.getElementsByTagName("head")[0];
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';

		newScript.text = htmlscript[1];
		headID.appendChild(newScript);
		//document.getElementById('opaquetext').innerHTML = htmlscript[0];
		//document.getElementById('opaquetext').innerHTML = htmlscript[0];
		//document.getElementById('buttonArea').innerHTML = generateFooter();
		//document.getElementById('opaquetext').style.height = "70%";
		//document.getElementById('buttonArea').style.height = "20%";
		//document.getElementById('topbuttonArea').innerHTML = generateHeader();
		
		if (!document.getElementById('describe')) {
			  var ni = document.getElementById('mapwrapper');
			  var newdiv = document.createElement('div');
			  var divIdName = 'describe';
			  newdiv.setAttribute('id',divIdName);
			  newdiv.setAttribute('class','png');
			  ni.appendChild(newdiv);
		}

		// put the black background
		if (!document.getElementById('mapoverlay')) {
			    var ni = document.getElementById('mapwrapper');
			    var overlaydiv = document.createElement('div');
			   var divIdName = 'mapoverlay';
			   overlaydiv.setAttribute('id',divIdName);
			   overlaydiv.setAttribute('class','png');
			   ni.appendChild(overlaydiv);
		}


		document.getElementById('describe').innerHTML = htmlscript[0]+generateFooter();


	   }
}

function showPreviousModerateLookup(lookuptablename, lookupid, parenttablename, parentid) {
	//alert("showpreviouslookup");
	
	// in this case, the child record should be deleted from the database first
	// this is a PROPER delete as opposed to a 'delete' requested by the user / moderator, whic
	// only flags the data as deleted

	// so the CANCEL button should only appear for an 'add new' operation


	xhReq = createXMLHttpRequest();
	// remove any dodgy characters
	var postdata = formData("userDataCapture");
	var poststring = stripNonValidXMLCharacters(postdata);
	poststring = poststring.substr(0,poststring.length - 1);

	poststring = poststring + "&parenttablename="+parenttablename+"&parentid="+parentid;
	poststring = poststring + "&lookuptablename="+lookuptablename+"&lookupid="+lookupid;
	poststring = poststring + "&action=showPreviousChildExistingParent";
	
		//var sPath = window.location;
		//var paths = String(sPath).split("=");
		//minisitename = paths[1];
		//minisitename = minisitename.replace("#","");
		minisitename = getMinisiteName();
		poststring = poststring+ "&minisitename="+minisitename;

	 xhReq.open("POST", "processEditData.php", true);
	 xhReq.onreadystatechange = getPreviousModeratedLookup;

	 //
	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	//alert(poststring);
	xhReq.send(poststring);
}
