
function PostcodeLookUp(pPostcode, pArea)
{
	if (pArea == 0){pArea = '';}

	pPostcode = pPostcode.toUpperCase();
	$('#Postcode' + pArea).val(pPostcode);

	var response = $.ajax({
		url: 'index.php?a=Ajax&b=PostcodeSearch&c=CheckPostcode&Postcode=' + escape(pPostcode), //'postcodesearch1.php?postcode=' + escape(pPostcode),
		dataType: 'xml',
		async: false
	}).responseText;
	response = response.replace('onchange="SPLAddressChange(this);"', 'id="AddressList' + pArea + '"');

	// interpret postcode lookup data
	$('#AddressSearchArea' + pArea).show();
	$('#AddressSearchArea' + pArea).html(response);

	// add individual address selection behaviour
	$('#AddressList' + pArea).change(function(){
		PostcodePick($('#AddressList' + pArea + ' option:selected').val(), pArea);
	});
}

function PostcodePick(pId, pArea)
{
	if (pArea == 0){pArea = '';}

	/*var response = $.ajax({
		url: 'index.php?a=Ajax&b=PostcodeSearch&c=CheckAddress&AddressID=' + pId, //'postcodesearch2.php?AddressID=' + pId,
		dataType: ($.browser.msie) ? 'text' : 'xml',
		async: false
	}).responseText;*/

	var response;
	if ($.browser.msie)
	{
		$.ajax({
			url: 'index.php?a=Ajax&b=PostcodeSearch&c=CheckAddress&AddressID=' + pId,
			dataType: 'text',
			async: false,
			success: function(data){
				response = new ActiveXObject("Microsoft.XMLDOM");
				response.async = false;
				response.loadXML(data);
			}
		});
	}
	else
	{
		response = $.ajax({
			url: 'index.php?a=Ajax&b=PostcodeSearch&c=CheckAddress&AddressID=' + pId, //'postcodesearch2.php?AddressID=' + pId,
			dataType: 'xml',
			async: false
		}).responseText;
	}

	company = $('organisation', response).text();
	line1 = $('line1', response).text();
	line2 = $('line2', response).text();
	line3 = $('line3', response).text();
	town = $('town', response).text();
	county = $('county', response).text();

	$('#CompanyName' + pArea).val(company);
	if (line3 != "")
	{
		$('#Address1' + pArea).val(line1 + ', ' + line2);
		$('#Address2' + pArea).val(line3);
	}
	else
	{
		$('#Address1' + pArea).val(line1);
		$('#Address2' + pArea).val(line2);
	}
	$('#Town' + pArea).val(town);
	$('#County' + pArea).val(county);

	$('#HiddenAddress' + pArea).show();
	$('#AddressSearchArea' + pArea).hide();

	if (typeof(checkPageComplete) == 'function')
	{
		checkPageComplete(1);
		checkPageComplete(2);
		checkPageComplete(3);
	}
}

