﻿var map = null;
var IsMapSearchActive = true;
var AustraliaZoomLevel = 4; //All of Australia
var StreetZoomLevel = 14; //Distance bar is set to 1km
var ZoomInOutStep = 2; //Zoom (in/out) levels to step through
var MAXIMUM_FEATURES_RADIAL_SEARCH_DISTANCE = 250;  //km
var MAXIMUM_SEARCH_FEATURES_DISPLAY_COUNT = 25; 
var MAXIMUM_ROUTE_FEATURES_DISPLAY_COUNT = 40; //Total features for the whole route.
var MAXIMUM_ROUTE_FEATURES_PER_POINT_DISPLAY_COUNT = 10;//20090520 George Petrakis Not currently used.

var SydneyCentre;
var AustraliaCentre;
var centreMarker;
var directionsCentreMarker;

var lastSuccessfulSearch = null;

var routeWSPoints = null;//MapDS.LatLong()
var routePoints = null; //List(of MapDS.Point)

var lastSuccessfulRouteFrom = null;
var lastSuccessfulRouteTo = null;

var lastSuccessfulRouteSettingsValues = null; //Instance of RouteSettingsValues

var lastSuccessfulRouteWaypoint1 = null;
var lastSuccessfulRouteWaypoint2 = null;
var lastSuccessfulRouteWaypoint3 = null;
var lastSuccessfulRouteWaypoint4 = null;
var lastSuccessfulRouteWaypoint5 = null;

var HAS_FEATURES_ALONG_ROUTE = false;

var NO_FIND_LOCATION_ENTRY = 'Please enter/select a suburb location from the available suggestions.';
var NO_START_ROUTE_ENTRY = 'Please enter/select a route start point from the available suggestions.';
var NO_END_ROUTE_ENTRY = 'Please enter/select a route end point from the available suggestions.';
var NO_START_AND_END_ROUTE_ENTRY = 'Please select route start and end points from the available suggestions.';
var NO_VALID_WAYPOINT_ENTRY = 'Please enter/select a waypoint from the available suggestions.';

var MAXIMUM_FEATURES_EXCEEDED_FOR_SELECTION = 'Too many features were found for your selection.<br/>Please refine your selection.';
var NO_FEATURES_FOUND_FOR_SELECTION = 'No features were found for your selection.';

var NO_ROUTE_GENERATED = 'Could not generate a route for your selection.<br/>Please refine your criteria.';

//Use to hold info related to the last successful searches/routes, by holding the text associated to an ID
//returned from the autocomplete function.
function KeyValuePair(key, textValue, featureOptionValue) {
    var _key = key || '';
    var _textValue = textValue || '';
    var _featureOptionValue = featureOptionValue || '';
        
    //Readonly
    this.key = function() { return _key; };

    //Readonly
    this.textValue = function() { return _textValue; };
    
    //Readonly
    this.featureOptionValue = function() { return _featureOptionValue; };
    
    //this.getInfo = function() {
    //    return this.color + ' ' + this.type + ' apple';
    //};
}

//Use to hold info related to the last successful route options specified
function RouteSettingsValues(buildType, avoidTunnels, avoidFerries, avoidTollways, avoidUnsealedRoads ) {
    var _buildType = buildType || '';
    var _avoidTunnels = avoidTunnels || '';
    var _avoidFerries = avoidFerries || '';
    var _avoidTollways = avoidTollways || '';
    var _avoidUnsealedRoads = avoidUnsealedRoads || '';
        
    //Readonly
    this.buildType = function() { return _buildType; };

    //Readonly
    this.avoidTunnels = function() { return _avoidTunnels; };
    
    //Readonly
    this.avoidFerries = function() { return _avoidFerries; };

    //Readonly
    this.avoidTollways = function() { return _avoidTollways; };
    
    //Readonly
    this.avoidUnsealedRoads = function() { return _avoidUnsealedRoads; };
        
    //this.getInfo = function() {
    //    return this.color + ' ' + this.type + ' apple';
    //};
}

function init(){
    //    var lat = -37.665;
    //    var lng = 145.157;
    //    var zoom = 12;

    SydneyCentre = new MapDS.LatLng( -33.866774, 151.207108 );
    AustraliaCentre = new MapDS.LatLng(-28.5, 133.5); //Centre coordinates for Australia

    try {
        map = new MapDS.Map( 'myMap', { navigation: MapDS.MapOptions.Navigation.PanZoomBar } );

        //Add a marker  
        //map.addMarker( new MapDS.Marker( new MapDS.LatLng( -33.866774, 151.207108 ) ) );  
        map.setCentre( AustraliaCentre, AustraliaZoomLevel);
    } catch(ex) {
        alert(ex);
    }
}

//Clears map of any markers or routes, plus resets all searches and routes, to be able to render them again if requested.
function clearMap(){
    lastSuccessfulSearch = null;

    lastSuccessfulRouteFrom = null;
    lastSuccessfulRouteTo = null;

    lastSuccessfulRouteWaypoint1 = null;
    lastSuccessfulRouteWaypoint2 = null;
    lastSuccessfulRouteWaypoint3 = null;
    lastSuccessfulRouteWaypoint4 = null;
    lastSuccessfulRouteWaypoint5 = null;

    if(null != map){
        map.clearMarkers();
        map.clearRoutes();
    
        map.setCentre( AustraliaCentre, AustraliaZoomLevel);
    }
    return true;
}

function clearSearch(){
    if(consoleLog == true){ console.log("clearSearch - Start.");}
    //clearRouteData();
    
    clearSearchText();
    clearSearchData();

    $get(ddlFeatureOptionsClientID).selectedIndex = 0;
    
    //Must clear map also
    clearMap();

    return true;
}

function clickClearRoute(){
    if(consoleLog == true){ console.log("clickClearRoute - Start.");}
    //clearSearchData();

    clearRouteText();
    clearRouteData();

    $get(radioRouteOptionTypeQuickestClientID).checked = true;
    $get(chkRouteOptionAvoidTunnelsClientID).checked = false;
    $get(chkRouteOptionAvoidFerriesClientID).checked = false;
    $get(chkRouteOptionAvoidTollwaysClientID).checked = false;
    $get(chkRouteOptionAvoidUnsealedRoadsClientID).checked = false;
                    
    $get(ddlFeatureOptionsClientID).selectedIndex = 0;
    
    //Must clear map also
    clearMap();

    return true;
}

function setErrorMessage(message){
    $get('lblErrorMessage').innerHTML = message;
    if('' != message){
        //20090428 George Petrakis 40px gives us two lines of text. Auto height is bad because with one line of text, the div expands, but chops
        //the background image in half as it is 32px high.
        expandPanel(collapsiblePanelExtenderErrorMessageID);
        //$get('divErrorMessage').style.height = '40px'; 
    }else{
        collapsePanel(collapsiblePanelExtenderErrorMessageID);
        //$get('divErrorMessage').style.height = '0px'; 
    }
    return true;
}

function clearSearchData()
{
    //Clear fields of values.
    if(consoleLog == true){ console.log("clearSearchData - Entered.");}
    lastSuccessfulSearch = null;
    
    setErrorMessage('');
      
    $get('hSearchID').value = '';
    setSearchXY('','');
    return true;
}

function clearRouteData()
{
    //Clear fields of values.
    if(consoleLog == true){ console.log("clearRouteData - Entered.");}
    lastSuccessfulRouteFrom = null;
    lastSuccessfulRouteTo = null;
    
    lastSuccessfulRouteWaypoint1 = null;
    lastSuccessfulRouteWaypoint2 = null;
    lastSuccessfulRouteWaypoint3 = null;
    lastSuccessfulRouteWaypoint4 = null;
    lastSuccessfulRouteWaypoint5 = null;
    
    setErrorMessage('');
        
    $get('hRouteFromID').value = '';
    $get('hRouteToID').value = '';
    $get('hRouteWaypoint1ID').value = '';
    $get('hRouteWaypoint2ID').value = '';
    $get('hRouteWaypoint3ID').value = '';
    $get('hRouteWaypoint4ID').value = '';
    $get('hRouteWaypoint5ID').value = '';
    
    setRouteFromToXY("","","","","");

    $get('hRouteWaypoint1X').value = '';
    $get('hRouteWaypoint1Y').value = '';
    $get('hRouteWaypoint2X').value = '';
    $get('hRouteWaypoint2Y').value = '';
    $get('hRouteWaypoint3X').value = '';
    $get('hRouteWaypoint3Y').value = '';
    $get('hRouteWaypoint4X').value = '';
    $get('hRouteWaypoint4Y').value = '';
    $get('hRouteWaypoint5X').value = '';
    $get('hRouteWaypoint5Y').value = '';
    return true;
}

function getSearchKey(source, eventArgs )
{
    if(consoleLog == true){ console.log("getSearchKey - Fired.");}
    
    //lastSuccessfulSearch = new KeyValuePair(eventArgs.get_value(), $get('textSearch').value);
    $get('hSearchID').value = eventArgs.get_value();}

function getRouteFromKey(source, eventArgs )
{    $get('hRouteFromID').value = eventArgs.get_value();}

function getRouteToKey(source, eventArgs )
{    $get('hRouteToID').value = eventArgs.get_value();}

function getRouteWaypoint1Key(source, eventArgs )
{    $get('hRouteWaypoint1ID').value = eventArgs.get_value();
    lastSuccessfulRouteFrom = null; lastSuccessfulRouteTo = null;
}
function getRouteWaypoint2Key(source, eventArgs )
{    $get('hRouteWaypoint2ID').value = eventArgs.get_value();
    lastSuccessfulRouteFrom = null; lastSuccessfulRouteTo = null;
}
function getRouteWaypoint3Key(source, eventArgs )
{    $get('hRouteWaypoint3ID').value = eventArgs.get_value();
    lastSuccessfulRouteFrom = null; lastSuccessfulRouteTo = null;
}
function getRouteWaypoint4Key(source, eventArgs )
{    $get('hRouteWaypoint4ID').value = eventArgs.get_value();
    lastSuccessfulRouteFrom = null; lastSuccessfulRouteTo = null;
}
function getRouteWaypoint5Key(source, eventArgs )
{    $get('hRouteWaypoint5ID').value = eventArgs.get_value();
    lastSuccessfulRouteFrom = null; lastSuccessfulRouteTo = null;
}
//Finds a location by either grabbing the associated ID if the entry is selected from the 
//the drop-down list, or by attempting to geocode the entered text.
//isViaClick is boolean, true represents a call from a button click event, false represents a call from elsewhere, e.g. firing of a drop-down list selection change. 
function generateSearch(isViaClick){
    if(consoleLog == true){ console.log("generateSearch - Start.");}

    var currentSearchText;
    if (isViaClick){
        //As it was fired by a click, we get the currentSearchText value directly from what the user has entered in the textbox.
        if(consoleLog == true){ console.log("generateSearch - isViaClick: true.");}
        currentSearchText = $get(textSearchClientID).value;
    }else{
        //As it was not fired by a click, we get the currentSearchText value from the last successful search, as the user might 
        //have changed the entry in the textbox, and then selected a tab or drop-down list, which can fire an event.
        if (null != lastSuccessfulSearch){
            if(consoleLog == true){ console.log("generateSearch - isViaClick: false, lastSuccessfulSearch is NOT null.");}
            currentSearchText = lastSuccessfulSearch.textValue();
        }else{
            if(consoleLog == true){ console.log("generateSearch - isViaClick: false, lastSuccessfulSearch is null.");}
            currentSearchText = $get(textSearchClientID).value;
        }
    }  

    var locationID  = $get('hSearchID').value
    
    if(consoleLog == true){ console.log("generateSearch - hSearchID: '" + locationID + "'");}    
    if(consoleLog == true){ console.log("generateSearch - lastSuccessfulSearch: '" + ((lastSuccessfulSearch == null) ? 'null' : lastSuccessfulSearch.textValue()) + "'");}
    if(consoleLog == true){ console.log("generateSearch - currentSearchText: '" + currentSearchText + "'");}
    
    if(lastSuccessfulSearch == null) {lastSuccessfulSearch = new KeyValuePair();}
    
    if (!isViaClick || null == lastSuccessfulSearch || lastSuccessfulSearch.key() == '' || lastSuccessfulSearch.key() != locationID){
        //Either an initial search, or a search has been reset by setting locationID to null.
        if(consoleLog == true){ console.log("generateSearch - Initial search.");}

        clearMap();
    
        if(locationID != ''){
            var locationIDs = new Array();
            locationIDs.push(locationID);
               
            clearRouteData();
            //if(consoleLog == true){ console.log("generateSearch - Calling SwaggieWS.GetPoints.");}
            //SwaggieWS.GetPoints(locationIDs, getSearchResultsCallback, getSearchNoResultsCallback);
            if(consoleLog == true){ console.log("generateSearch - Calling SwaggieWS.GetSuburbPoints.");}
            
            var x = $get(ddlFeatureOptionsClientID);
            var dataSourceSettings = x.options[x.selectedIndex].value;
            lastSuccessfulSearch = new KeyValuePair($get('hSearchID').value, currentSearchText, dataSourceSettings);
            //setWaitCursor(true);
            SwaggieWS.GetSuburbPoints(locationIDs, getSearchResultsCallback, getSearchNoResultsCallback);
        }else{
            //Needed for when we have 'reset' a search by setting locationID to null, therefore need to reselect from the drop list.
            if(consoleLog == true){ console.log("generateSearch - No Location IDs are defined.");}
            $get('hSearchID').value = '';
            setSearchXY('','');

            lastSuccessfulSearch = null;
            setErrorMessage(NO_FIND_LOCATION_ENTRY);
        }
    }else if(lastSuccessfulSearch.textValue() != currentSearchText ){//|| lastSuccessfulSearch.featureOptionValue() != ){
        //There is a difference between the current entered text, and the last previously SUCCESSFUL search.
        if(consoleLog == true){ console.log("generateSearch - hSearchID is null.");}

        clearMap();
    
        $get('hSearchID').value = '';
        setSearchXY('','');

        lastSuccessfulSearch = null;
        setErrorMessage(NO_FIND_LOCATION_ENTRY);
    }else{
        if(consoleLog == true){ console.log("generateSearch - No changes from previous requested search.");}
        return true;
    }
    return true;
}

/* Unfortunately this function has proven extremely difficult to implement due to the input validation needed.
Validation rules are broken down into a :
1) Check to see if either an initial route, or a route has been reset by setting last successful route values to null.
2) Check to see we have initially defined only a (from) route point, AND there is a difference to a previous (to) route point.
3) Check to see we have initially defined only a (to) route point, AND there is a difference to a previous (from) route point.

4) Check to see if we have initially defined only a (from) route point.
5) Check to see if we have initially defined only a (to) route point.
6) Check to see if a route has changed because of different current and previous keys.
7) Check to see if there is a difference to previous (from & to) route points.
8) Check to see if there is a difference to a previous (from) route point.
9) Check to see if there is a difference to a previous (to) route point.
    Note: 20090223 George Petrakis - Have commented out steps 3 & 4 as they were never executed during console debugging. 
    No problems seem to have occurred by doing this.
    Note: 20090224 George Petrakis - Have added logic for waypoints. Currently for waypoints, validation occurs, but no 
    messages are displayed to the end-user, if there is a failure. Therefore only when 
    isViaClick is boolean, true represents a call from a button click event, false represents a call from elsewhere, e.g. firing of a drop-down list selection change. 
*/
function generateRoute(isViaClick){
    if(consoleLog == true){ console.log("generateRoute - Start.");}
    
    $get('hRouteWaypoint1X').value = '';
    $get('hRouteWaypoint1Y').value = '';
    $get('hRouteWaypoint2X').value = '';
    $get('hRouteWaypoint2Y').value = '';
    $get('hRouteWaypoint3X').value = '';
    $get('hRouteWaypoint3Y').value = '';
    $get('hRouteWaypoint4X').value = '';
    $get('hRouteWaypoint4Y').value = '';
    $get('hRouteWaypoint5X').value = '';
    $get('hRouteWaypoint5Y').value = '';
    
    var currentRouteFromText;
    var currentRouteToText;
    if (isViaClick){
        //As it was fired by a click, we get the currentRouteFromText/currentRouteToText values directly from what the user has entered in the textboxes.
        if(consoleLog == true){ console.log("generateRoute - isViaClick: true.");}
        currentRouteFromText = $get(textRouteFromClientID).value;
        currentRouteToText = $get(textRouteToClientID).value;
    }else{
        //As it was not fired by a click, we get the currentRouteFromText/currentRouteToText values from the last successful route, as the user might 
        //have changed the entry in the textboxes, and then selected a tab or drop-down list, which can fire an event.
        if (null != lastSuccessfulRouteFrom){
            if(consoleLog == true){ console.log("generateRoute - isViaClick: false, lastSuccessfulRouteFrom is NOT null.");}
            currentRouteFromText = lastSuccessfulRouteFrom.textValue();
        }else{
            if(consoleLog == true){ console.log("generateRoute - isViaClick: false, lastSuccessfulRouteFrom is null.");}
            currentRouteFromText = $get(textRouteFromClientID).value;
        }
        
        if (null != lastSuccessfulRouteTo){
            if(consoleLog == true){ console.log("generateRoute - isViaClick: false, lastSuccessfulRouteTo is NOT null.");}
            currentRouteToText = lastSuccessfulRouteTo.textValue();
        }else{
            if(consoleLog == true){ console.log("generateRoute - isViaClick: false, lastSuccessfulRouteTo is null.");}
            currentRouteToText = $get(textRouteToClientID).value;
        }
    }    
                       
    var routeFromID = $get('hRouteFromID').value
    var routeToID = $get('hRouteToID').value
    
    if (haveRouteSettingsChanged() ){
        if(consoleLog == true){ console.log("generateRoute - Route information (settings) have changed.");}
        lastSuccessfulRouteFrom = null;
        lastSuccessfulRouteTo = null;
    }else if (haveRouteWaypointsChanged(false)){
        if(consoleLog == true){ console.log("generateRoute - Route information (waypoints) have changed.");}
    }else{
        if(consoleLog == true){ console.log("generateRoute - Route information (settings and waypoints) have NOT changed.");}   
    }

    var x = $get(ddlFeatureOptionsClientID);
    var dataSourceSettings = x.options[x.selectedIndex].value;

    if(consoleLog == true){ console.log("generateRoute - routeFromID: '" + routeFromID + "'");}
    if(consoleLog == true){ console.log("generateRoute - routeToID: '" + routeToID + "'");}

    if(consoleLog == true){ console.log("generateRoute - lastSuccessfulRouteFrom: '" + ((lastSuccessfulRouteFrom == null) ? 'null' : lastSuccessfulRouteFrom.textValue()) + "'");}
    if(consoleLog == true){ console.log("generateRoute - lastSuccessfulRouteTo: '" + ((lastSuccessfulRouteTo == null) ? 'null' : lastSuccessfulRouteTo.textValue()) + "'");}
    if(consoleLog == true){ console.log("generateRoute - currentRouteFromText: '" + currentRouteFromText + "'");}
    if(consoleLog == true){ console.log("generateRoute - currentRouteToText: '" + currentRouteToText + "'");}

    if(lastSuccessfulRouteFrom == null) {lastSuccessfulRouteFrom = new KeyValuePair();}
    if(lastSuccessfulRouteTo == null){ lastSuccessfulRouteTo = new KeyValuePair();}
           
    if (!isViaClick || (null == lastSuccessfulRouteFrom && null == lastSuccessfulRouteTo) || (lastSuccessfulRouteFrom.key() == '' && lastSuccessfulRouteTo.key() == '')){
        //1) Check to see if either an initial route, or a route has been reset by setting last successful route values to null.
        if(consoleLog == true){ console.log("generateRoute - Initial (from & to) route.");}

        clearMap();
               
        if ('' == routeFromID && '' == routeToID){
            //if(consoleLog == true){ console.log("generateRoute - routeFromID: '" + routeFromID + "', routeToID: '" + routeToID + "'.");}
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_START_AND_END_ROUTE_ENTRY);
            return true;
        }else if('' == routeFromID){
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_START_ROUTE_ENTRY);k
            return true;
        }else if('' == routeToID){
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_END_ROUTE_ENTRY);
            return true;
        }else if(haveRouteWaypointsChanged(true)){
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_VALID_WAYPOINT_ENTRY);
            return true;
        }else{
            ;//Continue
        }

        var locationIDs = new Array();
        var waypointIDs = getWaypointIDs();
        locationIDs.push(routeFromID);
        for(var i=0; i < waypointIDs.length; i++)
        {
            locationIDs.push(waypointIDs[i]);
        }
        locationIDs.push(routeToID);

        clearSearchData();
            
        lastSuccessfulRouteFrom = new KeyValuePair($get('hRouteFromID').value, currentRouteFromText, dataSourceSettings);
        lastSuccessfulRouteTo = new KeyValuePair($get('hRouteToID').value, currentRouteToText, dataSourceSettings);
        if(consoleLog == true){ console.log("generateRoute - Calling SwaggieWS.GetSuburbPoints.");}
        SwaggieWS.GetSuburbPoints(locationIDs, getRouteResultsCallback, getRouteNoResultsCallback);
    }else if (null == lastSuccessfulRouteFrom && lastSuccessfulRouteTo != currentRouteToText){
        //2) Check to see we have initially defined only a (from) route point, AND there is a difference to a previous (to) route point.
        if(consoleLog == true){ console.log("generateRoute - Initial (from) route point, and difference to previous (to) point.");}

        clearSearchData();
        clearMap();
        
        setRouteFromToXY("","","","","");
        setErrorMessage(NO_START_AND_END_ROUTE_ENTRY);
        return true;
    }else if (null == lastSuccessfulRouteTo && lastSuccessfulRouteFrom != currentRouteFromText){
        //3) Check to see we have initially defined only a (to) route point, AND there is a difference to a previous (from) route point.
        if(consoleLog == true){ console.log("generateRoute - Initial (to) route point, and difference to previous (from) point.");}

        clearSearchData();
        clearMap();
        
        setRouteFromToXY("","","","","");
        setErrorMessage(NO_START_AND_END_ROUTE_ENTRY);
        return true;
    }else if ('' == lastSuccessfulRouteFrom.key()){
    //}else if (null == lastSuccessfulRouteFrom){
        //4) Check to see if we have initially defined only a (from) route point.
        if(consoleLog == true){ console.log("generateRoute - Initial (from) route point.");}

        clearMap();
        
        if ('' == routeFromID && '' == routeToID){
            //if(consoleLog == true){ console.log("generateRoute - routeFromID: '" + routeFromID + "', routeToID: '" + routeToID + "'.");}
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_START_AND_END_ROUTE_ENTRY);
            return true;
        }else if('' == routeFromID){
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_START_ROUTE_ENTRY);
            return true;
        }else if('' == routeToID){
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_END_ROUTE_ENTRY);
            return true;
        }else{
            ;//Continue
        }

        var locationIDs = new Array();
        var waypointIDs = getWaypointIDs();
        locationIDs.push(routeFromID);
        for(var i=0; i < waypointIDs.length; i++)
        {
            locationIDs.push(waypointIDs[i]);
        }
        locationIDs.push(routeToID);
    
        clearSearchData();
        
        lastSuccessfulRouteFrom = new KeyValuePair($get('hRouteFromID').value, currentRouteFromText, dataSourceSettings);
        lastSuccessfulRouteTo = new KeyValuePair($get('hRouteToID').value, currentRouteToText, dataSourceSettings);
        if(consoleLog == true){ console.log("generateRoute - Calling SwaggieWS.GetSuburbPoints.");}
        SwaggieWS.GetSuburbPoints(locationIDs, getRouteResultsCallback, getRouteNoResultsCallback);
    }else if ('' == lastSuccessfulRouteTo.key()){
    //}else if (null == lastSuccessfulRouteTo){
        //5) Check to see if we have initially defined only a (to) route point.
        if(consoleLog == true){ console.log("generateRoute - Initial (to) route point.");}

        clearMap();
        
        if ('' == routeFromID && '' == routeToID){
            //if(consoleLog == true){ console.log("generateRoute - routeFromID: '" + routeFromID + "', routeToID: '" + routeToID + "'.");}
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_START_AND_END_ROUTE_ENTRY);
            return true;
        }else if('' == routeFromID){
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_START_ROUTE_ENTRY);
            return true;
        }else if('' == routeToID){
            setRouteFromToXY("","","","","");
            setErrorMessage(NO_END_ROUTE_ENTRY);
            return true;
        }else{
            ;//Continue
        }

        var locationIDs = new Array();
        var waypointIDs = getWaypointIDs();
        locationIDs.push(routeFromID);
        for(var i=0; i < waypointIDs.length; i++)
        {
            locationIDs.push(waypointIDs[i]);
        }
        locationIDs.push(routeToID);

        clearSearchData();
        
        lastSuccessfulRouteFrom = new KeyValuePair($get('hRouteFromID').value, currentRouteFromText, dataSourceSettings);
        lastSuccessfulRouteTo = new KeyValuePair($get('hRouteToID').value, currentRouteToText, dataSourceSettings);
        if(consoleLog == true){ console.log("generateRoute - Calling SwaggieWS.GetSuburbPoints.");}
        SwaggieWS.GetSuburbPoints(locationIDs, getRouteResultsCallback, getRouteNoResultsCallback);
    }else if(lastSuccessfulRouteFrom.textValue() != currentRouteFromText && lastSuccessfulRouteTo.textValue() != currentRouteToText){
        //7) Check to see if there is a difference to previous (from & to) route points.
        if(consoleLog == true){ console.log("generateRoute - Difference to previous (from & to) route points.");} 
        
        clearSearchData();
        clearMap();
        
        $get('hRouteFromID').value = '';
        $get('hRouteToID').value = '';
        setRouteFromToXY("","","","","");
                
        lastSuccessfulRouteFrom = null;
        lastSuccessfulRouteTo = null;
        setErrorMessage(NO_START_AND_END_ROUTE_ENTRY);
    }else if(lastSuccessfulRouteFrom.textValue() != currentRouteFromText){
        //8) Check to see if there is a difference to a previous (from) route point.
        if(consoleLog == true){ console.log("generateRoute - Difference to previous (from) route point.");} 
        
        clearSearchData();
        clearMap();
        
        $get('hRouteFromID').value = '';
        setRouteFromToXY("","","","","");
            
        lastSuccessfulRouteFrom = null;
        lastSuccessfulRouteTo = new KeyValuePair($get('hRouteToID').value, currentRouteToText, dataSourceSettings);
        
        setErrorMessage(NO_START_ROUTE_ENTRY);
    }else if(lastSuccessfulRouteTo.textValue() != currentRouteToText){
        //9) Check to see if there is a difference to a previous (to) route point.
        if(consoleLog == true){ console.log("generateRoute - Difference to previous (to) route point.");}

        clearSearchData();
        clearMap();
        
        $get('hRouteToID').value = '';
        setRouteFromToXY("","","","","");
            
        lastSuccessfulRouteFrom = new KeyValuePair($get('hRouteFromID').value, currentRouteFromText, dataSourceSettings);
        lastSuccessfulRouteTo = null;
        
        setErrorMessage(NO_END_ROUTE_ENTRY);
    }else if ((lastSuccessfulRouteFrom.key() != routeFromID) || (lastSuccessfulRouteTo.key() != routeToID)){
        //6) Check to see if a route has changed because of different current and previous keys.
        if(consoleLog == true){ console.log("generateRoute - Different (from & to) route keys.");}

        clearMap();
                   
        var locationIDs = new Array();
        var waypointIDs = getWaypointIDs();
        locationIDs.push(routeFromID);
        for(var i=0; i < waypointIDs.length; i++)
        {
            locationIDs.push(waypointIDs[i]);
        }
        locationIDs.push(routeToID);

        clearSearchData();
        
        lastSuccessfulRouteFrom = new KeyValuePair($get('hRouteFromID').value, currentRouteFromText, dataSourceSettings);
        lastSuccessfulRouteTo = new KeyValuePair($get('hRouteToID').value, currentRouteToText, dataSourceSettings);
        if(consoleLog == true){ console.log("generateRoute - Calling SwaggieWS.GetSuburbPoints.");}
        SwaggieWS.GetSuburbPoints(locationIDs, getRouteResultsCallback, getRouteNoResultsCallback);
    }else if(haveRouteWaypointsChanged(true)){
        if(consoleLog == true){ console.log("generateRoute - Different to previous waypoints.");}

        clearSearchData();
        clearMap();

        setRouteFromToXY("","","","","");
        setErrorMessage(NO_VALID_WAYPOINT_ENTRY);
        return true;
    }else{
        //No changes so therefore the current map on display, has been re-requested.
        if(consoleLog == true){ console.log("generateRoute - No changes from previous requested route.");} 
        return true;
    }
    return true;
}

function getWaypointIDs(){
    var waypointIDs = new Array();

    var id1 = $get('hRouteWaypoint1ID').value;
    var id2 = $get('hRouteWaypoint2ID').value;
    var id3 = $get('hRouteWaypoint3ID').value;
    var id4 = $get('hRouteWaypoint4ID').value;
    var id5 = $get('hRouteWaypoint5ID').value;
    
    if('' != id1){
        waypointIDs.push(id1);
    }
    if('' != id2){
        waypointIDs.push(id2);
    }
    if ('' != id3){
        waypointIDs.push(id3);
    }
    if('' != id4){
        waypointIDs.push(id4);
    }
    if('' != id5){
        waypointIDs.push(id5);
    }

    return waypointIDs
}

//To detect whether there are any changes, we have to cycle through all route waypoints to see if there are any changes.
//If we detect a change we set a flag, else we continue through our validation, where at the end of the fn, we return the flag.
function haveRouteSettingsChanged(){
    var returnValue = false;
    
    var buildText = getBuildType();
    var avoidTunnels = $get(chkRouteOptionAvoidTunnelsClientID).checked;
    var avoidFerries = $get(chkRouteOptionAvoidFerriesClientID).checked;
    var avoidTollways = $get(chkRouteOptionAvoidTollwaysClientID).checked;
    var avoidUnsealedRoads = $get(chkRouteOptionAvoidUnsealedRoadsClientID).checked;
    if(null == lastSuccessfulRouteSettingsValues){
        if(consoleLog == true){ console.log("haveRouteSettingsChanged - null");}
        //Either an initial waypoint, or the waypoint value has been reset by setting to null.
    }else if(lastSuccessfulRouteSettingsValues.buildType() != buildText || lastSuccessfulRouteSettingsValues.avoidTunnels() != avoidTunnels || lastSuccessfulRouteSettingsValues.avoidFerries() != avoidFerries || lastSuccessfulRouteSettingsValues.avoidTollways() != avoidTollways || lastSuccessfulRouteSettingsValues.avoidUnsealedRoads() != avoidUnsealedRoads){
        if(consoleLog == true){ console.log("haveRouteSettingsChanged - Return true");}
        returnValue =  true;
    }
    return returnValue;
}

//To detect whether there are any changes, we have to cycle through all route waypoints to see if there are any changes.
//If we detect a change we set a flag, else we continue through our validation, where at the end of the fn, we return the flag.
//A boolean that determines if we reset the textfield for an associated waypoint textfield if their is a difference between the last successful value and the current.
function haveRouteWaypointsChanged(resetInvalidTextField){
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged - Start, resetInvalidTextField: " + resetInvalidTextField);}
    var returnValue = false;
    
    var currentWaypoint1Text = $get(textRouteWaypoint1ClientID).value;
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   currentWaypoint1Text: '" + currentWaypoint1Text + "', hRouteWaypoint1ID: '" + $get('hRouteWaypoint1ID').value + "'.");}
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   lastSuccessfulRouteWaypoint1: '" + ((lastSuccessfulRouteWaypoint1 == null) ? 'null' : lastSuccessfulRouteWaypoint1.textValue()) + "'");}
    if ('' == $get('hRouteWaypoint1ID').value && DEFAULT_ROUTE_WAYPOINT_TEXT != currentWaypoint1Text){
         //For the current text we have no associated ID (therefore user entered text, but did not select an entry from the suggestions).
        if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint1ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 1 has changed.");}
    }else if (null == lastSuccessfulRouteWaypoint1 || lastSuccessfulRouteWaypoint1.key() == ''){
        //Either an initial waypoint, or the waypoint value has been reset by setting to null.
    }else if(lastSuccessfulRouteWaypoint1.textValue() != currentWaypoint1Text || ('' == $get('hRouteWaypoint1ID').value && '' != currentWaypoint1Text)){
        //There is a difference between the current entered text, and the last previously SUCCESSFUL search.
        if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint1ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 1 has changed.");}
    }

    var currentWaypoint2Text = $get(textRouteWaypoint2ClientID).value;
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   currentWaypoint2Text: '" + currentWaypoint2Text + "', hRouteWaypoint2ID: '" + $get('hRouteWaypoint2ID').value + "'.");}
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   lastSuccessfulRouteWaypoint2: '" + ((lastSuccessfulRouteWaypoint2 == null) ? 'null' : lastSuccessfulRouteWaypoint2.textValue()) + "'");}
    if ('' == $get('hRouteWaypoint2ID').value && DEFAULT_ROUTE_WAYPOINT_TEXT != currentWaypoint2Text){
         //For the current text we have no associated ID (therefore user entered text, but did not select an entry from the suggestions).
        if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint2ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 2 has changed.");}
    }else if (null == lastSuccessfulRouteWaypoint2 || lastSuccessfulRouteWaypoint2.key() == ''){
        //Either an initial waypoint, or the waypoint value has been reset by setting to null.
    }else if(lastSuccessfulRouteWaypoint2.textValue() != currentWaypoint2Text){
        //There is a difference between the current entered text, and the last previously SUCCESSFUL search.
         if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint2ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 2 has changed.");}
    }

    var currentWaypoint3Text = $get(textRouteWaypoint3ClientID).value;
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   currentWaypoint3Text: '" + currentWaypoint3Text + "', hRouteWaypoint3ID: '" + $get('hRouteWaypoint3ID').value + "'.");}
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   lastSuccessfulRouteWaypoint3: '" + ((lastSuccessfulRouteWaypoint3 == null) ? 'null' : lastSuccessfulRouteWaypoint3.textValue()) + "'");}
    if ('' == $get('hRouteWaypoint3ID').value && DEFAULT_ROUTE_WAYPOINT_TEXT != currentWaypoint3Text){
         //For the current text we have no associated ID (therefore user entered text, but did not select an entry from the suggestions).
        if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint3ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 3 has changed.");}
    }else if (null == lastSuccessfulRouteWaypoint3 || lastSuccessfulRouteWaypoint3.key() == ''){
        //Either an initial waypoint, or the waypoint value has been reset by setting to null.
    }else if(lastSuccessfulRouteWaypoint3.textValue() != currentWaypoint3Text || ('' == $get('hRouteWaypoint3ID').value && '' != currentWaypoint3Text)){
        //There is a difference between the current entered text, and the last previously SUCCESSFUL search.
        if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint3ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 3 has changed.");}
    }

    var currentWaypoint4Text = $get(textRouteWaypoint4ClientID).value;
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   currentWaypoint4Text: '" + currentWaypoint4Text + "', hRouteWaypoint4ID: '" + $get('hRouteWaypoint4ID').value + "'.");}
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   lastSuccessfulRouteWaypoint4: '" + ((lastSuccessfulRouteWaypoint4 == null) ? 'null' : lastSuccessfulRouteWaypoint4.textValue()) + "'");}
    if ('' == $get('hRouteWaypoint4ID').value && DEFAULT_ROUTE_WAYPOINT_TEXT != currentWaypoint4Text){
         //For the current text we have no associated ID (therefore user entered text, but did not select an entry from the suggestions).
        if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint4ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 4 has changed.");}
    }else if (null == lastSuccessfulRouteWaypoint4 || lastSuccessfulRouteWaypoint4.key() == ''){
        //Either an initial waypoint, or the waypoint value has been reset by setting to null.
    }else if(lastSuccessfulRouteWaypoint4.textValue() != currentWaypoint4Text || ('' == $get('hRouteWaypoint4ID').value && '' != currentWaypoint4Text)){
        //There is a difference between the current entered text, and the last previously SUCCESSFUL search.
        if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint4ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 4 has changed.");}
    }

    var currentWaypoint5Text = $get(textRouteWaypoint5ClientID).value;
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   currentWaypoint5Text: '" + currentWaypoint5Text + "', hRouteWaypoint5ID: '" + $get('hRouteWaypoint5ID').value + "'.");}
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   lastSuccessfulRouteWaypoint5: '" + ((lastSuccessfulRouteWaypoint5 == null) ? 'null' : lastSuccessfulRouteWaypoint5.textValue()) + "'");}
    if ('' == $get('hRouteWaypoint5ID').value && DEFAULT_ROUTE_WAYPOINT_TEXT != currentWaypoint5Text){
         //For the current text we have no associated ID (therefore user entered text, but did not select an entry from the suggestions).
        if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint5ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 5 has changed.");}
    }else if (null == lastSuccessfulRouteWaypoint5 || lastSuccessfulRouteWaypoint5.key() == ''){
        //Either an initial waypoint, or the waypoint value has been reset by setting to null.
    }else if(lastSuccessfulRouteWaypoint5.textValue() != currentWaypoint5Text || ('' == $get('hRouteWaypoint5ID').value && '' != currentWaypoint5Text)){
        //There is a difference between the current entered text, and the last previously SUCCESSFUL search.
        if(resetInvalidTextField){
            removeWaypointData(textRouteWaypoint5ClientID);
        }
        returnValue =  true;
        if(consoleLog == true){ console.log("haveRouteWaypointsChanged -   waypoint 5 has changed.");}
    }
    
    if(consoleLog == true){ console.log("haveRouteWaypointsChanged - Finish, returnValue: " + returnValue);}
    return returnValue;
}

//For a given ID find the point, and plot the marker/s (and centres) on the map.
//This is used by the 'Find Location' functionality to obtain a collection of locations.
//results as a complex object (in this case a List(of MapDS.Structures.Point)).
function getSearchResultsCallback(results){
    if(consoleLog == true){ console.log("getSearchResultsCallback - Start.");}
    
    //setWaitCursor(true);
    //clearSearchData();
    if (results != null){
        if( results.length > 0){
            var x = $get(ddlFeatureOptionsClientID);
            var dataSourceSettings = x.options[x.selectedIndex].value;
            
            if (null == results[0]){
                if(consoleLog == true){ console.log("getSearchResultsCallback - Returned result has no X/Y values defined.");}
                setSearchXY('','');
                return true;
            }
            var point = new MapDS.LatLng( results[0].Y, results[0].X );
            if(consoleLog == true){ console.log("getSearchResultsCallback - Point: " + point);}
            
            setSearchXY(results[0].X, results[0].Y);
            if(consoleLog == true){ console.log("getSearchResultsCallback - X: " + $get('hSearchX').value + ", Y: " + $get('hSearchY').value);}
                    
            //Define centre marker
            var centreCalloutIconUrl = './images/marker/centre_callout.png';
            var centreIconUrl = './images/marker/centre.png';
            centreMarker = new MapDS.Marker( point, getIcon(centreCalloutIconUrl, true));
            centreMarker.setPopupContent(createRouteMarkerPopupContent('Centre', centreIconUrl, point, lastSuccessfulSearch.textValue()));
            map.addMarker( centreMarker);
            
            if(consoleLog == true){ console.log("getSearchResultsCallback - Calling SwaggieWS.GetFeatures.");}

            var featurePoints = new Array(1);
            featurePoints[0] = results[0]; 
            if("" != dataSourceSettings){
                SwaggieWS.GetFeatures(null, featurePoints, dataSourceSettings, MAXIMUM_FEATURES_RADIAL_SEARCH_DISTANCE, MAXIMUM_SEARCH_FEATURES_DISPLAY_COUNT, getFeaturesResultsCallback, getFindFeaturesNoResultsCallback);
            }
            map.setCentre(point, StreetZoomLevel); //Further
        }
    }
    //if(consoleLog == true){ console.log("getRouteResultsCallback -       Executing Driving Directions AJAX postback.");}
    //__doPostBack('btnDirections'); //Will clear the associated update panel.
}

function getSearchNoResultsCallback(results){
    //Clear hidden values, as this will effectively 'reset' them for another search.
    clearSearchData();
    clearRouteData();
    
    //setWaitCursor(false);
    lastSuccessfulSearch = null;
}

//Changes map contents (if previously defined, to display the selected feature).
function selectedFeatureIndexChanged(){
    if(consoleLog == true){ console.log("selectedFeatureIndexChanged - Start.");}
    var x = $get(ddlFeatureOptionsClientID);
    var dataSourceSettings = x.options[x.selectedIndex].value;

    //20090327 George Petrakis - Unfortunately need to re-render map, as there is no other, clear feature markers, as we do not track them.
    //    if("" == dataSourceSettings){
    //        return true;//Don't change current map, if selecting a feature of none.
    //    }

    var tabContainer = $find("tabContainerSearch");
    var index = tabContainer.get_activeTabIndex();
    if(consoleLog == true){ console.log("selectedFeatureIndexChanged - tab index is: '" + index + "'.");}
    //if(tab._header.innerHTML.indexOf(TAB_PANEL_HEADER_SEARCH_FIND) != -1 && lastSuccessfulSearch != null && lastSuccessfulSearch.key() != ''){
    if(0 == index && lastSuccessfulSearch != null && lastSuccessfulSearch.key() != ''){
        //Found 'Find' tab panel.
        if(consoleLog == true){ console.log("selectedFeatureIndexChanged - Found 'Find' tab panel.");}
        //lastSuccessfulSearch = new KeyValuePair();//Will force a new search to occur, on a call to generateSearch()
        
        generateSearch(false);
    //}else if(tab._header.innerHTML.indexOf(TAB_PANEL_HEADER_SEARCH_DIRECTIONS && lastSuccessfulRouteFrom != null && lastSuccessfulRouteFrom.key() != '' && lastSuccessfulRouteTo != null && lastSuccessfulRouteTo.key() != '') != -1){
    }else if(1 == index && lastSuccessfulRouteFrom != null && lastSuccessfulRouteFrom.key() != '' && lastSuccessfulRouteTo != null && lastSuccessfulRouteTo.key() != ''){
        //Found 'Directions' tab panel.
        if(consoleLog == true){ console.log("selectedFeatureIndexChanged - Found 'Directions' tab panel.");}
        //lastSuccessfulRouteFrom = new KeyValuePair();//Will force a new search to occur, on a call to generateSearch()
        //lastSuccessfulRouteTo = new KeyValuePair();//Will force a new search to occur, on a call to generateSearch()
        
        generateRoute(false);
    }
    
    if(consoleLog == true){ console.log("selectedFeatureIndexChanged - Finish.");}
}

//Get the route locations (i.e. points).
//Results as a complex object (in this case a List(of MapDS.Structures.Point)).
function getRouteResultsCallback(results){
    if(consoleLog == true){ console.log("getRouteResultsCallback - Start.");}
    routeWSPoints = null;
    routePoints = results;
    
    //var routeFromX;
    //var routeFromY;
    //var routeToX;
    //var routeToY;

    if(results != null){
        if(consoleLog == true){ console.log("getRouteResultsCallback -    Results not null.");}
        if(results.length > 0){
            routeWSPoints = new Array();
            lastSuccessfulRouteWaypoint1 = null;
            lastSuccessfulRouteWaypoint2 = null;
            lastSuccessfulRouteWaypoint3 = null;
            lastSuccessfulRouteWaypoint4 = null;
            lastSuccessfulRouteWaypoint5 = null;

            var x = $get(ddlFeatureOptionsClientID);
            var dataSourceSettings = x.options[x.selectedIndex].value;
            if(consoleLog == true){ console.log("getRouteResultsCallback -    Results.length: " + results.length + ".");}
            for(var i=0; i<results.length; i++)
            {
                lastSuccessfulRouteSettingsValues = new RouteSettingsValues(getBuildType(), $get(chkRouteOptionAvoidTunnelsClientID).checked, $get(chkRouteOptionAvoidFerriesClientID).checked, $get(chkRouteOptionAvoidTollwaysClientID).checked, $get(chkRouteOptionAvoidUnsealedRoadsClientID).checked);

                if (0==i){
                    if(consoleLog == true){ console.log("getRouteResultsCallback -    Defined start route point.");}
                    //lastSuccessfulRouteFrom = new KeyValuePair($get('hRouteFromID').value, $get(textRouteFromClientID).value, dataSourceSettings);
                    routeWSPoints.push(new MapDS.LatLng( results[i].Y, results[i].X ));
                }else if( i == results.length-1){
                    if(consoleLog == true){ console.log("getRouteResultsCallback -    Defined end route point.");}
                    //lastSuccessfulRouteTo = new KeyValuePair($get('hRouteToID').value, $get(textRouteToClientID).value, dataSourceSettings);
                    routeWSPoints.push(new MapDS.LatLng( results[i].Y, results[i].X ));
                }else{
                    if(consoleLog == true){ console.log("getRouteResultsCallback -    Defined a waypoint.");}
                    routeWSPoints.push(new MapDS.LatLng( results[i].Y, results[i].X ));

                    if ($get('hRouteWaypoint1ID').value != ''){
                        lastSuccessfulRouteWaypoint1 = new KeyValuePair($get('hRouteWaypoint1ID').value, $get(textRouteWaypoint1ClientID).value, dataSourceSettings);
                    }
                    
                    if ($get('hRouteWaypoint2ID').value != ''){
                        lastSuccessfulRouteWaypoint2 = new KeyValuePair($get('hRouteWaypoint2ID').value, $get(textRouteWaypoint2ClientID).value, dataSourceSettings);
                    }
                    
                    if ($get('hRouteWaypoint3ID').value != ''){
                        lastSuccessfulRouteWaypoint3 = new KeyValuePair($get('hRouteWaypoint3ID').value, $get(textRouteWaypoint3ClientID).value, dataSourceSettings);
                    }
                    
                    if ($get('hRouteWaypoint4ID').value != ''){
                        lastSuccessfulRouteWaypoint4 = new KeyValuePair($get('hRouteWaypoint4ID').value, $get(textRouteWaypoint4ClientID).value, dataSourceSettings);
                    }
                    
                    if ($get('hRouteWaypoint5ID').value != ''){
                        lastSuccessfulRouteWaypoint5 = new KeyValuePair($get('hRouteWaypoint5ID').value, $get(textRouteWaypoint5ClientID).value, dataSourceSettings);
                    }
                }
            }
        }
    }
    
    if(consoleLog == true){ console.log("getRouteResultsCallback -    Results iteration complete.");}
    
    for(var i=1; i < routeWSPoints.length - 1; i++)
    {
        $get('hRouteWaypoint' + i + 'X').value = routeWSPoints[i].Longitude();
        $get('hRouteWaypoint' + i + 'Y').value = routeWSPoints[i].Latitude();
    }

    //Populate hidden inputs containing route co-ordinates.
    //setRouteFromToXY("", routeWSPoints[0].Longitude(), routeWSPoints[0].Latitude(), routeWSPoints[routeWSPoints.length-1].Longitude(), routeWSPoints[routeWSPoints.length-1].Latitude());
    setRouteFromToXY("", "", "", "", "");

    if(consoleLog == true){ console.log("getRouteResultsCallback -    Defined route.");}
    
    //Defining a route by adding waypoints.
    var route = new MapDS.Route();
    
    if ($find(collapsiblePanelExtenderRouteOptionsClientID).get_Collapsed()){
        SwaggieWS.GetRoute(routePoints, "Quickest", false, false , false , false, getRouteFilenameResultsCallback, getRouteFilenameNoResultsCallback);
        if(consoleLog == true){ console.log("getRouteResultsCallback -    Started (default) route generation...");}
    }else{
        var buildType = getBuildType();
        if(consoleLog == true){ console.log("getRouteResultsCallback -    Started (with custom parameters) route generation..." + $get(chkRouteOptionAvoidTunnelsClientID).checked);}
        SwaggieWS.GetRoute(routePoints, buildType, $get(chkRouteOptionAvoidTunnelsClientID).checked, $get(chkRouteOptionAvoidFerriesClientID).checked, $get(chkRouteOptionAvoidTollwaysClientID).checked, $get(chkRouteOptionAvoidUnsealedRoadsClientID).checked, getRouteFilenameResultsCallback, getRouteFilenameNoResultsCallback);
    }
    //route.addWayPoint( new MapDS.LatLng( routeToY, routeToX) );
    
    if(consoleLog == true){ console.log("getRouteResultsCallback - Finish.");}
    return true;
}

function getRouteNoResultsCallback(results){
    //Clear hidden values, as this will effectively 'reset' them for another search.
    routeWSPoints = null;
    routePoints = null;
    
    clearRouteData();
    
    lastSuccessfulRouteFrom = null;
    lastSuccessfulRouteTo = null;
    
    lastSuccessfulRouteWaypoint1 = null;
    lastSuccessfulRouteWaypoint2 = null;
    lastSuccessfulRouteWaypoint3 = null;
    lastSuccessfulRouteWaypoint4 = null;
    lastSuccessfulRouteWaypoint5 = null;

    lastSuccessfulRouteSettingsValues = null;
}

//Gets the route filename (contained in a RouteInfo argument) and generates route markers.
function getRouteFilenameResultsCallback(result){
    if(consoleLog == true){ console.log("getRouteFilenameResultsCallback - Start.");}

    //var route = new MapDS.Route(null, filename, new MapDS.Bounds( 150.19431, -34.937536, 152.218313, -32.825526 ));
    var route = new MapDS.Route(null, result.Filename, new MapDS.Bounds( result.Bounds.MinX, result.Bounds.MinY, result.Bounds.MaxX, result.Bounds.MaxY));
    map.addRoute(route);

    if(consoleLog == true){ console.log("getRouteFilenameResultsCallback -    Have added route " + result.Filename + " to map.");}

    var currentRouteFromText = $get(textRouteFromClientID).value;
    var currentRouteToText = $get(textRouteToClientID).value;
    var waypointText= '';
    
    if(consoleLog == true){ console.log("getRouteFilenameResultsCallback -    0.5.");}
    //Add markers (related to route start, waypoints, and end points).
    var startCalloutIconUrl = './images/marker/route_start_callout.png';
    var startIconUrl = './images/marker/route_start.png';
    var startRouteMarker = new MapDS.Marker( routeWSPoints[0], getIcon(startCalloutIconUrl, true));
    //startRouteMarker.setPopupContent(createRouteMarkerPopupContent('Start', startIconUrl, routeWSPoints[0], currentRouteFromText)); 
    startRouteMarker.setPopupContent(createRouteMarkerPopupContent('Start', startIconUrl, routeWSPoints[0], lastSuccessfulRouteFrom.textValue())); 

    map.addMarker( startRouteMarker);
    
    if(consoleLog == true){ console.log("getRouteFilenameResultsCallback -    1.");}
    
    var waypointTexts = new Array();

    if( lastSuccessfulRouteWaypoint1 != null){
        waypointTexts.push(lastSuccessfulRouteWaypoint1.textValue());
    }
    if( lastSuccessfulRouteWaypoint2 != null){
        waypointTexts.push(lastSuccessfulRouteWaypoint2.textValue());
    }
    if( lastSuccessfulRouteWaypoint3 != null){
        waypointTexts.push(lastSuccessfulRouteWaypoint3.textValue());
    }
    if( lastSuccessfulRouteWaypoint4 != null){
        waypointTexts.push(lastSuccessfulRouteWaypoint4.textValue());
    }
    if( lastSuccessfulRouteWaypoint5 != null){
        waypointTexts.push(lastSuccessfulRouteWaypoint5.textValue());
    }

    for(var i=0; i < waypointTexts.length; i++)
    {
        if(consoleLog == true){ console.log("getRouteFilenameResultsCallback -    Route waypoint ID: '" + 'textRouteWaypoint' + (i + 1) + 'ClientID' + "', waypointText: '" + waypointTexts[i] + "'.");}
        
        var waypointCalloutIconUrl = './images/marker/route_waypoint' + (i + 1) + '_callout.png'
        var waypointIconUrl = './images/marker/route_waypoint' + (i + 1) + '.png'
        var waypointMarker = new MapDS.Marker( routeWSPoints[i+1], getIcon(waypointCalloutIconUrl, true));
        waypointMarker.setPopupContent(createRouteMarkerPopupContent('Waypoint', waypointIconUrl, routeWSPoints[i+1], waypointTexts[i])); 
        map.addMarker(waypointMarker);
    }
    
    var endCalloutIconUrl = './images/marker/route_end_callout.png';    
    var endIconUrl = './images/marker/route_end.png';
    var endRouteMarker = new MapDS.Marker( routeWSPoints[routeWSPoints.length-1], getIcon(endCalloutIconUrl, true));
    //endRouteMarker.setPopupContent(createRouteMarkerPopupContent('End', endIconUrl, routeWSPoints[routeWSPoints.length-1], currentRouteToText)); 
    endRouteMarker.setPopupContent(createRouteMarkerPopupContent('End', endIconUrl, routeWSPoints[routeWSPoints.length-1], lastSuccessfulRouteTo.textValue())); 
    
    map.addMarker( endRouteMarker);

    //if(consoleLog == true){ console.log("getRouteFilenameResultsCallback -    Rendering route markers...");}
    if(consoleLog == true){ console.log("getRouteFilenameResultsCallback -    Route rendered. Setting route filename and route (start and end) markers...");}
    //setRouteFromToXY(result.Filename, routeWSPoints[0].Longitude(), routeWSPoints[0].Latitude(), routeWSPoints[routeWSPoints.length-1].Longitude(), routeWSPoints[routeWSPoints.length-1].Latitude());
    setRouteFromToXY(result.Filename, "", "", "", "");
      
    map.loadBestView(null,true);//20090604 George Petrakis Need to force a load or else long routes (e.g. Syd-Perth) wont be automatically rendered.
    //Add markers (related to route features).
    
    var x = $get(ddlFeatureOptionsClientID);
    var dataSourceSettings = x.options[x.selectedIndex].value;
    if("" != dataSourceSettings){
        if(consoleLog == true){ console.log("getRouteFilenameResultsCallback - Calling SwaggieWS.GetFeatures.");}

        //SwaggieWS.GetFeatures(null, routePoints, dataSourceSettings, MAXIMUM_FEATURES_RADIAL_SEARCH_DISTANCE, MAXIMUM_ROUTE_FEATURES_DISPLAY_COUNT, getFeaturesResultsCallback, getRouteFeaturesNoResultsCallback);
        SwaggieWS.GetFeatures(result.Filename, null, dataSourceSettings, MAXIMUM_FEATURES_RADIAL_SEARCH_DISTANCE, MAXIMUM_ROUTE_FEATURES_DISPLAY_COUNT, getFeaturesResultsCallback, getRouteFeaturesNoResultsCallback);
    }

    if(consoleLog == true){ console.log("getRouteFilenameResultsCallback - Finish.");}
}

function getRouteFilenameNoResultsCallback(results){
    if(consoleLog == true){ console.log("getRouteFilenameNoResultsCallback - Start & Finish.");}
    routeWSPoints = null;
    routePoints = null;

    setErrorMessage(NO_ROUTE_GENERATED);
}

//Defines the route build type according to the radio button selected.
//Text returned is the value that is passed to the route WS.
function getBuildType(){
    if(consoleLog == true){ console.log("getBuildType - Start - '" + collapsiblePanelExtenderRouteOptionsClientID + "'.");}
    if ($find(collapsiblePanelExtenderRouteOptionsClientID).get_Collapsed()){
        return "Quickest";
    }
    
    if ($get(radioRouteOptionTypeQuickestClientID).checked){
        return "Quickest";
    }else if($get(radioRouteOptionTypeSimplestClientID).checked){
        return "Simplest";
    }else{
        return "Quickest";
    }
}

//Gets a collection of nearest features. List(Of FeatureData)
function getFeaturesResultsCallback(results){
    if(consoleLog == true){ console.log("getFeaturesResultsCallback - Start.");}
    //Clear hidden values, as this will effectively 'reset' them for another search.
    //clearSearchData();
   
    if(results != null){
        if(results.length > 0){
            //map.DetachEvent("onendpan",MapMove);
            //map.DetachEvent("onendzoom",MapMove);
         
            var size = new MapDS.Size( 25, 35 );
            var offset = new MapDS.Pixel( -11, -33 );
            
            if(consoleLog == true){ console.log("getFeaturesResultsCallback - Have the following results: " + results.length);}
            for(var i=0; i < results.length; i++)
            {
                //var icon = new MapDS.Icon( url, size, offset );
                if(consoleLog == true){ console.log(i + ") Name:" + results[i].Name + ", ID:" + results[i].ID + ", point: " + (new MapDS.LatLng( results[i].Point.Y, results[i].Point.X )));}
                //var marker = new MapDS.Marker( new MapDS.LatLng( results[i].Point.Y, results[i].Point.X ), getIcon(getFeatureIconUrl(results[i].FeatureType, true), true));
                //marker.setPopupContent(createFeatureMarkerPopupContent (results[i], getFeatureIconUrl(results[i].FeatureType, false)));
                var marker = new MapDS.Marker( new MapDS.LatLng( results[i].Point.Y, results[i].Point.X ), getIcon(results[i].CalloutIconImageName, true));
                marker.setPopupContent(createFeatureMarkerPopupContent (results[i], results[i].IconImageName));
                marker.ID(results[i].ID);
                map.addMarker(marker);
            }
            var currentSearchText = $get(textSearchClientID).value;
            map.loadBestView();
        }else{
            if(consoleLog == true){ console.log("getFeaturesResultsCallback - No results. Features (if any) are outside of the search radius.");}
            setErrorMessage(NO_FEATURES_FOUND_FOR_SELECTION);
            //Do nothing
        }
    }else{
        if(consoleLog == true){ console.log("getFeaturesResultsCallback - No results.");}

        //Keep current centre and zoom level.
        //if(centreMarker != null){
        //    if(consoleLog == true){ console.log("getFeaturesResultsCallback - Adding centre Marker.");}
        //    map.addMarker(centreMarker);

        //    map.setCentre( centreMarker.getPoint(), StreetZoomLevel) 
        //}
    }
    if(consoleLog == true){ console.log("getFeaturesResultsCallback - Finish.");} 
}

function getFindFeaturesNoResultsCallback(results){
    //Clear hidden values, as this will effectively 'reset' them for another search.
    clearSearchData();
    
    if(centreMarker != null){
        if(consoleLog == true){ console.log("getFindFeaturesNoResultsCallback - Adding centre Marker.");}
        map.addMarker(centreMarker);
        
        map.setCentre( centreMarker.getPoint(), StreetZoomLevel) 
    }
    setErrorMessage(NO_FEATURES_FOUND_FOR_SELECTION);
}

function getRouteFeaturesNoResultsCallback(results){
    //Clear hidden values, as this will effectively 'reset' them for another search.
    clearRouteData();

    map.loadBestView();
    setErrorMessage(NO_FEATURES_FOUND_FOR_SELECTION);
}

//Sets the selected point. Point is a MapDS.Structures.Point instance, message a string.
//Used by the DrivingDirections user control. Don't change signature.
function showRouteMarker(point){
    if (directionsCentreMarker != null){
        map.removeMarker(directionsCentreMarker);
    }
    showMarkerPopup(point, null);
}

//Sets the selected point. Point is a MapDS.Structures.Point instance, message a string.
//Used by the DrivingDirections user control. Don't change signature.
function showDirectionsMarker(point, messageNo, message){
    if (directionsCentreMarker != null){
        map.removeMarker(directionsCentreMarker);
    }

    if (point != null){
        var wsPoint = new MapDS.LatLng(point.Y, point.X);
        var url = './images/marker/' + 'cross.png';
        directionsCentreMarker = new MapDS.Marker(wsPoint, getIcon(url, false));
        directionsCentreMarker.setPopupContent(createDirectionsPopupContent('Direction Information', url, wsPoint, messageNo, message));

        //if(consoleLog == true){ console.log("setDirectionsPoint - Adding directionsCentreMarker, url is: " + url);}
        map.addMarker(directionsCentreMarker);

        map.setCentre(wsPoint, StreetZoomLevel);

        directionsCentreMarker.showPopup();
    }else{
        if (directionsCentreMarker != null){
            map.removeMarker(directionsCentreMarker);
        }
    }
}

//Enables a popup for the selected marker. Identifying point is MapDS.Structures.Point instance.
//There may multiple colocated marker for a given co-ordinate pair, therefore we need to identify
//a specific marker via some unique identifying text. 
function showMarkerPopup(point, identifyingText){
    if(consoleLog == true){ console.log("showMarkerPopup - Start");}
    if(consoleLog == true){ console.log("showMarkerPopup -    point: " + ((identifyingText == null) ? 'null' : "X:" + point.X + ", Y:" + point.Y));}
    if(consoleLog == true){ console.log("showMarkerPopup -    identifyingText: '" + ((identifyingText == null) ? 'null' : identifyingText) + "'");}
    
    var wsPoint = new MapDS.LatLng(point.Y, point.X);
    map.setCentre(wsPoint, StreetZoomLevel);
    
    if (directionsCentreMarker != null){
        map.removeMarker(directionsCentreMarker);
    }
    
    var foundMarkers = map.findNearestMarker(wsPoint);
    if( null != foundMarkers){
        //foundMarkers.showPopup(); //is an array
        if(foundMarkers.length > 0){
            if(consoleLog == true){ console.log("showMarkerPopup -    Have the following foundMarkers: " + foundMarkers.length);}
            if(null == identifyingText || '' == identifyingText){
                foundMarkers[0].showPopup();
                return true;
            }

            for(var i=0; i < foundMarkers.length; i++)
            {
                if(consoleLog == true){ console.log("showMarkerPopup -    Comparing, i: " + i + ", foundMarkers[i].ID: '" + foundMarkers[i].ID() + "'.");}
                var markerID = foundMarkers[i].ID();
                if(null != markerID){
                    if(-1 != markerID.indexOf(identifyingText)){
                        if(consoleLog == true){ console.log("showMarkerPopup -    Found marker.");}
                        foundMarkers[i].showPopup();
                        return true;
                    }
                }else{
                    if(consoleLog == true){ console.log("showMarkerPopup -    markerID is null.");}
                }
            }
        }
    }
}

//Sets the selected point. Point is MapDS.Structures.Point instance. isNormalMarker is boolean.
function getIcon(url, isNormalMarker){
    var size;
    var offset;
    if(true == isNormalMarker){
        size = new MapDS.Size( 30, 32 );
        offset = new MapDS.Pixel( -15, -32 );
    }else{
        var width = 30;
        var height = 32;
        size = new MapDS.Size( width, height );
        offset = new MapDS.Pixel( -(width/2), -(height/2) );
    }

    var icon = new MapDS.Icon( url, size, offset );
    return icon;
}

//Zoom in + centre on selected point (MapDS.Structures.Point) if defined.
function zoomIn(point){
    var mapPoint;
    if(consoleLog == true){ console.log("zoomIn - Start." + point);}
    if(null == point ){
        //if(consoleLog == true){ console.log("zoomIn - Point is null.");}
        mapPoint = map.getCentre();
    }else{
        //if(consoleLog == true){ console.log("zoomIn - Point.X: " + point.X + ", Point.Y: " + point.Y);}
        mapPoint = new MapDS.LatLng(point.Y, point.X)
    }
    var currentZoomLevel = map.getZoom();
    //if(consoleLog == true){ console.log("zoomIn - Set currentZoomLevel: " + currentZoomLevel + ".");}
    map.setCentre(mapPoint, currentZoomLevel + ZoomInOutStep); //Closer
    if(consoleLog == true){ console.log("zoomIn - Finished.");}
}

//Zoom in + centre on selected point MapDS.LatLng if defined).
function zoomOut(point){
    var mapPoint;
    if(consoleLog == true){ console.log("zoomOut - Start.");}
    if(null == point ){
        //if(consoleLog == true){ console.log("zoomIn - Point is null.");}
        mapPoint = map.getCentre();
    }else{
        mapPoint = new MapDS.LatLng(point.Y, point.X)
    }
    var currentZoomLevel = map.getZoom();
    map.setCentre(mapPoint, currentZoomLevel - ZoomInOutStep); //Further
}

//feature is an instance of FeatureData, titleImageUrl is a string.
function createFeatureMarkerPopupContent(feature, titleImageUrl){
    var markup;
    markup = '<div class="mapPopup" title="Feature Information">';
    markup += '<div class="mapPopupTitle">';
    markup += '<table>';
    markup += '<tr>';
    markup += '<td><img class="mapPopupTitleImage" src=' + titleImageUrl + ' /></td>';
    markup += '<td class="mapPopupTitleText">'+ feature.Name + '</td>';
    markup += '</tr>';
    markup += '</table>';
    markup += '</div>';
    markup += '<div class="mapPopupContent">';
    markup += '<table>';
    if('' != feature.Company){
        markup += '<tr>';
        markup += '<td class="fieldName">Company:</td>';
        markup += '<td class="fieldValue">' + feature.Company + '</td>';
        markup += '</tr>';
    }
    markup += '<tr>';
    markup += '<td class="fieldName">Address:</td>';
    markup += '<td class="fieldValue">' + feature.Address + '</td>';
    markup += '</tr>';
      
    if('' != feature.RegionZone){
        markup += '<tr>';
        markup += '<td class="fieldName">Region:</td>';
        markup += '<td class="fieldValue">' + feature.RegionZone + '</td>';
        markup += '</tr>';
    }
    
    markup += '<tr>';
    markup += '<td class="fieldName">Phone:</td>';
    markup += '<td class="fieldValue">' + feature.Phone + '</td>';
    markup += '</tr>';
    if('' != feature.Fax){
        markup += '<tr>';
        markup += '<td class="fieldName">Fax:</td>';
        markup += '<td class="fieldValue">' + feature.Fax + '</td>';
        markup += '</tr>';
    }
    if('' != feature.Email){
        markup += '<tr>';
        markup += '<td class="fieldName">Email:</td>';
        markup += '<td class="fieldValue">' + feature.Email + '</td>';
        markup += '</tr>';
    }
    if('' != feature.URL){
        markup += '<tr>';
        markup += '<td class="fieldName">URL:</td>';
        markup += '<td class="fieldValue">' + feature.URL + '</td>';
        markup += '</tr>';
    }
    markup += '<tr>';
    markup += '<td></td>';
    markup += '<td class="fieldValue">';
    markup += '<a href="#" onclick="javascript:zoomIn({X:' + feature.Point.X + ', Y:' + feature.Point.Y + '});" title="Zoom In"><span>Zoom In</span></a>';
    markup += '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="javascript:zoomOut({X:' + feature.Point.X + ', Y:' + feature.Point.Y + '});" title="Zoom Out"><span>Zoom Out</span></a>';
    markup += '</td>';
    markup += '</tr>';
    markup += '</table>';
    markup += '</div>';
    markup += '</div>';
    return markup;
}

//titleText and titleImagUrl are strings, point is a MapDS.LatLong instance, contentText as string.
function createRouteMarkerPopupContent(titleText, titleImageUrl, point, contentText){
    var markup;
    markup = '<div class="mapPopup" title="Information">';
    markup += '<div class="mapPopupTitle">';
    markup += '<table>';
    markup += '<tr>';
    markup += '<td><img class="mapPopupTitleImage" src=' + titleImageUrl + ' /></td>';
    markup += '<td class="mapPopupTitleText">'+ titleText + '</td>';
    markup += '</tr>';
    markup += '</table>';
    markup += '</div>';
    markup += '<div class="mapPopupContent">';
    markup += '<table>';
    markup += '<tr>';
    markup += '<td class="fieldName">Address:</td>';
    markup += '<td class="fieldValue">' + contentText + '</td>';
    markup += '</tr>';
    if(null != point){
        markup += '<tr>';
        markup += '<td></td>';
        markup += '<td class="fieldValue">';
        markup += '<a href="#" onclick="javascript:zoomIn({X:' + point.Longitude() + ', Y:' + point.Latitude() + '});" title="Zoom In"><span>Zoom In</span></a>';
        markup += '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="javascript:zoomOut({X:' + point.Longitude() + ', Y:' + point.Latitude() + '});" title="Zoom Out"><span>Zoom Out</span></a>';
        markup += '</td>';
        markup += '</tr>';
    }
    markup += '</table>';
    markup += '</div>';
    markup += '</div>';
    return markup;
}

//titleText and titleImageUrl are strings, point is a MapDS.LatLong instance, contentNo as string, contentText as string.
function createDirectionsPopupContent(titleText, titleImageUrl, point, contentNo, contentText){
    var markup;
    markup = '<div class="mapPopup" title="Information">';
    markup += '<div class="mapPopupTitle">';
    markup += '<table>';
    markup += '<tr>';
    markup += '<td><img class="mapPopupTitleImage" src=' + titleImageUrl + ' /></td>';
    markup += '<td class="mapPopupTitleText">'+ titleText + '</td>';
    markup += '</tr>';
    markup += '</table>';
    markup += '</div>';
    markup += '<div class="mapPopupContent">';
    markup += '<table cellspacing="0" cellpadding="0">'
    markup += '<tr>';
    if("" != contentNo){
        markup += '<td class="fieldName">' + contentNo + '.</td>';
        markup += '<td class="fieldValue">' + contentText + '</td>';
    }else{
        markup += '<td colspan="2" class="fieldValue">' + contentText + '</td>';
    }
    markup += '</tr>';
    if(null != point){
        markup += '<tr>';
        if("" != contentNo){
            markup += '<td class="fieldName"></td>';
            markup += '<td class="fieldValue">';
        }else{
            markup += '<td colspan="2" class="fieldValue">';
        }
        markup += '<a href="#" onclick="javascript:zoomIn({X:' + point.Longitude() + ', Y:' + point.Latitude() + '});" title="Zoom In"><span>Zoom In</span></a>';
        markup += '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="javascript:zoomOut({X:' + point.Longitude() + ', Y:' + point.Latitude() + '});" title="Zoom Out"><span>Zoom Out</span></a>';
        markup += '</td>';
        markup += '</tr>';
    }
    markup += '</table>';
    markup += '</div>';
    markup += '</div>';
    return markup;
}

//Always do server-side postback, and let server-side logic validate values, as for databound controls,
//null or empty values meen that the control is not rendered if data source is 'empty'.
function setSearchXY(hX, hY){
    if(consoleLog == true){ console.log("setSearchXY - Start, hX: '" + hX + "', 'hY: '" + hY + "'.");}
    $get('hSearchX').value = hX;
    $get('hSearchY').value = hY;

    if(consoleLog == true){ console.log("setSearchXY -    Calling btnLoadSidebarContent_Click");}
    __doPostBack('btnLoadSidebarContent');
}

//Always do server-side postback, and let server-side logic validate values, as for databound controls,
//null or empty values mean that the control is not rendered if data source is 'empty'.
function setRouteFromToXY(hRouteFilenameID, hFromX, hFromY, hToX, hToY){
    if(consoleLog == true){ console.log("setRouteFromToXY - Start, hRouteFilenameID: '" + hRouteFilenameID + "', hRouteFromX: '" + hFromX + "', 'hRouteFromY: '" + hFromY + "', hRouteToX: '" + hToX + "', 'hRouteToY: '" + hToY + "'.");}
    $get('hRouteFilenameID').value = hRouteFilenameID;
    
    $get('hRouteFromX').value = hFromX;
    $get('hRouteFromY').value = hFromY;
    $get('hRouteToX').value = hToX;
    $get('hRouteToY').value = hToY;

    if(consoleLog == true){ console.log("setRouteFromToXY -    Calling btnLoadSidebarContent_Click");}
    __doPostBack('btnLoadSidebarContent');
}

function activeTabChanged(sender, e) {
    setErrorMessage('');
   // var CurrentTab = $get('<%=CurrentTab.ClientID%>');
    //CurrentTab.innerHTML = sender.get_activeTab().get_headerText();
    //Highlight(CurrentTab);
}

//function setWaitCursor(isOn){
//    if (isOn){
//        //document.body.style.cursor=url('../images/loading-circle-black.gif');
//        document.body.style.cursor='wait';
//    }else{
//        document.body.style.cursor='default';
//    }
//}

function normalView()
{
//    document.styleSheets[0].disabled = false;//UndoHTML.css
//    document.styleSheets[1].disabled = false;//Swaggie.css
//    document.styleSheets[2].disabled = false;//DrivingDirections.css
//    document.styleSheets[3].disabled = false;//WebResource.axd?...
//    document.styleSheets[4].disabled = false;//Style.css?cb=1242101845329 QuickMap
//    document.styleSheets[5].disabled = true;//???
    setActiveStyleSheet('UndoHTML.css');
    setActiveStyleSheet('Swaggie.css');
    setActiveStyleSheet('DrivingDirections.css');
}

function printFriendlyView()
{
//    document.styleSheets[0].disabled = false;//UndoHTML.css
//    document.styleSheets[1].disabled = true;//Swaggie.css
//    document.styleSheets[2].disabled = true;//DrivingDirections.css
//    document.styleSheets[3].disabled = false;//WebResource.axd?...
//    document.styleSheets[4].disabled = false;//Style.css?cb=1242101845329 QuickMap
//    document.styleSheets[5].disabled = false;//???
    setActiveStyleSheet('UndoHTML.css');
    setActiveStyleSheet('SwaggiePrintDrivingDirectionsOnly.css');
}

//20090512 George Petrakis Not working (at least under Firefox).
function setActiveStyleSheet(title){  // select the stylesheet
    var i, a, main;
    a = document.getElementsByTagName("link");
    for(i=0; i < document.getElementsByTagName("link").length -1; i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("href")) {
            a.disabled = true;
            if(a.getAttribute("href") == title){
                a.disabled = false;
            }
        }
    }
}
