// aimsClick.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*               dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*               aimsLayers.js, aimsDHTML.js
*               aimsNavigation.js
*/

aimsClickPresent=true;

var onOVArea = false;

// Global vars to save mouse position
var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var zleft=0;
var zright=0;
var ztop=0;
var zbottom=0;

var totalMeasure=0;
var currentMeasure=0;
var lastTotMeasure=0;

// variables for interactive clicks
var clickCount = 0;
var     clickPointX = new Array();
var clickPointY = new Array();
var clickMeasure = new Array();
        // type - 1=Measure; 2=SelectLine ; 3=SelectPolygon
var clickType = 1;

var shapeSelectBuffer = false;

var panning=false;
var zooming=false;
var selectBox=false;
var blankImage = "images/map.gif";

var leftButton =1;
var rightButton = 2;


//qualite eau
var qualite=false;

if (isNav) {
        leftButton = 1;
        rightButton = 3;
}


/*  *****************************************************
*       Point click functions
*       used by Measure and Select by Line/Polygon
*       *****************************************************
*/

// put a point at click and add to clickCount
function clickAddPoint() {
        var theX = mouseX;
        var theY = mouseY;
        getMapXY(theX,theY);
        clickPointX[clickCount]=mapX;
        clickPointY[clickCount]=mapY;
        clickCount += 1;
        selectCount=0;
        totalMeasure = totalMeasure + currentMeasure;
        clickMeasure[clickCount]=totalMeasure;
        legendTemp=legendVisible;
        legendVisible=false;
        var theString = writeXML();
        var theNum = 99;
        sendToServer(imsURL,theString,theNum);

}

// zero out all clicks in clickCount
function resetClick() {
        var c1 = clickCount;
        clickCount=0;
        clickPointX.length=1;
        clickPointY.length=1;
        currentMeasure=0;
        totalMeasure=0;
        lastTotMeasure=0;
        clickMeasure.length=1;
        selectCount=0;
        
        legendTemp=legendVisible;
        legendVisible=false;
        var theString = writeXML();
        var theNum = 99;
        sendToServer(imsURL,theString,theNum);
                
        if (toolMode==20) updateMeasureBox();

}       

// remove last click from clickCount
function deleteClick() {
        var c1 = clickCount;
        clickCount=clickCount-1;
        selectCount=0;
        if (clickCount<0) clickCount=0;
        if (clickCount>0) {
                totalMeasure = clickMeasure[clickCount]
                clickPointX.length=clickCount;
                clickPointY.length=clickCount;
                clickMeasure.length=clickCount;
                
        } else {
                totalMeasure=0;
                clickMeasure[0]=0;
        }
        currentMeasure=0;
        if (c1>0) {
                legendTemp=legendVisible;
                legendVisible=false;
                var theString = writeXML();
                var theNum = 99;
                sendToServer(imsURL,theString,theNum);
        }
        
}


function clickFunctionParam(toolName,param1) {
        if (hasLayer("measureBox"))
                hideLayer("measureBox");
                
        switch(toolName) {
               case "listeReleve":
                        parent.TOC2Frame.TOCFrame.setActiveNomLayer('Donnees anterieures');
                        var theString = LayerFields[0].substring(0,LayerFields[0].lastIndexOf(".")) + ".NO_STATION = " + dQuote + param1 + dQuote;
                        //alert("theString:  " + theString + "\nLayerName[ActiveLayerIndex]:   " + LayerName[ActiveLayerIndex]);
                        sendQueryStringMode(theString,1009);
                        break;  
                case "Profil_financement":
                        parent.TOC2Frame.TOCFrame.setActiveNomLayer('Profil_financement_lien_perma');
                        var theString = LayerFields[0].substring(0,LayerFields[0].lastIndexOf(".")) + ".NO_FINANCEMENT = " + dQuote + param1 + dQuote;
                        //alert("theString:    " + theString);
                        sendQueryStringMode(theString,1009);
                        break;
               default:
                        alert(msgList[51]);
        }
        modeName=modeBlurb;
        if (useModeFrame) {
                writeModeFrame(modeBlurb);
        } else if ((drawFloatingMode) && (modeLayerOn)) {
                writeModeLayers(modeBlurb);
        } else if ((modeRefreshMap) && (drawModeOnMap)) {
                sendMapXML();
        }
}

//keep track of currently selected tool, and display it to user
// set the imsMap cursor tool
function clickFunction (toolName) {
        if (hasLayer("measureBox"))
                hideLayer("measureBox");        
        switch(toolName) {
        case "aide":    
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                // geocode mode - requires aimsGeocode.js
                hideLayer("measureBox");
                modeBlurb = modeList[21];
                showGeocode=false;
                showBuffer=false;
                //var swidth = screen.width-50;
                //var sheight = screen.height-100;
                swidth=600
                sheight=500
                var top=(screen.height-sheight)/2;
                var left=(screen.width-swidth)/2;
                Win1 = open("aideinternet.htm","AideWindow","width="+ swidth +",height="+ sheight +",top=0,left=" + left + ",scrollbars=yes,resizable=yes");
                
                return;
                break
        // Zooming functions
        case "zoomin":
                // zoom in mode
                toolMode = 1;
                panning=false;
                selectBox=false;
                if (isIE)       {
                        document.all.theTop.style.cursor = "crosshair";
                        theCursor = document.all.theTop.style.cursor;
                }
                modeBlurb = modeList[0];
                break
        case "zoom25":
                // zoom 25%
                zoomScale(0.25);
                modeBlurb = modeList[21];
                break
        case "zoom50":
                // zoom 50%
                zoomScale(0.5);
                modeBlurb = modeList[22];
                break
        case "zoom75":
                // zoom 50%
                zoomScale(0.75);
                modeBlurb = modeList[23];
                break
        case "zoomout":
                // zoom out mode
                toolMode = 2;
                panning=false;
                selectBox=false;
                if (isIE)       {
                        document.all.theTop.style.cursor = "crosshair";
                        theCursor = document.all.theTop.style.cursor;
                }
                modeBlurb = modeList[1];
                break
        case "zoomlast":
                zoomBack();
                panning=false;
                zooming=false;
                selectBox=false;
                break
        case "zoomactive":
                var q = LayerExtent[ActiveLayerIndex].split("|");
                panning=false;
                zooming=false;
                selectBox=false;
                var l = parseFloat(setDecimalString(q[0]));
                var b = parseFloat(setDecimalString(q[1]));
                var r = parseFloat(setDecimalString(q[2]));
                var t = parseFloat(setDecimalString(q[3]));
                var w = r-l;
                var h = t-b;
                // add a bit of a margin around the layer
                var wm = w * (5/100);
                var hm = h * (5/100);
                l = l - wm;
                r = r + wm;
                b = b - hm;
                t = t + hm;
                zoomToEnvelope(l,b,r,t);
                break
        case "fullextent":
                fullExtent();
                break
                
        // Pan functions
        case "pan":
                // pan mode
                toolMode = 3;
                
                zooming=false;
                selectBox=false;
                if (isIE)       {
                        document.all.theTop.style.cursor = "move";
                        theCursor = document.all.theTop.style.cursor;
                }
                modeBlurb = modeList[2];
                break

        // Identify-Hyperlink functions
        case "identify":
                // identify mode - layer attributes - requires aimsIdentify.js
                
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                if (canQuery) {
                        toolMode = 4;
                        
                        if (isIE)       {
                                document.all.theTop.style.cursor = "crosshair";
                                theCursor = document.all.theTop.style.cursor;
                        }
                        modeBlurb = modeList[3];
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
                hideLayer("measureBox");
                break

        // Identify-Hyperlink functions
        case "info_centroide":          
                qualite=false;
                parent.TOC2Frame.TOCFrame.setActiveNomLayer('Role_evaluation_2.5k');
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                if (canQuery) {
                	//alert("aimsclick.js  info_centroide juste avant le toolmode=4");
                        toolMode = 4;
                        if (isIE)       {
                                document.all.theTop.style.cursor = "crosshair";
                                theCursor = document.all.theTop.style.cursor;
                        }
                        modeBlurb = modeList[3];
                } else {
                        alert(msgList[46]);
                }
                
        
                showGeocode=false;
                if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
                hideLayer("measureBox");
                break

        case "identifyall":
                // identify drill mode
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                toolMode = 5;
                if (canQuery) {
                        if (isIE)       {
                                document.all.theTop.style.cursor = "crosshair";
                                theCursor = document.all.theTop.style.cursor;
                        }
                        modeBlurb = modeList[20]; // identify visible features
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                drawSelectBoundary=false;
                if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
                hideLayer("measureBox");
                break
                
        case "qualite_eau":
		toolMode = 4;
		qualite=true;
		//alert("Click ds qualite_eau");
		break
		
        case "hyperlink":
                // hyperlink mode - requires aimsIdentify.js
                var isOk = false;
                var j=-1;
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                toolMode = 15;
                modeBlurb = modeList[9];
                showGeocode=false;
                if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
                hideLayer("measureBox");
                var isOk = checkHyperLinkLayer(ActiveLayerIndex)
                if (isOk) {
                        if (canQuery) {
                                
                                
                                if (isIE)       {
                                        document.all.theTop.style.cursor = "crosshair";
                                        theCursor = document.all.theTop.style.cursor;
                                }
                                
                        } else {
                                alert(msgList[46]);
                        }
                } else {
                        currentHyperLinkLayer="";
                        currentHyperLinkField="";
                        alert(msgList[47]);

                }
                break
                
        case "hyperlinkany":
                // hyperlink mode - requires aimsIdentify.js
                var j=-1;
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                toolMode = 30;
                modeBlurb = modeList[9];
                showGeocode=false;
                if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
                hideLayer("measureBox");
                        if (canQuery) {
                                
                                
                                if (isIE)       {
                                        document.all.theTop.style.cursor = "crosshair";
                                        theCursor = document.all.theTop.style.cursor;
                                }
                                
                        } else {
                                alert(msgList[46]);
                        }
                break
                
        // Measure-Unit function
        case "measure":
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                if (clickCount>0) {
                        if (totalMeasure==0) resetClick();
                }
                toolMode = 20;
                if (isIE)       {
                        document.all.theTop.style.cursor = "crosshair";
                        theCursor = document.all.theTop.style.cursor;
                }
                modeBlurb = modeList[12];
                if (clickType==1) {
                                showLayer("measureBox");
                                updateMeasureBox();
                }
                showGeocode=false;
                break
                
        case "setunits":
                if (useTextFrame) {
                        parent.TextFrame.location = "setUnits.htm";
                } else {
                        window.open((appDir + "setUnits.htm"),"OptionWindow","width=575,height=120,scrollbars=yes,resizable=yes");
                }
                break
                
        // Graphic Selection functions
        case "shape":
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                toolMode = 21;
                if (isIE)       {
                        document.all.theTop.style.cursor = "crosshair";
                        theCursor = document.all.theTop.style.cursor;
                }

                modeBlurb = modeList[13];
        
                showGeocode=false;

                hideLayer("measureBox");
                break
                
        case "selectbox":
                panning=false;
                zooming=false;
                // select mode - requires aimsSelect.js
                if (canQuery) {
                        toolMode = 10;
                        queryTool=0;
                        clickCount=0;
                        showBuffer=false;
                        if (isIE)       {
                                document.all.theTop.style.cursor = "crosshair";
                                theCursor = document.all.theTop.style.cursor;
                        }

                        modeBlurb = modeList[4];
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
                hideLayer("measureBox");
                break
                
        case "selectpoint":
                panning=false;
                zooming=false;
                shapeSelectBuffer = false;
                // select mode - requires aimsSelect.js
                if (canQuery) {
                        toolMode = 11;
                        queryTool=0;
                        clickCount=0;
                         resetClick();
                        if (isIE)       {
                                document.all.theTop.style.cursor = "hand";
                                theCursor = document.all.theTop.style.cursor;
                        }

                        modeBlurb = modeList[5];
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                showBuffer=false;
                hideLayer("measureBox");
                break
                
        case "selectline":
                panning=false;
                zooming=false;
                shapeSelectBuffer = false;
                // select mode - requires aimsSelect.js
                if (canQuery) {
                        toolMode = 12;
                        queryTool=0;
                        if (isIE)       {
                                document.all.theTop.style.cursor = "crosshair";
                                theCursor = document.all.theTop.style.cursor;
                        }
                        hideLayer("measureBox");
                        if (useTextFrame) {
                                parent.TextFrame.document.location= appDir + "selectline.htm";
                        } else {
                                Win1 = open("selectline.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
                        }
                        modeBlurb = modeList[6];
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                showBuffer=false;
                break
                
        case "selectpoly":
                panning=false;
                zooming=false;
                shapeSelectBuffer = false;
                // select mode - requires aimsSelect.js
                if (canQuery) {
                        toolMode = 13;
                        queryTool=0;
                        if (isIE)       {
                                document.all.theTop.style.cursor = "crosshair";
                                theCursor = document.all.theTop.style.cursor;
                        }
                        hideLayer("measureBox");
                        if (useTextFrame) {
                                parent.TextFrame.document.location= appDir + "selectpoly.htm";
                        } else {
                                Win1 = open("selectpoly.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
                        }
                        modeBlurb = modeList[7];
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                showBuffer=false;
                break
                
        case "selectshape":
                panning=false;
                zooming=false;
                shapeSelectBuffer = false;
                // select mode - requires aimsSelect.js
                if (canQuery) {
                        toolMode = 16;
                        queryTool=0;
                        if (isIE)       {
                                document.all.theTop.style.cursor = "crosshair";
                                theCursor = document.all.theTop.style.cursor;
                        }
                        hideLayer("measureBox");
                                usePolygone=true;
                                parent.TOC2Frame.location = parent.TOC2Frame.location;
                                setTimeout("parent.TOC2Frame.TOCFrame.document.location='" + appDir + "toc.htm'",100);
                        modeBlurb = modeList[8];
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                showBuffer=false;
                break
        case "buffershape":
                panning=false;
                zooming=false;
                
                // interactive shape buffer - not implemented
                if (canQuery) {
                        toolMode = 17;
                        queryTool=0;
                        shapeSelectBuffer = true;
                        if (isIE)       {
                                document.all.theTop.style.cursor = "crosshair";
                                theCursor = document.all.theTop.style.cursor;
                        }
                        hideLayer("measureBox");
                        if (useTextFrame) {
                                parent.TextFrame.document.location= appDir + "shapeBuffer.htm";
                        } else {
                                Win1 = open("shapeBuffer.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
                        }
                        modeBlurb = modeList[11];
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                showBuffer=false;
                break
        // Geocode Function
        case "geocode":
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                // geocode mode - requires aimsGeocode.js
                hideLayer("measureBox");
                modeBlurb = modeList[14];
                setupGeocode(); 
                break
                
        case "geocode_perso":
                tpTOC=false;
                //alert(tpTOC);
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                // geocode mode - requires aimsGeocode.js
                hideLayer("measureBox");
                modeBlurb = modeList[14];
                setupGeocode();
                parent.TOC2Frame.TOCTemp.location="rech_fram.htm" 
                break
        
        // Fonction de recherche numéro de lot
        case "lot":
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                // geocode mode - requires aimsGeocode.js
                hideLayer("measureBox");
                modeBlurb = modeList[21];
                showGeocode=false;
                showBuffer=false;
                Win1 = open("rechLot.htm","QueryWindow","width=225,height=140,scrollbars=yes,resizable=yes");
                break
        // Fonction de recherche numéro de matricule
        case "matricule":
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                // geocode mode - requires aimsGeocode.js
                hideLayer("measureBox");
                modeBlurb = modeList[21];
                showGeocode=false;
                showBuffer=false;
                Win1 = open("rechMat.htm","QueryWindow","width=300,height=160,scrollbars=yes,resizable=yes");
                break
        case "query":
                parent.TOC2Frame.TOCFrame.setActiveNomLayer('Points_interet_25k');
                // query mode - requires aimsQuery.js
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                queryStartRecord=1;
                queryTool=51;
                if ((canQuery) && (ActiveLayerIndex!=0)) {      
                        LayerFields.length=1;
                        LayerFieldType.length=1;
                        LayerFieldCount=0;
                        toolMode=8;
                        modeBlurb=modeList[15];
                        
                        fieldIndex=0;
                        setQueryString="";
                        hideLayer("measureBox");
                        queryForm();
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                showBuffer=false;
                break
                
        case "storedquery":
                // storedquery mode - requires aimsQuery.js
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                queryStartRecord=1;
                queryTool=1;
                toolMode=51;
                modeBlurb="Search";
                if (canQuery) { 
                        toolMode=51;
                        modeBlurb=modeList[16];
                        fieldIndex=0;
                        setQueryString="";
                        hideLayer("measureBox");
                        getStoredQueries();
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                showBuffer=false;
                break
                
        case "find":
                //find
                toolMode=9;
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                queryStartRecord=1;
                queryTool=1;
                if (canQuery) { 
                        LayerFields.length=1;
                        LayerFieldType.length=1;
                        LayerFieldCount=0;
                        
                        fieldIndex=0;
                        setQueryString="";
                        hideLayer("measureBox");
                        modeBlurb = modeList[17];
                        findForm();
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                showBuffer=false;
                break
                
        case "clearsel":
                 clearSelection();
                 break
                 
        // Buffer function
        case "buffer":
                //buffer - requires aimsBuffer.js
                if (useBuffer) {
                        if (checkSelected()) {
                                toolMode = 25;
                                shapeSelectBuffer = false;
                                modeBlurb = modeList[18];
                                writeBufferForm();      
                        } else {
                                showBuffer=false;
                                alert(msgList[48]);
                        }
                } else {
                        alert(msgList[49]);
                }
                break
                
        case "options":
                writeOptionForm();
                break
                
        // Print function       
        case "print":
                 printIt();
                 break
                 
        // custom modes
        case "dbidentify":
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                // identify mode - requires custom db query - not in basic
                if (canQuery) {
                        toolMode = 40;
                        if (isIE)       {
                                document.all.theTop.style.cursor = "hand";
                                theCursor = document.all.theTop.style.cursor;
                        }

                        modeBlurb = modeList[3];
                } else {
                        alert(msgList[46]);
                }
                showGeocode=false;
                if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
                hideLayer("measureBox");
                break
                
                
        case "extract":
                 extractIt();
                 break
                 
        case "legend":
                if (aimsLegendPresent) {
                        if (imsURL!="") {
                                if (hasTOC) {
                                        if (legendVisible) {
                                                legendVisible=false;
                                                //writeLayerList();
                                                parent.TOC2Frame.TOCFrame.document.location=appDir+"toc.htm";
                                        } else {
                                                legendVisible=true;
                                                obtenirLegend();
                                        }
                                } else {
                                        legendVisible=true;
                                        obtenirLegend();
                                }
                        } else {
                                alert(msgList[45]);
                        }
                } else {
                        alert(msgList[50]);
                }
                break
                
        case "layerlist":
                // put LayerList in separate window
                writeLayerListForm();
                break
                
        case "query2":
                // query mode - requires aimsQuery.js
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                queryStartRecord=1;
                //toolMode=
                queryTool=51;
                if (!canQuery){
                        showGeocode=false;
                        showBuffer=false;
                        alert(msgList[46]);
                } else {
                  if (ActiveLayerIndex == 0) {
                        showGeocode=false;
                        showBuffer=false;
                        alert('Il n\'y a pas de couche active.');
                  } else {
                        LayerFields.length=1;
                        LayerFieldType.length=1;
                        LayerFieldCount=0;
                        toolMode=8;
                        modeBlurb=modeList[15];
                        
                        fieldIndex=0;
                        setQueryString="";
                        hideLayer("measureBox");
                        showGeocode=false;
                        showBuffer=false;
                        queryForm2();
                  }
                }
                        
/*              
                if ((canQuery) && (ActiveLayerIndex!=0)) {      
                        LayerFields.length=1;
                        LayerFieldType.length=1;
                        LayerFieldCount=0;
                        toolMode=8;
                        modeBlurb=modeList[15];
                        
                        fieldIndex=0;
                        setQueryString="";
                        hideLayer("measureBox");
                        showGeocode=false;
                        showBuffer=false;
                        queryForm2();
                } else {
                        showGeocode=false;
                        showBuffer=false;
                        alert(msgList[46]);
                }
*/
                return;
                break
        case "proximite":
                panning=false;
                zooming=false;
                selectBox=false;
                shapeSelectBuffer = false;
                window.open(appDir + "\proximite.htm","QueryWindow2","width=500,height=100,scrollbars=yes,resizable=yes");

                break
        case "IdentifyRech":
                var theString = 'RC_PX =' + px + ' AND RC_PY = ' + py;
                sendQueryStringMode(theString,1005);
                break
        case "info_taxes":
                sendQueryStringMode(theString,1007);
                break   
        default:
                alert(msgList[51]);
        }
        modeName=modeBlurb;
        if (useModeFrame) {
                writeModeFrame(modeBlurb);
        } else if ((drawFloatingMode) && (modeLayerOn)) {
                writeModeLayers(modeBlurb);
        } else if ((modeRefreshMap) && (drawModeOnMap)) {
                sendMapXML();
        }
}

// check for mouseup
function chkMouseUp(e) { 
        if ((toolMode == 1) && (zooming)) {
                        stopZoomBox(e);
                
        }
        if ((toolMode == 2) && (zooming)) {
                        stopZoomOutBox(e);
        }
        if ((toolMode == 3) && (panning)) {
                        stopPan(e);

        }
        if ((toolMode == 10) && (selectBox)) {
                        stopSelectBox(e);
        }
                
        return false;
        
}

// perform appropriate action with mapTool
function mapTool (e) {
        var theButton= 0;
        // get the button pushed... if right, ignore... let browser do the popup... it will anyway
        if (isNav) {
                theButton = e.which;
        } else {
                theButton =window.event.button;
        }       
        if (theButton==leftButton) {
                getImageXY(e);
                if ((mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
                        if ((hasOVMap) && (ovIsVisible) && (mouseX<i2Width+ovBoxSize) && (mouseY<i2Height) && (ovMapIsLayer)) {
                                        ovMapClick(mouseX,mouseY);
                                        
                                        window.status = "On OV Map Area";
                        } else {
                                switch(toolMode) {
                                case 1:
                                                startZoomBox(e);
                                                return false;
                                                break
                                        
                                case 2:
                                                startZoomOutBox(e);
                                                return false;
                                                break
                                case 3:
                                                startPan(e);
                                                return false;
                                                break

                                case 4:
                                	if (qualite){
						//Changer de couche active
	            				parent.TOC2Frame.TOCFrame.setActiveNomLayer('Qualite eau releve');
						//qualite=false;
							
					} else {
						//Changer de couche active
	            				parent.TOC2Frame.TOCFrame.setActiveNomLayer('Role_evaluation_2.5k');
						// identify mode - layer attributes - requires aimsIdentify.js
					}
					panning=false;
					zooming=false;
					selectBox=false;
					shapeSelectBuffer = false;
					if (canQuery) {
						toolMode = 4;
						//toolMode = 1;
						if (isIE)	{
							document.all.theTop.style.cursor = "crosshair";
							theCursor = document.all.theTop.style.cursor;
						}
						modeBlurb = modeList[3];
					} else {
						alert(msgList[46]);
					}
					//alert("Function Not Implemented");
		
					showGeocode=false;
					if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
					hideLayer("measureBox");
					//alert("aimsclick.js ds case 4 du ToolMode juste avnt le identify(e)");
					identify(e);
                                        break
                                        
                                case 5:
                                        identifyAll(e);
                                        break
                                case 10:
                                        startSelectBox(e);
                                        return false;
                                        break
                                case 11:
                                        //select point
                                        if (checkIfActiveLayerAvailable()) {
                                                select(e);
                                        }
                                        break
                                case 12:
                                        //select line
                                        if (checkIfActiveLayerAvailable()) {
                                                clickType=2;
                                                clickAddPoint();
                                                if (useTextFrame) {
                                                        if (parent.TextFrame.document.title!==modeList[60]) {
                                                                parent.TextFrame.document.location= appDir + "selectline.htm";
                                                        }
                                                }
                                        }
                                        break
                                case 13:
                                        //select polygon
                                        if (checkIfActiveLayerAvailable()) {
                                                clickType=3;
                                                clickAddPoint();
                                                if (useTextFrame) {
                                                        if (parent.TextFrame.document.title!==modeList[7]) {
                                                                parent.TextFrame.document.location= appDir + "selectpoly.htm";
                                                        }
                                                }
                                        }
                                        break
                                case 15:
                                        // hyperlink
                                        hyperLink(e);
                                        break
                                case 16:
                                        //select shape
                                        if (checkIfActiveLayerAvailable()) {
                                                clickType=2;
                                                clickAddPoint();
                                                if (useTextFrame) {
                                                        if (parent.TextFrame.document.title!==modeList[8]) {
                                                                parent.TextFrame.document.location= appDir + "select.htm";
                                                        }
                                                }
                                        }
                                        break
                                        ///* 
                                case 17:
                                        //buffer shape - 
                                        if (checkIfActiveLayerAvailable()) {
                                                clickType=2;
                                                clickAddPoint();
                                                if (useTextFrame) {
                                                        if (parent.TextFrame.document.title!=modeList[11]) {
                                                                parent.TextFrame.document.location= appDir + "shapeBuffer.htm";
                                                        }
                                                }
                                        }
                                        break
                                case 20:
                                        // measure
                                        clickType=1;
                                        clickAddPoint();
                                        break
                                case 21:
                                        // shape
                                        clickType=4;
                                        clickAddPoint();
                                        break
                                case 30:
                                        // hyperlink
                                        hyperLinkAny(e);
                                        break
                                case 40:
                                        // db identify - requires custom db query - not in default
                                        if (aimsDBPresent) {
                                                matchDBLinkLayer(dbLinkLayer);
                                                dbIdentify(e);
                                        }
                                        break
                                default:
                                        if (toolMode>=1000) {
                                                customMapTool(e);
                                        }
                                }
                        }
                }
        }
}

// update measureBox layer
function updateMeasureBox() {
        if (isNav4) {
                var theForm = document.layers["measureBox"].document.forms[0];
        } else {
                var theForm = document.forms[0];
        }
        var j = 1;
        for (var i=0;i<sUnitList.length;i++) {
                if (ScaleBarUnits==sUnitList[i].toUpperCase()) {
                        j=i;
                        break;
                }
        }
        
        var u = Math.pow(10,numDecimals);
        var tMeas = 0;
        if (totalMeasure!=0) tMeas = parseInt(totalMeasure*u+0.5)/u;
        theForm.theMeasTotal.value = tMeas + " " + unitList[j];
        theForm.theMeasSegment.value = currentMeasure + " " + unitList[j];
        showLayer("measureBox");

}
