var nlcCodes=null;var crsCodes=null;var maxLocationElement=20;function FireOnCheckValidityEvent(A,B){if(A.getInputEl().checkValidityEvent){A.getInputEl().checkValidityEvent.fire(A,B)}}function SetLocationText(A,B){A.getInputEl().value=B;FireOnCheckValidityEvent(A,true)}function SetErrorText(B,A,D){var C;if(B.getInputEl().errorControlId==undefined){C=YAHOO.util.Dom.get(B.getInputEl().id+"Error")}else{C=YAHOO.util.Dom.get(B.getInputEl().errorControlId)}if(D){C.innerHTML=A;ShowElement(C,true);YAHOO.util.Dom.addClass(B.getInputEl(),"Error");FireOnCheckValidityEvent(B,false)}else{C.innerHTML="";ShowElement(C,false);YAHOO.util.Dom.removeClass(B.getInputEl(),"Error")}}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(B,E){var A=trim(B.getInputEl().value);if(A==""){SetErrorText(B,"",false);FireOnCheckValidityEvent(B,B.getInputEl().zeroLengthValid==true)}else{var G=B.dataSource.liveData(A);var C=false;if(G.length>0){for(var D=0;D<G.length;D++){if(G[D][0].toUpperCase()==A.toUpperCase()){SelectItem(B,G[D]);C=true;break}}if(!C){var F=G[0]&&G[0][0].toUpperCase()==A.toUpperCase();F=F||G[0][2]&&G[0][2].toUpperCase()==A.toUpperCase();if(E||G.length==1||F){SelectItem(B,G[0])}else{SetErrorText(B,B.strErrorText,true)}}}else{SetErrorText(B,B.strErrorText,true)}}}function LTrim(B){var A=/\s*((\S+\s*)*)/;return B.replace(A,"$1")}function RTrim(B){var A=/((\s*\S+)*)\s*/;return B.replace(A,"$1")}function trim(A){return LTrim(RTrim(A))}function ValidateLocationOnBlur(C,B){var A=B[0];ValidateLocation(A,true)}function SelectItem(A,B){if(B[0].substr(0,1)=="("){A.getInputEl().value=B[2]}else{A.getInputEl().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 IsStationValid(C,B,A){return(B&&C==1)||(A&&C!=1)}function GetStations(G,I,E){var H=[];var B=[];G=trim(G);G=unescape(G);var J;if(G.length==3){J=crsCodes[G.toUpperCase()];if(J&&IsStationValid(J[3],I,E)){B.push(["("+J[2]+") "+J[0],J[1],J[0]])}}else{if(G.length==4){J=nlcCodes[G.toUpperCase()];if(J&&IsStationValid(J[3],I,E)){B.push(["("+J[1]+") "+J[0],J[1],J[0]])}}}var F={};var A=(G+"~").toUpperCase();iStartItem=SearchForLocation(locations,G,0,locations.length);for(var K=iStartItem;K<locations.length&&locations[K][0].toUpperCase()<A;K++){selection=locations[K];var C=selection[0].toUpperCase();var D=1;if(G.length>1){D=G.length}if(C.length<G.length){D=C.length-1}if(C.substring(0,D)!=G.toUpperCase()){break}if(selection[4]!=undefined&&selection[4]=="I"){selection=nlcCodes[selection[1]]}if(selection[1]!=undefined&&F[selection[1]]==selection[0]){continue}if(IsStationValid(selection[3],I,E)){H.push(selection);if(selection[1]!=undefined){F[selection[1]]=selection[0]}}}delete F;H.sort(function(M,L){return SortLocations(M,L,G.toUpperCase())});return B.concat(H)}function SortLocations(C,B,F){var E=C[0].toUpperCase();var D=B[0].toUpperCase();if(E==F){return -1}if(D==F){return 1}var A=SortLoyalty(C,B);if(A==0){if(E==D){A=0}A=(E<D)?-1:1}return A}function SortLoyalty(B,A){if(!sortLoyaltyFirst){return 0}if(B[3]=="1"){if(A[3]=="1"){return 0}else{return -1}}else{if(A[3]=="1"){return 1}else{return 0}}}function GetAllStations(A){return GetStations(A,true,true)}function GetGnerStations(A){return GetStations(A,true,false)}function GetNonGnerStations(A){return GetStations(A,false,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]}}}}var GnerStationsDataSource=new YAHOO.widget.DS_JSFunction(GetGnerStations);var AllStationsDataSource=new YAHOO.widget.DS_JSFunction(GetAllStations);var NonGnerStationsDataSource=new YAHOO.widget.DS_JSFunction(GetNonGnerStations);function InitialiseAutoComplete(D,G,F,A,E,C,H){maxLocationElement=F;GetLocationData();if(C){oACDS=GnerStationsDataSource}else{oACDS=AllStationsDataSource}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.getInputEl().errorControlId=H;B.itemSelectEvent.subscribe(OnItemSelectEvent);B.getInputEl().oAutoComp=B;ValidateLocation(B,false);B.textboxBlurEvent.subscribe(ValidateLocationOnBlur);return B}var locationControls=[];function InitialiseAutoCompleteOlympic(E,I,H,A,C,F,B){var G=InitialiseAutoComplete(E,I,H,A,C,false,B);SetDatasource(G,AllStationsDataSource);if(locationControls.length<2){locationControls.push(G);var D=document.getElementById(E);D.checkValidityEvent.subscribe(HandleOlympicDatasourceSwitch,G,false);setTimeout(function(){ValidateLocation(G,false)},100)}return G}function HandleOlympicDatasourceSwitch(C,I){var D=I[0];var A=(locationControls[0]==D)?locationControls[1]:locationControls[0];D.isValid=I[1];var H=false;if(I[1]){var G=GnerStationsDataSource.liveData(D.getInputEl().value);var B=(G.length>0&&G[0][0]==D.getInputEl().value);for(var F in locationControls){if(locationControls[F]!=D){var E=!B?GnerStationsDataSource:NonGnerStationsDataSource;if(locationControls[F].dataSource!=E){H=true;SetDatasource(locationControls[F],E)}}}}else{if(A.dataSource!=AllStationsDataSource){H=true;SetDatasource(A,AllStationsDataSource)}}if(!A.isValid&&H){ValidateLocation(A,false)}}function SetDatasource(B,A){B.dataSource=A}
