﻿var map = null;
var SearchFromPoint = "52.81, -1.38";
var directions = new Array();
directions[0] = "London";


// Display the map control   
function GetMap()
{
    map = new VEMap('mapDiv');
    //map.SetDashboardSize(VEDashboardSize.Tiny);
    map.LoadMap(new VELatLong(52.81, -1.38), 6 ,'r' ,false);
    ShowAllDealers();
}


function ShowAllDealers()
{
 try
 {
  map.DeleteAllShapes();
 
 
  // Import GeoRSS feed of store data, and call onAllStoreLoad function when the data is loaded
  var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS,"./DealersGeoRSS.aspx");
  map.ImportShapeLayerData(veLayerSpec, null, true);
 }
 catch(e)
 {
    //document.getElementById("Info").innerHTML = e.Message;
    alert("error");
 }
}

function GetDirection(latlong)
{
    try
    {   
        map.DeleteRoute();  
    }            
    catch (e)
    {
    };
    directions[1] = latlong;
    var options = new VERouteOptions();            
    options.RouteCallback = onGotRoute;    
    map.GetDirections(directions, options);
    return false;
}

function onGotRoute(route)
{           
// Unroll route           
var legs     = route.RouteLegs;           
var turns    = "<h4>Traffic Route</h4><p>Total distance: <strong>" + route.Distance.toFixed(1) + " mi</strong></p>";           
var numTurns = 0;           
var leg      = null;           
// Get intermediate legs            
turns += "<ol>";
for(var i = 0; i < legs.length; i++)            {               
// Get this leg so we don't have to derefernce multiple times               
leg = legs[i];  // Leg is a VERouteLeg object                                 
// Unroll each intermediate leg               
var turn = null;  // The itinerary leg                                 
for(var j = 0; j < leg.Itinerary.Items.length; j ++)               {                  
turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object                  
numTurns++;                  
turns += "<li>" + turn.Text + " (" + turn.Distance.toFixed(1) + " mi)</li>";                                    
//Display any traffic warnings                  
 }            
 }            
 turns += "</ol>";
    //alert(turns);         
    getElementById_s("directions_info").innerHTML = turns;
 
 }

function ShowNearbyStores()
{
    try
    {   
        map.DeleteRoute();  
    }            
    catch (e)
    {
    };
 try
 {
  // Use the VEMap.Find method to find the location entered by the user and call the onFind function to process the results

     results = map.Find(null, getElementById_s("location").value.replace("UK", "").replace("United Kingdom", "").replace("England", "") + ' UK',
                      null, null,null, 1, false, false, false, true,
                      onFind);
  
 }
 catch(e) {
     
     
 
    alert("Please enter a valid location!");
 }      
 return false;    
}

function onFind(layer, resultsArray, places, hasMore, weErrorMessage)
{
 try
 {
   // verify the search location was found
   if(places == null) {
          
       alert("Search location not found");   
    
   }
   else
   {
    // define the search point as the latitude and longitude of the first place in the results
    SearchFromPoint = places[0].LatLong;    
    directions[0] = SearchFromPoint;
    map.SetZoomLevel(9);
    
   }
  }
  catch(e)
  {
     alert(e.Message);
  }
}

// getElementById Special to handle quirky browsers
// most will use getElementById()
function getElementById_s(id){
var obj = null;
if(document.getElementById){
/* Prefer the widely supported W3C DOM method, if
available:-
*/
obj = document.getElementById(id);
}else if(document.all){
/* Branch to use document.all on document.all only
browsers. Requires that IDs are unique to the page
and do not coincide with NAME attributes on other
elements:-
*/
obj = document.all[id];
}
/* If no appropriate element retrieval mechanism exists on
this browser this function always returns null:-
*/
return obj;
}