public static UserCreateResults CreateSupplier(string email, string password, Int64 CityId, out AppSupplier supplier)
        {
            supplier = null;
            if (!email.IsValidEmail())
            {
                return(UserCreateResults.InvalidEmailAddress);
            }
            supplier = AppSupplier.FetchByEmail(email);
            if (supplier != null)
            {
                return(UserCreateResults.AlreadyExists);
            }
            supplier                = new AppSupplier();
            supplier.Email          = email;
            supplier.UniqueIdString = email.NormalizeEmail();

            string pwd, salt;

            EncodePassword(password, out pwd, out salt);
            supplier.Password     = pwd;
            supplier.PasswordSalt = salt;

            try
            {
                supplier.AddressLocation = new Geometry.Point(0, 0);//TODO
                supplier.CityId          = CityId;
                supplier.Save();
                return(UserCreateResults.Success);
            }
            catch
            {
                supplier = null;
                return(UserCreateResults.UnknownError);
            }
        }
        public override void Post(HttpRequest Request, HttpResponse Response, params string[] PathParams)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetMaxAge(TimeSpan.Zero);

            JObject inputData = null;

            try
            {
                using (StreamReader reader = new StreamReader(Request.InputStream))
                {
                    using (JsonTextReader jsonReader = new JsonTextReader(reader))
                    {
                        inputData = JObject.Load(jsonReader);
                    }
                }
            }
            catch
            {
                RespondBadRequest(Response);
            }

            string email = inputData.Value <string>(@"email") ?? "";

            Response.ContentType = @"application/json";

            string      key  = AppMembership.GenerateRecoveryKeySupplier(email);
            AppSupplier user = null;

            if (key != null)
            {
                user = AppSupplier.FetchByEmail(email);
            }

            if (key == null || user == null)
            {
                RespondNotFound(Response);
            }
            else
            {
                EmailMessagingService.SendPasswordRecoveryMailForSupplier(user, key, "he-IL");

                using (StreamWriter streamWriter = new StreamWriter(Response.OutputStream))
                {
                    using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
                    {
                        jsonWriter.WriteStartObject();
                        jsonWriter.WriteEndObject();
                    }
                }
            }
        }
        public static UserPasswordChangeResults ChangeSupplierPassword(string email, string newPassword)
        {
            AppSupplier supplier = AppSupplier.FetchByEmail(email);

            if (supplier == null)
            {
                return(UserPasswordChangeResults.UserDoesNotExist);
            }
            if (string.IsNullOrEmpty(supplier.PasswordSalt))
            {
                string pass, salt;
                EncodePassword(newPassword, out pass, out salt);
                supplier.Password     = pass;
                supplier.PasswordSalt = salt;
            }
            else
            {
                supplier.Password = EncodePassword(newPassword, supplier.PasswordSalt);
            }
            supplier.Save();
            return(UserPasswordChangeResults.Success);
        }
        public static UserRecoveryResults SupplierVerifyRecoveryKey(string email, string key, string newPassword)
        {
            AppSupplier user = AppSupplier.FetchByEmail(email);

            if (user == null)
            {
                return(UserRecoveryResults.UserDoesNotExist);
            }

            if (user.PasswordRecoveryKey != key)
            {
                return(UserRecoveryResults.KeyDoNotMatch);
            }

            if (user.PasswordRecoveryDate.AddHours(RecoveryKeyLifeInHours) < DateTime.UtcNow)
            {
                return(UserRecoveryResults.Expired);
            }

            if (newPassword == null)
            {
                return(UserRecoveryResults.Success);
            }
            else
            {
                string pwd, salt;
                EncodePassword(newPassword, out pwd, out salt);
                user.Password             = pwd;
                user.PasswordSalt         = salt;
                user.PasswordRecoveryKey  = @"";
                user.PasswordRecoveryDate = DateTime.UtcNow;
                user.IsLocked             = false;
                user.Save();

                return(UserRecoveryResults.Success);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            string SupplierEmail = null;
            bool   EmailChanged  = false;

            AppSupplier supplier = null;

            if (IsNewMode)
            {
                Membership.UserCreateResults results = Membership.CreateSupplier(txtEmail.Text, txtPassword.Text.Trim(), Convert.ToInt64(ddlCity.SelectedValue), out supplier);
                switch (results)
                {
                default:
                case Membership.UserCreateResults.UnknownError:
                    Master.MessageCenter.DisplayErrorMessage(SuppliersStrings.GetText(@"MessageCreateFailedUnknown"));
                    return;

                case Membership.UserCreateResults.AlreadyExists:
                    Master.MessageCenter.DisplayErrorMessage(SuppliersStrings.GetText(@"MessageCreateFailedAlreadyExists"));
                    return;

                case Membership.UserCreateResults.InvalidEmailAddress:
                    Master.MessageCenter.DisplayErrorMessage(SuppliersStrings.GetText(@"MessageCreateFailedEmailAddressInvalid"));
                    return;

                case Membership.UserCreateResults.Success:
                    break;
                }
                SupplierId    = supplier.SupplierId;
                SupplierEmail = supplier.Email;
                //supplier.OrderDisplay = OrderDisplay.GetLastOrder() + 1;
            }
            else
            {
                supplier      = core.DAL.AppSupplier.FetchByID(SupplierId);
                SupplierEmail = supplier.Email;
            }
            supplier.BusinessName = txtBusinessName.Text;

            if (ddlIsProduct.SelectedValue == "prod")
            {
                supplier.IsProduct = true;
                supplier.IsService = false;
            }
            else
            {
                supplier.IsProduct = false;
                supplier.IsService = true;
            }
            //supplier.IsProduct = chkIsProduct.Checked;
            //supplier.IsService = chkIsService.Checked;
            supplier.IsPremium    = chkIsPremium.Checked;
            supplier.IsLocked     = chkIsLocked.Checked;
            supplier.ContactName  = txtContactName.Text;
            supplier.ContactPhone = txtContactPhone.Text;
            supplier.Phone        = txtPhone.Text;
            supplier.CityId       = Convert.ToInt64(ddlCity.SelectedValue);
            supplier.Street       = txtStreet.Text;
            supplier.HouseNum     = txtHouseNum.Text;
            try
            {
                string city = ddlCity.SelectedItem.Text;
                //var address = (city != "" ? city + " " : "") +" "+ (txtStreet.Text != "" ? txtStreet.Text+" " : "") + (txtHouseNum.Text != "" ? txtHouseNum.Text : "");
                var locationService = new GoogleLocationService();
                var point           = (city.Trim() != "" ? locationService.GetLatLongFromAddress(city) : new MapPoint());
                supplier.AddressLocation = new Geometry.Point(point.Latitude, point.Longitude);
            }
            catch (Exception) {
                supplier.AddressLocation = new Geometry.Point(0, 0);
            }
            supplier.HouseNum = txtHouseNum.Text;

            supplier.Precent     = txtPrecent.Text != "" ?Convert.ToInt32(txtPrecent.Text):0;
            supplier.SumPerMonth = txtSumPerMonth.Text != "" ? Convert.ToInt32(txtSumPerMonth.Text) : 0;
            //supplier.StatusJoinBid = chkIsStatusJoinBid.Checked;
            //supplier.AllowChangeStatusJoinBid = chkAllowChangeStatusJoinBid.Checked;
            //supplier.MaxWinningsNum =txtMaxWinningsNum.Text != "" ? Convert.ToInt32(txtMaxWinningsNum.Text) : 0;
            supplier.MastercardCode = txtMastercardCode.Text;
            supplier.Save();

            if (IsNewMode)
            {
                SupplierId = supplier.SupplierId;
                //if (chkIsStatusJoinBid.Checked == false)//handel
                //{
                //    (new Query(SupplierProduct.TableSchema).Where(SupplierProduct.Columns.SupplierId, SupplierId).Delete()).Execute();
                //    ProductCollection pcol = ProductCollection.FetchByQuery(new Query(Product.TableSchema).Where(Product.Columns.IsDeleted, false));
                //    foreach (Product item in pcol)
                //    {
                //        SupplierProduct sp = new SupplierProduct();
                //        sp.SupplierId = SupplierId;
                //        sp.ProductId = item.ProductId;
                //        sp.Gift = "";
                //        sp.Save();
                //    }
                //}
            }
            //if (chkIsService.Checked)
            if (ddlIsProduct.SelectedValue != "prod")
            {
                foreach (ListItem item in ddlServices.Items)
                {
                    if (item.Selected)
                    {
                        SupplierService supplierService = SupplierService.FetchByID(Convert.ToInt64(item.Value), SupplierId);
                        if (supplierService == null)
                        {
                            supplierService            = new SupplierService();
                            supplierService.SupplierId = SupplierId;
                            supplierService.ServiceId  = Convert.ToInt64(item.Value);
                            supplierService.Save();
                        }
                    }
                    else
                    {
                        SupplierService.Delete(Convert.ToInt64(item.Value), SupplierId);
                    }
                }
            }
            else
            {
                SupplierController.DeleteAllSupplierServices(SupplierId);
            }

            if (supplier.Email != txtEmail.Text.Trim().NormalizeEmail())
            {
                if (AppSupplier.FetchByEmail(txtEmail.Text.Trim().NormalizeEmail()) != null)
                {
                    Master.MessageCenter.DisplayWarningMessage(AppUsersStrings.GetText(@"MessageEmailChangeFailed"));
                }
                else
                {
                    supplier.Email          = txtEmail.Text.Trim().NormalizeEmail();
                    supplier.UniqueIdString = supplier.Email;//email.NormalizeEmail();
                    SupplierEmail           = supplier.Email;
                    EmailChanged            = true;
                }
            }

            SupplierEmail = supplier.Email;
            supplier.Save();

            if (txtPassword.Text.Length > 0)
            {
                if (txtConfirmPassword.Text != txtPassword.Text)
                {
                    Master.MessageCenter.DisplayErrorMessage(SuppliersStrings.GetText(@"SupplierNewPasswordConfirmInvalid"));
                    return;
                }
                Membership.UserPasswordChangeResults results;
                results = Membership.ChangeSupplierPassword(supplier.Email, txtPassword.Text);
                switch (results)
                {
                default:
                    Master.MessageCenter.DisplayWarningMessage(SuppliersStrings.GetText(@"MessagePasswordChangeFailedUnknown"));
                    break;

                case Membership.UserPasswordChangeResults.PasswordDoNotMatch:
                    Master.MessageCenter.DisplayWarningMessage(SuppliersStrings.GetText(@"MessagePasswordChangeBadOldPassword"));
                    break;

                case Membership.UserPasswordChangeResults.Success:
                    break;
                }
            }

            if (IsNewMode)
            {
                string successMessage = SuppliersStrings.GetText(@"MessageSupplierCreated");
                string url            = @"EditSupplier.aspx?Email=" + SupplierEmail + "&SupplierId=" + supplier.SupplierId;
                url += @"&message-success=" + Server.UrlEncode(successMessage);
                Response.Redirect(url, true);
            }
            else
            {
                string successMessage = SuppliersStrings.GetText(@"MessageSupplierSaved");
                if (EmailChanged)
                {
                    string url = @"EditSupplier.aspx?message-success=" + Server.UrlEncode(successMessage) + "&SupplierId=" + supplier.SupplierId;
                    if (SupplierId != supplier.SupplierId)
                    {
                        url += @"&Email=" + SupplierEmail;
                    }
                    Response.Redirect(url, true);
                }
                else
                {
                    Master.MessageCenter.DisplaySuccessMessage(successMessage);
                    LoadView();
                }
            }
        }