	
	
	$(document).ready(function() {
        $('#address_dialog').jqm({ modal: true });
    });

    function FindAddresses(Key, PostcodeElem, Building, UserName) {

		var Postcode = $("#" + PostcodeElem).val();
		aspnet_id = PostcodeElem.replace("txtPostcode", "");
        var scriptTag = document.getElementById("PCA38d38252878f434581f85b249661cd94");
        var headTag = document.getElementsByTagName("head").item(0);
        var strUrl = "";

        //Build the url
        strUrl = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveByPostcodeAndBuilding/v1.10/json.ws?";
        strUrl += "&Key=" + escape(Key);
        strUrl += "&Postcode=" + escape(Postcode);
        strUrl += "&Building=" + escape(Building);
        strUrl += "&UserName=" + escape(UserName);
        strUrl += "&CallbackFunction=FindAddressesCallback";

        //Make the request
        if (scriptTag) {
            try {
                headTag.removeChild(scriptTag);
            }
            catch (e) {
                //Ignore
            }
        }
        scriptTag = document.createElement("script");
        scriptTag.src = strUrl
        scriptTag.type = "text/javascript";
        scriptTag.id = "PCA38d38252878f434581f85b249661cd94";
        headTag.appendChild(scriptTag);
    }

    var jsonAddressArray = null;
	var aspnet_id = "";

    function FindAddressesCallback(response) {
        //Test for an error
        if (response.length == 1 && typeof (response[0].Error) != 'undefined') {
            //Show the error message
            alert(response[0].Description);
        }
        else {
            //Check if there were any items found
            if (response.length == 0) {
                alert("Sorry, no matching items found");
            }
            else {
                jsonAddressArray = response;
                DisplayLightBoxPlugin();
            }
        }
    }

    function DisplayLightBoxPlugin() {
        $('#address_dialog').jqmShow();
		
		var output = [];

		$.each(jsonAddressArray, function(i, item)
		{
			var addr = item.Line1 + ", " + item.Line2;
			if (item.Line3.length > 1)
				addr += ", " + item.Line3;
				
			addr += ", " + item.PostTown;
			output.push('<option value="'+ i +'">'+ addr +'</option>');
		});

		$('#addressList').html(output.join(''));
    }
	
	function AddDotNetId(elLabel)
	{
		return "#" + aspnet_id + elLabel;
	}
	
	function SelectAddressFromLightbox()
	{
		var selectedAddress = jsonAddressArray[$('#addressList').val()];
		$('#address_dialog').jqmHide(); 
		
		$(AddDotNetId("txtAddress1")).val(selectedAddress.Line1);
		$(AddDotNetId("txtAddress2")).val(selectedAddress.Line2);
		$(AddDotNetId("txtAddress3")).val(selectedAddress.Line3);
		$(AddDotNetId("txtTown")).val(selectedAddress.PostTown);
		$(AddDotNetId("txtCounty")).val(selectedAddress.County);
		$(AddDotNetId("txtPostcode")).val(selectedAddress.Postcode);
		
		if (selectedAddress.Company.length > 1)
			$(AddDotNetId("txtCompany")).val(selectedAddress.Company);
	}
	
	
	
	
	
	
	
	
	
	
	
	

