var step2html="Step 2";
var step1html="Step 1";
var step3postcodehtml="Step 3 postcode";
var step3maphtml="";
var captureUserDatahtml = "capture userdata";
var geocoder = null;
var userPolyline = null;
var userPoint = null;
var userPolygon = null;

var userLoginReq; // httpxml to log the user in ...

var myUsername = "";

var xhReq; // the http xml request to see if the user is logged in or not!
var xhAdminReq; // the http xml request to see if the user is logged in as an administrator or not!
var xhReqData; // the http xml request to download the correct form for data entry

var thePoint;
var theLine;
var thePolygon;
var data_capture_pts = [];
var capturePointArray = [];
var dataCaptureListener;



function drawCaptureDataGM() {
	if (startCapture == true) {
		if (capturePointArray.length > 0) {
			for (var i =0; i< capturePointArray.length; i++ ){
				//	map.addOverlay(capturePointArray[i]);
				capturePointArray[i].setMap(map);
			}
		}
	}
}

function clearCaptureOverlaysGM() {
	if (capturePointArray.length > 0) {
		var arrayLen = parseInt(capturePointArray.length);
		var minusPt = arrayLen - 1;

		for (var xx = minusPt;xx >=0; xx--){
			theOverlay = capturePointArray[xx];
			theOverlay.setMap(null);
		}

		for (var xx = minusPt;xx >=0; xx--){
			theOverlay = capturePointArray[xx];
			theOverlay.setMap(null);
			capturePointArray.pop();
		}
	}
	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 captureDataGM() {
	var pointIconLocation = getIconLocation(contentLayer);
	var linecolour = getColour(contentLayer);
	var pointIcon = new google.maps.MarkerImage(pointIconLocation,
        new google.maps.Size(20, 20),
        new google.maps.Point(0,0),
        new google.maps.Point(10, 10));
	// clear any pre-existing data and cancel the existing listener
	if (data_capture_pts.length > 0) {
		// clear capture 
		clearCaptureOverlaysGM();
	}
	try {
		//GEvent.removeListener(dataCaptureListener);
		google.maps.event.clearListeners(map, 'click');
	} catch (e) {}
	// start the listener
	//dataCaptureListener = GEvent.addListener(map,"click", function(overlay,point) {
    	dataCaptureListener = google.maps.event.addListener(map,'click',function(event) {
			point = event.latLng;
			if (document.getElementById('feature_type1').checked ) {
				// create a point where teh user has clicked
				// remove the point
				if (data_capture_pts.length > 0) {
					thePoint.setMap(null);
					data_capture_pts.pop(thePoint);
					capturePointArray.pop(thePoint);
				}
				thePoint = new google.maps.Marker({position:event.latLng, icon: pointIcon});
				thePoint.setMap(map);
				capturePointArray.push(thePoint);
				data_capture_pts.push(point);
			} // points
			if (document.getElementById('feature_type2').checked ) {
				data_capture_pts.push(point);

				// clear the line as it will be redrawn
				// NB: POP REMOVES THE LAST ELEMENT OF THE ARRAY!
				if (data_capture_pts.length > 2) {
					theLine.setMap(null);
					capturePointArray.pop(theLine);
				}
				thePoint = new google.maps.Marker({position:point, icon: pointIcon});
				thePoint.setMap(map);
				capturePointArray.push(thePoint);

				// if there is more than one point, then also draw the line that jons them
				if (data_capture_pts.length > 1) {
					theLine = new google.maps.Polyline({path:data_capture_pts,strokeColor:linecolour,strokeWeight:4,strokeOpacity:1});
					theLine.setMap(map);
					capturePointArray.push(theLine);
				}
			} // lines
			if (document.getElementById('feature_type3').checked ) {
					data_capture_pts.push(point);

					// REMOVE THE OLD DATA FIRST, AS POP REMOVES THE LAST ELEMENT OF AN ARRAY
					if (data_capture_pts.length == 3) {
						// remove the line as having 3 points makes a triangle so polygon will be drawn not a line
						theLine.setMap(null);
						capturePointArray.pop(theLine);
					}
					if (data_capture_pts.length > 3) {
						//remove the polygon as it will be redrawn
						thePolygon.setMap(null);
						capturePointArray.pop(thePolygon);
					}

					// DRAW THE POINT - ALWAYS
					thePoint = new google.maps.Marker({position:point, icon: pointIcon});
					thePoint.setMap(map);
					capturePointArray.push(thePoint);

					// if there is more than one point, then also draw the line that jons them
					if (data_capture_pts.length == 2) {
						// draw a line for the two points
						theLine = new google.maps.Polyline({path: data_capture_pts,strokeColor:linecolour,strokeWeight:4,strokeOpacity:1});
						theLine.setMap(map);
						capturePointArray.push(theLine);
					}

					if (data_capture_pts.length > 2 ) {
						// draw the polygon
						thePolygon = new google.maps.Polygon({paths:data_capture_pts,strokeColor: linecolour,strokeWeight:3,strokeOpacity:1,fillColor:linecolour,fillOpacity:0.2});
						thePolygon.setMap(map);
						capturePointArray.push(thePolygon);
					}

			} // polygons

			} // end of the click function
    	); // end of the map listener

	// now change the point on the map ...
	map.draggableCursor = "crosshair";


}

function processClearMapDataGM() {
	// we are starting step 3 map again
	startCapture = false;
	//GEvent.removeListener(dataCaptureListener);
	google.maps.event.clearListeners(map, 'click');
	clearCaptureOverlaysGM();
	setupStep3Map();
}


function processStep3MapGM() {	
	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]);
	}
	
	//document.getElementById("mapinterface").style.cursor="hand";
	//map.draggableCursor = "pointer";
	map.draggableCursor = "auto";
	return true;
}


function clearMapClickListenerGM() {
	google.maps.event.clearListeners(map, 'click');
}


function reenableMapGM() {
	//map.enableDragging();
	//map.enableDoubleClickZoom();
	//map.enableContinuousZoom();

	startCapture = false;
	
	// disable the listener
	//document.getElementById("map-canvas").style.cursor="hand";
	//map.draggableCursor = "hand";
	map.draggableCursor = "auto";
	// remove the tooltip
	    if(document.getElementById('tooltip')) {
		document.getElementById('tooltip').style.display = 'none';
	    }
	try {
		//GEvent.removeListener(dataCaptureListener);
		google.maps.event.clearListeners(map, 'click');
	}
	catch (e) {
	}

}


/*function createMarkerGM(point, text, title) {
  var marker = new GMarker(point,{title:title});
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(text);
  });
  return marker;
}*/


function resetCursorGM() {
		map.draggableCursor = "hand";
}


function getPointsStringGM(data_capture_pts) {
	var pointsstring = "";
	for (var i=0;i< data_capture_pts.length ; i++) {
		pt = data_capture_pts[i];
		pointsstring = pointsstring + pt.lng() + " "+pt.lat()+ ",";
	}
	//remove the last comma from pointstring
	pointsstring = pointsstring.substr(0,pointsstring.length -1);
	return pointsstring;
}

/*function drawLineGM() {
		var userPolylineString = "new GPolyline([";
		var elSel = document.getElementById('Select1[]');
		for (var i = 0; i < document.getElementById("Select1[]").length; i++) {
			var coordinate=elSel.options[i].text.split(" ");
			userPolylineString = userPolylineString + " new GLatLng(" +coordinate[1] + ","+ coordinate[0]+")";
			if (i < document.getElementById("Select1[]").length - 1) {
				userPolylineString = userPolylineString + ",";
			}
		}
		var polyline = new GPolyline([new GLatLng(51.51, 0.08),new GLatLng(51.50, 0.07)], "#FF0000", 5);
		map.addOverlay(polyline);
	
}

function drawPointGM(latStart,longStart) {
        var marker = createMarker(new GLatLng(latStart,longStart),'', '');
        map.addOverlay(marker);
}*/



