Cwo.RegisterNamespace("Cwo.AutoComplete");Cwo.AutoComplete=function(i,x,b,j,l,r){var t=this,y="WaterMarkOn",p="DemandEntryWaterMarkOn",s=$("#"+x),m=$("#"+b),f=$("#"+j),o=b,n=l,h=r,A="/webservices/AutoComplete.asmx/Lookup",e=i,z=$("#"+e),c=$("#"+e+" li.Buying a"),q=$("#"+e+" li.Renting a"),v=$("#"+e+" li.FindAnAgent a"),g=$("#"+i+" span"),u;t.Visible=false;this.SetAutoFocus=function(C){u=C};function w(){if(s.val()===""){g.removeClass().addClass(y)}else{g.removeClass()}f.unbind("click");if(n==="ToLet"){f.bind("click",function(){t.ToLetSearch()});z.removeClass().addClass("Renting");$("#HomeSearchPredictiveText").focus()}else{if(n==="FindAnAgent"){f.bind("click",function(){t.AgentSearch()});z.removeClass().addClass("FindAnAgent");$("#HomeSearchPredictiveText").focus()}else{if(n==="Promotion"){f.bind("click",function(){t.PromotionSearch()})}else{f.bind("click",function(){t.ForSaleSearch()});z.removeClass().addClass("Buying");$("#HomeSearchPredictiveText").focus()}}}}function B(){var C=$("#"+o+" li.Selected");if(C.size()>0){C.next().addClass("Selected")}else{$("#"+o+" li:first").addClass("Selected")}C.removeClass("Selected")}function k(){var C=$("#"+o+" li.Selected");if(C.size()>0){C.prev().addClass("Selected")}else{$("#"+o+" li:last").addClass("Selected")}C.removeClass("Selected")}function a(){var C=$("#"+o+" li.Selected");if(C.size()>0){s.val(C.text());m.empty()}if(n==="ToLet"){t.ToLetSearch()}else{if(n==="Promotion"){t.PromotionSearch()}else{if(n==="FindAnAgent"){t.AgentSearch()}else{t.ForSaleSearch()}}}}function d(){var C=s.val();if(C.length===0){m.empty();if(t.Visible){t.Visible=false;Cwo.Page.EventController.FireEvent("SearchPredictiveBox.VisibilityChange.Invisible")}return}g.removeClass();C=t.RemoveIllegalCharacters(C);var D=t.ValidCharacterCheck(C,C.length);C=C.replace(/['']/g,"\\'");var F=Cwo.Page.AjajCalls.CreateRequest("LocationSuggestion");var E='{"FormValue":"'+C+'"}';new Cwo.AjaxCall(A,E,function(G){t.Updated(G,F)},null,null,true)}this.KeyPressed=function(C){var D=C.keyCode?C.keyCode:C.which?C.which:C.charCode;var E={UP:38,DOWN:40,DEL:46,TAB:9,RETURN:13,ESC:27,COMMA:188,PAGEUP:33,PAGEDOWN:34,BACKSPACE:8};switch(D){case E.UP:k();break;case E.DOWN:B();break;case E.TAB:B();break;case E.RETURN:a();break;case E.ESC:m.empty();break;default:d();break}return};this.Updated=function(D,J){if(Cwo.Page.AjajCalls.IsCurrentSequenceIndex("LocationSuggestion",J)){var F=D.d;var C=s.val().length;var G="";m.empty();for(var E in F){if(F.length!==0){var I=F[E];var K="<em>"+I.substr(0,C)+"</em>"+I.substr(C,I.length-C);var H=encodeURI(I);if(n==="ToLet"){G="/rent/search/"+H}else{if(n==="FindAnAgent"){G="/findanagent/"+H;if(h!==null&&h!==""){if(typeof h!=="undefined"){G+="/"+h}}}else{if(n==="Promotion"&&typeof h!=="undefined"){G="/buy/search/"+H+"/p "+h+"/"}else{G="/buy/search/"+H+"/"}}}G=G.toLowerCase();m.append('<li><a href="'+G+'">'+K+"</a></li>")}}if(!t.Visible){t.Visible=true;Cwo.Page.EventController.FireEvent("SearchPredictiveBox.VisibilityChange.Visible")}$("#"+o+" li:last").addClass("Last")}};this.RemoveIllegalCharacters=function(D){var C=new RegExp("\\\\","g");D=D.replace(C," ");var E=new RegExp("/","g");D=D.replace(E," ");D=D.replace(/[#%.*{}+@:<>""|?]/g," ");D=D.replace(/[&]/g," ");D=$.trim(D);return D};this.ValidCharacterCheck=function(C,E){var G="";var D=false;for(var F=0;F<E;F++){G=C.charAt(F);G=G.match("^[a-zA-Z0-9!()-,_;~]$")?D=true:D=false;if(D===true){return true}}return D};this.ChangeSearchType=function(C){n=C;w();d()};this.ForSaleSearch=function(){var C=s.val();C=C.replace(/^\s+|\s+$/g,"");var D=t.RemoveIllegalCharacters(C);if(C.length>0){Cwo.Page.AjajCalls.CreateRequest("LocationSuggestion");t.SetLocation("/buy/search/"+encodeURI(D))}else{t.DemandEntry()}};this.ToLetSearch=function(){var C=s.val();C=C.replace(/^\s+|\s+$/g,"");var D=t.RemoveIllegalCharacters(C);if(C.length>0){Cwo.Page.AjajCalls.CreateRequest("LocationSuggestion");t.SetLocation("/rent/search/"+encodeURI(D))}else{t.DemandEntry()}};this.AgentSearch=function(){var E=s.val();E=E.replace(/^\s+|\s+$/g,"");var C=t.RemoveIllegalCharacters(E);if(E.length>0){Cwo.Page.AjajCalls.CreateRequest("LocationSuggestion");var D="/findanagent/"+encodeURI(C);if(h!==null&&h!==""){if(typeof h!=="undefined"){D+="/"+h}}t.SetLocation(D)}else{t.DemandEntry()}};this.PromotionSearch=function(){var C=s.val();C=C.replace(/^\s+|\s+$/g,"");var D=t.RemoveIllegalCharacters(C);if(C.length>0){Cwo.Page.AjajCalls.CreateRequest("LocationSuggestion");t.SetLocation("/buy/search/"+encodeURI(D)+"/p "+h+"/")}else{t.DemandEntry()}};this.SetLocation=function(C){if(C.lastIndexOf("/")!==C.length-1){C+="/"}window.location=C};this.SetPlace=function(C){s.val(C);if(s.val()===""){g.removeClass().addClass(y)}};this.DemandEntry=function(){g.removeClass().addClass(p)};$(document).ready(function(){if(u){s.focus()}Cwo.AutoComplete.OnStart=function(){if($.browser.opera){s.bind("keypress",function(D){t.KeyPressed(D)})}else{s.bind("keyup",function(D){t.KeyPressed(D)})}s.focus(function(){if(s.val()===""){g.removeClass().addClass(y)}}).blur(function(){if(s.val()===""){g.removeClass().addClass(y)}});c.bind("click",function(){t.ChangeSearchType("ForSale")});q.bind("click",function(){t.ChangeSearchType("ToLet")});v.bind("click",function(){t.ChangeSearchType("FindAnAgent")});w()};if(navigator.userAgent.indexOf("MSIE")>-1){Cwo.AutoComplete.OnStart();return}try{Cwo.AutoComplete.OnStart()}catch(C){Cwo.Error.TrackError(C)}})};
