var find = {
	form: "",
	postcode: "",
	init: function() {
		find.form = id("find");
		find.form.ftPostcode.value     = "Postcode";
		find.form.ftPostcode.className += " disabled";
		find.form.ftPostcode.onfocus = function() {
			this.className = this.className.replace(/disabled/, "");
			if(this.value == "Postcode") this.value = "";
		};
		find.form.ftPostcode.onblur = function() {
			if(this.value == "") {
				this.className += " disabled";
				this.value = "Postcode";
			};
		};		
		find.form.onsubmit = function() {
			find.postcode = find.form.ftPostcode.value;
			
			if(find.postcode !== "" && find.postcode !== "Postcode") {
				// Fix incorectly entered postcodes
				find.postcode = find.postcode.toUpperCase();
				find.postcode = find.postcode.replace(/( O)/, " 0");
			
				// Check for valid postcode
				if(/^[A-Z]{1,2}\d{1,2}[A-Z]? \d[A-Z]{2}$/.test(find.postcode)) {
					find.form.ftPostcode.value = "Searching...";
					var geocoder = new GClientGeocoder();
					if (geocoder) {
						geocoder.getLatLng(
							find.postcode + ", UK",
							function(point) {
								if (!point) {
									document.location = "find-your-club.htm?error=invalid";
								} else {
									document.location = find.form.action + (find.form.action.indexOf("?") > -1 ? "&" : "?") + "lat=" + point.lng() + "&lng=" + point.lat();
								}
							}
						);
					} else {
						document.location = "find-your-club.htm?error=unavailable";
					};
				} else {
					document.location = "find-your-club.htm?error=invalid";
				};
				return false;
			};
		};
	}
};

// Add DOMLoaded functions
window.addDOMLoadEvent(find.init);