﻿// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.

//EDITHERE
var sketchServicePath = "http://renogis2.renogov.org/ArcGIS1/rest/services/SketchORKA1/";

var sketch, sketchService;
var sketchPopup, sketchServicePopup;
var popup = false;

var colorFillArr = new Array([204, 106, 65, 0.5], [255, 255, 115, 0.5], [140, 180, 180, 0.5], [216, 148, 77, 0.5], [208, 209, 151, 0.5], [212, 170, 159, 0.5], [180, 194, 157, 0.5], [172, 147, 191, 0.5], [214, 163, 177, 0.5], [180, 214, 169, 0.5], [191, 147, 138, 0.5], [195, 176, 78, 0.5], [214, 216, 72, 0.5], [144, 132, 224, 0.5], [149, 150, 130, 0.5], [201, 100, 29, 0.5], [129, 156, 188, 0.5], [189, 160, 203, 0.5]);
var colorOutlineArr = new Array([204, 106, 65, 0.9], [255, 255, 115, 0.9], [140, 180, 180, 0.9], [216, 148, 77, 0.9], [208, 209, 151, 0.9], [212, 170, 159, 0.9], [180, 194, 157, 0.9], [172, 147, 191, 0.9], [214, 163, 177, 0.9], [180, 214, 169, 0.9], [191, 147, 138, 0.9], [195, 176, 78, 0.9], [214, 216, 72, 0.9], [144, 132, 224, 0.9], [149, 150, 130, 0.9], [201, 100, 29, 0.9], [129, 156, 188, 0.9], [189, 160, 203, 0.9]);
//alert(colorFillArr.length);
var colorCounter = 0;

var connectSketchMove = null;
var connectSketchMovePopup = null;

// Resizing the Sketch is controlled in function resizeMap in agsmap.js.
///////////////////////////////////////////////////////////////////////////
function displaySketchResults(coords, pid1, dspCount, totCount) {
    console.log("totCount: " + totCount + "\ndspCount: " + dspCount + "\ncoords: " + coords);
    //return false;

    //dojo.byId("h_coords").value = coords;

    var xMin = 0;
    var yMin = 0;
    var xMax = 0;
    var yMax = 0;
    var graphic = null;
    var alphabet = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
    var alphabetCounter = 0;
    var minDist = 12;
    if (popup == false) {
        sketch.graphics.clear();
        var sr = new esri.SpatialReference({ wkid: sketch.spatialReference });
    } else {
        sketchPopup.graphics.clear();
        var sr = new esri.SpatialReference({ wkid: sketchPopup.spatialReference });
    }

    /////////////////////////////////////////////////////////
    // Add header information to web page.
    /////////////////////////////////////////////////////////
    var htmlTxt = makeSketchHeader(pid1, dspCount);
    dojo.byId("sketchHdr").innerHTML = htmlTxt;
    //console.log(htmlTxt);
    /////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////
    // Adds Sketch Vector graphics to Grid Map Service.
    /////////////////////////////////////////////////////////
    if (dspCount > 0) {
        /////////////////////////////////////////////////////////
        // Establishes arrays of Sketch Vector results.
        // Sample: 14,51,51,14,|37,37,30,30,#36,36,9,9,|9,5,5,9,#28,#38,#2SFR/S |MS 7X4 |WDDK IRR [448] <14>
        /////////////////////////////////////////////////////////
        var mainArr = coords.split("#");
        var polyXcoords = mainArr[0];
        var polyYcoords = mainArr[1];
        var pntXcoords = mainArr[2];
        var pntYcoords = mainArr[3];
        var descString = mainArr[4];

        var polyXcoordsArr = null;
        if (polyXcoords != null && polyXcoords != "")
            polyXcoordsArr = polyXcoords.split("|");
        var polyYcoordsArr = null;
        if (polyYcoords != null && polyYcoords != "")
            polyYcoordsArr = polyYcoords.split("|");

        var pntXcoordsArr = null;
        if (pntXcoords != null && pntXcoords != "")
            pntXcoordsArr = pntXcoords.split("|");
        var pntYcoordsArr = null;
        if (pntYcoords != null && pntYcoords != "")
            pntYcoordsArr = pntYcoords.split("|");

        var descStringArr = null;
        if (descString != null && descString != "")
            descStringArr = descString.split("|");

        var distHolderArr = new Array();
        var midxHolderArr = new Array();
        var midyHolderArr = new Array();
        var coordsHolderArr1 = new Array();
        var coordsCounter = 0;
        var midxHolderStr = "";
        var midyHolderStr = "";
        var diagxHolderStr = "";
        var diagyHolderStr = "";
        colorCounter = 0;
        /////////////////////////////////////////////////////////

        /////////////////////////////////////////////////////////
        // Simple Line Symbol for Polygon outlines.
        /////////////////////////////////////////////////////////
        var linesymbol = new esri.symbol.SimpleLineSymbol();
        linesymbol.setColor(new dojo.Color([0, 0, 0, 0.9]));
        linesymbol.setStyle(esri.symbol.SimpleLineSymbol.STYLE_SOLID);
        linesymbol.setWidth(0.5);
        /////////////////////////////////////////////////////////

        /////////////////////////////////////////////////////////
        // Adds Polygons to Map.
        /////////////////////////////////////////////////////////
        if (polyXcoordsArr != null) {
            for (var a = 0; a < polyXcoordsArr.length; a++) {
                //14,51,51,14,|37,37,30,30,#36,36,9,9,|9,5,5,9,#
                var xArr = polyXcoordsArr[a].split(",");
                var yArr = polyYcoordsArr[a].split(",");
                // Adds the first coordinate to the end to close the polygon.
                xArr[xArr.length - 1] = xArr[0];
                yArr[yArr.length - 1] = yArr[0];
                //alert(xArr +"\n"+ yArr);

                // Initializes Polygon bounding box.
                var pxMin = xArr[0];
                var pyMin = yArr[0];
                var pxMax = xArr[0];
                var pyMax = yArr[0];

                // Initializes Polygon dimension variables.
                var tmpx = xArr[0];
                var tmpy = yArr[0];
                var midx = 0;
                var midy = 0;
                var coordsHolderX = tmpx;
                var coordsHolderY = tmpy;
                coordsCounter = 0;

                var cenx = 0;
                var ceny = 0;

                /////////////////////////////////////////////////////////
                // Loops through all Polygon Points and Lines to establish a
                // Polygon ring, the Polygon's bounding box and dimension labels.
                /////////////////////////////////////////////////////////
                var ring = new Array();
                for (var b = 0; b < xArr.length; b++) {
                    // The Ring of the Nebelung, I mean Polygon.
                    var pnt = new esri.geometry.Point();
                    pnt.setX(Number(xArr[b]));
                    pnt.setY(Number(yArr[b]));
                    ring[b] = pnt;

                    // Establishes Polygon bounding box.
                    pxMin = Math.min(pnt.x, pxMin);
                    pyMin = Math.min(pnt.y, pyMin);
                    pxMax = Math.max(pnt.x, pxMax);
                    pyMax = Math.max(pnt.y, pyMax);

                    /////////////////////////////////////////////////////////
                    // Establishes dimension labels for Lines around the Polygon.
                    // We process the various Lines surrounding the Polygon
                    // by stepping from Point to Point.  But, we do not compare
                    // every Point with every other Point.  If we did so, we'd
                    // criss-cross the Polygon which would give us bogus 
                    // dimensions in the interior of the Polygon.  
                    /////////////////////////////////////////////////////////
                    if (b > 0) {
                        tmpx = xArr[b];
                        tmpy = yArr[b];

                        var dimDist = Math.round(Math.sqrt((Math.pow((tmpx - coordsHolderX), 2)) + (Math.pow((tmpy - coordsHolderY), 2))));
                        //alert(dimDist);

                        if (dimDist >= minDist) {
                            if (coordsHolderX == tmpx) {
                                // VERTICAL LINE
                                midx = tmpx;
                                midy = Math.min(tmpy, coordsHolderY) + ((Math.max(tmpy, coordsHolderY) - Math.min(tmpy, coordsHolderY)) / 2);
                                if (midyHolderStr.indexOf(midy.toString()) == -1) {
                                    midyHolderStr += midy;
                                    distHolderArr[coordsCounter] = dimDist;
                                    midxHolderArr[coordsCounter] = midx;
                                    midyHolderArr[coordsCounter] = midy;
                                    coordsCounter += 1;
                                }
                            }
                            else if (coordsHolderY == tmpy) {
                                // HORIZONTAL LINE
                                midx = Math.min(tmpx, coordsHolderX) + ((Math.max(tmpx, coordsHolderX) - Math.min(tmpx, coordsHolderX)) / 2);
                                midy = tmpy;
                                if (midxHolderStr.indexOf(midx.toString()) == -1) {
                                    midxHolderStr += midx;
                                    distHolderArr[coordsCounter] = dimDist;
                                    midxHolderArr[coordsCounter] = midx;
                                    midyHolderArr[coordsCounter] = midy;
                                    coordsCounter += 1;
                                }
                            }
                            else {
                                // DIAGONAL LINE
                                midx = (Number(tmpx) + Number(coordsHolderX)) / 2;
                                midy = (Number(tmpy) + Number(coordsHolderY)) / 2;
                                //alert(midx +"\n"+ midy);

                                if (diagxHolderStr.indexOf(midx.toString()) == -1 ||
									diagyHolderStr.indexOf(midy.toString()) == -1) {
                                    diagxHolderStr += midx;
                                    diagyHolderStr += midy;
                                    distHolderArr[coordsCounter] = dimDist;
                                    midxHolderArr[coordsCounter] = midx;
                                    midyHolderArr[coordsCounter] = midy;
                                    coordsCounter += 1;
                                }
                            }
                        }
                        var coordsHolderX = tmpx;
                        var coordsHolderY = tmpy;
                    }
                    /////////////////////////////////////////////////////////
                }
                //alert(pxMin +", "+ pyMin +", "+ pxMax +", "+ pyMax);

                /////////////////////////////////////////////////////////
                // If we have a Ring, we put it on the Sketch Grid, label
                // the Polygon and display the dimensions.
                /////////////////////////////////////////////////////////
                if (ring.length > 0) {
                    var poly = new esri.geometry.Polygon();
                    poly.setSpatialReference(sr);
                    poly.addRing(ring);

                    var polysymbol = new esri.symbol.SimpleFillSymbol();
                    polysymbol.setColor(new dojo.Color(colorFillArr[colorCounter]));
                    polysymbol.setStyle(esri.symbol.SimpleFillSymbol.STYLE_SOLID);
                    polysymbol.setOutline(linesymbol);
                    graphic = new esri.Graphic(poly, polysymbol);
                    if (popup == false) {
                        sketch.graphics.add(graphic);
                    } else {
                        sketchPopup.graphics.add(graphic);
                    }

                    var font = new esri.symbol.Font();
                    font.setWeight(esri.symbol.Font.WEIGHT_NORMAL);
                    font.setSize(13);

                    var textsymbol = new esri.symbol.TextSymbol();
                    textsymbol.setText(alphabet[alphabetCounter]);
                    textsymbol.setOffset(0, -5);
                    textsymbol.setFont(font);

                    /////////////////////////////////////////////////////////
                    // Finds the center of the Polygon's bounding box for the
                    // sake of labeling the Polygon.
                    /////////////////////////////////////////////////////////
                    cenx = pxMin + ((pxMax - pxMin) / 2);
                    ceny = pyMin + ((pyMax - pyMin) / 2);

                    var pnt = new esri.geometry.Point();
                    pnt.setSpatialReference(sr);
                    pnt.setX(cenx);
                    pnt.setY(ceny);

                    /////////////////////////////////////////////////////////
                    // Checks to see if the label point falls within the Polygon.
                    // If the center of the bounding box is not within the Polygon,
                    // we loop through lines that most likely criss cross the Polygon
                    // looking for a midpoint that does fall within the Polygon.
                    /////////////////////////////////////////////////////////
                    var pntInPoly = poly.contains(pnt);

                    //pntInPoly = false;  // For testing.

                    if (!pntInPoly) {
                        cenx = 0;
                        ceny = 0;
                        var dimDist = 0;
                        var tmpDist = 0;

                        for (var c = 0; c < xArr.length; c++) {
                            var pnt1 = new esri.geometry.Point();
                            pnt1.setSpatialReference(sr);
                            pnt1.setX(xArr[c]);
                            pnt1.setY(yArr[c]);
                            for (var d = 0; d < xArr.length; d++) {
                                var pnt2 = new esri.geometry.Point();
                                pnt2.setSpatialReference(sr);
                                pnt2.setX(xArr[d]);
                                pnt2.setY(yArr[d]);
                                if (pnt1.x != pnt2.x && pnt1.y != pnt2.y) {
                                    tmpDist = Math.round(Math.sqrt((Math.pow((pnt1.x - pnt2.x), 2)) + (Math.pow((pnt1.y - pnt2.y), 2))));
                                    cenx = (Math.max(pnt1.x, pnt2.x) + Math.min(pnt1.x, pnt2.x)) / 2;
                                    ceny = (Math.max(pnt1.y, pnt2.y) + Math.min(pnt1.y, pnt2.y)) / 2;
                                    var pnt3 = new esri.geometry.Point();
                                    pnt3.setSpatialReference(sr);
                                    pnt3.setX(cenx);
                                    pnt3.setY(ceny);
                                    pntInPoly = poly.contains(pnt3);
                                    if (pntInPoly) {
                                        if (tmpDist > dimDist) {
                                            pnt.setX(cenx);
                                            pnt.setY(ceny);
                                            dimDist = tmpDist;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    /////////////////////////////////////////////////////////

                    var text = new esri.Graphic(pnt, textsymbol);
                    if (popup == false) {
                        sketch.graphics.add(text);
                    } else {
                        sketchPopup.graphics.add(text);
                    }

                    alphabetCounter += 1;
                    //alert(cenx +", "+ ceny);
                    /////////////////////////////////////////////////////////

                    /////////////////////////////////////////////////////////
                    // Adds dimensions to the perimeter of the Polygon.		
                    /////////////////////////////////////////////////////////
                    if (distHolderArr != null && distHolderArr.length > 0) {
                        var font = new esri.symbol.Font();
                        font.setSize(13);
                        font.setStyle(esri.symbol.Font.STYLE_ITALIC);
                        font.setWeight(esri.symbol.Font.WEIGHT_NORMAL);
                        font.setFamily("Verdana");

                        for (var e = 0; e < distHolderArr.length; e++) {
                            var textsymbol = new esri.symbol.TextSymbol();
                            textsymbol.setText(distHolderArr[e]);
                            textsymbol.setOffset(8, 2);
                            textsymbol.setColor(new dojo.Color([0, 0, 0, 1.0]));
                            textsymbol.setFont(font);
                            var pnt = new esri.geometry.Point();
                            pnt.setX(midxHolderArr[e]);
                            pnt.setY(midyHolderArr[e]);
                            var text = new esri.Graphic(pnt, textsymbol);
                            if (popup == false) {
                                sketch.graphics.add(text);
                            } else {
                                sketchPopup.graphics.add(text);
                            }
                        }
                    }
                    /////////////////////////////////////////////////////////
                } // Ends if ring.length > 0
                /////////////////////////////////////////////////////////

                // Increments the fill color.
                incrColor();

                /////////////////////////////////////////////////////////
                // Calculates Polygon area for use in the Legend.
                /////////////////////////////////////////////////////////
                var mArea = 0.00;
                var i = xArr.length - 1;
                for (var j = 0; j < xArr.length; j++) {
                    mArea += (xArr[i] * yArr[j] - xArr[j] * yArr[i]);
                    i = j;
                }
                // coordsHolderArr1 is used in the Legend.
                coordsHolderArr1[a] = Math.abs(mArea) / 2;
                //alert(Math.abs(mArea) / 2);
                /////////////////////////////////////////////////////////

                /////////////////////////////////////////////////////////
                // Evaluates extent of Polygon features.
                /////////////////////////////////////////////////////////
                if (a == 0) {
                    // Initializes min and max coordinates.
                    xMin = xArr[a];
                    yMin = yArr[a];
                    xMax = xArr[a];
                    yMax = yArr[a];
                }
                // If this is done as an "else", it produces strange results.
                for (var f = 0; f < xArr.length; f++) {
                    // Adjusts min and max coordinates based on new information.
                    xMin = Math.min(xMin, xArr[f]);
                    yMin = Math.min(yMin, yArr[f]);
                    xMax = Math.max(xMax, xArr[f]);
                    yMax = Math.max(yMax, yArr[f]);
                }
                /////////////////////////////////////////////////////////

            } // Ends for polyXcoordsArr loop.
        } // Ends add Polygons to Map.
        /////////////////////////////////////////////////////////

        /////////////////////////////////////////////////////////
        // Adds Points to Map.
        /////////////////////////////////////////////////////////
        if (pntXcoordsArr != null) {
            //#20,20,#6,35,#
            var xArr = pntXcoordsArr[0].split(",");
            var yArr = pntYcoordsArr[0].split(",");

            for (var b = 0; b < xArr.length; b++) {
                if (xArr[b] != null && xArr[b] != "") {
                    /////////////////////////////////////////////////////////
                    // Evaluates extent of Point features.
                    /////////////////////////////////////////////////////////
                    xMin = Math.min(xMin, xArr[b]);
                    yMin = Math.min(yMin, yArr[b]);
                    xMax = Math.max(xMax, xArr[b]);
                    yMax = Math.max(yMax, yArr[b]);
                    /////////////////////////////////////////////////////////

                    /////////////////////////////////////////////////////////
                    // Creates point graphic.
                    /////////////////////////////////////////////////////////
                    var pnt = new esri.geometry.Point();
                    pnt.setX(Number(xArr[b]));
                    pnt.setY(Number(yArr[b]));

                    var outline = new esri.symbol.SimpleLineSymbol();
                    outline.setColor(new dojo.Color(colorOutlineArr[colorCounter]));
                    outline.setStyle(esri.symbol.SimpleLineSymbol.STYLE_SOLID);
                    outline.setWidth(3);

                    var pntsymbol = new esri.symbol.SimpleMarkerSymbol();
                    pntsymbol.setColor(new dojo.Color(colorFillArr[colorCounter]));
                    pntsymbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE);
                    pntsymbol.setSize(22);
                    pntsymbol.setOutline(outline);
                    var graphic = new esri.Graphic(pnt, pntsymbol);

                    if (popup == false) {
                        sketch.graphics.add(graphic);
                    } else {
                        sketchPopup.graphics.add(graphic);
                    }
                    /////////////////////////////////////////////////////////

                    /////////////////////////////////////////////////////////
                    // Adds Point labels.
                    /////////////////////////////////////////////////////////
                    var font = new esri.symbol.Font();
                    font.setWeight(esri.symbol.Font.WEIGHT_NORMAL);
                    font.setSize(13);

                    var textsymbol = new esri.symbol.TextSymbol();
                    textsymbol.setText(alphabet[alphabetCounter]);
                    textsymbol.setOffset(0, -5);
                    textsymbol.setFont(font);

                    var text = new esri.Graphic(pnt, textsymbol);
                    if (popup == false) {
                        sketch.graphics.add(text);
                    } else {
                        sketchPopup.graphics.add(text);
                    }

                    alphabetCounter += 1;
                    /////////////////////////////////////////////////////////

                    // Increments the fill color.
                    incrColor();

                }
            } // Ends for xArr loop.
        } // Ends add Points to Map.
        /////////////////////////////////////////////////////////

        ///*		
        /////////////////////////////////////////////////////////
        // Legend - a.k.a. Building Areas.
        /////////////////////////////////////////////////////////
        alphabetCounter = 0;

        //alert(descArr.toString());
        var legend = '<table border="1">\n';

        for (var a = 0; a < coordsHolderArr1.length; a++) {
            if (coordsHolderArr1[a] != null && coordsHolderArr1[a] != ""){
                legend += '<tr>';

                legend += '<td align="Center" class="sketchText">';
                legend += alphabet[alphabetCounter];
                legend += '</td>';

                legend += '<td class="searchResultsLightLeftTD">';
                //if (coordsHolderArr1[a] != null && coordsHolderArr1[a] != "")
                    legend += ' ' + coordsHolderArr1[a] + ' Sq. Ft.';
                legend += '</td>';

                legend += '</tr>\n';
                
                alphabetCounter += 1;
            }
        }

        legend += '</table>';
        //alert(legend);
        if (dojo.byId("sketchLegendContent") != null) {
            //document.getElementById("sketchLegend").style.visibility = "visible";
            dojo.byId("sketchLegendContent").innerHTML = legend;
        }
        /////////////////////////////////////////////////////////
        //*/

        /////////////////////////////////////////////////////////
        // Zooms to features.
        /////////////////////////////////////////////////////////
        var ext = new esri.geometry.Extent();
        ext.xmin = xMin;
        ext.ymin = yMin;
        ext.xmax = xMax;
        ext.ymax = yMax;

        if (popup == false) {
            sketch.setExtent(ext.expand(1.2));
        } else {
            sketchPopup.setExtent(ext.expand(1.2));
        }
        /////////////////////////////////////////////////////////

        // Increments pull-down list, as needed.
        if (dojo.byId("sketchList") != null)
            dojo.byId("sketchList").selectedIndex = (totCount - 1);
        /*
        var f = document.forms[0];
        var id = "";
        var typ = "";
        for (var i = 0; i < f.elements.length; i++) 
        {
        typ = f.elements[i].type;
        id = f.elements[i].id
        if (typ == "select-one") 
        {
        if (id == "sketchList")
        f.elements[i].selectedIndex = (totCount - 1);
        }
        }
        */
        //console.log("here");
        /////////////////////////////////////////////////////////
        // Makes Sketch elements visible.
        /////////////////////////////////////////////////////////
        togglePage('hidden', 'hidden', '', '', '', 'hidden', 'hidden', 'visible', 'visible', 'hidden', 'hidden', '', 'displaySketchResults');
        /////////////////////////////////////////////////////////

    } // Ends if dspCount > 0.

    /////////////////////////////////////////////////////////
    // dspCount equals zero.
    // Hides Sketch elements.
    /////////////////////////////////////////////////////////
    else {
        document.getElementById("sketchLegend").style.visibility = "hidden";
        //document.getElementById("sketchBtm").innerHTML = "";
        //document.getElementById("sketchInfo").innerHTML = "";
        togglePage('hidden', 'hidden', '', '', '', 'hidden', 'hidden', 'hidden', 'visible', 'hidden', 'hidden', '', 'displaySketchResults');
    }
    //reset variable
    dojo.byId("h_printSketch").value = 'false';
    /////////////////////////////////////////////////////////
}  // Ends function displaySketchResults

function initSketch() {
    dojo.require("esri.map");
    dojo.require("esri.geometry");

    popup = false;

    if (displaySketchVectors == "TRUE") {
        sketch = new esri.Map("agsSketch", { slider: false });

        connectSketch = dojo.connect(sketch, 'onLoad', function(theSketch) {
            dojo.connect(dijit.byId('agsSketch'), 'resize', function() {
                resizeSketchDelay();
            });
        });

        //connectSketchMove = dojo.connect(sketch, "onMouseMove", showCoordinates);
        sketchService = new esri.layers.ArcGISDynamicMapServiceLayer(sketchServicePath + "MapServer");
        sketchService.setOpacity(1.0);
        sketch.addLayer(sketchService);
    }
}

function initSketchPopup() {
    dojo.require("esri.map");
    dojo.require("esri.geometry");

    popup = true;

    sketchPopup = new esri.Map("agsSketchPopup", { slider: false });

    connectSketchPopup = dojo.connect(sketchPopup, 'onLoad', function(theSketch) {
        dojo.connect(dijit.byId('agsSketchPopup'), 'resize', function() {
            resizeSketchDelay();
        });
    });

    //connectSketchMove = dojo.connect(sketch, "onMouseMove", showCoordinates);
    sketchServicePopup = new esri.layers.ArcGISDynamicMapServiceLayer(sketchServicePath + "MapServer");
    sketchServicePopup.setOpacity(1.0);
    sketchPopup.addLayer(sketchServicePopup);
}

function unloadSketch() {
    if (connectSketchMove != null)
        dojo.disconnect(connectSketchMove);

    if (connectSketchMovePopup != null)
        dojo.disconnect(connectSketchMovePopup);
}

/////////////////////////////////////
//Handles resize of browser
/////////////////////////////////////
function resizeSketchDelay() {
    if (popup == false) {
        sketch.resize();
        sketch.reposition();
    } else {
        sketchPopup.resize();
        sketchPopup.reposition();
    }

}
function resizeSketch() {
    if (popup == false) {
        sketch.resize();
        sketch.reposition();
    } else {
        sketchPopup.resize();
        sketchPopup.reposition();
    }
}
/////////////////////////////////////

function incrColor() {
    colorCounter += 1;
    if (colorCounter > colorFillArr.length - 1)
        colorCounter = 0;
}

function showCoordinates(evt) {
    // For testing - Not used in URECA.
    //get point from event 
    var mp = evt.mapPoint;
    //display mouse coordinates 
    dojo.byId("sketchInfo").innerHTML = Math.round(mp.x) + ", " + Math.round(mp.y);
}

function makeSketchHeader(pid1, dspCount) {
    var htmlTxt = "";
    var dateStamp = getDateStamp();

    //    var htmlTxt = '<TABLE border="2" width="100%" height="100%" cellspacing="0" cellpadding="0">\n';
    var htmlTxt = '<TABLE border="0" width="100%" cellspacing="0" cellpadding="0">\n';
    htmlTxt += '<TR>\n';
    htmlTxt += '<TD class="themeHdrTD">&nbsp;';
    htmlTxt += 'Building Sketch';
    htmlTxt += '</TD>';
    //htmlTxt += '<TD class="timeHdrTD">';
    //htmlTxt += dateStamp;
    //htmlTxt += '&nbsp;</TD>\n';
    htmlTxt += '</TR>\n';
    htmlTxt += '<TR>\n';
    htmlTxt += '<TD colspan="2">\n';
    htmlTxt += '<table width="100%" align="center">\n';
    htmlTxt += '<tr>\n';
    htmlTxt += '<td NOWRAP="true" class="themeResultsHdrTD">';
    if (dspCount == 0) {
        htmlTxt += 'There are no building sketches of Parcel ' + pid1;
    }
    else {
        if (dspCount == 1) {
            htmlTxt += 'There is one building sketch of Parcel ' + pid1;
        }
        else {
            htmlTxt += 'There are ' + dspCount + ' building sketches of Parcel ' + pid1 + ' : ';

            if (popup == false) {
                htmlTxt += '<select id="sketchList" onChange="fetchSketch(this.value)">';
                for (var v = 0; v < dspCount; v++) {
                    htmlTxt += '<option id="sketch' + (v + 1) + '" value="' + (v + 1) + '">' + (v + 1) + '</option>';
                }
                htmlTxt += '</select>';
            }
        }
        htmlTxt += '</td>\n';
        //htmlTxt += '<td NOWRAP="true" class="searchResultsCenBlankTD" width="1%" colspan="1">';
        //htmlTxt += '<INPUT type="Button" value="Print" id="Print" class="buttonMapOut" onClick="sendToPrinter()"></INPUT>';
        //htmlTxt += '</td>\n';
    }
    htmlTxt += '</tr>\n';
    htmlTxt += '</table>\n';
    htmlTxt += '</TD>\n';
    htmlTxt += '</TR>\n';
    htmlTxt += '</TABLE>';

    //alert(htmlTxt);

    return htmlTxt;
}

