
        var map = null;   
        var mgr = null;
        var geocoder;
        
        var gmarkers = [];

               
        var mapName = "map1";
        var latName = "lat";
        var lngName = "lng";
        var zoomName = "zoom";
        var startLat = 3.504962;
        var startLng = 27.142677;
        var startZoom = 1;
        
        var latElement, lngElement, zoomElement, mapElement, addressControls;        
                
        var reasons = [ ] ;
            reasons[G_GEO_SUCCESS] = "Success" ;
            reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value." ;
            reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address:  No corresponding geographic location could be found for the specified address." ;
            reasons[G_GEO_UNAVAILABLE_ADDRESS] = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons." ;
            reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given" ;
            reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded." ;
            reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed." ;
            reasons[G_GEO_BAD_REQUEST] = "A directions request could not be successfully parsed.";
            reasons[G_GEO_MISSING_QUERY] = "No query was specified in the input field.";
            reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
        
        //-----------------------------------------------------------------------------//
        
        function onLoad() {
    
            if (GBrowserIsCompatible()) {

                //Create a new geocoding object to be used for finding points on the map
                geocoder = new GClientGeocoder();
                
                mapElement = document.getElementById(mapName);
                latElement = document.getElementById(latName);
                lngElement = document.getElementById(lngName);
                zoomElement = document.getElementById(zoomName);
                
                addressControls = "address1,address2,city,state,postcode,country";
                                                                                                                
                if (mapElement != "undefined" && mapElement != null) {
                                        
                    if (!valididateCoords(latElement.value, lngElement.value) ) {
                        latElement.value = startLat;
                        lngElement.value = startLng;
                        zoomElement.value = startZoom;                                    
                    }
                    
                    map = new GMap2(mapElement);
                    map.setCenter(new GLatLng(latElement.value, lngElement.value), 10, G_NORMAL_MAP);
                    map.setZoom(zoomElement.value);
                    map.addControl(new GLargeMapControl());
                    map.addControl(new GMapTypeControl());
                    map.addControl(new GScaleControl());                
                    
                    setPoint(latElement.value,lngElement.value);
                                                        
                }
            } else {
                alert("Sorry, the Google Maps API is not compatible with this browser.");
            }
        }
        
        
        function addMarker(lat,lng) {
            var point, marker;
                    
            //Create a new point
            point = new GLatLng(lat,lng);

            //Create a new dragable marker at the point created
            marker = new GMarker(point); 
                                        
            //Add a new layer to the map which has the marker created above
            map.addOverlay(marker);        
           
        }
        
        
        function setPoint(lat,lng)
        {
            //This function will place a new marker on the google map which has drag functionality
            var point;
                    
            //Clear layers currently on map
            map.clearOverlays();
            
            //Create a new point
            point = new GLatLng(lat,lng);
            
            //Zoom and center the map on the point created            
            map.setCenter(point, parseInt(zoomElement.value));                                                          
            updateZoom(zoomElement.value);            
            
            //Create a new dragable marker at the point created
            marker = new GMarker(point, {draggable: true});
            
            //Add an event listener to the dragable maker (start dragging)
            GEvent.addListener(marker, "dragstart", function() {
              map.closeInfoWindow();
            });
            
            //Add an event listener to the dragable maker (end dragging)
            GEvent.addListener(marker, "dragend", function() {
              updatePoint();
            });
            
            //add the zoom control listener
            GEvent.addListener(map, "zoomend", function(oldZ, newZ) {
              updateZoom(newZ);
            });
            
            //Add a new layer to the map which has the marker created above
            map.addOverlay(marker);
            
            //Update the position of the point
            updatePoint();
        }
        
        function updateZoom(newLevel)
        {
            //update the current zoom level the map and populate form textbox controls with that zoom level
            zoomElement.value = newLevel;              
        }  
        
        function updatePoint()
        {
            //update point on the map and populate form textbox controls with coordinates of the new point
            var point = marker.getPoint();
            map.panTo(point);            
            latElement.value = point.lat();
            lngElement.value = point.lng();
        }
        
        function findLocation(type) {
            
            
        
        }
        
        function showLocation() 
        {
            //is called when you click on the Search button
            //in the form.  It geocodes the address entered into the form
            //controls and adds a marker to the map at that location found by google.
            var address = getControlAddress();
            geocoder.setBaseCountryCode("AU");
            
            //alert(zoomControl.value);
            
            geocoder.getLocations(address, addAddressToMap);
        }
        
        function getControlAddress()
        {
            //Based on controls supplied in the addressControls array 
            //build the address to search for using the google api
            
            var searchAddress = '';
            var controls = addressControls.split(",");
            
            //Build an address string based on vaules obtained from controls supplied
            //to the addressControls property
            for(i = 0; i < controls.length; i++)
            {
                searchAddress = searchAddress + ' ' + document.getElementById(controls[i]).value; 
            }
            
            //alert(searchAddress);
            
            return searchAddress;
        }
        
        function addAddressToMap(response) 
        {
           
            //This function handles the reponse returned by the google api in regard 
            //to geocodeing an address. Either shows an error or plots the point on 
            //the map
            var lat;
            var lng;
            var zoom = 14;
          
            map.clearOverlays();
            if (!response || response.Status.code != G_GEO_SUCCESS)
            {
                //Error getting result, show error message.
                var reason="Code "+response.Status.code;
                if (reasons[response.Status.code]) 
                {
                    reason = reasons[response.Status.code]
                } 
                alert('Could not find "'+ getControlAddress() + '" ' + reason);
            } 
            else 
            {
                //Plot first result returned onto the map
                place = response.Placemark[0];
                //lat = latElement.value;
                //lng = lngControl.value;
                
                lat = place.Point.coordinates[1];
                lng = place.Point.coordinates[0]; 
                
                latElement.value = startLat;
                lngElement.value = startLng;
                updateZoom(zoom);
                map.setZoom(zoom);    
                
                
                setPoint(lat,lng);      
            }
        }

        

//////////////////////////////////////////////////////////////////////////



        
        
        //-----------------------------------------------------------------------------//
        
        /*******
        
        function initialize() 
        {
          //This will setup the google map and is called when 
          //the page is loaded.
          
          map = new GMap2(document.getElementById(mapContainer));
          
          if (valididateCoords(latControl.value, lngControl.value))
          {    
            //Keep location of initial coords supplied in the textbox controls
            startLat = latControl.value;
            startLng = lngControl.value;
            //Set point on map based on inital coords
            setPoint(startLat,startLng)
            document.getElementById("GoogleReset").disabled = false
          }
          else    
          { 
            //Coordinates not supplied or are invalid, so show the default map
            map.setCenter(new GLatLng(defaultStartLat, defaultStartLng), defaultZoom); 
            document.getElementById("GoogleReset").disabled = true
          }
          //Create a new geocoding object to be used for finding points on the map
          geocoder = new GClientGeocoder();
          
          //Add map controls to the google map (zoom bar and map type buttons)
          map.addControl(new GSmallMapControl());
          map.addControl(new GMapTypeControl());
        }
        
        *******/
        
        
        //-----------------------------------------------------------------------------//
        function valididateCoords(SuppliedLat, SuppliedLng)
        {
             //Baisc validation for coordinates 
             
             //Check value was supplied
             if (SuppliedLat.length == 0 || SuppliedLng.length == 0)
                {return false;}   
             else
             {    
                //Check value is a number
                if (isNaN(SuppliedLat) || isNaN(SuppliedLng))
                    {return false;} 
                else
                {
                    //Check value is not zero
                    if (SuppliedLat == 0 || SuppliedLng == 0)
                    {return false;}
                }
             } 
            return true;      
        }  
        //-----------------------------------------------------------------------------//
        function resetPoint()
        {
            //Reset point back to the original position
            setPoint(startLat,startLng);    
        } 
        //-----------------------------------------------------------------------------//
                 
        
        
        
        
        //-----------------------------------------------------------------------------//
        

