Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Annotation of /trunk/web/www/routino/router.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1035 - (hide annotations) (download) (as text)
Fri Aug 3 14:38:45 2012 UTC (12 years, 7 months ago) by amb
File MIME type: application/javascript
File size: 46206 byte(s)
Add comments to the functions that are called from the HTML file (to simplify
debugging and make easier to maintain).

1 amb 569 //
2     // Routino router web page Javascript
3     //
4     // Part of the Routino routing software.
5     //
6 amb 984 // This file Copyright 2008-2012 Andrew M. Bishop
7 amb 569 //
8     // This program is free software: you can redistribute it and/or modify
9     // it under the terms of the GNU Affero General Public License as published by
10     // the Free Software Foundation, either version 3 of the License, or
11     // (at your option) any later version.
12     //
13     // This program is distributed in the hope that it will be useful,
14     // but WITHOUT ANY WARRANTY; without even the implied warranty of
15     // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     // GNU Affero General Public License for more details.
17     //
18     // You should have received a copy of the GNU Affero General Public License
19     // along with this program. If not, see <http://www.gnu.org/licenses/>.
20     //
21    
22 amb 574
23 amb 987 var vismarkers, markers, markersmoved, paramschanged;
24 amb 985 var homelat=null, homelon=null;
25    
26    
27     ////////////////////////////////////////////////////////////////////////////////
28     /////////////////////////////// Initialisation /////////////////////////////////
29     ////////////////////////////////////////////////////////////////////////////////
30    
31 amb 577 // Make a deep copy of the routino profile.
32 amb 569
33 amb 577 var routino_default={};
34     for(var l1 in routino)
35     if(typeof(routino[l1])!='object')
36     routino_default[l1]=routino[l1];
37     else
38     {
39     routino_default[l1]={};
40     for(var l2 in routino[l1])
41     if(typeof(routino[l1][l2])!='object')
42     routino_default[l1][l2]=Number(routino[l1][l2]);
43     else
44     {
45     routino_default[l1][l2]={};
46     for(var l3 in routino[l1][l2])
47     routino_default[l1][l2][l3]=Number(routino[l1][l2][l3]);
48     }
49     }
50    
51 amb 985 // Store the latitude and longitude in the routino variable
52 amb 577
53 amb 985 routino.point=[];
54 amb 1012 for(var marker=1;marker<=mapprops.maxmarkers;marker++)
55 amb 985 {
56     routino.point[marker]={};
57    
58     routino.point[marker].lon="";
59     routino.point[marker].lat="";
60 amb 1001 routino.point[marker].search="";
61 amb 985 routino.point[marker].active=false;
62 amb 1034 routino.point[marker].used=false;
63 amb 985 }
64    
65     // Process the URL query string and extract the arguments
66    
67     var legal={"^lon" : "^[-0-9.]+$",
68     "^lat" : "^[-0-9.]+$",
69     "^zoom" : "^[0-9]+$",
70    
71     "^lon[1-9]" : "^[-0-9.]+$",
72     "^lat[1-9]" : "^[-0-9.]+$",
73 amb 1001 "^search[1-9]" : "^.+$",
74 amb 985 "^transport" : "^[a-z]+$",
75     "^highway-[a-z]+" : "^[0-9.]+$",
76     "^speed-[a-z]+" : "^[0-9.]+$",
77     "^property-[a-z]+" : "^[0-9.]+$",
78     "^oneway" : "^(1|0|true|false|on|off)$",
79     "^turns" : "^(1|0|true|false|on|off)$",
80     "^weight" : "^[0-9.]+$",
81     "^height" : "^[0-9.]+$",
82     "^width" : "^[0-9.]+$",
83     "^length" : "^[0-9.]+$",
84    
85     "^language" : "^[-a-zA-Z]+$"};
86    
87     var args={};
88    
89     if(location.search.length>1)
90     {
91     var query,queries;
92    
93     query=location.search.replace(/^\?/,"");
94     query=query.replace(/;/g,'&');
95     queries=query.split('&');
96    
97     for(var i=0;i<queries.length;i++)
98     {
99     queries[i].match(/^([^=]+)(=(.*))?$/);
100    
101     k=RegExp.$1;
102     v=unescape(RegExp.$3);
103    
104     for(var l in legal)
105     {
106     if(k.match(RegExp(l)) && v.match(RegExp(legal[l])))
107     args[k]=v;
108     }
109     }
110     }
111    
112    
113 amb 987 //
114     // Fill in the HTML - add the missing waypoints
115     //
116    
117 amb 1035 function html_init() // called from router.html
118 amb 987 {
119     var waypoints=document.getElementById("waypoints");
120 amb 1005
121 amb 987 var waypoint_html=waypoints.rows[0].innerHTML;
122 amb 1005 waypoints.deleteRow(0);
123 amb 987
124 amb 1005 var searchresults_html=waypoints.rows[0].innerHTML;
125 amb 987 waypoints.deleteRow(0);
126    
127 amb 1012 for(var marker=mapprops.maxmarkers;marker>=1;marker--)
128 amb 987 {
129 amb 1005 var searchresults=waypoints.insertRow(0);
130 amb 1011
131 amb 1005 searchresults.id="searchresults" + marker;
132 amb 1011 searchresults.innerHTML=searchresults_html.split('XXX').join(marker);
133 amb 987
134 amb 1011 var waypoint=waypoints.insertRow(0);
135 amb 1005
136     waypoint.id="waypoint" + marker;
137 amb 1011 waypoint.innerHTML=waypoint_html.split('XXX').join(marker);
138 amb 987 }
139    
140 amb 1012 vismarkers=mapprops.maxmarkers;
141 amb 987 }
142    
143    
144 amb 577 ////////////////////////////////////////////////////////////////////////////////
145     //////////////////////////////// Form handling /////////////////////////////////
146     ////////////////////////////////////////////////////////////////////////////////
147    
148 amb 569 //
149     // Form initialisation - fill in the uninitialised parts
150     //
151    
152 amb 1035 function form_init() // called from router.html
153 amb 569 {
154 amb 985 // Fill in the waypoints
155 amb 569
156 amb 985 var filled=0;
157 amb 569
158 amb 1012 for(var marker=mapprops.maxmarkers;marker>=1;marker--)
159 amb 569 {
160 amb 985 var lon=args["lon" + marker];
161     var lat=args["lat" + marker];
162 amb 1001 var search=args["search" + marker];
163 amb 577
164 amb 1001 if(lon != undefined && lat != undefined && search != undefined && lon != "" && lat != "" && search != "")
165 amb 569 {
166 amb 1001 formSetSearch(marker,search);
167 amb 1032 formSetCoords(marker,lon,lat);
168 amb 569
169 amb 1032 markerAddMap(marker);
170    
171 amb 1001 markerSearch(marker);
172    
173 amb 985 filled++;
174 amb 569 }
175 amb 1001 else if(lon != undefined && lat != undefined && lon != "" && lat != "")
176     {
177 amb 1032 formSetCoords(marker,lon,lat);
178 amb 1001
179 amb 1032 markerAddMap(marker);
180    
181 amb 1001 markerCoords(marker);
182    
183     filled++;
184     }
185     else if(search != undefined && search != "")
186     {
187     formSetSearch(marker,search);
188    
189     markerSearch(marker);
190    
191     DoSearch(marker);
192    
193     filled++;
194     }
195 amb 985 else if(filled==0)
196     markerRemove(marker);
197 amb 1016
198     var searchfield=document.forms["form"].elements["search" + marker];
199    
200     if(searchfield.addEventListener)
201     searchfield.addEventListener('keyup', searchOnReturnKey, false);
202     else if(searchfield.attachEvent)
203     searchfield.attachEvent('keyup', searchOnReturnKey); // Internet Explorer
204 amb 985 }
205 amb 569
206 amb 985 // Update the transport type with the URL settings which updates all HTML forms to defaults.
207 amb 574
208 amb 985 var transport=routino.transport;
209 amb 569
210 amb 985 if(args["transport"] != undefined)
211     transport=args["transport"];
212 amb 577
213 amb 985 formSetTransport(transport);
214 amb 577
215 amb 985 // Update the HTML with the URL settings
216    
217     if(args["language"] != undefined)
218     formSetLanguage(args["language"]);
219    
220     for(var key in routino.profile_highway)
221     if(args["highway-" + key] != undefined)
222     formSetHighway(key,args["highway-" + key]);
223    
224     for(var key in routino.profile_speed)
225     if(args["speed-" + key] != undefined)
226     formSetSpeed(key,args["speed-" + key]);
227    
228     for(var key in routino.profile_property)
229     if(args["property-" + key] != undefined)
230     formSetProperty(key,args["property-" + key]);
231    
232     for(var key in routino.restrictions)
233 amb 574 {
234 amb 985 if(key=="oneway" || key=="turns")
235 amb 577 {
236 amb 985 if(args[key] != undefined)
237     formSetRestriction(key,args[key]);
238 amb 577 }
239 amb 985 else
240     {
241     if(args["restrict-" + key] != undefined)
242     formSetRestriction(key,args["restrict-" + key]);
243     }
244 amb 574 }
245 amb 569
246 amb 577 // Get the home location cookie and compare to each waypoint
247    
248     var cookies=document.cookie.split('; ');
249    
250     for(var cookie=0;cookie<cookies.length;cookie++)
251     if(cookies[cookie].substr(0,"Routino-home".length)=="Routino-home")
252     {
253     var data=cookies[cookie].split(/[=:;]/);
254    
255     if(data[1]=="lon") homelon=Number(data[2]);
256     if(data[3]=="lat") homelat=Number(data[4]);
257     }
258    
259     if(homelon!=null && homelat!=null)
260     {
261 amb 1012 for(var marker=mapprops.maxmarkers;marker>=1;marker--)
262 amb 577 {
263 amb 985 var lon=routino.point[marker].lon;
264     var lat=routino.point[marker].lat;
265 amb 577
266     if(lon==homelon && lat==homelat)
267     updateIcon(marker);
268     }
269    
270     // If the first location is empty and the cookie is set then fill it.
271    
272 amb 1034 if(!routino.point[1].used)
273 amb 1032 {
274     formSetCoords(1,homelon,homelat);
275     markerAddMap(1);
276     }
277 amb 577 }
278 amb 569 }
279    
280    
281     //
282 amb 1016 // Function to perform the search if the return key is pressed.
283     // (using 'onchange' only triggers once and is confusing when clicking outside the field).
284     //
285    
286     function searchOnReturnKey(ev)
287     {
288     if(ev.keyCode==13)
289     if(this.name.match(/^search([0-9]+)$/))
290     formSetSearch(RegExp.$1);
291    
292     return(true);
293     }
294    
295    
296     //
297 amb 577 // Change of language in the form
298     //
299    
300 amb 1035 function formSetLanguage(value) // called from router.html (with no arguments)
301 amb 577 {
302 amb 985 if(value == undefined)
303     {
304     for(var lang=0;lang<document.forms["form"].elements["language"].length;lang++)
305     if(document.forms["form"].elements["language"][lang].checked)
306     routino.language=document.forms["form"].elements["language"][lang].value;
307     }
308     else
309     {
310     for(var lang=0;lang<document.forms["form"].elements["language"].length;lang++)
311     if(document.forms["form"].elements["language"][lang].value==value)
312     document.forms["form"].elements["language"][lang].checked=true;
313     else
314     document.forms["form"].elements["language"][lang].checked=false;
315 amb 577
316 amb 985 routino.language=value;
317     }
318 amb 577 }
319    
320    
321     //
322 amb 569 // Change of transport in the form
323     //
324    
325 amb 1035 function formSetTransport(value) // called from router.html
326 amb 569 {
327 amb 985 routino.transport=value;
328 amb 569
329 amb 577 for(var key in routino.transports)
330     document.forms["form"].elements["transport"][routino.transports[key]-1].checked=(key==routino.transport);
331 amb 569
332 amb 577 for(var key in routino.profile_highway)
333     document.forms["form"].elements["highway-" + key].value=routino.profile_highway[key][routino.transport];
334 amb 569
335 amb 577 for(var key in routino.profile_speed)
336     document.forms["form"].elements["speed-" + key].value=routino.profile_speed[key][routino.transport];
337 amb 569
338 amb 577 for(var key in routino.profile_property)
339     document.forms["form"].elements["property-" + key].value=routino.profile_property[key][routino.transport];
340 amb 569
341 amb 577 for(var key in routino.restrictions)
342 amb 569 {
343 amb 622 if(key=="oneway" || key=="turns")
344 amb 577 document.forms["form"].elements["restrict-" + key].checked=routino.profile_restrictions[key][routino.transport];
345 amb 569 else
346 amb 577 document.forms["form"].elements["restrict-" + key].value=routino.profile_restrictions[key][routino.transport];
347 amb 569 }
348    
349     paramschanged=true;
350     }
351    
352    
353     //
354     // Change of highway in the form
355     //
356    
357 amb 1035 function formSetHighway(type,value) // called from router.html (with one argument)
358 amb 569 {
359 amb 985 if(value == undefined)
360     routino.profile_highway[type][routino.transport]=document.forms["form"].elements["highway-" + type].value;
361     else
362     {
363     document.forms["form"].elements["highway-" + type].value=value;
364     routino.profile_highway[type][routino.transport]=value;
365     }
366 amb 569
367     paramschanged=true;
368     }
369    
370    
371     //
372     // Change of Speed in the form
373     //
374    
375 amb 1035 function formSetSpeed(type,value) // called from router.html (with one argument)
376 amb 569 {
377 amb 985 if(value == undefined)
378     routino.profile_speed[type][routino.transport]=document.forms["form"].elements["speed-" + type].value;
379     else
380     {
381     document.forms["form"].elements["speed-" + type].value=value;
382     routino.profile_speed[type][routino.transport]=value;
383     }
384 amb 569
385     paramschanged=true;
386     }
387    
388    
389     //
390 amb 574 // Change of Property in the form
391     //
392    
393 amb 1035 function formSetProperty(type,value) // called from router.html (with one argument)
394 amb 574 {
395 amb 985 if(value == undefined)
396     routino.profile_property[type][routino.transport]=document.forms["form"].elements["property-" + type].value;
397     else
398     {
399     document.forms["form"].elements["property-" + type].value=value;
400     routino.profile_property[type][routino.transport]=value;
401     }
402 amb 574
403     paramschanged=true;
404     }
405    
406    
407     //
408 amb 622 // Change of Restriction rule in the form
409 amb 569 //
410    
411 amb 1035 function formSetRestriction(type,value) // called from router.html (with one argument)
412 amb 569 {
413 amb 985 if(value == undefined)
414     {
415     if(type=="oneway" || type=="turns")
416     routino.profile_restrictions[type][routino.transport]=document.forms["form"].elements["restrict-" + type].checked;
417     else
418     routino.profile_restrictions[type][routino.transport]=document.forms["form"].elements["restrict-" + type].value;
419     }
420 amb 569 else
421 amb 985 {
422     if(type=="oneway" || type=="turns")
423     document.forms["form"].elements["restrict-" + type].checked=value;
424     else
425     document.forms["form"].elements["restrict-" + type].value=value;
426 amb 569
427 amb 985 routino.profile_restrictions[type][routino.transport]=value;
428     }
429    
430 amb 569 paramschanged=true;
431     }
432    
433    
434     //
435 amb 577 // Set the feature coordinates from the form when the form changes.
436     //
437    
438 amb 1035 function formSetCoords(marker,lon,lat) // called from router.html (with one argument)
439 amb 577 {
440 amb 1005 clearSearchResult(marker);
441    
442 amb 1032 if(lon == undefined && lat == undefined)
443 amb 985 {
444 amb 1034 lon=document.forms["form"].elements["lon" + marker].value;
445     lat=document.forms["form"].elements["lat" + marker].value;
446 amb 985 }
447 amb 1034
448     if(lon == "" && lat == "")
449 amb 985 {
450 amb 1034 document.forms["form"].elements["lon" + marker].value="";
451     document.forms["form"].elements["lat" + marker].value="";
452 amb 985
453 amb 1034 routino.point[marker].lon="";
454     routino.point[marker].lat=""
455 amb 985 }
456 amb 1034 else
457     {
458     if(lon=="")
459     {
460     var lonlat=map.getCenter().clone();
461     lonlat.transform(epsg900913,epsg4326);
462 amb 985
463 amb 1034 lon=lonlat.lon;
464     }
465 amb 577
466 amb 1034 if(lon<-180) lon=-180;
467     if(lon>+180) lon=+180;
468 amb 577
469 amb 1034 if(lat=="")
470     {
471     var lonlat=map.getCenter().clone();
472     lonlat.transform(epsg900913,epsg4326);
473 amb 577
474 amb 1034 lat=lonlat.lat;
475     }
476 amb 577
477 amb 1034 if(lat<-90 ) lat=-90 ;
478     if(lat>+90 ) lat=+90 ;
479 amb 577
480 amb 1034 var lonlat = new OpenLayers.LonLat(lon,lat);
481     lonlat.transform(epsg4326,epsg900913);
482    
483     markers[marker].move(lonlat);
484    
485     markersmoved=true;
486    
487     document.forms["form"].elements["lon" + marker].value=format5f(lon);
488     document.forms["form"].elements["lat" + marker].value=format5f(lat);
489    
490     routino.point[marker].lon=lon;
491     routino.point[marker].lat=lat;
492     routino.point[marker].used=true;
493     }
494 amb 577 }
495    
496    
497     //
498 amb 1032 // Set the search field from the form when the form changes.
499 amb 1001 //
500    
501 amb 1035 function formSetSearch(marker,search) // called from event handler linked to router.html (with one argument)
502 amb 1001 {
503 amb 1005 clearSearchResult(marker);
504    
505 amb 1001 if(search == undefined)
506     {
507     routino.point[marker].search=document.forms["form"].elements["search" + marker].value;
508    
509     DoSearch(marker);
510     }
511     else
512     {
513     document.forms["form"].elements["search" + marker].value=search;
514    
515     routino.point[marker].search=search;
516     }
517     }
518    
519    
520     //
521 amb 577 // Format a number in printf("%.5f") format.
522     //
523    
524     function format5f(number)
525     {
526     var newnumber=Math.floor(number*100000+0.5);
527     var delta=0;
528    
529     if(newnumber>=0 && newnumber<100000) delta= 100000;
530     if(newnumber<0 && newnumber>-100000) delta=-100000;
531    
532     var string=String(newnumber+delta);
533    
534     var intpart =string.substring(0,string.length-5);
535     var fracpart=string.substring(string.length-5,string.length);
536    
537     if(delta>0) intpart="0";
538     if(delta<0) intpart="-0";
539    
540     return(intpart + "." + fracpart);
541     }
542    
543    
544     //
545     // Build a set of URL arguments
546     //
547    
548 amb 986 function buildURLArguments(lang)
549 amb 577 {
550 amb 986 var url= "transport=" + routino.transport;
551 amb 577
552     for(var marker=1;marker<=vismarkers;marker++)
553 amb 986 if(routino.point[marker].active)
554 amb 577 {
555 amb 985 url=url + ";lon" + marker + "=" + routino.point[marker].lon;
556     url=url + ";lat" + marker + "=" + routino.point[marker].lat;
557 amb 1001 if(routino.point[marker].search != "")
558     url=url + ";search" + marker + "=" + encodeURIComponent(routino.point[marker].search);
559 amb 577 }
560    
561     for(var key in routino.profile_highway)
562     if(routino.profile_highway[key][routino.transport]!=routino_default.profile_highway[key][routino.transport])
563     url=url + ";highway-" + key + "=" + routino.profile_highway[key][routino.transport];
564    
565     for(var key in routino.profile_speed)
566     if(routino.profile_speed[key][routino.transport]!=routino_default.profile_speed[key][routino.transport])
567     url=url + ";speed-" + key + "=" + routino.profile_speed[key][routino.transport];
568    
569     for(var key in routino.profile_property)
570     if(routino.profile_property[key][routino.transport]!=routino_default.profile_property[key][routino.transport])
571     url=url + ";property-" + key + "=" + routino.profile_property[key][routino.transport];
572    
573     for(var key in routino.restrictions)
574     if(routino.profile_restrictions[key][routino.transport]!=routino_default.profile_restrictions[key][routino.transport])
575     url=url + ";" + key + "=" + routino.profile_restrictions[key][routino.transport];
576    
577 amb 986 if(lang && routino.language)
578 amb 577 url=url + ";language=" + routino.language;
579    
580     return(url);
581     }
582    
583    
584     //
585 amb 986 // Build a set of URL arguments for the map location
586 amb 569 //
587    
588 amb 986 function buildMapArguments()
589 amb 569 {
590 amb 1009 var lonlat = map.getCenter().clone();
591     lonlat.transform(epsg900913,epsg4326);
592 amb 569
593 amb 986 var zoom = map.getZoom() + map.minZoomLevel;
594    
595     return "lat=" + format5f(lonlat.lat) + ";lon=" + format5f(lonlat.lon) + ";zoom=" + zoom;
596 amb 569 }
597    
598    
599     //
600 amb 986 // Update a URL
601     //
602    
603 amb 1035 function updateURL(element) // called from router.html
604 amb 986 {
605     if(element.id == "permalink_url")
606     element.href=location.pathname + "?" + buildURLArguments(true) + ";" + buildMapArguments();
607    
608     if(element.id == "visualiser_url")
609     element.href="visualiser.html" + "?" + buildMapArguments();
610    
611     if(element.id == "edit_url")
612     element.href="http://www.openstreetmap.org/edit" + "?" + buildMapArguments();
613    
614     if(element.id.match(/^lang_([a-zA-Z-]+)_url$/))
615     element.href="router.html" + "." + RegExp.$1 + "?" + buildURLArguments(false) + ";" + buildMapArguments();
616     }
617    
618    
619 amb 577 ////////////////////////////////////////////////////////////////////////////////
620     ///////////////////////////////// Map handling /////////////////////////////////
621     ////////////////////////////////////////////////////////////////////////////////
622 amb 569
623     var map;
624 amb 933 var layerMap=[], layerVectors, layerGPX;
625 amb 569 var epsg4326, epsg900913;
626    
627 amb 985 //
628 amb 569 // Initialise the 'map' object
629     //
630    
631 amb 1035 function map_init() // called from router.html
632 amb 569 {
633 amb 985 lon =args["lon"];
634     lat =args["lat"];
635     zoom=args["zoom"];
636    
637 amb 935 // Map properties (North/South and East/West limits and zoom in/out limits) are now in mapprops.js
638 amb 933 // Map URLs are now in mapprops.js
639 amb 577
640 amb 569 //
641     // Create the map
642     //
643    
644     epsg4326=new OpenLayers.Projection("EPSG:4326");
645     epsg900913=new OpenLayers.Projection("EPSG:900913");
646    
647     map = new OpenLayers.Map ("map",
648     {
649     controls:[
650     new OpenLayers.Control.Navigation(),
651     new OpenLayers.Control.PanZoomBar(),
652     new OpenLayers.Control.ScaleLine(),
653     new OpenLayers.Control.LayerSwitcher()
654     ],
655    
656     projection: epsg900913,
657     displayProjection: epsg4326,
658    
659 amb 933 minZoomLevel: mapprops.zoomout,
660     numZoomLevels: mapprops.zoomin-mapprops.zoomout+1,
661 amb 1018 maxResolution: 156543.03390625 / Math.pow(2,mapprops.zoomout),
662 amb 569
663 amb 1013 // These two lines are not needed with OpenLayers 2.12
664     units: "m",
665 amb 577 maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34),
666 amb 569
667 amb 1013 restrictedExtent: new OpenLayers.Bounds(mapprops.westedge,mapprops.southedge,mapprops.eastedge,mapprops.northedge).transform(epsg4326,epsg900913)
668 amb 569 });
669    
670 amb 933 // Add map tile layers
671 amb 569
672 amb 933 for(var l=0;l < mapprops.mapdata.length;l++)
673     {
674     layerMap[l] = new OpenLayers.Layer.TMS(mapprops.mapdata[l].label,
675     mapprops.mapdata[l].baseurl,
676     {
677     emptyUrl: mapprops.mapdata[l].errorurl,
678     type: 'png',
679     getURL: limitedUrl,
680     displayOutsideMaxExtent: true,
681     buffer: 1
682     });
683     map.addLayer(layerMap[l]);
684     }
685 amb 569
686 amb 577 // Get a URL for the tile; limited to map restricted extent.
687 amb 569
688     function limitedUrl(bounds)
689     {
690     var z = map.getZoom() + map.minZoomLevel;
691    
692 amb 577 if (z>=7 && (bounds.right < map.restrictedExtent.left ||
693     bounds.left > map.restrictedExtent.right ||
694     bounds.top < map.restrictedExtent.bottom ||
695     bounds.bottom > map.restrictedExtent.top))
696 amb 569 return this.emptyUrl;
697    
698     var res = map.getResolution();
699     var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
700     var limit = Math.pow(2, z);
701    
702     if (y < 0 || y >= limit)
703     return this.emptyUrl;
704    
705     var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
706    
707     x = ((x % limit) + limit) % limit;
708     return this.url + z + "/" + x + "/" + y + "." + this.type;
709     }
710    
711     // Define a GPX layer but don't add it yet
712    
713     layerGPX={shortest: null, quickest: null};
714    
715     gpx_style={shortest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#00FF00"}),
716     quickest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#0000FF"})};
717 amb 985
718 amb 569 // Add a vectors layer
719 amb 985
720 amb 569 layerVectors = new OpenLayers.Layer.Vector("Markers");
721     map.addLayer(layerVectors);
722    
723     // A set of markers
724    
725 amb 574 markers={};
726 amb 569 markersmoved=false;
727     paramschanged=false;
728    
729 amb 1012 for(var marker=1;marker<=mapprops.maxmarkers;marker++)
730 amb 569 {
731 amb 987 markers[marker] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{},
732     new OpenLayers.Style({},{externalGraphic: 'icons/marker-' + marker + '-red.png',
733     fillColor: "white",
734     graphicYOffset: -25,
735     graphicWidth: 21,
736     graphicHeight: 25,
737     display: "none"}));
738 amb 569
739 amb 987 layerVectors.addFeatures([markers[marker]]);
740 amb 569 }
741    
742     // A function to drag the markers
743    
744     var drag = new OpenLayers.Control.DragFeature(layerVectors,
745     {onDrag: dragMove,
746     onComplete: dragComplete });
747     map.addControl(drag);
748     drag.activate();
749    
750     // Markers to highlight a selected point
751    
752 amb 577 for(var highlight in highlights)
753 amb 569 {
754     highlights[highlight] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{},
755 amb 577 new OpenLayers.Style({},{strokeColor: route_dark_colours[highlight],
756 amb 574 fillColor: "white",
757 amb 569 pointRadius: 10,
758     strokeWidth: 4,
759     fillOpacity: 0,
760     display: "none"}));
761    
762     layerVectors.addFeatures([highlights[highlight]]);
763     }
764    
765 amb 574 // A popup for routing results
766    
767 amb 577 for(var popup in popups)
768     popups[popup] = createPopup(popup);
769 amb 574
770 amb 572 // Set the map centre to the limited range specified
771    
772 amb 577 map.setCenter(map.restrictedExtent.getCenterLonLat(), map.getZoomForExtent(map.restrictedExtent,true));
773 amb 572 map.maxResolution = map.getResolution();
774    
775 amb 569 // Move the map
776    
777 amb 985 if(lon != undefined && lat != undefined && zoom != undefined)
778 amb 569 {
779 amb 933 if(lon<mapprops.westedge) lon=mapprops.westedge;
780     if(lon>mapprops.eastedge) lon=mapprops.eastedge;
781    
782     if(lat<mapprops.southedge) lat=mapprops.southedge;
783     if(lat>mapprops.northedge) lat=mapprops.northedge;
784    
785     if(zoom<mapprops.zoomout) zoom=mapprops.zoomout;
786     if(zoom>mapprops.zoomin) zoom=mapprops.zoomin;
787    
788 amb 1009 var lonlat = new OpenLayers.LonLat(lon,lat);
789     lonlat.transform(epsg4326,epsg900913);
790 amb 569
791     map.moveTo(lonlat,zoom-map.minZoomLevel);
792     }
793     }
794    
795    
796     //
797     // OpenLayers.Control.DragFeature callback for a drag occuring.
798     //
799    
800     function dragMove(feature,pixel)
801     {
802 amb 574 for(var marker in markers)
803 amb 569 if(feature==markers[marker])
804     {
805     markersmoved=true;
806    
807 amb 985 dragSetForm(marker);
808 amb 569 }
809     }
810    
811    
812     //
813     // OpenLayers.Control.DragFeature callback for completing a drag.
814     //
815    
816     function dragComplete(feature,pixel)
817     {
818 amb 574 for(var marker in markers)
819 amb 569 if(feature==markers[marker])
820     {
821     markersmoved=true;
822    
823 amb 985 dragSetForm(marker);
824 amb 569 }
825     }
826    
827    
828 amb 985 //
829     // Set the feature coordinates in the form after dragging.
830     //
831    
832     function dragSetForm(marker)
833     {
834     var lonlat = new OpenLayers.LonLat(markers[marker].geometry.x, markers[marker].geometry.y);
835 amb 1009 lonlat.transform(epsg900913,epsg4326);
836 amb 985
837     var lon=format5f(lonlat.lon);
838     var lat=format5f(lonlat.lat);
839    
840     formSetCoords(marker,lon,lat);
841     }
842    
843    
844 amb 577 ////////////////////////////////////////////////////////////////////////////////
845     /////////////////////////////// Marker handling ////////////////////////////////
846     ////////////////////////////////////////////////////////////////////////////////
847    
848    
849 amb 569 //
850 amb 574 // Toggle a marker on the map.
851 amb 569 //
852    
853 amb 1035 function markerToggleMap(marker) // called from router.html
854 amb 569 {
855 amb 1034 if(!routino.point[marker].used)
856     {
857     routino.point[marker].used=true;
858     markerCentre(marker);
859     markerCoords(marker);
860     }
861 amb 1005
862 amb 1032 markerAddRemoveMap(marker,!routino.point[marker].active);
863     }
864    
865    
866     //
867     // Show or hide a marker on the map.
868     //
869    
870     function markerAddRemoveMap(marker,active)
871     {
872     if(active)
873     markerAddMap(marker);
874     else
875 amb 574 markerRemoveMap(marker);
876     }
877    
878    
879     //
880     // Show a marker on the map.
881     //
882    
883     function markerAddMap(marker)
884     {
885 amb 1005 clearSearchResult(marker);
886    
887 amb 577 markers[marker].style.display = "";
888 amb 985 routino.point[marker].active=true;
889 amb 1034 routino.point[marker].used=true;
890 amb 574
891 amb 577 updateIcon(marker);
892 amb 574
893     markersmoved=true;
894     }
895    
896    
897     //
898     // Remove a marker from the map.
899     //
900    
901     function markerRemoveMap(marker)
902     {
903 amb 1005 clearSearchResult(marker);
904    
905 amb 577 markers[marker].style.display = "none";
906 amb 985 routino.point[marker].active=false;
907 amb 574
908 amb 577 updateIcon(marker);
909 amb 574
910 amb 577 markersmoved=true;
911     }
912 amb 574
913 amb 577
914     //
915 amb 1001 // Display search string for the marker
916     //
917    
918 amb 1035 function markerSearch(marker) // called from router.html
919 amb 1001 {
920 amb 1005 clearSearchResult(marker);
921    
922 amb 1011 document.getElementById("coords" + marker).style.display="none";
923     document.getElementById("search" + marker).style.display="";
924 amb 1001 }
925    
926    
927     //
928     // Display coordinates for the marker
929     //
930    
931 amb 1035 function markerCoords(marker) // called from router.html
932 amb 1001 {
933 amb 1005 clearSearchResult(marker);
934    
935 amb 1011 document.getElementById("search" + marker).style.display="none";
936     document.getElementById("coords" + marker).style.display="";
937 amb 1001 }
938    
939    
940     //
941 amb 577 // Centre the marker on the map
942     //
943    
944 amb 1035 function markerCentre(marker) // called from router.html
945 amb 577 {
946 amb 1034 if(!routino.point[marker].used)
947     return;
948    
949 amb 1005 clearSearchResult(marker);
950    
951 amb 985 var lonlat=map.getCenter().clone();
952 amb 1009 lonlat.transform(epsg900913,epsg4326);
953 amb 577
954 amb 1032 formSetCoords(marker,lonlat.lon,lonlat.lat);
955 amb 574 }
956    
957    
958     //
959 amb 984 // Centre the map on the marker
960     //
961    
962 amb 1035 function markerRecentre(marker) // called from router.html
963 amb 984 {
964 amb 1034 if(!routino.point[marker].used)
965     return;
966    
967 amb 1005 clearSearchResult(marker);
968    
969 amb 985 lon=routino.point[marker].lon;
970     lat=routino.point[marker].lat;
971 amb 984
972 amb 1009 var lonlat = new OpenLayers.LonLat(lon,lat);
973     lonlat.transform(epsg4326,epsg900913);
974 amb 984
975     map.panTo(lonlat);
976     }
977    
978    
979     //
980 amb 574 // Clear the current marker.
981     //
982    
983 amb 1035 function markerRemove(marker) // called from router.html
984 amb 574 {
985 amb 1005 clearSearchResult(marker);
986    
987 amb 1033 for(var m=marker;m<vismarkers;m++)
988     markerMove(m,m+1);
989 amb 574
990 amb 1033 markerRemoveForm(vismarkers--);
991 amb 574
992     if(vismarkers==1)
993     markerAddAfter(1);
994     }
995    
996    
997     //
998     // Add a marker before the current one.
999     //
1000    
1001     function markerAddBefore(marker)
1002     {
1003 amb 1005 clearSearchResult(marker);
1004    
1005 amb 1012 if(vismarkers==mapprops.maxmarkers || marker==1)
1006 amb 574 return false;
1007    
1008 amb 1033 markerAddForm(++vismarkers);
1009 amb 574
1010 amb 1033 for(var m=vismarkers;m>marker;m--)
1011     markerMove(m,m-1);
1012 amb 574
1013 amb 1033 markerClearForm(marker-1);
1014 amb 574 }
1015    
1016    
1017     //
1018     // Add a marker after the current one.
1019     //
1020    
1021 amb 1035 function markerAddAfter(marker) // called from router.html
1022 amb 574 {
1023 amb 1005 clearSearchResult(marker);
1024    
1025 amb 1012 if(vismarkers==mapprops.maxmarkers)
1026 amb 574 return false;
1027    
1028 amb 1033 markerAddForm(++vismarkers);
1029 amb 574
1030 amb 1033 for(var m=vismarkers;m>(marker+1);m--)
1031     markerMove(m,m-1);
1032 amb 574
1033 amb 1033 markerClearForm(marker+1);
1034 amb 569 }
1035    
1036    
1037     //
1038 amb 577 // Set this marker as the home location.
1039     //
1040    
1041 amb 1035 function markerHome(marker) // called from router.html
1042 amb 577 {
1043 amb 1034 if(!routino.point[marker].used)
1044     return;
1045    
1046 amb 1005 clearSearchResult(marker);
1047    
1048 amb 577 if(markerHomeCookie(marker))
1049 amb 1012 for(marker=1;marker<=mapprops.maxmarkers;marker++)
1050 amb 577 updateIcon(marker);
1051     }
1052    
1053    
1054     //
1055 amb 984 // Set this marker as the current location.
1056     //
1057    
1058 amb 1035 function markerLocate(marker) // called from router.html
1059 amb 984 {
1060 amb 1005 clearSearchResult(marker);
1061    
1062 amb 984 if(navigator.geolocation)
1063     navigator.geolocation.getCurrentPosition(
1064     function(position) {
1065 amb 1032 formSetCoords(marker,position.coords.longitude,position.coords.latitude);
1066     markerAddMap(marker);
1067 amb 984 });
1068     }
1069    
1070    
1071     //
1072 amb 577 // Update an icon to set colours and home or normal marker.
1073     //
1074    
1075     function updateIcon(marker)
1076     {
1077 amb 985 var lon=routino.point[marker].lon;
1078     var lat=routino.point[marker].lat;
1079 amb 577
1080     if(lon==homelon && lat==homelat)
1081     {
1082 amb 985 if(routino.point[marker].active)
1083 amb 577 document.images["waypoint" + marker].src="icons/marker-home-red.png";
1084     else
1085     document.images["waypoint" + marker].src="icons/marker-home-grey.png";
1086    
1087     markers[marker].style.externalGraphic="icons/marker-home-red.png";
1088     }
1089     else
1090     {
1091 amb 985 if(routino.point[marker].active)
1092 amb 577 document.images["waypoint" + marker].src="icons/marker-" + marker + "-red.png";
1093     else
1094     document.images["waypoint" + marker].src="icons/marker-" + marker + "-grey.png";
1095    
1096     markers[marker].style.externalGraphic="icons/marker-" + marker + "-red.png";
1097     }
1098    
1099     layerVectors.drawFeature(markers[marker]);
1100     }
1101    
1102    
1103     //
1104     // Set or clear the home marker icon
1105     //
1106    
1107     function markerHomeCookie(marker)
1108     {
1109 amb 985 var lon=routino.point[marker].lon;
1110     var lat=routino.point[marker].lat;
1111 amb 577
1112     if(lon=="" || lat=="")
1113     return(false);
1114    
1115     var cookie;
1116     var date = new Date();
1117    
1118     if((homelat==null && homelon==null) ||
1119     (homelat!=lat && homelon!=lon))
1120     {
1121     cookie="Routino-home=lon:" + lon + ":lat:" + lat;
1122    
1123     date.setUTCFullYear(date.getUTCFullYear()+5);
1124    
1125     homelat=lat;
1126     homelon=lon;
1127     }
1128     else
1129     {
1130     cookie="Routino-home=unset";
1131    
1132     date.setUTCFullYear(date.getUTCFullYear()-1);
1133    
1134     homelat=null;
1135     homelon=null;
1136     }
1137    
1138     document.cookie=cookie + ";expires=" + date.toGMTString();
1139    
1140     return(true);
1141     }
1142    
1143    
1144     //
1145 amb 574 // Move this marker up.
1146     //
1147    
1148 amb 1035 function markerMoveUp(marker) // called from router.html
1149 amb 574 {
1150     if(marker==1)
1151 amb 984 {
1152     for(var m=1;m<vismarkers;m++)
1153     markerSwap(m,m+1);
1154     }
1155     else
1156     markerSwap(marker,marker-1);
1157 amb 574 }
1158    
1159    
1160     //
1161     // Move this marker down.
1162     //
1163    
1164 amb 1035 function markerMoveDown(marker) // called from router.html
1165 amb 574 {
1166     if(marker==vismarkers)
1167 amb 984 {
1168     for(var m=vismarkers;m>1;m--)
1169     markerSwap(m,m-1);
1170     }
1171     else
1172     markerSwap(marker,marker+1);
1173 amb 574 }
1174    
1175    
1176     //
1177 amb 1033 // Move a marker from one place to another.
1178     //
1179    
1180     function markerMove(marker1,marker2)
1181     {
1182     for(var element in routino.point[marker2])
1183     {
1184     routino.point[marker1][element]=routino.point[marker2][element];
1185     }
1186    
1187     document.getElementById("search" + marker1).style.display=document.getElementById("search" + marker2).style.display;
1188    
1189     document.getElementById("coords" + marker1).style.display=document.getElementById("coords" + marker2).style.display;
1190    
1191     document.forms["form"].elements["search" + marker1].value=document.forms["form"].elements["search" + marker2].value;
1192    
1193     formSetCoords(marker1,routino.point[marker1].lon,routino.point[marker1].lat);
1194    
1195     markerAddRemoveMap(marker1,routino.point[marker1].active);
1196     }
1197    
1198    
1199     //
1200 amb 574 // Swap a pair of markers.
1201     //
1202    
1203     function markerSwap(marker1,marker2)
1204     {
1205 amb 1033 for(var element in routino.point[marker2])
1206     {
1207     var temp=routino.point[marker1][element];
1208     routino.point[marker1][element]=routino.point[marker2][element];
1209     routino.point[marker2][element]=temp;
1210     }
1211 amb 574
1212 amb 1033 var search_display=document.getElementById("search" + marker1).style.display;
1213     document.getElementById("search" + marker1).style.display=document.getElementById("search" + marker2).style.display;
1214     document.getElementById("search" + marker2).style.display=search_display;
1215 amb 574
1216 amb 1033 var coords_display=document.getElementById("coords" + marker1).style.display;
1217     document.getElementById("coords" + marker1).style.display=document.getElementById("coords" + marker2).style.display;
1218     document.getElementById("coords" + marker2).style.display=coords_display;
1219    
1220     var search_value=document.forms["form"].elements["search" + marker1].value;
1221     document.forms["form"].elements["search" + marker1].value=document.forms["form"].elements["search" + marker2].value;
1222     document.forms["form"].elements["search" + marker2].value=search_value;
1223    
1224     formSetCoords(marker1,routino.point[marker1].lon,routino.point[marker1].lat);
1225     formSetCoords(marker2,routino.point[marker2].lon,routino.point[marker2].lat);
1226    
1227     markerAddRemoveMap(marker1,routino.point[marker1].active);
1228     markerAddRemoveMap(marker2,routino.point[marker2].active);
1229 amb 574 }
1230    
1231    
1232     //
1233     // Reverse the markers.
1234     //
1235    
1236 amb 1035 function markersReverse() // called from router.html
1237 amb 574 {
1238     for(var marker=1;marker<=vismarkers/2;marker++)
1239     markerSwap(marker,vismarkers+1-marker);
1240     }
1241    
1242    
1243 amb 1033 //
1244     // Display the form for a marker
1245     //
1246    
1247     function markerAddForm(marker)
1248     {
1249     document.getElementById("waypoint" + marker).style.display="";
1250     }
1251    
1252    
1253     //
1254     // Hide the form for a marker
1255     //
1256    
1257     function markerRemoveForm(marker)
1258     {
1259     document.getElementById("waypoint" + marker).style.display="none";
1260    
1261     markerClearForm(marker);
1262     }
1263    
1264    
1265     //
1266     // Clear the form for a marker
1267     //
1268    
1269     function markerClearForm(marker)
1270     {
1271     markerRemoveMap(marker);
1272     markerCoords(marker);
1273    
1274     formSetCoords(marker,"","");
1275     formSetSearch(marker,"");
1276    
1277     updateIcon(marker);
1278    
1279     routino.point[marker].used=false;
1280     }
1281    
1282    
1283 amb 577 ////////////////////////////////////////////////////////////////////////////////
1284     //////////////////////////// Route results handling ////////////////////////////
1285     ////////////////////////////////////////////////////////////////////////////////
1286    
1287     var route_light_colours={shortest: "#60C060", quickest: "#6060C0"};
1288     var route_dark_colours ={shortest: "#408040", quickest: "#404080"};
1289    
1290     var highlights={shortest: null, quickest: null};
1291     var popups={shortest: null, quickest: null};
1292     var routepoints={shortest: {}, quickest: {}};
1293     var gpx_style={shortest: null, quickest: null};
1294    
1295 amb 574 //
1296 amb 577 // Zoom to a specific item in the route
1297 amb 569 //
1298    
1299 amb 577 function zoomTo(type,line)
1300 amb 569 {
1301 amb 1009 var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat);
1302     lonlat.transform(epsg4326,epsg900913);
1303 amb 569
1304 amb 577 map.moveTo(lonlat,map.numZoomLevels-2);
1305 amb 569 }
1306    
1307    
1308     //
1309 amb 577 // Highlight a specific item in the route
1310 amb 569 //
1311    
1312 amb 577 function highlight(type,line)
1313 amb 569 {
1314 amb 577 if(line==-1)
1315     {
1316     highlights[type].style.display = "none";
1317 amb 569
1318 amb 577 drawPopup(popups[type],null);
1319 amb 569 }
1320 amb 577 else
1321 amb 569 {
1322 amb 577 // Marker
1323 amb 569
1324 amb 1009 var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat);
1325     lonlat.transform(epsg4326,epsg900913);
1326 amb 569
1327 amb 577 highlights[type].move(lonlat);
1328 amb 569
1329 amb 577 if(highlights[type].style.display = "none")
1330     highlights[type].style.display = "";
1331 amb 569
1332 amb 577 // Popup
1333 amb 569
1334 amb 577 drawPopup(popups[type],"<table>" + routepoints[type][line].html + "</table>");
1335     }
1336    
1337     layerVectors.drawFeature(highlights[type]);
1338 amb 569 }
1339    
1340    
1341     //
1342 amb 577 // Create a popup - not using OpenLayers because want it fixed on screen not fixed on map.
1343 amb 569 //
1344    
1345 amb 577 function createPopup(type)
1346 amb 569 {
1347 amb 577 var popup=document.createElement('div');
1348 amb 569
1349 amb 577 popup.className = "popup";
1350 amb 569
1351 amb 577 popup.innerHTML = "<span></span>";
1352 amb 569
1353 amb 577 popup.style.display = "none";
1354 amb 569
1355 amb 577 popup.style.position = "fixed";
1356     popup.style.top = "-4000px";
1357     popup.style.left = "-4000px";
1358     popup.style.zIndex = "100";
1359 amb 569
1360 amb 577 popup.style.padding = "5px";
1361 amb 569
1362 amb 577 popup.style.opacity=0.85;
1363     popup.style.backgroundColor=route_light_colours[type];
1364     popup.style.border="4px solid " + route_dark_colours[type];
1365 amb 569
1366 amb 577 document.body.appendChild(popup);
1367 amb 569
1368 amb 577 return(popup);
1369 amb 569 }
1370    
1371    
1372     //
1373 amb 577 // Draw a popup - not using OpenLayers because want it fixed on screen not fixed on map.
1374 amb 569 //
1375    
1376 amb 577 function drawPopup(popup,html)
1377 amb 569 {
1378 amb 577 if(html==null)
1379 amb 569 {
1380 amb 577 popup.style.display="none";
1381     return;
1382     }
1383 amb 574
1384 amb 577 if(popup.style.display=="none")
1385 amb 569 {
1386 amb 577 var map_div=document.getElementById("map");
1387 amb 569
1388 amb 577 popup.style.left =map_div.offsetParent.offsetLeft+map_div.offsetLeft+60 + "px";
1389     popup.style.top = map_div.offsetTop +30 + "px";
1390     popup.style.width =map_div.clientWidth-100 + "px";
1391 amb 574
1392 amb 577 popup.style.display="";
1393 amb 569 }
1394    
1395 amb 577 popup.innerHTML=html;
1396 amb 569 }
1397    
1398    
1399     //
1400 amb 577 // Remove a GPX trace
1401 amb 569 //
1402    
1403 amb 577 function removeGPXTrace(type)
1404 amb 569 {
1405 amb 577 map.removeLayer(layerGPX[type]);
1406     layerGPX[type].destroy();
1407     layerGPX[type]=null;
1408 amb 569
1409 amb 577 displayStatus(type,"no_info");
1410 amb 569
1411 amb 1011 document.getElementById(type + "_links").style.display = "none";
1412 amb 569
1413 amb 1011 document.getElementById(type + "_route").innerHTML = "";
1414 amb 569
1415 amb 577 hideshow_hide(type);
1416     }
1417 amb 569
1418 amb 574
1419 amb 577 ////////////////////////////////////////////////////////////////////////////////
1420     /////////////////////////////// Server handling ////////////////////////////////
1421     ////////////////////////////////////////////////////////////////////////////////
1422 amb 569
1423     //
1424     // Display data statistics
1425     //
1426    
1427 amb 1035 function displayStatistics() // called from router.html
1428 amb 569 {
1429     // Use AJAX to get the statistics
1430    
1431 amb 1013 OpenLayers.Request.GET({url: "statistics.cgi", success: runStatisticsSuccess});
1432 amb 569 }
1433    
1434    
1435     //
1436 amb 577 // Success in running data statistics generation.
1437 amb 569 //
1438    
1439     function runStatisticsSuccess(response)
1440     {
1441 amb 1011 document.getElementById("statistics_data").innerHTML="<pre>" + response.responseText + "</pre>";
1442     document.getElementById("statistics_link").style.display="none";
1443 amb 569 }
1444    
1445    
1446     //
1447 amb 577 // Submit form - perform the routing
1448 amb 569 //
1449    
1450 amb 1035 function findRoute(type) // called from router.html
1451 amb 569 {
1452     tab_select("results");
1453    
1454 amb 572 hideshow_hide('help_options');
1455 amb 569 hideshow_hide('shortest');
1456     hideshow_hide('quickest');
1457    
1458 amb 577 displayStatus("result","running");
1459 amb 569
1460 amb 986 var url="router.cgi" + "?" + buildURLArguments(true) + ";type=" + type;
1461 amb 569
1462     // Destroy the existing layer(s)
1463    
1464     if(markersmoved || paramschanged)
1465     {
1466     if(layerGPX.shortest!=null)
1467     removeGPXTrace("shortest");
1468     if(layerGPX.quickest!=null)
1469     removeGPXTrace("quickest");
1470     markersmoved=false;
1471     paramschanged=false;
1472     }
1473     else if(layerGPX[type]!=null)
1474     removeGPXTrace(type);
1475    
1476     // Use AJAX to run the router
1477    
1478     routing_type=type;
1479    
1480 amb 1013 OpenLayers.Request.GET({url: url, success: runRouterSuccess, failure: runRouterFailure});
1481 amb 569 }
1482    
1483    
1484     //
1485     // Success in running router.
1486     //
1487    
1488     function runRouterSuccess(response)
1489     {
1490     var lines=response.responseText.split('\n');
1491    
1492     var uuid=lines[0];
1493 amb 935 var cpuinfo=lines[1]; // not used
1494     var distinfo=lines[2]; // not used
1495     var message=lines[3]; // content not used
1496 amb 569
1497 amb 629 var link;
1498    
1499 amb 569 // Update the status message
1500    
1501     if(message!="")
1502     {
1503 amb 577 displayStatus("result","error");
1504 amb 572 hideshow_show('help_route');
1505 amb 629
1506     link=document.getElementById("router_log_error");
1507     link.href="results.cgi?uuid=" + uuid + ";type=router;format=log";
1508    
1509 amb 569 return;
1510     }
1511     else
1512     {
1513 amb 577 displayStatus("result","complete");
1514 amb 572 hideshow_hide('help_route');
1515 amb 629
1516     link=document.getElementById("router_log_complete");
1517     link.href="results.cgi?uuid=" + uuid + ";type=router;format=log";
1518 amb 569 }
1519    
1520     // Update the routing result message
1521    
1522 amb 577 link=document.getElementById(routing_type + "_html");
1523     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=html";
1524 amb 1011
1525 amb 577 link=document.getElementById(routing_type + "_gpx_track");
1526     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track";
1527 amb 1011
1528 amb 577 link=document.getElementById(routing_type + "_gpx_route");
1529     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-route";
1530 amb 1011
1531 amb 577 link=document.getElementById(routing_type + "_text_all");
1532     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=text-all";
1533 amb 1011
1534 amb 577 link=document.getElementById(routing_type + "_text");
1535     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=text";
1536 amb 569
1537 amb 1011 links=document.getElementById(routing_type + "_links").style.display = "";
1538 amb 569
1539     // Add a GPX layer
1540    
1541     var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track";
1542    
1543 amb 1013 layerGPX[routing_type] = new OpenLayers.Layer.Vector("GPX (" + routing_type + ")",
1544     {
1545     protocol: new OpenLayers.Protocol.HTTP({url: url, format: new OpenLayers.Format.GPX()}),
1546     strategies: [new OpenLayers.Strategy.Fixed()],
1547     style: gpx_style[routing_type],
1548     projection: map.displayProjection
1549     });
1550 amb 569
1551     map.addLayer(layerGPX[routing_type]);
1552    
1553     hideshow_show(routing_type);
1554    
1555     displayResult(routing_type,uuid);
1556     }
1557    
1558    
1559     //
1560     // Failure in running router.
1561     //
1562    
1563     function runRouterFailure(response)
1564     {
1565 amb 577 displayStatus("result","failed");
1566 amb 569 }
1567    
1568    
1569     //
1570 amb 577 // Display the status
1571     //
1572    
1573     function displayStatus(type,subtype,content)
1574     {
1575 amb 1011 var child=document.getElementById(type + "_status").firstChild;
1576 amb 577
1577     do
1578     {
1579     if(child.id != undefined)
1580     child.style.display="none";
1581    
1582     child=child.nextSibling;
1583     }
1584     while(child != undefined);
1585    
1586 amb 936 var chosen_status=document.getElementById(type + "_status_" + subtype);
1587 amb 577
1588 amb 936 chosen_status.style.display="";
1589 amb 577
1590     if(content != null)
1591 amb 936 chosen_status.innerHTML=content;
1592 amb 577 }
1593    
1594    
1595     //
1596 amb 569 // Display the route
1597     //
1598    
1599     function displayResult(type,uuid)
1600     {
1601     routing_type = type;
1602    
1603     // Add the route
1604    
1605 amb 577 var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=html";
1606 amb 569
1607     // Use AJAX to get the route
1608    
1609 amb 1013 OpenLayers.Request.GET({url: url, success: getRouteSuccess, failure: getRouteFailure});
1610 amb 569 }
1611    
1612    
1613     //
1614     // Success in getting route.
1615     //
1616    
1617     function getRouteSuccess(response)
1618     {
1619     var lines=response.responseText.split('\n');
1620    
1621 amb 577 routepoints[routing_type]=[];
1622 amb 569
1623 amb 577 var points=routepoints[routing_type];
1624 amb 569
1625 amb 577 var table=0;
1626     var point=0;
1627     var total_table,total_word;
1628 amb 569
1629 amb 577 for(var line=0;line<lines.length;line++)
1630 amb 569 {
1631 amb 577 var thisline=lines[line];
1632 amb 569
1633 amb 577 if(table==0)
1634     {
1635     if(thisline.match('<table>'))
1636     table=1;
1637     else
1638     continue;
1639     }
1640 amb 574
1641 amb 577 if(thisline.match('</table>'))
1642     break;
1643    
1644     if(thisline.match('<tr class=\'([a-z])\'>'))
1645 amb 569 {
1646 amb 577 var rowtype=RegExp.$1;
1647 amb 569
1648 amb 577 if(rowtype=='c')
1649     {
1650     thisline.match('<td class=\'r\'> *([-0-9.]+) *([-0-9.]+)');
1651     points[point]={lat: Number(RegExp.$1), lon: Number(RegExp.$2), html: "", highway: "", distance: "", total: ""};
1652 amb 574
1653 amb 577 point++;
1654     }
1655     else if(rowtype=='n')
1656     {
1657     points[point-1].html += thisline;
1658     }
1659     else if(rowtype=='s')
1660     {
1661     thisline.match('<span class=\'h\'>([^<]+)</span>');
1662     points[point-1].highway = RegExp.$1;
1663 amb 574
1664 amb 577 thisline.match('<span class=\'d\'>([^<]+)</span>');
1665     points[point-1].distance = RegExp.$1;
1666 amb 574
1667 amb 577 thisline.match('(<span class=\'j\'>[^<]+</span>)');
1668     points[point-1].total = RegExp.$1;
1669    
1670     thisline.match('^(.*).<span class=\'j\'>');
1671    
1672     points[point-1].html += RegExp.$1;
1673     }
1674     else if(rowtype=='t')
1675     {
1676     points[point-1].html += thisline;
1677    
1678     thisline.match('^(.*<td class=\'r\'>)');
1679     total_table = RegExp.$1;
1680    
1681     thisline.match('<td class=\'l\'>([^<]+)<');
1682     total_word = RegExp.$1;
1683    
1684     thisline.match('<span class=\'j\'>([^<]+)</span>');
1685     points[point-1].total = RegExp.$1;
1686     }
1687 amb 569 }
1688     }
1689    
1690 amb 935 displayStatus(routing_type,"info",points[point-1].total.bold());
1691    
1692 amb 577 var result="<table onmouseout='highlight(\"" + routing_type + "\",-1)'>";
1693    
1694     for(var p=0;p<point-1;p++)
1695     {
1696     points[p].html += total_table + points[p].total;
1697    
1698     result=result + "<tr onclick='zoomTo(\"" + routing_type + "\"," + p + ")'" +
1699     " onmouseover='highlight(\"" + routing_type + "\"," + p + ")'>" +
1700     "<td class='distance' title='" + points[p].distance + "'>#" + (p+1) +
1701     "<td class='highway'>" + points[p].highway;
1702     }
1703    
1704     result=result + "<tr onclick='zoomTo(\"" + routing_type + "\"," + p + ")'" +
1705     " onmouseover='highlight(\"" + routing_type + "\"," + p + ")'>" +
1706     "<td colspan='2'>" + total_word + " " + points[p].total;
1707    
1708 amb 569 result=result + "</table>";
1709    
1710 amb 1011 document.getElementById(routing_type + "_route").innerHTML=result;
1711 amb 569 }
1712    
1713    
1714     //
1715     // Failure in getting route.
1716     //
1717    
1718     function getRouteFailure(response)
1719     {
1720 amb 1011 document.getElementById(routing_type + "_route").innerHTML = "";
1721 amb 569 }
1722 amb 1001
1723    
1724     //
1725     // Perform a search
1726     //
1727    
1728     function DoSearch(marker)
1729     {
1730     // Use AJAX to get the search result
1731    
1732     var search=routino.point[marker].search;
1733    
1734 amb 1009 var bounds=map.getExtent().clone();
1735     bounds.transform(epsg900913,epsg4326);
1736 amb 1001
1737 amb 1008 var url="search.cgi?marker=" + marker +
1738 amb 1009 ";left=" + format5f(bounds.left) +
1739     ";top=" + format5f(bounds.top) +
1740     ";right=" + format5f(bounds.right) +
1741     ";bottom=" + format5f(bounds.bottom) +
1742 amb 1008 ";search=" + encodeURIComponent(search);
1743    
1744 amb 1013 OpenLayers.Request.GET({url: url, success: runSearchSuccess});
1745 amb 1001 }
1746    
1747    
1748 amb 1005 var searchresults=[];
1749    
1750 amb 1001 //
1751     // Success in running search.
1752     //
1753    
1754     function runSearchSuccess(response)
1755     {
1756     var lines=response.responseText.split('\n');
1757    
1758     var marker=lines[0];
1759     var cpuinfo=lines[1]; // not used
1760 amb 1005 var message=lines[2]; // not used
1761 amb 1001
1762 amb 1005 if(message != "")
1763     {
1764     alert(message);
1765     return;
1766     }
1767 amb 1001
1768 amb 1005 searchresults[marker]=[];
1769    
1770     for(var line=3;line<lines.length;line++)
1771     {
1772     var thisline=lines[line];
1773    
1774     if(thisline=="")
1775     break;
1776    
1777     thisline.match('([-.0-9]+) ([-.0-9]+) (.*)');
1778    
1779     searchresults[marker][searchresults[marker].length]={lat: Number(RegExp.$1), lon: Number(RegExp.$2), name: RegExp.$3};
1780     }
1781    
1782     if(searchresults[marker].length==1)
1783     {
1784     formSetSearch(marker,searchresults[marker][0].name);
1785 amb 1032 formSetCoords(marker,searchresults[marker][0].lon,searchresults[marker][0].lat);
1786     markerAddMap(marker);
1787 amb 1005 }
1788     else
1789     {
1790     var results=document.getElementById("searchresults" + marker);
1791    
1792     var innerHTML="<td colspan=\"3\">";
1793    
1794     for(var n=0;n<searchresults[marker].length;n++)
1795     {
1796     if(n>0)
1797     innerHTML+="<br>";
1798    
1799     innerHTML+="<a href=\"#\" onclick=\"choseSearchResult(" + marker + "," + n + ")\">" +
1800     searchresults[marker][n].name +
1801     "</a>";
1802     }
1803    
1804     results.innerHTML=innerHTML;
1805    
1806     results.style.display="";
1807     }
1808 amb 1001 }
1809 amb 1005
1810    
1811     //
1812     // Display search results.
1813     //
1814    
1815     function choseSearchResult(marker,n)
1816     {
1817     if(n>=0)
1818     {
1819     formSetSearch(marker,searchresults[marker][n].name);
1820 amb 1032 formSetCoords(marker,searchresults[marker][n].lon,searchresults[marker][n].lat);
1821     markerAddMap(marker);
1822 amb 1005 }
1823     }
1824    
1825    
1826     //
1827     // Clear search results.
1828     //
1829    
1830     function clearSearchResult(marker)
1831     {
1832 amb 1011 document.getElementById("searchresults" + marker).style.display="none";
1833 amb 1005 }