﻿// JScript File to find nearest Finning Branch

//<![CDATA[

var map;
var currentMapType; // global var to house the current map type
var icon0;
var newpoints = new Array();
var markers = new Array();


function addLoadEvent(func) { 
    var oldonload = window.onload; 
    if (typeof window.onload != 'function'){ 
        window.onload = func
    } else { 
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

    addLoadEvent(loadMap);
    addLoadEvent(addPoints);



function loadMap() {
    map = new GMap2(document.getElementById("map"));
    map.addMapType(G_SATELLITE_3D_MAP);
    map.addControl(new GHierarchicalMapTypeControl());
      
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng( 54.41892996865827, -7.119140625), 5);
    
    
    if (currentMapType != null)
            map.setMapType(currentMapType);

    icon0 = new GIcon();
    icon0.image = "../_images/logos/f_icon.png";
    icon0.iconSize = new GSize(17, 17);
    icon0.iconAnchor = new GPoint(9, 34);
    icon0.infoWindowAnchor = new GPoint(9, 2);
    icon0.infoShadowAnchor = new GPoint(18, 25);
    


}




function addPoints() {

    newpoints[0] = new Array(52.677195, -2.041322, icon0, 'Cannock (Head Office)', '<span class=popup_header>Cannock (Head Office)</span><br>Watling Street<br>Cannock<br>WS11 8LL<br>Phone: 01543 461461<br>Fax: 01543 461700<br>'); 
    newpoints[1] = new Array(57.196758, -2.0931, icon0, 'Aberdeen', '<span class=popup_header>Aberdeen</span><br>Denmore Industrial Estate<br>Bridge of Don<br>AB23 8JW<br>Phone: 01224 820000<br>Fax: 01224 826307<br>'); 
    newpoints[2] = new Array(51.15004, 0.85334, icon0, 'Ashford', '<span class=popup_header>Ashford</span><br>Cobbs Wood Ind Estate<br>Brunswick Road<br>Ashford<br>Kent<br>TN23 1EN<br>Phone: 01787 272020<br>Fax: 01233 650336<br>'); 
    newpoints[3] = new Array(54.948792, -1.47102, icon0, 'Boldon', '<span class=popup_header>Boldon</span><br>Unit 3 Brooklands Way<br>Boldon Business Park<br>Boldon<br>Tyne And Wear<br>NE35 9LZ<br>Phone: 0191 5377735<br>Fax: 0191 4107890<br>'); 
    newpoints[4] = new Array(51.519562, -2.69182, icon0, 'Bristol', '<span class=popup_header>Bristol</span><br>Unit 5B Severnside Trading Estate<br>St Andrews Road<br>Avonmouth<br>Bristol BS11 9EB<br>Phone: 0117 9802180<br>Fax: 0117 9802181<br>'); 
    newpoints[5] = new Array(51.553051, -3.38297, icon0, 'Cardiff', '<span class=popup_header>Cardiff</span><br>Fforest View Business Park<br>Llantrisant<br>Pontyclun<br>CF72 8LX<br>Phone: 01443 223456<br>Fax: 01443 236299<br>'); 
    newpoints[6] = new Array(53.21912, -1.42202, icon0, 'Chesterfield', '<span class=popup_header>Chesterfield</span><br>Britannia Road<br>Chesterfield<br>Derbyshire<br>S40 2TZ<br>Phone: 01246 541541<br>Fax: 01246 541555<br>'); 
    newpoints[7] = new Array(52.625301, -1.29556, icon0, 'Desford', '<span class=popup_header>Desford</span><br>Peckleton Lane<br>Desford<br>Leicester<br>LE9 9JT<br>Phone: 01455 826026<br>Fax: 01455 826028<br>');  
    newpoints[8] = new Array(55.876942, -3.16784, icon0, 'Edinburgh', '<span class=popup_header>Edinburgh</span><br>Borthwick View<br>Pentland Industrial Estate<br>Loanhead<br>Edinburgh<br>EH20 9QH<br>Phone: 0131 4407080<br>Fax: 0131 4407081<br>');
    newpoints[9] = new Array(55.792042, -4.06541, icon0, 'Glasgow', '<span class=popup_header>Glasgow</span><br>Whistleberry Road<br>Hamilton<br>ML3 0EG<br>Phone: 01698 713713<br>Fax: 01698 711712<br>'); 
    newpoints[10] = new Array(51.940559, 0.65767, icon0, 'Halstead', '<span class=popup_header>Halstead</span><br>19 Fourth Avenue<br>Bluebridge Ind. Estate<br>Halstead<br>Essex<br>CO9 2SY<br>Phone: 01787 272000<br>Fax: 01787 272021<br>'); 
    newpoints[11] = new Array(53.753681, -1.62949, icon0, 'Leeds', '<span class=popup_header>Leeds</span><br>Gelderd Road<br>Gildersome<br>Morley<br>Leeds<br>LS27 7JS<br>Phone: 0113 2534221<br>Fax: 0113 2534221<br>'); 
    newpoints[12] = new Array(52.625301, -1.29556, icon0, 'Leicester', '<span class=popup_header>Leicester</span><br>Peckleton Lane<br>Desford<br>Leicester<br>LE9 9JT<br>Phone: 01455 826010<br>Fax: 01455 826020'); 
    newpoints[13] = new Array(57.505131, -4.45198, icon0, 'Muir Of Ord', '<span class=popup_header>Muir Of Ord</span><br>Muir of Ord Industrial Estate<br>Great North Road<br>IV6 7UA<br>Phone: 01463 870575<br>Fax: 01463 870620<br>'); 
    newpoints[14] = new Array(50.36425, -4.87137, icon0, 'Nanpean', '<span class=popup_header>Nanpean</span><br>Drinnick Road<br>Nanpean<br>St Austell<br>Cornwall<br>PL26 7TU<br>Phone: 01726 823322<br>Fax: 01726 821333<br>'); 
    newpoints[15] = new Array(56.418781, -3.48052, icon0, 'Perth', '<span class=popup_header>Perth</span><br>Ruthenfield Place<br>Inveralmond Industrial Estate<br>Perth<br>PH1 3XU<br>Phone: 01738 623181<br>Fax: 01738 637254<br>');
    newpoints[16] = new Array(52.604191, -0.28261, icon0, 'Peterborough', '<span class=popup_header>Peterborough</span><br>2 Sturrock way<br>Off Bretton Way<br>Peterborough<br>PE3 8YF<br>Phone: 01733 334664<br>Fax: 01733 334674<br>'); 
    newpoints[17] = new Array(50.747929, -1.97389, icon0, 'Poole', '<span class=popup_header>Poole - Construction</span><br>21 Witney Road<br>Poole<br>Dorset<br>BH17 0GL<br>Phone: 01202 330789<br>Fax: 01202 330790<br>'); 
    newpoints[18] = new Array(51.520168, -0.62907, icon0, 'Slough', '<span class=popup_header>Slough - Construction</span><br>558 Ipswich Road<br>Slough Trading Est<br>Slough<br>SL1 4EP<br>Phone: 01753 505070<br>Fax: 01753 505071'); 
    newpoints[19] = new Array(53.196121, -2.49333, icon0, 'Winsford', '<span class=popup_header>Winsford</span><br>12 Road One<br>Winsford Industrial Estate<br>Cheshire<br>CW7 3QE<br>Phone: 01606 594311<br>Fax: 01606 595101</span><br>'); 
    

    for(var i = 0; i < newpoints.length; i++) {
        var point = new GPoint(newpoints[i][1],newpoints[i][0]);
        var popuphtml = newpoints[i][4];
        var marker = createMarker(point,newpoints[i][2],popuphtml);
        markers.push(marker);
        map.addOverlay(marker);
    }
}

function createMarker(point, icon, popuphtml) {
    var popuphtml = "<div id=\"popup\">" + popuphtml + "<\/div>";
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(popuphtml);
    });
    return marker;
}

function closestMarkerSearch(map, markers, zoom, address) {
    if (markers.length == 0) {
        throw new Exception('No markers given');
    }
    new GClientGeocoder().getLatLng(address, function(latLng) {
        if (latLng === null) {
            alert('Please enter a postcode in the correct UK format');
        } else {
            var distance;
            var closestDistance = null;
            var closestMarker = null;
            for (var i = 0; i < markers.length; i++) {
                distance = latLng.distanceFrom(markers[i].getLatLng());
                if (closestDistance === null || distance < closestDistance) {
                    closestDistance = distance;
                    closestMarker = markers[i];
                }
            }
            map.setZoom(15);
            map.setCenter(closestMarker.latLng);
            GEvent.trigger(closestMarker, 'click');
        }
    });
}
//]]>

function testPostCode () {
  var myPostCode = document.getElementById('postcode').value;
  if (checkPostCode (myPostCode)) {
    document.getElementById('postcode').value = checkPostCode (myPostCode)
    //alert ("Postcode has a valid format")
  } 
  
}






