    function select( buttonId ) {
        document.getElementById("hand_b").className="unselected";
        if (document.getElementById("shape_b") != null) { document.getElementById("shape_b").className="unselected"; }
        if (document.getElementById("marker_b") != null) { document.getElementById("marker_b").className="unselected"; }
        document.getElementById(buttonId).className="selected";
    }

    function stopEditing() {
        select( "hand_b" );
    }

    var images = [ "http://maps.google.com/mapfiles/marker.png",
                   "http://maps.google.com/mapfiles/dd-start.png",
                   "http://maps.google.com/mapfiles/dd-end.png" ];

    // creates a marker that follows the cursor, for placing business markers on map
    function follow(imageInd) {
        resetMarker();
        select("marker_b");
        var marker;
        var following = true;
        var noMore = false;

        var mouseMove = GEvent.addListener( map, "mousemove", function(cursorPoint) {
            if(!noMore) {
                marker = new GMarker(cursorPoint,{draggable:true, autoPan:false});
                map.addOverlay(marker);
                marker.setImage(images[imageInd]);
                noMore = true;
            }
            if(following) {
                marker.setLatLng(cursorPoint);
            }
        });
        var mapClick = GEvent.addListener( map, "click", function() {
            following = false;
            GEvent.removeListener(mouseMove);
            GEvent.removeListener(mapClick);

            document.outform.position.value = marker.getLatLng();
            //document.outform.lat.value = marker.getLatLng().lat();
            //document.outform.lng.value = marker.getLatLng().lng();

            GEvent.addListener(marker, "dragend", function() {
                document.outform.position.value = marker.getLatLng();
                //document.outform.lat.value = marker.getLatLng().lat();
                //document.outform.lng.value = marker.getLatLng().lng();
            })
        });
    }

    // creates a marker that follows the cursor, for placing business markers on map
    function follow2() {
        resetMarker2();
        select( "marker_b" );
        var marker;
        var following = true;
        var noMore = false;

        var mouseMove = GEvent.addListener( map, "mousemove", function(cursorPoint) {
            if(!noMore) {
                marker = new GMarker( cursorPoint,{draggable:true, autoPan:false} );
                map.addOverlay( marker );
                marker.setImage( "images/icons/markers/woodyDrop.png" );
                noMore = true;
            }
            if(following) {
                marker.setLatLng(cursorPoint);
            }
        });
        var mapClick = GEvent.addListener( map, "click", function() {
            following = false;
            GEvent.removeListener( mouseMove );
            GEvent.removeListener( mapClick );

            document.outform.lat.value = marker.getLatLng().lat();
            document.outform.lng.value = marker.getLatLng().lng();

            GEvent.addListener(marker, "dragend", function() {
                document.outform.lat.value = marker.getLatLng().lat();
                document.outform.lng.value = marker.getLatLng().lng();
            })
        });
    }

    function resetShape() {
        if ( typeof currentPoly != "undefined" ) {
            currentPoly.disableEditing();
            map.removeOverlay(currentPoly);
        }
        document.outform.polyvals.value = "";
        document.outform.polyarea.value = "";
        document.outform.polyareaDisplay.value = "";
    }

    function resetMarker() {
        select("hand_b");
        map.clearOverlays();
        document.outform.position.value = "";
    }
    function resetMarker2() {
        select("hand_b");
        map.clearOverlays();
        document.outform.lat.value = "";
        document.outform.lng.value = "";
    }

    function startShape() {
        resetShape();
        select( "shape_b" );
        currentPoly = new GPolygon( [], "#800000", 2, 0.7, "#800000", 0.2 );
        startDrawing( function() { var cell = this;
                                   var area = currentPoly.getArea();
                                 }, "#800000" );
    }

    function startDrawing(onUpdate, color) {

        map.addOverlay(currentPoly);
        currentPoly.enableDrawing();
        currentPoly.enableEditing({onEvent: "mouseover"});
        GEvent.addListener(currentPoly, "endline", function() {
            select("hand_b");
            GEvent.addListener(currentPoly, "lineupdated", function() {
                var numvertices = currentPoly.getVertexCount();
                var vertices = [];
                document.outform.polyvals.value = "";
                for ( i=0 ; i<numvertices ; i++ ) {
                    vertices[i] = currentPoly.getVertex(i);
                    document.outform.polyvals.value += vertices[i] + '@';
                }
                var polyarea = currentPoly.getArea() / 10000;
                document.outform.polyarea.value         = polyarea.toFixed(2);
                document.outform.polyareaDisplay.value  = polyarea.toFixed(2);
                document.outform.polyareaHectares.value = (polyarea*2.471).toFixed(2);
            } );

            GEvent.addListener( currentPoly, "click", function( latlng, index ) {
                if (typeof index == "number") {
                    currentPoly.deleteVertex(index);
                }
            } );
        } );
    }
