/// <summary>
        /// Get a list of postcodes that match the search criteria
        /// </summary>
        /// <param name="logonId">Logon id obtained when logging on to the logon service.</param>
        /// <param name="criteria">Postcode Lookup search criteria</param>
        /// <returns></returns>
        public PostcodeLookupReturnValue PostcodeLookupSearch(Guid logonId, PostcodeLookupSearchCriteria criteria)
        {
            IrisLogManager _logError = new IrisLogManager(LogType.ERROR);

            PostcodeLookupReturnValue returnValue = new PostcodeLookupReturnValue();
            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            // Can do everything
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    StringBuilder sbQuery = new StringBuilder();

                    if (criteria.SearchStatus == string.Empty)
                    {

                        if (criteria.Address.HouseName != string.Empty)
                            sbQuery.Append(criteria.Address.HouseName + ", ");

                        if (criteria.Address.StreetNumber != string.Empty)
                            sbQuery.Append(criteria.Address.StreetNumber + ", ");

                        if (criteria.Address.Line1 != string.Empty)
                            sbQuery.Append(criteria.Address.Line1 + ", ");

                        if (criteria.Address.Line2 != string.Empty)
                            sbQuery.Append(criteria.Address.Line2 + ", ");

                        if (criteria.Address.DependantLocality != string.Empty)
                            sbQuery.Append(criteria.Address.DependantLocality + ", ");

                        if (criteria.Address.Line3 != string.Empty)
                            sbQuery.Append(criteria.Address.Line3 + ", ");

                        if (criteria.Address.Town != string.Empty)
                            sbQuery.Append(criteria.Address.Town + ", ");

                        if (criteria.Address.County != string.Empty)
                            sbQuery.Append(criteria.Address.County + ", ");

                        if (criteria.Address.PostCode != string.Empty)
                            sbQuery.Append(criteria.Address.PostCode + ", ");

                        while (sbQuery.Length > 0 && (sbQuery[sbQuery.Length - 1] == ' ' || sbQuery[sbQuery.Length - 1] == ','))
                        {
                            sbQuery.Length--;
                        }
                    }
                    else
                    {
                        sbQuery.Append(criteria.SearchStatus);
                    }

                    _logError.TraceMessage(" Query = " + sbQuery.ToString());

                    SrvPostcodeLookup postcodeLookup = new SrvPostcodeLookup();
                    List<CapscanAddress> results = postcodeLookup.CapscanQuery(sbQuery.ToString(), criteria.AmbiguityId);

                    _logError.TraceMessage(" CapscanQuery Results Count = " + results.Count);

                    List<PostcodeLookupSearchItem> postcodeItemList = new List<PostcodeLookupSearchItem>();

                    if (results.Count == 0)
                    {
                        returnValue = null;
                    }
                    else
                    {
                        //Add each address
                        foreach (CapscanAddress cAddress in results)
                        {
                            PostcodeLookupSearchItem postcodeItem = new PostcodeLookupSearchItem();
                            postcodeItem.AdminCounty = cAddress.admincounty;
                            postcodeItem.AmbiguityId = cAddress.ambiguityid;
                            postcodeItem.AmbiguityText = cAddress.ambiguitytext;
                            postcodeItem.Buildingname = cAddress.buildingname;
                            postcodeItem.BuildingNumber = cAddress.buildingnumber;
                            postcodeItem.Country = cAddress.country;
                            postcodeItem.County = cAddress.county;
                            postcodeItem.DepLocality = cAddress.deplocality;
                            postcodeItem.DepStreet = cAddress.depstreet;
                            postcodeItem.Locality = cAddress.locality;
                            postcodeItem.Organisation = cAddress.organisation;
                            postcodeItem.Postcode = cAddress.postcode;
                            postcodeItem.PostTown = cAddress.posttown;
                            postcodeItem.Resolved = cAddress.resolved;
                            postcodeItem.SearchStatus = cAddress.searchstatus;
                            postcodeItem.Street = cAddress.street;
                            postcodeItem.SubBuilding = cAddress.subbuilding;

                            postcodeItemList.Add(postcodeItem);
                        }

                        if (postcodeItemList != null)
                        {
                            returnValue.PostcodeLookup = postcodeItemList;
                        }
                    }

                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (Exception Ex)
            {
                returnValue.Success = false;
                returnValue.Message = "Could not load postcode lookup for the current user. <br>Exception: " + Ex.Message;
            }

            return returnValue;
        }
        private ListItem CreateListViewItem(PostcodeLookupSearchItem cAddress)
        {
            StringBuilder sbAddress     = new StringBuilder();
            StringBuilder sbAddressText = new StringBuilder();


            sbAddress.Append(cAddress.SearchStatus + "~" + cAddress.AmbiguityId + "~");

            if (cAddress.AmbiguityText != string.Empty)
            {
                sbAddress.Append(cAddress.AmbiguityText + "~");
                sbAddressText.Append(cAddress.AmbiguityText + ", ");
            }

            if (cAddress.BuildingNumber != string.Empty && cAddress.AmbiguityText == string.Empty)
            {
                sbAddress.Append(cAddress.BuildingNumber + "~");
                sbAddressText.Append(cAddress.BuildingNumber + ", ");
            }

            if (cAddress.AmbiguityText == string.Empty && cAddress.BuildingNumber == string.Empty)
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Buildingname != string.Empty)
            {
                sbAddress.Append(cAddress.Buildingname + "~");
                sbAddressText.Append(cAddress.Buildingname + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Street != string.Empty)
            {
                sbAddress.Append(cAddress.Street + "~");
                sbAddressText.Append(cAddress.Street + ", ");
            }

            if (cAddress.DepStreet != string.Empty && cAddress.Street == string.Empty)
            {
                sbAddress.Append(cAddress.DepStreet + "~");
                sbAddressText.Append(cAddress.DepStreet + ", ");
            }

            if (cAddress.DepStreet == string.Empty && cAddress.Street == string.Empty)
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.DepLocality != string.Empty)
            {
                sbAddress.Append(cAddress.DepLocality + "~");
                sbAddressText.Append(cAddress.DepLocality + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Locality != string.Empty && cAddress.DepLocality == string.Empty)
            {
                sbAddress.Append(cAddress.Locality + "~");
                sbAddressText.Append(cAddress.Locality + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }


            if (cAddress.PostTown != string.Empty)
            {
                sbAddress.Append(cAddress.PostTown + "~");
                sbAddressText.Append(cAddress.PostTown + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.County != string.Empty)
            {
                sbAddress.Append(cAddress.County + "~");
                sbAddressText.Append(cAddress.County + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Postcode != string.Empty)
            {
                sbAddress.Append(cAddress.Postcode + "~");
                sbAddressText.Append(cAddress.Postcode + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Country != string.Empty)
            {
                sbAddress.Append(cAddress.Country + "~");
                sbAddressText.Append(cAddress.Country + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            while (sbAddress.Length > 0 && sbAddress[sbAddress.Length - 1] == '~')
            {
                sbAddress.Length--;
            }

            while (sbAddressText.Length > 0 && (sbAddressText[sbAddressText.Length - 1] == ' ' || sbAddressText[sbAddressText.Length - 1] == ','))
            {
                sbAddressText.Length--;
            }

            ListItem lvItem = new ListItem(sbAddressText.ToString(), sbAddress.ToString());

            return(lvItem);
        }
        private ListItem CreateListViewItem(PostcodeLookupSearchItem cAddress)
        {
            StringBuilder sbAddress = new StringBuilder();
            StringBuilder sbAddressText = new StringBuilder();

            sbAddress.Append(cAddress.SearchStatus + "~" + cAddress.AmbiguityId + "~");

            if (cAddress.AmbiguityText != string.Empty)
            {
                sbAddress.Append(cAddress.AmbiguityText + "~");
                sbAddressText.Append(cAddress.AmbiguityText + ", ");
            }

            if (cAddress.BuildingNumber != string.Empty && cAddress.AmbiguityText == string.Empty)
            {
                sbAddress.Append(cAddress.BuildingNumber + "~");
                sbAddressText.Append(cAddress.BuildingNumber + ", ");
            }

            if (cAddress.AmbiguityText == string.Empty && cAddress.BuildingNumber == string.Empty)
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Buildingname != string.Empty)
            {
                sbAddress.Append(cAddress.Buildingname + "~");
                sbAddressText.Append(cAddress.Buildingname + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Street != string.Empty)
            {
                sbAddress.Append(cAddress.Street + "~");
                sbAddressText.Append(cAddress.Street + ", ");
            }

            if (cAddress.DepStreet != string.Empty && cAddress.Street == string.Empty)
            {
                sbAddress.Append(cAddress.DepStreet + "~");
                sbAddressText.Append(cAddress.DepStreet + ", ");
            }

            if (cAddress.DepStreet == string.Empty && cAddress.Street == string.Empty)
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.DepLocality != string.Empty)
            {
                sbAddress.Append(cAddress.DepLocality + "~");
                sbAddressText.Append(cAddress.DepLocality + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Locality != string.Empty && cAddress.DepLocality == string.Empty)
            {
                sbAddress.Append(cAddress.Locality + "~");
                sbAddressText.Append(cAddress.Locality + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.PostTown != string.Empty)
            {
                sbAddress.Append(cAddress.PostTown + "~");
                sbAddressText.Append(cAddress.PostTown + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.County != string.Empty)
            {
                sbAddress.Append(cAddress.County + "~");
                sbAddressText.Append(cAddress.County + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Postcode != string.Empty)
            {
                sbAddress.Append(cAddress.Postcode + "~");
                sbAddressText.Append(cAddress.Postcode + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            if (cAddress.Country != string.Empty)
            {
                sbAddress.Append(cAddress.Country + "~");
                sbAddressText.Append(cAddress.Country + ", ");
            }
            else
            {
                sbAddress.Append("~");
                //sbAddressText.Append(", ");
            }

            while (sbAddress.Length > 0 && sbAddress[sbAddress.Length - 1] == '~')
            {
                sbAddress.Length--;
            }

            while (sbAddressText.Length > 0 && (sbAddressText[sbAddressText.Length - 1] == ' ' || sbAddressText[sbAddressText.Length - 1] == ','))
            {
                sbAddressText.Length--;
            }

            ListItem lvItem = new ListItem(sbAddressText.ToString(), sbAddress.ToString());

            return lvItem;
        }