﻿// Call the Web service method to get the lat/longs 
function FindZipCodeInfo(zipCode)  
{
    // Call the Web service method to get the lat/longs
    ZipCodeService.FindZipCodeInfo(zipCode, SucceededCallback, FailedCallback);  
}

//  Display any errors that occur
function FailedCallback(result)
{
    var message = error.get_message();
    alert('An unhandled exception has occurred:\n' + message);
}

// This is the callback function that 
// processes the value returned by the Web service.
function SucceededCallback(result)
{
    var points = result;
    if(points.length > 0)
    {
        var latLongTokens = points.split('|');
        var veLatLongs = new Array(latLongTokens.length - 1);
        
        var centerLatLong;
        for(index = 0; index < latLongTokens.length; index++)
        {
            //  format= {0},{1}|
            var latitude = latLongTokens[index].split(',')[0];
            var longitude = latLongTokens[index].split(',')[1];

            if(index == 0)
            {
                // center point of the zipcode
                centerLatLong = new VELatLong(latitude, longitude);                     
            }
            else
            {
                //  build the points array from the string
                veLatLongs[index - 1] = new VELatLong(latitude, longitude);
            }
        }
        
        var poly = new VEPolygon('polygon', veLatLongs, new VEColor(0,0,255,.2), new VEColor(0,0,255,1), 1);           

        //  clear any existing polygons
        map.DeleteAllPolygons();
        //  add our new polygon
        map.AddPolygon(poly);
        //  set the view to these points
        map.SetMapView(veLatLongs);
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
