Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 14.71 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var markersArray = [];
  2. var infowindow;
  3. function addMarker(location,green)
  4. {
  5.     marker = new google.maps.Marker(
  6.     {
  7.         position: location,
  8.         map: map
  9.     });
  10.         if (green)
  11.         {
  12.                 marker.setIcon("markergreen.png");
  13.         }
  14.     markersArray.push(marker);
  15. }
  16. // Removes the overlays from the map, but keeps them in the array
  17.  
  18. function clearOverlays()
  19. {
  20.     if (markersArray)
  21.     {
  22.         for (i in markersArray)
  23.         {
  24.             markersArray[i].setMap(null);
  25.         }
  26.     }
  27. }
  28. // Shows any overlays currently in the array
  29.  
  30. function showOverlays()
  31. {
  32.     if (markersArray)
  33.     {
  34.         for (i in markersArray)
  35.         {
  36.             markersArray[i].setMap(map);
  37.         }
  38.     }
  39. }
  40. // Deletes all markers in the array by removing references to them
  41.  
  42. function deleteOverlays()
  43. {
  44.     if (markersArray)
  45.     {
  46.         for (i in markersArray)
  47.         {
  48.             markersArray[i].setMap(null);
  49.         }
  50.         markersArray.length = 0;
  51.     }
  52. }
  53. $(document).ready(function ()
  54. {
  55.     $.getJSON('api.php?m=counties', function (data)
  56.     {
  57.         $.each(data, function (i, item)
  58.         {
  59.             $('#county').append('<option value="' + item.county_name + '">' + item.county_name + '</option>');
  60.             $('#afCounty').append('<option value="' + item.county_name + '">' + item.county_name + '</option>');
  61.         });
  62.     });
  63.     if (document.getElementById('mload').value == 'do')
  64.     {
  65.         initialize();
  66.                 updateHlMarkers();
  67.     }
  68. });
  69. function countyChanged()
  70. {
  71.     var sCounty = document.getElementById('county').value;
  72.     if (sCounty.length > 0)
  73.     {
  74.         $('#town').find('option').remove().end().append('<option value="notselected" selected>--Area--</option>').val('--Area--');
  75.         $.getJSON('api.php?m=towns&p1=' + sCounty, function (data)
  76.         {
  77.             $.each(data, function (i, item)
  78.             {
  79.                 $('#town').append('<option value="' + item.town + '">' + item.town + '</option>');
  80.                 $("#town").fadeIn('slow');
  81.             });
  82.         });
  83.     }
  84. }
  85. function afCountyChanged()
  86. {
  87.     $.getJSON('api.php?m=cCo&p1=' + document.getElementById('afCounty').value, function (data)
  88.     {
  89.         $.each(data, function (i, item)
  90.         {
  91.             var crd = new google.maps.LatLng(item.latitude, item.longitude);
  92.             map.setCenter(crd);
  93.             map.setZoom(10);
  94.         });
  95.     });
  96.     var sCounty = document.getElementById('afCounty').value;
  97.     if (sCounty.length > 0)
  98.     {
  99.         $('#afTown').find('option').remove().end().append('<option value="notselected" selected>--Area--</option>').val('--Area--');
  100.         $.getJSON('api.php?m=towns&p1=' + sCounty, function (data)
  101.         {
  102.             $.each(data, function (i, item)
  103.             {
  104.                 $('#afTown').append('<option value="' + item.town + '">' + item.town + '</option>');
  105.             });
  106.         });
  107.     }
  108. }
  109. function submitStation()
  110. {
  111.     var rVal = true;
  112.     if (document.getElementById('afCounty').value == 'notselected')
  113.     {
  114.         alert('Please select a county!');
  115.         rVal = false;
  116.     }
  117.     else if (document.getElementById('afTown').value == 'notselected')
  118.     {
  119.         alert('Please select a town!');
  120.         rVal = false;
  121.     }
  122.     else if (!document.getElementById('address1').value)
  123.     {
  124.         alert('Please enter an address!');
  125.                 rVal = false
  126.     }
  127.     else if (!document.getElementById('latf').value)
  128.     {
  129.         alert('Please select the station location on the map!');
  130.         rVal = false;
  131.     }
  132.         else if (!document.getElementById('email').value)
  133.         {
  134.                 alert('Please enter a valid email!');
  135.                 rVal = false;
  136.         }
  137.         else if (isNaN(document.getElementById('pprice').value) || isNaN(document.getElementById('dprice').value) || document.getElementById('dprice').value.length < 0 || document.getElementById('pprice').value.length < 0)
  138.         {
  139.                 alert('Please enter a valid price, in cents!');
  140.                 rVal = false;
  141.         }
  142.         else if (!document.getElementById('sname').value)
  143.         {
  144.                 alert('Please enter a station name!');
  145.                 rVal = false;
  146.         }
  147.         return rVal;
  148. }
  149. function scCountyChanged()
  150. {
  151.     $.getJSON('api.php?m=cCo&p1=' + document.getElementById('afCounty').value, function (data)
  152.     {
  153.         $.each(data, function (i, item)
  154.         {
  155.             var crd = new google.maps.LatLng(item.latitude, item.longitude);
  156.             map.setCenter(crd);
  157.             map.setZoom(10);
  158.         });
  159.     });
  160.     updateMarkers();
  161. }
  162. function scTownChanged()
  163. {
  164.     if (document.getElementById('afTown').value != 'notselected')
  165.     {
  166.         if (document.getElementById('afTown').value != 'Other')
  167.         {
  168.             $.getJSON('api.php?m=cTo&p1=' + document.getElementById('afTown').value, function (data)
  169.             {
  170.                 $.each(data, function (i, item)
  171.                 {
  172.                     var crd = new google.maps.LatLng(item.latitude, item.longitude);
  173.                     map.setCenter(crd);
  174.                     map.setZoom(12);
  175.                 });
  176.             });
  177.         }
  178.         else
  179.         {
  180.             scCountyChanged();
  181.         }
  182.     }
  183.     updateMarkers();
  184. }
  185. function updateHlMarkers()
  186. {
  187.     deleteOverlays();
  188.     purgeResTable();
  189.         var temp = true;
  190.     if (document.getElementById('afCounty').value == "notselected")
  191.     {
  192.         $.getJSON('api.php?p1=petrol&m=hl1', function (data) {        
  193.             $.each(data, function (i, item)
  194.             {
  195.                 var loc = new google.maps.LatLng(item.latitude, item.longitude);
  196.                 var markerMsg = '<div style="height:80px"><a href="petrol-stations.php?id=' + item.id + '">' + item.name + '</a><br/><b>' + item.address + '</b><br />Petrol: ' + item.petrol + 'c<br />Diesel: ' + item.diesel + 'c</div>';
  197.                 addMarker(loc,temp);
  198.                 if (!infowindow)
  199.                 {
  200.                     infowindow = new google.maps.InfoWindow(
  201.                     {
  202.                         content: markerMsg
  203.                     });
  204.                 }
  205.                 google.maps.event.addListener(markersArray[markersArray.length - 1], 'click', buildClickHandler(markersArray[markersArray.length - 1], markerMsg));
  206.                                 temp = false;
  207.             });
  208.         });
  209.     }
  210.     else if (document.getElementById('afTown').value == "notselected")
  211.     {
  212.         $.getJSON('api.php?m=hl2&p1=petrol&p2=' + document.getElementById('afCounty').value, function (data)
  213.         {
  214.             $.each(data, function (i, item)
  215.             {
  216.                 var loc = new google.maps.LatLng(item.latitude, item.longitude);
  217.                 var markerMsg = '<div style="height:80px"><a href="petrol-stations.php?id=' + item.id + '">' + item.name + '</a><br/><b>' + item.address + '</b><br />Petrol: ' + item.petrol + 'c<br />Diesel: ' + item.diesel + 'c</div>';
  218.                 addMarker(loc,temp);
  219.                 if (!infowindow)
  220.                 {
  221.                     infowindow = new google.maps.InfoWindow(
  222.                     {
  223.                         content: markerMsg
  224.                     });
  225.                 }
  226.                 google.maps.event.addListener(markersArray[markersArray.length - 1], 'click', buildClickHandler(markersArray[markersArray.length - 1], markerMsg));
  227.                                 temp = false;
  228.             });
  229.         });
  230.     }
  231.     else
  232.     {
  233.         $.getJSON('api.php?m=hl3&p1=petrol&p2=' + document.getElementById('afCounty').value + '&p3=' + document.getElementById('afTown').value, function (data)
  234.         {
  235.             $.each(data, function (i, item)
  236.             {
  237.                 var loc = new google.maps.LatLng(item.latitude, item.longitude);
  238.                 var markerMsg = '<div style="height:80px"><a href="petrol-stations.php?id=' + item.id + '">' + item.name + '</a><br/><b>' + item.address + '</b><br />Petrol: ' + item.petrol + 'c<br />Diesel: ' + item.diesel + 'c</div>';
  239.                 addMarker(loc,temp);
  240.                 if (!infowindow)
  241.                 {
  242.                     infowindow = new google.maps.InfoWindow(
  243.                     {
  244.                         content: markerMsg
  245.                     });
  246.                 }
  247.                 google.maps.event.addListener(markersArray[markersArray.length - 1], 'click', buildClickHandler(markersArray[markersArray.length - 1], markerMsg));
  248.                                 temp = false;
  249.             });
  250.         });
  251.     }
  252.        
  253.  
  254.     if (document.getElementById('afCounty').value == "notselected")
  255.     {
  256.                 temp = true;
  257.         $.getJSON('api.php?p1=diesel&m=hl1', function (data) {        
  258.             $.each(data, function (i, item)
  259.             {
  260.                                 alert(temp);
  261.                 var loc = new google.maps.LatLng(item.latitude, item.longitude);
  262.                 var markerMsg = '<div style="height:80px"><a href="petrol-stations.php?id=' + item.id + '">' + item.name + '</a><br/><b>' + item.address + '</b><br />Petrol: ' + item.petrol + 'c<br />Diesel: ' + item.diesel + 'c</div>';
  263.                 addMarker(loc,temp);
  264.                 if (!infowindow)
  265.                 {
  266.                     infowindow = new google.maps.InfoWindow(
  267.                     {
  268.                         content: markerMsg
  269.                     });
  270.                 }
  271.                 google.maps.event.addListener(markersArray[markersArray.length - 1], 'click', buildClickHandler(markersArray[markersArray.length - 1], markerMsg));
  272.                                 temp = false;
  273.             });
  274.         });
  275.     }
  276.     else if (document.getElementById('afTown').value == "notselected")
  277.     {
  278.                 temp = true;
  279.         $.getJSON('api.php?m=hl2&p1=diesel&p2=' + document.getElementById('afCounty').value, function (data)
  280.         {
  281.             $.each(data, function (i, item)
  282.             {
  283.                                 alert(temp);
  284.                 var loc = new google.maps.LatLng(item.latitude, item.longitude);
  285.                 var markerMsg = '<div style="height:80px"><a href="petrol-stations.php?id=' + item.id + '">' + item.name + '</a><br/><b>' + item.address + '</b><br />Petrol: ' + item.petrol + 'c<br />Diesel: ' + item.diesel + 'c</div>';
  286.                 addMarker(loc,temp);
  287.                 if (!infowindow)
  288.                 {
  289.                     infowindow = new google.maps.InfoWindow(
  290.                     {
  291.                         content: markerMsg
  292.                     });
  293.                 }
  294.                 google.maps.event.addListener(markersArray[markersArray.length - 1], 'click', buildClickHandler(markersArray[markersArray.length - 1], markerMsg));
  295.                                 temp = false;
  296.             });
  297.         });
  298.     }
  299.     else
  300.     {
  301.                 temp = true;
  302.         $.getJSON('api.php?m=hl3&p1=diesel&p2=' + document.getElementById('afCounty').value + '&p3=' + document.getElementById('afTown').value, function (data)
  303.         {
  304.             $.each(data, function (i, item)
  305.             {
  306.                                 alert(temp);
  307.                 var loc = new google.maps.LatLng(item.latitude, item.longitude);
  308.                 var markerMsg = '<div style="height:80px"><a href="petrol-stations.php?id=' + item.id + '">' + item.name + '</a><br/><b>' + item.address + '</b><br />Petrol: ' + item.petrol + 'c<br />Diesel: ' + item.diesel + 'c</div>';
  309.                 addMarker(loc,temp);
  310.                 if (!infowindow)
  311.                 {
  312.                     infowindow = new google.maps.InfoWindow(
  313.                     {
  314.                         content: markerMsg
  315.                     });
  316.                 }
  317.                 google.maps.event.addListener(markersArray[markersArray.length - 1], 'click', buildClickHandler(markersArray[markersArray.length - 1], markerMsg));
  318.                                 temp = false;
  319.             });
  320.         });
  321.     }
  322.  
  323. }
  324. function hlChanged(wasC)
  325. {
  326.     if (wasC)
  327.     {
  328.         $.getJSON('api.php?m=cCo&p1=' + document.getElementById('afCounty').value, function (data)
  329.         {
  330.             $.each(data, function (i, item)
  331.             {
  332.                 var crd = new google.maps.LatLng(item.latitude, item.longitude);
  333.                 map.setCenter(crd);
  334.                 map.setZoom(10);
  335.             });
  336.         });
  337.         $('#afTown').find('option').remove().end().append('<option value="notselected" selected>--Area--</option>').val('--Area--');
  338.         $.getJSON('api.php?m=towns&p1=' + document.getElementById('afCounty').value, function (data)
  339.         {
  340.             $.each(data, function (i, item)
  341.             {
  342.                 $('#afTown').append('<option value="' + item.town + '">' + item.town + '</option>');
  343.             });
  344.         });
  345.     }
  346.     if (document.getElementById('afTown').value != 'notselected')
  347.     {
  348.         if (document.getElementById('afTown').value != 'Other')
  349.         {
  350.             $.getJSON('api.php?m=cTo&p1=' + document.getElementById('afTown').value, function (data)
  351.             {
  352.                 $.each(data, function (i, item)
  353.                 {
  354.                     var crd = new google.maps.LatLng(item.latitude, item.longitude);
  355.                     map.setCenter(crd);
  356.                     map.setZoom(12);
  357.                 });
  358.             });
  359.         }
  360.         else
  361.         {
  362.             $.getJSON('api.php?m=cCo&p1=' + document.getElementById('afCounty').value, function (data)
  363.             {
  364.                 $.each(data, function (i, item)
  365.                 {
  366.                     var crd = new google.maps.LatLng(item.latitude, item.longitude);
  367.                     map.setCenter(crd);
  368.                     map.setZoom(10);
  369.                 });
  370.             });
  371.         }
  372.     }
  373.     updateHlMarkers();
  374. }
  375. function buildClickHandler(marker, message)
  376. {
  377.     return function ()
  378.     {
  379.         infowindow.setContent(message);
  380.         infowindow.open(map, marker);
  381.     }
  382. }
  383. function updateMarkers()
  384. {
  385.     deleteOverlays();
  386.     purgeResTable();
  387.     $.getJSON('api.php?m=stations&p1=' + document.getElementById('afCounty').value + '&p2=' + document.getElementById('afTown').value, function (data)
  388.     {
  389.         $.each(data, function (i, item)
  390.         {
  391.             var loc = new google.maps.LatLng(item.latitude, item.longitude);
  392.             var markerMsg = '<div style="height:80px"><a href="petrol-stations.php?id=' + item.id + '">' + item.name + '</a><br/><b>' + item.address + '</b><br />Petrol: ' + item.petrol + 'c<br />Diesel: ' + item.diesel + 'c</div>';
  393.             addMarker(loc);
  394.             if (!infowindow)
  395.             {
  396.                 infowindow = new google.maps.InfoWindow(
  397.                 {
  398.                     content: markerMsg
  399.                 });
  400.             }
  401.             google.maps.event.addListener(markersArray[markersArray.length - 1], 'click', buildClickHandler(markersArray[markersArray.length - 1], markerMsg));
  402.             $('#resTable tr:last').after('<tr><td><a href="petrol-stations.php?id=' + item.id + '">' + item.name + '</a></td><td>' + item.town + ', Co. ' + item.county + '</td><td>' + item.petrol + '</td><td>' + item.diesel + '</td></tr>');
  403.             $("#resDiv").fadeIn('slow');
  404.         });
  405.     });
  406. }
  407. function purgeResTable()
  408. {
  409.     $("#resTable").find("tr:gt(0)").remove();
  410. }