﻿         var map = null;
         var index = 0;
         var results = null;
         var latCo = '';
         var longCo = '';

         function GetMap()
         {
            try
            {
                map = new VEMap('nhsmap');
                map.AttachEvent("oncredentialserror", MyHandleCredentialsError);
                //map.AttachEvent("oncredentialsvalid", MyHandleCredentialsValid);
                map.SetCredentials("AoFy7xhQqqzngfbv8HRU6640GwcKJRtkcKV1xTrlLg2_OV3Xbs6qeK2MJrHkdnre");
                var adiv = document.getElementById('form1');
                latCo = eval(document.getElementById('latitude').innerText);
                longCo = eval(document.getElementById('longitude').innerText);
                if(typeof(latCo)=='undefined' || typeof(longCo)=='undefined')
                {  
                   map.LoadMap(new VELatLong(51.5,0));//, VEMapStyle.Road, false, VEMapMode.Mode2D);                   
                   FindLoc();
                }
                else
                {
                    map.LoadMap(new VELatLong(latCo,longCo));
                    map.SetZoomLevel(16);
                    var pin = new VEShape(VEShapeType.Pushpin, map.GetCenter());
                    pin.SetCustomIcon("<img src=\"/live/images/map_pin.gif\" />");
                    map.AddShape(pin);    
                }
            }
            catch(e)
            {
                alert("GetMap - " + e.message);
            }
         }
         function MyHandleCredentialsError()
         {
            alert("The credentials are invalid.");
         }

         function MyHandleCredentialsValid()
         {
            alert("The credentials are valid.");
          
         }

         function FindLoc()
         {
            try
            {
               results = map.Find(null,
                                  document.getElementById('postcode').innerText,
                                  null,
                                  null,
                                  0,
                                  1,
                                  true,
                                  true,
                                  false,
                                  true,
                                  AddPushpin);
            }
            catch(e)
            {
               //alert("FindLoc - " + e.message);
            }
         }
         //***************************************************
         function AddPushpin(layer, resultsArray, places, hasMore, veErrorMessage)
         {     
            try
            { 
                map.SetZoomLevel(16);
                var pin = new VEShape(VEShapeType.Pushpin,places[0].LatLong);//map.GetCenter());
                pin.SetCustomIcon("<img src=\"/live/images/map_pin.gif\" />");
                map.AddShape(pin);    
            }
            catch(e)
            {
                //alert("AddPushpin - " + e.message);
            }
         }
         



