/// <summary>
        /// This method uploads an avatar image
        /// </summary>
        /// <param name="server">HttpServerUtilityBase object. Used to map the server path</param>
        /// <param name="avatar">Contains the file bytes to save</param>
        /// <param name="id">The id to use for the image</param>
        /// <param name="avatarType">Currently canbe either Team or Player</param>
        /// <returns></returns>
        public static AvatarImage UploadFile2(HttpServerUtilityBase server, HttpPostedFileBase avatar, int id, AvatarTypeEnum avatarType)
        {
            try
            {
                String fileType = Path.GetExtension(avatar.FileName);
                String name = id + fileType;
                AvatarImage image = null;

                switch (avatar.ContentType)
                {
                    //Only accepct these content types
                    case "image/gif":
                    case "image/png":
                    case "image/jpg":
                    case "image/jpeg":
                        break;

                    //invalid file type
                    default:
                        throw new InvalidFileTypeException("Invalid File Type");
                }

                switch (avatarType)
                {
                    case AvatarTypeEnum.Team:
                        image = new TeamAvatarImage(server, name);
                        break;

                    case AvatarTypeEnum.Player:
                        image = new PlayerAvatarImage(server, name);
                        break;
                }

                try
                {
                    String absPath = image.AbsolutePath;
                    avatar.SaveAs(absPath);
                }
                catch (Exception e)
                {
                    //log
                    throw new FileUploadException("There was an error uploading the file");
                }

                return image;
            }
            catch (Exception e)
            {
                throw new GeneralException();
            }
        }
        public ActionResult Delete(int id)
        {
            var team = _teamDb.Find(id);
            if (team == null) return HttpNotFound();

            TeamAvatarImage image = new TeamAvatarImage(Server, team.AvatarName);

            TeamViewModel viewmodel = new TeamViewModel { ID = id, Name = team.Name };
            viewmodel.Avatar = image.Exists ? image.RelativePath : null;

            return View(viewmodel);
        }
        public ActionResult Details(int id)
        {
            var team = _teamDb.Find(id);
            if (team == null) return HttpNotFound();

            TeamAvatarImage image = new TeamAvatarImage(Server, team.AvatarName);//find out the correct path for the avatar (if there is one)
            TeamViewModel viewmodel = new TeamViewModel { ID = id, Name = team.Name };
            viewmodel.Avatar = image.Exists ? image.RelativePath : null;

            //build the list of players for this team to display
            viewmodel.PlayerList = _playerDb.FindAll().Where(x => x.TeamId == team.ID).Select(x => new PlayerViewModel { Name = x.Name, ID = x.ID }).ToList();

            return View(viewmodel);
        }
        public ActionResult Edit(TeamViewModel viewmodel, HttpPostedFileBase avatar)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Team team = _teamDb.Find(viewmodel.ID);
                    if (team == null) return HttpNotFound();

                    team.Name = viewmodel.Name;

                    if (!_teamDb.Update(team))
                    {
                        //log error...
                        ModelState.AddModelError("", "An error occurred updating the use information");
                        return View(viewmodel);
                    }

                    //if an image has been provided then upload it
                    if (avatar != null && avatar.ContentLength > 0)
                    {
                        try
                        {
                            //delete the current avatar image
                            TeamAvatarImage currentImage = new TeamAvatarImage(Server, team.AvatarName);
                            System.IO.File.Delete(currentImage.AbsolutePath);
                        }
                        catch (CustomExceptionBase e)
                        {//safe error message for user
                            ModelState.AddModelError("", e.Message);
                            return View(viewmodel);
                        }
                        catch (Exception e)
                        {
                            //log error
                            ModelState.AddModelError("", "An error has occurred on the server.");
                            return View(viewmodel);
                        }

                        AvatarImage image = AvatarUploader.UploadFile2(Server, avatar, team.ID, AvatarTypeEnum.Team);

                        team.AvatarName = image.Name;
                        _teamDb.Update(team);

                    }

                    return RedirectToAction("Details", new { id = team.ID });
                }
                catch (CustomExceptionBase e)
                {
                    ModelState.AddModelError("", e.Message);
                }
                catch (Exception e)
                {
                    //log exception
                    //return error message to user
                    ModelState.AddModelError("", "Exception occured on the server");
                }

            }

            return View(viewmodel);
        }
        public ActionResult Delete(TeamViewModel viewmodel)
        {
            try
            {
                var team = _teamDb.Find(viewmodel.ID);
                if (team == null) return HttpNotFound();

                //if there is a current image then delete it
                if (!String.IsNullOrEmpty(team.AvatarName))
                {
                    TeamAvatarImage image = new TeamAvatarImage(Server, team.AvatarName);
                    System.IO.File.Delete(image.AbsolutePath);
                }
            }
            catch (Exception e)
            {
                //log error...
                ModelState.AddModelError("", GenericErrorMessage);
                return View(viewmodel);
            }

            try
            {
                var playersOnTeam = _playerDb.FindAll().Where(x => x.TeamId == viewmodel.ID).ToList();

                playersOnTeam.ForEach(x =>
                    {
                        try
                        {
                            //delete this player from the database
                            _playerDb.Delete(x.ID);

                            //delete the players avatar
                            PlayerAvatarImage image = new PlayerAvatarImage(Server, x.AvatarName);
                            if(!String.IsNullOrEmpty(x.AvatarName))System.IO.File.Delete(image.AbsolutePath);
                        }
                        catch (Exception e)
                        {
                            //log error...
                        }
                    });//delete all players on the team first
                _teamDb.Delete(viewmodel.ID);
            }
            catch (Exception e)
            {
                //log error
                return Content("Error");//temp
            }

            return RedirectToAction("Index");
        }