﻿//GET ADDRESS LIST

function PostcodeAnywhereInternational_Interactive_RetrieveByPostalCode_v1_10Begin(Key, Country, PostalCode) {
    var scriptTag = document.getElementById("PCA50dbdd0ef1c64f12b091e12c5c9bff49");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    //Build the url
    strUrl = "http://services.postcodeanywhere.co.uk/PostcodeAnywhereInternational/Interactive/RetrieveByPostalCode/v1.10/json.ws?";
    strUrl += "&Key=" + encodeURI(Key);
    strUrl += "&Country=" + encodeURI(Country);
    strUrl += "&PostalCode=" + encodeURI(PostalCode);
    strUrl += "&CallbackFunction=PostcodeAnywhereInternational_Interactive_RetrieveByPostalCode_v1_10End";

    //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 = "PCA50dbdd0ef1c64f12b091e12c5c9bff49";
    headTag.appendChild(scriptTag);
};

function PostcodeAnywhereInternational_Interactive_RetrieveByPostalCode_v1_10End(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) {
            $("[id$=Address]").fadeIn();
            $("[id$=Address2]").fadeIn();
            $("[id$=City]").fadeIn();
            $("[id$=County]").fadeIn();
            $("[id$=Postcode]").fadeIn();
            $("[id$=txtAddress1]").focus();
        }
        else if (response.length == 1) {
            $("[id$=txtAddress1]").val(response[0].Street);
            $("[id$=txtCity]").val(response[0].City);
            $("[id$=txtCounty]").val(response[0].State);
            $("[id$=txtPostcode]").val(response[0].PostalCode);
            $("[id$=Address]").fadeIn();
            $("[id$=Address2]").fadeIn();
            $("[id$=City]").fadeIn();
            $("[id$=County]").fadeIn();
            $("[id$=Postcode]").fadeIn();
            $("[id$=txtAddress1]").focus();
        } else {
            $("[id$=txtAddress]").val(response[0].Street);
            $("[id$=txtCity]").val(response[0].City);
            $("[id$=txtCounty]").val(response[0].State);
            $("[id$=txtPostcode]").val(response[0].PostalCode);
            $("[id$=Address]").fadeIn();
            $("[id$=Address2]").fadeIn();
            $("[id$=City]").fadeIn();
            $("[id$=County]").fadeIn();
            $("[id$=Postcode]").fadeIn();
            $("[id$=txtAddress1]").focus();
        }
    }
};

//END GET ADDRESS LIST
