function lookupLocation() {
	new Ajax.Request('http://www.centreforsleep.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[Phone]').focus();
			}
		});
}

function confirmEmail() {
	if (!confirm("You entered: " + $('email').value + "\n\nIs this correct?")) {
		$('email').select();
	}
}

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);
	$('email').observe('blur', confirmEmail);
});