﻿var account_code = 'GUIDE11111';
var license_code = 'ZF99-BR99-ER38-JX69';
var machine_id = '';
var address = '';
var deliveryAdd = '';
var chkSameAddress = '';
var hiddenDiv = '';
var loading = ''
var selectAddress = '';

function pcaByPostcodeBegin(objPostcode, divSelect, divLoading, select) {
    hiddenDiv = divSelect;
    loading = divLoading;
    selectAddress = select;
    var postcode = objPostcode.value;
    var scriptTag = document.getElementById("pcaScriptTag");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    loading.style.display = '';

    //Build the url
    strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
    strUrl += "&action=lookup";
    strUrl += "&type=by_postcode";
    strUrl += "&postcode=" + escape(postcode);
    strUrl += "&account_code=" + escape(account_code);
    strUrl += "&license_code=" + escape(license_code);
    strUrl += "&machine_id=" + escape(machine_id);
    strUrl += "&callback=pcaByPostcodeEnd";

    //Make the request
    if (scriptTag) {
        //The following 2 lines perform the same function and should be interchangeable
        headTag.removeChild(scriptTag);
        //scriptTag.parentNode.removeChild(scriptTag);
    }
    scriptTag = document.createElement("script");
    scriptTag.src = strUrl
    scriptTag.type = "text/javascript";
    scriptTag.id = "pcaScriptTag";
    headTag.appendChild(scriptTag);
}

function pcaByPostcodeEnd() {
    loading.style.display = 'none';
    //Test for an error
    if (pcaIsError) {
        //Show the error message
        hiddenDiv.style.display = 'none';
        alert(pcaErrorMessage);
    }
    else {
        //Check if there were any items found
        if (pcaRecordCount == 0) {
            hiddenDiv.style.display = 'none';
            alert("Sorry, no matching items found. Please try another postcode.");
        }
        else {
            hiddenDiv.style.display = 'block';

            for (i = selectAddress.options.length - 1; i >= 0; i--) {
                selectAddress.options[i] = null;
            }
            for (i = 0; i < pca_id.length; i++) {
                selectAddress.options[selectAddress.length] = new Option(pca_description[i], pca_id[i]);
            }
        }
    }
}

function pcaFetchBegin(arrayAddress, cb, deliveryAddress) {
    address = arrayAddress;
    deliveryAdd = deliveryAddress;
    chkSameAddress = cb;
    var address_id = selectAddress.value;
    var scriptTag = document.getElementById("pcaScriptTag");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    //Build the url
    strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
    strUrl += "&action=fetch";
    strUrl += "&id=" + escape(address_id);
    strUrl += "&account_code=" + escape(account_code);
    strUrl += "&license_code=" + escape(license_code);
    strUrl += "&machine_id=" + escape(machine_id);
    strUrl += "&callback=pcaFetchEnd";

    //Make the request
    if (scriptTag) {
        //The following 2 lines perform the same function and should be interchangeable
        headTag.removeChild(scriptTag);
        //scriptTag.parentNode.removeChild(scriptTag);
    }
    scriptTag = document.createElement("script");
    scriptTag.src = strUrl
    scriptTag.type = "text/javascript";
    scriptTag.id = "pcaScriptTag";
    headTag.appendChild(scriptTag);

    hiddenDiv.style.display = 'none';
}

function pcaFetchEnd() {
    //Test for an error
    if (pcaIsError) {
        //Show the error message
        alert(pcaErrorMessage);
    }
    else {
        //Check if there were any items found
        if (pcaRecordCount == 0) {
            alert("Sorry, no matching items found");
        }
        else {
            if (address != null) {
                

               // document.getElementById(address[0]).focus();
                document.getElementById(address[0]).value = '' + pca_line1[0];
                document.getElementById(address[0]).onfocus();
                changeToWhiteBrg(address[0]);
  
                document.getElementById(address[1]).value = '' + pca_line2[0];
                document.getElementById(address[1]).onchange();
               
                document.getElementById(address[2]).value = '' + pca_post_town[0];
                document.getElementById(address[2]).onfocus();
                changeToWhiteBrg(address[2]);

                document.getElementById(address[3]).value = '' + pca_county[0];
                document.getElementById(address[3]).onchange();

                document.getElementById(address[4]).value = '' + pca_organisation_name[0];
                document.getElementById(address[4]).onchange();

                document.getElementById(address[5]).value = 'England';
                document.getElementById(address[5]).onfocus();

                document.getElementById(address[6]).value = pca_postcode[0];
                document.getElementById(address[6]).onfocus();
                changeToWhiteBrg(address[5]);

                if (document.getElementById(chkSameAddress) != null)
                {
                    if (document.getElementById(chkSameAddress).checked == true) {
                        if (deliveryAdd != null) {
                            document.getElementById(deliveryAdd[0]).value = '' + pca_line1[0];
                            document.getElementById(deliveryAdd[0]).onfocus();
                            changeToWhiteBrg(deliveryAdd[0]);

                            document.getElementById(deliveryAdd[1]).value = '' + pca_line2[0];
                            document.getElementById(deliveryAdd[1]).onchange();

                            document.getElementById(deliveryAdd[2]).value = '' + pca_post_town[0];
                            document.getElementById(deliveryAdd[2]).onfocus();
                            changeToWhiteBrg(deliveryAdd[2]);

                            document.getElementById(deliveryAdd[3]).value = '' + pca_county[0];
                            document.getElementById(deliveryAdd[3]).onchange();

                            document.getElementById(deliveryAdd[4]).value = '' + pca_organisation_name[0];
                            document.getElementById(deliveryAdd[4]).onchange();

                            document.getElementById(deliveryAdd[5]).value = 'England';
                            document.getElementById(deliveryAdd[5]).onfocus();

                            document.getElementById(deliveryAdd[6]).value = pca_postcode[0];
                            changeToWhiteBrg(deliveryAdd[6]);
                            changeToWhiteBrg(deliveryAdd[5]);
                        }
                    }
                }
            }
        }
    }
}
		
