protected void btnAdd_Click(object sender, EventArgs e)
        {
            Class.Group group    = new Class.Group();
            string      labeling = Constant.currentDatabase;
            //string labeling = DataAccessBase.GetLabeling();

            int numberOfImages = 0;

            group.IsPatientChosen = Convert.ToBoolean(HiddenFieldIsPatientChosen.Value);

            // - Martin - split at '|' (no regular directory symbol)
            string[] path      = HiddenFieldPathSel.Value.Split('|');
            string   reference = HiddenFieldReferenceName.Value;

            group.ImagesPerPatient = Int32.Parse(hddNumberImagePatient.Value);

            if (reference != "Choose a Reference")
            {
                group.GroupHasReference = true;
            }
            bool isNotLocal = false;

            // definition of variables used later
            if ((reference.ToUpper() == "GLOBAL") || (reference.ToUpper() == "CHOOSE A REFERENCE"))
            {
                isNotLocal = true;
            }

            if ((reference.ToUpper() == "GLOBAL"))
            {
                group.ReferenceIsGlobal = true;
            }

            for (int i = 0; i < path.Length - 1; i++)
            {
                numberOfImages++;
                Class.Directory dir = new Class.Directory();
                dir.Path = path[i];

                if (!isNotLocal)
                {
                    if (path[i].ToUpper().Contains(reference.ToUpper()))
                    {
                        dir.IsReference     = true;
                        group.ReferenceName = path[i].Split('/')[1];
                    }
                    else
                    {
                        dir.IsReference = false;
                    }

                    group.LoopOver += path[i].Replace("/", "_") + System.Environment.NewLine;
                }
                else
                {
                    group.LoopOver += path[i].Replace("/", "_") + System.Environment.NewLine;

                    if (group.ReferenceIsGlobal)
                    {
                        group.ReferenceName = "GLOBAL";
                    }
                    else
                    {
                        group.ReferenceName = "";
                    }
                }


                group.SubDirectory.Add(dir);
            }
            group.PageStyle = Int64.Parse(hddImagesPerPage.Value);
            int images = 0;     // max amount of images to be shown on a a single page

            switch (group.PageStyle)
            {
            case 0: { group.ImagesPerPage = -1; images = group.ImagesPerPatient; break; }

            // case 1: { group.ImagesPerPage = 1; break; }
            case 1: { try { group.ImagesPerPage = Int32.Parse(txtmImagesPerPage.Text); group.ImagesPerPage = (group.ImagesPerPage < 0) ? -1 : group.ImagesPerPage; } catch (Exception) { group.ImagesPerPage = -1; }; break; }

            default: { group.ImagesPerPage = -1; break; }
            }


            // check if value is within codomain [1, number of selected images]
            if (group.ImagesPerPatient == 0)
            {
                group.ImagesPerPatient = 1;
            }

            if (0 < group.ImagesPerPage)
            {
                images = group.ImagesPerPage;
            }

            if (group.ImagesPerPatient < group.ImagesPerPage)
            {
                images = group.ImagesPerPatient;
            }


            string patient          = "";
            int    numberOfPatients = 0;

            for (int i = 0; i < path.Length; i++)
            {
                if ((path[i] != "") && (path[i].Substring(0, path[i].IndexOf("/")) != patient))
                {
                    patient = path[i].Substring(0, path[i].IndexOf("/"));
                    numberOfPatients++;
                }
            }

            if (group.GroupHasReference && !group.ReferenceIsGlobal)
            {
                group.ImagesPerPatient--;
            }

            group.PageStyleDescription = getText(group.PageStyle, images);
            group.OverallNumber        = numberOfImages;
            double numberOfPages = ((double)group.ImagesPerPatient / images);

            group.NumberOfPages = (int)Math.Ceiling(numberOfPages) * numberOfPatients;



            listAddedGroups.Add(group);
            listGroup.Add(group);


            // to avoid adding the same group again after a postback
            if (listGroup.Count > 1)
            {
                if ((listGroup[listGroup.Count - 1].GroupHasReference == listGroup[listGroup.Count - 2].GroupHasReference) && (listGroup[listGroup.Count - 1].LoopOver == listGroup[listGroup.Count - 2].LoopOver) && (listGroup[listGroup.Count - 1].ReferenceName == listGroup[listGroup.Count - 2].ReferenceName))
                {
                    listGroup.RemoveAt(listGroup.Count() - 1);
                }
            }



            if (listGroup[listGroup.Count() - 1].LoopOver == null)
            {
                listGroup.RemoveAt(listGroup.Count() - 1); // remove last entry in case of an added empty group
            }
            else
            {
                group.Name               = (listGroup.Count()).ToString();
                Session["Group"]         = listGroup;
                HiddenFieldPathSel.Value = "";
                gvGroupName.DataSource   = listGroup;
                gvGroupName.DataBind();
            }
            //for (int i = 0; i < ddlDatabase.Items.Count; i++)
            //{
            //    if (ddlDatabase.Items[i].Selected)
            //    {
            //        Constant.currentDatabase = ddlDatabase.Items[i].Text;
            //        break;
            //    }
            //}
            ScriptManager.RegisterStartupScript(this, GetType(), "fUpdateTable", "fUpdateTable();", true);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (HttpContext.Current.Session["User"] == null)
                {
                    Response.Redirect("~/Account/Login.aspx");
                }
                else
                {
                    listGroup         = new List <Class.Group>();
                    listAddedGroups   = new List <Group>();
                    listRemovedGroups = new List <long>();

                    // check for duplicate descriptions, add the version to the list and remove duplicates
                    for (int i = 0; i < tsc.Count(); i++)
                    {
                        tsc[i].scaleVersions = new List <string>();
                        tsc[i].scaleVersions.Add(tsc[i].verScaleCont + "");

                        for (int c = i + 1; c < tsc.Count(); c++)
                        {
                            if (tsc[i].DescriptionScaleContinuous == tsc[c].DescriptionScaleContinuous)
                            {
                                tsc[i].scaleVersions.Add(tsc[c].verScaleCont + "");
                                tsc.RemoveAt(c);
                                c--;
                            }
                        }

                        tsc[i].ddlContinuous.DataSource = tsc[i].scaleVersions;
                        tsc[i].ddlContinuous.DataBind();
                    }


                    PopulateTypeUser();            // insert in the drop down list all existing users
                    PopulateTypeScaleContinuous(); // insert in the drop down list all existing scale continous
                    PopulateTypeScaleDiscrete();
                    PopulateDatabases();

                    cbxDiscreteScale.Attributes.Add("style", "display:none");

                    string labeling = listDatabase[0].DatabasePath;
                    Constant.currentDatabase = labeling;

                    long?id_TestCase = null;
                    if (Session["IdTestCase"] != null)
                    {
                        id_TestCase = Convert.ToInt64(Session["IdTestCase"]);
                    }
                    if (!IsPostBack)
                    {
                        listGroup = new List <Class.Group>();
                        // if admin wants to change a test case from view test case
                        if (id_TestCase != null)// es gibt schon ein id
                        {
                            //listGroup = new List<Class.Group>();
                            long                 idTestCase = (long)id_TestCase;
                            Class.TestCase       tc         = DataAccess.DataAccessTestCase.getTestCase().Where(p => p.IDTestcase == idTestCase).FirstOrDefault();
                            List <Class.TypUser> listtu     = DataAccess.DataAccessUser.GetTypeUserForTestCase(idTestCase);
                            listGroup_ = DataAccess.DataAccessTestCase.getGroupFromIdTestCase(idTestCase);
                            // name test case and testquestion cant be changed
                            txtNameTestCase.Text      = tc.NameTestCase;
                            txtNameTestCase.Enabled   = false;
                            txtTestQuestion.Text      = tc.TestQuestion;
                            txtTestQuestion.Enabled   = false;
                            txtGeneralInfo.Text       = tc.GeneralInfo;
                            cbxActiveLearning.Checked = tc.ActiveLearning;

                            if (tc.ActiveLearning)
                            {
                                txtActiveUserThreshold.Text    = tc.userThreshold + "";
                                txtActiveInitialThreshold.Text = tc.initialThreshold + "";
                            }

                            hddTCSeed.Value = Convert.ToString(tc.iSeed);

                            // schaut welche skala ausgesucht ist
                            if (tc.DiskreteScale)
                            {
                                rblTypeScale.SelectedValue = "0";
                            }
                            else
                            {
                                rblTypeScale.SelectedValue = "1";
                            }

                            rblTypeScale.Enabled = false;

                            btnInsertTestCase.Visible = false;// no button insert, but instead later button change

                            // takes all the selected type of user
                            foreach (Class.TypUser tu in listtu)
                            {
                                ListItem app = rblTypeUserAssigned.Items.FindByValue(tu.ID_Typ.ToString());

                                int id = rblTypeUserAssigned.Items.IndexOf(app);

                                rblTypeUserAssigned.Items[id].Selected = true;
                            }

                            // insert all the already created images groups
                            foreach (Class.Group g in listGroup_)
                            {
                                List <Class.GroupImage> listGroupImage = DataAccess.DataAccessTestCase.GetGrouphasImageFromGroup(g.IdGroup, 0);
                                Class.Group             appgroup       = new Class.Group();
                                appgroup.Name              = g.Name;
                                appgroup.GroupExistDB      = true;
                                appgroup.IdGroup           = g.IdGroup;
                                appgroup.IdTestCase        = g.IdTestCase;
                                appgroup.ReferenceIsGlobal = g.ReferenceIsGlobal;
                                appgroup.IsPatientChosen   = g.IsPatientChosen;
                                appgroup.PageStyle         = g.PageStyle;
                                appgroup.GroupHasReference = g.GroupHasReference;
                                appgroup.ImagesPerPage     = g.ImagesPerPage;



                                if (g.ReferenceIsGlobal)
                                {
                                    appgroup.ReferenceName = "GLOBAL";
                                }
                                else
                                {
                                    appgroup.ReferenceName = "";
                                }

                                int numberOfImages = 0;
                                g.ImagesPerPage = 0;
                                string Patient = "";
                                for (int i = 0; i < listGroupImage.Count; i++)
                                {
                                    if (i == 0)
                                    {
                                        Patient = listGroupImage[0].PatienteName;
                                    }

                                    if (listGroupImage[i].PatienteName == Patient)
                                    {
                                        g.ImagesPerPage++;
                                    }

                                    appgroup.LoopOver += listGroupImage[i].Path.Replace("/", "_") + System.Environment.NewLine;
                                    numberOfImages++;
                                    if (listGroupImage[i].IsReference)
                                    {
                                        appgroup.ReferenceName = listGroupImage[i].Path.Split('/')[1];
                                    }
                                }

                                if (g.PageStyle > 0)
                                {
                                    g.ImagesPerPage = Convert.ToInt32(g.PageStyle);
                                }

                                if (g.ImagesPerPage < 1)
                                {
                                    g.ImagesPerPage = -1;
                                }

                                appgroup.PageStyleDescription = getText(g.PageStyle, numberOfImages);
                                appgroup.OverallNumber        = numberOfImages;
                                double numberOfPages = (numberOfImages / g.ImagesPerPage);
                                appgroup.NumberOfPages = (int)Math.Ceiling(numberOfPages);


                                listGroup.Add(appgroup);
                            }

                            Session["Group"]       = listGroup;
                            gvGroupName.DataSource = listGroup;
                            gvGroupName.DataBind();
                            EnabledSelectedTypeUser();
                            btnUpdateTestCase.Visible = true;
                        }
                        else
                        {
                            btnUpdateTestCase.Visible = false;// es wurde kein id gefunden und desshalb muessen keine infos geladen werden
                        }
                    }
                }

                gvContinuous.DataSource = tsc;
                gvContinuous.DataBind();
                gvContinuous.Visible = false;
                ScriptManager.RegisterStartupScript(this, GetType(), "fUpdateTable", "fUpdateTable();", true);
            }
            if (Session["Group"] != null)
            {
                listGroup = (List <Class.Group>)Session["Group"];
            }

            if (Session["typeckeched"] != null)
            {
                listtypechecked = (List <string>)Session["typeckeched"];
            }
        }