﻿
var account_code = 'BRITI11220';
var license_code = 'WF33-EB96-FN96-CW99';
var curAdd = '';
var curCountry = 'United Kingdom'
var machine_id = '';


var selAddressSelectId = '';
var frmSchoolNameId = '';
var frmAddress1Id = '';
var frmAddress2Id = '';
var frmAddress3Id = '';
var frmTownId = '';
var frmCountyId = '';

function pcaByPostcodeBegin(pcaScriptTagId, pcaPostCodeId) {
    var postcode = document.getElementById(pcaPostCodeId).value;
    var scriptTag = document.getElementById(pcaScriptTagId);
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    //document.getElementById("divLoading").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 = pcaScriptTagId;
    headTag.appendChild(scriptTag);



}

function pcaByPostcodeEnd() {
    //document.getElementById("divLoading").style.display = 'none';

    //Test for an error
    if (pcaIsError) {
        //Show the error message
        //document.getElementById(selAddressSelectId).style.display = 'none';
        //document.getElementById("btnFetch").style.display = 'none';
        alert(pcaErrorMessage);
    }
    else {
        //Check if there were any items found
        if (pcaRecordCount == 0) {
            //document.getElementById(selAddressSelectId).style.display = 'none';
            //document.getElementById("btnFetch").style.display = 'none';
            alert("Sorry, no matching items found. Please try another postcode.");
        }
        else {
            //document.getElementById(selAddressSelectId).style.display = '';
            //document.forms[0]["btnFetch"].style.display = '';

            if (pca_id.length > 0) {
                document.getElementById(selAddressSelectId).options[0] = null;
                document.getElementById(selAddressSelectId).options[0] = new Option("Please Select Address", "");
                hightlightBox(document.getElementById(selAddressSelectId));
            }
            for (i = document.getElementById(selAddressSelectId).options.length - 1; i > 0; i--) {
                document.getElementById(selAddressSelectId).options[i] = null;
            }
            for (i = 0; i < pca_id.length; i++) {
                document.getElementById(selAddressSelectId).options[document.getElementById(selAddressSelectId).length] = new Option(pca_description[i], pca_id[i]);
            }
        }
    }
}

function pcaFetchBegin(pcaScriptTagId) {
    var address_id = document.getElementById(selAddressSelectId).value;
    var scriptTag = document.getElementById(pcaScriptTagId);
    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 = pcaScriptTagId;
    headTag.appendChild(scriptTag);

    //document.getElementById(selAddressSelectId).style.display = 'none';
    //document.forms[0]["btnFetch"].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 {
            document.getElementById(frmSchoolNameId).value = '' + pca_organisation_name[0];
            document.getElementById(frmAddress1Id).value = '' + pca_line1[0];
            document.getElementById(frmAddress2Id).value = '' + pca_line2[0];
            document.getElementById(frmAddress3Id).value = '' + pca_line3[0];
            document.getElementById(frmTownId).value = '' + pca_post_town[0];
            document.getElementById(frmCountyId).value = '' + pca_county[0];

            document.getElementById(frmAddress1Id).readOnly = true;
            document.getElementById(frmAddress2Id).readOnly = true;
            document.getElementById(frmTownId).readOnly = true;
            document.getElementById(frmCountyId).readOnly = true;
            
        }
    }
}
