var sDIVName = 'stateMap';
var gLatValue; 
var gLongValue;
var mapObject; 
var aryMarkerItems = new Array();
var aryArtistMarkers = new Array();
var aryProductMarkers = new Array();
var sDirectionEndPoint = ''
var objDirectionsLink;
var asJourneyPlannerPoints = new Array();
var iJourneyPlannerCount = 0;
var sWayPointDIV = '';
var iMaxOzLActivityCounter = 0;
var iOzLCompleteCounter = 0;
var asOzLActivities = new Array();
var iOzLActivityCounter = 0;
var aryJourneyPlannerMarkers = new Array();
var iJourneyPlannerMarkerCount = 0;

var bMapScrolling = false;


function SetDIVName(DIVName)
    {
        sDIVName = DIVName;
    }
    	        
function SetLatLong(gLat, gLong)
    {
        gLatValue = gLat;
        gLongValue = gLong;

	    load(0, false, false, false);
    }
		
function SetLatLongWithMovement(gLat, gLong)
    {
    
        gLatValue = gLat;
        gLongValue = gLong;

	    load(1, false, false, false);
    }

function SetLatLongWithMovementNoProducts(gLat, gLong) {

        gLatValue = gLat;
        gLongValue = gLong;

        load(1, false, true, true);
    }

function SetLatLongWithArtistMovement(gLat, gLong)
    {

        gLatValue = gLat;
        gLongValue = gLong;

	    load(1, true, false, false);
    }

function SetLatLongWithProductMovement(gLat, gLong) {

        gLatValue = gLat;
        gLongValue = gLong;

        load(1, false, true, false);
    }

function load(TrackMovement, IsArtist, isProduct, bypassDataLoad) 
    {

        if (GBrowserIsCompatible()) 
            {

            if (document.getElementById(sDIVName)) 
	            {
		            mapObject = new GMap2(document.getElementById(sDIVName));
		            
		            mapObject.addControl(new GMapTypeControl());
		            mapObject.addControl(new GLargeMapControl());

		            var iZoomLevel; 
		            
		            if (IsArtist == true)
		                {
		                    iZoomLevel = 4; 
		                }
                    else
                        {
                            if (isProduct == true)
                                {
                                    iZoomLevel = 4; 
                                }
                            else
                                {
                                    // Activity
                                    iZoomLevel = 6; 
                                }
                        }
		                    
		            mapObject.setCenter(new GLatLng(gLatValue, gLongValue), iZoomLevel);
		            mapObject.checkResize(); 
                        		        
    		        if (TrackMovement == 1)
    		            {
		                    // Add the event handlers that we need for moving
		                    if (IsArtist == true)
		                        {
		                            // Show the first bunch of data
		                            if (!bypassDataLoad) 
		                                {
		                                    GEvent.addListener(mapObject, "moveend", mapListenterAfterArtistMove);
		                                    getArtistMapData(mapObject); 
		                                } 
		                        }
		                    else {
		                        if (isProduct == true) {
		                            // Show the first bunch of data
		                            if (!bypassDataLoad) 
		                                {
		                                    GEvent.addListener(mapObject, "moveend", mapListenerAfterProductMove);
		                                    getProductMapData(mapObject); 
		                                } 
		                        }
		                        else {
		                            // Show the first bunch of data
		                            if (!bypassDataLoad) 
		                                {
		                                    GEvent.addListener(mapObject, "moveend", mapListenterAfterMove);
		                                    getMapData(mapObject);
		                                }
		                        }
            		        }
                        }
	            }
            }
    }

function addSingleMapPoint(gLat, gLong)
    {

        mapObject.clearOverlays();
        var markerPoint = new GLatLng(gLat, gLong);
        mapObject.addOverlay(new GMarker(markerPoint));
    }

function clearMapMarkers() {

    mapObject.clearOverlays();

    }    
    
function AddDraggableMarker(gLat, gLong, sLatDisplayFieldName, sLongDisplayFieldName)
    {
        var startPoint = new GLatLng(gLat, gLong);
        mapObject.setCenter(startPoint, 5);
        
        // Create the marker item //
       var markerPoint = new GMarker(startPoint, {draggable: true});
       GEvent.addListener(markerPoint, "dragstart", function() {});
       GEvent.addListener(markerPoint, "dragend", function() 
        { 
        
            if (document.getElementById(sLatDisplayFieldName)) { document.getElementById(sLatDisplayFieldName).innerHTML = markerPoint.getPoint().lat(); }
            if (document.getElementById(sLongDisplayFieldName)) { document.getElementById(sLongDisplayFieldName).innerHTML = markerPoint.getPoint().lng(); }
        });
       
       // Add the marker to the map //
       mapObject.clearOverlays();
       mapObject.addOverlay(markerPoint)
    }

    function AddProductWizardDraggableMarker(gLat, gLong, sLatDisplayFieldName, sLongDisplayFieldName, displayName, hidePopup) {

       
        var startPoint = new GLatLng(gLat, gLong);
        mapObject.setCenter(startPoint, 5);

        // Create the marker item //
        var markerPoint = new GMarker(startPoint, { draggable: true, bouncy: true });
        GEvent.addListener(markerPoint, "click", function() { markerPoint.openInfoWindow('<b>' + displayName + '</b><p />Please drag this marker to the correct location. <p />Remember to use the zoom controls on the left to place it accurately.'); }); 
        GEvent.addListener(markerPoint, "dragstart", function() { markerPoint.closeInfoWindow(); });
        GEvent.addListener(markerPoint, "dragend", function() {

            if (document.getElementById(sLatDisplayFieldName)) { document.getElementById(sLatDisplayFieldName).innerHTML = markerPoint.getPoint().lat(); }
            if (document.getElementById(sLongDisplayFieldName)) { document.getElementById(sLongDisplayFieldName).innerHTML = markerPoint.getPoint().lng(); }
        });

        // Add the marker to the map //
        mapObject.addOverlay(markerPoint)
        if (hidePopup != true) {
            markerPoint.openInfoWindow('<b>' + displayName + '</b><p />Please drag this marker to the correct location. <p />Remember to use the zoom controls on the left to place it accurately.');
        }            
    }


function getMapData(MapItem)
    {  
                    
        var MapBounds = MapItem.getBounds()
        var gTRLat = MapBounds.getNorthEast().lat();
        var gTRLong = MapBounds.getNorthEast().lng(); 
        var gBLLat = MapBounds.getSouthWest().lat();
        var gBLLong = MapBounds.getSouthWest().lng();
        
        // alert('Top : ' + gBLLong + ', Bottom: ' + gTRLong + ', Left: ' + gTRLat + ', Right: ' + gBLLat);
        
        var xmlHttp;
        try
            {    // Firefox, Opera 8.0+, Safari    
                 xmlHttp=new XMLHttpRequest();    
            }
        catch (e)
            {    
                 // Internet Explorer    
                 try
                    {      
                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
                    }
                catch (e)
                    {      
                        try
                            {        
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
                            }
                        catch (e)
                            {        
                                alert("Your browser does not support AJAX!");        
                                return false;        
                            }      
                     }    
             }

             xmlHttp.onreadystatechange = function() {
                 if (xmlHttp.readyState == 4) {
                     // Process the results that we get from our call
                     var sResultText = xmlHttp.responseText;

                     // Trim off the spurious script block from the reply
                     if (sResultText.indexOf("/script") > -1) {
                         sResultText = sResultText.substr(sResultText.indexOf("/script") + 8);
                     }

                     // Trim off the spaces and things
                     var sResultText = sResultText.replace(/^\s+|\s+$/g, '')

                     // Split the result up into the individual items
                     var asItemList = sResultText.split('|')

                     // Remove any overlay data that we may already have
                     mapObject.clearOverlays();
                     aryMarkerItems = new Array();

                     // Loop through the items that we have and add them to the map
                     var iLoop;

                     for (iLoop = 0; iLoop < asItemList.length; iLoop++) {
                         // Split up the individual item (ID~Title~Lat~Long~Synopsis~ImagePath)
                         asSingleItem = asItemList[iLoop].split('~');

                         if (asSingleItem[2] && asSingleItem[3]) {
                             var markerPoint = new GLatLng(asSingleItem[2], asSingleItem[3]);
                             var markerItem = new GMarker(markerPoint)
                             mapObject.addOverlay(markerItem);

                             // Add the marker to the array
                             var arySingleMarker = new Array();

                             arySingleMarker[0] = asSingleItem[2] // Lat
                             arySingleMarker[1] = asSingleItem[3] // Long
                             arySingleMarker[2] = asSingleItem[0] // ID
                             arySingleMarker[3] = asSingleItem[1] // Title
                             if (asSingleItem[4]) { arySingleMarker[4] = asSingleItem[4]; }
                             if (asSingleItem[5]) { arySingleMarker[5] = asSingleItem[5]; }

                             aryMarkerItems[iLoop] = arySingleMarker;
                         }
                     }
                     // Create the listener for clicking a marker item
                     GEvent.addListener(mapObject, "click", mapListenterMarkerClick);

                 }
             }

        if (!bMapScrolling) 
            {
                // Make the main request - pass the map bounds (split out) to the read page
// alert('Request : L=' + gBLLong + '&R=' + gTRLong + '&T=' + gTRLat + '&B=' + gBLLat);
                xmlHttp.open('GET','/GetStateMapData.asp?L=' + gBLLong + '&R=' + gTRLong + '&T=' + gTRLat + '&B=' + gBLLat + '&ms=' + new Date().getTime(),true);
                xmlHttp.send(null);  
            }

        bMapScrolling = false;
    }          

  
function getArtistMapData(MapItem)
    {  
                    
        var MapBounds = MapItem.getBounds()
        var gTRLat = MapBounds.getNorthEast().lat();
        var gTRLong = MapBounds.getNorthEast().lng(); 
        var gBLLat = MapBounds.getSouthWest().lat();
        var gBLLong = MapBounds.getSouthWest().lng();
        
        // alert('Left : ' + gBLLong + ', Right: ' + gTRLong + ', Top: ' + gTRLat + ', Bottom: ' + gBLLat);
        
        var xmlHttp;
        try
            {    // Firefox, Opera 8.0+, Safari    
                 xmlHttp=new XMLHttpRequest();    
            }
        catch (e)
            {    
                 // Internet Explorer    
                 try
                    {      
                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
                    }
                catch (e)
                    {      
                        try
                            {        
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
                            }
                        catch (e)
                            {        
                                alert("Your browser does not support AJAX!");        
                                return false;        
                            }      
                     }    
             }

             xmlHttp.onreadystatechange = function() {
                 if (xmlHttp.readyState == 4) {
                     // Process the results that we get from our call
                     var sResultText = xmlHttp.responseText;
                     
                     // Trim off the spurious script block from the reply
                     if (sResultText.indexOf("/script") > -1) {
                         sResultText = sResultText.substr(sResultText.indexOf("/script") + 8);
                     }

                     // Trim off the spaces and things
                     var sResultText = sResultText.replace(/^\s+|\s+$/g, '')

                     if (sResultText != '') {
                         // Split the result up into the individual items
                         var asItemList = sResultText.split('|')

                         // Remove any overlay data that we may already have
                         mapObject.clearOverlays();
                         aryArtistMarkers = new Array();

                         // Loop through the items that we have and add them to the map
                         var iLoop;

                         for (iLoop = 0; iLoop < asItemList.length; iLoop++) {
                             // Split up the individual item (IsArtist=ArtistID or ExhibitionID=FirstName or ExhibitionTitle=Surname or ExhibitionAddress=Lat=Long)
                             asSingleItem = asItemList[iLoop].split('=');

                             if (asSingleItem[4] && asSingleItem[5]) {
                                 var markerPoint = new GLatLng(asSingleItem[4], asSingleItem[5]);
                                 var markerItem = new GMarker(markerPoint)
                                 mapObject.addOverlay(markerItem);

                                 // Add the marker to the array
                                 var arySingleMarker = new Array();

                                 arySingleMarker[0] = asSingleItem[4] // Lat
                                 arySingleMarker[1] = asSingleItem[5] // Long
                                 arySingleMarker[2] = asSingleItem[1] // Artist / ExhibitionID
                                 arySingleMarker[3] = asSingleItem[2] // Firstname / Exhibition title
                                 arySingleMarker[4] = asSingleItem[3] // Surname / Exhibition address
                                 arySingleMarker[5] = asSingleItem[0] // IsArtist indicator

                                 aryArtistMarkers[iLoop] = arySingleMarker;
                             }
                         }
                         // Create the listener for clicking a marker item
                         GEvent.addListener(mapObject, "click", mapListenterArtistMarkerClick);
                     }
                 }
             }

        // Make the main request - pass the map bounds (split out) to the read page
        xmlHttp.open('GET','/ArtistLounge/GetArtistMapData.asp?L=' + gBLLong + '&R=' + gTRLong + '&T=' + gTRLat + '&B=' + gBLLat,true);
        xmlHttp.send(null);  
    }

function getProductMapData(MapItem) {

        var MapBounds = mapObject.getBounds()
        var gTRLat = MapBounds.getNorthEast().lat();
        var gTRLong = MapBounds.getNorthEast().lng();
        var gBLLat = MapBounds.getSouthWest().lat();
        var gBLLong = MapBounds.getSouthWest().lng();

        // alert('Left : ' + gBLLong + ', Right: ' + gTRLong + ', Top: ' + gTRLat + ', Bottom: ' + gBLLat);

        var xmlHttp;
        try {    // Firefox, Opera 8.0+, Safari    
            xmlHttp = new XMLHttpRequest();
        }
        catch (e) {
            // Internet Explorer    
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                    alert("Your browser does not support AJAX!");
                    return false;
                }
            }
        }

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                // Process the results that we get from our call
                var sResultText = xmlHttp.responseText;

                // Trim off the spurious script block from the reply
                if (sResultText.indexOf("/script") > -1) {
                    sResultText = sResultText.substr(sResultText.indexOf("/script") + 8);
                }

                // Trim off the spaces and things
                var sResultText = sResultText.replace(/^\s+|\s+$/g, '')

                if (sResultText != '') {
                    // Split the result up into the individual items
                    var asItemList = sResultText.split('|')

                    
                    // Remove any overlay data that we may already have
                    mapObject.clearOverlays();
                    aryProductMarkers = new Array();

                    // Loop through the items that we have and add them to the map
                    var iLoop;

                    for (iLoop = 0; iLoop < asItemList.length; iLoop++) {
                        // Split up the individual item (LocationItemID~LocationText~Lat~Long)
                        asSingleItem = asItemList[iLoop].split('~');

                        if (asSingleItem[2] && asSingleItem[3]) {

                            // Put the item location on the map
                            var markerPoint = new GLatLng(asSingleItem[2], asSingleItem[3]);
                            var markerItem = new GMarker(markerPoint)
                            mapObject.addOverlay(markerItem);

                            // Add the marker to the array
                            var arySingleProductMarker = new Array();

                            arySingleProductMarker[0] = asSingleItem[0] // Item ID
                            arySingleProductMarker[1] = asSingleItem[1] // Address
                            arySingleProductMarker[2] = asSingleItem[2] // Latitude
                            arySingleProductMarker[3] = asSingleItem[3] // Longitude

                            aryProductMarkers[iLoop] = arySingleProductMarker;
                        }
                    }
                    // Create the listener for clicking a marker item
                    GEvent.addListener(mapObject, "click", mapListenterProductMarkerClick);

                    // Make sure that the list of text items is up-to-date
                    showProductLocationList();
                }
            }
        }

        // Make the main request - pass the map bounds (split out) to the read page
        xmlHttp.open('GET', '/GetProductMapData.asp?L=' + gBLLong + '&R=' + gTRLong + '&T=' + gTRLat + '&B=' + gBLLat + '&PID=' + iCurrentlyViewedProductID + '&ms=' + new Date().getTime(), true);
        xmlHttp.send(null);
    }          

function mapListenterAfterMove(MapItem)
    {
        getMapData(mapObject);
    }

function mapListenterAfterArtistMove(MapItem)
    {
        getArtistMapData(mapObject);
    }

function mapListenerAfterProductMove(mapItem) {
    getProductMapData(mapItem);
}
                
function mapListenterMarkerClick(MarkerItem, Point)
    {
    
        if (MarkerItem != null)
            {
                // Find the marker that we have clicked in our list
                if (MarkerItem.getPoint())
                    {
                        var iFindMarker;
                        var objCurrentMarkerLocation = MarkerItem.getPoint();
                        var bFound = false;
                        
                        for (iFindMarker = 0; iFindMarker < aryMarkerItems.length; iFindMarker++)
                            {
                                var aryThisMarker = aryMarkerItems[iFindMarker];
                                
                                if (aryThisMarker[0] == objCurrentMarkerLocation.lat() && aryThisMarker[1] == objCurrentMarkerLocation.lng())
                                    {
                                        // Found it, show the details
                                        var sWindowText = '<strong>' + aryThisMarker[3] + '</strong><br />'
                                        
                                        if (aryThisMarker[5])
                                            {
                                                if (aryThisMarker[5] != '')
                                                    {
                                                        // We have an image to show
                                                        sWindowText += '<img src="' + aryThisMarker[5] + '" align="left" width="30" height="50" style="border:none;" alt="" title="" />';
                                                    }
                                            }


                                        if (aryThisMarker[4])
                                            {
                                                sWindowText += aryThisMarker[4];
                                            }
                                                                        
                                        sWindowText += '<p /><a href="/' + aryThisMarker[2] + '">Click here to view this activity in detail</a>'

                                        if (MarkerItem.openInfoWindowHtml)
                                            {
                                                bMapScrolling = true; 
                                                
                                                MarkerItem.openInfoWindowHtml(sWindowText, {maxWidth:230});
                                                bFound = true;
                                                
                                                // bMapScrolling = false;
                                            }
                                    }
                            }
                    }  
            
            }
                      
    }

function mapListenterArtistMarkerClick(MarkerItem, Point)
    {
        if (MarkerItem != null)
            {
                if ((MarkerItem.getPoint()) && (MarkerItem.openInfoWindowHtml))
                    {
                        // Find the marker that we have clicked in our list
                        var iFindMarker;
                        var objCurrentMarkerLocation = MarkerItem.getPoint();
                        var bFound = false;
                        
                        for (iFindMarker = 0; iFindMarker < aryArtistMarkers.length; iFindMarker++)
                            {
                                var aryThisMarker = aryArtistMarkers[iFindMarker];
                                
                                if (aryThisMarker[0] == objCurrentMarkerLocation.lat() && aryThisMarker[1] == objCurrentMarkerLocation.lng())
                                    {
                                        // Found it, show the details
                                        if (aryThisMarker[5] == 1)
                                            {
                                                // This is an artist marker //
                                                MarkerItem.openInfoWindowHtml('<b>' + aryThisMarker[3] + ' ' + aryThisMarker[4] + '</b><br /><a href="/ArtistInfo/' + aryThisMarker[2] + '">Click here to view this artist\'s profile</a>');
                                            }
                                        else
                                            {
                                                // This is an exhibition marker //
                                                MarkerItem.openInfoWindowHtml('<b>' + aryThisMarker[3] + '</b><br />' + aryThisMarker[4] + '<br /><a href="/Exhibition/' + aryThisMarker[2] + '">Click here to view this exhibition in detail</a>');
                                            }
                                                
                                        bFound = true;
                                    }
                            }
                    }
            }            
    }


function mapListenterProductMarkerClick(MarkerItem, Point) {
        if (MarkerItem != null) {
            if ((MarkerItem.getPoint()) && (MarkerItem.openInfoWindowHtml)) {
                // Find the marker that we have clicked in our list
                var iFindMarker;
                var objCurrentMarkerLocation = MarkerItem.getPoint();
                var bFound = false;

                for (iFindMarker = 0; iFindMarker < aryProductMarkers.length; iFindMarker++) {
                    var aryThisMarker = aryProductMarkers[iFindMarker];

                    if (aryThisMarker[2] == objCurrentMarkerLocation.lat() && aryThisMarker[3] == objCurrentMarkerLocation.lng()) {
                        // This is an exhibition marker //
                        MarkerItem.openInfoWindowHtml('<b>' + aryThisMarker[1] + '</b>');

                        bFound = true;
                    }
                }
            }
        }
    }    


function FlickDirectionsPanel(ChangeLinkText)
    {
    
        if (document.getElementById('DirectionsPanel')) 
            { document.getElementById('DirectionsPanel').style.display = (document.getElementById('DirectionsPanel').style.display == 'block') ? 'none' : 'block' ;  }

        if (ChangeLinkText)
            {
                if (objDirectionsLink)
                    {
                        objDirectionsLink.innerHTML = '<strong>' + ((document.getElementById('DirectionsPanel').style.display == 'block') ? 'Hide' : 'Show') + ' turn-by-turn directions to this location</strong>';
                    }
            }        
    }

function SetDirectionsEndPoint(AddressText, CaptionText)    
    {
        sDirectionEndPoint = CaptionText + '@' + AddressText;
    }

function SetDirectionsLink(objLink)
    {
        objDirectionsLink = objLink;
    }
        
function FindDirections()
    {
    
        // Build up the address that we are looking to find, and pass it on to the main routine //
        var sStartAddress = '';
        
        sStartAddress = document.frmMain.txtStreet.value + ','
        sStartAddress += document.frmMain.txtSuburb.value + ','
        sStartAddress += document.frmMain.cmbStateID[document.frmMain.cmbStateID.selectedIndex].text + ','
        sStartAddress += document.frmMain.txtPostcode.value + ', AUSTRALIA'
        SendDirectionsRequest(sStartAddress);

    }    

function SendDirectionsRequest(StartingAddress)
    {
        var directionsPanel;
        var directions;
        
        directionsPanel = document.getElementById("DrivingDirections");  
        directionsPanel.innerHTML = '';
        mapObject.setCenter(new GLatLng(gLatValue,gLongValue), 3);  
        directions = new GDirections(mapObject, directionsPanel);  
        GEvent.addListener(directions, "load" ,function() 
            {
                
            });
        GEvent.addListener(directions, "error" ,function() 
            {
                if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
                    alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
	            else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
	                alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
	            else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
	                alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);
	            else if (directions.getStatus().code == G_GEO_BAD_KEY)
	                alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);
	            else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
	                alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
	            else alert("An unknown error occurred.");    
            });
        
        directions.load(StartingAddress + " to " + sDirectionEndPoint, "en-AU");
    }

function FindDirectionsFromWaypoints()
    {
        if (asJourneyPlannerPoints.length > 1)
            {
                var directionsPanel;
                var directions;
                
                directionsPanel = document.getElementById("DrivingDirections");  
                directionsPanel.innerHTML = '';
                mapObject.setCenter(new GLatLng(gLatValue,gLongValue), 3);  
                directions = new GDirections(mapObject, directionsPanel);  
                directions.clear();
                GEvent.addListener(directions, "load" ,function() 
                    {
                        iMaxOzLActivityCounter = 0;
                        
                        document.frmMain.hidWayPoints.value = '';
                        
                        // Work out how many steps we have to do //
                        for (iRouteCount = 0; iRouteCount < directions.getNumRoutes(); iRouteCount++)
                            {
                                if (directions.getRoute(iRouteCount))
                                    {
                                        iMaxOzLActivityCounter += directions.getRoute(iRouteCount).getNumSteps();
                                    }
                            }

                        // Now the OzL activities section //
                        if (document.getElementById('OzLActivities')) 
                            {
                                document.getElementById('OzLActivities').style.display = 'block';
                                if (document.getElementById('OzlActivityProgress')) { document.getElementById('OzlActivityProgress').style.display = 'block'; }

                                // Loop through all of the steps in the route and run the query that we need to run //  
                                for (iRouteCount = 0; iRouteCount < directions.getNumRoutes(); iRouteCount++)
                                    {
                                        if (directions.getRoute(iRouteCount))
                                            {
                                                for (iStepCount = 0; iStepCount < directions.getRoute(iRouteCount).getNumSteps(); iStepCount++)
                                                    {
                                                        document.frmMain.hidWayPoints.value += iRouteCount + '=' + iStepCount + '=' + directions.getRoute(iRouteCount).getStep(iStepCount).getLatLng().lat() + '=' + directions.getRoute(iRouteCount).getStep(iStepCount).getLatLng().lng() + '|'
                                                    }                                              
                                            }
                                    }
                                                              
                            }                            

                        LoadActivitiesAJAX();
                        
                    });
                GEvent.addListener(directions, "error" ,function() 
                    {
                        if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
                            alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
	                    else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
	                        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
	                    else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
	                        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);
	                    else if (directions.getStatus().code == G_GEO_BAD_KEY)
	                        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);
	                    else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
	                        alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
	                    else alert("An unknown error occurred.");    
                    });
                
                // Get our way point array into something that google can understand //
                var asGoogleEntries = new Array();
                var iGoogleCounter = 0;
                
                for (iOutputWP = 0; iOutputWP < iJourneyPlannerCount; iOutputWP++)
                    {
                        var asSingleItem = asJourneyPlannerPoints[iOutputWP];
                        var sSingleItem = asSingleItem[0] + ',' + asSingleItem[1] + ',' + asSingleItem[2] + ' ' + asSingleItem[3] + ', AUSTRALIA';
                        
                        asGoogleEntries[iGoogleCounter] = sSingleItem;
                        iGoogleCounter++;
                    }
                    
                // Run the query //
                directions.loadFromWaypoints(asGoogleEntries,"en-AU");

                
            }
        else
            {
                if (asJourneyPlannerPoints.length == 1)
                    {
                        alert('In order to plot a route, you need at least 2 waypoints which will be the start and end of the route.'); 
                    }
                else
                    {
                        alert('You have not defined any waypoints yet');
                    }                        
            }
    }
    
function SetWaypointList(TBodyName)
    {
        sWayPointDIV = TBodyName;
    }
function CreateJourneyPlannerItem()
    {
    
        if (CheckWaypointEntry())
            {
                var avSingleWayPoint = new Array();
                
                avSingleWayPoint[0] = document.frmMain.txtStreet.value;
                avSingleWayPoint[1] = document.frmMain.txtSuburb.value;
                avSingleWayPoint[2] = document.frmMain.cmbStateID[document.frmMain.cmbStateID.selectedIndex].text;
                avSingleWayPoint[3] = document.frmMain.txtPostcode.value;
                avSingleWayPoint[4] = '' // This will be the name of the saved waypoint //
                
                asJourneyPlannerPoints[iJourneyPlannerCount] = avSingleWayPoint;
                iJourneyPlannerCount++;
                
                // Show the result //
                DisplayWayPoints();
            }
    }

function RebuildSavedJourneyPlannerItem(Street, Suburb, State, Postcode, Name)
    {
    
        var avSingleItem = new Array();
        
        avSingleItem[0] = Street;
        avSingleItem[1] = Suburb; 
        avSingleItem[2] = State;
        avSingleItem[3] = Postcode;
        avSingleItem[4] = Name;
        
        asJourneyPlannerPoints[iJourneyPlannerCount] = avSingleItem;
        iJourneyPlannerCount++;
        
        // Show the result //
        DisplayWayPoints();

    }
    
function CheckWaypointEntry()
    {

        var sErrorText = '';
        
        if (document.frmMain.txtStreet.value == '') { sErrorText = 'Street name is required.\n'; }
        if (document.frmMain.txtSuburb.value == '') { sErrorText += 'Suburb is required.\n'; }
        if (document.frmMain.txtPostcode.value == '') { sErrorText += 'Postcode is required.\n'; }

        if (sErrorText != '')
            {
                alert(sErrorText);
            }

        return (sErrorText == '');            
    }
        
function ClearJourneyPlannerItems()
    {
        asJourneyPlannerPoints.length = 0;
        iJourneyPlannerCount = 0;
    }

function RemoveJourneyPlannerItem(ItemIndex)
    {
    
        // Remove the single item
        if (asJourneyPlannerPoints[ItemIndex])
            {
                if (window.confirm('Are you sure you want to remove this item from your route?'))
                    {
                        asJourneyPlannerPoints[ItemIndex] = '';
                        
                        // Shuffle all of the items following this back by 1 subscript //
                        for (iShuffle = ItemIndex + 1; iShuffle < iJourneyPlannerCount; iShuffle ++)
                            {
                                asJourneyPlannerPoints[iShuffle - 1] = asJourneyPlannerPoints[iShuffle];
                            }

                        // Trim off the last item in the array and reset the counter //
                        asJourneyPlannerPoints.length = iJourneyPlannerCount - 1;
                        iJourneyPlannerCount--;
                        
                        // Redisplay all of the items //
                        DisplayWayPoints();                 
                    }
            }
    }    

function DisplayWayPoints()
    {
    
        if (document.getElementById(sWayPointDIV)) 
            {
                // Start by clearing what we have //
                var objTableBody = document.getElementById(sWayPointDIV);
                if (objTableBody.hasChildNodes())
                    {
                        while (objTableBody.childNodes.length >= 1)
                            {
                                objTableBody.removeChild(objTableBody.firstChild);
                            }
                    }

                
                // Now, if we have some way points, then write them all out //
                if (iJourneyPlannerCount > 0)
                    {
                        for (iDisplay = 0; iDisplay < iJourneyPlannerCount; iDisplay++)
                            {
                                CreateWayPointRowItem(iDisplay);
                            }
                    }
                else
                    {
                        // There are no items in the list //
                        var objRowItem = document.createElement('tr');
                        var objCellItem = document.createElement('td');
                        
                        objCellItem.setAttribute('colspan', 2);
                        objCellItem.setAttribute('align', 'center');
                        objCellItem.setAttribute('valign', 'middle');
                        
                        objCellItem.innerHTML = '<strong>There are no way points defined yet</strong>';
                        
                        objRowItem.appendChild(objCellItem);
                        objTableBody.appendChild(objRowItem);
                    }
            }
    }    
    
function CreateWayPointRowItem(ArraySubscript)
    {
        
        if (asJourneyPlannerPoints[ArraySubscript])
            {
                var asSingleWayPointItem = asJourneyPlannerPoints[ArraySubscript];
                
                var objRowItem = document.createElement('tr');
                
                var objPointCell = document.createElement('td');
                objPointCell.setAttribute('align', 'left');
                objPointCell.setAttribute('valign', 'center');
                objPointCell.setAttribute('width', '555');
                
                if (asSingleWayPointItem[4] == '')
                    {
                        // Show the address as entered //
                        objPointCell.innerHTML = asSingleWayPointItem[0] + ',' + asSingleWayPointItem[1] + ',' + asSingleWayPointItem[2] + ' ' + asSingleWayPointItem[3];
                    }
                else
                    {
                        // Show the name of the route //
                        objPointCell.innerHTML = asSingleWayPointItem[4];
                    }
                                        
                var objDeleteCell = document.createElement('td');
                objDeleteCell.setAttribute('align', 'left');
                objDeleteCell.setAttribute('valign', 'center');
                objDeleteCell.setAttribute('width', '75');
                
                // Do we need an up link? //
                if (ArraySubscript  > 0)
                    {
                        
                        // Create the up image //
                        var objUpImage = document.createElement('img')
                        objUpImage.src = '/Images/Up.gif';
                        objUpImage.alt = 'Move this waypoint up the list';
                        objUpImage.title = 'Move this waypoint up the list';
                        objUpImage.style.cssText = 'border:none; width:16; height:16; padding-right:2px;';
                        objUpImage.onclick = function() { MoveJourneyPlannerItem(ArraySubscript, -1) };
                        
                        objDeleteCell.appendChild(objUpImage);
                    }
                    
                // Do we need a down link? //
                if (ArraySubscript < iJourneyPlannerCount - 1)
                    {

                        // Create the down image //
                        var objDownImage = document.createElement('img')
                        objDownImage.src = '/Images/Down.gif';
                        objDownImage.alt = 'Move this waypoint down the list';
                        objDownImage.title = 'Move this waypoint down the list';
                        objDownImage.style.cssText = 'border:none; width:16; height:16; padding-right:2px; cursor:pointer;';
                        objDownImage.onclick = function() { MoveJourneyPlannerItem(ArraySubscript, 1) };
                        
                        objDeleteCell.appendChild(objDownImage);
                    }
                    
                
                // Create the remove image //
                var objRemoveImage = document.createElement('img')
                objRemoveImage.src = '/Images/PopupClose.gif';
                objRemoveImage.alt = 'Remove this waypoint from the list';
                objRemoveImage.title = 'Remove this waypoint from the list';
                objRemoveImage.style.cssText = 'border:none; width:16; height:16; cursor:pointer;';
                objRemoveImage.onclick = function() { RemoveJourneyPlannerItem(ArraySubscript) };
                objDeleteCell.appendChild(objRemoveImage);

                                   
                // Add everything to the row //
                objRowItem.appendChild(objPointCell);
                objRowItem.appendChild(objDeleteCell);
                
                // Add the row to the table //
                if (document.getElementById(sWayPointDIV)) 
                    {
                        var objTBody = document.getElementById(sWayPointDIV)
                        objTBody.appendChild(objRowItem);
                    }
                
            }
    }    
    
function MoveJourneyPlannerItem(ItemIndex, Direction)
    {
    
        if (asJourneyPlannerPoints[ItemIndex])
            {
                // Make a copy of the existing item //
                var asCopy = asJourneyPlannerPoints[ItemIndex];
                
                // Move the item we are going to replace into the current index //
                if (Direction == 1)
                    {
                        asJourneyPlannerPoints[ItemIndex] = asJourneyPlannerPoints[ItemIndex + 1]
                    }
                else
                    {
                        asJourneyPlannerPoints[ItemIndex] = asJourneyPlannerPoints[ItemIndex - 1]
                    }

                // Put our copied item into the new index //
                asJourneyPlannerPoints[ItemIndex + Direction] = asCopy;
                
                // Redisplay the items //
                DisplayWayPoints();
            }
    } 
    
function DisplayOzLActivities()
    {
        // Clear down the journey planner markers array //
        aryJourneyPlannerMarkers.length = 0;
        iJourneyPlannerMarkerCount = 0;
    
        // Hide the searching bar //
        if (document.getElementById('OzlActivityProgress')) { document.getElementById('OzlActivityProgress').style.display = 'none'; }
        
        if (document.getElementById('OzLResults')) 
            {
                // Show the results list //
                document.getElementById('OzLResults').style.display = 'block';

                var objTableBody = document.getElementById('OzLMatchingItems')
                
                // Clear down our existing list of items if we have one //
                if (objTableBody.hasChildNodes())
                    {
                        while (objTableBody.childNodes.length >= 1)
                            {
                                objTableBody.removeChild(objTableBody.firstChild);
                            }
                    }
                
                // Loop through our results and display them //
                if (asOzLActivities.length == 0)
                    {
                        // No items
                        var objRowItem = document.createElement('tr');
                        var objCellItem = document.createElement('td');
                        objCellItem.setAttribute('align', 'center');
                        objCellItem.setAttribute('valign', 'middle');
                        objCellItem.setAttribute('colspan', '2');
                        objCellItem.innerHTML = 'There were no activities found along your route';
                        
                        objRowItem.appendChild(objCellItem);
                        document.getElementById('OzLMatchingItems').appendChild(objRowItem);
                    }
                else
                    {
                        // We have some items //
                        for (iMainLoop = 0; iMainLoop < asOzLActivities.length; iMainLoop++)
                            {

                                var asSingleResult = asOzLActivities[iMainLoop];
                                
                                var objMainRow = document.createElement('tr');
                                var objTypeCell = document.createElement('td');
                                objTypeCell.setAttribute('align', 'left');
                                objTypeCell.setAttribute('valign', 'top');
                                
                                switch (parseInt(asSingleResult[2]))
                                    {
                                        case 1:
                                            objTypeCell.innerHTML = 'Activity';                                            
                                            break;
                                        case 2:
                                            objTypeCell.innerHTML = 'Event';
                                            break;
                                        case 3:
                                            objTypeCell.innerHTML = 'Artist';
                                            break;
                                        case 4:
                                            objTypeCell.innerHTML = 'Exhibition';
                                            break;
                                    }
                                
                                // The hide or show on map link //
                                var objHideShowOnMap = document.createElement('a');
                                objHideShowOnMap.id = 'HideShowLink_' + iMainLoop;
                                objHideShowOnMap.title = '0'; // Indicates not yet shown on the map
                                
                                // The hide or show image //
                                var objHideShowImage = document.createElement('img');
                                objHideShowImage.src = '/Images/Maps/Markers/AddMarker.png';
                                objHideShowImage.id = 'HideShowImage_' + iMainLoop;
                                objHideShowImage.title = 'Add this item as a marker point to the route map'
                                objHideShowImage.style.cssText = 'border:none;';
                                
                                objHideShowOnMap.onclick = function()
                                    {
                                        var iClickedId = this.id.substr(this.id.indexOf('_') + 1, this.id.substr.length);
                                        
                                        if (asOzLActivities[iClickedId])
                                            {
                                                var arySingleClickItem = asOzLActivities[iClickedId];
                                    
                                                if (this.title == '1')
                                                    {
                                                        // Already shown, so we need to hide it //
                                                        RemoveJourneyMarker(arySingleClickItem [6], arySingleClickItem [7]);
                                                        if (document.getElementById('HideShowImage_' + iClickedId)) { document.getElementById('HideShowImage_' + iClickedId).src = '/Images/Maps/Markers/AddMarker.png'; document.getElementById('HideShowImage_' + iClickedId).title = 'Add this item as a marker point to the route map'; } 
                                                        this.title = '0'; // Not yet shown //
                                                    }
                                                else
                                                    {
                                                        // Not currently shown, so we need to show it //
                                                        AddJourneyMarker(arySingleClickItem [6], arySingleClickItem [7]);
                                                        if (document.getElementById('HideShowImage_' + iClickedId)) { document.getElementById('HideShowImage_' + iClickedId).src = '/Images/Maps/Markers/RemoveMarker.png'; document.getElementById('HideShowImage_' + iClickedId).title = 'Remove this marker point from the route map'; } 
                                                        this.title = '1'; // Currently shown //
                                                    }
                                        
                                            }
                                            
                                    }
                                objHideShowOnMap.appendChild(objHideShowImage);
                                
                                // Main body //
                                var objTitleCell = document.createElement('td');
                                objTitleCell.setAttribute('align', 'left');
                                objTitleCell.setAttribute('valign', 'top');

                                var objTitleLink = document.createElement('a');
                                objTitleLink.className = 'Image16Middle';
                                
                                switch (parseInt(asSingleResult[2]))
                                    {
                                        case 1:
                                            objTitleLink.href = '/' + asSingleResult[3];                                            
                                            break;
                                        case 2:
                                            objTitleLink.href = '/Events/' + asSingleResult[3];
                                            break;
                                        case 3:
                                            objTitleLink.href = '/ArtistInfo/' + asSingleResult[3];
                                            break;
                                        case 4:
                                            objTitleLink.href = '/Exhibition/' + asSingleResult[3];
                                            break;
                                    }
                                objTitleLink.innerHTML = asSingleResult[4];
                                objTitleCell.appendChild(objTitleLink);
                                
                                // Waypoint information //
                                var objWaypoint = document.createElement('span');
                                objWaypoint.style.cssText = 'position:relative; top:-4px;';
                                objWaypoint.innerHTML += '&nbsp;(Route <strong>' + (parseInt(asSingleResult[0]) + 1) + '</strong>, waypoint <strong>' + (parseInt(asSingleResult[1]) + 1) + '</strong>)&nbsp;'; 
                                objTitleCell.appendChild(objWaypoint);
                                
                                // Add in the hide or show image after the title // 
                                objTitleCell.appendChild(objHideShowOnMap);
                                
                                objMainRow.appendChild(objTypeCell);
                                objMainRow.appendChild(objTitleCell);
                                
                                document.getElementById('OzLMatchingItems').appendChild(objMainRow);
                                
                                // Summary data //
                                if (asSingleResult[5])
                                    {
                                        var objSummaryRow = document.createElement('tr');
                                        var objBlankCell = document.createElement('td');
                                        var objSummaryCell = document.createElement('td');
                                        
                                        objBlankCell.innerHTML = '&nbsp;';
                                        objSummaryRow.appendChild(objBlankCell);

                                        objSummaryCell.setAttribute('align', 'left');
                                        objSummaryCell.setAttribute('valign', 'top');
                                        objSummaryCell.innerHTML = asSingleResult[5] + '&nbsp;';
                                        
                                        // Add the more image //
                                        var objMoreLink = document.createElement('a');
                                        
                                        switch (parseInt(asSingleResult[2]))
                                            {
                                                case 1:
                                                    objMoreLink.href = '/' + asSingleResult[3];                                            
                                                    break;
                                                case 2:
                                                    objMoreLink.href = '/Events/' + asSingleResult[3];
                                                    break;
                                                case 3:
                                                    objMoreLink.href = '/ArtistInfo/' + asSingleResult[3];
                                                    break;
                                                case 4:
                                                    objMoreLink.href = '/Exhibition/' + asSingleResult[3];
                                                    break;
                                            }

                                        var objMoreImage = document.createElement('img');
                                        objMoreImage.src = '/Images/more.gif';
                                        objMoreImage.style.cssText = 'border:none;';
                                        
                                        objMoreLink.appendChild(objMoreImage);
                                        objSummaryCell.appendChild(objMoreLink);
                                        
                                        objSummaryRow.appendChild(objSummaryCell);
                                        
                                        document.getElementById('OzLMatchingItems').appendChild(objSummaryRow);
                                    }
                                    
                                
                                
                            }
                    }                    
            }
    }
    
function LoadActivitiesAJAX()
    {
    
        // Make the call //
        var xmlHttp;
        try
            {    // Firefox, Opera 8.0+, Safari    
                 xmlHttp=new XMLHttpRequest();    
            }
        catch (e)
            {    
                 // Internet Explorer    
                 try
                    {      
                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
                    }
                catch (e)
                    {      
                        try
                            {        
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
                            }
                        catch (e)
                            {        
                                alert("Your browser does not support AJAX!");        
                                return false;        
                            }      
                     }    
             }

        xmlHttp.onreadystatechange=function()
            {
                if(xmlHttp.readyState==4)
                    {

                        // Clear down our main array //
                        asOzLActivities.length = 0;
                        iOzLCompleteCounter = 0;
                        iOzLActivityCounter = 0;
                   
                        // Process the results that we get from our call
                        var sResultText = xmlHttp.responseText;

                        // Trim off the spurious script block from the reply
                        if (sResultText.indexOf("/script")  > -1)
                            {
                                sResultText = sResultText.substr(sResultText.indexOf("/script") + 8);
                            }
                            
                        // Trim off the spaces and things
                        var sResultText = sResultText.replace(/^\s+|\s+$/g, '')

                        // Split the result up into the individual items
                        var asItemList = sResultText.split('|')

                        // Loop through the items that we have and add them to our main array
                        var iLoop;


                        for (iLoop = 0; iLoop < asItemList.length; iLoop++)
                            {
                                // Split up the individual item (RouteNumber~StepNumber~ItemType~ItemID~ItemTitle)
                                asSingleItem = asItemList[iLoop].split('~');


                                // Does this item already exist in our results //
                                var bItemOnFile = false;

                                for (iFindExisting = 0; iFindExisting < asOzLActivities.length; iFindExisting++)
                                    {
                                        var asCheckItem = asOzLActivities[iFindExisting];
                                        
                                        if ((parseInt(asCheckItem[2]) == parseInt(asSingleItem[2])) && (parseInt(asCheckItem[3]) == parseInt(asSingleItem[3])))
                                            {
                                                bItemOnFile = true;
                                            }
                                    }
                                    
                                // Add this item to our main array //
                                if (!bItemOnFile)
                                    {
                                        asOzLActivities[iOzLActivityCounter] = asSingleItem;
                                        iOzLActivityCounter++;                                                                   

                                    }
                            }


                        // Increment our overall step completed counter
                        iOzLCompleteCounter++;
                        
                        // Update the gauge
                        if (document.getElementById('ProgressOn')) { document.getElementById('ProgressOn').style.width = 400; }
                        if (document.getElementById('ProgressOff')) { document.getElementById('ProgressOff').style.width = 0; }
                        if (document.getElementById('OzLPercentage')) { document.getElementById('OzLPercentage').innerHTML = '100% complete'; }
                        
                        // Display the results //
                        DisplayOzLActivities();
                    }
                    
            }

        // Make the main request - pass the map bounds (split out) to the read page
        xmlHttp.open('GET','/GetFullActivityData.asp?Data=' + document.frmMain.hidWayPoints.value);
        xmlHttp.send(null);    
    
    }    

function JourneyMarkerIsShown(gLat, gLong, bForceShow, bForceHide, bRemoveItem)
    {
    
        var bResult = false;
        var iFindMarker = 0;
        var iMarkerSubscript = -1; 
        
        for (iFindMarker = 0; iFindMarker < aryJourneyPlannerMarkers.length; iFindMarker++)
            {

                if (aryJourneyPlannerMarkers[iFindMarker])
                    {
                        
                        if ((aryJourneyPlannerMarkers[iFindMarker].getPoint().lat() == gLat) && (aryJourneyPlannerMarkers[iFindMarker].getPoint().lng() == gLong))
                            {
                                if (bForceShow == true)
                                    {
                                        aryJourneyPlannerMarkers[iFindMarker].show();
                                    }

                                if (bForceHide == true)
                                    {
                                        aryJourneyPlannerMarkers[iFindMarker].hide();
                                        mapObject.removeOverlay(aryJourneyPlannerMarkers[iFindMarker]);
                                    }

                                iMarkerSubscript = iFindMarker;                                                                        
                                bResult = true;                            
                            }
                    }
            }

        if ((bRemoveItem == true) && (iMarkerSubscript >-1))
            {
                // Clear down the item at the removal location //
                aryJourneyPlannerMarkers[iMarkerSubscript] = '';
                
                // Shuffle everything after this marker in the array back one subscript //
                for (iShuffle = iMarkerSubscript + 1; iShuffle < aryJourneyPlannerMarkers.length; iShuffle++)
                    {
                        aryJourneyPlannerMarkers[iShuffle - 1] = aryJourneyPlannerMarkers[iShuffle];
                    }

                // Chop off the last subscript and reset the counter //
                aryJourneyPlannerMarkers.length = iJourneyPlannerMarkerCount - 1;
                iJourneyPlannerMarkerCount--;                    
            }

        return bResult;
                    
    }
    
function AddJourneyMarker(gLat, gLong)
    {
    
        if (!JourneyMarkerIsShown(gLat, gLong, true, false, false))
            {

                // Create a new point marker //
                var icon = new GIcon();
                icon.image = "/Images/Maps/Markers/Small_red.png";
                icon.shadow = "/Images/Maps/Markers/Small_red_shadow.png";
                icon.iconSize = new GSize(12, 20);
                icon.shadowSize = new GSize(22, 20);
                icon.iconAnchor = new GPoint(6, 20);
                icon.infoWindowAnchor = new GPoint(5, 1);
            

                var markerPoint = new GLatLng(gLat, gLong);
                var markerItem = new GMarker(markerPoint, icon)
                mapObject.addOverlay(markerItem);

                // Add this marker to our array //
                aryJourneyPlannerMarkers[iJourneyPlannerMarkerCount + 1] = markerItem;
                iJourneyPlannerMarkerCount++;
            }
            
    }

function RemoveJourneyMarker(gLat, gLong)
    {
    
        JourneyMarkerIsShown(gLat, gLong, false, true, true); 

    }        

function DrawWaypointsList()
    {
        DrawWaypointsList(false);
    }

function DrawRPWaypointsList()
    {
        DrawWaypointsList(true);
    }
            
function DrawWaypointsList(IsRoutePlanner)
    {
    
        if (document.getElementById('WaypointSelector')) 
            {
            
                // Remove the child elements of our span //
                var objContainer = document.getElementById('WaypointSelector')
                
                // Clear down our existing list of items if we have one //
                if (objContainer.hasChildNodes())
                    {
                        while (objContainer.childNodes.length >= 1)
                            {
                                objContainer.removeChild(objContainer.firstChild);
                            }
                    }
                
                // Run the AJAX to retrieve the list of items //
                var xmlHttp;
                try
                    {    // Firefox, Opera 8.0+, Safari    
                         xmlHttp=new XMLHttpRequest();    
                    }
                catch (e)
                    {    
                         // Internet Explorer    
                         try
                            {      
                                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
                            }
                        catch (e)
                            {      
                                try
                                    {        
                                        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
                                    }
                                catch (e)
                                    {        
                                        alert("Your browser does not support AJAX!");        
                                        return false;        
                                    }      
                             }    
                     }

                xmlHttp.onreadystatechange=function()
                    {
                        if(xmlHttp.readyState==4)
                            {

                                // Process the results that we get from our call
                                var sResultText = xmlHttp.responseText;

                                // Trim off the spurious script block from the reply
                                if (sResultText.indexOf("/script")  > -1)
                                    {
                                        sResultText = sResultText.substr(sResultText.indexOf("/script") + 8);
                                    }
                                    
                                // Trim off the spaces and things
                                var sResultText = sResultText.replace(/^\s+|\s+$/g, '')

                                // Do we actually have any items //
                                if (sResultText != '')
                                    {
                                        // Split the result up into the individual items
                                        var asItemList = sResultText.split('|')

                                        // Do we need to create a combo to hold the results //
                                        var objWaypointCombo;
                                        
                                        if (asItemList.length > 0)
                                            {
                                                objWaypointCombo = document.createElement('select');
                                                objWaypointCombo.setAttribute('size', '1');
                                                objWaypointCombo.setAttribute('id', 'WaypointCombo');
                                            }
                                            
                                        // Create a 'please select' item in the combo //
                                        var objPSElement = document.createElement('option');
                                        var objPSTextItem = document.createTextNode('Please select ...');
                                        objPSElement.appendChild(objPSTextItem);
                                        objPSElement.setAttribute('value', -1);
                                        
                                        objWaypointCombo.appendChild(objPSElement);

                                        // Loop through the items that we have and add them to our main array
                                        var iLoop;

                                        for (iLoop = 0; iLoop < asItemList.length; iLoop++)
                                            {
                                                // Split up the individual item (WaypointID~WaypointName)
                                                asSingleItem = asItemList[iLoop].split('~');

                                                // Create an option item for the selection //
                                                var objWPElement = document.createElement('option');
                                                var objWPTextItem = document.createTextNode(asSingleItem[1]);
                                                objWPElement.appendChild(objWPTextItem);
                                                objWPElement.setAttribute('value', asSingleItem[0]);
                                                
                                                objWaypointCombo.appendChild(objWPElement);

                                            }

                                        // Attach the combo to the form //
                                        objContainer.appendChild(objWaypointCombo);
                                        
                                        // Add a link to use the selected item //
                                        var objSpace = document.createElement('span');
                                        objSpace.innerHTML = '&nbsp;';
                                        objContainer.appendChild(objSpace);
                                        
                                        var objUseWPLink = document.createElement('a');
                                        objUseWPLink.href = '#';
                                        
                                        if (IsRoutePlanner)
                                            {
                                                objUseWPLink.onclick = function() { UseRPSingleSavedWayPoint(); return false; }; 
                                            }
                                        else
                                            {                                            
                                                objUseWPLink.onclick = function() { UseSingleSavedWayPoint(); return false; }; 
                                            }                                                
                                        objUseWPLink.innerHTML = 'Use this waypoint';
                                        objContainer.appendChild(objUseWPLink);
                                    }                                        
                                else
                                    {
                                        objContainer.innerHTML = '<strong>You do not currently have any saved waypoints</strong>';
                                    }                                    
                                                                                                    
                            }
                            
                    }

                // Make the main request //
                xmlHttp.open('GET','/GetSavedWaypoints.asp?UID=' + document.frmMain.hidUID.value + '&ms=' + new Date().getTime(), true);
                xmlHttp.send(null);    
                            
            }
    }    

function UseSingleSavedWayPoint()
    {
        UseSelectedSavedWayPoint(false);
    }

function UseRPSingleSavedWayPoint()
    {
        UseSelectedSavedWayPoint(true);
    }
            
function UseSelectedSavedWayPoint(IsRoutePlanner)
    {
    
        if (document.getElementById('WaypointCombo'))
            {
            
                var objSelector = document.getElementById('WaypointCombo');
                
                if (parseInt(objSelector[objSelector.selectedIndex].value) == -1)
                    {
                        // We are looking at the please select item //
                        alert('Please select an item from the drop down list first.');
                    }
                else
                    {
                        // We have selected a valid item, so read in the details, populate the field and run the route finder //
                        var xmlHttp;
                        try
                            {    // Firefox, Opera 8.0+, Safari    
                                 xmlHttp=new XMLHttpRequest();    
                            }
                        catch (e)
                            {    
                                 // Internet Explorer    
                                 try
                                    {      
                                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
                                    }
                                catch (e)
                                    {      
                                        try
                                            {        
                                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
                                            }
                                        catch (e)
                                            {        
                                                alert("Your browser does not support AJAX!");        
                                                return false;        
                                            }      
                                     }    
                             }

                        xmlHttp.onreadystatechange=function()
                            {
                                if(xmlHttp.readyState==4)
                                    {

                                        // Process the results that we get from our call
                                        var sResultText = xmlHttp.responseText;

                                        // Trim off the spurious script block from the reply
                                        if (sResultText.indexOf("/script")  > -1)
                                            {
                                                sResultText = sResultText.substr(sResultText.indexOf("/script") + 8);
                                            }
                                            
                                        // Trim off the spaces and things
                                        var sResultText = sResultText.replace(/^\s+|\s+$/g, '')

                                        // Do we actually have any items //
                                        if (sResultText != '')
                                            {
                                                // Split the result up into the individual items
                                                var asItemList = sResultText.split('|')
                                                
                                                if (asItemList[0])
                                                    {
                                                        // We have an item, so populate all of the fields that we need to deal with //
                                                        var asSingleItem = asItemList[0].split('~');

                                                        if (!IsRoutePlanner) 
                                                            {                                                            
                                                
                                                                document.frmMain.txtStreet.value = asSingleItem[0];
                                                                document.frmMain.txtSuburb.value = asSingleItem[1];
                                                                document.frmMain.txtPostcode.value = asSingleItem[3];
                                                                
                                                                // Set the selected index of the state combo //
                                                                for (iFindState = 0; iFindState < document.frmMain.cmbStateID.length; iFindState++)
                                                                    {
                                                                        if (parseInt(document.frmMain.cmbStateID[iFindState].value) == parseInt(asSingleItem[2]))
                                                                            {
                                                                                document.frmMain.cmbStateID.selectedIndex = iFindState;                                        
                                                                            }
                                                                    }

                                                                // Now that everything is populated, we can run the main search // 
                                                                FindDirections();                                                            
                                                            }
                                                        else
                                                            {
                            
                                                                // We are in the route planner, so we need to create a new waypoint item from the information that we have //
                                                                var avSingleWayPoint = new Array();
                                                                
                                                                avSingleWayPoint[0] = asSingleItem[0];
                                                                avSingleWayPoint[1] = asSingleItem[1];
                                                                avSingleWayPoint[2] = GetStateDesc(asSingleItem[2]);
                                                                avSingleWayPoint[3] = asSingleItem[3];
                                                                avSingleWayPoint[4] = document.getElementById('WaypointCombo')[document.getElementById('WaypointCombo').selectedIndex].text;
                                                                
                                                                asJourneyPlannerPoints[iJourneyPlannerCount] = avSingleWayPoint;
                                                                iJourneyPlannerCount++;
                                                                
                                                                // Show the result //
                                                                DisplayWayPoints();
                                                                                
                                                            }                                                            
                                                    }
                                            }                                        
                                        else
                                            {
                                                // Got a blank back from the AJAX, item must have been removed from the database or something //
                                                alert('We are unable to lookup the details of your selected waypoint at the moment. Perhaps you have removed it using MyOzLeisure. If so, please refesh this page and try again. If not, please try again in a few seconds. If you are still having difficulties, please contact us for assistance.');
                                            }                                    
                                                                                                            
                                    }
                                    
                            }

                        // Make the main request //
                        xmlHttp.open('GET','/GetWaypointItemData.asp?UID=' + document.frmMain.hidUID.value + '&WPID=' + parseInt(objSelector[objSelector.selectedIndex].value) + '&ms=' + new Date().getTime(), true);
                        xmlHttp.send(null); 
                                        
                    }
                
            }
    
    }    

    
function SaveWaypoint()
    {
    
        // Make sure that the fields have been completed //
        var sErrorText = '';
        
        if (!IsCompleted(document.frmMain.txtStreet)) { sErrorText += 'Street is a required field.\n'; }
        if (!IsCompleted(document.frmMain.txtSuburb)) { sErrorText += 'Suburb is a required field.\n'; }
        if (!IsCompleted(document.frmMain.txtPostcode)) { sErrorText += 'Postcode is a required field.\n'; }
        
        if (sErrorText != '')
            {
                // Problems here //
                alert(sErrorText);
            }
        else
            {
                // All looks good //
                if (document.frmMain.hidUID.value != '')
                    {
                        // We are currently logged in //
                        if (document.getElementById('WaypointNameSelector'))
                            {

                                // Show the selector panel //
                                document.getElementById('WaypointNameSelector').style.display = 'block';
                                
                            }
                        
                    }
                else
                    {
                        // We are not currently logged in, so head off and do that now //
                        document.frmMain.hidLocationData.value = document.frmMain.txtStreet.value + '|' + document.frmMain.txtSuburb.value + '|' + document.frmMain.cmbStateID[document.frmMain.cmbStateID.selectedIndex].value + '|' + document.frmMain.txtPostcode.value;
                        FormSubmit('WaypointLogin')
                    }                    
            }
    }    

function DoSaveWP()
    {
        DoSaveWaypointItem(false);
    }

function DoRPSaveWP()        
    {
        DoSaveWaypointItem(true);
    }
    
function DoSaveWaypointItem(IsRoutePlanner)
    {
        // Called from the waypoint name selector - does the actual saving //
        if (document.frmMain.txtWPName.value != '')
            {
                // Show that the save is in progress //
                if (document.getElementById('WPSaveProgress')) { document.getElementById('WPSaveProgress').innerHTML = 'Saving your waypoint, please wait ...'; }
                
                // Make the AJAX call to do the saving //
                var xmlHttp;
                try
                    {    // Firefox, Opera 8.0+, Safari    
                         xmlHttp=new XMLHttpRequest();    
                    }
                catch (e)
                    {    
                         // Internet Explorer    
                         try
                            {      
                                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
                            }
                        catch (e)
                            {      
                                try
                                    {        
                                        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
                                    }
                                catch (e)
                                    {        
                                        alert("Your browser does not support AJAX!");        
                                        return false;        
                                    }      
                             }    
                     }

                xmlHttp.onreadystatechange=function()
                    {
                        if(xmlHttp.readyState==4)
                            {

                                // Reload the combo box on the screen with the updated details //
                                if (IsRoutePlanner)
                                    {
                                        DrawRPWaypointsList();
                                    }
                                else
                                    {
                                        DrawWaypointsList();
                                    }
                                    
                                // Save is complete - remove the name div //
                                if (document.getElementById('WPSaveProgress')) { document.getElementById('WPSaveProgress').innerHTML = 'Your waypoint has been saved.'; }
                                if (document.getElementById('WaypointNameSelector')) { document.getElementById('WaypointNameSelector').style.display = 'none'; }

                            }
                            
                    }

                // Make the main request //
                xmlHttp.open('GET','/ModifyWaypoint.asp?Action=1&UID=' + document.frmMain.hidUID.value + '&WPN=' + document.frmMain.txtWPName.value + '&STR=' + document.frmMain.txtStreet.value + '&SUB=' + document.frmMain.txtSuburb.value + '&SID=' + document.frmMain.cmbStateID[document.frmMain.cmbStateID.selectedIndex].value + '&PC=' + document.frmMain.txtPostcode.value + '&ms=' + new Date().getTime(), true);
                xmlHttp.send(null);                 
                
            }
    }    

function SetLocationData(AllData)
    {
    
        var asLocationData = AllData.split('|')
        
        if (asLocationData[0]) { document.frmMain.txtStreet.value = asLocationData[0]; }
        if (asLocationData[1]) { document.frmMain.txtSuburb.value = asLocationData[1]; }
        if (asLocationData[2]) 
            { 
                for (iFindIndex = 0; iFindIndex < document.frmMain.cmbStateID.length; iFindIndex++)
                    {
                        if (parseInt(asLocationData[2]) == parseInt(document.frmMain.cmbStateID[iFindIndex].value))
                            {
                                document.frmMain.cmbStateID.selectedIndex = iFindIndex;
                            }
                    }
            }
        if (asLocationData[3]) { document.frmMain.txtPostcode.value = asLocationData[3]; }
    }    
    
function GetStateDesc(StateID)
    {
    
        var sStateText = ''
        
        switch(parseInt(StateID))
            {
                case 1: 
                    sStateText = 'ACT';
                    break;
                case 2:
                    sStateText = 'NT';
                    break;
                case 3:
                    sStateText = 'NSW';
                    break;
                case 4:
                    sStateText = 'QLD';
                    break;
                case 5:
                    sStateText = 'SA';
                    break;
                case 6:
                    sStateText = 'TAS';
                    break;
                case 7:
                    sStateText = 'VIC';
                    break;
                case 8:
                    sStateText = 'WA';
                    break;
            }
            
         return sStateText;
    }

function RPDoLogin()
    {
    
        if (asJourneyPlannerPoints.length > 0)
            {
                if (document.frmMain.hidEnteredRouteItems)
                    {
                        // We have points stored, and a place to write them away temporarily //
                        var sStoredPoints = '';
                        
                        for (iStorePoints = 0; iStorePoints < asJourneyPlannerPoints.length; iStorePoints++)
                            {
                                sStoredPoints += (sStoredPoints != '')? '|' : ''
                                sStoredPoints += asJourneyPlannerPoints[iStorePoints].join('~');    
                            }
                            
                        document.frmMain.hidEnteredRouteItems.value = sStoredPoints;
                    }
            }

        // Do we have a single item entered //
        document.frmMain.hidRPSaveItem.value = document.frmMain.txtStreet.value + '|' + document.frmMain.txtSuburb.value + '|' + document.frmMain.cmbStateID[document.frmMain.cmbStateID.selectedIndex].text + '|' + document.frmMain.txtPostcode.value;

        // Do the submit //
        FormSubmit('RoutePlannerAddItem');                            

    }    

function SetLocationFields(Street, Suburb, StateName, Postcode)
    {
    
        if (document.frmMain.txtStreet) { document.frmMain.txtStreet.value = Street; }
        if (document.frmMain.txtSuburb) { document.frmMain.txtSuburb.value = Suburb; }
        if (document.frmMain.txtPostcode) { document.frmMain.txtPostcode.value = Postcode; }
        if (document.frmMain.cmbStateID)
            {
            
                for (iFindItem = 0; iFindItem < document.frmMain.cmbStateID.lenght; iFindItem++)
                    {
                        if (document.frmMain.cmbStateID[iFindItem].text == StateName)
                            {
                                document.frmMain.cmbStateID.selectedIndex = iFindItem;
                            }
                    }
            }
    }

function setMapCentre(lat, lng) {

    mapObject.setCenter(new GLatLng(lat, lng), mapObject.getZoom());

}

function setMapZoom(zoomLevel) {

    mapObject.setZoom(zoomLevel); 
}    
