﻿var spatialQueryTask = null;
var spatialQueryParams = null;
var parcelDisplayData = "";
var zoningDisplayData = "";
var flupDisplayData = "";
var subdivisionDisplayData = "";
var parcelQueryComplete;
var zoningQueryComplete;
var flupQueryComplete;
var subdivisionQueryComplete;
var parcelShape=null;

function ExecuteQueries() {
    //draybuck: in order to display the query results in the description window in a specified order, a workaround in necessary
    //due to the fact that query tasks are asynchronous and there is no guarantee that they will complete in the same order.

    //these boolean values are initially set to false but are set to true at the end of each query task callback function.
    //each callback function then calls the buildQueryResults function which checks to see if all of the boolean values are true,
    //and if so, it builds the description text to display in the info window for the point.
    parcelQueryComplete = false;
    zoningQueryComplete = false;
    flupQueryComplete = false;
    subdivisionQueryComplete = false;
    
    ExecuteZoningQuery(); 
    ExecuteFlupQuery();
    ExecuteSubdivisionQuery();
    ExecuteParcelQuery();
}

function buildQueryResults() {
    if (parcelQueryComplete == true && zoningQueryComplete == true && flupQueryComplete == true && subdivisionQueryComplete == true){
        
        //rearrange order of query results here
        var description = parcelDisplayData + subdivisionDisplayData + zoningDisplayData + flupDisplayData + censusDisplayData;
        //add 'exit query tool' option to bottom of infobox
        description += "<br><span onmouseover='this.style.fontWeight=\"bold\";this.style.cursor=\"pointer\";'"
        description += " onmouseout='this.style.fontWeight=\"normal\";' onclick='unselectQueryTool();'>Exit Query Tool</span>"
        queryveLLShape[0].SetDescription(description);
        map.ShowInfoBox(queryveLLShape[0]);
        
    }
}


// function to send query request
function ExecuteParcelQuery() {
    spatialQueryParams = new ESRI.ArcGIS.VE.Query();
    spatialQueryParams.Geometry = queryveLLShape[0];
    //spatialQueryParams.SpatialRelationship = "esriSpatialRelCrosses";
    spatialQueryParams.OutFields = parcelSearchFields;
    spatialQueryTask = new ESRI.ArcGIS.VE.QueryTask();
    spatialQueryTask.Url = parcelUrl;
    spatialQueryTask.Execute(spatialQueryParams, addParcelQueryResults);

}

// function to process query response
function addParcelQueryResults(data) {

    // check for error. . . if none, add returned shapes to map
    var err = data.Error;
    if (err != null) {
        //div.innerHTML = err.message;
    }
    else {
        var rs = data.Features;
        var color = new VEColor(0, 0, 255, .15);
        var lineColor = new VEColor(0, 0, 0, .5);
        if (rs && rs.length > 0) {
            data.SetShapeStyle(lineColor, 2, color, false);
            parcelShape = data.ToVEShapeLayer();
            map.AddShapeLayer(parcelShape);
       
            if (data.Features.length > 0) {
                parcelDisplayData = "<a onclick='toggleDiv(\"parcelresults\")';> <b>Parcel Information</a></b><div id='parcelresults' style='display:visible'><table class='ReportTable' cellpadding='0' cellspacing='0'>";
                var resultFeature = data.Features[0];
                var address = resultFeature.Attributes['STREETNUMBER'] + ' ' + resultFeature.Attributes['STREETNAME'] + ' ' + resultFeature.Attributes['STREETTYPE']
                var link = resultFeature.Attributes['GISLINK']
                parcelDisplayData += "<tr><td>&nbsp;</td><td>&nbsp;</td><tr>"
                parcelDisplayData += "<tr><td class='ReportColLeft'>Address:&nbsp;</td><td class='ReportColRight'>" + address + "</td></tr>"
                parcelDisplayData += "<tr><td class='ReportColLeft'></td><td align='right'><a href='http://www.tad.org/Datasearch/geo.cfm?GISLink=" + link + "' target='_blank'>More info</a></td></tr>"
                parcelDisplayData += "</a></table></div><br>";
                //var resultShape = parcelShape.GetShapeByIndex(0);
                
            }

        }
        else {
            parcelDisplayData = '';
        }
    }

    parcelQueryComplete = true;
    buildQueryResults();

}

// function to send query request
function ExecuteZoningQuery() {
    spatialQueryParams = new ESRI.ArcGIS.VE.Query();
    spatialQueryParams.Geometry = queryveLLShape[0];
    //spatialQueryParams.SpatialRelationship = "esriSpatialRelCrosses";
    spatialQueryParams.OutFields = zoningSearchFields;
    spatialQueryTask = new ESRI.ArcGIS.VE.QueryTask();
    spatialQueryTask.Url = zoningUrl;
    spatialQueryTask.Execute(spatialQueryParams, addZoningQueryResults);
}

// function to process query response
function addZoningQueryResults(data) {

    // check for error. . . if none, add returned shapes to map
    var err = data.Error;
    if (err != null) {
        //div.innerHTML = err.message;
    }
    else {
        var rs = data.Features;
        if (rs && rs.length > 0) {
            if (data.Features.length > 0) {
                zoningDisplayData = "<a onclick='toggleDiv(\"zoningresults\")';> <b>Zoning Information</a></b><div id='zoningresults' style='display:none'><table class='ReportTable' cellpadding='0' cellspacing='0'>";
                var resultFeature = data.Features[0];
                for (p in resultFeature.Attributes) {
                    for (var i = 0; i < zoningFieldsJson.Fields.length; i++) {
                        if (zoningFieldsJson.Fields[i].Name == p) {
                            var value = resultFeature.Attributes[p];
                            if (zoningFieldsJson.Fields[i].Type != null) {
                                if (zoningFieldsJson.Fields[i].Type == "Money") {
                                    value = new Number(value);
                                    value = "$" + value.formatMoney(0);
                                }
                            }
                            zoningDisplayData = zoningDisplayData + "<tr><td class='ReportColLeft'>" + zoningFieldsJson.Fields[i].Alias + ":&nbsp;</td><td class='ReportColRight'>" + value + "</td></tr>";
                            break;
                        }
                    }

                }
                zoningDisplayData = zoningDisplayData + "<br></table></div><br>";
            }
        }
        else {
            zoningDisplayData = '';
            
        }
    }
    zoningQueryComplete = true;
    buildQueryResults();  

}

// function to send query request
function ExecuteFlupQuery() {
    spatialQueryParams = new ESRI.ArcGIS.VE.Query();
    spatialQueryParams.Geometry = queryveLLShape[0];
    //spatialQueryParams.SpatialRelationship = "esriSpatialRelCrosses";
    spatialQueryParams.OutFields = flupSearchFields;
    spatialQueryTask = new ESRI.ArcGIS.VE.QueryTask();
    spatialQueryTask.Url = flupUrl;
    spatialQueryTask.Execute(spatialQueryParams, addFlupQueryResults);
}
// function to process query response
function addFlupQueryResults(data) {

    // check for error. . . if none, add returned shapes to map
    var err = data.Error;
    if (err != null) {
        //div.innerHTML = err.message;
    }
    else {
        var rs = data.Features;
        if (rs && rs.length > 0) {
            if (data.Features.length > 0) {
                flupDisplayData = "<a onclick='toggleDiv(\"flupresults\")';> <b>Future Land Use Information</a></b><div id='flupresults' style='display:none'><table class='ReportTable' cellpadding='0' cellspacing='0'>";
                var resultFeature = data.Features[0];
                for (p in resultFeature.Attributes) {
                    for (var i = 0; i < flupFieldsJson.Fields.length; i++) {
                        if (flupFieldsJson.Fields[i].Name == p) {
                            var value = resultFeature.Attributes[p];
                            if (flupFieldsJson.Fields[i].Type != null) {
                                if (flupFieldsJson.Fields[i].Type == "Money") {
                                    value = new Number(value);
                                    value = "$" + value.formatMoney(0);
                                }
                            }
                            flupDisplayData = flupDisplayData + "<tr><td class='ReportColLeft'>" + flupFieldsJson.Fields[i].Alias + ":&nbsp;</td><td class='ReportColRight'>" + value + "</td></tr>";
                            break;
                        }
                    }

                }
                flupDisplayData = flupDisplayData + "<br></table></div><br>";
              
            }

        }
        else {
            flupDisplayData = '';
        }
    }
    flupQueryComplete = true;
    buildQueryResults();

}

// function to send query request
function ExecuteSubdivisionQuery() {
    spatialQueryParams = new ESRI.ArcGIS.VE.Query();
    spatialQueryParams.Geometry = queryveLLShape[0];
    //spatialQueryParams.SpatialRelationship = "esriSpatialRelCrosses";
    spatialQueryParams.OutFields = subdivisionSearchFields;
    spatialQueryTask = new ESRI.ArcGIS.VE.QueryTask();
    spatialQueryTask.Url = subdivisionUrl;
    spatialQueryTask.Execute(spatialQueryParams, addSubdivisionQueryResults);
}

// function to process query response
function addSubdivisionQueryResults(data) {

    // check for error. . . if none, add returned shapes to map
    var err = data.Error;
    if (err != null) {
        //div.innerHTML = err.message;
    }
    else {
        var rs = data.Features;
        if (rs && rs.length > 0) {
            if (data.Features.length > 0) {
                subdivisionDisplayData = "<a onclick='toggleDiv(\"subdivisionresults\")';> <b>Subdivision Information</a></b><div id='subdivisionresults' style='display:none'><table class='ReportTable' cellpadding='0' cellspacing='0'>";
                var resultFeature = data.Features[0];
                for (p in resultFeature.Attributes) {
                    for (var i = 0; i < subdivisionFieldsJson.Fields.length; i++) {
                        if (subdivisionFieldsJson.Fields[i].Name == p) {
                            var value = resultFeature.Attributes[p];
                            if (subdivisionFieldsJson.Fields[i].Type != null) {
                                if (subdivisionFieldsJson.Fields[i].Type == "Money") {
                                    value = new Number(value);
                                    value = "$" + value.formatMoney(0);
                                }
                            }
                            subdivisionDisplayData = subdivisionDisplayData + "<tr><td class='ReportColLeft'>" + subdivisionFieldsJson.Fields[i].Alias + ":&nbsp;</td><td class='ReportColRight'>" + value + "</td></tr>";
                            break;
                        }
                    }

                }
                subdivisionDisplayData = subdivisionDisplayData + "<br></table></div><br>";

            }

        }
        else {
            subdivisionDisplayData = '';
        }
    }
    subdivisionQueryComplete = true;
    buildQueryResults();

}