(function($){$.fn.location=function(list_class){var timer;var el=$(this);var ul=$(list_class);var old_loc_id=$('input[name="location_id"]').val();var country=$('#ddlCountry');var form=el.parents('form:first');var queue=[];ul.css('width',el.width());ul.parent().css("",$('label[for="'+el.attr('id')+'"]').width());$(this).keydown(function(e){clearTimeout(timer);if(e.keyCode!=40&&e.keyCode!=38&&e.keyCode!=13&&e.keyCode!=9){timer=setTimeout(function(){var val=el.val();if(val.length>0){$('#location span.area').addClass('hide');ul.empty();var li=$('<li><span>Loading...</span></li>');ul.append(li);for(j=0;j<=queue.length-1;j++){queue[j].abort();}
queue=[];var ajax=$.get('/'+cookie('lang')+'/a/loc',{q:val,iso:country.val()},function(data){ul.empty();if(data.length>0){var selected='selected';for(i=0;i<=data.length-1;i++){var d=data[i];var li=$('<li id="'+d.id+'" class="'+selected+'"><span class="place">'+d.name+'</span><br/><span class="area">'+d.area+'</span></li>');li.mouseover(function(){select(ul,$(this));});li.click(function(){replace($(this),el);});ul.append(li);selected='';}}else{var li=$('<li><span>No results found</span></li>');ul.append(li);}},'json');queue[queue.length]=ajax;}},300);}else{var id=0;var li=ul.children('li.selected');var new_li=li;if(e.keyCode==40&&!li.is(':last-child')){new_li=li.next();}else if(e.keyCode==38&&!li.is(':first-child')){new_li=li.prev();}
select(ul,new_li);if(e.keyCode==13){replace(li,el);}}});$(this).focus(function(){disable_form(form);});$(this).blur(function(){replace(ul.children('li.selected'),el);enable_form(form);});};$.fn.toggle_fieldset=function(di,cl){var el=$(this);var ed=di;toggle_fieldset(el);$(this).click(function(){toggle_fieldset(el);$(ed).each(function(){$(this).toggleClass(cl,!el.attr('checked'));});});}})(jQuery);$(document).ready(function(){$('#txtLocation').location('#location ul');$(":checkbox[name='fl']").toggle_fieldset($("div.within ul li"),"deactivated");});function disable_form(form){form.bind("keypress",function(e){if(e.keyCode==13){return false;}});}
function enable_form(form){form.unbind("keypress");}
function select(ul,li){ul.children('li.selected').removeClass('selected');li.addClass('selected');}
function replace(li,txtfield){var place=li.children('span.place').text();if(place.length>0){txtfield.val(place);$('#location .area').html(li.children('span.area').text());$('#location .area').removeClass('hide');$('input#location_id').val(li.attr('id'));li.parent().empty();}}
function toggle_fieldset(el){el.parents('fieldset:eq(0)').find(':input').each(function(index,elem){if($(elem).attr('name')!==el.attr('name')){$(elem).attr('disabled',!el.attr('checked'));}});}