var isle_of_man_error = "<span class=\"no-isle-of-man\">Sorry, we don't currently support Isle of Man postcodes, please enter your address manually</span>";
var ireland_error = "<span class=\"no-ireland\">Sorry, no stores in Ireland, please enter your address manually</span>";
var ireland_contact_us = 'Sorry, we don\'t have any stores in Ireland, why not <a href="/contact-us">contact us</a>';
$(document).ready(function() {
	$('#postcode-search').click(function(event) {
		event.preventDefault();
		
		var field = $('#postcode');
		var form = $(this).parent().parent().parent();
		var postcode = field.val();
		
		// If the postcode begins with BT and we *can* fail, then do so now
		if(postcode.toLowerCase().indexOf('bt') == 0 && !field.hasClass('cant-fail')) {
			form.html(ireland_contact_us);
		} else {
			$.ajax({
				type: 'GET',
				url: '/postcode_search.php?postcode='+postcode,
				success: function(response) {
					if(response == 'IM' || response == 'BT') {
						var location = response;
						$.ajax({
							type: 'GET',
							url: '/manual_address.php',
							success: function(response) {
								// Remove some no longer needed divs
								$('#postcode-div').remove();
								$('#address-dropdown-placeholder').remove();
								
								// Put in the fields to enter the address manually
								if(location == 'IM') {
									$('#manual-address-placeholder').html(isle_of_man_error+response);
								} else if(location == 'BT') {
									if(field.hasClass('cant-fail')) {
										$('#manual-address-placeholder').html(ireland_error+response);
									} else {
										form.html(ireland_error);
									}
								}
								$('#postcode').val(postcode);
							}
						});
					} else {
						// We need to reset the style at the end as we had to override the default padding-bottom before
						// So as to stop the extra white space, but we now want that padding back
						$('#address-dropdown-placeholder').html(response).attr('style', '');
					}
				}
			});
		}
	});
	
	$('#postcode-search-mini').click(function(event) {
		event.preventDefault();
		
		var field = $('#postcode');
		var form = $(this).parent().parent().parent();
		var postcode = field.val();
		
		// If the postcode begins with BT and we *can* fail, then do so now
		if(postcode.toLowerCase().indexOf('bt') == 0 && !field.hasClass('cant-fail')) {
			form.html(ireland_contact_us);
		} else {
			$.ajax({
				type: 'GET',
				url: '/postcode_search.php?mini=1&postcode='+postcode,
				success: function(response) {
					if(response == 'IM' || response === 'BT') {
						var location = response;
						$.ajax({
							type: 'GET',
							url: '/manual_address_mini.php',
							success: function(response) {
								// Remove some no longer needed divs
								$('#postcode-div').remove();
								$('#address-dropdown-placeholder').remove();
								
								// Put in the fields to enter the address manually
								if(location == 'IM') {
									$('#manual-address-placeholder').html(isle_of_man_error+response);
								} else if(location == 'BT') {
									$('#manual-address-placeholder').html(ireland_error+response);
								}
								$('#postcode').val(postcode);
							}
						});
					} else {
						// We need to reset the style at the end as we had to override the default padding-bottom before
						// So as to stop the extra white space, but we now want that padding back
						$('#manual-address-placeholder').remove();
						$('#address-dropdown-placeholder').html(response).attr('style', '');
					}
				}
			});
		}
	});
	
	$('#postcode-anywhere-address').live('change', function() {
		var address_id = $(this).val();
		if(address_id != '') {
			$.ajax({
				type: 'GET',
				url: '/postcode_search.php?address_id='+address_id,
				success: function(response) {
					$('#address-details-placeholder').html(response);
				}
			});
		}
	});
	
	$('#postcode-anywhere-address').live('click', function() {
		var address_id = $(this).val();
		if(address_id != '') {
			$.ajax({
				type: 'GET',
				url: '/postcode_search.php?address_id='+address_id,
				success: function(response) {
					$('#address-details-placeholder').html(response);
				}
			});
		}
	});
	
	$('#enter-address-manually.full').live('click', function(event) {
		event.preventDefault();
		
		$.ajax({
			type: 'GET',
			url: '/manual_address.php',
			success: function(response) {
				// Remove some no longer needed divs
				$('#postcode-div').remove();
				$('#address-dropdown-placeholder').remove();
				
				// Put in the fields to enter the address manually
				$('#manual-address-placeholder').html(response);
			}
		});
	});
	
	$('#enter-address-manually.mini').live('click', function(event) {
		event.preventDefault();
		
		$.ajax({
			type: 'GET',
			url: '/manual_address_mini.php',
			success: function(response) {
				// Remove some no longer needed divs
				$('#postcode-div').remove();
				$('#address-dropdown-placeholder').remove();
				
				// Put in the fields to enter the address manually
				$('#manual-address-placeholder').html(response);
			}
		});
	});
});









