﻿// JScript File
function SelectPlace(source, eventArgs) {
    //alert(" Key : "+ eventArgs.get_text() +"  Value :  "+eventArgs.get_value());
    map.DeleteAllShapeLayers();
    var LatLong = eventArgs.get_value();
    var QueryText = eventArgs.get_text();
    LatLong = LatLong.split(/\;/);
    var geoType = LatLong[LatLong.length - 1];
    if (geoType == 'Counties') {
        QueryText = QueryText.replace(/ County/g, '');
    }
    
    var ParamGeoType = { "GeoType": geoType };
    var ParamQueryText = { "QueryText": QueryText };

    if (geoType == 'Counties') {
        WebServices.GetFeatureWebService.GetFeatureCityCounty(ParamGeoType.GeoType, ParamQueryText.QueryText, OnSucceededCounty, OnFailed);
    }
    else if (geoType == 'Cities') {
        WebServices.GetFeatureWebService.GetFeatureCityCounty(ParamGeoType.GeoType, ParamQueryText.QueryText, OnSucceededCity, OnFailed);
    }
    
    if (geoType == 'Address') {
        LatLong[1] = parseFloat(LatLong[1]);
        LatLong[0] = parseFloat(LatLong[0]);
        var LL = new VELatLong(LatLong[1], LatLong[0]);
        var pin = map.AddPushpin(LL);
        pin.SetTitle(QueryText);
        map.SetCenterAndZoom(LL, 16);
    }
//    else if (geoType == 'Cities') {
//        map.SetCenterAndZoom(LL, 11);
//    }
}
    
//    if (LatLong.length == 4)
//    {
//    LatLong[3] = parseFloat(LatLong[3]);
//    LatLong[2] = parseFloat(LatLong[2]);
//    var DeltaLat = (LatLong[3]-LatLong[1]);
//    var DeltaLong = (LatLong[2]-LatLong[0]);
//    LatLong[1] = LatLong[1] + DeltaLat/2;
//    LatLong[0] = LatLong[0] + DeltaLong/2;
//    }
    
   // var LL = null;
     //   var mode = map.GetMapMode();
        //2D
        //if (mode == 1)
        //{
           // LatLong[1] = parseFloat(LatLong[1]);
           // LatLong[0] = parseFloat(LatLong[0]);
            //var LL = new VELatLong(LatLong[1],LatLong[0]);
       //     map.SetCenter(LL);          
               
        //}
        //3D
        //else if (mode == 2)
        //{
           // LL = e.latLong;
        //}
        
    
    //var LatLong = eventArgs.get_value();
    
//}


function OnSucceededCity(result) {
    // map.DeleteAllShapeLayers();
    if (result != null) {
        var polyRings = result.getElementsByTagName("RING").length;
        //for (var h = polyRings - 1; h > -1; h--) {
        for (var h = 0; h < polyRings; h++) {
            //ArcIMS 9.2+ has "HOLE" tags.  Not currently drawing.  Selecting the first child element COORDS of RING for the primary shape.
            var PolyCoords = result.getElementsByTagName("RING")[h].childNodes[0].text;
            var Points = PolyCoords.split(/\;/);
            var polyPoints = new Array();
            var LatLong = new Array();
            for (var i = 0; i < Points.length; i++) {
                LatLong = Points[i].split(/\ /);
                LatLong[1] = parseFloat(LatLong[1]);
                LatLong[0] = parseFloat(LatLong[0]);
                polyPoints.push(new VELatLong(LatLong[1], LatLong[0]));
            }
            var title = 'City';
            var sDescription = '';
            if (h == 0) {
                map.SetMapView(polyPoints);
            }
            AddFeaturePoly(polyPoints, title, sDescription, 'City')
        }
    }
}

function OnSucceededCounty(result) {
    // map.DeleteAllShapeLayers();
    if (result != null) {
        var polyRings = result.getElementsByTagName("RING").length;
        for (var h = polyRings - 1; h > -1; h--) {
            var PolyCoords = result.getElementsByTagName("RING")[h].text;
            var Points = PolyCoords.split(/\;/);
            var polyPoints = new Array();
            var LatLong = new Array();
            for (var i = 0; i < Points.length; i++) {
                LatLong = Points[i].split(/\ /);
                LatLong[1] = parseFloat(LatLong[1]);
                LatLong[0] = parseFloat(LatLong[0]);
                polyPoints.push(new VELatLong(LatLong[1], LatLong[0]));
            }
            var title = 'County';
            var sDescription = '';

            AddFeaturePoly(polyPoints, title, sDescription, 'County')
        }
    }
}
//var envMinX = result.getElementsByTagName("FEATURES")[0].lastChild.getAttribute("minx");
//envMinX = parseFloat(envMinX);
//var envMinY = result.getElementsByTagName("FEATURES")[0].lastChild.getAttribute("miny");
//envMinY = parseFloat(envMinY);
//var envMaxX = result.getElementsByTagName("FEATURES")[0].lastChild.getAttribute("maxx");
//envMaxX = parseFloat(envMaxX);
//var envMaxY = result.getElementsByTagName("FEATURES")[0].lastChild.getAttribute("maxy");
//envMaxY = parseFloat(envMaxY);
//var BondingPolyPoints = new Array();
//BondingPolyPoints.push(new VELatLong(envMinY,envMinX));
//BondingPolyPoints.push(new VELatLong(envMaxY,envMaxX));
//BondingPolyPoints.push(new VELatLong(envMinY,envMaxX));
//BondingPolyPoints.push(new VELatLong(envMaxY,envMinX));

//BoundingShape = new VEShape(VEShapeType.Polygon, BondingPolyPoints);
////map.AddShape(shape);                                   
//var BBpoints = BoundingShape.GetPoints();
//map.SetMapView(BBpoints);

