public ActionResult ConvoWith(string interlocutor)
        {
            interlocutor = (string)RouteData.Values["id"];
            List<UserMessage> messages;
            CustomUserInfo user; ;
            using(CommonContext ctx = new CommonContext())
            {
                messages = (from items in ctx.UsersMessages 
                                                  where ((items.Sender == interlocutor && 
                                                        items.Receiver == User.Identity.Name) ||
                                                        (items.Receiver == interlocutor &&
                                                        items.Sender == User.Identity.Name))
                                                    select items).ToList<UserMessage>();
            
                List<MessageRel> mrl = (from items in ctx.MessageRels 
                                     where (items.Receiver == User.Identity.Name && items.Sender == interlocutor)
                                 select items).ToList<MessageRel>();
                if (mrl.Count != 0)
                {
                    MessageRel mr = mrl.Single<MessageRel>();
                    mr.Unread = false;
                    ctx.Entry(mr).State = System.Data.EntityState.Modified;
                    ctx.SaveChanges();   
                }

                user = (from items in ctx.CustomUserInfos
                        where items.UserName == interlocutor
                        select items).Single<CustomUserInfo>();
            }
            messages.Reverse();
            ViewBag.Messages = messages;
            ViewBag.Interlocutor = user;
            ViewBag.IsUnread = IsUnread();
            return View();
        }
        public ActionResult RemoveAccount(string userName, FormCollection collection)
        {
            userName = User.Identity.Name;
            try
            {
                // TODO: Add delete logic here
                if (Roles.GetRolesForUser(userName).Count() > 0)
                {
                    Roles.RemoveUserFromRoles(userName, Roles.GetRolesForUser(userName));
                }
                ((SimpleMembershipProvider)Membership.Provider).DeleteAccount(userName); // deletes record from webpages_Membership table
                ((SimpleMembershipProvider)Membership.Provider).DeleteUser(userName, true); // deletes record from UserProfile table
                
                using (CommonContext ctx = new CommonContext())
                {
                    CustomUserInfo user = (from items in ctx.CustomUserInfos 
                                               where items.UserName == User.Identity.Name
                                               select items).Single<CustomUserInfo>();
                    ctx.Entry(user).State = System.Data.EntityState.Deleted;
                    ctx.SaveChanges();
                }
                
                foreach (var cookie in Request.Cookies.AllKeys)
                {
                    Request.Cookies.Remove(cookie);
                }
                foreach (var cookie in Response.Cookies.AllKeys)
                {
                    Response.Cookies.Remove(cookie);
                }

                // Invalidate the Cache on the Client Side
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetNoStore();

                return RedirectToAction("Index", "Ads");
            }
            catch
            {
                return View(userName);
            }
        }
        public ActionResult ConvoWith()
        {
            string message = Request.Form["MessageText"];
            DateTime date = DateTime.Now;
            string sender = User.Identity.Name;
            string receiver = Request.Form["MessageReceiver"];

            using (CommonContext ctx = new CommonContext())
            {
                CustomUserInfo senderInfo = (from items in ctx.CustomUserInfos
                                             where items.UserName == sender
                                             select items).Single<CustomUserInfo>();
                CustomUserInfo receiverInfo = (from items in ctx.CustomUserInfos
                                               where items.UserName == receiver
                                             select items).Single<CustomUserInfo>();

                UserMessage msg = new UserMessage();
                msg.Sender = sender;
                msg.SenderName = senderInfo.Nickname;
                msg.Receiver = receiver;
                msg.ReceiverName = receiverInfo.Nickname;
                msg.Message = message;
                msg.Date = date;

                List<MessageRel> ml = (from items in ctx.MessageRels
                                       where items.Receiver == receiver
                                       select items).ToList<MessageRel>();
                MessageRel seekingMR = null;
                if (ml.Count != 0)
                {
                    foreach (MessageRel item in ml)
                    {
                        if (item.Sender == sender)
                        {
                            seekingMR = item;
                            seekingMR.Unread = true;
                            ctx.Entry(seekingMR).State = System.Data.EntityState.Modified;
                        }
                    }
                    if (seekingMR == null)
                    {
                        seekingMR = new MessageRel();
                        seekingMR.Receiver = receiver;
                        seekingMR.Sender = sender;
                        seekingMR.Unread = true;
                        ctx.Entry(seekingMR).State = System.Data.EntityState.Added;
                    }
                }
                else
                {
                    seekingMR = new MessageRel();
                    seekingMR.Receiver = receiver;
                    seekingMR.Sender = sender;
                    seekingMR.Unread = true;
                    ctx.Entry(seekingMR).State = System.Data.EntityState.Added;
                }

                ctx.Entry(msg).State = System.Data.EntityState.Added;
                ctx.SaveChanges();
            }

            return RedirectToAction("ConvoWith/" + receiver);
        }
        public ActionResult Settings(CustomUserInfo model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                using (CommonContext ctx = new CommonContext())
                {

                    var user = (from items in ctx.CustomUserInfos
                                where
                                    items.UserName == User.Identity.Name
                                select items).Single();

                    if (file != null)
                    {
                        string[] fileName = (file.FileName).Split('.');
                        string ext = fileName[1];
                        string name = User.Identity.Name;
                        string uniqName = name + "." + ext;
                        string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/Users"), uniqName);

                        file.SaveAs(path);
                        user.Image = uniqName;
                    }
                    user.UserName = User.Identity.Name;
                    user.FirstName = model.FirstName;
                    user.LastName = model.LastName;
                    user.Nickname = model.Nickname;
                    user.Skills = model.Skills;
                    user.Country = model.Country;
                    user.Occupation = model.Occupation;
                    user.Interests = model.Interests;
                    user.Town = model.Town;
                    user.UserDescription = model.UserDescription;

                    ctx.Entry(user).State = System.Data.EntityState.Modified;
                    ctx.SaveChanges();
                }

            }
            return RedirectToAction("Index", "Ads");
        }
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);
                    using (CommonContext ctx = new CommonContext()){
                        CustomUserInfo user = new CustomUserInfo();
                        user.UserName = model.UserName;
                        user.FirstName = "Не вказано";
                        user.LastName = "Не вказано";
                        user.Nickname = "Не вказано";
                        user.Image = "default.png";
                        user.Country = "Не вказано";
                        user.Town = "Не вказано";
                        user.Occupation = "Не вказано";
                        user.Skills = "Не вказано";
                        user.UserDescription = "Не вказано";
                        user.LastTimeOnline = DateTime.Now;
                        ctx.Entry(user).State = System.Data.EntityState.Added;
                        ctx.SaveChanges();

                    }
                    return RedirectToAction("Index", "MySubs");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }