//function performs geo-code on location string in format: Sea Point, Cape Town, Western Cape, South Africa
//if fails then strips text before 1st '1' and tries again
//when finished calls function geocodeSuburbResultHandler (which is to be defined in another js file) with obtained latLng
//  - this is done to enable this method to be called from editUnitGoogleMaps.js & unitGoogleMaps.js
debug=false;
function geocode(locationString, tryShortened, alertsOnFail) {
    if(debug) alert('geocoding address: ' + locationString);
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( {'address': locationString}, function(results, status) {
	if (status == google.maps.GeocoderStatus.OK) {
	    geocodeResultHandler(results[0].geometry.location);
	} else {
	    if(debug) alert('geocode result length = ' + results.length);
	    //alert('geocode result location_type = ' + results[0].geometry.location_type);
	    if(tryShortened && results[0].geometry.location_type!=google.maps.GeocoderLocationType.APPROXIMATE) {
		var withoutSuburb = locationString.substring(locationString.indexOf(",")+1)
		if(debug) alert("trying without suburb: " + withoutSuburb);
		geocoder.geocode( {'address': withoutSuburb}, function(results, status) {
		    if (status == google.maps.GeocoderStatus.OK) {
                        if(debug) alert("without suburb success");
			geocodeResultHandler(results[0].geometry.location);
		    } else {
			if(alertsOnFail) { alert("Geocode was not successful for the following reason: " + status); }
		    }
		});
	    } else {
		if(debug) alert("Geocode was not successful for the following reason: " + status);
	    }
	    
	}
    });
}

