var _currentPoint = null;
var _exampleText = '';

function performGeocode()
{   
    hideGeocodeSubmit();
    
    setTimeout(submitGeocodeRequest, 100);
}

function imposeMaxLength(textBox, maxLength)
{
    return (textBox.value.length <= maxLength);
}

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode

    if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;

    return true;
}

function submitGeocodeRequest()
{
    var geocodeText = getStartAddressTextBox().value;
    if (geocodeText == '') { displayGeocodeError(_geoCodeError1);  showGeocodeSubmit(); getStartAddressTextBox().focus(); }
    else { hideGeocodeError(); doGeocode(geocodeText); }  
    
}

function doGeocode(geocodeText)
{
  geocodeText = modifyAddress(geocodeText);
  
  if (geocoder) {
    geocoder.getLatLng(
      geocodeText,
      function(point) {
        if (!point) 
        {
            displayGeocodeError(_geoCodeError2); 
            showGeocodeSubmit();
            getStartAddressTextBox().focus(); 
        } 
        else 
        {                    
            addMapMarker(point);
        }
      }
    );
  }
}
function onSaveSuccess(message, overLimit)
{
    clearAndCenterMap();    
    alert(message.replace(/<br>/g, '\r\n'));
}

function addMapMarker(point)
{       
    map.closeInfoWindow();
    map.clearOverlays(); 
    
    if (checkGeoFence(point))
    {
        map.setCenter(point, _zoomInLevel);  
        setMarkerPoint(point);    
            
        if (_turnOnSatelliteImages != null && _turnOnSatelliteImages)
            map.setMapType(G_SATELLITE_MAP);    
            
        addMarkerToMap(point);
        
        showGeocodeSubmit();
    }
    else
    {
        centerMapOnEugene();
        displayMapError('Please select a point within Eugene\'s city limits.');
        showGeocodeSubmit();
    }
}
function addMarkerToMap(point)
{
        var markerOptions = { draggable:true }; 
        theMarker = new GMarker(point, markerOptions);
        
        GEvent.addListener(theMarker, "dragstart", function() {
          map.closeInfoWindow();
        });

        GEvent.addListener(theMarker, "dragend", function() {
          setMarkerPoint(theMarker.getLatLng());
          theMarker.openInfoWindowHtml(_infoWindow1);
        });
           
        map.addOverlay(theMarker);
        
        theMarker.openInfoWindowHtml(_infoWindow2);
        theMarker.enableDragging();
}
function getAddressErrorDiv() { return getAddressErrorDiv(); }
function getGeocodeSubmit() { return document.getElementById('imgGeocoderSubmit'); }
function getMapReset() { return document.getElementById('imgReset'); }
function getGeocodeWaitIndicator() { return document.getElementById('imgGeocodeSpinner'); }
function showGeocodeSubmit() { getGeocodeSubmit().src = 'Images/nav_right_green.png'; getMapReset().src = 'Images/recycle.gif'; getGeocodeSubmit().alt = "Click to locate your Address."} //getGeocodeWaitIndicator().style.visibility = 'hidden'; getGeocodeSubmit().width = 20; }
function hideGeocodeSubmit() { getGeocodeSubmit().src = 'Images/spinner.gif'; getMapReset().src = 'Images/22_Blank.gif'; getGeocodeSubmit().alt = "Geocoding your input, please wait..."} //getGeocodeSubmit().style.width = 0; getGeocodeWaitIndicator().style.visibility = 'visible'; }
function displayGeocodeError(errorMsg) 
{ 
    var errorDiv = getAddressErrorDiv();
    errorDiv.style.visibility = 'visible';
    errorDiv.innerHTML = errorMsg;
}
function displayMapError(errorMsg)
{
    var errorDiv = getMapErrorDiv();
    errorDiv.style.visibility = 'visible';
    errorDiv.innerHTML = errorMsg;
}
function hideGeocodeError() { getAddressErrorDiv().style.visibility = 'hidden'; }
function hideMapError() { getMapErrorDiv().style.visibility = 'hidden'; }
function enterKeyListener(e) { if (!e) e = window.event; if (e && e.keyCode == 13) { performGeocode(); return false; } else { return true; } }
function resetMap() { clearAndCenterMap(); _currentPoint = null; getMarkerPointTextBox().value = ''; }
function checkForBlank(textBox) { 
                if (textBox.value == '') 
                { 
                    textBox.value = _exampleText; 
                    textBox.className = 'addressTextBoxDisabled'; 
                    
                    // If the user previously set a point - reset it 
                    if (_currentPoint != null)
                        resetMap();
                } 
            }
function resetAddressProcess()
{
    getStartAddressTextBox().value = '';
    checkForBlank(getStartAddressTextBox());
    resetMap();
}
function removeExample(textBox) { 
                if (textBox.value == _exampleText) 
                { 
                    textBox.value = ''; 
                    textBox.className = 'addressTextBox'; 
                } 
            }
function setMarkerPoint(currentPoint) 
{ 
    if (currentPoint != null) 
    {           
        hideMapError();
        
        if (checkGeoFence(currentPoint))
        {               
            _currentPoint = currentPoint;
            var pointInfo = currentPoint.lat() + '~' + currentPoint.lng(); 
            getMarkerPointTextBox().value = pointInfo; 
        }
        else
        {
            // Do we have a last "current point"?
            if (_currentPoint != null)
            {
                // Reset - our point...
                addMapMarker(_currentPoint);
            }
            else
            {
                // No last point - pan the map to the center of eugene
                centerMapOnEugene();
            }
                        
            // The user dragged the marker to a point outside of the GeoFence
            displayMapError('Please select a point within Eugene\'s city limits.');
        }
    } 
}
function dropPointOnMapCenter()
{
    p = map.getCenter();
    
    if (checkGeoFence(p))
    {
        map.closeInfoWindow();
        map.clearOverlays(); 
        setMarkerPoint(p);
        addMarkerToMap(p);
    }
    else
    {
         displayMapError('Please verify that you are within Eugene\'s city limits.');
    }
}
