if($('addresslookup')) {
	Event.observe($('addresslookup'), "click", function(e) {
		new Ajax.Request('/common/php/common.address_lookup.php?postcode='+encodeURIComponent($('post_code').value), {
			method: 'get',
			onSuccess: function(transport) {

				if(transport.responseText.indexOf('Not Found') >= 0) {
					$('addrres1').show();
					$('addrres2').show();
					$('addrres3').show();
					alert("Postcode not found");
					$('findaddress_loader').hide();
				} else {
					addritems = transport.responseXML.getElementsByTagName('AFDPostcodeEverywhere')[0].getElementsByTagName('AddressListItem');

					// Empty the results select box
					$('addressresults').options.length = 0;

					for(i=0;i<addritems.length;i++) {
						// Set each option tag in the results select box

						addrline = addritems[i].getElementsByTagName('Address').item(0).firstChild.nodeValue;
						postkey = addritems[i].getElementsByTagName('PostKey').item(0).firstChild.nodeValue;
						addrlines = addrline.split(' ');
						addrstring = "";
						for(j=2;j<addrlines.length;j++) {
							addrstring += addrlines[j]+" ";
						}
						$('addressresults').options[i] = new Option(addrstring,postkey);
					}

					// Display the results to the user
					$('addr').show();

					// Hide the loader graphic
					$('findaddress_loader').hide();
				}
			},
			onLoading: function() {
				// Display the loader graphic
				$('findaddress_loader').show();
			}
		});
	});
}

if($('addressresults')) {
	Event.observe($('addressresults'),'click', function(e) {
		var address = Event.element(e);
		$('addr').hide();

		// Look up more details about the selected address
		new Ajax.Request('/common/php/common.address_lookup.php?postkey='+encodeURIComponent(address.value), {
			method: 'get',
			onSuccess: function(t) {
				addritem = t.responseXML.getElementsByTagName('AFDPostcodeEverywhere')[0].getElementsByTagName('Address')[0];

				// Update the relevant fields on the form
				if(!updateaddresselement('Property','address_line1')) {
					updateaddresselement('Street','address_line1');
					$('addrres1').show();
					updateaddresselement('Locality','address_line2');
					$('addrres2').show();
				} else {
					updateaddresselement('Property','address_line1');
					$('addrres1').show();
					updateaddresselement('Street','address_line2');
					$('addrres2').show();
				}
				updateaddresselement('Town','town');
				$('addrres3').show();
				updateaddresselement('Postcode','post_code');
				
				$('findaddress_loader').hide();
			},
			onLoading: function() {
				// Display the loader graphic
				$('findaddress_loader').show();
			}
		});

		function updateaddresselement(xmltag,htmltag) {
			$(htmltag).value = "";
			// If the element exists in the XML, update the HTML tag with its contents
			if(xmltag) {
				if(addritem.getElementsByTagName(xmltag).item(0).firstChild) {
					$(htmltag).value = addritem.getElementsByTagName(xmltag).item(0).firstChild.nodeValue;
					return true;
				} else {
					return false;
				}
			} else {
				return true;
			}
		}
	});
}