    var map = null;
    var mapCenter = new VELatLong(53.808760650677456, -1.510620117187513);
    var mapZoom = 8;
    var shape = null;
    var layer = null;
    var shapeID = null;
    var aLongLats = new Array();
    var p = 0;
    var isAdmin = false;
    var pinid = 0;
    var icon = "";
    var infobox = "";
    var BaseShapeLayer;
    
    addLoadEvent(loadMap);
    
    function loadMap()
    {
        map = new VEMap('Map');
        map.LoadMap(mapCenter, mapZoom);
        BaseShapeLayer = new VEShapeLayer();
        BaseShapeLayer.SetClusteringConfiguration(VEClusteringType.Grid);
        map.AddShapeLayer(BaseShapeLayer);
/*
POSTCODE STUFF
        results = map.Find('',
                                  'ls22 6le',
                                  null,
                                  null,
                                  null,
                                  null,
                                  true,
                                  true,
                                  true,
                                  true,
                                  AddPostcodePin);

    }

         function AddPostcodePin(layer, resultsArray, places, hasMore, veErrorMessage)
         {
               var pin = new VEShape(VEShapeType.Pushpin, places[0].LatLong);
                 
                map.SetCenter(places[0].LatLong);
                map.SetZoomLevel(18);
                map.AddShape(pin);
*/
         }


/* BEGIN - CALLED DIRECTLY FROM MAP APP */

    function AddNewPin(sPoint, sLineColor, sLineWidth, sFillColour, sIcon, sTitle, sDescription, sPhoto)
    {
        if(sPoint != null)
        {
            var coordinates = sPoint.split(", ");
            var tmpLat = parseFloat(coordinates[0]);
            var tmpLong = parseFloat(coordinates[1]);
            var LL = new VELatLong(tmpLat,tmpLong);
            var pin = new VEShape(VEShapeType.Pushpin, LL);
            pinid++;
        

            var icon = "<div style='color:#000000; font-size:85%;'>"+sTitle+"</div>";
            if(sPhoto != null && sPhoto != "")
            {
                sPhoto = '<img src="'+sPhoto+'" class="mapThumbnail"/>';
            }
            var infobox = "<div style='width:309px;'>"+sPhoto+sDescription+"</div>";

            //Set the icon
            
            //pin.SetCustomIcon('<img src="/SiteImages/Diamond.gif" />');
            pin.SetCustomIcon(sIcon);

            //Set the info box
            map.ClearInfoBoxStyles();
            pin.SetTitle("<h3>"+sTitle+"</h3>");
            pin.SetDescription('<div class="mapInfoBox">'+infobox+'<div class="clear">&nbsp;</div></div>');
            
            try
            {
                BaseShapeLayer.AddShape(pin);
            }
            catch(ex)
            {
                //alert(ex.toString());
            }
        }
    }

    function AddCurrentRoute(sPoints)
    {
        if(sPoints != null && sPoints != '')
        {
            var aPoints = GetPointsArrayFromString(sPoints);
        
            if(aPoints.length >= 2)
            {
                var shape = new VEShape(VEShapeType.Polyline, aPoints);
                var lineWidth = Math.round(2);
                shape.SetLineWidth(lineWidth);
                shape.SetCustomIcon("<img src=\"/SiteImages/spacer.gif\" />");
                map.ClearInfoBoxStyles();
                
                try
                {
                    BaseShapeLayer.AddShape(shape);
                }
                catch(ex)
                {
                    //alert(ex.toString());
                }
            }
        }
    }


    function AddCurrentArea(sPoints)
    {
        if(sPoints != null && sPoints != '')
        {
            var aPoints = GetPointsArrayFromString(sPoints);
        
            if(aPoints.length >= 3)
            {
                var shape = new VEShape(VEShapeType.Polygon, aPoints);
                //var lineColor = new VEColor(Math.round(254*Math.random()),Math.round(254*Math.random()),Math.round(254*Math.random()),Math.random());
                //shape.SetLineColor(lineColor);

                //Set the line color
                //var lineColor = new VEColor(Math.round(254*Math.random()),Math.round(254*Math.random()),Math.round(254*Math.random()),Math.random());
                //shape.SetLineColor(lineColor);

                //Set the line width
                var lineWidth = Math.round(2);
                shape.SetLineWidth(lineWidth);
                shape.SetCustomIcon("<img src=\"/SiteImages/spacer.gif\" />");
                map.ClearInfoBoxStyles();

                try
                {
                    BaseShapeLayer.AddShape(shape);
                }
                catch(ex)
                {
                    //alert(ex.toString());
                }
            }
        }
    }

    function AddNewArea(sPoints, sLineColor, sLineWidth, sFillColour, sIcon, sTitle, sDescription, sPhoto)
    {
        var aPoints = GetPointsArrayFromString(sPoints);
    
        if(aPoints.length >= 3)
        {
            var shape = new VEShape(VEShapeType.Polygon, aPoints);

            if(sLineColor != null && sLineColor != "")
            {
                var lineColor = new VEColor(sLineColor);
                shape.SetLineColor(lineColor);
            }
            if(sLineWidth != null && sLineWidth != "")
            {
                var lineWidth = Math.round(sLineWidth1);
                shape.SetLineWidth(lineWidth);
            }
            if(sFillColour != null && sFillColour != "")
            {
                var fillColor = new VEColor(sFillColour);
                shape.SetFillColor(fillColor);
            }
            
            //var icon = '<img src="/SiteImages/Diamond.gif" />';
            var icon = sIcon;
            
            if(sPhoto != null && sPhoto != "")
            {
                sPhoto = '<img src="'+sPhoto+'" class="mapThumbnail"/>';
            }
            var infobox = '<div style="width:309px;">'+sPhoto+sDescription+'</div>';

            map.ClearInfoBoxStyles();
            shape.SetCustomIcon(icon);
            shape.SetTitle("<h3>"+sTitle+"</h3>");
            shape.SetDescription('<div class="mapInfoBox">'+infobox+'<div class="clear">&nbsp;</div></div>');

            try
            {
                BaseShapeLayer.AddShape(shape);
            }
            catch(ex)
            {
                //alert(ex.toString());
            }
        }
    }
    
    function AddNewRoute(sPoints, sLineColor, sLineWidth, sFillColour, sIcon, sTitle, sDescription, sPhoto)
    {
        var aPoints = GetPointsArrayFromString(sPoints);
    
        if(aPoints.length >= 3)
        {
            var shape = new VEShape(VEShapeType.Polyline, aPoints);

            if(sLineColor != null && sLineColor != "")
            {
                var lineColor = new VEColor(sLineColor);
                shape.SetLineColor(lineColor);
            }
            if(sLineWidth != null && sLineWidth != "")
            {
                var lineWidth = Math.round(sLineWidth1);
                shape.SetLineWidth(lineWidth);
            }
            if(sFillColour != null && sFillColour != "")
            {
                var fillColor = new VEColor(sFillColour);
                shape.SetFillColor(fillColor);
            }
            
            //<div class="mapIconTitle">'+sIcon+'</div>';
            //var icon = '<img src="/SiteImages/Diamond.gif" />';
            var icon = sIcon;
            
            if(sPhoto != null && sPhoto != "")
            {
                sPhoto = '<img src="'+sPhoto+'" class="mapThumbnail"/>';
            }
            var infobox = '<div style="width:309px;">'+sPhoto+sDescription+'</div>';

            map.ClearInfoBoxStyles();
            shape.SetCustomIcon(icon);
            shape.SetTitle("<h3>"+sTitle+"</h3>");
            shape.SetDescription('<div class="mapInfoBox">'+infobox+'<div class="clear">&nbsp;</div></div>');

            try
            {
                BaseShapeLayer.AddShape(shape);
            }
            catch(ex)
            {
                //alert(ex.toString());
            }
        }
    }

/* END - CALLED DIRECTLY FROM MAP APP */

/* BEGIN - UTILS */
    function addLoadEvent(func) 
    {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') 
      {
        window.onload = func;
      } 
      else
      {
        window.onload = function()
        {
          if (oldonload) 
          {
            oldonload();
          }
          func();
        }
      }
    }

    function GetPointsArrayFromString(stringPoints)
    {
        var coordinates = stringPoints.split("|");
        var aLongLats = new Array();

        if(coordinates.length > 3)
        {
            var p = 0;
            for(var i = 0; i < coordinates.length; i ++)
            {
                if(trim(coordinates[i]) != "")
                {
                    var tmpArray = coordinates[i].split(", ");
                    var tmpLat = parseFloat(tmpArray[0]);
                    var tmpLong = parseFloat(tmpArray[1]);
                    aLongLats[p] = new VELatLong(tmpLat,tmpLong);
                    p ++;
                } 
            }
            return aLongLats;
        }
        else
        {
            return false;
        }      
    }

    function trim(str, chars) {
        return ltrim(rtrim(str, chars), chars);
    }

    function ltrim(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }

    function rtrim(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
    }
/* END - UTILS */

/* BEGIN - USED IN THE ADMIN MAP HELPER PAGE */
    function EnableAdminControls()
    {
        map.AttachEvent("onclick", admin_OnClick);
        isAdmin = true;
    }

    function admin_OnClick(e)
    {
        // add point to polygon
        if(e.rightMouseButton)
        {
            var x = e.mapX;
            var y = e.mapY;
            pixel = new VEPixel(x, y);
            LL = map.PixelToLatLong(pixel);
            if(isAdmin)
            {
                document.getElementById('locationArea').value += '|'+LL;
            }
            AddPointToArea(LL)
        }
        // add individual point
        else if(e.ctrlKey)
        {
            var x = e.mapX;
            var y = e.mapY;
            pixel = new VEPixel(x, y);
            LL = map.PixelToLatLong(pixel);
            if(isAdmin)
            {
                document.getElementById('locationPoint').value = LL;
            }
            addPin(LL);
         }
        else if(e.altKey)
        {
            var x = e.mapX;
            var y = e.mapY;
            pixel = new VEPixel(x, y);
            LL = map.PixelToLatLong(pixel);
            if(isAdmin)
            {
                document.getElementById('locationArea').value += '|'+LL;
            }
            AddPointToRoute(LL);
         }
    }
    
    function addPin(LL)
    {
        pin = new VEShape(VEShapeType.Pushpin, LL);
        pin.SetTitle('My pushpin');
        pin.SetDescription('This is a pushpin.');
        layer.AddShape(pin);
        pinid++;
    }

    function AddPointToArea(LL)
    {
        aLongLats[p] = LL;
        p ++;
        if(isAdmin)
        {
            RenderArea();
        }
    }

    function AddPointToRoute(LL)
    {
        aLongLats[p] = LL;
        p ++;
        if(isAdmin)
        {
            RenderRoute();
        }
    }

    function GetMapCenter()
    {
        var center = map.GetCenter();
        if(isAdmin)
        {
            document.getElementById('mapCenter').value = center;
        }
        return center;
    }

    function GetMapZoom()
    {
        var zoom = map.GetZoomLevel();
        if(isAdmin)
        {
            document.getElementById('mapZoom').value = zoom;
        }
        return zoom;
    }
    
    function resetArea()
    {
        aLongLats = new Array();
    }
    
    function RenderArea(LLArray)
    {
        if(p >= 3)
        {
            LLArray = (LLArray != null && LLArray != undefined ? LLArray : aLongLats);
            
            var shape = new VEShape(VEShapeType.Polygon, LLArray);

            //Set the line color
            var lineColor = new VEColor(100,100,100,1);
            shape.SetLineColor(lineColor);

            //Set the line width
            shape.SetLineWidth(2);

            //Set the fill color
            var fillColor = new VEColor(0,150,100,0.5);
            shape.SetFillColor(fillColor);

            //Set the icon
            shape.SetCustomIcon("");

            //Set the info box
            map.ClearInfoBoxStyles();
            shape.SetTitle("<h2>Custom Pin</h2>");
            shape.SetDescription(infobox);

            //Add the shape the the map
            if(shapeID != null)
            {
                var tmpShape = map.GetShapeByID(shapeID);
                map.DeleteShape(tmpShape);
            }
            try
            {
                map.AddShape(shape);
            }
            catch(ex)
            {
                //alert(ex.toString());
            }
            shapeID = shape.GetID();

            pinid++;
        }
    }

    function RenderRoute(LLArray)
    {
        if(p >= 2)
        {
            LLArray = (LLArray != null && LLArray != undefined ? LLArray : aLongLats);
            
            var shape = new VEShape(VEShapeType.Polyline, LLArray);

            //Set the line color
            var lineColor = new VEColor(100,100,100,1);
            shape.SetLineColor(lineColor);

            //Set the line width
            shape.SetLineWidth(2);

            //Set the fill color
            var fillColor = new VEColor(0,150,100,0.5);
            shape.SetFillColor(fillColor);

            //Set the icon
            shape.SetCustomIcon("");

            //Set the info box
            map.ClearInfoBoxStyles();
            shape.SetTitle("<h2>Custom Pin</h2>");
            shape.SetDescription(infobox);

            //Add the shape the the map
            if(shapeID != null)
            {
                var tmpShape = map.GetShapeByID(shapeID);
                map.DeleteShape(tmpShape);
            }
            try
            {
                map.AddShape(shape);
            }
            catch(ex)
            {
                //alert(ex.toString());
            }
            shapeID = shape.GetID();

            pinid++;
        }
    }
    
    function BuildAreaFromString(polygonPoints)
    {
        var coordValue = (polygonPoints == null || polygonPoints == undefined ? document.getElementById('locationArea').value : polygonPoints);
        var coordinates = coordValue.split("|");
        var aLongLats = new Array();

        if(coordinates.length > 3)
        {
            p = 0;
            for(var i = 0; i < coordinates.length; i ++)
            {
                if(trim(coordinates[i]) != "")
                {
                    var tmpArray = coordinates[i].split(", ");
                    var tmpLat = parseFloat(tmpArray[0]);
                    var tmpLong = parseFloat(tmpArray[1]);
                    aLongLats[p] = new VELatLong(tmpLat,tmpLong);
                    p ++;
                } 
            }
            RenderArea(aLongLats);              
        }
        else
        {
            alert('An area requires at least 3 points');
        }       
}