
var postcodeAnywhereServiceUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?"

if(window.location.href.substr(0, 5) == "https") {
	postcodeAnywhereServiceUrl = "https://services.postcodeanywhere.co.uk/inline.aspx?";
}

var pa_accountCode;
var pa_licenseCode;
var pa_address = [];

function DoSearch(addressFields, accountCode, licenseCode)
{
    pa_address = addressFields; 
    pa_accountCode = accountCode;
    pa_licenseCode = licenseCode; 
    
    var searchTextBox = document.getElementById(pa_address[0]);
     
    if (searchTextBox != null && searchTextBox.value != "")
    {   
        //The following regex must be identical to the one used on the validator on the postcode field.
        var reg = new RegExp("([Gg][Ii][Rr] 0[Aa][Aa]|[A-Pa-pR-Ur-uWwYyZz]([0-9]{1,2}|([A-Ha-hK-Yk-y][0-9]|[A-Ha-hK-Yk-y][0-9]([0-9]|[AaBbEeHhMmNnPpRrV-Yv-y]))|[0-9][A-Ha-hJjKkS-Us-uWw]) *[0-9][AaBbD-Hd-hJjLlNnP-Up-uW-Zw-z]{2})$");
        if (searchTextBox.value.match(reg))
        {             
            pcaByPostcodeBegin(searchTextBox.value, pa_accountCode, pa_licenseCode, "");
            return;
        }
    }    
    
    var addressSelect = document.getElementById("addressSelect");         
    addressSelect.style.display = "none";
}

function populateAddress()
{
    document.getElementById(pa_address[1]).value = pcaFields[4];
    document.getElementById(pa_address[2]).value = pcaFields[5];
    document.getElementById(pa_address[3]).value = pcaFields[6];
    document.getElementById(pa_address[4]).value = pcaFields[9];
    document.getElementById(pa_address[5]).value = pcaFields[10];
    document.getElementById(pa_address[0]).value = pcaFields[11];
}

function clearAddress()
{
    document.getElementById(pa_address[1]).value = "";
    document.getElementById(pa_address[2]).value = "";
    document.getElementById(pa_address[3]).value = "";
    document.getElementById(pa_address[4]).value = "";
    document.getElementById(pa_address[5]).value = "";
}

function pcaByPostcodeBegin(postcode, account_code, license_code, machine_id)
{
    var scriptTag = document.getElementById("pcaScript");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";
    
    strUrl = postcodeAnywhereServiceUrl;
    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";
    
    if (scriptTag) 
    {
        try
        {
            headTag.removeChild(scriptTag);
        }
        catch (e)
        {
              //Ignore
        }
    }
    
    scriptTag = document.createElement("script");
    scriptTag.src = strUrl;
    scriptTag.type = "text/javascript";
    scriptTag.id = "pcaScript";
    headTag.appendChild(scriptTag);
}

function pcaByPostcodeEnd()
{
    if (pcaIsError)
    {
        alert(pcaErrorMessage);
    }
    else
    {
        if (pcaRecordCount==0)
        {
            alert("Sorry, no matching items found");
        }
        else
        {
            var addressSelect = document.getElementById("addressSelect");
            addressSelect.options.length = 0; //remove options
            
            if (pca_description.length > 0)
            {            
                addressSelect.style.display = "block";
                
                var option = document.createElement('option');                
                var text = document.createTextNode('Please Select');
                option.appendChild(text);
                addressSelect.appendChild(option);
                
                for(var i = 0; i < pca_description.length; i++)
                {               
                    option = document.createElement('option');
                    option.setAttribute('value', pca_id[i]);
                    text = document.createTextNode(pca_description[i]);
                    option.appendChild(text);
                    addressSelect.appendChild(option);
                }
            }
        }
    }
}

function pcaFetchAddressBegin(id, language, style, account_code, license_code, machine_id, options)
{
    var scriptTag = document.getElementById("pcaScript");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    strUrl = postcodeAnywhereServiceUrl;
    strUrl += "&action=fetch";
    strUrl += "&id=" + escape(id);
    strUrl += "&language=" + escape(language);
    strUrl += "&style=" + escape(style);
    strUrl += "&account_code=" + escape(account_code);
    strUrl += "&license_code=" + escape(license_code);
    strUrl += "&machine_id=" + escape(machine_id);
    strUrl += "&options=" + escape(options);
    strUrl += "&callback=pcaFetchAddressEnd";

    if (scriptTag) 
    {
        try
        {
            headTag.removeChild(scriptTag);
        }
        catch (e)
        {
              //Ignore
        }
     }
    scriptTag = document.createElement("script");
    scriptTag.src = strUrl;
    scriptTag.type = "text/javascript";
    scriptTag.id = "pcaScript";
    headTag.appendChild(scriptTag);
}

function pcaFetchAddressEnd()
{
    if (pcaIsError)
    {
        alert(pcaErrorMessage);
    }
    else
    {
        if (pcaRecordCount==0)
        {
            alert("Sorry, no matching items found");
        }
        else
        {
           populateAddress();
       }
    }
}	

function addressSelected()
{
    var addressSelect = document.getElementById("addressSelect"); 
    if (addressSelect.selectedIndex > 0)
    {
        var id = addressSelect[addressSelect.selectedIndex].value;
        pcaFetchAddressBegin(id, "", "", pa_accountCode, pa_licenseCode, "", "");
    }
    else
    {
        clearAddress();
    }
}