public PartialViewResult AddService(string PropertyType, string id = "")
        {

            PropertyType = Server.HtmlEncode(PropertyType);
            id = Server.HtmlEncode(id);

            PropertyType = Encoder.HtmlEncode(PropertyType);
            id = Encoder.HtmlEncode(id);

            int iPropertyType;

            if (int.TryParse(PropertyType, out iPropertyType))
            {
                SearchPageParams spr = new SearchPageParams()
                {
                    iPropertyType = iPropertyType,
                    iPropertyTypeAction = -1,
                    iPropertyTypeCities = -1,
                    iPropertyTypeCityDistrict = -1
                };
                ViewBag.SearchPageParams = spr;

                var currency = from s in _db.CurrencyTypes.AsQueryable()
                               select new { id = s.Id, name = s.CurrencyTypeName };
                var sl = new SelectList(currency, "id", "name");
                ViewBag.currency = sl;

                var priceForType = from s in _db.PriceForTypes.AsQueryable()
                                   select new { id = s.Id, name = s.PriveForTypeName };
                sl = new SelectList(priceForType, "id", "name");
                ViewBag.priceForType = sl;

                var serviceTypes = from s in _db.ServiceTypes
                                   select new { id = s.Id, name = s.ServiceTypeName };
                sl = new SelectList(serviceTypes, "id", "name");
                ViewBag.serviceTypes = sl;

                int membershipId;
                if (Auth.IsAuthenticated(Request, User, out membershipId))
                {
                    var userDetails = Auth.GetUserDetails(membershipId, _db);
                    if (!string.IsNullOrEmpty(id))
                    {
                        PropertyObject obj = null;
                        obj = GetPropertyObject(id);
                        if (obj != null)
                        {
                            if (!string.IsNullOrEmpty(obj.Phone1)) obj.Phone1 = userDetails.Phone1;
                            if (!string.IsNullOrEmpty(obj.Phone2)) obj.Phone1 = userDetails.Phone2;
                            if (!string.IsNullOrEmpty(obj.Phone3)) obj.Phone1 = userDetails.Phone3;
                            if (!string.IsNullOrEmpty(obj.SourceUrl)) obj.Phone1 = userDetails.SourceUrl;
                            if (!string.IsNullOrEmpty(obj.ContactName)) obj.Phone1 = userDetails.ContactName;
                            return PartialView("AddService", obj);
                        }
                    }
                    return PartialView("AddService", userDetails);
                }
                return PartialView("AddService");
            }
            else
            {
                Content(errMsg);
            }
            return null;
        }
        public PartialViewResult AddLand(string PropertyType, string PropertyTypeAction, string PropertyTypeCities, string PropertyTypeCityDistrict, string id = "")
        {
            PropertyType = Server.HtmlEncode(PropertyType);
            PropertyTypeAction = Server.HtmlEncode(PropertyTypeAction);
            PropertyTypeCities = Server.HtmlEncode(PropertyTypeCities);
            PropertyTypeCityDistrict = Server.HtmlEncode(PropertyTypeCityDistrict);
            id = Server.HtmlEncode(id);


            PropertyType = Encoder.HtmlEncode(PropertyType);
            PropertyTypeAction = Encoder.HtmlEncode(PropertyTypeAction);
            PropertyTypeCities = Encoder.HtmlEncode(PropertyTypeCities);
            PropertyTypeCityDistrict = Encoder.HtmlEncode(PropertyTypeCityDistrict);
            id = Encoder.HtmlEncode(id);

            int iPropertyType, iPropertyTypeAction, iPropertyTypeCities, iPropertyTypeCityDistrict;

            if (int.TryParse(PropertyType, out iPropertyType) &&
                int.TryParse(PropertyTypeAction, out iPropertyTypeAction) &&
                int.TryParse(PropertyTypeCities, out iPropertyTypeCities) &&
                int.TryParse(PropertyTypeCityDistrict, out iPropertyTypeCityDistrict))
            {
                SearchPageParams spr = new SearchPageParams()
                {
                    iPropertyType = iPropertyType,
                    iPropertyTypeAction = iPropertyTypeAction,
                    iPropertyTypeCities = iPropertyTypeCities,
                    iPropertyTypeCityDistrict = iPropertyTypeCityDistrict
                };
                ViewBag.SearchPageParams = spr;

                var currency = from s in _db.CurrencyTypes.AsQueryable()
                               select new { id = s.Id, name = s.CurrencyTypeName };
                var sl = new SelectList(currency, "id", "name");
                ViewBag.currency = sl;

                var landCommunication = (from s in _db.LandCommunications
                                         select s).ToList<LandCommunication>();
                ViewBag.landCommunication = landCommunication;

                var landFunction = (from s in _db.LandFunctions
                                    select s).ToList<LandFunction>();

                ViewBag.landFunction = landFunction;

                int membershipId;
                if (Auth.IsAuthenticated(Request, User, out membershipId))
                {
                    var userDetails = Auth.GetUserDetails(membershipId, _db);
                    if (!string.IsNullOrEmpty(id))
                    {
                        PropertyObject obj = null;
                        obj = GetPropertyObject(id);
                        if (obj != null)
                        {
                            if (!string.IsNullOrEmpty(obj.Phone1)) obj.Phone1 = userDetails.Phone1;
                            if (!string.IsNullOrEmpty(obj.Phone2)) obj.Phone1 = userDetails.Phone2;
                            if (!string.IsNullOrEmpty(obj.Phone3)) obj.Phone1 = userDetails.Phone3;
                            if (!string.IsNullOrEmpty(obj.SourceUrl)) obj.Phone1 = userDetails.SourceUrl;
                            if (!string.IsNullOrEmpty(obj.ContactName)) obj.Phone1 = userDetails.ContactName;
                            return PartialView("AddLand", obj);
                        }
                    }
                    return PartialView("AddLand", userDetails);
                }
                return PartialView("AddLand");
            }
            else
            {
                Content(errMsg);
            }
            return null;
        }
        public ActionResult ShowForUpdatePropertyObject(string id)
        {
            id = Server.HtmlEncode(id);
            id = Encoder.HtmlEncode(id);

            if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                return Content(string.Empty);

            int iID = -1;
            if (!int.TryParse(id, out iID))
                return RedirectToAction("index", "home");

            var obj = (from s in _db.PropertyObjects
                       .Include("City")
                       .Include("CityDistrict")
                       .Include("PropertyType")
                       .Include("PropertyAction")
                       .Include("BuildingTypeName")
                       .Include("Currency")
                       .Include("PriceForTypeName")
                       .Include("UserOwner")
                       .Include("WCType")
                       .Include("CommercialPropertyType")
                       .Include("ServiceType")
                       .Include("Periods")

                       where s.Id == iID
                       select s).SingleOrDefault<PropertyObject>();

            if (obj == null)//not found
                return RedirectToAction("index", "home");

            SearchPageParams spr = new SearchPageParams()
            {
                iPropertyType = obj.PropertyType.Id,//iPropertyType,
                iPropertyTypeAction = obj.PropertyAction.Id,//iPropertyTypeAction,
                iPropertyTypeCities = obj.City.Id,//iPropertyTypeCities,
                iPropertyTypeCityDistrict = obj.CityDistrict.Id//iPropertyTypeCityDistrict
            };
            ViewBag.SearchPageParams = spr;

            SelectList sl = null;

            var currency = from s in _db.CurrencyTypes
                           //               select s;
                           select new { id = s.Id, name = s.CurrencyTypeName };

            sl = new SelectList(currency, "id", "name", obj.Currency.Id);

            ViewBag.currency = sl;

            var priceForType = from s in _db.PriceForTypes
                               select new { id = s.Id, name = s.PriveForTypeName };


            switch (obj.PropertyType.Id)
            {
                case 1:
                    var buildingType = from s in _db.BuildingTypes.AsQueryable()
                                       select new { id = s.Id, name = s.BuildingTypeName };
                    if (obj.BuildingTypeName != null)
                        sl = new SelectList(buildingType, "id", "name", obj.BuildingTypeName.Id.ToString());
                    else
                        sl = new SelectList(buildingType, "id", "name");
                    ViewBag.buildingType = sl;

                    var wcType = from s in _db.WCTypes.AsQueryable()
                                 select new { id = s.Id, name = s.WCTypeName };
                    if (obj.WCType != null)
                        sl = new SelectList(wcType, "id", "name", obj.WCType.Id);
                    else
                        sl = new SelectList(wcType, "id", "name");
                    ViewBag.wcType = sl;

                    if (obj.PriceForTypeName != null)
                        sl = new SelectList(priceForType, "id", "name", obj.PriceForTypeName.Id.ToString());
                    else
                        sl = new SelectList(priceForType, "id", "name");
                    ViewBag.priceForType = sl;

                    return PartialView("AddSellFlat", obj);

                case 2:

                    //sl = new SelectList(currency, "id", "name");

                    //ViewBag.currency = sl;

                    var chooseTypeAction = (from s in _db.PropertyActions
                                            where s.Id == obj.PropertyAction.Id//iPropertyTypeAction
                                            select s.PropertyActionName).Single<string>();
                    //выбрать на основе типа дейстивия /аренда/продажа, кикие еэлементы показывать а какие нет
                    if (chooseTypeAction.ToLower().Contains("аренда"))
                    {
                        var periods = from s in _db.Periods.AsQueryable()
                                      select new { id = s.Id, name = s.PeriodName };
                        if (obj.Periods != null)
                            sl = new SelectList(periods, "id", "name", obj.Periods.Id);
                        else
                            sl = new SelectList(periods, "id", "name");
                        ViewBag.periods = sl;
                    }
                    else
                    {
                        if (obj.PriceForTypeName != null)
                            sl = new SelectList(priceForType, "Id", "name", obj.PriceForTypeName.Id);
                        else
                            sl = new SelectList(priceForType, "Id", "name");
                        ViewBag.priceForType = sl;
                    }
                    return PartialView("AddCountryHouse", obj);

                case 3:
                    //sl = new SelectList(currency, "id", "name");
                    //ViewBag.currency = sl;

                    var landCommunication = (from s in _db.LandCommunications
                                             select s).ToList<LandCommunication>();
                    ViewBag.landCommunication = landCommunication;

                    var landFunction = (from s in _db.LandFunctions
                                        select s).ToList<LandFunction>();
                    ViewBag.landFunction = landFunction;

                    return PartialView("AddLand", obj);

                case 4:
                    //sl = new SelectList(currency, "id", "name");
                    //ViewBag.currency = sl;

                    var commercialPropertyType = from s in _db.CommercialPropertyTypes
                                                 select new { id = s.Id, name = s.CommercialPropertyTypeName };
                    if (obj.CommercialPropertyType != null)
                        sl = new SelectList(commercialPropertyType, "id", "name", obj.CommercialPropertyType.Id.ToString());
                    else
                        sl = new SelectList(commercialPropertyType, "id", "name");

                    ViewBag.commercialPropertyType = sl;

                    if (obj.PriceForTypeName != null)
                        sl = new SelectList(priceForType, "id", "name", obj.PriceForTypeName.Id.ToString());
                    else
                        sl = new SelectList(priceForType, "id", "name");
                    ViewBag.priceForType = sl;

                    return PartialView("AddCommercialProperty", obj);

                case 5:
                    //sl = new SelectList(currency, "id", "name");
                    //ViewBag.currency = sl;
                    if (obj.PriceForTypeName != null)
                        sl = new SelectList(priceForType, "id", "name", obj.PriceForTypeName.Id.ToString());
                    else
                        sl = new SelectList(priceForType, "id", "name");
                    ViewBag.priceForType = sl;

                    return PartialView("AddParkingGarage", obj);

                case 6:
                    //sl = new SelectList(currency, "id", "name");
                    //ViewBag.currency = sl;
                    if (obj.PriceForTypeName != null)
                        sl = new SelectList(priceForType, "id", "name", obj.PriceForTypeName.Id.ToString());
                    else
                        sl = new SelectList(priceForType, "id", "name");
                    ViewBag.priceForType = sl;

                    return PartialView("AddParkingGarage", obj);

                case 7:
                    //sl = new SelectList(currency, "id", "name");
                    //ViewBag.currency = sl;

                    if (obj.PriceForTypeName != null)
                        sl = new SelectList(priceForType, "id", "name", obj.PriceForTypeName.Id.ToString());
                    else
                        sl = new SelectList(priceForType, "id", "name");
                    ViewBag.priceForType = sl;

                    var serviceTypes = from s in _db.ServiceTypes
                                       select new { id = s.Id, name = s.ServiceTypeName };
                    if (obj.ServiceType != null)
                        sl = new SelectList(serviceTypes, "id", "name", obj.ServiceType.Id.ToString());
                    else
                        sl = new SelectList(serviceTypes, "id", "name");
                    ViewBag.serviceTypes = sl;
                    return PartialView("AddService", obj);
                default:
                    return RedirectToAction("index", "home");
            }



        }
        public PartialViewResult AddCountryHouse(string PropertyType, string PropertyTypeAction, string PropertyTypeCities, string PropertyTypeCityDistrict, string id = "")
        {
            
            PropertyType = Server.HtmlEncode(PropertyType);
            PropertyTypeAction = Server.HtmlEncode(PropertyTypeAction);
            PropertyTypeCities = Server.HtmlEncode(PropertyTypeCities);
            PropertyTypeCityDistrict = Server.HtmlEncode(PropertyTypeCityDistrict);
            id = Server.HtmlEncode(id);


            PropertyType = Encoder.HtmlEncode(PropertyType);
            PropertyTypeAction = Encoder.HtmlEncode(PropertyTypeAction);
            PropertyTypeCities = Encoder.HtmlEncode(PropertyTypeCities);
            PropertyTypeCityDistrict = Encoder.HtmlEncode(PropertyTypeCityDistrict);
            id = Encoder.HtmlEncode(id);


            int iPropertyType, iPropertyTypeAction, iPropertyTypeCities, iPropertyTypeCityDistrict;

            if (int.TryParse(PropertyType, out iPropertyType) &&
                int.TryParse(PropertyTypeAction, out iPropertyTypeAction) &&
                int.TryParse(PropertyTypeCities, out iPropertyTypeCities) &&
                int.TryParse(PropertyTypeCityDistrict, out iPropertyTypeCityDistrict))
            {
                SearchPageParams spr = new SearchPageParams()
                {
                    iPropertyType = iPropertyType,
                    iPropertyTypeAction = iPropertyTypeAction,
                    iPropertyTypeCities = iPropertyTypeCities,
                    iPropertyTypeCityDistrict = iPropertyTypeCityDistrict
                };
                ViewBag.SearchPageParams = spr;

                var currency = from s in _db.CurrencyTypes.AsQueryable()
                               select new { id = s.Id, name = s.CurrencyTypeName };
                var sl = new SelectList(currency, "id", "name");
                ViewBag.currency = sl;

                var chooseTypeAction = (from s in _db.PropertyActions
                                        where s.Id == iPropertyTypeAction
                                        select s.PropertyActionName).Single<string>();
                //выбрать на основе типа дейстивия /аренда/продажа, кикие еэлементы показывать а какие нет
                if (chooseTypeAction.ToLower() == "аренда")
                {
                    var periods = from s in _db.Periods.AsQueryable()
                                  select new { id = s.Id, name = s.PeriodName };
                    sl = new SelectList(periods, "id", "name");
                    ViewBag.periods = sl;
                }
                else
                {
                    var priceForType = from s in _db.PriceForTypes.AsQueryable()
                                       select new { id = s.Id, name = s.PriveForTypeName };
                    sl = new SelectList(priceForType, "id", "name");
                    ViewBag.priceForType = sl;
                }

                int membershipId;
                if (Auth.IsAuthenticated(Request, User, out membershipId))
                {
                    var userDetails = Auth.GetUserDetails(membershipId, _db);
                    if (!string.IsNullOrEmpty(id))
                    {
                        PropertyObject obj = null;
                        obj = GetPropertyObject(id);
                        if (obj != null)
                        {
                            if (!string.IsNullOrEmpty(obj.Phone1)) obj.Phone1 = userDetails.Phone1;
                            if (!string.IsNullOrEmpty(obj.Phone2)) obj.Phone1 = userDetails.Phone2;
                            if (!string.IsNullOrEmpty(obj.Phone3)) obj.Phone1 = userDetails.Phone3;
                            if (!string.IsNullOrEmpty(obj.SourceUrl)) obj.Phone1 = userDetails.SourceUrl;
                            if (!string.IsNullOrEmpty(obj.ContactName)) obj.Phone1 = userDetails.ContactName;
                            return PartialView("AddCountryHouse", obj);
                        }
                    }
                    return PartialView("AddCountryHouse", userDetails);
                }
                return PartialView("AddCountryHouse");
            }
            else
            {
                Content(errMsg);
            }
            return null;
        }
        public PartialViewResult ShowPaging(
            string PropertyType,
            string PropertyTypeAction,
            string PropertyTypeCities,
            string PropertyTypeCityDistrict,
            string NavigatePage,
            string SortType,
            string ShowType,
            string pricemin,
            string pricemax,
            string currency,
            string commercialPropertyType="",
            string countFloors="",
            string distToCityMin="",
            string distToCityMax="",
            string buildingType="",
            string wcType="",
            string roomCount="",
            string sizemin="",
            string sizemax=""
            )
        {
           

            int pageSize = WebRealty.Common.PagingHelper.GetPageSize();

            int iPropertyType, iPropertyTypeAction, iPropertyTypeCities, iPropertyTypeCityDistrict, iNavigatePage, iSortTypeDll,
                iShowType, iPriceMin, iPriceMax, iCurrency,
                iBuildingType, iWcType, iRoomCount, iSizemin, iSizemax,
                iCommercialPropertyType, iCountFloors, iDistToCityMin, iDistToCityMax;

            iPropertyType = iPropertyTypeAction = iPropertyTypeCities = iPropertyTypeCityDistrict = iSortTypeDll = iPriceMin = iPriceMax = iCurrency = -1;
            iNavigatePage = iShowType = 
                iBuildingType= iWcType= iRoomCount= iSizemin= iSizemax=
                iCommercialPropertyType= iCountFloors= iDistToCityMin= iDistToCityMax=1;

            if (!int.TryParse(PropertyType, out iPropertyType)) iPropertyType = -1;
            if (!int.TryParse(PropertyTypeAction, out iPropertyTypeAction)) iPropertyTypeAction = -1;
            if (!int.TryParse(PropertyTypeCities, out iPropertyTypeCities)) iPropertyTypeCities = -1;
            if (!int.TryParse(PropertyTypeCityDistrict, out iPropertyTypeCityDistrict)) iPropertyTypeCityDistrict = -1;
            if (!int.TryParse(NavigatePage, out iNavigatePage)) iNavigatePage = 1;
            if (!int.TryParse(SortType, out iSortTypeDll)) iSortTypeDll = 1;
            if (!int.TryParse(ShowType, out iShowType)) iShowType = -1;

            if (!int.TryParse(commercialPropertyType, out iCommercialPropertyType)) iCommercialPropertyType = -1;
            if (!int.TryParse(countFloors, out iCountFloors)) iCountFloors = -1;
            if (!int.TryParse(distToCityMin, out iDistToCityMin)) iDistToCityMin = -1;
            if (!int.TryParse(distToCityMax, out iDistToCityMax)) iDistToCityMax = -1;
            if (!int.TryParse(buildingType, out iBuildingType)) iBuildingType = -1;
            if (!int.TryParse(wcType, out iWcType)) iWcType = -1;
            if (!int.TryParse(roomCount, out iRoomCount)) iRoomCount = -1;
            if (!int.TryParse(sizemin, out iSizemin)) iSizemin = -1;
            if (!int.TryParse(sizemax, out iSizemax)) iSizemax = -1;


            int countObjs = 0;
            var result = from s in _db.PropertyObjects
                         where s.IsDeleted == (int)DeleteStatus.Available
                         select s;
            if (iPropertyType > -1)
                result = result.Where(x => x.PropertyType.Id == iPropertyType);

            if (iPropertyTypeAction > -1)
                result = result.Where(x => x.PropertyAction.Id == iPropertyTypeAction);

            if (iPropertyTypeCities > -1)
                result = result.Where(x => x.City.Id == iPropertyTypeCities);

            if (iPropertyTypeCityDistrict > -1)
                result = result.Where(x => x.CityDistrict.Id == iPropertyTypeCityDistrict);

            if (iPriceMin != -1 && iCurrency != -1)
                result = result.Where(q => q.Price >= iPriceMin && q.Currency.Id == iCurrency);

            if (iPriceMax != -1 && iCurrency != -1)
                result = result.Where(q => q.Price <= iPriceMax && q.Currency.Id == iCurrency);


            if (iBuildingType != -1)
                result = result.Where(q => q.BuildingTypeName.Id == iBuildingType);
            if (iWcType != -1)
                result = result.Where(q => q.WCType.Id == iWcType);
            if (iRoomCount != -1)
                if (roomCount == "более")//если  пришло  "более" значит  ищем  количество комнат больше  4
                    result = result.Where(q => q.RoomCount > 4);//4- ВАЖНО
                else
                    result = result.Where(q => q.RoomCount == iRoomCount);
            if (iSizemin != -1)
                result = result.Where(q => q.TotalSpace >= iSizemin);
            if (iSizemax != -1)
                result = result.Where(q => q.TotalSpace <= iSizemax);

            if (iCommercialPropertyType != -1)
                result = result.Where(q => q.CommercialPropertyType.Id == iCommercialPropertyType);

            if (iCountFloors != -1)
                result = result.Where(q => q.CountFloors == iCountFloors);

            if (iDistToCityMin != -1)
                result = result.Where(q => q.DistanceToCity >= iDistToCityMin);

            if (iDistToCityMax != -1)
                result = result.Where(q => q.DistanceToCity <= iDistToCityMax);

            countObjs = (from s in result
                         select 1).Count<int>();



            SearchPageParams model = new SearchPageParams()
            {
                iPropertyType = iPropertyType,
                iPropertyTypeAction = iPropertyTypeAction,
                iPropertyTypeCities = iPropertyTypeCities,
                iPropertyTypeCityDistrict = iPropertyTypeCityDistrict,
                countObjectFound = countObjs,
                countPagesFound = countObjs / pageSize,
                currentPage = iNavigatePage,
                navigatePage = iNavigatePage,
                ShowType = iShowType,
                SortType = iSortTypeDll,
                
                commercialPropertyType=iCommercialPropertyType,
                countFloors=iCountFloors,
                distToCityMin=iDistToCityMin,
                distToCityMax=iDistToCityMax,
                buildingType=iBuildingType,
                wcType=iWcType,
                roomCount=iRoomCount,
                sizemin=iSizemin,
                sizemax = iSizemax
            };
            if (countObjs % pageSize > 0) model.countPagesFound++;
            return PartialView("ShowPaging", model);
        }