/*
DATA CAPTURE REQUIREMENTS


1.  The following variables are used by all the data capture code:

a) data_capture_pts - stores all the points that the user has clicked on the map, as a list of POINTS only. This list of points is then converted into 
text format for upload to the server.    The points


How data capture works for the geometry:
1.  the user clicks the first point on the map
2.  a point object is created and added to data_capture_pts array
2b. a point object is also added to the capturePointArray, and this point is displayed on the map

(if they are capturing a line or polygon)
3.  the user clicks on the map for a second time
4.  this second point object is created, and added as a point object to data_capture_pts
4b. a line object is created.  the original point object is removed from capturePointArray and replaced by the line object, which is drawn on the map

5.  the user clicks for a 3rd time - 
6.  the third point is added to data_capture_pts as a point
7.  a polygon or line object is created (depending on what they chose) and then displayed on the map and added to capturePointArray

*/

function drawCaptureDataOL() {
	// take the capturePointArray, which stores the point/line/polygon that the user has clicked on the map, and draw these on the map
	/* ***********************
	CODE REQUIRED HERE
	***********************  */

}

function clearCaptureOverlaysOL() {

	// clear the capturePointArray and remove all the capture data from the map
	// also clear the data_capture_pts
	// this is called either when the user cancels the data capture or clicks 'back' to go back a step

	if (capturePointArray.length > 0) {
		// remove the points in here
		// from capturePointArray
		// first remove them from the map
		// and then from the array itself
		/* ***********************
		CODE REQUIRED HERE
		***********************  */
	}
	if (data_capture_pts.length > 0) {
		for (var j = parseInt(data_capture_pts.length) - 1; j >=0; j=j-1) {
			data_capture_pts.pop(data_capture_pts[i]);
		}
	}
	data_capture_pts = [];
	capturePointArray = [];
}


function captureDataOL() {
	// this function creates the LISTENER for the click event on the map 
	// when the user is drawing a new point, line or polygon
	/* ***********************
	CODE REQUIRED ALL THROUGH THIS FUNCTION!!!
	***********************  */

	
	// get hold of the icon for the layer, and also the colour required for lines and polygons
	var pointIconLocation = getIconLocation(contentLayer);
	var linecolour = getColour(contentLayer);
	
	// create the point icon

	// clear any pre-existing capture data from capturePointArray
	// cancel the existing listener
	
	
	// change the cursor on the map to a point ...
	
	// set up the listener that enables the click event on the map to be
	// convered into a point/line/polygon
	mapClickListenerOL = function () {
		// the code to create the new points, lines or polygons on the map
		// nb - this code need to populate the capturePointArray

		// now process the actual click event, depending on what the user has clicked on 
		if (document.getElementById('feature_type1').checked ) {
			// create a point object where the user has clicked
			// add this point to capturePointArray
			// add this point to data_capture_points

		} // points
		if (document.getElementById('feature_type2').checked ) {
			// add the point to data_capture_pnts
			data_capture_pts.push(point);

			// clear capturePointArray from all the previous objects


			// if there is more than one point, then also draw the line that jons them
			if (data_capture_pts.length > 1) {
				// loop through data_capture_pnts and create a line object from the points in the list


				//add the resulting line to capturePointArray
			}
		} // lines
		if (document.getElementById('feature_type3').checked ) {
			// the user wants to draw a polygon
			// add the point to data_capture_pnts
			data_capture_pts.push(point);


			// clear capturePointArray from all the previous objects
			// if there is more than one point, then  draw the line that jons them
			if (data_capture_pts.length == 2) {
				// add the line to capturePointArray

			}
			// if there are 3 points or more, then draw a polygon rather than the line
			if (data_capture_pts.length > 2 ) {
				// add to capturePointArray
			}

		} // polygons
		
	}; // end of the map listener
}

function processClearMapDataOL() {

	// we are starting step 3 map again
	// so need to do the following steps
	// 1.  set startCapture to false so that the system knows we are not drawing on the map
	
	startCapture = false;
	
	// 2.  then remove any 'onclick' listeners on the map object itself
	//e.g. google.maps.event.clearListeners(map, 'click');
	clearMapClickListenerOL();
	
	// then clear any data that has been drawn on the map
	clearCaptureOverlaysOL();
	
	// finally reset the text on the 'add new' tab 
	setupStep3Map();

}

function processStep3MapOL() {
	// this function is called once the user clicks 'NEXT' to move on to capturing data on the form
	// it validates whether the user has clicked on the map and drawn something appropriate to what they
	// selected - i.e. a point line or polygon
	
	
	if (data_capture_pts.length == 0) {
		alert("Please draw on the map");
		return false;
		//return 0;
	}
	if (document.forms["processdata"].feature_type[0].checked ) {
		//document.forms["processdata"].selected_geometrytype.value = "point";
		selectedGeometryType = "point";
		// capturing a point
		if (data_capture_pts.length != 1) {
			alert("Please draw a single point on the map");
			return false;
		}
	}
	if (document.forms["processdata"].feature_type[1].checked ) {
		//document.forms["processdata"].selected_geometrytype.value = "line";
		selectedGeometryType = "line";
		// capturing a line
		
		if (data_capture_pts.length < 2) {
			alert("Please draw a line on the map - you need at least 2 points");
			return false;
		}
	}
	if (document.forms["processdata"].feature_type[2].checked ) {
		//document.forms["processdata"].selected_geometrytype.value = "polygon";
		selectedGeometryType = "polygon";
		// capturing a polygon
		if (data_capture_pts.length < 3) {
			alert("Please draw a polygon on the map - you need at least 3 points");
			return false;
		}
		// add the first point again to close the polygon
		data_capture_pts.push(data_capture_pts[0]);
	}
	
	//map.draggableCursor = "pointer";
	/* ***********************
	CODE REQUIRED HERE - change the cursor back to the normal hand
	***********************  */
	return true;
}

function clearMapClickListenerOL() {
	// remove the map click listener - we are restarting data capture
	/* ***********************
	CODE REQUIRED HERE
	***********************  */
}

function reenableMapOL() {
	// have finished data capture, reset the map to normal viewing
	startCapture = false;
	clearMapClickListenerOL();
	resetCursorOL();
}



function resetCursorOL() {
	// reset the map cursor from a point/cross hair to the usual hand
	/* ***********************
	CODE REQUIRED HERE
	***********************  */
	
}


function getPointsStringOL(data_capture_pts) {
	// generate a string of the point data, in a format that will be sent to the server
	// as part of the 'add data' routine.

	for (var i=0;i< data_capture_pts.length ; i++) {
		pt = data_capture_pts[i];
		// the string should take the format of lng[space]lat,
		// i.e. lng coordinate, then a space, then the lat coordinate, then a comma
		// e.g. for google maps:	pointsstring = pointsstring + pt.lng() + " "+pt.lat()+ ",";
		/* ***********************
		CODE REQUIRED HERE
		***********************  */

	}
	//remove the last comma from pointstring
	pointsstring = pointsstring.substr(0,pointsstring.length -1);
	return pointsstring;
}


