// Data Export - Added by KC
function downloadData(tablename, userid){
    
  var url = "../includes/generateExportData.php?queryType=layer&approved=1&queryType=layer&export=csv&tablename="+tablename+"&userid="+userid
  
  var newIFrame = document.createElement("iframe");
  newIFrame.src = url;
  newIFrame.style.display = "none";
  document.body.appendChild(newIFrame);
 
};

function downloadKML(tablename, userid){

  var url = "../includes/generateExportKML.php?queryType=layer&approved=1&queryType=layer&export=kml&tablename="+tablename+"&userid="+userid
  //var url = "../includes/mfcPhpRequests.php?mfcAction=processDataRequest&approved=1&queryType=layer&export=kml&tablename="+tablename+"&userid="+userid
  var newIFrame = document.createElement("iframe");
  newIFrame.src = url;
  newIFrame.style.display = "none";
  document.body.appendChild(newIFrame);
}
// END-Export Data

function cancelModeration(){
	doCloseInfoWindow();
	doCloseFormWindow();
	checkAdminUserLoggedIn();
	
	// clear the data from the map
	// and redisplay other data (depending on which layers are switched on or off)
	if (isGoogleMap) {
		resetOverlayGM();
	}
	else {
		resetOverlayOL();
	}
	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);
	}
}
//Mandatory field - Altered by KC
function refuseData(tablename) {
  if (checkData())  {
	var poststring = getPostString(tablename);
	poststring = poststring+"&approved=-1";
	sendModeratedDataToServer(poststring);
    }
}
//Mandatory field - END

function cancelModeratedCapture() {
	if (confirm("Cancel Data Edit?  You will lose all changes.")) {
		clearAllEditValues();
	}
}
function saveData(tablename) {
	if (checkData()) {
		saveToServer(tablename);
	}

}
function getApprovedData() {
	//alert("get approved");
	generateListsForAdmin(1);
}
function getUnApprovedData() {
	//alert("get unapproved");
	generateListsForAdmin(0);
}


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');	
	
}
// Export data - Modified by KC (userid added)
function processAdminResults (resultString,tablename,layername,userid) {
// END - KC
	// we need to put an entry for each result, with a hotlink that moves the map
	// to centre the map on the point
    // gxml no longer exists at version 3
//	alert(resultString);
    var xmlDoc = xmlParse(resultString);
    var root = xmlDoc.documentElement;
  
    if(!root){ //alert("We are sorry but we were unable to download data for: "+ layername); return 0; 
    }  // no document found
    var placemarks = [];
    basename = root.nodeName;
    var bases = basename.split(":");
    if(bases.length>1){basename = bases[1];
    }  // bases length greater than 1
    if(basename == "kml") {	
	var node=root;
	// now process the search results
		var that = this;
		var resultsHTML = "";
		//get the next level node down ..
		node = node.childNodes.item(0);
		for (var ln = 0; ln < node.childNodes.length; ln++) {
			var nextn = node.childNodes.item(ln);
			var nn = nextn.nodeName;
			var nv = nextn.nodeValue;
			
			// only add the result if it has coordinate information with it...
			var coordString = nextn.childNodes.item(2).childNodes.item(0).childNodes.item(0).nodeValue;
			if ((coordString != ",") && (null != coordString)) {
				// this is a POINT or it could be a LINE
				// add the result onto the list
				// add as a text item
				// build up the parameter string to pass to goto result
				// we need the layer name and the coordinates
				var resultName = nextn.childNodes.item(0).childNodes.item(0).nodeValue;
				// now strip the CDATA tags away
				resultName = resultName.substr(9,resultName.length - 9); 
				resultName = resultName.substr(0, resultName.length - 3);

				// now remove the tablename and the ID value
				// this will be used to create the admin page required ...
				resultArray = resultName.split("||");
				var tablename = resultArray[0];
				var resultID = resultArray[1];
				resultName = resultArray[2];

				//a href=\"javascript:gotoLoginPage()\">
				
				resultString = coordString ;
				// check if point or line - if line will have multiple ,
				splitres = coordString.split(",");
				if (splitres.length > 2) {
					resultsHTML = resultsHTML + "<a href=\"#\" onclick=\"gotoLineAdminResult('"+coordString+"','"+ tablename+"',"+resultID+");return false;\">";
				}
				else {				
					resultsHTML = resultsHTML + "<a href=\"#\" onclick=\"gotoAdminResult("+coordString+",'"+ tablename+"',"+resultID+");return false;\">";
				}
				iconstring = tablename;
				resultsHTML = resultsHTML + "<img style=\"width:12px;height:12px\" src='"+getIconLocation(iconstring,false)+"'/>";
				resultsHTML = resultsHTML + "<span style=\"font:12px Helvetica, sans-serif;\">" + resultName+ "</span></a><br>";
			} // coordstring is not null			
			if (null == coordString) { // this is a line or a polygon ...
				//coordString = nextn.childNodes.item(2).childNodes.item(0).nodeValue;
				coordString = nextn.childNodes.item(2).childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.item(0).nodeValue;
				
				// nb in the following line g means replace all occurances ..
				coordString = coordString.replace(/,/g,"||");
				
				var resultName = nextn.childNodes.item(0).childNodes.item(0).nodeValue;
				// now strip the CDATA tags away
				resultName = resultName.substr(9,resultName.length - 9); 
				resultName = resultName.substr(0, resultName.length - 3);

				// now remove the tablename and the ID value
				// this will be used to create the admin page required ...
				resultArray = resultName.split("||");
				var tablename = resultArray[0];
				var resultID = resultArray[1];
				resultName = resultArray[2];
				resultsHTML = resultsHTML + "<a href=\"#\" onclick=\"gotoPolygonAdminResult('"+coordString+"','"+ tablename+"',"+resultID+");return false;\">";
				iconstring = tablename;
				resultsHTML = resultsHTML + "<img style=\"width:12px;height:12px\" src='"+getIconLocation(iconstring,false)+"'/>";
				resultsHTML = resultsHTML + "<span style=\"font:12px Helvetica, sans-serif;\">" + resultName+ "</span></a><br>";
                    }
		} // nodes.childnode.length	
		if (resultsHTML == "") {
			resultsHTML = "No data to edit";
		}
	var divString = tablename + "AdminHTML";
	
//      Data Export - Altered by KC
	document.getElementById(divString).innerHTML = "<h3>" + layername + " <a href=\"#\" onclick=\"downloadKML('"+tablename+"','"+userid+"')\"> kml</a> / <a href=\"#\" onclick=\"downloadData('"+tablename+"','"+userid+"')\"> csv</a></h3>" + resultsHTML ;
//      END - KC
//      
	// now display the search results!
	//document.getElementById("dataAdmin").style.display = "inline";
	
    }  // basename = "kml"
    else {
    	//alert("We are sorry but we were unable to download data for: "+ layername);
    } // basename not kml
}
function setupAdminInstructions() {
	// only do this if they don't already exist ... 
	if (!document.getElementById('admin1')) {
		//var htmlString = "<br><input id=\"admin2\" type=\"radio\" name=\"admin1\" value=\"EditUnApproved\" onclick=\"getUnApprovedData()\" checked=\"true\"><label for=\"admin2\">Edit UnApproved Data</label>";
		//htmlString = htmlString + "<br><input id=\"admin1\" type=\"radio\" name=\"admin1\" value=\"EditApproved\" onclick=\"getApprovedData()\" checked=\"false\"><label for=\"admin1\">Edit Approved Data</label>";
		var htmlString = "<br><input id=\"admin2\" type=\"radio\" name=\"admin12\" value=\"EditUnApproved\" onclick=\"getUnApprovedData()\" checked><label for=\"admin2\">Edit UnApproved Data</label>";
		htmlString = htmlString + "<br><input id=\"admin1\" type=\"radio\" name=\"admin12\" value=\"EditApproved\" onclick=\"getApprovedData()\" ><label for=\"admin1\">Edit Approved Data</label>";
                htmlString = htmlString + "<br><input id=\"admin3\" type=\"radio\" name=\"admin12\" value=\"Export\" onclick=\"XXXX()\" ><label for=\"admin3\">Download Data</label>";
                htmlString = htmlString +"<div id=\"adminProcess\">";
		htmlString = htmlString + "<div style = \"font-family: arial, sans-serif;font-size: small;padding-bottom:.7em;\">";
		htmlString = htmlString +"<p>1.  Click on a Link to zoom to the data</p>";
		htmlString = htmlString + "<p>2.  Click on the object to edit the data</p>";
		htmlString = htmlString + "<p>Note: it is not possible to move objects on the map.  You must delete and recreate the object.  All other data has been switched off.</p>";
		htmlString = htmlString + "</div>";
		htmlString = htmlString + "</div> <!-- adminProcess -->";
		document.getElementById('adminInstructions').innerHTML = htmlString;
	}
}




/*===============================*/
// SPLIT DIRECTION DEPENDING ON MAP PLATFORM
function gotoAdminResult(lng,lat,tablename, resultID) {
	if (isGoogleMap) {
		gotoAdminResultGM(lng,lat,tablename, resultID);
	}
	else {
		gotoAdminResultOL(lng,lat,tablename, resultID);
	
	}
}

function gotoPolygonAdminResult(coordString,tablename, resultID) {
	if (isGoogleMap) {
		gotoPolygonAdminResultGM(coordString,tablename, resultID);
	}
	else {
		gotoPolygonAdminResultOL(coordString,tablename, resultID);
	}
}


function gotoLineAdminResult(coordString,tablename, resultID) {
	if (isGoogleMap) {
		gotoLineAdminResultGM(coordString,tablename, resultID);
	}
	else {
		gotoLineAdminResultOL(coordString,tablename, resultID);
	}
}
