﻿function PostcodeAnywhere_Interactive_Find_v1_10Begin(Key, SearchTerm, PreferredLanguage, Filter, UserName)
{
    var pcSelect = document.getElementById('return');
    var pcSelectRow = document.getElementById('trreturn');
    var pcAddressDiv = jQuery("#divaddress")
    if (pcSelectRow) { pcSelectRow.style.display = 'none'; }
    if (pcSelect) { pcSelect.style.display = 'none'; }
    if (pcAddressDiv) { pcAddressDiv.css('display', 'none'); }

    var scriptTag = document.getElementById("PCA6d35cfc188f1451f9cfdf1b5d751a716");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    //Build the url
    strUrl = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/Find/v1.10/json.ws?";
    strUrl += "&Key=" + encodeURI(Key);
    strUrl += "&SearchTerm=" + encodeURI(SearchTerm);
    strUrl += "&PreferredLanguage=" + encodeURI(PreferredLanguage);
    strUrl += "&Filter=" + encodeURI(Filter);
    strUrl += "&UserName=" + encodeURI(UserName);
    strUrl += "&CallbackFunction=PostcodeAnywhere_Interactive_Find_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 = "PCA6d35cfc188f1451f9cfdf1b5d751a716";
    headTag.appendChild(scriptTag);
}

function PostcodeAnywhere_Interactive_Find_v1_10End(response)
{
    var pcSelect = document.getElementById('return');
    var pcSelectRow = document.getElementById('trreturn');
    var pcAddressDiv = jQuery("#divaddress")
    if (pcSelectRow) { pcSelectRow.style.display = 'none'; }
    if (pcSelect) { pcSelect.style.display = 'none'; }
    if (pcAddressDiv) { pcAddressDiv.css('display', 'none'); }
    //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 we could not find your postcode. Please check you have entered it correctly and try again.");
        }
        else
        {
            if (pcSelect) {
                pcSelect.options.length = 0;
                pcSelect.options.add(new Option("Please choose an address", 'manual'));
                for (var i = 0; i < response.length; i++)
                    pcSelect.options.add(new Option(response[i].StreetAddress + ", " + response[i].Place, response[i].Id));
                pcSelect.options.add(new Option("My address is not listed, I'll enter it manually", 'manual'));
                pcSelect.style.display = '';
            }
            if (pcSelectRow) { pcSelectRow.style.display = ''; }
        }
    }
}

function PostcodeAnywhere_Interactive_RetrieveById_v1_10Begin(Key, Id, PreferredLanguage, UserName)
{
    if (Id== 'manual') {
        var response = [new Object()];
        response[0].Line1 = '';
        response[0].Line2 = '';
        response[0].PostTown = '';
        response[0].County = '';
        PostcodeAnywhere_Interactive_RetrieveById_v1_10End(response);
        return;
    }
    
    var scriptTag = document.getElementById("PCAa73f9bc2b60d4e4cbd595512478a3291");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    //Build the url
    strUrl = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveById/v1.10/json.ws?";
    strUrl += "&Key=" + encodeURI(Key);
    strUrl += "&Id=" + encodeURI(Id);
    strUrl += "&PreferredLanguage=" + encodeURI(PreferredLanguage);
    strUrl += "&UserName=" + encodeURI(UserName);
    strUrl += "&CallbackFunction=PostcodeAnywhere_Interactive_RetrieveById_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 = "PCAa73f9bc2b60d4e4cbd595512478a3291";
    headTag.appendChild(scriptTag);
}

function PostcodeAnywhere_Interactive_RetrieveById_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)
        {
            alert("Sorry, no matching items found");
        }
        else
        {
            // Populate the address fields on the edit profile page (if we're on the edit profile page).
            if (document.getElementById('ctl00_plcMain_MyAccount_myProfile_editProfileForm_ctl00_UserAddressLine1')) document.getElementById('ctl00_plcMain_MyAccount_myProfile_editProfileForm_ctl00_UserAddressLine1').value = response[0].Line1;
            if (document.getElementById('ctl00_plcMain_MyAccount_myProfile_editProfileForm_ctl00_UserAddressLine2')) document.getElementById('ctl00_plcMain_MyAccount_myProfile_editProfileForm_ctl00_UserAddressLine2').value = response[0].Line2;
            if (document.getElementById('ctl00_plcMain_MyAccount_myProfile_editProfileForm_ctl00_UserTown')) document.getElementById('ctl00_plcMain_MyAccount_myProfile_editProfileForm_ctl00_UserTown').value = response[0].PostTown;
            if (document.getElementById('ctl00_plcMain_MyAccount_myProfile_editProfileForm_ctl00_UserCounty')) document.getElementById('ctl00_plcMain_MyAccount_myProfile_editProfileForm_ctl00_UserCounty').value = response[0].County;

            // Populate the address fields on the registration page (if we're on the registration page) & show them.
            if (document.getElementById('ctl00_RegistrationForm_txtAddress1')) document.getElementById('ctl00_RegistrationForm_txtAddress1').value = response[0].Line1;
            if (document.getElementById('ctl00_RegistrationForm_txtAddress2')) document.getElementById('ctl00_RegistrationForm_txtAddress2').value = response[0].Line2;
            if (document.getElementById('ctl00_RegistrationForm_txtTown')) document.getElementById('ctl00_RegistrationForm_txtTown').value = response[0].PostTown;
            if (document.getElementById('ctl00_RegistrationForm_txtCounty')) document.getElementById('ctl00_RegistrationForm_txtCounty').value = response[0].County;
            jQuery("#divaddress").css('display', '');
        }
    }
}
