public ActionResult ConfirmChangesScreen(TeamWizardVM updatedTeam)
        {
            WizardMessages wizardMessages = new WizardMessages();

            if (updatedTeam.Team.TeamId > 0)
            {
                Team originalTeam = new Team();
                originalTeam = teamRepository.GetTeam(updatedTeam.Team.TeamId);
                teamRepository.EditGroupForDisplay(originalTeam);

                teamWizardRepository.BuildTeamChangeMessages(wizardMessages, originalTeam, updatedTeam);
            }
            else
            {
                teamWizardRepository.BuildTeamChangeMessages(wizardMessages, null, updatedTeam);
            }

            return(Json(new WizardJSONResponse
            {
                html = ControllerExtension.RenderPartialViewToString(this, "ConfirmChangesScreen", wizardMessages),
                message = "Success",
                success = true
            }));
        }
        //Update Team ClientSubUnits
        public WizardMessages UpdateTeamClientSubUnits(TeamWizardVM teamChanges, WizardMessages wizardMessages)
        {
            Team team = new Team();

            team = teamChanges.Team;
            bool changesExist = false;

            // Create the xml document container
            XmlDocument    doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);

            doc.AppendChild(dec);// Create the root element
            XmlElement root = doc.CreateElement("TeamClientSubUnits");

            doc.AppendChild(root);

            ClientSubUnitTeamRepository clientSubUnitTeamRepository = new ClientSubUnitTeamRepository();
            ClientSubUnitRepository     clientSubUnitRepository     = new ClientSubUnitRepository();

            if (teamChanges.ClientSubUnitsAdded != null)
            {
                if (teamChanges.ClientSubUnitsAdded.Count > 0)
                {
                    changesExist = true;
                    XmlElement xmlClientSubUnitsAdded = doc.CreateElement("ClientSubUnitsAdded");


                    foreach (ClientSubUnitTeam item in teamChanges.ClientSubUnitsAdded)
                    {
                        ClientSubUnit clientSubUnit = new ClientSubUnit();
                        clientSubUnit = clientSubUnitRepository.GetClientSubUnit(item.ClientSubUnitGuid);

                        XmlElement xmlClientSubUnit = doc.CreateElement("ClientSubUnit");
                        xmlClientSubUnitsAdded.AppendChild(xmlClientSubUnit);

                        XmlElement xmlClientSubUnitName = doc.CreateElement("ClientSubUnitName");
                        xmlClientSubUnitName.InnerText = clientSubUnit.ClientSubUnitName;
                        xmlClientSubUnit.AppendChild(xmlClientSubUnitName);

                        XmlElement xmlClientSubUnitGuid = doc.CreateElement("ClientSubUnitGuid");
                        xmlClientSubUnitGuid.InnerText = item.ClientSubUnitGuid;
                        xmlClientSubUnit.AppendChild(xmlClientSubUnitGuid);

                        XmlElement xmlIncludeInClientDroplistFlag = doc.CreateElement("IncludeInClientDroplistFlag");
                        xmlIncludeInClientDroplistFlag.InnerText = item.IncludeInClientDroplistFlag == true ? "1" : "0";
                        xmlClientSubUnit.AppendChild(xmlIncludeInClientDroplistFlag);
                    }
                    root.AppendChild(xmlClientSubUnitsAdded);
                }
            }
            if (teamChanges.ClientSubUnitsRemoved != null)
            {
                if (teamChanges.ClientSubUnitsRemoved.Count > 0)
                {
                    changesExist = true;
                    XmlElement xmlClientSubUnitsRemoved = doc.CreateElement("ClientSubUnitsRemoved");

                    foreach (ClientSubUnitTeam item in teamChanges.ClientSubUnitsRemoved)
                    {
                        ClientSubUnit clientSubUnit = new ClientSubUnit();
                        clientSubUnit = clientSubUnitRepository.GetClientSubUnit(item.ClientSubUnitGuid);

                        XmlElement xmlClientSubUnit = doc.CreateElement("ClientSubUnit");
                        xmlClientSubUnitsRemoved.AppendChild(xmlClientSubUnit);

                        XmlElement xmlClientSubUnitName = doc.CreateElement("ClientSubUnitName");
                        xmlClientSubUnitName.InnerText = clientSubUnit.ClientSubUnitName;
                        xmlClientSubUnit.AppendChild(xmlClientSubUnitName);

                        XmlElement xmlClientSubUnitGuid = doc.CreateElement("ClientSubUnitGuid");
                        xmlClientSubUnitGuid.InnerText = item.ClientSubUnitGuid;
                        xmlClientSubUnit.AppendChild(xmlClientSubUnitGuid);
                    }
                    root.AppendChild(xmlClientSubUnitsRemoved);
                }
            }

            if (teamChanges.ClientSubUnitsAltered != null)
            {
                if (teamChanges.ClientSubUnitsAltered.Count > 0)
                {
                    changesExist = true;
                    XmlElement xmlClientSubUnitsAltered = doc.CreateElement("ClientSubUnitsAltered");

                    foreach (ClientSubUnitTeam item in teamChanges.ClientSubUnitsAltered)
                    {
                        ClientSubUnit clientSubUnit = new ClientSubUnit();
                        clientSubUnit = clientSubUnitRepository.GetClientSubUnit(item.ClientSubUnitGuid);

                        XmlElement xmlClientSubUnit = doc.CreateElement("ClientSubUnit");
                        xmlClientSubUnitsAltered.AppendChild(xmlClientSubUnit);

                        XmlElement xmlClientSubUnitName = doc.CreateElement("ClientSubUnitName");
                        xmlClientSubUnitName.InnerText = clientSubUnit.ClientSubUnitName;
                        xmlClientSubUnit.AppendChild(xmlClientSubUnitName);

                        XmlElement xmlClientSubUnitGuid = doc.CreateElement("ClientSubUnitGuid");
                        xmlClientSubUnitGuid.InnerText = item.ClientSubUnitGuid;
                        xmlClientSubUnit.AppendChild(xmlClientSubUnitGuid);

                        XmlElement xmlIncludeInClientDroplistFlag = doc.CreateElement("IncludeInClientDroplistFlag");
                        xmlIncludeInClientDroplistFlag.InnerText = item.IncludeInClientDroplistFlag == true ? "1" : "0";
                        xmlClientSubUnit.AppendChild(xmlIncludeInClientDroplistFlag);
                    }
                    root.AppendChild(xmlClientSubUnitsAltered);
                }
            }
            if (changesExist)
            {
                string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];

                var output = (from n in db.spDDAWizard_UpdateTeamClientSubUnits_v1(
                                  team.TeamId,
                                  System.Xml.Linq.XElement.Parse(doc.OuterXml),
                                  adminUserGuid)
                              select n).ToList();

                foreach (spDDAWizard_UpdateTeamClientSubUnits_v1Result message in output)
                {
                    wizardMessages.AddMessage(message.MessageText.ToString(), (bool)message.Success);
                }
            }
            return(wizardMessages);
        }
        //Update Team SystemUsers
        public WizardMessages UpdateTeamSystemUsers(TeamWizardVM teamChanges, WizardMessages wizardMessages)
        {
            string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];

            Team team = new Team();

            team = teamChanges.Team;
            bool changesExist = false;

            // Create the xml document container
            XmlDocument    doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);

            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("TeamSystemUsers");

            doc.AppendChild(root);

            SystemUserRepository systemUserRepository = new SystemUserRepository();

            if (teamChanges.SystemUsersAdded != null)
            {
                if (teamChanges.SystemUsersAdded.Count > 0)
                {
                    changesExist = true;
                    XmlElement xmlSystemUsersAdded = doc.CreateElement("SystemUsersAdded");

                    foreach (SystemUser item in teamChanges.SystemUsersAdded)
                    {
                        SystemUser systemUser = new SystemUser();
                        systemUser = systemUserRepository.GetUserBySystemUserGuid(item.SystemUserGuid);
                        string systemUserName = systemUser.FirstName + (item.MiddleName != "" ? item.MiddleName + " " : "") + systemUser.LastName;

                        XmlElement xmlSystemUser = doc.CreateElement("SystemUser");
                        xmlSystemUsersAdded.AppendChild(xmlSystemUser);

                        XmlElement xmlSystemUserName = doc.CreateElement("SystemUserName");
                        xmlSystemUserName.InnerText = systemUserName;
                        xmlSystemUser.AppendChild(xmlSystemUserName);

                        XmlElement xmlSystemUserGuid = doc.CreateElement("SystemUserGuid");
                        xmlSystemUserGuid.InnerText = item.SystemUserGuid;
                        xmlSystemUser.AppendChild(xmlSystemUserGuid);
                    }
                    root.AppendChild(xmlSystemUsersAdded);
                }
            }
            if (teamChanges.SystemUsersRemoved != null)
            {
                if (teamChanges.SystemUsersRemoved.Count > 0)
                {
                    changesExist = true;
                    // xml = xml + "<SystemUsersRemoved>";
                    XmlElement xmlSystemUsersRemoved = doc.CreateElement("SystemUsersRemoved");
                    foreach (SystemUser item in teamChanges.SystemUsersRemoved)
                    {
                        SystemUser systemUser = new SystemUser();
                        systemUser = systemUserRepository.GetUserBySystemUserGuid(item.SystemUserGuid);
                        string systemUserName = systemUser.FirstName + (item.MiddleName != "" ? item.MiddleName + " " : "") + systemUser.LastName;

                        XmlElement xmlSystemUser = doc.CreateElement("SystemUser");
                        xmlSystemUsersRemoved.AppendChild(xmlSystemUser);

                        XmlElement xmlSystemUserName = doc.CreateElement("SystemUserName");
                        xmlSystemUserName.InnerText = systemUserName;
                        xmlSystemUser.AppendChild(xmlSystemUserName);

                        XmlElement xmlSystemUserGuid = doc.CreateElement("SystemUserGuid");
                        xmlSystemUserGuid.InnerText = item.SystemUserGuid;
                        xmlSystemUser.AppendChild(xmlSystemUserGuid);
                    }
                    root.AppendChild(xmlSystemUsersRemoved);
                }
            }

            if (changesExist)
            {
                var output = (from n in db.spDDAWizard_UpdateTeamSystemUsers_v1(
                                  team.TeamId,
                                  System.Xml.Linq.XElement.Parse(doc.OuterXml),
                                  adminUserGuid)
                              select n).ToList();

                foreach (spDDAWizard_UpdateTeamSystemUsers_v1Result message in output)
                {
                    wizardMessages.AddMessage(message.MessageText.ToString(), (bool)message.Success);
                }
            }
            return(wizardMessages);
        }
        //Compare two Teams and return a list of messages about changes
        public WizardMessages BuildTeamChangeMessages(WizardMessages wizardMessages, Team originalTeam, TeamWizardVM teamChanges)
        {
            TeamRepository          teamRepository          = new TeamRepository();
            ClientSubUnitRepository clientSubUnitRepository = new ClientSubUnitRepository();
            SystemUserRepository    systemUserRepository    = new SystemUserRepository();

            Team updatedTeam = new Team();

            updatedTeam = teamChanges.Team;
            //teamRepository.EditGroupForDisplay(updatedTeam); removed- gets info from original Team

            if (originalTeam == null)
            {
                wizardMessages.AddMessage("A new Team \"" + updatedTeam.TeamName + "\"has been added.", true);
            }
            else
            {
                if (originalTeam.TeamName != updatedTeam.TeamName)
                {
                    wizardMessages.AddMessage("Team Name will be updated to \"" + updatedTeam.TeamName + "\".", true);
                }

                if (originalTeam.TeamEmail != updatedTeam.TeamEmail)
                {
                    wizardMessages.AddMessage("Team Email will be updated to \"" + updatedTeam.TeamEmail + "\".", true);
                }

                if (originalTeam.TeamPhoneNumber != updatedTeam.TeamPhoneNumber)
                {
                    wizardMessages.AddMessage("Team phone Number will be updated to \"" + updatedTeam.TeamPhoneNumber + "\".", true);
                }
                if (originalTeam.CityCode != updatedTeam.CityCode)
                {
                    wizardMessages.AddMessage("Team City Code will be updated to \"" + updatedTeam.CityCode + "\".", true);
                }
                if (originalTeam.TeamQueue != updatedTeam.TeamQueue)
                {
                    wizardMessages.AddMessage("Team Queue will be updated to \"" + updatedTeam.TeamQueue + "\".", true);
                }


                if (originalTeam.TeamTypeCode != updatedTeam.TeamTypeCode)
                {
                    TeamType           teamType           = new TeamType();
                    TeamTypeRepository teamTypeRepository = new TeamTypeRepository();
                    teamType = teamTypeRepository.GetTeamType(updatedTeam.TeamTypeCode);
                    wizardMessages.AddMessage("Team Type will be updated to \"" + teamType.TeamTypeDescription + "\".", true);
                }

                if (originalTeam.HierarchyType != updatedTeam.HierarchyType)
                {
                    wizardMessages.AddMessage("Hierarchy will be updated to \"" + updatedTeam.HierarchyType + "\".", true);
                }

                if (originalTeam.HierarchyItem != updatedTeam.HierarchyItem)
                {
                    wizardMessages.AddMessage(updatedTeam.HierarchyType + " value will be updated to \"" + updatedTeam.HierarchyItem + "\".", true);
                }
                if (originalTeam.TravelerTypeGuid != updatedTeam.TravelerTypeGuid)
                {
                    wizardMessages.AddMessage("TravelerType will be updated to \"" + updatedTeam.TravelerTypeName + "\".", true);
                }
            }

            if (teamChanges.SystemUsersAdded != null)
            {
                if (teamChanges.SystemUsersAdded.Count > 0)
                {
                    foreach (SystemUser item in teamChanges.SystemUsersAdded)
                    {
                        SystemUser systemUser = new SystemUser();
                        systemUser = systemUserRepository.GetUserBySystemUserGuid(item.SystemUserGuid);
                        if (systemUser != null)
                        {
                            wizardMessages.AddMessage("You will add User \"" + systemUser.LastName + "," + (systemUser.MiddleName != "" ? systemUser.MiddleName + " " : "") + systemUser.FirstName + "\".", true);
                        }
                    }
                }
            }
            if (teamChanges.SystemUsersRemoved != null)
            {
                if (teamChanges.SystemUsersRemoved.Count > 0)
                {
                    foreach (SystemUser item in teamChanges.SystemUsersRemoved)
                    {
                        SystemUser systemUser = new SystemUser();
                        systemUser = systemUserRepository.GetUserBySystemUserGuid(item.SystemUserGuid);
                        if (systemUser != null)
                        {
                            wizardMessages.AddMessage("You will remove User \"" + systemUser.LastName + "," + (systemUser.MiddleName != "" ? systemUser.MiddleName + " " : "") + systemUser.FirstName + "\".", true);
                        }
                    }
                }
            }
            if (teamChanges.ClientSubUnitsAdded != null)
            {
                if (teamChanges.ClientSubUnitsAdded.Count > 0)
                {
                    foreach (ClientSubUnitTeam item in teamChanges.ClientSubUnitsAdded)
                    {
                        ClientSubUnit clientSubUnit = new ClientSubUnit();
                        clientSubUnit = clientSubUnitRepository.GetClientSubUnit(item.ClientSubUnitGuid);
                        if (clientSubUnit != null)
                        {
                            wizardMessages.AddMessage("You will add ClientSubUnit \"" + clientSubUnit.ClientSubUnitName + "\".", true);
                        }
                    }
                }
            }
            if (teamChanges.ClientSubUnitsRemoved != null)
            {
                if (teamChanges.ClientSubUnitsRemoved.Count > 0)
                {
                    foreach (ClientSubUnitTeam item in teamChanges.ClientSubUnitsRemoved)
                    {
                        ClientSubUnit clientSubUnit = new ClientSubUnit();
                        clientSubUnit = clientSubUnitRepository.GetClientSubUnit(item.ClientSubUnitGuid);
                        if (clientSubUnit != null)
                        {
                            wizardMessages.AddMessage("You will remove ClientSubUnit \"" + clientSubUnit.ClientSubUnitName + "\".", true);
                        }
                    }
                }
            }

            if (teamChanges.ClientSubUnitsAltered != null)
            {
                if (teamChanges.ClientSubUnitsAltered.Count > 0)
                {
                    foreach (ClientSubUnitTeam item in teamChanges.ClientSubUnitsAltered)
                    {
                        ClientSubUnit clientSubUnit = new ClientSubUnit();
                        clientSubUnit = clientSubUnitRepository.GetClientSubUnit(item.ClientSubUnitGuid);
                        if (clientSubUnit != null)
                        {
                            wizardMessages.AddMessage("You will alter ClientSubUnit \"" + clientSubUnit.ClientSubUnitName + "\".", true);
                        }
                    }
                }
            }
            return(wizardMessages);
        }
        public ActionResult CommitChanges(TeamWizardVM teamChanges)
        {
            Team team = new Team();

            team = teamChanges.Team;

            WizardMessages wizardMessages = new WizardMessages();

            try{
                UpdateModel(team);
            }
            catch
            {
                //Validation Error
                string msg = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        msg += error.ErrorMessage;
                    }
                }
                return(Json(new WizardJSONResponse
                {
                    html = ControllerExtension.RenderPartialViewToString(this, "Error", msg),
                    message = msg,
                    success = false
                }));
            }
            //Editing A Team
            if (team.TeamId > 0)
            {
                try
                {
                    teamWizardRepository.UpdateTeam(team);
                    wizardMessages.AddMessage("Team Details successfully updated", true);
                }
                catch (SqlException ex)
                {
                    //If there is error we will continue, but store error to return to user

                    //Versioning Error
                    if (ex.Message == "SQLVersioningError")
                    {
                        wizardMessages.AddMessage("Team Detail was not updated. Another user has already changed this Team.", false);
                    }
                    else //Other Error
                    {
                        LogRepository logRepository = new LogRepository();
                        logRepository.LogError(ex.Message);

                        wizardMessages.AddMessage("Team Details were not updated, please check Event Log for details", false);
                        wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
                    }
                }
            }
            else //Adding A Team
            {
                try
                {
                    int teamId = teamWizardRepository.AddTeam(team);
                    team             = teamRepository.GetTeam(teamId);
                    teamChanges.Team = team;
                    wizardMessages.AddMessage("Team added successfully", true);
                }
                catch (SqlException ex)
                {
                    LogRepository logRepository = new LogRepository();
                    logRepository.LogError(ex.Message);

                    wizardMessages.AddMessage("Team was not added, please check Event Log for details", false);
                    wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);

                    //If we cannot add a Team , we cannot continue, so return error to User
                    return(Json(new
                    {
                        html = ControllerExtension.RenderPartialViewToString(this, "FinishedScreen", wizardMessages),
                        message = "DBError",
                        success = false
                    }));
                }
            }
            //If we have added a Team successfully, or edited a Team (successfully or unsuccessfully), we continue to add SystemUsers/ClientSubUnits
            try
            {
                wizardMessages = teamWizardRepository.UpdateTeamSystemUsers(teamChanges, wizardMessages);
            }
            catch (SqlException ex)
            {
                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                wizardMessages.AddMessage("Team SystemUser were not changed, please check Event Log for details", false);
                wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
            }
            try
            {
                wizardMessages = teamWizardRepository.UpdateTeamClientSubUnits(teamChanges, wizardMessages);
            }
            catch (SqlException ex)
            {
                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                wizardMessages.AddMessage("Team ClientSubUnits were not changed, please check Event Log for details", false);
                wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
            }
            return(Json(new
            {
                html = ControllerExtension.RenderPartialViewToString(this, "FinishedScreen", wizardMessages),
                message = "Success",
                success = true
            }));
        }