﻿var CantFindLocationMsg='Please check the postcode \\ location and try again. <br /><br />Alternatively browse the map by zooming and panning around.';var MasterSessionTimer;var KillSession;var stMapSize="L";var mbounds=[];var geo=new GClientGeocoder();var map;var MapBounds_OldNE="";var MapBounds_OldSW="";function debugBox(a){if(WhichEnv=='dev'){GLog.write(a)}}function debugBox2(a){if(WhichEnv=='dev'){GLog.write(a)}}function popUpBox(a){a+='<br /><a href="#" class="closePopUpBox">[x] Close</a>';$("#PopUpBoxMsg").empty();$("#PopUpBoxMsg").append(a);$(".closePopUpBox").bind("click",function(){popUpBoxClose()});$("#PopUpBoxMsg").show()}function popUpBoxClose(){$("#PopUpBoxMsg").hide();$("#PopUpBoxMsg").empty()}function loadOn(){resetSessionTimer();$("#MapWorkingMsg").show()}function loadOff(){$("#MapWorkingMsg").hide()}function StatusMsg(a){document.getElementById("PostCodestatus").innerHTML=a}function StartMapSub(){map=new GMap2(document.getElementById("map"));map.setCenter(new GLatLng(51.499553,-0.093384),12);map.addMapType(G_PHYSICAL_MAP);map.addControl(new GMenuMapTypeControl());map.addControl(new GLargeMapControl3D());map.addControl(new GScaleControl());map.enableContinuousZoom();map.enableDoubleClickZoom();map.enableScrollWheelZoom();map.addControl(new GOverviewMapControl());var a=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(80,80));a.apply(document.getElementById("PopUpBoxMsg"));map.getContainer().appendChild(document.getElementById("PopUpBoxMsg"))}function printMap(){window.print()}function ResizeMap(){var a=document.getElementById('map');switch(stMapSize){case'S':width='536px';height='600px';stMapSize='L';document.getElementById('cycleLeftCol').style.display="block";document.getElementById('cycleRightCol').style.width="536px";document.getElementById('cycleRightCol_TopSearchBarButton_ShowLHS').style.display="none";document.getElementById('cycleRightCol_TopSearchBarButton_ShowLHS').style.margin="0px";break;case'L':width='744px';height='600px';stMapSize='S';document.getElementById('cycleLeftCol').style.display="none";document.getElementById('cycleRightCol').style.width="744px";document.getElementById('cycleRightCol_TopSearchBarButton_ShowLHS').style.display="inline";break;default:width='536px';height='600px'}a.style.width=width;a.style.height=height;map.checkResize()}function genDynMarker(a,b,c,d,e){var f=new GIcon();f.image="http://chart.apis.google.com/chart?chst="+a+"&chld="+b+"|bb|"+c+"|"+d+"|"+e+"&ext=.png";f.iconAnchor=new GPoint(0,43);f.infoWindowAnchor=new GPoint(0,43);return f}function GetValue(e){a=GXml.value(e);a=a.replace(/^\s*/,"");a=a.replace(/\s*$/,"");return a}function setSessionTimer(){MasterSessionTimer=setTimeout("resetTheSession();",700000)}function resetSessionTimer(){clearTimeout(MasterSessionTimer);MasterSessionTimer=setTimeout("resetTheSession();",700000)}function resetTheSession(){switch(vMapMode){case"add":break;case"view":map.clearOverlays();document.getElementById('cycle_route_area').innerHTML="";KillSessionOnNextCall=true;GetLocationsLoader();break}KillSession=true;isVagueCount=0;MapBounds_OldNE="";MapBounds_OldSW=""}function AutoRunPostCodeSearch(){UserSearchField=document.getElementById('PostCode').value;if(UserSearchField=="Search postcode or address"){UserSearchField=""}if(UserSearchField!=""){goSearch()}}function goSearch(){switch(vMapMode){case"add":break;case"view":map.clearOverlays();document.getElementById('cycle_route_area').innerHTML="";break}KillSession=true;isVagueCount=0;UserSearchField=document.getElementById('PostCode').value;UserSearchFieldPCCheck=UserSearchField;if(UserSearchField==""){popUpBox('Please enter a valid postcode \\ location.')}else{UserSearchField=UserSearchField.replace(/, London, UK/,"");UserSearchField+=", London, UK";loadOn();if(checkPostCode(UserSearchFieldPCCheck)){var f=new google.search.LocalSearch();f.setSearchCompleteCallback(null,function(){if(f.results[0]){var a=f.results[0].lat;var b=f.results[0].lng;place(a,b,UserSearchFieldPCCheck)}else{popUpBox(CantFindLocationMsg)}});f.execute(UserSearchField)}else{geo.getLocations(UserSearchField,function(a){if(a.Status.code==G_GEO_SUCCESS){if(a.Placemark.length>1){var b="";var c={};b="<h3>Did you mean:</h3>";var d="0";for(var i=0;i<a.Placemark.length;i++){var p=a.Placemark[i].Point.coordinates;var e=a.Placemark[i].address;if(d.indexOf(e)<0){d+=e+"|#|";b+="<br /><a href=\"javascript:didPlace("+p[1]+","+p[0]+",\'"+a.Placemark[i].address+"\')\">"+a.Placemark[i].address+"</a>"}}popUpBox(b)}else{var p=a.Placemark[0].Point.coordinates;place(p[1],p[0],UserSearchField)}}else{popUpBox(CantFindLocationMsg)}})};loadOff()}}function checkPostCode(a){var b="[abcdefghijklmnoprstuwyz]";var c="[abcdefghklmnopqrstuvwxy]";var d="[abcdefghjkstuw]";var e="[abehmnprvwxy]";var f="[abdefghjlnpqrstuwxyz]";var g=new Array();g.push(new RegExp("^("+b+"{1}"+c+"?[0-9]{1,2})(\\s*)([0-9]{1}"+f+"{2})$","i"));g.push(new RegExp("^("+b+"{1}[0-9]{1}"+d+"{1})(\\s*)([0-9]{1}"+f+"{2})$","i"));g.push(new RegExp("^("+b+"{1}"+c+"?[0-9]{1}"+e+"{1})(\\s*)([0-9]{1}"+f+"{2})$","i"));g.push(/^(GIR)(\s*)(0AA)$/i);g.push(/^(bfpo)(\s*)([0-9]{1,4})$/i);g.push(/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);g.push(/^([A-Z]{4})(\s*)(1ZZ)$/i);var h=a;var j=false;for(var i=0;i<g.length;i++){if(g[i].test(h)){g[i].exec(h);h=RegExp.$1.toUpperCase()+" "+RegExp.$3.toUpperCase();h=h.replace(/C\/O\s*/,"c/o ");j=true;break}}if(j){return h}else return false}function didPlace(a,b,c){popUpBoxClose();document.getElementById("PostCode").value=c;place(a,b,c)}function place(a,b,c){$('#NavButton_Routes').attr('href',"?mode=view&PostCode="+c);$('#NavButton_Add').attr('href',"?mode=add&PostCode="+c);document.getElementById("UsersGeoCode").value=a+','+b;var d=new GLatLng(a,b);map.setCenter(d,17);c="Location: "+c;map.addOverlay(createMarkerWindow(d,c));bounds=new GLatLngBounds();bounds.extend(d);loadOff()}function createMarkerWindow(a,b){var c=new GMarker(a);GEvent.addListener(c,"click",function(){c.openInfoWindowHtml(b)});return c}