(function() {
	$$("input.ajax-complete-office").each(function(el) {
		div = Builder.node("div", {"class":"ajax-search-results", "style":""});
		if (el.id)
			div.id = "as-"+el.id;
		//el.insert({after:div});
		document.body.appendChild(div);
		var parameters = "field="+escape(el.getAttribute("rel"))+"&show_type="+Number(Boolean(el.hasClassName("ac-show-type")));
		if (el.getAttribute("rev"))
			parameters += "&parameters="+el.getAttribute("rev");
		new Ajax.Autocompleter(el, div, "/ajax/search-autocomplete-office", {
			paramName: "value",
			parameters: parameters,
			updateElement: function(li) {
				el.value = li.down("span.data").firstChild.nodeValue;
				if (el.hasClassName("ac-set-name"))
					el.name = li.getAttribute("rel");
			},
			select: "result",
			minChars: 1
		});
	});

	$$("div#faq-questions h2").each(function(el) {
		var next = el.next();
		var answer_nodes = [];
		while (next) {
			if (!next)
				break;
			if (next.nodeName == "H2")
				break;
			answer_nodes.push(next);
			next = next.next();
		}
		var div = Builder.node("div", {"class":"answer"}, answer_nodes);
		$(div).hide();
		el.insert({after: div});
		el.down("a").observe("click", function(ev) {
			ev.stop();
			var el = ev.element().up("h2").next("div.answer");
			if (!el.visible())
				el.appear();
		});
	});

	if ($("office-map")) {
		if (GBrowserIsCompatible()) {
			var map = new GMap2($("office-map"));

			var parts = $$("meta[name=geo.position]")[0].getAttribute("content").split(";");
			var latlng = new GLatLng(parts[0], parts[1]);
			var marker = new GMarker(latlng);

			map.setCenter(latlng, 13);
			map.addOverlay(marker);
			map.setUIToDefault();
		}
	}
}).onLoad();

function validate_property() {
	if ($("office-only") && $("office-only").checked)
		return true;
	if ($F("property_radius") == "") {
		alert( "Please enter your search radius." );
		$("property_radius").focus();
		return false ;
	} 
	if ($F("property_location") == "") {
		alert( "Please enter your search location." );
		$("property_location").focus();
		return false ;
	} 
	return true;
}

function validate_office() {
	form = $('officeSearchForm');
	if (form.radius_miles.value == "") {
		alert( "Please enter your search radius." );
		form.radius_miles.focus();
		return false ;
	} 
	if (false && form.radius_location.value == "") { // disabled
		alert( "Please enter your search location." );
		form.radius_location.focus();
		return false ;
	} 
	return true;
}

function office_search_submit() {
	if ($("office_location").name == "radius_location,town,county,part_postcode")
		$("office_location").name = "radius_location";
}

var property_location_onchange = function() {
	ajaxSearchOnchange($("property_location"), $("property_radius"));
}
