var nlcCodes=null;var crsCodes=null;var maxLocationElement=20;function FireOnCheckValidityEvent(A,B){if(A._oTextbox.checkValidityEvent){A._oTextbox.checkValidityEvent.fire(A,B)}}function SetLocationText(A,B){A._oTextbox.value=B;FireOnCheckValidityEvent(A,true)}function SetErrorText(B,A,D){var C;if(B._oTextbox.errorControlId==undefined){C=YAHOO.util.Dom.get(B._oTextbox.id+"Error")}else{C=YAHOO.util.Dom.get(B._oTextbox.errorControlId)}if(D){C.innerHTML=A;ShowElement(C,true);YAHOO.util.Dom.addClass(B._oTextbox,"Error");FireOnCheckValidityEvent(B,false)}else{C.innerHTML="";ShowElement(C,false);YAHOO.util.Dom.removeClass(B._oTextbox,"Error");FireOnCheckValidityEvent(B,false)}}function BeforeExpandContainer(A,B,F,E){var D=YAHOO.util.Dom.getXY(A);D[1]+=YAHOO.util.Dom.get(A).offsetHeight;YAHOO.util.Dom.setXY(B,D);var C=YAHOO.util.Dom.getElementsByClassName("yui-ac-content",undefined,B);YAHOO.util.Dom.setStyle(C[0],"width",A.clientWidth+"px");return true}function ValidateLocation(A,D){if(A._oTextbox.value==""){SetErrorText(A,"",false);FireOnCheckValidityEvent(A,A._oTextbox.zeroLengthValid==true)}else{var F=GetStations(A._oTextbox.value,A.gnerServed);var B=false;if(F.length>0){for(var C=0;C<F.length;C++){if(F[C][0].toUpperCase()==A._oTextbox.value.toUpperCase()){SelectItem(A,F[C]);B=true;break}}if(!B){var E=F[0]&&F[0][0].toUpperCase()==A._oTextbox.value.toUpperCase();E=E||F[0][2]&&F[0][2].toUpperCase()==A._oTextbox.value.toUpperCase();if(D||F.length==1||E){SelectItem(A,F[0])}else{SetErrorText(A,A.strErrorText,true)}}}else{SetErrorText(A,A.strErrorText,true)}}}function ValidateLocationOnBlur(C,B){var A=B[0];ValidateLocation(A,true)}function SelectItem(A,B){if(B[0].substr(0,1)=="("){A._oTextbox.value=B[2]}else{A._oTextbox.value=B[0]}SetErrorText(A,"",false);FireOnCheckValidityEvent(A,true)}function OnItemSelectEvent(C,B){var A=B[0];var D=B[2];SelectItem(A,D)}function SearchForLocation(C,D,H,F){var I=D.toUpperCase();var E=H;var A=F;while(E<=A){var G=Math.floor((E+A)/2);var B=C[G][0].toUpperCase();if(B<I){E=G+1}else{if(B>I){A=G-1}else{return G}}}return E}function GetStations(H,J){var I=new Array();H=unescape(H);var K;if(H.length==3){K=crsCodes[H.toUpperCase()];if(K&&(!J||K[3]==1)){I.push(["("+K[2]+") "+K[0],K[1],K[0]])}}else{if(H.length==4){K=nlcCodes[H.toUpperCase()];if(K&&(!J||K[3]==1)){I.push(["("+K[1]+") "+K[0],K[1],K[0]])}}}var G=new Array();var C=(H+"~").toUpperCase();if(sortLoyaltyFirst||J){locStartIndex=sortLoyaltyFirst?locStartIndex:locations.length;var A=SearchForLocation(locations,H,0,locStartIndex);for(var L=A;I.length<maxLocationElement&&locations[L][0].toUpperCase()<C;L++){var E=locations[L][0].toUpperCase();var F=1;if(H.length>1){F=H.length}if(E.length<H.length){F=E.length-1}if(E.substring(0,F)!=H.toUpperCase()){break}I.push(locations[L]);G[locations[L][1]]=locations[L][0]}if(J){return I}}A=SearchForLocation(locations,H,locStartIndex,locations.length);for(var L=A;L<locations.length&&I.length<maxLocationElement&&locations[L][0].toUpperCase()<C;L++){selection=locations[L];var E=selection[0].toUpperCase();var F=1;if(H.length>1){F=H.length}if(E.length<H.length){F=E.length-1}if(E.substring(0,F)!=H.toUpperCase()){break}if(selection[4]!=undefined&&selection[4]=="I"){selection=nlcCodes[selection[1]]}if(selection[1]!=undefined&&G[selection[1]]==selection[0]){continue}I.push(selection);if(selection[1]!=undefined){G[selection[1]]=selection[0]}}if(sortLoyaltyFirst||J){var M,B,D=true;while(D){D=false;for(var L=0;L<I.length;L++){if(I[L][0].toUpperCase()==H.toUpperCase()){if(L>0){M=I[L-1];newItem=I[L];I[L-1]=newItem;I[L]=M;D=true}break}}}}delete G;return I}function GetAllStations(A){return GetStations(A,false)}function GetGnerStations(A){return GetStations(A,true)}function GetLocationData(){if(nlcCodes==null){nlcCodes=new Object();crsCodes=new Object();for(var A in locations){if(locations[A][1]&&locations[A][1].length>0&&locations[A][4]==undefined){nlcCodes[locations[A][1]]=locations[A]}if(locations[A][2]&&locations[A][2].length>0&&locations[A][4]==undefined){crsCodes[locations[A][2]]=locations[A]}}}}function InitialiseAutoComplete(D,G,F,A,E,C,H){maxLocationElement=F;GetLocationData();if(C){oACDS=new YAHOO.widget.DS_JSFunction(GetGnerStations)}else{oACDS=new YAHOO.widget.DS_JSFunction(GetAllStations)}oACDS.maxCacheEntries=100;var B=new YAHOO.widget.AutoComplete(D,D+"List",oACDS);B.minQueryLength=1;B.forceSelection=A;B.useShadow=true;B.maxResultsDisplayed=F;B.doBeforeExpandContainer=BeforeExpandContainer;B.useIFrame=(BrowserDetect.browser=="Explorer"&&BrowserDetect.version<7);B.strErrorText=G;B.queryDelay=0;B.gnerServed=C;B.animVert=false;B.convertCRS=E;B._oTextbox.errorControlId=H;B.itemSelectEvent.subscribe(OnItemSelectEvent);B._oTextbox.oAutoComp=B;ValidateLocation(B,false);B.textboxBlurEvent.subscribe(ValidateLocationOnBlur);return B}