/*! Open Postcodes jQuery Plugin - v1.0.0 - 2014-06-24 * https://github.com/OpenPostcodes/postcodes-jquery * Copyright (c) 2014 Open Postcodes; MIT */ (function(e){"use strict";function i(t){this.config={};e.extend(this,r);if(t){e.extend(this,t)}var n={};for(var i in this.output_fields){if(this.output_fields[i]!==undefined){n[i]=e(this.output_fields[i])}}this.$output_fields=n}var t=[];var n;var r={api_key:"",output_fields:{line_1:"#line1",line_2:"#line2",line_3:"#line3",post_town:"#town",postcode:"#postcode",postcode_inward:undefined,postcode_outward:undefined,udprn:undefined,dependant_locality:undefined,double_dependant_locality:undefined,thoroughfare:undefined,dependant_thoroughfare:undefined,building_number:undefined,building_name:undefined,sub_building_name:undefined,po_box:undefined,department_name:undefined,organisation_name:undefined,postcode_type:undefined,su_organisation_indicator:undefined,delivery_point_suffix:undefined},api_endpoint:"https://api.openpostcodes.com/v1",input:undefined,$input:undefined,input_label:"Enter your Postcode",input_muted_style:"color:#CBCBCB;",input_class:"",input_id:"opc_input",button:undefined,$button:undefined,button_id:"opc_button",button_label:"Find your Address",button_class:"",button_disabled_message:"Fetching Addresses...",$dropdown:undefined,dropdown_separator:",",dropdown_id:"opc_dropdown",dropdown_select_message:"Select your Address",dropdown_class:"",$error_message:undefined,error_message_id:"opc_error_message",error_message_postcode_invalid:"Please recheck your postcode, it seems to be incorrect",error_message_postcode_not_found:"Your postcode could not be found. Please type in your address",error_message_default:"We were not able to your address from your Postcode. Please input your address manually",error_message_class:"",lookup_interval:1e3,debug_mode:false,onLookupSuccess:undefined,onLookupError:undefined,onAddressSelected:undefined};i.prototype.setupPostcodeInput=function(e){this.$context=e;this.setupInputField();this.setupLookupButton()};i.prototype.setupInputField=function(){var t=this;if(e(this.input).length){this.$input=e(this.input).first()}else{this.$input=e("",{type:"text",id:this.input_id,value:this.input_label}).appendTo(this.$context).addClass(this.input_class).val(this.input_label).attr("style",this.input_muted_style).submit(function(){return false}).keypress(function(e){if(e.which===13){t.$button.trigger("click")}}).focus(function(){t.$input.removeAttr("style").val("")}).blur(function(){if(!t.$input.val()){t.$input.val(t.input_label);t.$input.attr("style",t.input_muted_style)}})}return this.$input};i.prototype.setupLookupButton=function(){var t=this;if(e(this.button).length){this.$button=e(this.button).first()}else{this.$button=e("",{html:this.button_label,id:this.button_id,type:"button"}).appendTo(this.$context).addClass(this.button_class).attr("onclick","return false;").submit(function(){return false})}this.$button.click(function(){var e=t.$input.val();t.disableLookup();t.clearAll();t.lookupPostcode(e)});return this.$button};i.prototype.disableLookup=function(e){e=e||this.button_disabled_message;this.$button.prop("disabled",true).html(e)};i.prototype.enableLookup=function(){var e=this;if(e.lookup_interval===0){e.$button.prop("disabled",false).html(e.button_label)}else{setTimeout(function(){e.$button.prop("disabled",false).html(e.button_label)},e.lookup_interval)}};i.prototype.clearAll=function(){this.setDropDown();this.setErrorMessage();this.setAddressFields()};i.prototype.removeAll=function(){this.$context=null;e.each([this.$input,this.$button,this.$dropdown,this.$error_message],function(e,t){if(t){t.remove()}})};i.prototype.lookupPostcode=function(t){var n=this;if(!e.openPostcodes.validatePostcodeFormat(t)){this.enableLookup();return n.setErrorMessage(this.error_message_postcode_invalid)}e.openPostcodes.lookupPostcode(t,n.api_key,function(e){n.response_code=e.code;n.response_message=e.message;n.result=e.result;n.enableLookup();if(n.response_code===2e3){n.setDropDown(n.result)}else if(n.response_code===4040){n.setErrorMessage(n.error_message_postcode_not_found)}else{if(n.debug_mode){n.setErrorMessage("("+n.response_code+") "+n.response_message)}else{n.setErrorMessage(n.error_message_default)}}if(n.onLookupSuccess){n.onLookupSuccess(e)}},function(){n.setErrorMessage("Unable to connect to server");n.enableLookup();if(n.onLookupError){n.onLookupError()}})};i.prototype.setDropDown=function(t){var n=this;if(this.$dropdown&&this.$dropdown.length){this.$dropdown.remove();delete this.$dropdown}if(!t){return}var r=e("",{id:n.dropdown_id}).addClass(n.dropdown_class);e("",{value:"open",text:n.dropdown_select_message}).appendTo(r);var i=t.length;for(var s=0;s",{value:s,text:t[s].line_1+(t[s].line_2===""?"":n.dropdown_separator+" "+t[s].line_2)}).appendTo(r)}r.appendTo(n.$context).change(function(){var r=e(this).val();if(r>=0){n.setAddressFields(t[r]);if(n.onAddressSelected){n.onAddressSelected.call(this,t[r])}}});n.$dropdown=r;return r};i.prototype.setErrorMessage=function(t){if(this.$error_message&&this.$error_message.length){this.$error_message.remove();delete this.$error_message}if(!t){return}this.$error_message=e("
",{html:t,id:this.error_message_id}).addClass(this.error_message_class).appendTo(this.$context);return this.$error_message};i.prototype.setAddressFields=function(e){e=e||{};for(var t in this.$output_fields){this.$output_fields[t].val(e[t]||"")}};e.openPostcodes={defaults:function(){return r},setup:function(e){n=new i(e);t.push(n)},validatePostcodeFormat:function(e){return!!e.match(/^[a-zA-Z0-9]{1,4}\s?\d[a-zA-Z]{2}$/)},lookupPostcode:function(t,n,i,s){var o=r.api_endpoint,u="postcodes",a=[o,u,t].join("/"),f={url:a,data:{api_key:n},dataType:"jsonp",timeout:5e3,success:i};if(s){f.error=s}e.ajax(f)},clearAll:function(){var e=t.length;for(var n=0;n