<!-- 
var anywhere_enkey="";
var collection_dropdown="";
var collection_id=0;
var delivery_dropdown="";
var delivery_id=0;

function swap_string(Key) {
	var newKey=Key.split('');
	var len=Key.length;
	var tmp;
	for (var i=0; i<(Key.length/2);i++) {
		tmp=newKey[i];
		newKey[i]=newKey[len-i];
		newKey[len-i]=tmp;
	}
	return newKey.join("");
}

function AnywhereKey() 
{
	if (anywhere_enkey.length==0) {
		var Key2=decode64(swap_string(document.getElementById('footer_code').value));
		anywhere_enkey=Key2.substring(0,4)+"-"+Key2.substring(4,8)+"-"+Key2.substring(8,12)+"-"+Key2.substring(12,16);
	}
	return anywhere_enkey;
}
function ukpostcode(postcode)
{
	var ucode = postcode.toUpperCase()
	if (ucode.length > 4) {
		var temp = new Array();
		temp = ucode.split(' ');
		if (temp.length < 2) {
			var newcode = ucode.substring(0, ucode.length-3) + " " + ucode.substring(ucode.length-3, ucode.length);
			ucode = newcode;
		}
	}
	return ucode;
}

function PostcodeAnywhere_Interactive_Find_URL(SearchTerm, PreferredLanguage, Filter, UserName, CallbackFunction) {
	// Build the query string
	var url = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/Find/v1.00/json.ws?";
	url += "&Key=" + encodeURI(AnywhereKey());
	url += "&SearchTerm=" + encodeURI(SearchTerm);
	url += "&PreferredLanguage=" + encodeURI(PreferredLanguage);
	url += "&Filter=" + encodeURI(Filter);
	url += "&UserName=" + encodeURI(UserName);
	url += "&CallbackFunction=" + CallbackFunction;
	return url;
}
function PostcodeAnywhere_Interactive_RetrieveById_URL(Id, PreferredLanguage, UserName, CallbackFunction) {

	var url = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveById/v1.20/json.ws?";

    // Build the query string
    url += "&Key=" + encodeURI(AnywhereKey());
    url += "&Id=" + encodeURI(Id);
    url += "&PreferredLanguage=" + encodeURI(PreferredLanguage);
    url += "&UserName=" + encodeURI(UserName);
    url += "&CallbackFunction=" + CallbackFunction;
	return url;
}
function SetMatchDropDownField(list, key) {
	if (key.length > 0) {
		var found=false;
		for (var j=0; (j < list.length) && (found == false); j++) {
			if (list.options[j].text == key) {
				list.options[j].selected = true;
				found = true;
			}
		}
	}
}

function SetPostcodeAnywhereBacktoFields(id, Company, Line1, Line2, Line3, PostTown, Country, Postcode, County, Type) {
	//alert("SetPostcodeAnywhereBacktoFields");
	var fieldname='FormField_'+String(id-7);
	document.getElementById(fieldname).value = Company;
	fieldname='FormField_'+String(id-5);
	document.getElementById(fieldname).value = Line1;
	var fulladdress;
	if (Line3.length > 0)
		fulladdress = Line2+" "+Line3;
	else
		fulladdress = Line2;
	fieldname='FormField_'+String(id-4);
	document.getElementById(fieldname).value = fulladdress;

	fieldname='FormField_'+String(id-3);
	document.getElementById(fieldname).value = PostTown;
	if (Country != 'undefined') {
		fieldname='FormField_'+String(id-2);
		document.getElementById(fieldname).selectOptions(Country, true);
	}
	fieldname='FormField_'+String(id);
	document.getElementById(fieldname).value = Postcode;
	fieldname='FormField_'+String(id-1);
	//alert("Set field ["+fieldname+"] = "+ County + " Type=" + Type);
	SetMatchDropDownField(document.getElementById(fieldname), County);
	//if (response[0].Type == "Residential")
}

function PostcodeAnywhere_Interactive_Find_v1_10Begin_collection(id, PreferredLanguage, Filter, UserName, AddressDrop) {
	//alert("PostcodeAnywhere_Interactive_Find_v1_10Begin_collection");
	var script = document.createElement("script"),
	head = document.getElementsByTagName("head")[0];
	var SearchTerm=ukpostcode(document.getElementById(id).value);
	document.getElementById(id).value = SearchTerm;
	collection_dropdown = AddressDrop;

	// Build the query string

	script.src = PostcodeAnywhere_Interactive_Find_URL(SearchTerm, PreferredLanguage, Filter, UserName, 'PostcodeAnywhere_Interactive_Find_v1_10End_collection');

	// Make the request
	script.onload = script.onreadystatechange = function () {
		if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
			script.onload = script.onreadystatechange = null;
			if (head && script.parentNode)
				head.removeChild(script);
		}
	}

	head.insertBefore(script, head.firstChild);
}


function PostcodeAnywhere_Interactive_Find_v1_10End_collection(response)
{
	//alert("PostcodeAnywhere_Interactive_Find_v1_10End_collection");
	//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 {
			document.getElementById(collection_dropdown).style.display = '';
			document.getElementById(collection_dropdown).options.length = 0;
			document.getElementById(collection_dropdown).options.add(new Option("Please select the address"));
			for (var i=0;i<response.length;i++)
				document.getElementById(collection_dropdown).options.add(new Option(response[i].StreetAddress + ", " + response[i].Place, response[i].Id));
		}
	}
}
function PostcodeAnywhere_Interactive_RetrieveById_v1_20Begin_collection(Id, PreferredLanguage, UserName, IdNo) {
	//alert("PostcodeAnywhere_Interactive_RetrieveById_v1_20Begin_collection");
    var script = document.createElement("script"),
        head = document.getElementsByTagName("head")[0];
	collection_id = parseInt(IdNo);

    script.src = PostcodeAnywhere_Interactive_RetrieveById_URL(Id, PreferredLanguage, UserName, 'PostcodeAnywhere_Interactive_RetrieveById_v1_20End_collection');

    // Make the request
    script.onload = script.onreadystatechange = function () {
        if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
            script.onload = script.onreadystatechange = null;
            if (head && script.parentNode)
                head.removeChild(script);
        }
    }

    head.insertBefore(script, head.firstChild);
}


function PostcodeAnywhere_Interactive_RetrieveById_v1_20End_collection(response) {
    // Test for an error
	//alert("PostcodeAnywhere_Interactive_RetrieveById_v1_20End_collection");
    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, there were no results");
        else {
            // PUT YOUR CODE HERE
            //FYI: The output is an array of key value pairs (e.g. response[0].Udprn), the keys being:
            //Udprn
            //Company
            //Department
            //Line1
            //Line2
            //Line3
            //Line4
            //Line5
            //PostTown
            //County
            //Postcode
            //Mailsort
            //Barcode
            //Type
            //DeliveryPointSuffix
            //SubBuilding
            //BuildingName
            //BuildingNumber
            //PrimaryStreet
            //SecondaryStreet
            //DoubleDependentLocality
            //DependentLocality
            //PoBox
            //PrimaryStreetName
            //PrimaryStreetType
            //SecondaryStreetName
            //SecondaryStreetType
            //CountryName
			//var retstring = response[0].Company + "<br>" + response[0].Line1 + "<br>" + response[0].Line2 + "<br>" + response[0].Line3 + "<br>" + response[0].PostTown + "<br>" + response[0].County + "<br>" + response[0].Postcode;
			SetPostcodeAnywhereBacktoFields(collection_id, response[0].Company, response[0].Line1, response[0].Line2, response[0].Line3, response[0].PostTown, response[0].Country, response[0].Postcode, response[0].County, response[0].Type);
			document.getElementById(collection_dropdown).style.display = 'none';
        }
    }
}

function PostcodeAnywhere_Interactive_Find_v1_10Begin_delivery(id, PreferredLanguage, Filter, UserName, AddressDrop) {
	var script = document.createElement("script"),
	head = document.getElementsByTagName("head")[0];
	var SearchTerm=ukpostcode(document.getElementById(id).value);
	document.getElementById(id).value = SearchTerm;
	delivery_dropdown=AddressDrop;

	// Build the query string
	script.src = PostcodeAnywhere_Interactive_Find_URL(SearchTerm, PreferredLanguage, Filter, UserName, 'PostcodeAnywhere_Interactive_Find_v1_10End_delivery');

	// Make the request
	script.onload = script.onreadystatechange = function () {
		if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
			script.onload = script.onreadystatechange = null;
			if (head && script.parentNode)
				head.removeChild(script);
		}
	}

	head.insertBefore(script, head.firstChild);
}


function PostcodeAnywhere_Interactive_Find_v1_10End_delivery(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 {
			document.getElementById(delivery_dropdown).style.display = '';
			document.getElementById(delivery_dropdown).options.length = 0;
			document.getElementById(delivery_dropdown).options.add(new Option("Please select the address"));
			for (var i=0;i<response.length;i++)
				document.getElementById(delivery_dropdown).options.add(new Option(response[i].StreetAddress + ", " + response[i].Place, response[i].Id));
		}
	}
}
function PostcodeAnywhere_Interactive_RetrieveById_v1_20Begin_delivery(Id, PreferredLanguage, UserName, IdNo) {
    var script = document.createElement("script"),
        head = document.getElementsByTagName("head")[0];
	script.src = PostcodeAnywhere_Interactive_RetrieveById_URL(Id, PreferredLanguage, UserName, 'PostcodeAnywhere_Interactive_RetrieveById_v1_20End_delivery');
	delivery_id = parseInt(IdNo);

    // Make the request
    script.onload = script.onreadystatechange = function () {
        if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
            script.onload = script.onreadystatechange = null;
            if (head && script.parentNode)
                head.removeChild(script);
        }
    }

    head.insertBefore(script, head.firstChild);
}

function PostcodeAnywhere_Interactive_RetrieveById_v1_20End_delivery(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, there were no results");
        else {
            // PUT YOUR CODE HERE
            //FYI: The output is an array of key value pairs (e.g. response[0].Udprn), the keys being:
            //Udprn
            //Company
            //Department
            //Line1
            //Line2
            //Line3
            //Line4
            //Line5
            //PostTown
            //County
            //Postcode
            //Mailsort
            //Barcode
            //Type
            //DeliveryPointSuffix
            //SubBuilding
            //BuildingName
            //BuildingNumber
            //PrimaryStreet
            //SecondaryStreet
            //DoubleDependentLocality
            //DependentLocality
            //PoBox
            //PrimaryStreetName
            //PrimaryStreetType
            //SecondaryStreetName
            //SecondaryStreetType
            //CountryName
			//var retstring = response[0].Company + "<br>" + response[0].Line1 + "<br>" + response[0].Line2 + "<br>" + response[0].Line3 + "<br>" + response[0].PostTown + "<br>" + response[0].County + "<br>" + response[0].Postcode + "<br>" + response[0].Type;
			SetPostcodeAnywhereBacktoFields(delivery_id, response[0].Company, response[0].Line1, response[0].Line2, response[0].Line3, response[0].PostTown, response[0].Country, response[0].Postcode, response[0].County, response[0].Type);
			document.getElementById(delivery_dropdown).style.display = 'none';
        }
    }
}

