
var addressian_api_url = "https://api.addressian.co.uk/v2/autocomplete/";
var getAddresses = function (code) {

    var data = "";
    var type = "GET";
    var dataType = "json";
    var url = addressian_api_url + code;
    $.ajax({
        url: url,
        type: type,
        data: data,
        dataType: dataType,
        cache: true,
        crossDomain: true,
        headers: {
            "x-api-key": "7C5NpUmFG513hL5DvQOqs2axEuebBBWh4WpqBA0K",
            'Content-Type': 'application/json'
        },
        success: function (response) {
            alert('success');

            if (response.length > 0) {
                var $dropdown = jQuery("#addresses");
                $dropdown.empty();
                response.sort();
                var optionHtml = "<option value='' >Please select your address</option>";
                $dropdown.append(optionHtml);
                var html = "";
                var l = "";
                var selected = "";

                jQuery.each(response, function () {

                    l = this.address.join(", ") + ", " + this.postcode + ", " + this.city;

                    if (l == address1) {
                        html += "<option selected  value='" + l + "' >" + l + "</option>";
                    } else {
                        html += "<option  value='" + l + "' >" + l + "</option>";
                    }

                });
                html += "<option value='-1'  >Not on the list?</option>";
                $dropdown.append(html);
                if (addresses == "-1") {
                    $dropdown.val("-1");
//                    $("#div_reg_address1").show();
                    $("#data").val(address1);
                }
                $("#div_addresses").show();
            }
        },
        error: function (jqXHR, error, errorThrown) {
             var err = eval("(" + jqXHR.responseText + ")");
                alert(err.Message);
        }
    });
}
/*
 jQuery("#postcode-go").click(function () {
 jQuery
 .when(
 jQuery.get(
 "https://api.addressian.co.uk/v2/autocomplete/" +
 jQuery("#postcode").val(),
 function (data) {
 postcodeAddresses = data;
 },
 "json"
 )
 )
 .then(function () {
 var $dropdown = jQuery("#dropdown");
 $dropdown.empty();
 postcodeAddresses.sort();
 jQuery.each(postcodeAddresses, function () {
 var $option = jQuery("<option />");
 $option.text(this.address.join(", ") + ", " + this.postcode);
 $option.data("addressdetails", this);
 $dropdown.append($option);
 });
 $dropdown.fadeIn("slow");
 });
 });
 
 jQuery("#dropdown").change(function () {
 jQuery("#chosen-from-postcode-picker").fadeOut("slow");
 var addressDetails = jQuery(this)
 .children("option:selected")
 .data("addressdetails");
 jQuery("#chosen-from-postcode-picker").html(
 "<strong>Selected address:</strong><br/><br/>" +
 addressDetails.address.join(", ") +
 (addressDetails.company != null
 ? "<br/>" + addressDetails.company
 : "") +
 (addressDetails.city != null ? "<br/>" + addressDetails.city : "") +
 (addressDetails.county != null ? "<br/>" + addressDetails.county : "") +
 "<br/>" +
 addressDetails.postcode
 );
 jQuery("#chosen-from-postcode-picker").fadeIn("slow");
 });
 });
 */


