/// <summary>
    /// Execute the distance search and creates the result set.
    /// </summary>
    public int ExecDistanceSearch()
    {
        ValidateInputs();

        //We use the alias AccountName to remain consistent with App Architect syntax.
        double lat, lon;

        GetLatLonForLocation(out lat, out lon);

        var results = DistanceSearchHelper.AccountDistanceSearch(lat, lon, Convert.ToDouble(cbxDistance.Text),
                                                                 pklType.PickListValue, pklSubType.PickListValue,
                                                                 Page.Session["m_sortExpression"].ToString(),
                                                                 ((SortDirection)Page.Session["m_sortDirection"] == SortDirection.Ascending),
                                                                 (!(cbxNumResults.Text == GetLocalResourceObject("Results_All").ToString() || cbxNumResults.Text == "")) ? Convert.ToInt32(cbxNumResults.Text) : 0,
                                                                 0, CurrentRegion.IsMetric);

        dgPostalCodeResults.DataSource = null;
        dgPostalCodeResults.DataSource = results;
        dgPostalCodeResults.DataBind();

        return((results != null) ? results.Count : 0);
    }
    /// <summary>
    /// This provides the data
    /// </summary>
    /// <returns></returns>
    protected string GenerateDataScript()
    {
        IList <DistanceDTO> results = null;
        StringBuilder       script  = new StringBuilder();

        string fromLoc        = Request.QueryString["from"];
        double fromLat        = Convert.ToDouble(Request.QueryString["fromlat"] ?? "0");
        double fromLon        = Convert.ToDouble(Request.QueryString["fromlon"] ?? "0");
        string entityType     = Request.QueryString["type"];
        double distance       = Convert.ToDouble(Request.QueryString["dist"] ?? "0");
        string accountType    = Request.QueryString["atype"];
        string accountSubType = Request.QueryString["asub"];
        string sortBy         = Request.QueryString["sort"];
        bool   sortAsc        = Convert.ToBoolean(Request.QueryString["asc"] ?? true.ToString());
        int    count          = Convert.ToInt32(Request.QueryString["cnt"] ?? "0");

        string         selected   = Request.QueryString["selected"];
        IList <string> selections = null;

        if (!string.IsNullOrEmpty(selected))
        {
            selections = new List <string>(selected.Split(';'));
        }

        // Contact Prox Search
        string contactType   = Request.QueryString["ctype"];
        string contactStatus = Request.QueryString["cstat"];

        // Map Group
        string group = Request.QueryString["grp"];

        if (entityType == null)
        {
            throw new Exception(GetLocalResourceObject("Error_EntityType_Null").ToString());
        }

        if (fromLoc != null && Request.QueryString["fromlat"] != null && Request.QueryString["fromlon"] != null)
        {
            script.AppendFormat("data.addPoint({{latitude: {0}, longitude: {1}, name: '{2}', description: '', address: '', type: 'poi', id: 'poi'}}, true);\n",
                                fromLat.ToString(CultureInfo.GetCultureInfo("en-US")), fromLon.ToString(CultureInfo.GetCultureInfo("en-US")),
                                PortalUtil.JavaScriptEncode(fromLoc, true));
        }

        if (string.IsNullOrEmpty(group))
        {
            // Use the query from the prox search page
            switch (entityType.ToUpper())
            {
            case "IACCOUNT":
            {
                results = DistanceSearchHelper.AccountDistanceSearch(
                    fromLat, fromLon, distance, accountType, accountSubType,
                    sortBy, sortAsc, count, 0, CurrentRegion.IsMetric);
                break;
            }

            case "ICONTACT":
            {
                results = DistanceSearchHelper.ContactDistanceSearch(
                    fromLat, fromLon, distance, contactType, contactStatus,
                    sortBy, sortAsc, count, 0, CurrentRegion.IsMetric);
                break;
            }
            }
        }
        else
        {
            results = GetGroupData(entityType, group, sortBy, sortAsc, count, selections);
        }

        // Add the points to the map
        if (results != null && results.Count > 0)
        {
            foreach (var dist in results)
            {
                // '*' is appended to descriptions when no geolocation data is avaialble.
                script.Append(
                    string.Format("data.addPoint(new SlxDataPoint({{latitude: {0}, longitude: {1}, name: '{2}', address: '{3}', description: '{7}', id: '{4}', entityid: '{5}', url: '{6}', type: 'data'}}), true);\n",
                                  (dist.GeocodeLatitude.HasValue) ? dist.GeocodeLatitude.Value.ToString(CultureInfo.GetCultureInfo("en-US")) : "0.0",
                                  (dist.GeocodeLongitude.HasValue) ? dist.GeocodeLongitude.Value.ToString(CultureInfo.GetCultureInfo("en-US")) : "0.0",
                                  PortalUtil.JavaScriptEncode(string.Format("{0}{1}", (entityType.ToUpper().Equals("IACCOUNT")) ? dist.AccountName : dist.NameLF, (dist.GeocodeLatitude.HasValue) ? string.Empty : "*"), true),
                                  PortalUtil.JavaScriptEncode(dist.DisplayAddress, true),
                                  PortalUtil.JavaScriptEncode(dist.AddressId, true),
                                  PortalUtil.JavaScriptEncode((entityType.ToUpper().Equals("IACCOUNT")) ? dist.AccountId : dist.ContactId, true),
                                  GetEntityUrl(entityType, dist.AccountId, dist.ContactId),
                                  ((dist.GeocodeLatitude.HasValue) ? string.Empty : "<em>" + GetLocalResourceObject("AddressFailedToGeocode").ToString() + "</em><br/><br/>") + GetDescription(dist.MainPhone, dist.AccountManagerLF)));
            }
        }

        return(script.ToString());
    }