//var answer=-1;  // keeps track of whether the user wants to see only unmoderated data
//function displayListToModerate() {
//
//  if (xhReq.readyState != 4)  { 
//   		return;
//   	}
//   var serverResponse = xhReq.responseText;
//   alert(serverResponse);
//   processAdminResults(serverResponse);
// }

function processAdminResults (resultString,tablename,layername) {
	
// we need to put an entry for each result, with a hotlink that moves the map
// to centre the map on the point

    //alert(resultString);
   
    var xmlDoc = GXml.parse(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 ;
				
				  
				  //  map.addOverlay(m);
								
//				  var html1 = createInfo(this.layername,taggedName,desc);
				  
				
				// 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];
				//resultString = coordString ;
				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>";

				
				
				//for (var ln1 = 0; ln1 < nextn.childNodes.length; ln1++) {
				//		alert("ln1" + ln1);
				//		var nextn1 = nextn.childNodes.item(ln1);
				//		var nn1 = nextn1.nodeName;
				//		alert(nn1);
				//		if (nn1 == "Polygon") {
				//		   for (var ln2 = 0; ln2 < nextn1.childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.length; ln2++) {
				//		   	alert("ln2"+ln2);
				//			var nextn2 = nextn1.childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.item(ln2);
				//			var nn2 = nextn2.nodeValue;
				//			alert(nn2);
				//		    } //ln2
				//
				//		}  //= polygon
				//}
				
			}
		} // nodes.childnode.length	
		if (resultsHTML == "") {
			resultsHTML = "No data to moderate";
		}
	//alert(tablename);
	var divString = tablename + "AdminHTML";
	document.getElementById(divString).innerHTML = "<h3>"+ layername + " </h3>" + resultsHTML;
	
	// 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 showAdminResult(tablename, resultID) {
	 xhReq = createXMLHttpRequest();
	 	 //var sPath = window.location;
	 	 //var paths = String(sPath).split("=");
	 	 //minisitename = paths[1];
	 	 minisitename = getMinisiteName();

	 var poststring = "action=moderate&tablename="+tablename+"&resultID="+resultID+"&minisitename="+minisitename;
	 //alert(poststring);
	 xhReq.open("POST", "processData.php", true);
	 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.onreadystatechange = showDataToModerate;
	 xhReq.onreadystatechange = showform;
	 
	 
	 /*====================================================*/
	 /* TEMPORARY CODE - IT TAKES A WHILE TO GENERATE THE FORM SO SHOW THE BLANK SCREEN SO THAT THE USER KNOWS SOMETHING IS HAPPENING*/
	 	document.getElementById('opaquetext').innerHTML = "<div style=\"padding-left:40px;left:150px;\"><h2>Please wait - retrieving data</h2><br>This may take a little time ...</div>";
	 	document.getElementById('container2').style.left = "0px";
	 	document.getElementById('container2').style.top = "0px";
	 	document.getElementById('topbuttonArea').innerHTML = "";
	 	document.getElementById('buttonArea').innerHTML = "";
	 	document.getElementById('container2').style.width = document.getElementById('map-canvas').style.width;
	 	document.getElementById('container2').style.height = "100%";
	 	document.getElementById('container2').style.display = "block";

	/*====================================================*/
	 xhReq.send(poststring);
}
/*function showDataToModerate() {
  if (xhReq.readyState != 4)  { 
   		return;
   	}
   		var serverResponse = xhReq.responseText;
   		ale
   		document.getElementById('opaquetext').innerHTML = "";
      		document.getElementById('container2').style.left = "0px";
		document.getElementById('container2').style.top = "0px";
		document.getElementById('container2').style.width = document.getElementById('map-canvas').style.width;
		document.getElementById('container2').style.height = "100%"; //document.getElementById('map-canvas').style.height;

		// split the server response to remove the javascript out ...
		// and to only add the buttons etc to the form
		
		splitResponse = serverResponse.split("|XX|");
		var headID = document.getElementsByTagName("head")[0];
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.text = splitResponse[1];
		headID.appendChild(newScript);



  var theWdth = parseInt(document.getElementById('map-canvas').style.width);
   theWdth = theWdth * (80/100);


//var  html = "<div style=\"height:85%\">";
//html += "<INPUT TYPE=\"image\" SRC=\"../images/addline.png\" VALUE=\"Close\"";
//html += "ALT=\"Close\" title=\"close\" NAME=\"Close\" onclick=\"doCloseInfoWindow()\" style=\"height:20px;width:20px;float:right;\">"; 
//  html += "<div style=\"height:5px;width:100%\">";
//  html += "</div>"; // end of the gap div at the top of the table
  html = splitResponse[0];

                document.getElementById('opaquetext').innerHTML = html;
                document.getElementById('container2').style.display = "block";

}*/
function gotoAdminResult(lng,lat,tablename, resultID) {
	
	//alert(lng,lat);
	doCloseInfoWindow()
	// zoom in rather than stay at current zoom
	var point = new GLatLng(parseFloat(lat),parseFloat(lng));

	var icon = getCustomIcon(tablename, false) ;
	  var markeroptions = {};
	  if (!markeroptions.icon) {
	    markeroptions.icon = icon;
	  }
	  //var m = new GMarker(point, markeroptions);
	  theAdminPoint = new GMarker(point, markeroptions);
	  //var m = new GMarker(point);
	  // strip the header texts from the name string

	map.setCenter(new GLatLng(lat, lng), 15);
	map.addOverlay(theAdminPoint);
	
	
	GEvent.addListener(theAdminPoint, "click", function() {
	    if (startCapture == false ) {
		showAdminResult(tablename, resultID);	
	     } // startcapture = false
	}); // end of the listener function

}

function gotoPolygonAdminResult(coordString,tablename, resultID) {
	
	
	coordString = coordString.replace(/ /g,"||");
	// now split into an array of long then lat ...
	var coordArray = coordString.split("||");
	
	// then create an array of points from this array using the lats first then the longs ..
	var adminpoints = [];
	
	// temp move the map first ...
	map.setCenter(new GLatLng(coordArray[1],coordArray[0]),15);
	
	for (var i = 0;i< coordArray.length;i=i+2){
		//alert(i);
		var point = new GLatLng(coordArray[i+1],coordArray[i]);
		var tempMarkerPoint = new GMarker(point);
		//var gPoint= new GPoint(coordArray[i+1],coordArray[i]);
		adminpoints.push(point);
		map.addOverlay(tempMarkerPoint);
	}
	
	
	//theAdminPolygon = new GPolygon(adminpoints,"#ff0000",); //,linecolour,3,1,linecolour,0.2);
	
	var theColour = getColour(tablename);
	//alert(theColour);
	//alert(tablename);
	//theAdminPolygon = new GPolygon(adminpoints,"#ff0000",3,1,"#ff0000",0.2);
	theAdminPolygon = new GPolygon(adminpoints,theColour,3,1,theColour,0.2);
	
	//alert("pre add listener");
	GEvent.addListener(theAdminPolygon, "click", function() {
	    if (startCapture == false ) {
		showAdminResult(tablename, resultID);	
	     } // startcapture = false
	}); // end of the listener function

	
	map.setCenter(theAdminPolygon.getBounds().getCenter());
	var theBounds = theAdminPolygon.getBounds();
	var theZoom = map.getBoundsZoomLevel(theBounds);
	//map.setZoom(theAdminPolygon);
	map.setZoom(theZoom);
	//alert("pre info close");
	try {
		doCloseInfoWindow();
	}
	catch ( e) {
	}
	
	map.addOverlay(theAdminPolygon);

}



function generateListsForModeration(is_admin) {
	
	 xhReq = createXMLHttpRequest();
	 
	 // pass the name of the minisite ...
	 //var sPath = window.location;

	 //var paths = String(sPath).split("=");
	 //var minisitename = paths[1];
	 minisitename = getMinisiteName();
	 xhReq.open("GET", "generateModerationList.php?minisitename="+minisitename, true);
	 
	 xhReq.onreadystatechange = showModerationList;
	 xhReq.send(null);
}
function showModerationList() {
  if (xhReq.readyState != 4)  { 
   		return;
   	}
  		var serverResponse = xhReq.responseText;
  		//alert(serverResponse);
		var headID = document.getElementsByTagName("head")[0];
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.text = serverResponse;
		headID.appendChild(newScript);


		// if there is already some data then find out what the user was doing previously
		if (document.getElementById('admin1').checked == true) {
			generateListsForAdmin(1);
		}
		else {
			// this is either the first go or the edit unmoderated option is checked
			generateListsForAdmin(0);
		}
}
function gotoLineAdminResult(coordString,tablename, resultID) {
		
	//alert(coordString);
	coordString = coordString.replace(/ /g,"||");
	// now split into an array of long then lat ...
	//alert(coordString);
	var coordArray = coordString.split("||");
	
	// then create an array of points from this array using the lats first then the longs ..
	var points = [];
	
	// temp move the map first ...
	
	
	//alert("pre loop");
	
	for (var i = 0;i< coordArray.length;i=i+1){
		//alert(i);
		//alert(coordArray[i+1]);
		//alert(coordArray[i]);
		var temppoints = coordArray[i].split(",");
		//var point = new GLatLng(coordArray[i+1],coordArray[i]);
		var point = new GLatLng(temppoints[1],temppoints[0]);
		//theMarkerPoint = new GMarker(point);
		//var gPoint= new GPoint(coordArray[i+1],coordArray[i]);
		points.push(point);
		//map.addOverlay(theMarkerPoint);
	}
	
	//map.setCenter(new GLatLng(coordArray[1],coordArray[0]),15);
	var theColour = getColour(tablename);
	//alert(theColour);
	//alert(tablename);
	//theAdminLine = new GPolyline(points,"#ff0000"); //,linecolour,3,1,linecolour,0.2);
	theAdminLine = new GPolyline(points,theColour); //,linecolour,3,1,linecolour,0.2);
	
	//alert("pre add listener");
	GEvent.addListener(theAdminLine, "click", function() {
	    if (startCapture == false ) {
		showAdminResult(tablename, resultID);	
	     } // startcapture = false
	}); // end of the listener function

	map.setCenter(theAdminLine.getBounds().getCenter());
	var theBounds = theAdminLine.getBounds();
	var theZoom = map.getBoundsZoomLevel(theBounds);
	map.setZoom(theZoom);
	//alert("pre info close");
	try {
		doCloseInfoWindow();
	}
	catch ( e) {
	}
	
	map.addOverlay(theAdminLine);
	// zoom in rather than stay at current zoom
}


function setupAdminInstructions() {

//alert("admin instr");
// 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 +"<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;
}
}
