function lookupLocation() {
	new Ajax.Request('http://www.oceanovillage.com/geocoder.php',
		{
			method: 'GET',
			parameters: 'location=' + $('location').value,
			onComplete: function(originalRequest) {
				result = eval('(' + originalRequest.responseText + ')');
				$('data[City]').value = result.City;
				$('data[Province or State]').value = result.ProvState;
				$('data[Latitude]').value = result.Latitude;
				$('data[Phone]').focus();
			}
		});
}

function confirmEmail() {
	if ($F('email') != $F('email2')) {
		alert('The email address you entered wasn\'t the same. Please retype your email address.');
		$('email').value = '';
		$('email2').value = '';
		$('email').focus();
	}
}

function confirmZipPostal() {
	var v = $('location').value;
    v = v.toUpperCase();
    var regex = /((^\d{5}([- |]\d{4})?$)|(^[A-Z]\d[A-Z][- |]\d[A-Z]\d$))/;
    if (regex.test(v) || (regex.test(v.substring(0,3) + ' ' + v.substring(3,6)) && v.length == 6)) {
        lookupLocation();
    }
	else if (v != '') {
		lookupLocation();
		alert('Oops! That\'s not a valid zip or postal code. If you live outside of North America you can probably ignore this message, but you may want to double-check the value that you\'ve entered.');
	}
}

Event.observe(window, 'load', function() {
	$('location').observe('blur', confirmZipPostal);
	$('email2').observe('blur', confirmEmail);
});