﻿/// <reference path="ProgressBar.js" />

// Call the Web service method to get the lat/longs 
function FindZipCodeInfo(zipCode)  
{
    // Initialize the Progress Bar
    ShowProgressBar();
    
    // Call the Web service method to get the lat/longs
    ZipCodeService.FindZipCodeInfo(zipCode, SucceededCallback, FailedCallback);  
}

//  Display any errors that occur
function FailedCallback(results)
{
    // Close the Progress Bar
    HideProgressBar();
    

    var message = 'Unkown technical error';
    if (results._timedOut)
    { 
        message = 'We are experiencing heavy traffic at the moment, please try again.'; 
    }
    else 
    {
        message = results._message;
    }
    alert(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);
        
        
        // Close the Progress Bar
        HideProgressBar();
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

