;(function($){$.fn.postcoder=function(service,options){var opts=$.extend({},$.fn.postcoder.defaults,options);if(!eval('$.fn.postcoder.'+service)){alert("The service '"+service+"' doesn't exist!");return false}else{opts=$.extend({},eval('$.fn.postcoder.'+service+'.defaults'),opts);opts.xmlmap=$.extend({},eval('$.fn.postcoder.'+service+'.defaults.xmlmap'),opts.xmlmap)}
if(opts.output=='thickbox'){opts=$.extend({},eval('$.fn.postcoder.thickbox.defaults'),opts)}
opts.service=service;opts.out=$('#'+opts.output);return this.each(function(){opts.search_btn=$(this);opts.form=$(this).parents('form');opts.search=$('input[id='+opts.search_id+']',opts.form).val();if(opts.output=='thickbox'){opts.out=$.fn.postcoder.thickbox.prepare_out(opts.service,opts.form)}
opts.search_btn.data('postcoder',opts);opts.search_btn.click(function(){var this_data=$(this).data('postcoder'),search=$('input[id='+this_data.search_id+']',this_data.form).val();if(search!=''){$.each(this_data.formmap,function(index,item){$('[name='+index+']').val('')});if(this_data.output=='thickbox'){tb_show();$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");$('#TB_load').show()}
if(this_data.search==search){$.fn.postcoder.parse_xml(this_data.results.xml,this_data)}else{this_data.search=search;$(this).data('postcoder',this_data);$.fn.postcoder.run(search,this_data)}}
return false})})};$.fn.postcoder.premise=function(){};$.fn.postcoder.street=function(){};$.fn.postcoder.match=function(){};$.fn.postcoder.grid=function(){};$.fn.postcoder.postzon=function(){};$.fn.postcoder.defaults={search_id:'pc_search',search_btn_id:'pc_btn',output:'thickbox',server_script:'postcoder_xml.php',wsdl:'http://www.postcoderwebsoap.co.uk/websoap/websoap.php?wsdl',identifier:'Example - WebSOAPTest',span_class:'thoroughfare',span_class_a:'premise',street_num:true};$.fn.postcoder.check_postcode=function(postcode){var pc_regexp=new RegExp('^(gir 0aa|((b|e|g|l|m|n|s|w)[0-9]([0-9]|[a-hjks-uw]){0,1}|(ab|al|ba|bb|bd|bh|bl|bn|br|bs|bt|ca|cb|cf|ch|cm|co|cr|ct|cv|cw|da|dd|de|dg|dh|dl|dn|dt|dy|ec|eh|en|ex|fk|fy|gl|gu|gy|ha|hd|hg|hp|hr|hs|hu|hx|ig|im|ip|iv|je|ka|kt|kw|ky|la|ld|le|ll|ln|ls|lu|me|mk|ml|ne|ng|nn|np|nr|nw|ol|ox|pa|pe|ph|pl|po|pr|rg|rh|rm|sa|se|sg|sk|sl|sm|sn|so|sp|sr|ss|st|sw|sy|ta|td|tf|tn|tq|tr|ts|tw|ub|wa|wc|wd|wf|wn|wr|ws|wv|xa|xb|xc|xd|xe|xf|xg|xh|xi|xj|yo|ze)[0-9]([0-9]|[abehmnprv-y]){0,1})(\\s+)([0-9])([abd-hjlnp-uw-z]){2})$','i');var pc_s_regexp=new RegExp('^([^\\s]+)(\\s+)([^\\s]+)$','i');postcode=$.trim(postcode.toUpperCase());if(pc_s_regexp.test(postcode)){pc_s_regexp.exec(postcode);postcode=RegExp.$1+' '+RegExp.$3}else{postcode=postcode.replace(/ /g,'');postcode=postcode.substr(0,postcode.length-3)+' '+postcode.substr(postcode.length-3,3)}
if(pc_regexp.test(postcode)){return postcode}else{return false}}
$.fn.postcoder.house_num=function(my_string){if(my_string.length==0)return false;var tmp_string=my_string.substr(0,my_string.length-1),is_house_num=true;$.each(tmp_string,function(index,item){var testchar=item[index];if(((testchar<'0')||(testchar>'9'))&&(testchar!='-')&&(testchar!='/')){is_house_num=false}});return is_house_num};$.fn.postcoder.array_impl=function(my_array,delimiter){if(delimiter==null)delimiter=", ";var out='',add_out='';$.each(my_array,function(index,item){add_out=my_array[index];if(add_out!=''&&add_out!=undefined){if(out!='')out+=delimiter;out+=add_out}});return out};$.fn.postcoder.wrap_string=function(my_string,pre_string,post_string){if(my_string!=null&&my_string!='')my_string=pre_string+my_string+post_string;return my_string};$.fn.postcoder.is_array=function(obj){if(obj.constructor.toString().indexOf('Array')==-1)return false;else
return true};$.fn.postcoder.in_array=function(find_item,arr){var result=false;$.each(arr,function(index,item){if(item==find_item)result=index});return result};$.fn.postcoder.behaviour=function(out,opts){$('ul:first',out).addClass('treeview-postcoder');$('ul:first > li > ul',out).each(function(){if($(this).children('li').length==1){$(this).find('a').append(', '+$(this).parent('li').children('span').text());$(this).parent('li').html($(this).children('li').html())}});if($('ul ul',out).length==1){$('li:first',out).addClass('open')}
$('ul:first',out).treeview({collapsed:true});return $('a',out).length};$.fn.postcoder.out=function(out,content,title,opts){out.html(content);var num_results=$.fn.postcoder.behaviour(out,opts);if(num_results==1){$.fn.postcoder.populate(opts.search_btn.data('count_0'),opts)}else{if(opts.output=='thickbox'){$.fn.postcoder.thickbox(title,out)}
$('a',out).click(function(){var a_title=$(this).attr('title');a_title=a_title.split('#');var address_data=$('#'+a_title[0]).data(a_title[1]),opts=$('#'+a_title[0]).data('postcoder');$.fn.postcoder.populate(address_data,opts);return false})}};$.fn.postcoder.populate_fields=function(address_data,formmap,opts){var fieldorder={};if(opts.linefields){var count=1;$.each(opts.linefields,function(index,item){var linefields=item.split(',');$.each(linefields,function(index,item){if(!eval('fieldorder.'+item)){eval('fieldorder.'+item+' = count;');count++}})});var lineorder_sort=function(a,b){return(b.seq<a.seq)-(a.seq<b.seq)}
$.each(formmap2,function(index,item){var line=item[1];line=line.split(',')})}};$.fn.postcoder.populate=function(address_data,opts){if(opts.output=='thickbox')tb_remove();premise=[];var formmap=opts.formmap;if(address_data.premise){var premise=address_data.premise.split(',');if($.fn.postcoder.house_num(premise[premise.length-1])){address_data.building_number=premise[premise.length-1];if(opts.building_num){address_data.premise=address_data.premise.substr(0,address_data.premise.length-address_data.building_number.length-1);if(address_data.dependent_street!=''||address_data.street=='')address_data.dependent_street=$.fn.postcoder.array_impl([address_data.building_number,address_data.dependent_street],' ');else
address_data.street=$.fn.postcoder.array_impl([address_data.building_number,address_data.street],' ')}}
premise=address_data.premise.split(',');$.each(premise,function(index,item){eval('address_data.premise_'+index+' = item;')});var formmap={};$.each(opts.formmap,function(index,item){var i_old=item.split(','),i_new=[],count=0;$.each(i_old,function(index,item){if(item=='premise'){var c=0;$.each(premise,function(index,item){i_new[count]='premise_'+c;count++;c++})}else{i_new[count]=item;count++}});eval('formmap.'+index+' = $.fn.postcoder.array_impl(i_new, ",");')});address_data.premise=premise.length}else{address_data.premise=0}
$.each(formmap,function(index,item){var field_value=$.fn.postcoder.field_value(address_data,item);$('[name='+index+']',opts.form).val(field_value)})};$.fn.postcoder.field_value=function(results,fields){var field_split=fields.split(',');$.each(field_split,function(index,item){field_split[index]=eval('results.'+field_split[index])});var field_value=$.fn.postcoder.array_impl(field_split);if(field_value==undefined||field_value==null)field_value='';return field_value};$.fn.postcoder.thickbox=function(title,out){var tb_opts=$.fn.postcoder.thickbox.defaults;tb_show(title,tb_opts.url+out.parents('div').attr('id'))};$.fn.postcoder.thickbox.prepare_out=function(service,$form){var id_out=service+$.fn.postcoder.thickbox.defaults.id_append;$.fn.postcoder.thickbox.defaults.inline_id=id_out;while($('#'+id_out).html()!=null){id_out+='_1'}
$($form).append('<div id="'+id_out+'"><div class="'+$.fn.postcoder.thickbox.defaults.inner+'"></div></div>');$('#'+id_out,$form).css({'display':'none'});var out=$('#'+id_out+' .'+$.fn.postcoder.thickbox.defaults.inner,$form);return out};$.fn.postcoder.thickbox.defaults={id_append:'_thickbox',inner:'thickbox_content',url:'#TB_inline?height=300&width=400&inlineId='};$.fn.postcoder.run=function(search,opts){$.ajax({type:'POST',url:opts.server_script,data:'search='+search+'&identifier='+opts.identifier+'&wsdl='+opts.wsdl+'&func='+opts.func,dataType:'xml',error:function(error_mess){$.fn.postcoder.out(opts.out,opts.error_mess,search,opts)},success:function(xml){opts.json=$.xmlToJSON(xml);$.fn.postcoder.parse_xml(xml,opts)}})};$.fn.postcoder.parse_xml=function(xml,opts){opts.results_xml=$.fn.postcoder.xml_array($('return',xml),opts.selectmap);opts.results={};opts.results.xml=xml;opts.search_btn.data('postcoder',opts);$.fn.postcoder.selection(opts)};$.fn.postcoder.selection=function(opts){var results=opts.results_xml,output='',display_data=results.data;if(results.children.length==0){$.fn.postcoder.populate(display_data,opts)}else if(opts.selectmap){output=$.fn.postcoder.selection_out(results.children,opts,display_data);$.fn.postcoder.out(opts.out,output,'Results for '+display_data.search_string,opts)}};$.fn.postcoder.selection_out=function(children,opts,parent_data,count){if(count==undefined)count=0;var output='<ul>\n';$.each(children,function(index,item){var children_len=children[index].children.length,span_class=opts.span_class,next_parent_data=$.extend({},parent_data,children[index].data);if(children_len==0)span_class=opts.span_class_a;output+='<li>\n';output+='<span class="'+span_class+'">';if(children_len==0){output+='<a href="" title="'+opts.search_btn.attr('id')+'#count_'+count+'">';opts.search_btn.data('count_'+count,next_parent_data);count++}
output+=$.fn.postcoder.array_impl(children[index].data);if(children_len==0){output+='</a>'}
output+='</span>\n';if(children[index].children.length>0){output+=$.fn.postcoder.selection_out(children[index].children,opts,next_parent_data,count);count+=children[index].children.length}
output+='</li>\n'});output+='</ul>\n';return output};$.fn.postcoder.xml_array=function(xml,selectmap,level,fields){var results={};results.data={};results.children=[];if(level==undefined)level=-1;if(fields==undefined)fields=[];level++;var count=0,tagname;xml.children().each(function(){tagname=this.tagName;if($(this).children().length==0){if(fields.length==0||$.fn.postcoder.in_array(tagname,fields)!==false){eval('results.data.'+tagname+' = $(this).text();')}}else if(selectmap!=undefined&&selectmap[level]){count=0;var selectmap_level=selectmap[level],next_fields=[];if($.fn.postcoder.is_array(selectmap_level)){next_fields=selectmap_level[1].split(',');selectmap_level=selectmap_level[0]}
$(selectmap_level,xml).each(function(){results.children[count]=$.fn.postcoder.xml_array($(this),selectmap,level,next_fields);count++})}});return results}
$.fn.postcoder.premise.defaults={func:'getPremiseList',error_mess:'An error has occured, please enter your address manually.',selectmap:[['addresses > item','dependent_street,street,double_dependent_locality,dependent_locality,post_town,county,postcode'],['premise > item','organisation,premise']],formmap:{'organisation':'organisation','premise':'premise','dependent_street':'dependent_street','street':'street','double_dependent_locality':'double_dependent_locality','dependent_locality':'dependent_locality','post_town':'post_town','county':'county','postcode':'postcode'},linefields:['organisation','premise_0,premise_1,premise_2,premise_3','dependent_street,street,double_dependent_locality,dependent_locality','post_town,county','postcode'],fieldorder:'organisation,premise_0,premise_1,premise_2,premise_3,dependent_street,street,double_dependent_locality,dependent_locality,post_town,county,postcode',format:true};$.fn.postcoder.street.defaults={func:'getThrfareAddresses',error_mess:'An error has occured, please enter your address manually.',selectmap:[['addresses > item','dependent_street,street,double_dependent_locality,dependent_locality,post_town,county,postcode']],formmap:{'dependent_street':'dependent_street','street':'street','double_dependent_locality':'double_dependent_locality','dependent_locality':'dependent_locality','post_town':'post_town','county':'county','postcode':'postcode'},linefields:['dependent_street,street,double_dependent_locality,dependent_locality','post_town,county','postcode']};$.fn.postcoder.match.defaults={func:'getMatchAddress',error_mess:'An error has occured, please enter your address manually.',selectmap:[['addresses > item','dependent_street,street,double_dependent_locality,dependent_locality,post_town,county,postcode'],['premise > item','organisation,premise']],formmap:{'organisation':'organisation','premise':'premise','dependent_street':'dependent_street','street':'street','double_dependent_locality':'double_dependent_locality','dependent_locality':'dependent_locality','post_town':'post_town','county':'county','postcode':'postcode'},linefields:['organisation','premise','dependent_street,street,double_dependent_locality,dependent_locality','post_town,county','postcode']};$.fn.postcoder.grid.defaults={func:'getGrids',error_mess:'An error has occured, please enter your address manually.',formmap:{'grideast':'grideast','gridnorth':'gridnorth','latitude_osgb36':'latitude_osgb36','longitude_osgb36':'longitude_osgb36','latitude_etrs89':'latitude_etrs89','longitude_etrs89':'longitude_etrs89'},linefields:['grideast,gridnorth,latitude_osgb36,longitude_osgb36,latitude_etrs89,longitude_etrs89']};$.fn.postcoder.postzon.defaults={func:'getPostzon',error_mess:'An error has occured, please enter your address manually.',formmap:{'grideasting':'grideasting','gridnorthing':'gridnorthing','grideastingstatus':'grideastingstatus','gridnorthingstatus':'gridnorthingstatus','localauthoritywardcode':'localauthoritywardcode','localauthoritywardname':'localauthoritywardname','localunitaryauthorityname':'localunitaryauthorityname','nhshealthauthoritycode':'nhshealthauthoritycode','nhshealthauthorityname':'nhshealthauthorityname','primarycaretrustcode':'primarycaretrustcode','primarycaretrustname':'primarycaretrustname','primarycaretrustha':'primarycaretrustha'},linefields:['grideasting,gridnorthing,grideastingstatus,gridnorthingstatus,localauthoritywardcode,localauthoritywardname,localunitaryauthorityname,nhshealthauthoritycode,nhshealthauthorityname,primarycaretrustcode,primarycaretrustname,primarycaretrustha']}})(jQuery);