﻿// JScript File
var GotFeature = true;
var owner;
var DemandFiled;
var County;
var ClaimSizePop;
var CurrentZoning;
var LandDiv;
var NumLots;
var TypeofDeve;
var Notes;
var Compensation;


function onMapChangeViewHandler()
{
  var view = map.GetMapView();
  var MinX = view.TopLeftLatLong.Longitude;
  var MaxY = view.TopLeftLatLong.Latitude;
  var MaxX = view.BottomRightLatLong.Longitude;
  var MinY = view.BottomRightLatLong.Latitude;  
  var ParamMaxX = {"Long": MaxX};
  var ParamMaxY = {"Lat": MaxY};
  var ParamMinX = {"Long": MinX};
  var ParamMinY = {"Lat": MinY};  
  WebServices.GetFeatureWebService.GetCounties(ParamMaxX.Long, ParamMaxY.Lat, ParamMinX.Long, ParamMinY.Lat, OnSucceededCounties, OnFailed);     

}

function onClickHandler(e)
{       
        ShapeLayerClaims.DeleteAllShapes();              

        var LL = null;
        var mode = map.GetMapMode();
        //2D
        if (mode == 1)
        {
            var x = e.mapX;
            var y = e.mapY;
            pixel = new VEPixel(x, y);
            LL = map.PixelToLatLong(pixel);        
        }
        //3D
        else if (mode == 2)
        {
            LL = e.latLong;            
        }
        var Lat = LL.Latitude;
        var Long = LL.Longitude;
        var ParamLat = {"Lat": Lat};
        var ParamLong = {"Long": Long}; 
        var selectedClaimLayers = $get('hiddenFld_claimSelected');
        var selectedClaimLayerProp = selectedClaimLayers.value.split(/\;/);
        var activeLayers = selectedClaimLayerProp[3];
        var activeService = selectedClaimLayerProp[0];           
        var ParamActiveLayers = {"Layers": activeLayers};
        var ParamService = {"Service": activeService};                     
        WebServices.GetFeatureWebService.GetFeature(ParamLat.Lat, ParamLong.Long, ParamActiveLayers.Layers, ParamService.Service, OnSucceeded, OnFailed);     
       
}

function OnSucceeded(result) {
    if (result != null) {
        var polyRings = result.getElementsByTagName("RING").length;
        for (var h = 0; h < polyRings; 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]));
            }

            owner = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.CLAIMANT_1");
            DemandFiled = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.DEMAND_FIL");
            County = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.COUNTY");
            ClaimSizePop = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.PARCEL_SIZ");
            CurrentZoning = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.CURRENT_ZO");
            LandDiv = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.TYPE_OF_LA");
            NumLots = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.NO__OF_LOT");
            TypeofDev = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.TYPE_OF_DE");
            Notes = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.NOTES_DES");
            Compensation = result.getElementsByTagName("FIELDS")[0].getAttribute("SDE.SDE.M37PARCELS15.COMPANSATI");
            var title = 'Claim Information';

            var sDescription = setDescriptionFields();

            AddFeaturePoly(polyPoints, title, sDescription, '')
        }
    }
}


function setDescriptionFields()
{
    var string = '<table id="PopUpTable">';
    if (owner != null)
    {
    string += '<tr><td class="PopUpDataTitle">Claimant:</td><td class="PopUpData">'+owner+'</td><tr>';
    }
    if (DemandFiled != null)
    {
    string += '<tr><td class="PopUpDataTitle">Demand filed with:</td><td class="PopUpData">'+DemandFiled+'</td><tr>';
    }
    if (County != null)
    {
    string += '<tr><td class="PopUpDataTitle">County:</td><td class="PopUpData">'+County+'</td><tr>';
    }
    if (ClaimSizePop != null)
    {string += '<tr><td class="PopUpDataTitle">Claim Size:</td><td class="PopUpData">'+ClaimSizePop+'</td><tr>';}
    if (CurrentZoning != null)
    {
    string += '<tr><td class="PopUpDataTitle">Current Zoning:</td><td class="PopUpData">'+CurrentZoning+'</td><tr>';
    }
    if (LandDiv != null)
    {
    string += '<tr><td class="PopUpDataTitle">Type of Land Division Requested:</td><td class="PopUpData">'+LandDiv+'</td><tr>';
    }
    if (TypeofDeve != null)
    {
    string += '<tr><td class="PopUpDataTitle">Type of Development:</td><td class="PopUpData">'+TypeofDeve+'</td><tr>';
    }
    if (NumLots != null)
    {
    string += '<tr><td class="PopUpDataTitle">Number of Lots:</td><td class="PopUpData">'+NumLots+'</td><tr>';
    }
    if (Compensation != null)    
    {
    Compensation = formatCurrency(Compensation);
    string += '<tr><td class="PopUpDataTitle">Compensation Requested:</td><td class="PopUpData">'+Compensation+'</td><tr>';
    }
    if (Notes != null)
    {
    string += '<tr><td class="PopUpDataTitle">Other Descriptions:</td><td class="PopUpData">'+Notes+'</td><tr>';
    }    
    string += '</table>';
    return string;
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function AddFeaturePoly(polyPoints, title, sDescription, isCityCounty) {
    shape = new VEShape(VEShapeType.Polygon, polyPoints);
    var icon = new VECustomIconSpecification();
    var layer = null;
    icon.Image = 'blankIcon.gif';
    shape.SetTitle(title);
    shape.SetDescription(sDescription); shape.SetCustomIcon(icon);
    shape.ShowDetailOnMouseOver = false;
    shape.SetCustomIcon(icon);
    if (isCityCounty != '') {
        shape.SetLineWidth(3);
        shape.SetLineColor(new VEColor(102, 0, 0, 1.0));
        shape.SetFillColor(new VEColor(102, 0, 0, 0.1));
        //ShapeLayerCityCounty.AddShape(shape);    
        map.AddShape(shape);
        if (isCityCounty == 'County') {
        var points = shape.GetPoints();
         map.SetMapView(points);
        }
    }
    else {
        shape.SetLineWidth(3);
        shape.SetZIndex(100);
        shape.SetLineColor(new VEColor(0, 150, 100, 1.0));
        shape.SetFillColor(new VEColor(0, 150, 100, 0.9));
        //ShapeLayerClaims.DeleteAllShapes();          
        ShapeLayerClaims.AddShape(shape);
    }
}

function OnFailed(result)
{
//alert(result);
}

function onMouseOverHandler(e)
{
if (e.elementID)
  {
        // e.elementID is the root ID, plus extensions.
        //alert("ID of shape on mouseover is " + e.elementID);
        // Use VEMap.GetShapeByID, then VEShape.GetID
        // to return the root ID.
        try
        {
        var shapeT = map.GetShapeByID(e.elementID);
        var shapeTTitle = shapeT.GetTitle();
        if (shapeTTitle != 'City' && shapeTTitle != 'County')
            {
                map.ShowInfoBox(shapeT);
            }
        }
        catch (exp)
        {
        try
        {
        //var ShapeLayerClaimsID = ShapeLayerClaims.iid;
        //var isShapeClaim = e.elementID.search(ShapeLayerClaimsID);
       //if (isShapeClaim != -1)
        //{
        //var shapeID = ShapeLayerClaims.GetShapeByIndex(0).iid
           // var shape = map.GetShapeByID(shapeID);
            var shapeTitle = shape.GetTitle();
            if (shapeTitle != 'City' && shapeTitle != 'County' )
            {
                map.ShowInfoBox(shape);
            }
       // }
        }
        catch (ex)
        {}    
        }   
        //alert("ID of shape on mouseover is " + id);
        return true;
    }
}

function onMouseOutHandler(e)
{
if (e.elementID)
  {
        // e.elementID is the root ID, plus extensions.
        //alert("ID of shape on mouseover is " + e.elementID);
        // Use VEMap.GetShapeByID, then VEShape.GetID
        // to return the root ID.
        var shape = map.GetShapeByID(e.elementID);
        map.HideInfoBox(shape);
        //alert("ID of shape on mouseover is " + id);
        return true;
    }
}
// FadeTransitions property accessors
//    get_FadeTransitions : function() {
//        return this._fadeTransitions;
//    }
//    set_FadeTransitions : function(value) {
//        if (this._fadeTransitions != value) {
//            this._fadeTransitions = value;
//            this._disposeAnimations();
//            this._animations = [];
//            this._fadeAnimations = [];
//            this._lengthAnimations = [];
//            if (!this._fadeTransitions) {
//                for (var i = this._panes.length - 1; i >= 0; i--) {
//                    CommonToolkitScripts.setElementOpacity(this._panes[i], 1);
//                }
//            }
//            this.raisePropertyChanged('FadeTransitions');
//        }
//    }
function OnSucceededCounties(result)
{
   if (result != null)
   {
    var divCountiesInView = $get('CountiesInView');
    var Counties = result.split(/\,/);
    if (Counties.length > 0)
    {
    var innerHTML = '<table>';
    for (var i= 0; i<Counties.length;i++)
    {
    innerHTML += '<tr><td><a href=\"./reports.aspx?extent='+Counties[i]+'\" class=\"SelectCoLink\">'+Counties[i]+'</a></td>';
    if (Counties[i+1]!= undefined)
    {
    innerHTML += '<td style=\"width:10px\">&nbsp;</td><td><a href=\"./reports.aspx?extent='+Counties[i+1]+'\" class=\"SelectCoLink\">'+Counties[i+1]+'</a></td></tr>';
    }
    else
    {
    innerHTML += '</tr>';
    }
    i = i +1;
    }
    innerHTML += '<tr><td colspan=3 width=220px style=\"padding-top:5px; padding-bottom:5px; border-bottom:solid 1px #a7a7a7;\"><a href=\"./reports.aspx?\" class=\"SelectCoLink\"> All Counties with M37 data</a></td><tr></table>';
    divCountiesInView.innerHTML = innerHTML;
    }
    }
    
}
