﻿// --------------------------------------------------------------------------------
// Variables
var ajaxRequest = null;
var getAddressesTimeout = null;
var getAddressTimeout = null;


// --------------------------------------------------------------------------------
// Open the address pop-up window for those without Ajax capability
function openAddressFinderPopUp() {
	// Do nothing - the address finder pop-up is no longer used
}


// --------------------------------------------------------------------------------
// Show the main address section
function showAddressInputs() {
	document.getElementById('trAddress1').style.display = '';
	document.getElementById('trAddress2').style.display = '';
	document.getElementById('trTown').style.display = '';
	document.getElementById('trCounty').style.display = '';
	document.getElementById('trPostcode').style.display = '';
	document.getElementById('trCountry').style.display = '';
}


// --------------------------------------------------------------------------------
// Hide the main address section
function hideAddressInputs() {
	document.getElementById('trAddress1').style.display = 'none';
	document.getElementById('trAddress2').style.display = 'none';
	document.getElementById('trTown').style.display = 'none';
	document.getElementById('trCounty').style.display = 'none';
	document.getElementById('trPostcode').style.display = 'none';
	document.getElementById('trCountry').style.display = 'none';
}


// --------------------------------------------------------------------------------
// Hide the input form and show the address list
function showAddressList() {
	document.getElementById('trAddressFinderIntro').style.display = 'none';
	document.getElementById('trAFDMoreInfo').style.display = 'none';
	document.getElementById('trAFDPostcode').style.display = 'none';
	document.getElementById('trNonUKAddress').style.display = 'none';
	document.getElementById('spnSearching').style.display = 'none';
	document.getElementById('trAddressList').style.display = '';
}


// --------------------------------------------------------------------------------
// Show the address finder section
function showAddressFinder() {
	document.getElementById('trAddressFinder').style.display = '';
}


// --------------------------------------------------------------------------------
// Hide the address finder section
function hideAddressFinder() {
	document.getElementById('trAddressFinder').style.display = 'none';
}


// --------------------------------------------------------------------------------
// Show the address finder pop-up section
function showAddressFinderPopUp() {
	// The address finder pop-up is no longer used, so show the address inputs instead
	showAddressInputs();
}


// --------------------------------------------------------------------------------
// Hide the address finder pop-up section
function hideAddressFinderPopUp() {
	document.getElementById('trAddressFinderPopUp').style.display = 'none';
}


// --------------------------------------------------------------------------------
// Get the address list from the server
function getAddresses() {
	ajaxRequest = jSki.ajax.createAjaxRequest();
	var afdPostcode = document.getElementById('strAFDPostcode').value;
	var afdProperty = document.getElementById('strAFDMoreInfo').value;
	if (afdPostcode == '') {
		alert('Please enter your postcode to find your address.');
	} else {
		afdPostcode = afdPostcode.toUpperCase();
		var url = '/skiclub/webservices/addressfinder.asmx/GetAddressList?strPostcode=' + afdPostcode + '&strProperty=' + afdProperty;
		getAddressesTimeout = window.setTimeout(showAddressesError, 10000);
		jSki.ajax.sendAjaxRequest(ajaxRequest, url, showAddresses, showAddressesError, 10000);
	}
}


// --------------------------------------------------------------------------------
// Show the address list (if any) for the user to choose their address
function showAddresses() {
	if (ajaxRequest.readyState == 4) {
		if (ajaxRequest.status == 200) {
			window.clearTimeout(getAddressesTimeout);
			var response = ajaxRequest.responseXML;
			var addressList = response.getElementsByTagName('AddressList');
			if (addressList[0] == null) {
				showAddressesError();
			} else {
				if (addressList[0].hasChildNodes()) {
					var addresses = addressList[0].childNodes;
					var list = '';
					var address = '';
					var postkey = '';
					for (var i = 0; i < addresses.length; i++) {
						if (addresses[i].hasChildNodes()) {
							address = addresses[i].firstChild.nodeValue;
							postkey = addresses[i].getAttribute('postkey');
							list += '<li><a href="javascript:getAddress(\'' + postkey + '\');">' + address + '</a></li>';
						}
					}
					document.getElementById('ulAddressList').innerHTML = list;
					showAddressList();
				} else {
					showAddressesError();
				}
			}
		} else {
			showAddressesError();
		}
	} else {
		document.getElementById('spnSearching').style.display = '';
	}
}


// --------------------------------------------------------------------------------
// Get the address from the server
function getAddress(afdPostKey) {
	ajaxRequest = jSki.ajax.createAjaxRequest();
	if (afdPostKey == '') {
		insertAddressError();
	} else {
		var url = '/skiclub/webservices/addressfinder.asmx/GetAddress?strPostkey=' + afdPostKey;
		getAddressTimeout = window.setTimeout(insertAddressError, 10000);
		jSki.ajax.sendAjaxRequest(ajaxRequest, url, insertAddress, insertAddressError, 10000);
	}
}


// --------------------------------------------------------------------------------
// Enter the chosen address into the form and replace the address finder section
// with the main address section
function insertAddress() {
	if (ajaxRequest.readyState == 4) {
		if (ajaxRequest.status == 200) {
			window.clearTimeout(getAddressTimeout);
			var form = document.getElementById('aspnetForm');
			var response = ajaxRequest.responseXML;
			var address = response.getElementsByTagName('Address');
			if (form == null || address[0] == null) {
				insertAddressError();
			} else {
				if (address[0].hasChildNodes()) {
					var address1 = address[0].getElementsByTagName('Address1')[0];
					if (address1.hasChildNodes()) {
						form.strAddress1.value = address1.firstChild.nodeValue;
					}
					var address2 = address[0].getElementsByTagName('Address2')[0];
					if (address2.hasChildNodes()) {
						form.strAddress2.value = address2.firstChild.nodeValue;
					}
					var town = address[0].getElementsByTagName('Town')[0];
					if (town.hasChildNodes()) {
						form.strTown.value = town.firstChild.nodeValue;
					}
					var county = address[0].getElementsByTagName('County')[0];
					if (county.hasChildNodes()) {
						form.strCounty.value = county.firstChild.nodeValue;
					}
					var postcode = address[0].getElementsByTagName('Postcode')[0];
					if (postcode.hasChildNodes()) {
						form.strPostcode.value = postcode.firstChild.nodeValue;
					}
					hideAddressFinder();
					showAddressInputs();
				} else {
					insertAddressError();
				}
			}
		} else {
			insertAddressError();
		}
	}
}


// --------------------------------------------------------------------------------
// It's a non-UK address (or the user doesn't want to use the address finder),
// so hide the address finder section and show the main address section
function setNonUKAddress() {
	hideAddressFinder();
	showAddressInputs();
}


// --------------------------------------------------------------------------------
// There was a problem getting the address list from the server, so hide the address
// finder section and show the main address section
function showAddressesError() {
	ajaxRequest.abort();
	window.clearTimeout(getAddressesTimeout);
	hideAddressFinder();
	showAddressInputs();
	alert('Sorry, there was a problem finding your address.\n\nPlease type in your address.');
}


// --------------------------------------------------------------------------------
// There was a problem getting the address from the server, so hide the address
// finder section and show the main address section
function insertAddressError() {
	ajaxRequest.abort();
	window.clearTimeout(getAddressTimeout);
	hideAddressFinder();
	showAddressInputs();
	alert('Sorry, there was a problem finding your address.\n\nPlease type in your address.');
}


// --------------------------------------------------------------------------------
// If the browser is Ajax enabled, hide the main address section and show the address finder section
function initialiseAddressFinder() {
	if (document.getElementById('AddressFinder')) {
		if (jSki.ajax.createAjaxRequest()) {
			hideAddressInputs();
			showAddressFinder();
		}
	}
}

// --------------------------------------------------------------------------------
// Initialise the address finder when the page has loaded
jSki.utility.addHandler(window, 'load', initialiseAddressFinder);

