private RealtyDomainObjects.User AddNewUserIfNotExist(RealtyDomainObjects.PropertyObject po)
        {
            int membershipId = -1;
            RealtyDomainObjects.User user = null;
            if (Auth.IsAuthenticated(Request, User, out membershipId))
            {
                user = (from s in _db.Users
                        where s.MembershipId == membershipId
                        select s).SingleOrDefault<RealtyDomainObjects.User>();
                if (user == null)
                {
                    user = new RealtyDomainObjects.User()
                    {
                        MembershipId = membershipId,
                        Phone1 = po.Phone1,
                        Phone2 = po.Phone2,
                        Phone3 = po.Phone3,
                        url = po.SourceUrl,
                        UserName = po.ContactName
                    };
                    _db.Users.Add(user);
                    _db.SaveChanges();
                }
            }
            return user;

        }
        public ActionResult SavePropertyObject(RealtyDomainObjects.PropertyObject po)
        {
            string currency = Request.Form["Currency.Id"] as string;
            string priceForType = Request.Form["PriceForTypeName.Id"] as string;
            string PropertyType = Request.Form["PropertyType"] as string;
            string PropertyTypeAction = Request.Form["PropertyTypeAction"] as string;
            string PropertyTypeCities = Request.Form["PropertyTypeCities"] as string;
            string PropertyTypeCityDistrict = Request.Form["PropertyTypeCityDistrict"] as string;
            string PropertyTypePeriods = Request.Form["Periods.Id"] as string;
            string wcType = Request.Form["WCType.Id"] as string;
            string buildingType = Request.Form["BuildingTypeName.Id"] as string;

            currency = Server.HtmlEncode(currency);
            priceForType = Server.HtmlEncode(priceForType);
            PropertyType = Server.HtmlEncode(PropertyType);
            PropertyTypeAction = Server.HtmlEncode(PropertyTypeAction);
            PropertyTypeCities = Server.HtmlEncode(PropertyTypeCities);
            PropertyTypeCityDistrict = Server.HtmlEncode(PropertyTypeCityDistrict);
            PropertyTypePeriods = Server.HtmlEncode(PropertyTypePeriods);
            wcType = Server.HtmlEncode(wcType);
            buildingType = Server.HtmlEncode(buildingType);

            currency = Encoder.HtmlEncode(currency);
            priceForType = Encoder.HtmlEncode(priceForType);
            PropertyType = Encoder.HtmlEncode(PropertyType);
            PropertyTypeAction = Encoder.HtmlEncode(PropertyTypeAction);
            PropertyTypeCities = Encoder.HtmlEncode(PropertyTypeCities);
            PropertyTypeCityDistrict = Encoder.HtmlEncode(PropertyTypeCityDistrict);
            PropertyTypePeriods = Encoder.HtmlEncode(PropertyTypePeriods);
            wcType = Encoder.HtmlEncode(wcType);
            buildingType = Encoder.HtmlEncode(buildingType);

            

            int iCurrency, iPriceForType, iPropertyType, iPropertyTypeAction,
                iPropertyTypeCities, iPropertyTypeCityDistrict, iPeriods, iwcType, ibuildingType;

            if (int.TryParse(currency, out iCurrency)
                && int.TryParse(PropertyType, out iPropertyType)
                && int.TryParse(PropertyTypeAction, out iPropertyTypeAction)
                && int.TryParse(PropertyTypeCities, out iPropertyTypeCities)
                && int.TryParse(PropertyTypeCityDistrict, out iPropertyTypeCityDistrict))
            {
                try
                {
                    RealtyDomainObjects.User user = AddNewUserIfNotExist(po);

                    var currencyObj = (from s in _db.CurrencyTypes
                                       where s.Id == iCurrency
                                       select s).Single<CurrencyType>();

                    WCType wcTypeObj = null;

                    if (int.TryParse(wcType, out iwcType))
                    {
                        wcTypeObj = (from s in _db.WCTypes
                                     where s.Id == iwcType
                                     select s).Single<WCType>();
                    }
                    PriceForType priceforTypeObj = null;
                    if (int.TryParse(priceForType, out iPriceForType))
                    {
                        priceforTypeObj = (from s in _db.PriceForTypes
                                           where s.Id == iPriceForType
                                           select s).Single<PriceForType>();
                    }
                    Periods periods = null;
                    if (int.TryParse(PropertyTypePeriods, out iPeriods))
                    {
                        periods = (from s in _db.Periods
                                   where s.Id == iPeriods
                                   select s).Single<Periods>();
                    }

                    BuildingType buildingTypeObj = null;
                    if (int.TryParse(buildingType, out ibuildingType))
                    {
                        buildingTypeObj = (from s in _db.BuildingTypes
                                           where s.Id == ibuildingType
                                           select s).SingleOrDefault<BuildingType>();
                    }

                    var propertyTypeObj = (from s in _db.PropertyTypes
                                           where s.Id == iPropertyType
                                           select s).SingleOrDefault<PropertyType>();

                    var PropertyTypeActionObj = (from s in _db.PropertyActions
                                                 where s.Id == iPropertyTypeAction
                                                 select s).SingleOrDefault<PropertyAction>();

                    var PropertyTypeCitiesObj = (from s in _db.Cities
                                                 where s.Id == iPropertyTypeCities
                                                 select s).SingleOrDefault<City>();

                    var PropertyTypeCityDistrictObj = (from s in _db.CityDistricts
                                                       where s.Id == iPropertyTypeCityDistrict
                                                       select s).SingleOrDefault<CityDisctict>();

                    CommercialPropertyType commercialPropertyType = null;
                    if (po.CommercialPropertyType != null)
                    {
                        commercialPropertyType = (from s in _db.CommercialPropertyTypes
                                                  where s.Id == po.CommercialPropertyType.Id
                                                  select s).Single<CommercialPropertyType>();
                    }
                    ServiceType serviceType = null;
                    if (po.ServiceType != null)
                    {
                        serviceType = (from s in _db.ServiceTypes
                                       where s.Id == po.ServiceType.Id
                                       select s).Single<ServiceType>();
                    }

                    if (po.Id > 0)
                    {

                        po.IsActive = true;
                        po.Currency = currencyObj;
                        po.PriceForTypeName = priceforTypeObj;
                        po.PropertyType = propertyTypeObj;
                        po.PropertyAction = PropertyTypeActionObj;
                        po.City = PropertyTypeCitiesObj;
                        po.CityDistrict = PropertyTypeCityDistrictObj;
                        po.Periods = periods;
                        po.UserOwner = user;
                        po.CreatedDate = DateTime.Now;
                        po.WCType = wcTypeObj;
                        po.BuildingTypeName = buildingTypeObj;
                        po.ServiceType = serviceType;


                        var existingObj = (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 == po.Id
                                           select s).SingleOrDefault<RealtyDomainObjects.PropertyObject>();






                        existingObj.Currency = po.Currency;
                        existingObj.PriceForTypeName = po.PriceForTypeName;
                        existingObj.PropertyType = po.PropertyType;
                        existingObj.PropertyAction = po.PropertyAction;
                        existingObj.City = po.City;
                        existingObj.CityDistrict = po.CityDistrict;
                        existingObj.Periods = po.Periods;
                        existingObj.UserOwner = po.UserOwner;
                        existingObj.CreatedDate = po.CreatedDate;
                        existingObj.WCType = po.WCType;
                        existingObj.BuildingTypeName = po.BuildingTypeName;
                        existingObj.CommercialPropertyType = commercialPropertyType;
                        existingObj.ServiceType = serviceType;

                        existingObj.Title = po.Title;
                        existingObj.PropertyDescription = po.PropertyDescription;
                        existingObj.RoomCount = po.RoomCount;
                        existingObj.TotalSpace = po.TotalSpace;
                        existingObj.LivingSpace = po.LivingSpace;
                        existingObj.KitchenSpace = po.KitchenSpace;
                        existingObj.Floor = po.Floor;
                        existingObj.CountFloors = po.CountFloors;
                        existingObj.IsNewBuilding = po.IsNewBuilding;
                        existingObj.BalconAvailable = po.BalconAvailable;
                        existingObj.BalconSpace = po.BalconSpace;
                        existingObj.isBalconGlassed = po.isBalconGlassed;
                        existingObj.ContactName = po.ContactName;
                        existingObj.Price = po.Price;
                        existingObj.NoCommission = po.NoCommission;
                        existingObj.Phone1 = po.Phone1;
                        existingObj.Phone2 = po.Phone2;
                        existingObj.Phone3 = po.Phone3;
                        existingObj.SourceUrl = po.SourceUrl;
                        existingObj.DistanceToCity = po.DistanceToCity;
                        existingObj.CountPhotos = po.CountPhotos;
                        existingObj.IsDeleted = po.IsDeleted;
                        existingObj.DeletedDate = po.DeletedDate;

                        //_db.PropertyObjects.Attach(existingObj);
                        _db.Entry(existingObj).State = System.Data.EntityState.Modified;



                    }
                    else
                    {
                        po.IsActive = true;
                        po.Currency = currencyObj;
                        po.PriceForTypeName = priceforTypeObj;
                        po.PropertyType = propertyTypeObj;
                        po.PropertyAction = PropertyTypeActionObj;
                        po.City = PropertyTypeCitiesObj;
                        po.CityDistrict = PropertyTypeCityDistrictObj;
                        po.Periods = periods;
                        po.UserOwner = user;
                        po.CreatedDate = DateTime.Now;
                        po.WCType = wcTypeObj;
                        po.BuildingTypeName = buildingTypeObj;
                        po.CommercialPropertyType = commercialPropertyType;
                        po.ServiceType = serviceType;


                        _db.PropertyObjects.Add(po);


                        if (po.ServiceType != null)
                            _db.Entry(po.ServiceType).State = System.Data.EntityState.Unchanged;

                        if (po.CommercialPropertyType != null)
                            _db.Entry(po.CommercialPropertyType).State = System.Data.EntityState.Unchanged;

                        if (po.BuildingTypeName != null)
                            _db.Entry(po.BuildingTypeName).State = System.Data.EntityState.Unchanged;

                        if (po.WCType != null)
                            _db.Entry(po.WCType).State = System.Data.EntityState.Unchanged;

                        if (po.BuildingTypeName != null)
                            _db.Entry(po.BuildingTypeName).State = System.Data.EntityState.Unchanged;

                        if (po.Periods != null)
                            _db.Entry(po.Periods).State = System.Data.EntityState.Unchanged;

                        if (po.WCType != null)
                            _db.Entry(po.WCType).State = System.Data.EntityState.Unchanged;

                        if (user != null)
                            _db.Entry(po.UserOwner).State = System.Data.EntityState.Unchanged;

                        _db.Entry(po.Currency).State = System.Data.EntityState.Unchanged;

                        if (po.PriceForTypeName != null)
                            _db.Entry(po.PriceForTypeName).State = System.Data.EntityState.Unchanged;

                        if (po.PropertyType != null)
                            _db.Entry(po.PropertyType).State = System.Data.EntityState.Unchanged;

                        if (po.PropertyAction != null)
                            _db.Entry(po.PropertyAction).State = System.Data.EntityState.Unchanged;

                        if (po.City != null)
                            _db.Entry(po.City).State = System.Data.EntityState.Unchanged;

                        if (po.CityDistrict != null)
                            _db.Entry(po.CityDistrict).State = System.Data.EntityState.Unchanged;
                    }
                    _db.SaveChanges();
                    AddLandSpecifics(po);

                    //PropertyStatsHelper.CreateProperyStat(po, _db);

                }
                catch (Exception ex)
                {
                    return Content(errMsg + ": " + ex.Message);
                }
            }
            else
            {
                Content(errMsg);
            }

            return RedirectToAction("GetImageUploadPreview", new { objId = po.Id });
        }