var super_form_name;

function find_address(postcode, form_name) {
	var sUrl = '/scripts/address_finder.php';

	/*
	* Remember to encode the key-value string if and when
	* the string contains special characters.
	*/
	var postData = "postcode=" + postcode + "&form_name=" + form_name;
	
	if (postcode.length > 3)
		ajax({
			url : sUrl,
			method : "POST",
			postBody : postData,
			fillDiv : form_name + 'address_response',
			busyDiv : form_name + 'spinner'
		});
}

function populate_address(address, form_name) {
	var sUrl = '/scripts/address_finder.php';

	/*
	* Remember to encode the key-value string if and when
	* the string contains special characters.
	*/
	var postData = "address=" + address;
	
	super_form_name = form_name;
	
	if (address != '')
		ajax({
			url : sUrl,
			method : "POST",
			postBody : postData,
			busyDiv : form_name + 'spinner',
			onSuccess : handle_success,
			onFailure : handle_failure
		});
}

function handle_success(req) {
	if(req.responseText !== undefined){
		var address_details = req.responseText.split("\n");
		
		$(super_form_name + 'street').value = address_details[0];
		$(super_form_name + 'street2').value = address_details[1]
		$(super_form_name + 'town').value = address_details[2];
		$(super_form_name + 'city').value = address_details[3];
		$(super_form_name + 'county').value = address_details[4];
		$(super_form_name + 'postcode').value = address_details[5];
	}
}
	
function handle_failure() {
	$(super_form_name + 'address_response').innerHTML = '<p class="error">Unfortunately the address finder service is not working at the moment; please fill in your address manually.</p>';
}

function capitalize(elem) {
	$(elem).value = $(elem).value.toUpperCase();
}

function copy_billing() {
	// if (b.checked) {
		// $('shipto_title').value = $('billto_title').value;
		$('shipto_firstname').value = $('billto_firstname').value;
		$('shipto_lastname').value = $('billto_lastname').value;
		$('shipto_street').value = $('billto_street').value;
		$('shipto_street2').value = $('billto_street2').value;
		$('shipto_town').value = $('billto_town').value;
		$('shipto_city').value = $('billto_city').value;
		$('shipto_county').value = $('billto_county').value;
		$('shipto_postcode').value = $('billto_postcode').value;
		$('shipto_country').value = $('billto_country').value;
		// $('shipto_firstname').value = '';
		// $('shipto_lastname').value = '';
		// $('shipto_street').value = '';
		// $('shipto_street2').value = '';
		// $('shipto_town').value = '';
		// $('shipto_city').value = '';
		// $('shipto_county').value = '';
		// $('shipto_postcode').value = '';
		// $('shipto_country').value = '';
	// }
}

function update_billto_(n) {
	// if (!($('shipto_billing').checked)) {
	// 	$('shipto_' + n).value = $('billto_' + n).value;
	// }
}

function update_shipto_(n) {
	// if (!($('shipto_billing').checked)) {
	// 	// $('billto_' + n).value = $('shipto_' + n).value;
	// 	$('shipto_billing').checked = true;
	// }
}

window.onload = function() {
	if (document.getElementById('shipto_billing')) {
		$('shipto_billing').style.display = 'block';
	}
	// copy_billing($('shipto_billing'));
}