function geocoder(){
	
	function getNewCoords() {
		var coords=this.getLatLng();
		form.listing_lat.value=coords.lat();
		form.listing_lng.value=coords.lng();
	}
	
	function geocode() {
		
		if(form.listing_town.value=="") {
			return false;
		} else {
			var addr=(form.listing_add1.value!="")?form.listing_add1.value+", ": "";
			addr+=(form.listing_add2.value!="")? form.listing_add2.value+", ": "";
			addr+=(form.listing_add3.value!="")? form.listing_add3.value+", ": "";
			addr+=form.listing_town.value;
			addr+=(form.listing_region.value!="")? ", "+form.listing_region.value: "";
			addr+=(form.listing_pcode.value!="")? " "+form.listing_pcode.value+" ": " ";
			addr+=(form.listing_country.value!="")?form.listing_country.value: "";
			
			var g=new GClientGeocoder();
        	g.getLatLng(addr, function(point){
  
        		if(!point){
        			
        			var addr2=(form.listing_add2.value!="")? form.listing_add2.value+", ": "";
					addr2+=(form.listing_add3.value!="")? form.listing_add3.value+", ": "";
					addr2+=form.listing_town.value;
					addr2+=(form.listing_region.value!="")? ", "+form.listing_region.value: "";
					addr2+=" "+form.listing_pcode.value+" "+form.listing_country.value;
					
					g.getLatLng(addr2, function(point){
						if(!point) {
							
							var addr3=(form.listing_add1.value!="")?form.listing_add1.value+", ": "";
							addr3=(form.listing_add2.value!="")? form.listing_add2.value+", ": "";
							addr3+=(form.listing_add3.value!="")? form.listing_add3.value+", ": "";
							addr3+=form.listing_town.value;
							addr3+=" "+form.listing_pcode.value+" "+form.listing_country.value;
							
							g.getLatLng(addr3, function(point){
								if(!point) {
									return false;
								} else {
        							map.setCenter(point, 14);
    								marker.setLatLng(point);
    								form.listing_lat.value=point.lat();
    								form.listing_lng.value=point.lng();
        						}
							});

						} else {
        					map.setCenter(point, 14);
    						marker.setLatLng(point);
    						form.listing_lat.value=point.lat();
    						form.listing_lng.value=point.lng();
        				}
					});
        		}else {
        			map.setCenter(point, 14);
    				marker.setLatLng(point);
    				form.listing_lat.value=point.lat();
    				form.listing_lng.value=point.lng();
        		}
			});
		
		}
	
	}
	
	function win(location){
		function useCurrentLocation(){
			//set hidden location fields
			if(location.coords){
				var lat=location.coords.latitude;
				var lng=location.coords.longitude;
			} else {
				var lat=location.latitude;
				var lng=location.longitude;			
			}
			form.listing_lat.value=lat;
    		form.listing_lng.value=lng;
			
			//centre map
			var center=new GLatLng(lat,lng);
			map.setCenter(center,14);
    		marker.setLatLng(center);
    		return false;
		} 
		
		locationDiv=document.getElementById("locationmsg");
		if (locationDiv.hasChildNodes()) {
    		while (locationDiv.childNodes.length>=1) {
       	 		locationDiv.removeChild(locationDiv.firstChild);       
    		} 
		}
		locationInstr=document.createElement("p");
		locationInstrText=document.createTextNode("Your browser has detected your current location. ");
		locationInstrLink=document.createElement("a");
		locationInstrLink.setAttribute("href","#");
		locationInstrLink.setAttribute("id","setcurrentloc");
		locationInstrLinkText=document.createTextNode("Use for this listing.");
		locationInstrLink.appendChild(locationInstrLinkText);
		locationInstr.appendChild(locationInstrText);
		locationInstr.appendChild(locationInstrLink);
		locationDiv.appendChild(locationInstr);
		
		setCurrentLoc=document.getElementById("setcurrentloc");
		setCurrentLoc.onclick=useCurrentLocation;
	}

	function fail(){
		alert("Could not find your location. Sorry!");
	}
	
	var mapDiv=document.getElementById("map");
	mapDiv.style.width="240px";
	mapDiv.style.height="160px";
		
	var map=new GMap2(mapDiv);
	map.addControl(new GSmallMapControl());
	var marker=null;
	var markerOptions={draggable:true};
	
	var form=document.getElementById("addnewform");;
	
	if(form.listing_lat.value!="" && form.listing_lng.value!="") {
	
		var center=new GLatLng(parseFloat(form.listing_lat.value),parseFloat(form.listing_lng.value));
		map.setCenter(center,14);
		marker=new GMarker(center,markerOptions);
		
		map.addOverlay(marker);
		marker.enableDragging();
		GEvent.addListener(marker,"dragend",getNewCoords);

	} else if(form.listing_id) {
		
		var initPoint=new GClientGeocoder();
        	initPoint.getLatLng("London, UK", function(point){
        		if(!point){
        			return false;
        		}else {
        			map.setCenter(point, 14);
        			marker=new GMarker(point,markerOptions);
    				map.addOverlay(marker);
					marker.enableDragging();
					GEvent.addListener(marker,"dragend",getNewCoords);

        		}
			});
		//try geocoding again
		
		geocode();
			
	} else {
	
		//new entry
		var initPoint=new GClientGeocoder();
        	initPoint.getLatLng("London, UK", function(point){
        		if(!point){
        			return false;
        		}else {
        			map.setCenter(point, 10);
        			marker=new GMarker(point,markerOptions);
    				map.addOverlay(marker);
					marker.enableDragging();
					GEvent.addListener(marker,"dragend",getNewCoords);

        		}
			});
		
	
	}
	
	if(geo!=false){
		geo.getCurrentPosition(win,fail,{maximumAge:0});
	}
	
	addEvent(form.listing_add1,"keyup",geocode,true);
	addEvent(form.listing_add2,"keyup",geocode,true);
	addEvent(form.listing_add3,"keyup",geocode,true);
	addEvent(form.listing_town,"keyup",geocode,true);
	addEvent(form.listing_region,"keyup",geocode,true);
	addEvent(form.listing_pcode,"keyup",geocode,true);
	addEvent(form.listing_add1,"blur",geocode,true);
	addEvent(form.listing_add2,"blur",geocode,true);
	addEvent(form.listing_add3,"blur",geocode,true);
	addEvent(form.listing_town,"blur",geocode,true);
	addEvent(form.listing_region,"blur",geocode,true);
	addEvent(form.listing_pcode,"blur",geocode,true);
	addEvent(form.listing_country,"click",geocode,true);

	refresh=document.getElementById("refresh");
	addEvent(refresh,"click",geocode,true);

	
}

addEvent(window,"load",geocoder,true);
addEvent(window,"unload",GUnload,true);
