
  var geocoder;

  $(document).ready(function() {
    geocoder = new google.maps.Geocoder();
  });

  function valid_postcode(postcode) {
	    return postcode.match(/[a-zA-Z]{1,2}[0-9][0-9A-Za-z]? [0-9][a-zA-Z]{2}/);
  }
  



$(document).ready(function() {
	
	$('#manual-address-entry').hide();
	
	$('#enter-address-manually').click(function(event) {
		event.preventDefault();
		$('#manual-address-entry').show();
	} );

	$('#postcode-search').click(function(event) {
		event.preventDefault();
		$('#postcode + p').remove();
		if(!valid_postcode($.trim($("#postcode").val()))) {
			$('#postcode').after('<p>That postcode is invalid</p>');
			return false;
			} else {
				 $.ajax({
				 type: 'GET',
				 url: '/postcode-search/'+$('#postcode').val() + '/',
				 dataType: 'json',
				 cache: 'false',
				 contentType: 'application/json',
				 success: function(data, status, request) {
					 											var addresses = '<label>&nbsp;</label><select name="postcode-anywhere-address" id="postcode-anywhere-address" class="new-field"><option value="">Select address</option>';
					 											
					 											$.each(data, function(index, value) {
					 												addresses += '<option value="' + index + '">' + value + '</option>';
					 											});
					 											addresses += '</select>';
					 											$('#address-dropdown-placeholder').html(addresses);
					 											$('#postcode-anywhere-address').bind('change', function() {
					 											var address_id = $(this).val();
					 											if(address_id != '') {
					 												$.ajax({
					 													type: 'GET',
					 													url: '/get-address/'+address_id+'/',
					 													dataType: 'json',
					 													success: function(response) {
					 													// Populate hidden (manual entry) form fields
					 										            if (response.SubBuilding != null) {
					 										            	$('input#house_name').val(response.SubBuilding + ' ' + response.BuildingName);
					 										            	} else {
					 										            		$('input#house_name').val(response.BuildingName);
					 										            	}
					 													$('input#house_number').val(response.BuildingNumber);
					 													$('input#street1').val(response.PrimaryStreet);
					 													$('input#street2').val(response.DependentLocality);
					 													$('input#city').val(response.PostTown);
					 													$('select#region').val(response.County);
					 													$('input#postcode').val(response.Postcode);
					 												}
					 												});
					 											}
					 											return nearest_store();					 											});

				 											}
				 });
			}
											});
});

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: 'http://forms.magnet.co.uk/postcode_search.php?postcode='+postcode,
				success: function(response) {
					if(response == 'IM' || response == 'BT') {
						var location = response;
						$.ajax({
							type: 'GET',
							url: 'http://forms.magnet.co.uk/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: 'http://forms.magnet.co.uk/postcode_search.php?mini=1&postcode='+postcode,
				success: function(response) {
					if(response == 'IM' || response === 'BT') {
						var location = response;
						$.ajax({
							type: 'GET',
							url: 'http://forms.magnet.co.uk/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').empty();
						$('#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: 'http://forms.magnet.co.uk/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: 'http://forms.magnet.co.uk/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: 'http://forms.magnet.co.uk/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: 'http://forms.magnet.co.uk/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);
			}
		});
	});
});


