/// <reference path="AxMap.js"/>

var startL_global;
var endL_global;

function GetRoute() {
    axRoute.GetRoute();
}

var axRoute = new Object();

/**/
axRoute.HeaderSearchRoute = function() {
    /// <summary>Get input from header Routenplaner and insert to task form by calling GetRoute()</summary>

    // Insert values from header to Task panel
    var header_from = $get("header_directions_from");
    var header_to = $get("header_directions_to");

    if (header_from.value == FROM_STRING || header_to.value == TO_STRING || header_from.value == "" || header_to.value == "") { return; }

    var task_from = $get("direction_TaskHost_Directions_from");
    var task_to = $get("direction_TaskHost_Directions_to");

    // Show panel
    axRoute.ShowDirections();

    task_from.value = header_from.value;
    task_to.value = header_to.value;

    // Execute search
    GetRoute();
}
axRoute.GetRoute = function() {
    LogDriving();
    map.DeleteRoute();
    if (shapesLayer != null) {
        shapesLayer.DeleteAllShapes();
    }
    $get("TaskHost_Directions_sBasicWrapper").style.border = "1px solid #CCCCCC";
    $get("TaskHost_Directions_eBasicWrapper").style.border = "1px solid #CCCCCC";
    $get("disambgContainer").style.display = "none";
    var a = $get("direction_TaskHost_Directions_from");
    var b = $get("direction_TaskHost_Directions_to");
    if (a.value == "" || a.value == L_Driv_Default || b.value == "" || b.value == L_Driv_Default) return;
    axSearch.ShowTaskProgress()
    axRoute.GetCoordinates();
}
axRoute.ShowDirectionsPanel = function() {
    axSearch.ShowMessage(L_Directions_Text);
    axSearch.ShowTaskHostLayer("driving");
    axSearch.ShowTaskArea();

    exlopereCollections = false;
    searchBusinesses = false;
}
axRoute.ShowDirections = function() {
    axRoute.RemoveDirections();
    axRoute.ShowDirectionsPanel();
    if ($get("TaskProgress").style.display = "block") {
        $get("TaskProgress").style.display = "none";
        $get("TaskHost").style.display = "block";
    }
}
axRoute.RemoveDirections = function() {
    map.DeleteRoute();
    shapesLayer.DeleteAllShapes();
    $get("TaskHost_Directions_sBasicWrapper").style.display = "block";
    $get("TaskHost_Directions_sNoEdit").style.display = "none";
    $get("direction_TaskHost_Directions_from").value = L_Driv_Default;
    $get("direction_TaskHost_Directions_from").className = "Watermark";
    $get("TaskHost_Directions_eBasicWrapper").style.display = "block";
    $get("TaskHost_Directions_eNoEdit").style.display = "none";
    $get("direction_TaskHost_Directions_to").value = L_Driv_Default;
    $get("direction_TaskHost_Directions_to").className = "Watermark";
    $get("disclaimerDiv").style.display = "none";
    $get("TaskHost_Directions_RouteSummary").style.display = "none";
    $get("TaskHost_Directions_directions").style.display = "none";
    $get("TaskHost_Directions_RouteGenerated").value = "false";
    $get("TaskHost_Directions_StartAddress").value = "";
    $get("TaskHost_Directions_EndAddress").value = "";
    $get("TaskHost_Directions_EndLatLong").value = "";
    $get("TaskHost_Directions_StartLatLong").value = "";
    $get("TaskHost_Directions_EndReverseGeocoded").value = "false";
    $get("TaskHost_Directions_StartReverseGeocoded").value = "false";
    temporaryStartLatLong = null;
    temporaryEndLatLong = null;
}
axRoute.GetCoordinates = function() {
    var routeDirections = ";";
    var isfirst = false;
    var issecond = false;

    // Change header view to RoutenPlaner
    axPage.ChangeTaskBarByName("Routenplaner");

    // Directions from -----------------
    var a = $get("direction_TaskHost_Directions_from");
    // Write start value to header start box
    $get("header_directions_from").value = a.value;

    var b = $get("TaskHost_Directions_StartReverseGeocoded");
    if (a.value == L_directions_from || a.value == L_directions_to || b.value == "true") {
        $get("TaskHost_Directions_DisplayText_from").innerHTML = a.value;
        var l = $get("TaskHost_Directions_StartLatLong").value;
        if (l != "") {
            var s = l.split("~");
            startL_global = new VELatLong(s[0], s[1]);
            isfirst = true;
        }
    }
    else {
        routeDirections = a.value + ";";
        axRoute.SaveHistoryToCookie(a.value);
    }
    // Direction To -----------------
    // Write start value to header start box
    var d_to = $get("direction_TaskHost_Directions_to");
    $get("header_directions_to").value = d_to.value;

    var d_to = $get("direction_TaskHost_Directions_to");
    var c = $get("TaskHost_Directions_EndReverseGeocoded");
    if (d_to.value == L_directions_from || d_to.value == L_directions_to || c.value == "true") {
        $get("TaskHost_Directions_DisplayText_to").innerHTML = d_to.value;
        var l = $get("TaskHost_Directions_EndLatLong").value;
        if (l != "") {
            var s = l.split("~");
            endL_global = new VELatLong(s[0], s[1]);
            issecond = true;
        }
    }
    else {
        routeDirections += d_to.value;
        axRoute.SaveHistoryToCookie(d_to.value);
    }
    if (isfirst && issecond) {
        axRoute.CalculateRoute(new Array(startL_global, endL_global));
    }
    else {
        CallPageMethod("GetRoutePoints", { "routeDirections": routeDirections }, axRoute.OnGetRoutePointsSucceeded);
    }
    //        axRoute.CalculateRoute(locations);
}
axRoute.OnGetRoutePointsSucceeded = function(r) {
    if (r[0] == null) {   // display that nothing was found
        $get("disambgContainer").style.display = "block";
        $get("TaskHost_Directions_sBasicWrapper").style.border = "2px solid #FF0000";
        $get("TaskProgress").style.display = "none";
        $get("TaskHost").style.display = "block";
    }
    else {
        if (r[0].Latitude != 0 && r[0].Longitude != 0) {
            startL_global = new VELatLong(r[0].Latitude, r[0].Longitude);
            $get("TaskHost_Directions_StartAddress").value = r[0].DisplayName;
            $get("TaskHost_Directions_DisplayText_from").innerHTML = r[0].DisplayName;
            $get("direction_TaskHost_Directions_from").value = r[0].DisplayName;
        }
    }
    if (r[1] == null) {   // display that nothing was found
        $get("disambgContainer").style.display = "block";
        $get("TaskHost_Directions_eBasicWrapper").style.border = "2px solid #FF0000";
        $get("TaskProgress").style.display = "none";
        $get("TaskHost").style.display = "block";
    }
    else {
        if (r[1].Latitude != 0 && r[1].Longitude != 0) {
            endL_global = new VELatLong(r[1].Latitude, r[1].Longitude);
            $get("TaskHost_Directions_EndAddress").value = r[1].DisplayName;
            $get("TaskHost_Directions_DisplayText_to").innerHTML = r[1].DisplayName;
            $get("direction_TaskHost_Directions_to").value = r[1].DisplayName;
        }
    }
    // Nothing found, do nothing further
    if (r[0] != null && r[1] != null) {
        var start = new VELatLong(startL_global.Latitude, startL_global.Longitude);
        var end = new VELatLong(endL_global.Latitude, endL_global.Longitude);
        var locations = new Array(start, end);
        axRoute.CalculateRoute(locations);
    }
}

axRoute.CalculateRoute = function DD(locations) {
    var options = new VERouteOptions;
    options.DrawRoute = true;
    options.SetBestMapView = false;
    options.RouteCallback = RouteCallbackEvent;
    options.DistanceUnit = VERouteDistanceUnit.Kilometer;
    options.ShowErrorMessages = false;
    options.ShowDisambiguation = false;

    // Root optimization - VERouteOptimize{ MinimizeTime MinimizeDistance Default }
    var s = $get("direction_routeOptions_1");
    var q = $get("direction_routeOptions_0");
    var w = $get("direction_routeOptions_2");

    if (s.checked) {
        options.RouteOptimize = VERouteOptimize.MinimizeDistance;
    }
    if (q.checked) {
        options.RouteOptimize = VERouteOptimize.MinimizeTime;
    }
    if (w.checked) {
        options.RouteMode = VERouteMode.Walking;
        options.RouteColor = new VEColor(0, 102, 41, .7);
//        options.UseMWS = false;
        options.RouteCallback = WalkingRouteCallbackEvent;
    }
    map.GetDirections(locations, options);
}
axRoute.SetLocation = function(name, id) {
    var element = $get(id);
    if (element != null) {
        element.className = "";
        element.value = name;
    }
    this.HideRecallList();
}
axRoute.ShowRecallList = function(id) {
    var r = axRoute.ReadHistoryFromCookie();
    if (r != "" && r != null && r != 'undefined') {
        var results = r.split('_');
        var showlist = false;
        var c = "";

        for (var i = 0; i < results.length; i++) {
            var r = unescape(results[i]);
            if (r != "") {
                showlist = true;
                c += "<li style='width: 254px;' onclick='axRoute.SetLocation(\"" + r + "\",\"" + id + "\")'>" + r + "</li>";
            }
        }
        if (showlist) {
            var content = "<ul class='pl_dropDown pl_dropDown_DD' style='width: 270px;'>" + c + "</ul>";
            $get("recalllist_content").innerHTML = content;
            var list = $get("recalllist");
            list.style.left = axPage.GetElementLeftPositionByID(id) + "px";
            list.style.top = axPage.GetElementTopPositionByID(id) + 25 + "px";
            $get("recalllist").style.visibility = "visible";
        }
    }
}
axRoute.HideRecallList = function() {
    var list = $get("recalllist");
    list.style.visibility = "hidden";
}
axRoute.ReadHistoryFromCookie = function() {
    var name = "VE_pst";
    try {
        if (document.cookie.length > 0) {
            var c_start = document.cookie.indexOf(name + "=");
            if (c_start != -1) {
                c_start = c_start + name.length + 1;
                var c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }
    catch (e) { return "" }
}
axRoute.SaveHistoryToCookie = function(city) {
    var content = this.ReadHistoryFromCookie();
    if (content.indexOf(city) == -1) {
        var _6 = new Date();
        _6.setDate(_6.getDate() + 21);
        var c = escape(content + city);
        document.cookie = "VE_pst=" + c + "_;expires=" + _6.toGMTString();
    }
}



function SetDrvDir(a, b) {
    try {
        var c = a % 10;
        if (c == 0) {
            c = 10;
        }
        if (b == "end") {
            axRoute.ShowDirectionsPanel();
            map.DeleteRoute();
            shapesLayer.DeleteAllShapes();

            $get("direction_TaskHost_Directions_to").value = L_directions_to;
            $get("TaskHost_Directions_EndAddress").value = L_directions_to;
            var temp = latLongList[c - 1];
            $get("TaskHost_Directions_EndLatLong").value = temp.Latitude + "~" + temp.Longitude;
            ShowEndShape(temp);
            if (temporaryStartLatLong != null) {
                ShowStartShape(temporaryStartLatLong);
            }
            temporaryEndLatLong = temp;
        }
        else {
            axRoute.ShowDirectionsPanel();
            map.DeleteRoute();
            shapesLayer.DeleteAllShapes();

            $get("direction_TaskHost_Directions_from").value = L_directions_from;
            $get("TaskHost_Directions_StartAddress").value = L_directions_from;
            var temp = latLongList[c - 1];
            $get("TaskHost_Directions_StartLatLong").value = temp.Latitude + "~" + temp.Longitude;
            ShowStartShape(temp);
            if (temporaryEndLatLong != null) {
                ShowEndShape(temporaryEndLatLong);
            }
            temporaryStartLatLong = temp;
        }
    }
    catch (e) { }
}
function SetDrvDirAdress(a, b, adress) {
    try {
        var c = a % 10;
        if (c == 0) {
            c = 10;
        }
        if (b == "end") {
            axRoute.ShowDirectionsPanel();
            map.DeleteRoute();
            shapesLayer.DeleteAllShapes();

            $get("direction_TaskHost_Directions_to").value = adress;
            $get("TaskHost_Directions_EndAddress").value = L_directions_to;
            var temp = latLongList[c - 1];
            $get("TaskHost_Directions_EndLatLong").value = temp.Latitude + "~" + temp.Longitude;
            ShowEndShape(temp);
            if (temporaryStartLatLong != null) {
                ShowStartShape(temporaryStartLatLong);
            }
            temporaryEndLatLong = temp;
        }
        else {
            axRoute.ShowDirectionsPanel();
            map.DeleteRoute();
            shapesLayer.DeleteAllShapes();

            $get("direction_TaskHost_Directions_from").value = adress;
            $get("TaskHost_Directions_StartAddress").value = L_directions_from;
            var temp = latLongList[c - 1];
            $get("TaskHost_Directions_StartLatLong").value = temp.Latitude + "~" + temp.Longitude;
            ShowStartShape(temp);
            if (temporaryEndLatLong != null) {
                ShowEndShape(temporaryEndLatLong);
            }
            temporaryStartLatLong = temp;
        }
    }
    catch (e) { }
}
function _SetDrvDir(lat, lon, b) {
    try {
        if (b == "end") {
            axRoute.ShowDirectionsPanel();
            map.DeleteRoute();
            shapesLayer.DeleteAllShapes();

            $get("direction_TaskHost_Directions_to").value = L_directions_to;
            $get("TaskHost_Directions_EndAddress").value = L_directions_to;
            var ll = new VELatLong(lat, lon);
            var temp = ll;
            $get("TaskHost_Directions_EndLatLong").value = temp.Latitude + "~" + temp.Longitude;
            ShowEndShape(temp);
            if (temporaryStartLatLong != null) {
                ShowStartShape(temporaryStartLatLong);
            }
            temporaryEndLatLong = temp;
        }
        else {
            axRoute.ShowDirectionsPanel();
            map.DeleteRoute();
            shapesLayer.DeleteAllShapes();

            $get("direction_TaskHost_Directions_from").value = L_directions_from;
            $get("TaskHost_Directions_StartAddress").value = L_directions_from;
            var ll = new VELatLong(lat, lon);
            var temp = ll;
            $get("TaskHost_Directions_StartLatLong").value = temp.Latitude + "~" + temp.Longitude;
            ShowStartShape(temp);
            if (temporaryEndLatLong != null) {
                ShowEndShape(temporaryEndLatLong);
            }
            temporaryStartLatLong = temp;
        }
    }
    catch (e) { }
}
function RouteCallbackEvent(route) {
    $get("TaskHost_Directions_RouteGenerated").value = "true";
    var polyPoints = new Array();
    $get("TaskHost_Directions_RouteDistance").innerHTML = route.Distance.toFixed(1) + " km, ";
    $get("TaskHost_Directions_RouteTime").innerHTML = GetTime(route.Time);
    $get("TaskHost_Directions_RouteSummary").style.display = "block";
    $get("TaskHost_Directions_directions").style.display = "block";
    $get("disclaimerDiv").style.display = "block";

    if (route && route.RouteLegs) {
        var turns = "<ol class='stepsList'>";

        for (i = 0; i < route.RouteLegs.length; ++i) {
            var leg = route.RouteLegs[i];
            var m = map.GetBirdseyeScene();
            if (m != null) {
                var dc = new _xy1;
                var s = dc.Decode(leg.StartLocation);
                $get("TaskHost_Directions_StartLatLong").value = s.Latitude + "~" + s.Longitude;
            }
            else {
                $get("TaskHost_Directions_StartLatLong").value = leg.StartLocation.Latitude + "~" + leg.StartLocation.Longitude;
            }

            var m = map.GetBirdseyeScene();
            if (m != null) {
                var dc = new _xy1;
                var s = dc.Decode(leg.EndLocation);
                $get("TaskHost_Directions_EndLatLong").value = s.Latitude + "~" + s.Longitude;
            }
            else {
                $get("TaskHost_Directions_EndLatLong").value = leg.EndLocation.Latitude + "~" + leg.EndLocation.Longitude;
            }
            if (leg.Itinerary && leg.Itinerary.Items) {
                for (var j = 0; j < leg.Itinerary.Items.length; j++) {
                    var item = leg.Itinerary.Items[j];

                    if (item && item.Shape) {
                        turns += "<li class='FullRoute'>";
                        turns += "<a href='#' class='bullet'>" + j + "</a><span class='instruction'>" + item.Text + "</span>";
                        if (item.Distance > 0) {
                            // Round distances to 1/10ths
                            turns += "<span class='distance'>" + item.Distance.toFixed(1) + " km</span>";
                        }
                        turns += "<span class=\"preIntersectionHint\"></span><span class=\"postIntersectionHint\"></span>";
                        turns += "</li>";

                        item.Shape.SetTitle("");
                    }
                    if (item && item.LatLong) {
                        polyPoints.push(item.LatLong);
                    }
                }
            }
        }
        turns += "<ol/>";
        try {
            var o = $get("TaskHost_Directions_directions");
            o.innerHTML = turns;
        }
        catch (e) { }

    }
    map.SetMapView(polyPoints);
    $get("TaskProgress").style.display = "none";
    $get("TaskHost").style.display = "block";

    $get("TaskHost_Directions_sBasicWrapper").style.display = "none";
    $get("TaskHost_Directions_sNoEdit").style.display = "block";
    $get("TaskHost_Directions_eBasicWrapper").style.display = "none";
    $get("TaskHost_Directions_eNoEdit").style.display = "block";
}

function WalkingRouteCallbackEvent(route) {
    $get("TaskHost_Directions_RouteGenerated").value = "true";
    var endIcon = imagesPath + "mapicon_end.gif";
    var startIcon = imagesPath + "mapicon_start.gif";

    var polyPoints = new Array();

    $get("TaskHost_Directions_RouteDistance").innerHTML = route.Distance.toFixed(1) + " km, ";
    $get("TaskHost_Directions_RouteTime").innerHTML = GetTime(route.Time);
    $get("TaskHost_Directions_RouteSummary").style.display = "block";
    $get("TaskHost_Directions_directions").style.display = "block";
    $get("disclaimerDiv").style.display = "block";

    if (route && route.RouteLegs) {
        var turns = "<ol class='stepsList'>";

        for (i = 0; i < route.RouteLegs.length; ++i) {
            var leg = route.RouteLegs[i];

            var m = map.GetBirdseyeScene();
            if (m != null) {
                var dc = new _xy1;
                var s = dc.Decode(leg.StartLocation);
                $get("TaskHost_Directions_StartLatLong").value = s.Latitude + "~" + s.Longitude;
            }
            else {
                $get("TaskHost_Directions_StartLatLong").value = leg.StartLocation.Latitude + "~" + leg.StartLocation.Longitude;
            }

            var m = map.GetBirdseyeScene();
            if (m != null) {
                var dc = new _xy1;
                var s = dc.Decode(leg.EndLocation);
                $get("TaskHost_Directions_EndLatLong").value = s.Latitude + "~" + s.Longitude;
            }
            else {
                $get("TaskHost_Directions_EndLatLong").value = leg.EndLocation.Latitude + "~" + leg.EndLocation.Longitude;
            }
            if (leg.Itinerary && leg.Itinerary.Items) {
                for (var j = 0; j < leg.Itinerary.Items.length; j++) {
                    var item = leg.Itinerary.Items[j];

                    if (item && item.Shape) {
                        turns += "<li class='FullRoute'>";
                        var text = item.Text;
                        if (text.indexOf("Turn rechts onto") > -1) {
                            text = text.replace("Turn rechts onto", "RECHTS abbiegen auf");
                        }
                        if (text.indexOf("Turn links onto") > -1) {
                            text = text.replace("Turn links onto", "LINKS abbiegen auf");
                        }
                        turns += "<a href='#' class='bullet'>" + j + "</a><span class='instruction'>" + text + "</span>";
                        if (item.Distance > 0) {
                            // Round distances to 1/10ths
                            turns += "<span class='distance'>" + item.Distance.toFixed(1) + " km</span>";
                        }
                        turns += "<span class=\"preIntersectionHint\"></span><span class=\"postIntersectionHint\"></span>";
                        turns += "</li>";

                        item.Shape.SetDescription(text);
                        item.Shape.SetTitle("");
                    }
                    if (item && item.LatLong) {
                        polyPoints.push(item.LatLong);
                    }
                }
            }
        }
        turns += "<ol/>";
        try {
            var o = $get("TaskHost_Directions_directions");
            o.innerHTML = turns;
        }
        catch (e) { }

    }
    map.SetMapView(polyPoints);
    $get("TaskProgress").style.display = "none";
    $get("TaskHost").style.display = "block";

    $get("TaskHost_Directions_sBasicWrapper").style.display = "none";
    $get("TaskHost_Directions_sNoEdit").style.display = "block";
    $get("TaskHost_Directions_eBasicWrapper").style.display = "none";
    $get("TaskHost_Directions_eNoEdit").style.display = "block";
}


function GetTime(time) {
    if (time == null) {
        return ("");
    }

    if (time > 60) {
        var minutes = time;  // minutes == 60
        minutes = minutes / 60;    // minutes == 1
        if (minutes > 60) {                                     // if minutes == 100
            var minLeft = minutes % 60;        // minLeft    == 40
            var hours = minutes - minLeft;   // hours      == 60
            hours = hours / 60;          // hours      == 1

            return (hours + " h " + Math.round(minLeft) + " min");
        }
        else {
            return (Math.round(minutes) + " min");
        }
    }
    else {
        return "< 1 min";
    }
}
function ToggleClickToEdit(o) {
    if (o == "s") {
        $get("TaskHost_Directions_sBasicWrapper").style.display = "block";
        $get("TaskHost_Directions_sNoEdit").style.display = "none";
        $get("TaskHost_Directions_StartReverseGeocoded").value = "false";
    }
    else {
        $get("TaskHost_Directions_eBasicWrapper").style.display = "block";
        $get("TaskHost_Directions_eNoEdit").style.display = "none";
        $get("TaskHost_Directions_EndReverseGeocoded").value = "false";
    }
}
function RouteEnter(e) {
    /// <summary>Handle enter key press. Call GetRoute()</summary>
    var _1b;
    if (window.event) {
        _1b = e.keyCode;
    }
    else {
        if (e.which) {
            _1b = e.which;
        }
    }
    if (_1b == 13) {
        e.returnValue = false;
        e.cancel = true;
        axRoute.GetRoute();
        return false;
    }
    else { return true; }
}
function HeaderRouteEnter(e) {
    /// <summary>handle enter key press. Call HeaderSearchRoute()</summary>
    var _1b;
    if (window.event) {
        _1b = e.keyCode;
    }
    else {
        if (e.which) {
            _1b = e.which;
        }
    }
    if (_1b == 13) {
        e.returnValue = false;
        e.cancel = true;
        axRoute.HeaderSearchRoute();
        return false;
    }
    else { return true; }
}
function ChangeDirectionsBoxes(_b) {
    if (_b.value == L_Driv_Default) {
        _b.value = "";
        _b.className = "";
    }
    axRoute.ShowRecallList(_b.id);
}
function ChangeDirectionsBoxesBack(_e) {
    if (_e.value == "") {
        _e.value = L_Driv_Default;
        _e.className = "Watermark text";
    }
    setTimeout("axRoute.HideRecallList()", 500);
}
function ContextMenuManager() {
    this.SetAction = function(a, b) {
        if (a == "center") {
            map.SetCenter(temporaryLatLong);
        }
        if (a == "zoom") {
            var s = map.GetMapStyle();
            if (s == "o") {
                map.SetMapStyle("r");
            }
            map.SetZoomLevel(b);
        }
        if (a == "driveFrom") {
            axRoute.ShowDirectionsPanel();
            map.DeleteRoute();
            shapesLayer.DeleteAllShapes();

            $get("direction_TaskHost_Directions_from").value = L_directions_from;
            $get("direction_TaskHost_Directions_from").className = "Watermark text";
            $get("TaskHost_Directions_StartAddress").value = L_directions_from;
            $get("TaskHost_Directions_StartLatLong").value = temporaryLatLong.Latitude + "~" + temporaryLatLong.Longitude;

            //
            CallPageMethod("ReverseGeocoding", { "latitude": temporaryLatLong.Latitude, "longitude": temporaryLatLong.Longitude }, driveFromSetAddress);
            //

            ShowStartShape(temporaryLatLong);
            if (temporaryEndLatLong != null) {
                ShowEndShape(temporaryEndLatLong);
            }
            temporaryStartLatLong = temporaryLatLong;
        }
        if (a == "driveTo") {
            axRoute.ShowDirectionsPanel();
            map.DeleteRoute();
            shapesLayer.DeleteAllShapes();

            $get("direction_TaskHost_Directions_to").value = L_directions_to;
            $get("direction_TaskHost_Directions_to").className = "Watermark text";
            $get("TaskHost_Directions_EndAddress").value = L_directions_to;
            $get("TaskHost_Directions_EndLatLong").value = temporaryLatLong.Latitude + "~" + temporaryLatLong.Longitude;

            //
            CallPageMethod("ReverseGeocoding", { "latitude": temporaryLatLong.Latitude, "longitude": temporaryLatLong.Longitude }, driveToSetAddress);
            //

            ShowEndShape(temporaryLatLong);
            if (temporaryStartLatLong != null) {
                ShowStartShape(temporaryStartLatLong);
            }
            temporaryEndLatLong = temporaryLatLong;
        }
        axMap.HideContextMenu();
    }
}

function driveFromSetAddress(address) {
    if (address == "" || address == null) return;
    $get("direction_TaskHost_Directions_from").value = address;
    $get("TaskHost_Directions_StartReverseGeocoded").value = "true";
}

function driveToSetAddress(address) {
    if (address == "" || address == null) return;
    $get("direction_TaskHost_Directions_to").value = address;
    $get("TaskHost_Directions_EndReverseGeocoded").value = "true";
}

function ShowStartShape(latLong) {
    var startIcon = imagesPath + "mapicon_start.gif"; ;
    var shape = new VEShape(VEShapeType.Pushpin, latLong);
    shape.SetCustomIcon(startIcon);
    shapesLayer.AddShape(shape);
}
function ShowEndShape(latLong) {
    var endIcon = imagesPath + "mapicon_end.gif";
    var shape = new VEShape(VEShapeType.Pushpin, latLong);
    shape.SetCustomIcon(endIcon);
    shapesLayer.AddShape(shape);
}
function Reverse() {
    var temp1;
    var temp2;
    temp1 = $get("direction_TaskHost_Directions_from").value;
    temp2 = $get("direction_TaskHost_Directions_to").value;
    $get("direction_TaskHost_Directions_from").value = temp2;
    $get("direction_TaskHost_Directions_to").value = temp1;
    /**/
    temp1 = $get("TaskHost_Directions_StartLatLong").value;
    temp2 = $get("TaskHost_Directions_EndLatLong").value;
    $get("TaskHost_Directions_StartLatLong").value = temp2;
    $get("TaskHost_Directions_EndLatLong").value = temp1;
    axRoute.GetRoute();
}

