// 
// All the js code required to handle the postcode to address functionality on the apply page
//

	$(document).ready(function() {
		// add postcode search button
		// have to just use a div, because an input button mucks up the values used by cforms (still?  Now using names, not numbers?)
		// also have to add it to an element a bit further in the html, so that it comes after the other address elements, and the address list box will appear over the others inputs in IE7 - http://tjkdesign.com/articles/everything_you_always_wanted_to_know_about_z-index_but_were_afraid_to_ask.asp
		$("#li--25").append("<div id='postcode-search'> </div>");
		$("#postcode-search").append("<div id='address-list-searching'></div>");
		// action when postcode search button clicked
		$("#postcode-search").click(requestAddressList);
	});

	// handles display update, requests address list, defines handler
	function requestAddressList() {
		$("#address-select").remove();
		$("#address-list-searching").css("display", "block");
		var addressListUrl = "http://www.paydayagency.co.uk/addresslist.php?pc="+$("#Post-Code").val().replace(/ /g,'');
		$.ajax({
			url: addressListUrl,
			dataType: "xml",
			success: handleAddressList 
		});
	}

	
	// Accepts an xml list of addresses, adds a select box for the users, then uses the 
	// selected address to make another ajax request to get the full address details
	function handleAddressList (xml) {
		// create address list
		var selectaddress = "<select id='address-select' multiple='multiple' size='10'> <option value=''>select address:</option>";
		$(xml).find("Row").each(function() {
			selectaddress = selectaddress + "<option value='"+$(this).attr("Id")+"'>"+$(this).attr("StreetAddress") + "</option>";
		});			
		selectaddress = selectaddress + "</select>";
		// show the address search box
		$("#address-list-searching").css("display", "none");
		$("#postcode-search").append(selectaddress);
	
		// add the function to be run when users clicks a value
		$("#address-select").change(requestAddress);
	}

	// handles display update, requests address details, defines handler
	function requestAddress() {
		$("#address-list-searching").css("display", "block");
		var addressId = new String($("#address-select").val());

		var addressDetailsUrl = "http://www.paydayagency.co.uk/addressdetails.php?id="+addressId;
		$.ajax({
			url: addressDetailsUrl,
			dataType: "xml",
			success: insertDetails
		});

		$("#address-select").remove();
	}
	
	// accepts the chosen address details xml string, and puts the details in the form
	function insertDetails (xml) {
		$("#address-list-searching").css("display", "none");
		
		var housenum = "";
		if ("" != $(xml).find("Row").attr("Company")) { housenum = $(xml).find("Row").attr("Company"); }
		if ("" != $(xml).find("Row").attr("BuildingName")) { housenum = $(xml).find("Row").attr("BuildingName"); }
		if ("" != $(xml).find("Row").attr("BuildingNumber")) { housenum = $(xml).find("Row").attr("BuildingNumber"); }
		$("#House-Number").val(housenum);
		$("#Street").val($(xml).find("Row").attr("PrimaryStreet"));
		$("#Town").val($(xml).find("Row").attr("PostTown"));
		$("#County").val($(xml).find("Row").attr("County"));
		
	}

