function ontokenexpireEvent(e) {
    // Insert code here to handle token expiration
    alert("Your map token has expired.  Please refresh the page to renew it.");
}
function ontokenerrorEvent(e) {
    // Insert code here to handle token errors
    alert("ERROR: token error");
}

function onchangeviewEvent(e) {
    // Enable OS maps?
    if (map.GetZoomLevel() >= 14 && map.GetZoomLevel() <= 15) {
        if (document.getElementById('mapZoomLevel').selectedIndex > 0) {
            updateMapStyleSelection();
            map.HideTileLayer("os25k");
        }
        enableOSTiles(true);
    } else {
        map.HideTileLayer("os25k");
        enableOSTiles(false);
        updateMapStyleSelection();
    }

    // Map has changed view (pan, zoom etc.)
    getMapAdverts(searchType);
}
function updateMapStyleSelection() {
    if (map.GetMapStyle() == VEMapStyle.Road) {
        document.getElementById('mapZoomLevel').selectedIndex = 2;
    } else {
        document.getElementById('mapZoomLevel').selectedIndex = 1;
    }
}

function onclickEvent(e) {
    inSearch = false;
    shapeZoomId = null;
    // Clicks on VEShapes...
    if (e.elementID != null) {
        // Get the shape and shapelayer.
        var shape = map.GetShapeByID(e.elementID);
        var shapeLayer = shape.GetShapeLayer();

        // Clicks on advertShapeLayer...
        if (shapeLayer.GetId() == advertsShapeLayer.GetId()) {
            // Is this an advert shape?
            if (shape.X_Id != null) {
                // Track advert click.
                trackAdvert(shape.X_Id);
            }
        }
        // Show advert (or advert-cluster) infobox.
        map.ShowInfoBox(shape, new VELatLong(shape.Latitude, shape.Longitude));

        // Click on search results...
        if (shapeLayer.GetId() == searchResultsShapeLayer.GetId()) {
            if (searchType == "search") {
                gotoLocation(shape.Longitude, shape.Latitude);
            } else {
                findRoutesByLocation(shape.Longitude, shape.Latitude);
            }
        }
    }
}
function onmouseoverEvent(e) {
    // Prevent shape infoboxes appearing onmouseover.
    //return true;

    if (e.elementID != null) {
        // Get the shape and shapelayer.
        var shape = map.GetShapeByID(e.elementID);
        var shapeLayer = shape.GetShapeLayer();

        // Clicks on advertShapeLayer...
        if (shapeLayer.GetId() == advertsShapeLayer.GetId()) {
            // Is this an advert shape?
            if (shape.X_Id != null) {
                // Track advert click.
                trackAdvert(shape.X_Id);
            }
        }
    }
}

// Functions to disable pan and zoom.
function ondoubleclickEvent() {
	return true;
}
function onmousedownEvent() {
	return true;
}
function onmousewheelEvent() {
    shapeZoomId = null;
	return true;
}
