((function ($) {

	// Regular expression 'borrowed' from wikipedia (http://en.wikipedia.org/wiki/Postal_codes_in_the_United_Kingdom#Validation)	
	var POSTCODE = /^([A-Z]{1,2}[0-9R][0-9A-Z]?) *([0-9][ABD-HJLNP-UW-Z]{2})$/i;	
	$(document).ready(function () {			
	
		$("input").keypress(function (e){
			if (e.which == 13){
		    		e.preventDefault();
		    		$("input[type=button]").trigger("click");
		  	}
		});


		$("input[type=button]").click(function () {			
			var inputValue = $("#postcode").val().replace(/\s/g, "");			
			var postcode;			
			var postcodeFull;			
			var postcodeFirstPart;						
			postcode = POSTCODE.exec(inputValue);								
			if (postcode === null) {				
				postcodeFirstPart = inputValue.toUpperCase();			
			} else {				
				postcodeFirstPart = postcode[1].toUpperCase();				
				postcodeFull = postcode[0].toUpperCase();			
			}              
			var url = '/collect/the-times/the-times-subscription/outside-offers?postcode='+inputValue;
			if ($.inArray(postcodeFirstPart, postCodes) !== -1) {
				url = '/collect/the-times/the-times-subscription/inside-offers?postcode='+postcodeFirstPart;
			} else if ($.inArray(postcodeFull, postCodeExact) !== -1) {
				url = '/collect/the-times/the-times-subscription/inside-offers?postcode='+postcodeFull;
			} 
 			document.location.href=escape(url);		
		});	
	});
	
	
})(jQuery));
