/*
functions for handling postcode lookups

The Address object these handle usually comes from a <cfwddx> of an Address struct returned from the coldfusion postcode fetching libraries.
*/

function setPaymentAddress(Address) {
	document.custdetails.adr1.value = Address.address1;
	if(document.custdetails.adr1b && typeof(Address.address2) != 'undefined')
		document.custdetails.adr1b.value = Address.address2;
	document.custdetails.adr2.value = Address.town;
	document.custdetails.adr3.value = Address.county;
	document.custdetails.pcde.value = Address.postcode;
}

function setDeliveryAddress(Address) {
	document.custdetails.ddr1.value = Address.address1;
	if(document.custdetails.ddr1b && typeof(Address.address2) != 'undefined')
		document.custdetails.ddr1b.value = Address.address2;
	document.custdetails.ddr2.value = Address.town;
	document.custdetails.ddr3.value = Address.county;
	document.custdetails.dcde.value = Address.postcode;
}

/*
	Opens another page in a window which will populate the form with address data
*/


function search_postcode(store, house_number, postcode, callback)
{
	// optional param do_return - if this is called from an href, it mustn't return anything, if it's called from an 
	//image button, it needs to return false.
  if(search_postcode.arguments.length > 4)
  	do_return = search_postcode.arguments[4];
  else
  	do_return = true;
	
	if (postcode == '') {
		alert('You must enter a postcode.');
	} else {
		//check the postcode is in a valid format
		var objRegExp  = /[A-Z]{1,2}\d[A-Z\d]?[ ]*\d[ABD-HJLNP-UW-Z]{2}/;
		 
		if ( objRegExp.test(postcode.toUpperCase()) ) {
		
			tmpAddrLineToggle="";
			if(document.custdetails.adr1b)
				tmpAddrLineToggle="&noOfAddressLines=2";
			link_addy = "/mall/pafcode.cfm?store=" + store + "&callback=" + callback + tmpAddrLineToggle + "&postcode=" + escape(postcode.toUpperCase());
			window_options = 'WIDTH=600,HEIGHT=100,RESIZABLE=no,scrollbars=no';
			window_handler = window.open(link_addy, 'Postcode', window_options);
			window_handler.focus();
		} else {
			alert("Invalid Postcode Format (" + postcode + ")\n\nPlease check you have entered your postcode correctly.\nNote this service is only available for UK addresses.");
		}
	}
	if(do_return)
		return false;
}
	

