public void Save()
        {
            var clearedAddedBillingAccountUsers = AddedBillingAccountUsers.Where(newUser => !string.IsNullOrWhiteSpace(newUser.Email));

            //validate
            bool errors = false;
            foreach (var newUser in clearedAddedBillingAccountUsers)
            {
                newUser.Validate();
                if (!newUser.HasErrors)
                    if (BillingAccount.Users.Contains(u => u.User.Email == newUser.Email) || AddedBillingAccountUsers.Contains(u => u != newUser && u.Email == newUser.Email))
                        newUser.AddError("Email", "The same email is added a few times!");

                if (newUser.HasErrors)
                    errors = true;
            }

            if (errors)
                return;

            var request = new SaveAssociationBillingAccountUsersRequest()
            {
                BillingAccountId = BillingAccount.Id,
                RemovedUserIds = RemovedBillingAccountUserIds,
                NewUserEmails = clearedAddedBillingAccountUsers.Select(newUser => newUser.Email).ToArray()
            };

            Service.BeginSaveBillingAccountUsers(request,
                CreateAsyncCallback(ar => Service.EndSaveBillingAccountUsers(ar), response =>
                {
                    if (response.EmailErrors.Count > 0)
                    {
                        foreach (var email in response.EmailErrors.Keys)
                        {
                            var newEmail = AddedBillingAccountUsers.SingleOrDefault(u => u.Email == email);
                            if (newEmail != null)
                                newEmail.AddError("Email", response.EmailErrors[email]);
                        }

                        HtmlPage.Window.Alert("Invalid email addresses");
                        return;
                    }
                    else
                        Completed.Raise(this, response.BillingAccount);
                }), null);
        }
        public SaveAssociationBillingAccountUsersResponse SaveBillingAccountUsers(SaveAssociationBillingAccountUsersRequest request)
        {
            var response = new SaveAssociationBillingAccountUsersResponse();

            using (var db = new LomsContext())
            {
                if (request.NewUserEmails != null)
                    foreach (var email in request.NewUserEmails)
                    {
                        var user = (from u in db.AssociationUsers
                                    where u.AssociationId == CurrentAssociationId && u.Email == email
                                    select u)
                                .SingleOrDefault();

                        if (user == null || !user.HasOnlineAccess)
                            response.EmailErrors[email] = "User is not registered!";
                        else if (response.EmailErrors.Count == 0)
                        {
                            var billingAccountUser = new AssociationBillingAccountUser()
                            {
                                BillingAccountId = request.BillingAccountId,
                                UserId = user.Id
                            };
                            db.AssociationBillingAccountUsers.ApplyChanges(billingAccountUser);
                        }
                    }

                if (response.EmailErrors.Count == 0)
                {
                    if (request.RemovedUserIds != null)
                        foreach (var billingAccountUserId in request.RemovedUserIds)
                        {
                            var user = new AssociationBillingAccountUser() { Id = billingAccountUserId };
                            db.AssociationBillingAccountUsers.Attach(user);
                            db.AssociationBillingAccountUsers.DeleteObject(user);
                        }

                    db.SaveChanges();

                    response.BillingAccount = (from cb in db.AssociationBillingAccounts.IncludeAll("Country", "Users", "Users.User")
                                               where cb.Id == request.BillingAccountId
                                           select cb).Single();
                }

                return response;
            }
        }