var localSearch;
var localSearchAuto;



function automaticallySetLatLong(){
	postCodeIn = document.getElementById('GoogleMapPostCodeValueIn').value;
	latitudeIn = document.getElementById('LatitudeIn').value;
	longitudeIn = document.getElementById('LongitudeIn').value;
	postCodeColumnIn = document.getElementById('GoogleMapPostCodeColumnIn').value;
	postCode = '';	
	if( document.getElementById(postCodeColumnIn)){
		postCode = document.getElementById(postCodeColumnIn).value;
		lat = document.getElementById('Latitude').value;
		long = document.getElementById('Longitude').value;
	}	
	
	localSearchAuto  = new GlocalSearch();
	
	localSearchAuto.setSearchCompleteCallback(null, 
		function() {
			if (localSearchAuto.results[0])	{	
				lat = document.getElementById('Latitude');
				long = document.getElementById('Longitude');
				lat.value =  localSearchAuto.results[0].lat;
				long.value = localSearchAuto.results[0].lng;
				document.getElementById('AutoApply').value = 1;
				document.updateForm.submit();
			}
			else{
				document.getElementById('AutoApply').value = 1;	
				document.updateForm.submit();
			}
		});		
	
	//If post code 
	if(postCode){
		if(postCodeIn){
			if(postCode != postCodeIn){
				if(latitudeIn == lat && longitudeIn == long){			
					localSearchAuto.execute(postCode + ", UK");
					return false;
				}
			}	
		}
		else{
			localSearchAuto.execute(postCode + ", UK");
			return false;
		}
	}
}


function getPostcode(postcodeColumn) {
	
	var postcodeObj = document.getElementById(postcodeColumn);
	var postcode = postcodeObj.value;
	
	localSearch = new GlocalSearch();
	
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0])	{	
				lat = document.getElementById('OverrideInitialLatitude');
				long = document.getElementById('OverrideInitialLongitude');
				lat.value =  localSearch.results[0].lat;
				long.value = localSearch.results[0].lng;
				if(lat.value && long.value){
					//alert(lat.value + " " + long.value);					
					document.mapPostcodeForm.submit();
				}
				else{
					alert("Postcode not found!");
				}	
			}
			else{
				alert("Postcode not found!");
			}
			
		});	
	
	if(postcode){
		localSearch.execute(postcode + ", UK");
	}	
	else{
		alert('Post Code Required');
	}
}

function setLatLngByCrosswire(){
	  
	if (GBrowserIsCompatible()) {
      	var map = new GMap2(document.getElementById("memberMap"));
		
		
		// Create our centre marker icon
		crossIcon = new GIcon();
		crossIcon.image = iconUrl;
		
		crossIcon.iconSize = new GSize(9, 9);
		crossIcon.iconAnchor = new GPoint(5,5);
		//crossIcon.infoWindowAnchor = new GPoint(5, 1);
		
				
		//Add std zoom and map type contrlols
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());		

		//Position map on page
	 	map.setCenter(new GLatLng(latitude, longitude), 15);
	 	
	 		
		//Add centre marker
		var center = map.getCenter();
		if(showCentreCross){
			map.addOverlay(new GMarker(center, crossIcon));
		}
		
		//Add listner function and display lat and long
		GEvent.addListener(map, "move", function() {
  			map.clearOverlays()
			
			//Add centre marker
			var center = map.getCenter();
			if(showCentreCross){
				map.addOverlay(new GMarker(center, crossIcon));
			}
			
			//Add back location marker
			var point = new GLatLng(latitude, longitude);
 			map.addOverlay(new GMarker(point));
			
  			//Show lat and long
			if(showLatLong){
				if(document.getElementById("mapMessage")){
					document.getElementById("mapMessage").innerHTML = "Centre Latitude, Longitude " +center.toString();
				}	
			}	
				//Store Updated Latitude and Longitude
			if(document.getElementById("Latitude") && document.getElementById("Longitude")){						
				document.getElementById("Latitude").value = center.lat();
	  			document.getElementById("Longitude").value = center.lng();
			}				
		});
		
		//Add location marker
		var point = new GLatLng(latitude, longitude);
 		map.addOverlay(new GMarker(point));

	}	
}
