//
        // GET: /Account/Register
        public ActionResult DisplayAdminRegister()
        {
            RegisterModel adminReg = new RegisterModel();
            if (DatabaseHelper.GetAdminData(adminReg, -1) == null)
            {
                TempData["RegistrationMessage"] = "Admin registration form.";
            }

            return View(adminReg);
        }
        //==================================================================================//
        //      Store Admin information                                                     //
        //                                                                                  //
        //      Register the admin information if it is empty. Otherwise edit it.           //
        //                                                                                  //
        //      Note: If the edit is true then the account is being edited.                 //
        //      Otherwise it is being registered.                                           //
        //==================================================================================//
        public static bool StoreAdminData(RegisterModel regAdmin, ref bool edit)
        {
            int UserId = WebSecurity.GetUserId(regAdmin.EmailAddress);

            edit = false;

            try
            {
                SiteAdmin CurrentAdmin;

                using (ITintheDTestTableEntities context = new ITintheDTestTableEntities())
                {
                    // Put everything we find in the database in the var variable. All the
                    // information will be gotten using the User ID.

                    var AdminData = from r in context.SiteAdmin
                                    where r.UserId == UserId
                                    select r;

                    // If the user has some information then edit it.
                    // Otherwise register the account.

                    if (AdminData.Count() > 0 && UserId > 0)
                    {
                        CurrentAdmin = AdminData.FirstOrDefault();
                        CurrentAdmin.Status = regAdmin.AccountStatus;
                        CurrentAdmin.Company = regAdmin.CompanyName;
                        CurrentAdmin.EmailAddress = regAdmin.EmailAddress;
                        CurrentAdmin.Name = regAdmin.Name;
                        CurrentAdmin.Telephone = regAdmin.Telephone;
                        CurrentAdmin.UserId = UserId;

                        // Store the avatar if it is supplied.

                        if (regAdmin.ImageFile != null)
                        {
                            UserImage image = new UserImage();

                            using (MemoryStream ms = new MemoryStream())
                            {
                                regAdmin.ImageFile.InputStream.CopyTo(ms);

                                image.FileContent = ms.ToArray();
                                image.FileName = Path.GetFileName(regAdmin.ImageFile.FileName);
                                image.ContentType = regAdmin.ImageFile.ContentType;
                                image.ContentLength = regAdmin.ImageFile.ContentLength;

                                DatabaseHelper.UploadImage(image, CurrentAdmin.UserId);

                                CurrentAdmin.ImageUploaded = "Yes";
                                regAdmin.ImageUploaded = "Yes";
                            }
                        }

                        edit = true;
                    }

                    else
                    {
                        CurrentAdmin = new SiteAdmin();

                        CurrentAdmin.Status = regAdmin.AccountStatus;
                        CurrentAdmin.Company = regAdmin.CompanyName;
                        CurrentAdmin.Name = regAdmin.Name;
                        CurrentAdmin.EmailAddress = regAdmin.EmailAddress;
                        CurrentAdmin.Telephone = regAdmin.Telephone;

                        context.AddToSiteAdmin(CurrentAdmin);
                    }

                    try
                    {
                        // If the account is edited then save changes. Otherwise register the account.

                        if (edit == false)
                        {
                            WebSecurity.CreateUserAndAccount(regAdmin.EmailAddress, regAdmin.Password);

                            DatabaseHelper.AddUserToRole(regAdmin.EmailAddress, "Admin");

                            CurrentAdmin.UserId = WebSecurity.GetUserId(regAdmin.EmailAddress);

                            // Store the avatar if it is supplied.

                            if (regAdmin.ImageFile != null)
                            {
                                UserImage image = new UserImage();

                                using (MemoryStream ms = new MemoryStream())
                                {
                                    regAdmin.ImageFile.InputStream.CopyTo(ms);

                                    image.FileContent = ms.ToArray();
                                    image.FileName = Path.GetFileName(regAdmin.ImageFile.FileName);
                                    image.ContentType = regAdmin.ImageFile.ContentType;
                                    image.ContentLength = regAdmin.ImageFile.ContentLength;

                                    DatabaseHelper.UploadImage(image, CurrentAdmin.UserId);

                                    CurrentAdmin.ImageUploaded = "Yes";
                                    regAdmin.ImageUploaded = "Yes";
                                }
                            }

                            else
                            {
                                CurrentAdmin.ImageUploaded = "No";
                                regAdmin.ImageUploaded = "No";
                            }
                        }

                        context.SaveChanges();

                        return true;
                    }

                    catch (Exception ex)
                    {
                        string errorMessage = ex.Message;

                        return false;
                    }
                }
            }

            catch (Exception ex)
            {
                string exMessage = ex.Message;

                return false;
            }
        }
        public ActionResult StoreAdmin(RegisterModel adminReg)
        {
            if (ModelState.IsValid)
            {
                bool edit = false;

                if (adminReg.AccountStatus < 1)
                {
                    adminReg.AccountStatus = 1;
                }

                else if (adminReg.AccountStatus > 3)
                {
                    adminReg.AccountStatus = 3;
                }

                if (DatabaseHelper.StoreAdminData(adminReg, ref edit))
                {
                    int ID = WebSecurity.GetUserId(adminReg.EmailAddress);

                    if (edit == true && ID != WebSecurity.CurrentUserId)
                    {
                        TempData["Message"] = "Successfully edited the user's information.";

                        return RedirectToAction("User", "Admin", new { ID });
                    }

                    if (edit == true)
                    {
                        TempData["Message"] = "Successfully edited your information.";
                        return RedirectToAction("Manage", "Account");
                    }

                    else
                    {
                        TempData["Message"] = "Successfully registered an Admin";
                        return RedirectToAction("DisplayAdminRegister", "Admin");
                    }
                }

                else
                {
                    TempData["Message"] = "Registeration failed.";
                    return RedirectToAction("DisplayAdminRegister", "Admin");
                }
            }

            // If we got this far, something failed, redisplay form
            TempData["Message"] = "Registeration failed.";
            return RedirectToAction("DisplayAdminRegister", "Admin");
        }
        //==================================================================================//
        //      Get Admin information                                                       //
        //                                                                                  //
        //      Gets the admin information if it is not empty.                              //
        //                                                                                  //
        //      Note: If the User ID is -1 then it is being checked out by the user.        //
        //      If not then it is being checked by the admin.                               //
        //==================================================================================//
        public static RegisterModel GetAdminData(RegisterModel regAdmin, int UserId)
        {
            // If the User ID is -1 then it is being checked out by the user. We will then
            // get the current user ID.

            if (UserId == -1)
            {
                UserId = WebSecurity.CurrentUserId;
            }

            try
            {
                using (ITintheDTestTableEntities context = new ITintheDTestTableEntities())
                {
                    // Put everything we find in the database in the var variable. All the
                    // information will be gotten using the User ID.

                    var currentAdmin = from r in context.SiteAdmin
                                       where r.UserId == UserId
                                       select r;

                    // If the user has some information then edit it.
                    // Otherwise return nothing.

                    if (currentAdmin.Count() > 0)
                    {
                        regAdmin.Name = currentAdmin.FirstOrDefault().Name;
                        regAdmin.AccountStatus = currentAdmin.FirstOrDefault().Status;
                        regAdmin.EmailAddress = currentAdmin.FirstOrDefault().EmailAddress;
                        regAdmin.CompanyName = currentAdmin.FirstOrDefault().Company;
                        regAdmin.Telephone = currentAdmin.FirstOrDefault().Telephone;
                        regAdmin.EmailAddress = currentAdmin.FirstOrDefault().EmailAddress;
                        regAdmin.ImageUploaded = currentAdmin.FirstOrDefault().ImageUploaded;

                        // Return the modal that is filled with information from the database.

                        return (regAdmin);
                    }

                    else
                    {
                        return (null);
                    }
                }
            }

            catch
            {
                return (null);
            }
        }