var geocoder;
function setLocation(point)
{
	if (!point)
	{
		alert(address + " not found");
	}
	else
	{
		$('lat').value = point.lat();
		$('lng').value = point.lng();
		$('locatorForm').submit();
	}
}

function checkFormFields()
{
	if (!$('type').value && ($("zip").value == '' || $("zip").value == 'Zip'))
	{
		alert ('Please, select the type of location you are looking for or enter a zip code of your location');
		return false;
	}

	if ($("zip").value != '' && $("zip").value != 'Zip' && !$('distanceId').value)
	{
		alert('Please, select a search radius');
		return false;
	}
	return true;

}


function loadScript(callback) {
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps?file=api&v=2.x&key="+googleAPIKey+"&async=2&callback="+callback;
	document.body.appendChild(script);
}

function getLocation ()
{
	if (!checkFormFields()) return false;
	if ($("zip").value == '' || $("zip").value == 'Zip')
	{
		$0('#zip.blur').val('');//empty fields whose default values were not changed
		return true;
	}
	if ($0.browser.safari && $0.browser.version < 419)
	{
		alert('Your browser does not support Google Maps feature.');
		return false;
	}
	loadScript('getLatLng');
	return false;
}

function getLatLng()
{
	geocoder = new GClientGeocoder();
	address = '';
	address += $('zip').value == '' ? '' : (address == '' ? $('zip').value : ', ' + $('zip').value);
	geocoder.getLatLng(address, setLocation);
	return false;
}
