var map;
var localSearch = new GlocalSearch();
var poly
var currMarker
var msgGood = "<div class=\"greenTxt\"><strong>Great! You're in our catchment area.</strong></div>Please check that this marker accurately represents your location.<br/>If not, click and hold to drag the marker to the correct location.<br/><br/>Use the + / - buttons on the left to adjust zoom and drag the map to pan.";
var msgBad = "<div class=\"redTxt\"><strong>Sorry!</strong></div>You're not located in the catchment area!<br/>To be notified when we go live in your area <a href=\"#\" target=\"_blank\">please click here...</a>";	
var enforceCatchment = false
var blnIsContained = false




function loadMap(lat,lng,zoom,el,polyPoints, polyLevels) {
  if (GBrowserIsCompatible()) {

	var e = document.getElementById(el)
    map = new GMap2(e);	
	map.setCenter(new GLatLng(lat,lng),zoom);
	map.setUIToDefault();
	map.savePosition() 
	
	
	var polygon1 = new GPolygon.fromEncoded({
	  polylines: [
		{points: polyPoints,
		 levels: polyLevels,
		 color: "#ffbf00",
		 opacity: 1,
		 weight: 2,
		 numLevels: 18,
		 zoomFactor: 2}],
	  fill: true,
	  color: "#ffbf00",
	  opacity: 0.1,
	  outline: true
	});
	map.addOverlay(polygon1);
	
	poly = polygon1

  } else { e.innerHTML = "Your browser is not able to display Google Maps.  Please adjust your settings or upgrade your web browser."
	  
  }
}

function usePointFromPostcode(postcode, callbackFunction) {

	if (postcode!=''){
	
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				//alert(localSearch.results[0].streetAddress)
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point);
			}else{
				alert("Sorry, we're unable to find that location.  Please try again.");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
	}
}

function placeMarkerAtPoint(point)
{	
//remove any existing marker first
	try {
	map.removeOverlay(currMarker) ;
	} catch(e) {}
	
	map.returnToSavedPosition() ;
	
	
	var btn = document.getElementById('Next')
	var fld = document.getElementById('location_OK')
	
	var marker = new GMarker(point, {draggable: true});
	grabLatLong(marker.getLatLng())
	
	 GEvent.addListener(marker, "dragstart", function() {
      map.closeInfoWindow();
	  document.getElementById('markerShifted').value = 1
    });
        GEvent.addListener(marker, "dragend", function() {
			
			
													   
			if (isContained(poly,marker.getLatLng())) {
				btn.disabled=false;
				btn.value='Continue'
				fld.value=1;		
				
				if (blnIsContained==false) {blnIsContained = true;marker.openInfoWindowHtml(msgGood)}
				
				} else {
					btn.disabled=true;
					btn.value='You must be located in the catchment area';
					fld.value=0;
					blnIsContained = false
					}		
			
			var latlng = marker.getLatLng(); 		
			grabLatLong(marker.getLatLng())
			
		});
	
	map.addOverlay(marker);
	
	if (enforceCatchment==true) {			
			//force the user to be located within the catchment area
			if (isContained(poly,point)) {
				var infoHTML = msgGood
				map.setZoom(15);
				btn.disabled=false;
				btn.value='Continue';
				fld.value=1;
				blnIsContained = true
				
			} else {
				var infoHTML = msgBad
				btn.disabled=true;
				btn.value='You must be located in the catchment area';
				fld.value=0;
				map.setZoom(12);
				blnIsContained = false
				}
	} else { 
		
		if (isContained(poly,point)) {
				var infoHTML = msgGood
				map.setZoom(15);
				btn.disabled=false;
				btn.value='Continue';
				fld.value=1;
				blnIsContained = true
				
			} else {
				//tell them if they are not in the catchment area, but let them register anyway
				var infoHTML = "You're not actually located in the catchment area, but it's no big deal!<br/>Please continue registering. When we get the site online for your area,<br/>we'll move your details over automatically."
				map.setZoom(15);
				btn.disabled=false;
				btn.value='Continue';
				fld.value=0;
				blnIsContained = true
				}
	}
	
	marker.openInfoWindowHtml(infoHTML);
	map.setCenter(point);	
	currMarker = marker		
	
} 