protected void btnPreview_Click(object sender, EventArgs e)
        {
            try
            {
                int DataElementId = Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]);
                _conditionId = Convert.ToInt16(lstCondition.SelectedItem.Value);
                String Entity = _sqlHelper.GetEntityNameByDataElementId(DataElementId);
                String FieldName = _sqlHelper.GetFieldNameByDataElementId(DataElementId);
                String DataType = _sqlHelper.GetDataTypeByDataElementId(DataElementId);
                bool _responseType = false;
                _user = (User)Session["UserDetail"];
                String UserId = _user.ExternalId;
                String SchoolId = Session[SessionEnum.SchoolId.ToString()].ToString();
                String CourseId = Session[SessionEnum.CourseId.ToString()].ToString();
                String SectionId = Session[SessionEnum.SectionId.ToString()].ToString();
                String Value1 = txtSetVal.Text;
                String Value2 = txtSetVal2.Text;

               // String ExternalField = "";
                bool _isPreview = true;

                DataTable dt = _inBloomApi.RunFlag(FieldName, DataType, _responseType, UserId, SchoolId, CourseId, SectionId, _conditionId, Value1, Value2, Entity, _isPreview);

                gridDisplayResult.DataSource = dt;
                gridDisplayResult.DataBind();
                gridDisplayResult.HeaderRow.Cells[1].Text = FieldName;
            }
            catch (Exception Ex)
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _user = (User) Session["UserDetail"];
            Session[SessionEnum.AccessToken.ToString()] = null;

            if (Session["ErrorStack"] != null)
            {
                if (_user != null)
                    lblUser.Text = _user.FullName;

                lblErrorMessage.Text = Session["ErrorStack"].ToString();

                Session["ErrorStack"] = null;
            }
        }
        public void UpateFlag(int FlagId, String UserCustomLink, String EducationOrganizationId, bool IsAdmin, bool IsPublic, String FlagUserId, FlagCls[] _flagClsUpdated, User _user)
        {
            if (IsAdmin)
            {

                if (IsPublic)
                {
                    // get from orgnization
                    if (_flagClsUpdated[0].IsPublic == false)
                    {
                        // process to delete flag from organization and add to custom of admin
                        //public to private
                        DeleteFlagIntoEducationOrganization(_user, EducationOrganizationId, _flagClsUpdated);

                        AddFlagsIntoCustom(UserCustomLink, _user, _flagClsUpdated);
                    }
                    else
                    {
                        // update at organization
                        UpdateFlagIntoEducationOrganization(_user, EducationOrganizationId, _flagClsUpdated);
                    }
                }
                else
                    if ((!IsPublic) && _flagClsUpdated[0].IsPublic)
                    {
                        // private to public
                        DeleteFlagsIntoCustom(UserCustomLink, _user, _flagClsUpdated);

                        AddFlagIntoEducationOrganization(_user, EducationOrganizationId, _flagClsUpdated);
                    }
                    else
                    {
                        // update at custom
                        UpdateFlagsIntoCustom(UserCustomLink, _user, _flagClsUpdated);
                    }
            }
            else
                UpdateFlagsIntoCustom(UserCustomLink, _user, _flagClsUpdated);
        }
 public bool IsLeader()
 {
     _user = (User)Session["UserDetail"];
     String[] _sliRoles = _user.SliRoles;
     for (int i = 0; i < _sliRoles.Count(); i++)
     {
         if (_sliRoles[i] == "Leader")
         {
             return true;
         }
     }
     return false;
 }
        public void GetSectionList()
        {
            DropDownListSection.Items.Clear();
            try
            {
                _user = (User)Session["UserDetail"];
                if (_user.IsAdminUser)
                {
                    String CourseName = DropDownListCourse.SelectedItem.Value;
                    ListItem[] _sectionList = _inBloomApi.GetSectionForAdmin(DropDownListSchool.SelectedItem.Value.ToString(), CourseName);

                    if (_sectionList != null)
                    {
                        for (int i = 0; i < _sectionList.Count(); i++)
                        {
                            if (_sectionList[i] != null)
                            {
                                DropDownListSection.Items.Add(new ListItem(_sectionList[i].Text, _sectionList[i].Value.ToString()));
                            }
                        }
                    }

                    ListItem[] _temp = new ListItem[DropDownListSection.Items.Count];
                    for (int i = 0; i < DropDownListSection.Items.Count; i++)
                    {
                        _temp[i] = new ListItem(DropDownListSection.Items[i].Text, DropDownListSection.Items[i].Value);
                    }
                    Section section = new Section(_temp);
                    Session["Section"] = section;

                }
                else
                {
                    ListItem[] _courseIdAndCourseOfferingId = (ListItem[])Session["CourseIdAndCourseOfferingId"];
                    String SelectedCourseId = DropDownListCourse.SelectedItem.Value;
                    String CourseOfferingId = "";
                    for (int i = 0; i < _courseIdAndCourseOfferingId.Count(); i++)
                    {
                        if (_courseIdAndCourseOfferingId[i].Text == SelectedCourseId)
                        {
                            CourseOfferingId = _courseIdAndCourseOfferingId[i].Value;
                        }
                    }

                    ListItem[] _listSection = _inBloomApi.GetSection(DropDownListSchool.SelectedItem.Value.ToString(), CourseOfferingId);

                    if (_listSection != null)
                    {
                        ListItem[] _sectionAndCourseOfferingId = (ListItem[])Session["SectionAndCourseOfferingId"];
                        ListItem[] _temp = new ListItem[_sectionAndCourseOfferingId.Count()];
                        for (int Index = 0; Index < _listSection.Length; Index++)
                        {
                            if (_listSection[Index] != null)
                            {
                                for (int i = 0; i < _sectionAndCourseOfferingId.Count(); i++)
                                {
                                    if (_sectionAndCourseOfferingId[i].Text == _listSection[Index].Value.ToString())
                                    {
                                        DropDownListSection.Items.Add(new ListItem(_listSection[Index].Text, _listSection[Index].Value.ToString()));
                                        _temp[i] = new ListItem(_listSection[Index].Text, _listSection[Index].Value.ToString());
                                    }
                                }

                            }

                        }

                        //ListItem[] _temp = new ListItem[_listSection.Count()];
                        //int count = 0;
                        //for (int i = 0; i < _listSection.Count(); i++)
                        //{
                        //    if (_listSection[i] != null)
                        //    {
                        //        _temp[count] = new ListItem(_listSection[i].Text, _listSection[i].Value);
                        //        count = count + 1;
                        //    }
                        //}

                        //Section section = new Section(_listSection);
                        //Session["Section"] = section;
                        Section section = new Section(_temp);
                        Session["Section"] = section;
                    }
                    else
                    {
                        Session["Section"] = null;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        public void GetCourseList()
        {
            DropDownListCourse.Items.Clear();
            try
            {
                ListItem[] _listCourse = _inBloomApi.GetCourseBySchool(DropDownListSchool.SelectedItem.Value.ToString());
                _user = (User)Session["UserDetail"];

                if (_user.IsAdminUser || IsLeader())
                {
                    _inBloomApi.GetSectionByStaffIdAndSchoolId(dropDownListStaff.SelectedItem.Value, DropDownListSchool.SelectedItem.Value);

                    //_sectionAndCourseOfferingId=> sectionid and courseofferingid
                    ListItem[] _sectionAndCourseOfferingId = _inBloomApi.GetSectionAndCourseOfferingIdList();
                    ListItem[] _temp = new ListItem[_sectionAndCourseOfferingId.Count()];
                    ListItem[] _courseIdAndCourseOfferingId = new ListItem[_sectionAndCourseOfferingId.Count()];

                    for (int i = 0; i < _sectionAndCourseOfferingId.Count(); i++)
                    {
                        ListItem _course = _inBloomApi.GetCourseBySectionCourseOfferingId(_sectionAndCourseOfferingId[i].Value);
                        _temp[i] = new ListItem(_course.Text, _course.Value);
                        _courseIdAndCourseOfferingId[i] = new ListItem(_course.Value, _sectionAndCourseOfferingId[i].Value);

                    }

                    DropDownListCourse.Items.AddRange(_temp);
                    Course course = new Course(_temp);
                    Session["Course"] = course;
                    Session["SectionAndCourseOfferingId"] = _sectionAndCourseOfferingId;
                    Session["CourseIdAndCourseOfferingId"] = _courseIdAndCourseOfferingId;

                }
                else
                {

                    if (!IsEducator())
                    {
                        _inBloomApi.GetSectionByStaffIdAndSchoolId(dropDownListStaff.SelectedItem.Value, DropDownListSchool.SelectedItem.Value);
                    }
                    else
                    {
                        _inBloomApi.GetSectionListForLoginUser();
                    }
                    ListItem[] _sectionAndCourseOfferingId = _inBloomApi.GetSectionAndCourseOfferingIdList();
                    ListItem[] _temp = new ListItem[_sectionAndCourseOfferingId.Count()];
                    ListItem[] _courseIdAndCourseOfferingId = new ListItem[_sectionAndCourseOfferingId.Count()];
                    if (_listCourse != null)
                    {

                        for (int i = 0; i < _sectionAndCourseOfferingId.Count(); i++)
                        {
                            ListItem _course = _inBloomApi.GetCourseBySectionCourseOfferingId(_sectionAndCourseOfferingId[i].Value);

                            // check more than one course
                            bool IsCoursePresent = false;
                            for (int j = 0; j < _temp.Count(); j++)
                            {
                                if (_temp[j] != null)
                                {
                                    if (_temp[j].Value == _course.Value)
                                    {
                                        IsCoursePresent = true;
                                        break;
                                    }
                                }
                            }

                            if (IsCoursePresent)
                            {
                            }
                            else
                            {
                                _temp[i] = new ListItem(_course.Text, _course.Value);
                            }
                            _courseIdAndCourseOfferingId[i] = new ListItem(_course.Value, _sectionAndCourseOfferingId[i].Value);

                            //for (int Index = 0; Index < _listCourse.Count(); Index++)
                            //{
                            //    bool Find = _inBloomApi.IsSectionContainOfferingOfThisCourse(_sectionAndCourseOfferingId[i].Value, _listCourse[Index].Value);
                            //    if (Find)
                            //    {
                            //        _temp[i] = new ListItem(_listCourse[Index].Text, _listCourse[Index].Value);
                            //        _courseIdAndCourseOfferingId[i] = new ListItem(_listCourse[Index].Value, _sectionAndCourseOfferingId[i].Value);
                            //        break;
                            //    }
                            //}
                        }

                        //DropDownListCourse.Items.AddRange(_listCourse);
                        //Course course = new Course(_listCourse);
                        //Session["Course"] = course;

                        for (int i = 0; i < _temp.Count(); i++)
                        {
                            if (_temp[i] != null)
                                DropDownListCourse.Items.Add(_temp[i]);
                        }

                        // DropDownListCourse.Items.AddRange(_temp);
                        Course course = new Course(_temp);
                        Session["Course"] = course;
                        Session["SectionAndCourseOfferingId"] = _sectionAndCourseOfferingId;
                        Session["CourseIdAndCourseOfferingId"] = _courseIdAndCourseOfferingId;
                    }
                    else
                    {
                        Session["Course"] = null;
                    }

                }

            }
            catch (Exception ex)
            {
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            try
            {
                _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());
            }
            catch (Exception Ex)
            {
                Response.Redirect("Search.aspx");
            }

            if (!IsPostBack)
            {
                if (Session["CopyAggregateFlag"] != null)
                {
                    _aggregateFlagId = Convert.ToInt16(Session["CopyAggregateFlag"].ToString());

                    IList<AggregateFlag> _AggregateFlag = _sqlHelper.GetAggregateFlag(_aggregateFlagId);

                    txtFlagName.Text = _AggregateFlag[0].AggregateFlagName;
                    txtDescription.Text = _AggregateFlag[0].AggregateFlagDescription;
                    txtFlag.Text = _AggregateFlag[0].Keyword;

                    _user = (User)Session["UserDetail"];
                    if (_user == null)
                    {
                        Session[SessionEnum.AccessToken.ToString()] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else
                    {
                        if (_user.IsAdminUser)
                        {
                            String IsPublic = _AggregateFlag[0].IsPublic.ToString();
                            if (IsPublic == "True")
                            {
                                radioFlagType.SelectedIndex = 0;
                            }
                            else
                            {
                                radioFlagType.SelectedIndex = 1;
                            }
                        }
                        else
                        {
                            PanelFlagType.Visible = false;
                        }

                        _user = (User)Session["UserDetail"];
                        //gridStudentInfo.DataSource = _sqlHelper.GetFlagList(_user.ExternalId);
                        //gridStudentInfo.DataBind();

                        IList<Flag> _flagForGrid = _sqlHelper.GetFlagList(_user.ExternalId);
                        IList<Flag> _flagListTemp = null;

                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();
                        String[] AdminUserId = _inBloomApi.GetAdminIdByEducationOrganizationId(EducationOrganizationId);
                        if (AdminUserId != null)
                        {
                            for (int i = 0; i < AdminUserId.Count(); i++)
                            {
                                _flagListTemp = _sqlHelper.GetPublicFlagListForAdmin(AdminUserId[i]);
                                if (_flagForGrid == null)
                                {
                                    _flagForGrid = _flagListTemp;
                                }
                                else
                                {
                                    for (int j = 0; j < _flagListTemp.Count; j++)
                                    {
                                        _flagForGrid.Add(_flagListTemp[j]);
                                    }

                                }
                            }
                        }

                        gridStudentInfo.DataSource = _flagForGrid;
                        gridStudentInfo.DataBind();

                        // add to listbox
                        IList<Flag> _flag = _sqlHelper.GetFlagAddedForInAggregate(_aggregateFlagId);
                        for (int i = 0; i < _flag.Count; i++)
                        {
                            int FlagId = _flag[i].FlagId;
                            String FlagName = _flag[i].FlagName;

                            lstcategory.Items.Add(new ListItem(FlagName, FlagId.ToString()));
                        }

                        foreach (GridViewRow row in gridStudentInfo.Rows)
                        {
                            int FlagId = Convert.ToInt16(row.Cells[4].Text);
                            for (int i = 0; i < _flag.Count; i++)
                            {
                                if (_flag[i].FlagId == FlagId)
                                {
                                    row.Visible = false;
                                    break;
                                }
                            }
                        }
                    }

                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _inBloomApi = new inBloomApi();
            _sqlHelper = new SqlHelper();
            _user = (User) Session["UserDetail"];

            try
            {
                _inBloomApi.AccessToken = Session[SessionEnum.AccessToken.ToString()].ToString();
            }
            catch (Exception ex)
            {
                Response.Redirect("Search.aspx");
            }

            if (!IsPostBack)
            {
                try
                {
                    if (!IsPostBack)
                    {
                        if (Session[SessionEnum.SchoolId.ToString()] == null)
                        {
                        }
                        else
                        {
                            LoadFieldNames();

                            _schoolId = Session[SessionEnum.SchoolId.ToString()].ToString();
                            _courseId = Session[SessionEnum.CourseId.ToString()].ToString();
                            _sectionId = Session[SessionEnum.SectionId.ToString()].ToString();

                            _user = (User) Session["UserDetail"];

                            if (Session["FlagId"] == null || Session["FlagType"] == null)
                            {
                                // show result for school and courses only.
                                Response.Redirect("Search.aspx");
                            }
                            else
                            {
                                _flagId = Convert.ToInt16(Session["FlagId"].ToString());
                                _flagType = Session["FlagType"].ToString();

                                if (_flagType.Equals("Flag"))
                                {
                                    // get all the values for flag
                                    IList<Flag> _flag = _sqlHelper.GetFlag(_flagId);

                                    lblFlagName.Text = _flag[0].FlagName;
                                    lblFlagDescription.Text = _flag[0].FlagDescription;

                                    if (_flag.Count > 0)
                                    {
                                        int DataElementId = _flag[0].DataElementId;
                                        int ConditionId = _flag[0].ConditionId;
                                        _value1 = _flag[0].ValueSet1;
                                        _value2 = _flag[0].ValueSet2;
                                        _flagUser = _flag[0].UserId;

                                        _dataType = _sqlHelper.GetDataTypeByDataElementId(DataElementId);
                                        _fieldName = _sqlHelper.GetFieldNameByDataElementId(DataElementId);
                                        _entityName = _sqlHelper.GetEntityNameByDataElementId(DataElementId);

                                        DataTable dt = _inBloomApi.RunFlag(_fieldName, _dataType, _responseType, _flagUser, _schoolId, _courseId, _sectionId, ConditionId, _value1, _value2, _entityName, _isPreview);

                                        Session.Add("FieldName", _fieldName);
                                        gvResult.DataSource = dt;
                                        gvResult.DataBind();

                                        //gvResult.HeaderRow.Cells[3].Text = _inBloomApi.GetWellFormattedString(FieldName);
                                        for (int i = 0; i < gvResult.HeaderRow.Cells.Count; i++)
                                        {
                                            String HeaderText = gvResult.HeaderRow.Cells[i].Text;
                                            if (HeaderText == "FieldName")
                                            {

                                                gvResult.HeaderRow.Cells[i].Text = _inBloomApi.GetWellFormattedString(char.ToUpper(_entityName[0]) + _entityName.Substring(1)) + "." + _inBloomApi.GetWellFormattedString(_fieldName);

                                                break;
                                            }
                                        }

                                        HideColumnInitial();

                                        Session.Add("ResultGrid", dt);

                                        if (_sqlHelper.IsFav(_flagId) || _sqlHelper.IsInPublicFavorite(_flagId, _user.ExternalId))
                                        {
                                            btnlnkfavorite.Text = "Remove Favorite";
                                            btnlnkfavorite.CssClass = "";
                                        }
                                        else
                                        {
                                            btnlnkfavorite.Text = "Add to Favorite";
                                            btnlnkfavorite.CssClass = "unfavorite_result";
                                        }
                                    }
                                }
                                else if (_flagType.Equals("AggregateFlag"))
                                {
                                    try
                                    {
                                        IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(_flagId);

                                        lblFlagName.Text = _aggregateFlag[0].AggregateFlagName;
                                        lblFlagDescription.Text = _aggregateFlag[0].AggregateFlagDescription;

                                        int[] FlagArrayId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(_flagId);

                                        DataTable dt = _inBloomApi.RunAggregateFlag(FlagArrayId, _schoolId, _courseId, _sectionId);

                                        GridViewAggregateFlag.DataSource = dt;
                                        Session.Add("ResultGrid", dt);
                                        GridViewAggregateFlag.DataBind();
                                        for (int i = 0; i < GridViewAggregateFlag.HeaderRow.Cells.Count; i++)
                                        {
                                            //GridViewAggregateFlag.HeaderRow.Cells[i].Text = _inBloomApi.GetWellFormattedString(GridViewAggregateFlag.HeaderRow.Cells[i].Text);
                                        }

                                        HideColumnInitialForAggregate();

                                        //GridViewAggregateFlag.HeaderRow.Cells[0].Visible = false;

                                        if (_sqlHelper.IsAggregateIsFav(_flagId) || _sqlHelper.IsAggregateInPublicFavorite(_flagId, _user.ExternalId))
                                        {
                                            btnlnkfavorite.Text = "Remove Favorite";
                                            btnlnkfavorite.CssClass = "";
                                            btnlnkfavorite.Attributes.Add("onclick", "showunfavmsg();");

                                        }
                                        else
                                        {
                                            btnlnkfavorite.Text = "Add to Favorite";
                                            btnlnkfavorite.CssClass = "unfavorite_result";
                                            btnlnkfavorite.Attributes.Add("onclick", "showfavmsg();");

                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                    }

                                }
                            }

                            //LoadStudentGrid();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Redirect("Search.aspx");
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            try
            {
                if (!IsPostBack)
                {
                    if (Session[SessionEnum.AccessToken.ToString()] == null)
                    {
                        if (Request.QueryString[QueryStringTokenEnum.Code.ToString()] == null)
                        {
                            _authenticateUser.AuthorizeUser();
                        }
                        else
                        {
                            Session.Add(SessionEnum.AccessToken.ToString(), _authenticateUser.GetAccessToken());
                        }
                    }

                    _slcApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

                    Session["ResultPage"] = null;
                    _user = _slcApi.UserDetails();

                    if (_user == null)
                    {
                        Session[SessionEnum.AccessToken.ToString()] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else if (Session["UserDetail"] == null)
                    {
                        Session.Add("UserDetail", _user);
                    }

                    _homeLinks = _slcApi.GetHomeLinks();
                    if (_homeLinks != null)
                    {
                        Session.Add("HomeLinks", _homeLinks);
                    }

                    _homeLinks = (JArray) Session["HomeLinks"];
                    String EducationOrganizationId = _slcApi.GetEducationOrganizationId(_homeLinks);

                    if (EducationOrganizationId != null || EducationOrganizationId != "")
                    {
                        // not state level admin
                        Session.Add("EducationOrganizationId", EducationOrganizationId);
                        //_slcApi.GetFlagListForAdminUser(EducationOrganizationId, _user.ExternalId);
                    }

                    LoadFlagGrid();
                }
            }
            catch (Exception Ex)
            {
                //if (Ex.ToString().Contains("The remote server returned an error: (403) Forbidden."))
                //{
                //    Session.Add("ErrorStack", Ex.ToString());
                //    Session[SessionEnum.AccessToken.ToString()] = null;
                //    Response.Redirect("ErrorPage.aspx");

                //}
                //Response.Redirect("Search.aspx");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            try
            {
                _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());
            }
            catch (Exception Ex)
            {
                Response.Redirect("Search.aspx");
            }

            if (!IsPostBack)
            {

                 _user = (User)Session["UserDetail"];

                 if (_user == null)
                 {
                     Session[SessionEnum.AccessToken.ToString()] = null;
                     Response.Redirect("Search.aspx");
                 }
                 else
                 {
                     //gridStudentInfo.DataSource = _sDACEntities.Flags;
                     //gridStudentInfo.DataSource = _sqlHelper.GetFlagList(_user.ExternalId);
                     //gridStudentInfo.DataBind();

                     IList<Flag> _flag = _sqlHelper.GetFlagList(_user.ExternalId);

                     IList<Flag> _flagListTemp = null;

                     String EducationOrganizationId = Session["EducationOrganizationId"].ToString();
                     String[] AdminUserId = _inBloomApi.GetAdminIdByEducationOrganizationId(EducationOrganizationId);
                     if (AdminUserId != null)
                     {
                         for (int i = 0; i < AdminUserId.Count(); i++)
                         {
                             _flagListTemp = _sqlHelper.GetPublicFlagListForAdmin(AdminUserId[i]);
                             if (_flag == null)
                             {
                                 _flag = _flagListTemp;
                             }
                             else
                             {
                                 for (int j = 0; j < _flagListTemp.Count; j++)
                                 {
                                     _flag.Add(_flagListTemp[j]);
                                 }

                             }
                         }
                     }

                     gridStudentInfo.DataSource = _flag;

                     gridStudentInfo.DataBind();

                     if (_user.IsAdminUser)
                     {
                     }
                     else
                     {
                         PanelFlagType.Visible = false;
                     }

                 }

            }

            String script = "function abc() {" +

                         "var count = 0;" +
                         "$('#lstcategory option').each(function (e) {" +
                             "count = count + 1;" +
                         "});" +

                         "$('#gridStudentInfo tr input:checkbox').each(function () {" +

                             "if (this.checked) {" +
                                 "count = count + 1;" +
                             "}" +

                         "});" +

                         "if (count > 5) {" +

                             "DisplayErrorPopup('You can select only 5 flags.');" +
                             "return false;" +
                         "}" +

                         "return true;" +

                     "}";

            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), Guid.NewGuid().ToString(), script, true);
        }
        public void AddAggregateFlagIntoEducationOrganization(User _user, String EducationOrganizationId, AggregateCls[] _aggregateFlagCls)
        {
            try
            {
                AdminCls _adminCls = GetFlagListForAdminUser(EducationOrganizationId, _user.ExternalId);
                Temp _flagListPrivateAdminCustom = null;
                if (_adminCls == null)
                {
                    // no data present in organization
                    Temp[] _temp = new Temp[1];
                    _temp[0] = new Temp();
                    _temp[0].AggregateFlagList = _aggregateFlagCls;
                    _temp[0].UserId = _user.ExternalId;
                    _temp[0].IsAdmin = _user.IsAdminUser;

                    _adminCls = new AdminCls();
                    _adminCls.AdminList = _temp;

                    PutCustomForAdmin(EducationOrganizationId, _adminCls);

                }
                else
                {
                    // data is present need to add new

                    Temp[] _temp = _adminCls.AdminList;
                    bool UserExist = false;
                    int Index = 0;
                    int UserIndex = 0;
                    for (int i = 0; i < _temp.Count(); i++)
                    {
                        if (_temp[i].UserId == _user.ExternalId)
                        {
                            // user record exist
                            UserExist = true;
                            Index = i;
                            break;
                        }
                    }

                    if (UserExist)
                    {

                        Temp _flagForEdit = _temp[Index];

                        AggregateCls[] _flagClsForEdit = _flagForEdit.AggregateFlagList;

                        if (_flagClsForEdit != null)
                        {

                            AggregateCls[] _flagClsNew = new AggregateCls[_flagClsForEdit.Count() + 1];

                            for (int i = 0; i < _flagClsForEdit.Count(); i++)
                            {
                                _flagClsNew[i] = new AggregateCls();
                                _flagClsNew[i] = _flagClsForEdit[i];
                            }

                            _flagClsNew[_flagClsForEdit.Count()] = new AggregateCls();
                            _flagClsNew[_flagClsForEdit.Count()] = _aggregateFlagCls[0];

                            _flagForEdit.AggregateFlagList = _flagClsNew;
                        }
                        else
                        {
                            _flagForEdit.AggregateFlagList = _aggregateFlagCls;
                        }
                        _temp[Index] = _flagForEdit;
                        _adminCls.AdminList = _temp;

                        PutCustomForAdmin(EducationOrganizationId, _adminCls);

                    }
                    else
                    {
                        // new admin user
                        Temp[] _tempForNew = new Temp[_temp.Count() + 1];
                        for (int i = 0; i < _temp.Count(); i++)
                        {
                            _tempForNew[i] = new Temp();
                            _tempForNew[i] = _temp[i];
                        }

                        Temp _tempAdmin = new Temp();
                        _tempAdmin.IsAdmin = _user.IsAdminUser;
                        _tempAdmin.UserId = _user.ExternalId;
                        _tempAdmin.AggregateFlagList = _aggregateFlagCls;

                        _tempForNew[_tempForNew.Count() - 1] = new Temp();
                        _tempForNew[_tempForNew.Count() - 1] = _tempAdmin;

                        _adminCls.AdminList = _tempForNew;

                        PutCustomForAdmin(EducationOrganizationId, _adminCls);
                    }
                }
            }
            catch (Exception Ex)
            {

            }
        }
        public void DeleteFlagsIntoCustom(String Link, User _user, FlagCls[] _flagCls)
        {
            FlagCls[] _forNew = _flagCls;
            AggregateCls[] _forAggregateCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.FlagList = _flagCls;

                JObject UserCustomResponse = JObject.Parse(RestApiHelper.CallApiWithParameter(Link, this.AccessToken));
                if (UserCustomResponse != null)
                {
                    Temp _list = FlagObjectDeserialize(UserCustomResponse.ToString());
                    if (_list != null)
                    {
                        Temp _temp = new Temp();
                        _forAggregateCls = _list.AggregateFlagList;

                        if (_list.FlagList.Count() == 1)
                        {
                            // only one need to delete
                            _temp.FlagList = null;
                        }
                        else
                        {
                            FlagCls[] _flagClsNew = new FlagCls[_list.FlagList.Count() - 1];
                            int Count = 0;
                            for (int i = 0; i < _list.FlagList.Count(); i++)
                            {
                                if (_list.FlagList[i].FlagId == _flagCls[0].FlagId)
                                    continue;
                                _flagClsNew[Count] = new FlagCls();
                                _flagClsNew[Count] = _list.FlagList[i];
                                Count++;
                            }

                            _temp.FlagList = _flagClsNew;
                            _temp.AggregateFlagList = _list.AggregateFlagList;
                            _temp.UserId = _user.ExternalId;
                            _temp.IsAdmin = _user.IsAdminUser;
                        }
                        String Result = FlagObjectToJson(_temp);

                        RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
                    }
                }

            }
            catch (Exception Ex)
            {

            }
        }
        public void DeleteFlagIntoEducationOrganization(User _user, String EducationOrganizationId, FlagCls[] _flagCls)
        {
            try
            {
                AdminCls _adminCls = GetFlagListForAdminUser(EducationOrganizationId, _user.ExternalId);
                Temp _flagListPrivateAdminCustom = null;
                if (_adminCls == null)
                {

                }
                else
                {
                    // data is present need to add new

                    Temp[] _temp = _adminCls.AdminList;
                    bool UserExist = false;
                    int Index = 0;
                    int UserIndex = 0;
                    for (int i = 0; i < _temp.Count(); i++)
                    {
                        if (_temp[i].UserId == _user.ExternalId)
                        {
                            // user record exist
                            UserExist = true;
                            Index = i;
                            break;
                        }
                    }

                    if (UserExist)
                    {

                        Temp _flagForEdit = _temp[Index];

                        FlagCls[] _flagClsForEdit = _flagForEdit.FlagList;
                        FlagCls[] _flagClsNew = null;

                        if (_flagClsForEdit.Count() == 1)
                        {
                            _flagClsNew = null;
                        }
                        else
                        {
                            int Count = 0;
                            _flagClsNew = new FlagCls[_flagClsForEdit.Count() - 1];

                            for (int i = 0; i < _flagClsForEdit.Count(); i++)
                            {
                                if (_flagClsForEdit[i].FlagId == _flagCls[0].FlagId)
                                    continue;
                                _flagClsNew[Count] = new FlagCls();
                                _flagClsNew[Count] = _flagClsForEdit[i];
                                Count++;
                            }

                        }

                        _flagForEdit.FlagList = _flagClsNew;

                        _temp[Index] = _flagForEdit;
                        _adminCls.AdminList = _temp;

                        PutCustomForAdmin(EducationOrganizationId, _adminCls);

                    }
                    else
                    {

                    }
                }
            }
            catch (Exception Ex)
            {

            }
        }
        /// <summary>
        /// This function is used to get the User session with all user details.
        /// </summary>
        /// <returns></returns>
        public User UserDetails()
        {
            try
            {
                JObject UserResponse = JObject.Parse(RestApiHelper.CallApiWithParameter(ConfigurationHelper.GetItem("UserSession"), this._accessToken));
                User _user=new User();
                _user.Authenticated =(bool) UserResponse["authenticated"];

                _user.EducationOrganization = _authenticateUser.GetStringWithoutQuote(UserResponse["edOrg"].ToString());
                _user.EducationOrganizationId=_authenticateUser.GetStringWithoutQuote(UserResponse["edOrgId"].ToString());
                _user.Email = _authenticateUser.GetStringWithoutQuote(UserResponse["email"].ToString());

                _user.FullName = _authenticateUser.GetStringWithoutQuote(UserResponse["full_name"].ToString());

                JToken Token= UserResponse["granted_authorities"];
                String[] Auth = new String[Token.Count()];
                for (int i = 0; i < Token.Count(); i++)
                {
                    Auth[i] = _authenticateUser.GetStringWithoutQuote(Token[i].ToString());

                }
                _user.GrantedAuthorities = Auth;
                _user.Realm=_authenticateUser.GetStringWithoutQuote(UserResponse["realm"].ToString());

                Token = UserResponse["rights"];

                String[] Rights = new String[Token.Count()];

                for (int i = 0; i < Token.Count(); i++)
                {
                    Rights[i] = _authenticateUser.GetStringWithoutQuote(Token[i].ToString());
                }
                _user.Rights = Rights;

                Token = UserResponse["sliRoles"];

                String[] SliRoles = new String[Token.Count()];
                for (int i = 0; i < Token.Count(); i++)
                {
                    SliRoles[i] = _authenticateUser.GetStringWithoutQuote(Token[i].ToString());
                }
                _user.SliRoles = SliRoles;

                _user.TenantId = _authenticateUser.GetStringWithoutQuote(UserResponse["tenantId"].ToString());
                // TenantId added here to prevent the application database from sharing user information across tenancies for users with the same inbloom userId
                _user.ExternalId = _authenticateUser.GetStringWithoutQuote(UserResponse["external_id"].ToString()) + _user.TenantId;

                _user.UserId = _authenticateUser.GetStringWithoutQuote(UserResponse["user_id"].ToString());

                _user.IsAdminUser =(bool)UserResponse["isAdminUser"];

                return _user;

            }
            catch (Exception Ex)
            {
               return null;
            }
        }
        public void UpdateFlagsIntoCustom(String Link, User _user, FlagCls[] _flagCls)
        {
            FlagCls[] _forNew = _flagCls;
            AggregateCls[] _forAggregateCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.FlagList = _flagCls;

                JObject UserCustomResponse = JObject.Parse(RestApiHelper.CallApiWithParameter(Link, this.AccessToken));
                if (UserCustomResponse != null)
                {
                    Temp _list = FlagObjectDeserialize(UserCustomResponse.ToString());
                    if (_list != null)
                    {
                        Temp _temp = new Temp();
                        _forAggregateCls = _list.AggregateFlagList;

                        FlagCls[] _flagClsNew = new FlagCls[_list.FlagList.Count()];
                        for (int i = 0; i < _list.FlagList.Count(); i++)
                        {
                            _flagClsNew[i] = new FlagCls();
                            if (_list.FlagList[i].FlagId == _flagCls[0].FlagId)
                            {
                                _flagClsNew[i] = _flagCls[0];
                            }
                            else
                                _flagClsNew[i] = _list.FlagList[i];
                        }

                        _temp.FlagList = _flagClsNew;
                        _temp.AggregateFlagList = _list.AggregateFlagList;

                        String Result = FlagObjectToJson(_temp);

                        RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
                    }
                }

            }
            catch (Exception Ex)
            {

            }
        }
        public DataView GetFlagForGrid()
        {
            try
            {
                _slcApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

                _user = (User) Session["UserDetail"];

                Temp[] _temp = null;
                Temp[] _tempForStaff = null;
                JArray _homeLinks = (JArray) Session["HomeLinks"];
                String UserCustomLink = _slcApi.GetCustomLink(_homeLinks);

                String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                if (_user.IsAdminUser)
                {
                    // get all the flags from organization by admin user specific
                    _temp = _slcApi.GetFlagListByEducationOrganization(_user, EducationOrganizationId);
                    ListItem[] _schoolList = null;

                    _schoolList = GetSchoolList();

                    _tempForStaff = _slcApi.GetFlagListOfStaffForAdminUser(_user, EducationOrganizationId, _schoolList);

                    if (_tempForStaff.Any())
                    {
                        if (_temp != null)
                        {
                            Temp[] _tempNew = _temp;
                            _temp = new Temp[_tempNew.Count() + _tempForStaff.Count()];

                            for (int i = 0; i < _tempNew.Count(); i++)
                            {
                                _temp[i] = new Temp();
                                _temp[i] = _tempNew[i];
                            }
                            int j = 0;
                            for (int i = _tempNew.Count(); i < _tempNew.Count() + _tempForStaff.Count(); i++)
                            {
                                _temp[i] = new Temp();
                                _temp[i] = _tempForStaff[j];
                                j++;
                            }
                        }
                        else
                            _temp = _tempForStaff;
                    }
                }
                else
                {
                    // get all the flags from custom and organization for normal users i.e public flag
                    _temp = _slcApi.GetFlagListByEducationOrganization(null, EducationOrganizationId);
                }

                Temp[] _newTemp = null;
                Temp _ownTemp = null;

                if (_temp != null)
                {
                    _newTemp = new Temp[_temp.Count() + 1];
                    for (int i = 0; i < _temp.Count(); i++)
                    {
                        _newTemp[i] = new Temp();
                        _newTemp[i] = _temp[i];
                    }

                    _ownTemp = _slcApi.GetCustomForStaff(UserCustomLink);
                    _newTemp[_temp.Count()] = new Temp();
                    _newTemp[_temp.Count()] = _ownTemp;
                }
                else
                {
                    // to get the flags from user custom, private for admin users
                    _ownTemp = _slcApi.GetCustomForStaff(UserCustomLink);
                    _newTemp = new Temp[1];
                    _newTemp[0] = new Temp();
                    _newTemp[0] = _ownTemp;
                }

                DataTable dt = new DataTable("blablaTable");
                dt.Columns.Add("FlagId", typeof (int));
                dt.Columns.Add("FlagName", typeof (string));
                dt.Columns.Add("FlagDescription", typeof (string));
                dt.Columns.Add("Keyword", typeof (string));
                dt.Columns.Add("IsPublic", typeof (bool));
                dt.Columns.Add("IsFavorite", typeof (bool));
                dt.Columns.Add("CreatedDate", typeof (DateTime));
                dt.Columns.Add("Type", typeof (string));

                dt.Columns.Add("IsAdmin", typeof (string));
                dt.Columns.Add("UserId", typeof (string));

                for (int i = 0; i < _newTemp.Count(); i++)
                {
                    if (_newTemp[i] != null)
                    {
                        FlagCls[] _flagCls = _newTemp[i].FlagList;
                        AggregateCls[] _aggregateFlagCls = _newTemp[i].AggregateFlagList;

                        if (_flagCls != null)
                        {
                            for (int j = 0; j < _flagCls.Count(); j++)
                            {
                                if (_flagCls[j].IsDeleted == false)
                                    dt.Rows.Add(new Object[] {_flagCls[j].FlagId, _flagCls[j].FlagName, _flagCls[j].FlagDescription, _flagCls[j].FlagKeyword, _flagCls[j].IsPublic, _flagCls[j].IsFavorite, _flagCls[j].CreatedDate, "Flag", _newTemp[i].IsAdmin, _newTemp[i].UserId});
                            }
                        }

                        if (_aggregateFlagCls != null)
                        {
                            for (int j = 0; j < _aggregateFlagCls.Count(); j++)
                            {
                                if (_aggregateFlagCls[j].IsDeleted == false)
                                    dt.Rows.Add(new Object[] {_aggregateFlagCls[j].AggregateFlagId, _aggregateFlagCls[j].AggregateFlagName, _aggregateFlagCls[j].AggregateFlagDescription, _aggregateFlagCls[j].Keyword, _aggregateFlagCls[j].IsPublic, _aggregateFlagCls[j].IsFavorite, _aggregateFlagCls[j].CreatedDate, "AggregateFlag", _newTemp[i].IsAdmin, _newTemp[i].UserId});
                            }
                        }

                    }
                }

                return dt.DefaultView;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        protected void gridViewFlag_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridViewRow d = e.Row;

                // check the flag is public or private and the button of edit and delete
                String IsPublic = d.Cells[4].Text;

                _user = (User) Session["UserDetail"];

                if (IsPublic == "True")
                {
                    // hide the controls
                    ImageButton btnDelete = (ImageButton) d.FindControl("ImgBtndelete");
                    btnDelete.Visible = false;

                    ImageButton btnEdit = (ImageButton) d.FindControl("ImgBtnEdit");
                    btnEdit.Visible = false;
                }

                // admin can edit and delete his flag
                if (_user.IsAdminUser)
                {
                    ImageButton btnDelete = (ImageButton) d.FindControl("ImgBtndelete");
                    btnDelete.Visible = true;

                    ImageButton btnEdit = (ImageButton) d.FindControl("ImgBtnEdit");
                    btnEdit.Visible = true;
                }

                // check flag is favorite or not

                String IsFavorite = d.Cells[5].Text;

                int FlagId = Convert.ToInt16(gridViewFlag.DataKeys[e.Row.RowIndex].Value.ToString());

                String FlagType = d.Cells[7].Text;

                ImageButton imgBtnFav = null;

                if (FlagType == "Flag")
                {
                    if (_sqlHelper.AddedByUser(_user.ExternalId, FlagId))
                    {
                        if (IsFavorite == "True")
                        {
                            imgBtnFav = (ImageButton) d.FindControl("ImgBtnFav");
                            imgBtnFav.CssClass = "fav";
                            imgBtnFav.ToolTip = "Remove from Favorite";
                            //imgBtnFav.OnClientClick = "return confirm('Are you sure you want to set flag as un favorite ?');";
                            imgBtnFav.OnClientClick = "javascript:showunfavmsg();";

                        }
                        else
                        {
                            imgBtnFav = (ImageButton) d.FindControl("ImgBtnFav");
                            imgBtnFav.CssClass = "unfav";
                            //imgBtnFav.ImageUrl = "~/Images/fav.png";
                            imgBtnFav.ToolTip = "Add to Favorite";
                            //imgBtnFav.OnClientClick = "return confirm('Are you sure you want to set flag as favorite ?');";
                            imgBtnFav.OnClientClick = "javascript:showfavmsg();";
                        }
                    }
                    else if (_sqlHelper.IsInPublicFavorite(FlagId, _user.ExternalId))
                    {
                        imgBtnFav = (ImageButton) d.FindControl("ImgBtnFav");
                        imgBtnFav.CssClass = "fav";
                        imgBtnFav.ToolTip = "Remove from Favorite";
                        //imgBtnFav.OnClientClick = "return confirm('Are you sure you want to set flag as un favorite ?');";
                        imgBtnFav.OnClientClick = "javascript:showunfavmsg();";
                    }
                    else
                    {
                        imgBtnFav = (ImageButton) d.FindControl("ImgBtnFav");
                        imgBtnFav.CssClass = "unfav";
                        imgBtnFav.ToolTip = "Add to Favorite";
                        //imgBtnFav.OnClientClick = "return confirm('Are you sure you want to set flag as favorite ?');";
                        imgBtnFav.OnClientClick = "javascript:showfavmsg();";
                    }
                }
                else if (FlagType == "AggregateFlag")
                {
                    // we need to change the command name and other things

                    if (_sqlHelper.AggregateAddedByUser(_user.ExternalId, FlagId))
                    {
                        if (IsFavorite == "True")
                        {
                            imgBtnFav = (ImageButton) d.FindControl("ImgBtnFav");
                            imgBtnFav.CssClass = "fav";
                            imgBtnFav.ToolTip = "Remove from Favorite";
                            imgBtnFav.OnClientClick = "javascript:showunfavmsg();";
                        }
                        else
                        {
                            imgBtnFav = (ImageButton) d.FindControl("ImgBtnFav");
                            imgBtnFav.CssClass = "unfav";
                            //imgBtnFav.ImageUrl = "~/Images/fav.png";
                            imgBtnFav.ToolTip = "Add to Favorite";
                            imgBtnFav.OnClientClick = "javascript:showfavmsg();";
                        }
                    }

                    else if (_sqlHelper.IsAggregateInPublicFavorite(FlagId, _user.ExternalId))
                    {
                        imgBtnFav = (ImageButton) d.FindControl("ImgBtnFav");
                        imgBtnFav.CssClass = "fav";
                        imgBtnFav.ToolTip = "Remove from Favorite";
                        imgBtnFav.OnClientClick = "javascript:showunfavmsg();";
                    }
                    else
                    {
                        imgBtnFav = (ImageButton) d.FindControl("ImgBtnFav");
                        imgBtnFav.CssClass = "unfav";
                        imgBtnFav.ToolTip = "Add to Favorite";
                        imgBtnFav.OnClientClick = "javascript:showfavmsg();";
                    }

                    ImageButton btnCopy = (ImageButton) d.FindControl("ImgBtnCopy");
                    btnCopy.CommandName = "CmdCopyAggregateFlag";

                    ImageButton btnEdit = (ImageButton) d.FindControl("ImgBtnEdit");
                    btnEdit.CommandName = "CmdEditAggregateFlag";

                    ImageButton btnFav = (ImageButton) d.FindControl("ImgBtnFav");
                    btnFav.CommandName = "CmdFavoriteAggregateFlag";

                    ImageButton btnDel = (ImageButton) d.FindControl("ImgBtndelete");
                    btnDel.CommandName = "CmdDeleteAggregateFlag";

                    d.Cells[0].CssClass = "agimage";
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                bool flagValue = false;

                SqlHelper _sqlHelper = new SqlHelper();

                if (Session["UserDetail"] != null)
                {
                    _user = (User)Session["UserDetail"];

                    if (_user.IsAdminUser)
                        flagValue = radioFlagType.SelectedItem.Text == "Public";

                    bool IsFlagAdded = false;

                    if (Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"] != null)
                    {
                        IsFlagAdded = _sqlHelper.AddFlag(txtFlagName.Text.Trim(), txtDescription.Text.Trim(), txtFlag.Text.Trim(), Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]),
                        Convert.ToInt16(lstCondition.SelectedItem.Value), txtSetVal.Text.Trim(), txtSetVal2.Text.Trim(), _user.ExternalId, _user.FullName, flagValue
                       );
                    }

                    if (IsFlagAdded)
                    {
                        JArray _links = (JArray)Session["HomeLinks"];
                        int FlagId = _sqlHelper.GetFlagId(txtFlagName.Text.Trim());

                        String GetCustomLink = _inBloomApi.GetCustomLink(_links);
                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                        FlagCls[] _flagCls = new FlagCls[1];
                        _flagCls[0] = new FlagCls();
                        _flagCls[0].FlagId = FlagId;
                        _flagCls[0].FlagName = txtFlagName.Text.Trim();
                        _flagCls[0].FlagDescription = txtDescription.Text.Trim();
                        _flagCls[0].FlagKeyword = txtFlag.Text.Trim();
                        _flagCls[0].IsPublic = false;
                        _flagCls[0].IsFavorite = false;
                        _flagCls[0].IsDeleted = false;
                        _flagCls[0].ConditionId = Convert.ToInt16(lstCondition.SelectedItem.Value);
                        _flagCls[0].DataElementId = Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]);
                        _flagCls[0].ValueSet1 = txtSetVal.Text.Trim();
                        _flagCls[0].ValueSet2 = txtSetVal2.Text.Trim();
                        _flagCls[0].UserId = _user.ExternalId;
                        _flagCls[0].CreatedBy = _user.ExternalId;
                        _flagCls[0].CreatedDate = DateTime.Now;
                        _flagCls[0].ModifiedBy = _user.ExternalId;
                        _flagCls[0].ModifiedDate = DateTime.Now;

                        if (_user.IsAdminUser && flagValue == true)
                        {
                            // process to store the public flag into organization
                            _flagCls[0].IsPublic = true;
                            _inBloomApi.AddFlagIntoEducationOrganization(_user, EducationOrganizationId, _flagCls);
                        }
                        else
                        {
                            // process to add flag
                            _inBloomApi.AddFlagsIntoCustom(GetCustomLink, _user, _flagCls);
                        }

                        //successfully added
                        Session["CopyFlag"] = null;
                        Session["Success"]="Flag copied Successfully";
                        Session["FlagIdForCopyPage"] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else
                    {
                        Session["Success"] = "A flag with the name " + txtFlagName.Text.ToString() + " already exists.  Please enter a different name.";
                        Session["CopyFlag"] = Session["FlagIdForCopyPage"].ToString();
                        Response.Redirect("CopyFlag.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        protected void btnlnkfavorite_Click(object sender, EventArgs e)
        {
            int FlagId = Convert.ToInt16(Session["FlagId"].ToString());
            String FlagType = Session["FlagType"].ToString();
            if (FlagType == "Flag")
            {
                // check for flag
                _sqlHelper.FavoriteFlag(FlagId, _user.ExternalId);

                JArray _links = (JArray) Session["HomeLinks"];

                String UserCustomLink = _inBloomApi.GetCustomLink(_links);
                String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                String FlagUserId = Session["CustomUserId"].ToString();
                bool IsAdminUser = (bool) Session["CustomIsAdminUser"];
                bool IsPublic = (bool) Session["CustomIsPublic"];

                IList<Flag> _flag = (IList<Flag>) _sqlHelper.GetFlag(FlagId);

                FlagCls[] _flagCls = new FlagCls[1];
                _flagCls[0] = new FlagCls();
                _flagCls[0].FlagId = _flag[0].FlagId;
                _flagCls[0].FlagName = _flag[0].FlagName;
                _flagCls[0].FlagDescription = _flag[0].FlagDescription;
                _flagCls[0].FlagKeyword = _flag[0].Keyword;
                _flagCls[0].IsPublic = _flag[0].IsPublic;
                _flagCls[0].IsFavorite = _flag[0].IsFavorite;
                //_flagCls[0].IsDeleted =(bool) _flag[0].IsDeleted;
                _flagCls[0].ConditionId = _flag[0].ConditionId;
                _flagCls[0].DataElementId = _flag[0].DataElementId;
                _flagCls[0].ValueSet1 = _flag[0].ValueSet1;
                _flagCls[0].ValueSet2 = _flag[0].ValueSet2;
                _flagCls[0].UserId = _flag[0].UserId;
                _flagCls[0].CreatedBy = _flag[0].CreatedBy;
                _flagCls[0].CreatedDate = _flag[0].CreatedDate;
                _flagCls[0].ModifiedBy = _user.ExternalId;
                _flagCls[0].ModifiedDate = DateTime.Now;

                _inBloomApi.UpateFlag(FlagId, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, FlagUserId, _flagCls, _user);

                if (_sqlHelper.IsFav(FlagId) || _sqlHelper.IsInPublicFavorite(FlagId, _user.ExternalId))
                {
                    btnlnkfavorite.Text = "Remove Favorite";
                    btnlnkfavorite.CssClass = "";
                    btnlnkfavorite.Attributes.Add("onclick", "showunfavmsg();");

                }
                else
                {
                    btnlnkfavorite.Text = "Add to Favorite";
                    btnlnkfavorite.CssClass = "unfavorite_result";
                    btnlnkfavorite.Attributes.Add("onclick", "showfavmsg();");

                }

            }
            else if (FlagType == "AggregateFlag")
            {
                try
                {
                    _sqlHelper.AggregatetFavoriteFlag(FlagId, _user.ExternalId);

                    _user = (User) Session["UserDetail"];

                    JArray _links = (JArray) Session["HomeLinks"];

                    String GetCustomLink = _inBloomApi.GetCustomLink(_links);
                    String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                    String FlagUserId = Session["CustomUserId"].ToString();
                    bool IsAdminUser = (bool) Session["CustomIsAdminUser"];
                    bool IsPublic = (bool) Session["CustomIsPublic"];

                    String UserId = _user.ExternalId;

                    AggregateCls[] _aggregateCls = new AggregateCls[1];

                    IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(FlagId);

                    _aggregateCls[0] = new AggregateCls();
                    _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                    _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                    _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                    _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;

                    _aggregateCls[0].IsPublic = _aggregateFlag[0].IsPublic;
                    _aggregateCls[0].IsFavorite = _aggregateFlag[0].IsFavorite;
                    _aggregateCls[0].UserId = _aggregateFlag[0].UserId;
                    _aggregateCls[0].CreatedBy = _aggregateFlag[0].CreatedBy;
                    _aggregateCls[0].ModifiedBy = _user.ExternalId;
                    _aggregateCls[0].CreatedDate = _aggregateFlag[0].CreatedDate;
                    _aggregateCls[0].ModifiedDate = DateTime.Now;

                    int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(FlagId);
                    FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];
                    for (int j = 0; j < flagId.Count(); j++)
                    {
                        _flagForAggregateList[j] = new FlagForAggregate();

                        _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                        _flagForAggregateList[j].FlagId = flagId[j];
                        _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                        _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                        _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                        _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;

                    }

                    _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                    _inBloomApi.UpateAggregateFlag(FlagId, GetCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _aggregateCls, _user);

                    if (_sqlHelper.IsAggregateIsFav(FlagId) || _sqlHelper.IsAggregateInPublicFavorite(FlagId, _user.ExternalId))
                    {
                        btnlnkfavorite.Text = "Remove Favorite";
                        btnlnkfavorite.CssClass = "";
                        btnlnkfavorite.Attributes.Add("onclick", "showunfavmsg();");

                    }
                    else
                    {
                        btnlnkfavorite.Text = "Add to Favorite";
                        btnlnkfavorite.CssClass = "unfavorite_result";
                        btnlnkfavorite.Attributes.Add("onclick", "showfavmsg();");

                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            UpdateMasterFlagList();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            try
            {
                _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());
            }
            catch (Exception Ex)
            {
                Session[SessionEnum.AccessToken.ToString()] = null;
                Response.Redirect("Search.aspx");

            }

            if (!IsPostBack)
            {
                try
                {

                    LoadDataDomainList();
                    LoadGridWithAttribute();
                    lstCondition.DataSource = _sDACEntities.Conditions;
                    lstCondition.DataBind();

                    DataTable dt = new DataTable();
                    dt.Columns.Add("student_Name");
                    dt.Columns.Add("GPA");

                    for (int i = 0; i < 5; i++)
                    {
                        dt.Rows.Add((new object[] { "", "" }));
                    }

                    gridDisplayResult.DataSource = dt;
                    gridDisplayResult.DataBind();

                    _user = (User)Session["UserDetail"];

                    if (_user.IsAdminUser)
                    {
                    }
                    else
                    {
                        PanelFlagType.Visible = false;
                    }

                }
                catch (Exception Ex)
                {
                }

            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                String AggregateFlagName = txtFlagName.Text.Trim();
                String AggregateDescription = txtDescription.Text.Trim();
                String AggregateKeyword = txtFlag.Text.Trim();

                bool FlagValue = false;
                bool IsAdded = false;

                if (Session["CopyAggregateFlag"] != null)
                {

                    int AggregateFlagId = Convert.ToInt16(Session["CopyAggregateFlag"].ToString());
                    int[] FlagId = new int[lstcategory.Items.Count];

                    for (int i = 0; i < lstcategory.Items.Count; i++)
                    {
                        String FlagName = lstcategory.Items[i].Text;
                        //FlagId[i] = _sqlHelper.GetFlagId(FlagName);
                        FlagId[i] = Convert.ToInt16(lstcategory.Items[i].Value);
                    }

                    _user = (User)Session["UserDetail"];

                    if (_user.IsAdminUser)
                    {
                        if (radioFlagType.SelectedItem.Text == "Public")
                        {
                            FlagValue = true;
                        }
                        else
                        {
                            FlagValue = false;
                        }
                    }

                    _user = (User)Session["UserDetail"];

                    String UserId = _user.ExternalId;

                    IsAdded = _sqlHelper.AddAggregateFlag(AggregateFlagName, AggregateDescription, AggregateKeyword, FlagValue, FlagId, UserId);

                    if (IsAdded)
                    {
                        JArray _links = (JArray)Session["HomeLinks"];

                        String GetCustomLink = _inBloomApi.GetCustomLink(_links);
                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                        AggregateCls[] _aggregateCls = new AggregateCls[1];
                        AggregateFlagId = _sqlHelper.GetAggregateFlagById(AggregateFlagName);

                        IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(AggregateFlagId);

                        _aggregateCls[0] = new AggregateCls();
                        _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                        _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                        _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                        _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;

                        _aggregateCls[0].IsPublic = false;
                        _aggregateCls[0].IsFavorite = false;
                        _aggregateCls[0].UserId = _user.ExternalId;
                        _aggregateCls[0].CreatedBy = _user.FullName;
                        _aggregateCls[0].ModifiedBy = _user.FullName;
                        _aggregateCls[0].CreatedDate = DateTime.Now;
                        _aggregateCls[0].ModifiedDate = DateTime.Now;

                        int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(_aggregateFlag[0].AggregateFlagId);
                        FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];
                        for (int j = 0; j < flagId.Count(); j++)
                        {
                            _flagForAggregateList[j] = new FlagForAggregate();

                            _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                            _flagForAggregateList[j].FlagId = flagId[j];
                            _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                            _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                            _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                            _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;

                        }

                        _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                        if (_user.IsAdminUser && FlagValue == true)
                        {
                            // process to store the public flag into organization
                            _aggregateCls[0].IsPublic = true;
                            _inBloomApi.AddAggregateFlagIntoEducationOrganization(_user, EducationOrganizationId, _aggregateCls);
                        }
                        else
                        {
                            // process to add flag
                            _inBloomApi.AddAggregateFlagsIntoCustom(GetCustomLink, _user, _aggregateCls);
                        }

                        Session.Add("Success", "Aggregate flag copied successfully.");
                        Session["CopyAggregateFlag"] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else
                    {
                        Session.Add("Success", "A flag with the name " + AggregateFlagName + " already exists.  Please enter a different name.");
                        Session["CopyAggregateFlag"] = AggregateFlagId;
                        Response.Redirect("CopyAggregateFlag.aspx");
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                 bool FlagValue = false;

                 _sqlHelper = new SqlHelper();

                if (Session["UserDetail"] != null)
                {
                    _user = (User)Session["UserDetail"];

                    if (_user.IsAdminUser)
                    {
                        if (radioFlagType.SelectedItem.Text == "Public")
                        {
                            FlagValue = true;
                        }
                        else
                        {
                            FlagValue = false;
                        }
                    }

                    String FlagKeyword = txtFlag.Text.Trim();
                    if (FlagKeyword.Equals("Enter one or more keywords for the flag (optional)"))
                    {
                        FlagKeyword = "";
                    }

                    bool IsFlagAdded = false;

                    if (Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"] != null)
                    {
                        IsFlagAdded = _sqlHelper.AddFlag(txtFlagName.Text.Trim(), txtDescription.Text.Trim(), FlagKeyword, Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]),
                            Convert.ToInt16(lstCondition.SelectedItem.Value), txtSetVal.Text.Trim(), txtSetVal2.Text.Trim(), _user.ExternalId, _user.FullName, FlagValue
                            );

                    }

                    if (IsFlagAdded)
                    {
                        Session.Add("Success", "Flag added successfully.");

                        int FlagId = _sqlHelper.GetFlagIdByNameDescriptionAndKeyword(txtFlagName.Text.Trim(), txtDescription.Text.Trim(), FlagKeyword);

                        JArray _links = (JArray)Session["HomeLinks"];

                        String GetCustomLink = _inBloomApi.GetCustomLink(_links);
                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                        FlagCls[] _flagCls = new FlagCls[1];
                        _flagCls[0] = new FlagCls();
                        _flagCls[0].FlagId = FlagId;
                        _flagCls[0].FlagName = txtFlagName.Text.Trim();
                        _flagCls[0].FlagDescription = txtDescription.Text.Trim();
                        _flagCls[0].FlagKeyword = FlagKeyword;
                        _flagCls[0].IsPublic = false;
                        _flagCls[0].IsFavorite = false;
                        _flagCls[0].IsDeleted = false;
                        _flagCls[0].ConditionId = Convert.ToInt16(lstCondition.SelectedItem.Value);
                        _flagCls[0].DataElementId = Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]);
                        _flagCls[0].ValueSet1 = txtSetVal.Text.Trim();
                        _flagCls[0].ValueSet2 = txtSetVal2.Text.Trim();
                        _flagCls[0].UserId = _user.ExternalId;
                        _flagCls[0].CreatedBy = _user.ExternalId;
                        _flagCls[0].CreatedDate = DateTime.Now;
                        _flagCls[0].ModifiedBy = _user.ExternalId;
                        _flagCls[0].ModifiedDate = DateTime.Now;

                        if (_user.IsAdminUser && FlagValue == true)
                        {
                            // process to store the public flag into organization
                            _flagCls[0].IsPublic = true;
                            _inBloomApi.AddFlagIntoEducationOrganization(_user, EducationOrganizationId, _flagCls);
                        }
                        else
                        {
                            // process to add flag
                            _inBloomApi.AddFlagsIntoCustom(GetCustomLink, _user, _flagCls);
                        }

                        Response.Redirect("Search.aspx");

                    }
                    else
                    {
                        //flag is already exist or some exception
                        Session.Add("Success", "A flag with the name " + txtFlagName.Text.ToString()  + " already exists. Please enter a different name.");
                        // Session.Add("Success", "Flag is name already exist");
                        Response.Redirect("AddFlag.aspx");

                    }
                }
                else
                {
                    Response.Redirect("Search.aspx");
                }
            }
            catch (Exception Ex)
            {

            }
        }
        public void GetCourseListNew()
        {
            try
            {
                _user = (User)Session["UserDetail"];
                DropDownListCourse.Items.Clear();
                ListItem[] _sectionList = null;
                if (_user.IsAdminUser || IsLeader())
                {
                    _sectionList = _inBloomApi.GetSectionByStaffIdAndSchoolId(dropDownListStaff.SelectedItem.Value, DropDownListSchool.SelectedItem.Value);
                }
                else
                {
                    _sectionList = _inBloomApi.GetSectionListForLoginUser();
                }
                ListItem[] _sectionAndCourseOfferingId = _inBloomApi.GetSectionAndCourseOfferingIdList();
                ListItem[] _temp = new ListItem[_sectionAndCourseOfferingId.Count()];

                ListItem[] _courseAndSectionIdTemp = new ListItem[_sectionAndCourseOfferingId.Count()];
                ListItem[] _sectionAndCourseIdTemp = new ListItem[_sectionAndCourseOfferingId.Count()];

                for (int i = 0; i < _sectionAndCourseOfferingId.Count(); i++)
                {
                    ListItem _course = _inBloomApi.GetCourseBySectionCourseOfferingId(_sectionAndCourseOfferingId[i].Value);
                    bool IsPresent = false;
                    for (int j = 0; j < _temp.Count(); j++)
                    {
                        if (_temp[j] != null)
                        {
                            if (_temp[j].Value == _course.Value)
                            {
                                IsPresent = true;
                                break;
                            }

                        }

                    }
                    if (!IsPresent)
                    {
                        _temp[i] = new ListItem(_course.Text, _course.Value);

                    }
                    _courseAndSectionIdTemp[i] = new ListItem(_course.Value, _sectionList[i].Value);
                    _sectionAndCourseIdTemp[i] = new ListItem(_sectionList[i].Value, _course.Value);
                }

                for (int i = 0; i < _temp.Count(); i++)
                {
                    if (_temp[i] != null)
                    {
                        DropDownListCourse.Items.Add(_temp[i]);
                    }
                }
                //DropDownListCourse.Items.AddRange(_temp);

                Course course = new Course(_temp);
                Session["Course"] = course;
                Session["CourseAndSectionIdTemp"] = _courseAndSectionIdTemp;
                Session["SectionAndCourseIdTemp"] = _sectionAndCourseIdTemp;

            }
            catch (Exception Ex)
            {
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool FlagValue = false;

                SqlHelper _sqlHelper = new SqlHelper();

                if (Session["UserDetail"] != null)
                {
                    _user = (User)Session["UserDetail"];

                    if (_user.IsAdminUser)
                    {
                        if (radioFlagType.SelectedItem.Text == "Public")
                        {
                            FlagValue = true;
                        }
                        else
                        {
                            FlagValue = false;
                        }
                    }

                    bool IsFlagAdded = false;
                    int FlagId = Convert.ToInt16(Session["EditFlag"]);
                    if (Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"] != null)
                    {
                        //int FlagId = Convert.ToInt16(Session["EditFlag"]);
                        IsFlagAdded = _sqlHelper.UpdateFlag(FlagId, txtFlagName.Text.Trim(), txtDescription.Text.Trim(), txtFlag.Text.Trim(), Convert.ToInt16(Request.Form["ctl00$ContentPlaceHolder1$hdnDataElementId"]),
                        Convert.ToInt16(lstCondition.SelectedItem.Value), txtSetVal.Text.Trim(), txtSetVal2.Text.Trim(), _user.ExternalId, _user.FullName, FlagValue
                        );

                    }
                    if (IsFlagAdded)
                    {
                        //successfully update

                        JArray _links = (JArray)Session["HomeLinks"];

                        String UserCustomLink = _inBloomApi.GetCustomLink(_links);
                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                        String FlagUserId = Session["CustomUserId"].ToString();
                        bool IsAdminUser = (bool)Session["CustomIsAdminUser"];
                        bool IsPublic = (bool)Session["CustomIsPublic"];

                        IList<Flag> _flag = (IList<Flag>)_sqlHelper.GetFlag(FlagId);

                        FlagCls[] _flagCls = new FlagCls[1];
                        _flagCls[0] = new FlagCls();
                        _flagCls[0].FlagId = _flag[0].FlagId;
                        _flagCls[0].FlagName = _flag[0].FlagName;
                        _flagCls[0].FlagDescription = _flag[0].FlagDescription;
                        _flagCls[0].FlagKeyword = _flag[0].Keyword;
                        _flagCls[0].IsPublic = _flag[0].IsPublic;
                        _flagCls[0].IsFavorite = _flag[0].IsFavorite;
                        //_flagCls[0].IsDeleted =(bool) _flag[0].IsDeleted;
                        _flagCls[0].ConditionId = _flag[0].ConditionId;
                        _flagCls[0].DataElementId = _flag[0].DataElementId;
                        _flagCls[0].ValueSet1 = _flag[0].ValueSet1;
                        _flagCls[0].ValueSet2 = _flag[0].ValueSet2;
                        _flagCls[0].UserId = _flag[0].UserId;
                        _flagCls[0].CreatedBy = _flag[0].CreatedBy;
                        _flagCls[0].CreatedDate = _flag[0].CreatedDate;
                        _flagCls[0].ModifiedBy = _user.ExternalId;
                        _flagCls[0].ModifiedDate = DateTime.Now;

                        _inBloomApi.UpateFlag(FlagId, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, FlagUserId, _flagCls, _user);

                        if (Session["EditFlag"] != null)
                        {
                            // flag is updated
                            Session["EditFlag"] = null;
                        }

                        Session.Add("Success", "Flag edited successfully.");
                        Session["FlagIdForEditPage"] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else
                    {
                        Session["Success"] = "A flag with the name " + txtFlagName.Text.ToString() + " already exists.  Please enter a different name.";
                        Session["EditFlag"] = Session["FlagIdForEditPage"].ToString();
                        Response.Redirect("EditFlag.aspx");
                    }
                }
                else
                {
                    Response.Redirect("Search.aspx");
                }
        }
        public void GetFlag()
        {
            try
            {

                _user = (User)Session["UserDetail"];
                ListItem[] _FlagList = _sqlHelper.GetFlagListByCategory(_user.ExternalId);

                String EducationOrganizationId = Session["EducationOrganizationId"].ToString();
                String[] AdminUserId = _inBloomApi.GetAdminIdByEducationOrganizationId(EducationOrganizationId);

                IList<Flag> _flagListTemp = null;
                IList<Flag> _flagListPublic = null;

                IList<AggregateFlag> _aggregateFlagListTemp = null;
                IList<AggregateFlag> _aggregateFlagListPublic = null;

                if (AdminUserId != null)
                {

                    for (int i = 0; i < AdminUserId.Count(); i++)
                    {
                        _flagListTemp = _sqlHelper.GetPublicFlagListForAdmin(AdminUserId[i]);
                        if (_flagListPublic == null)
                        {
                            _flagListPublic = _flagListTemp;
                        }
                        else
                        {
                            for (int j = 0; j < _flagListTemp.Count; j++)
                            {
                                _flagListPublic.Add(_flagListTemp[j]);
                            }

                        }
                    }

                    for (int i = 0; i < AdminUserId.Count(); i++)
                    {
                        _aggregateFlagListTemp = _sqlHelper.GetAllAggregatePublicFlagByUserId(AdminUserId[i]);
                        if (_aggregateFlagListPublic == null)
                        {
                            _aggregateFlagListPublic = _aggregateFlagListTemp;
                        }
                        else
                        {
                            for (int j = 0; j < _aggregateFlagListTemp.Count; j++)
                            {
                                _aggregateFlagListPublic.Add(_aggregateFlagListTemp[j]);
                            }
                        }

                    }

                }

                if (_FlagList != null)
                {
                    bool FavStart = false;
                    for (int Index = 0; Index < _FlagList.Length; Index++)
                    {
                        if (_FlagList[Index] == null)
                        {
                        }
                        else
                        {
                            if (_FlagList[Index].Text == "Public")
                            {

                                _FlagList[Index].Attributes.Add("class", "abc");
                                DropDownListFlag.Items.Add(_FlagList[Index]);

                                try
                                {
                                    for (int j = 0; j < _flagListPublic.Count(); j++)
                                    {
                                        DropDownListFlag.Items.Add(new ListItem(_flagListPublic[j].FlagName, _flagListPublic[j].FlagId + "Flag"));

                                    }

                                }
                                catch (Exception Ex)
                                {
                                }

                                try
                                {
                                    for (int j = 0; j < _aggregateFlagListPublic.Count(); j++)
                                    {
                                        DropDownListFlag.Items.Add(new ListItem(_aggregateFlagListPublic[j].AggregateFlagName, _aggregateFlagListPublic[j].AggregateFlagId + "_AggregateFlag"));

                                    }
                                }
                                catch (Exception Ex)
                                {
                                }

                            }
                            else
                            {
                                if (_FlagList[Index].Text == "Favorite" || _FlagList[Index].Text == "Recent Flag")
                                {

                                    _FlagList[Index].Attributes.Add("class", "abc");
                                    //_FlagList[Index].Attributes.CssStyle.Add("color", "black");
                                    //_FlagList[Index].Attributes.CssStyle.Add("font-weight", "bold");
                                    FavStart = true;
                                }
                                if (FavStart)
                                    DropDownListFlag.Items.Add(_FlagList[Index]);
                            }
                        }
                    }
                    //DropDownListFlag.Items.AddRange(_FlagList);
                }

            }
            catch (Exception Ex)
            {
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

            if (!IsPostBack)
            {
                _sqlHelper = new SqlHelper();
                LoadDataDomainList();
                LoadGridWithAttribute();
                lstCondition.DataSource = _sDACEntities.Conditions;
                lstCondition.DataBind();

                _user = (User)Session["UserDetail"];

                if (!_user.IsAdminUser)
                    PanelFlagType.Visible = false;

                if (Session["EditFlag"] != null)
                {
                    try
                    {
                        int FlagId = Convert.ToInt16(Session["EditFlag"]);
                        Session.Add("FlagIdForEditPage", FlagId);
                        _flagIdForPage = FlagId;

                        IList<Flag> _Flag = _sqlHelper.GetFlag(FlagId);

                        txtFlagName.Text = _Flag[0].FlagName;
                        txtDescription.Text = _Flag[0].FlagDescription;
                        txtFlag.Text = _Flag[0].Keyword;

                        String IsPublic = _Flag[0].IsPublic.ToString();

                        if (IsPublic == "True")
                        {
                            radioFlagType.SelectedIndex = 0;
                        }
                        else
                        {
                            radioFlagType.SelectedIndex = 1;
                        }

                        lstCondition.SelectedValue = _Flag[0].ConditionId.ToString();

                        if (_Flag[0].ConditionId.ToString() != null && _Flag[0].ConditionId.ToString() != "")
                        {
                            condition.Style.Add("display", "block");
                            condition.Text = lstCondition.SelectedItem.Text;
                        }

                        txtSetVal.Text = _Flag[0].ValueSet1;
                        txtSetVal2.Text = _Flag[0].ValueSet2;

                        if (_Flag[0].ValueSet1 != null && _Flag[0].ValueSet1 != "")
                        {
                            val1.Style.Add("display", "block");
                            val1.Text = _Flag[0].ValueSet1;
                        }

                        if (_Flag[0].ValueSet2 != null && _Flag[0].ValueSet2 != "")
                        {
                            val2.Style.Add("display", "block");
                            val2.Text = " & " + _Flag[0].ValueSet2;
                        }

                        _conditionId = _Flag[0].ConditionId;
                        if (condition.Text == "Is between" || condition.Text == "Is not between")
                        {
                            txtSetVal2.Style.Add("display", "block");
                        }

                        hdnDataElementId.Value = _Flag[0].DataElementId.ToString();

                        int DataElementId = _Flag[0].DataElementId;
                        hdnDataType.Value = _sqlHelper.GetDataTypeByDataElementId(DataElementId);

                        String EntityName = _sqlHelper.GetDataDomainNameByDataElementId(DataElementId);

                        // LoadEntityList();
                        LoadDataDomainList();
                        DropDownListEntity.SelectedItem.Text = EntityName;
                        LoadGridWithAttribute();

                        SelectRow(_Flag[0].DataElementId);

                        // process to call run flag

                        String Entity = _sqlHelper.GetEntityNameByDataElementId(DataElementId);
                        String FieldName = _sqlHelper.GetFieldNameByDataElementId(DataElementId);
                        String DataType = _sqlHelper.GetDataTypeByDataElementId(DataElementId);
                        bool ResponseType = false;
                        _user = (User)Session["UserDetail"];
                        String UserId = _user.ExternalId;
                        String SchoolId = Session[SessionEnum.SchoolId.ToString()].ToString();
                        String CourseId = Session[SessionEnum.CourseId.ToString()].ToString();
                        String SectionId = Session[SessionEnum.SectionId.ToString()].ToString();
                        String Value1 = txtSetVal.Text;
                        String Value2 = txtSetVal2.Text;

                       // String ExternalField = "";
                        bool IsPreview = true;

                        DataTable dt = _inBloomApi.RunFlag(FieldName, DataType, ResponseType, UserId, SchoolId, CourseId, SectionId, _conditionId, Value1, Value2, Entity, IsPreview);

                        gridDisplayResult.DataSource = dt;
                        gridDisplayResult.DataBind();
                        gridDisplayResult.HeaderRow.Cells[1].Text =_inBloomApi.GetWellFormattedString(char.ToUpper(Entity[0]) + Entity.Substring(1))+"."+_inBloomApi.GetWellFormattedString(FieldName);
                        entity.Text = _inBloomApi.GetWellFormattedString(char.ToUpper(Entity[0]) + Entity.Substring(1)) + "." + _inBloomApi.GetWellFormattedString(FieldName); ;

                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    Response.Redirect("Search.aspx");
                }
            }
        }
        public void GetSectionListNew()
        {
            try
            {
                _user = (User)Session["UserDetail"];
                DropDownListSection.Items.Clear();
                if (_user.IsAdminUser || IsLeader() || IsEducator())
                {
                    // _courseAndSectionIdTemp course id and section id
                    ListItem[] _courseAndSectionIdTemp = (ListItem[])Session["CourseAndSectionIdTemp"];

                    // _sectionAndCourseIdTemp section id and course id
                    ListItem[] _sectionAndCourseIdTemp = (ListItem[])Session["SectionAndCourseIdTemp"];
                    ListItem[] _temp = new ListItem[_courseAndSectionIdTemp.Count()];

                    String selectedCourseId = DropDownListCourse.SelectedItem.Value;
                    int count = 0;
                    for (int i = 0; i < _sectionAndCourseIdTemp.Count(); i++)
                    {
                        if (selectedCourseId == _sectionAndCourseIdTemp[i].Value)
                        {
                            ListItem _section = _inBloomApi.GetSectionById(_sectionAndCourseIdTemp[i].Text);
                            _temp[count] = new ListItem(_section.Text, _section.Value);
                            DropDownListSection.Items.Add(_section);
                        }
                    }

                    Section section = new Section(_temp);
                    Session["Section"] = section;

                }

            }
            catch (Exception Ex)
            {

            }
        }
        protected void gridViewFlag_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            #region Grid Operation Edit, Delete, Update, Favorite

            _slcApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

            if (e.CommandName == "CmdDelete")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int FlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _user = (User) Session["UserDetail"];
                    IList<Flag> _flag = (IList<Flag>) _sqlHelper.GetFlag(FlagID);

                    if (_user.ExternalId == _flag[0].UserId)
                    {

                        _sqlHelper.DeleteFlag(FlagID);

                        JArray _links = (JArray) Session["HomeLinks"];

                        String UserCustomLink = _slcApi.GetCustomLink(_links);
                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                        String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                        String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                        bool IsAdminUser = (AdminUser == "True");

                        String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                        bool IsPublic = (IsPublicFag == "True");

                        FlagCls[] _flagCls = new FlagCls[1];
                        _flagCls[0] = new FlagCls();
                        _flagCls[0].FlagId = _flag[0].FlagId;
                        _flagCls[0].FlagName = _flag[0].FlagName;
                        _flagCls[0].FlagDescription = _flag[0].FlagDescription;
                        _flagCls[0].FlagKeyword = _flag[0].Keyword;
                        _flagCls[0].IsPublic = _flag[0].IsPublic;
                        _flagCls[0].IsFavorite = _flag[0].IsFavorite;
                        _flagCls[0].IsDeleted = true;
                        _flagCls[0].ConditionId = _flag[0].ConditionId;
                        _flagCls[0].DataElementId = _flag[0].DataElementId;
                        _flagCls[0].ValueSet1 = _flag[0].ValueSet1;
                        _flagCls[0].ValueSet2 = _flag[0].ValueSet2;
                        _flagCls[0].UserId = _flag[0].UserId;
                        _flagCls[0].CreatedBy = _flag[0].CreatedBy;
                        _flagCls[0].CreatedDate = _flag[0].CreatedDate;
                        _flagCls[0].ModifiedBy = _user.ExternalId;
                        _flagCls[0].ModifiedDate = DateTime.Now;

                        _slcApi.UpateFlag(FlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _flagCls, _user);

                        LoadFlagGrid();
                    }
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdFavorite")
            {
                try
                {
                    _user = (User) Session["UserDetail"];

                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int FlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _sqlHelper.FavoriteFlag(FlagID, _user.ExternalId);
                    _user = (User) Session["UserDetail"];

                    JArray _links = (JArray) Session["HomeLinks"];

                    String UserCustomLink = _slcApi.GetCustomLink(_links);
                    String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = AdminUser == "True";

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = IsPublicFag == "True";

                    IList<Flag> _flag = _sqlHelper.GetFlag(FlagID);

                    FlagCls[] _flagCls = new FlagCls[1];
                    _flagCls[0] = new FlagCls();
                    _flagCls[0].FlagId = _flag[0].FlagId;
                    _flagCls[0].FlagName = _flag[0].FlagName;
                    _flagCls[0].FlagDescription = _flag[0].FlagDescription;
                    _flagCls[0].FlagKeyword = _flag[0].Keyword;
                    _flagCls[0].IsPublic = _flag[0].IsPublic;
                    _flagCls[0].IsFavorite = _flag[0].IsFavorite;
                    //_flagCls[0].IsDeleted =(bool) _flag[0].IsDeleted;
                    _flagCls[0].ConditionId = _flag[0].ConditionId;
                    _flagCls[0].DataElementId = _flag[0].DataElementId;
                    _flagCls[0].ValueSet1 = _flag[0].ValueSet1;
                    _flagCls[0].ValueSet2 = _flag[0].ValueSet2;
                    _flagCls[0].UserId = _flag[0].UserId;
                    _flagCls[0].CreatedBy = _flag[0].CreatedBy;
                    _flagCls[0].CreatedDate = _flag[0].CreatedDate;
                    _flagCls[0].ModifiedBy = _user.ExternalId;
                    _flagCls[0].ModifiedDate = DateTime.Now;

                    _slcApi.UpateFlag(FlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _flagCls, _user);

                    UpdateMasterFlagList();

                    LoadFlagGrid();

                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdCopyFlag")
            {
                try
                {
                    int FlagID = Int32.Parse(e.CommandArgument.ToString());
                    Session.Add("CopyFlag", FlagID);
                    Response.Redirect("CopyFlag.aspx");
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdEditFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int FlagId = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());
                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = (AdminUser == "True");

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = (IsPublicFag == "True");

                    String FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;

                    Session.Add("CustomUserId", UserId);
                    Session.Add("CustomIsPublic", IsPublic);
                    Session.Add("CustomFlagType", FlagType);
                    Session.Add("CustomIsAdminUser", IsAdminUser);
                    Session.Add("EditFlag", FlagId);
                    Response.Redirect("EditFlag.aspx");
                }
                catch (Exception Ex)
                {
                }
            }
            else if (e.CommandName == "CmdCopyAggregateFlag")
            {
                try
                {
                    int AggregateFlagID = Int32.Parse(e.CommandArgument.ToString());
                    Session.Add("CopyAggregateFlag", AggregateFlagID);
                    Response.Redirect("CopyAggregateFlag.aspx");
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdEditAggregateFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int AggregateFlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());
                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = (AdminUser == "True");

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = (IsPublicFag == "True");

                    String FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;

                    Session.Add("CustomUserId", UserId);
                    Session.Add("CustomIsPublic", IsPublic);
                    Session.Add("CustomFlagType", FlagType);
                    Session.Add("CustomIsAdminUser", IsAdminUser);
                    Session.Add("EditAggregateFlag", AggregateFlagID);

                    Response.Redirect("EditAggregateFlag.aspx");
                }
                catch (Exception Ex)
                {
                }
            }
            else if (e.CommandName == "CmdFavoriteAggregateFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int AggregateFlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _user = (User) Session["UserDetail"];
                    _sqlHelper.AggregatetFavoriteFlag(AggregateFlagID, _user.ExternalId);

                    JArray _links = (JArray) Session["HomeLinks"];

                    String UserCustomLink = _slcApi.GetCustomLink(_links);
                    String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = (AdminUser == "True");

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = (IsPublicFag == "True");

                    AggregateCls[] _aggregateCls = new AggregateCls[1];

                    IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(AggregateFlagID);

                    _aggregateCls[0] = new AggregateCls();
                    _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                    _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                    _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                    _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;

                    _aggregateCls[0].IsPublic = _aggregateFlag[0].IsPublic;
                    _aggregateCls[0].IsFavorite = _aggregateFlag[0].IsFavorite;
                    _aggregateCls[0].UserId = _aggregateFlag[0].UserId;
                    _aggregateCls[0].CreatedBy = _aggregateFlag[0].CreatedBy;
                    _aggregateCls[0].ModifiedBy = _user.ExternalId;
                    _aggregateCls[0].CreatedDate = _aggregateFlag[0].CreatedDate;
                    _aggregateCls[0].ModifiedDate = DateTime.Now;

                    int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(AggregateFlagID);
                    FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];
                    for (int j = 0; j < flagId.Count(); j++)
                    {
                        _flagForAggregateList[j] = new FlagForAggregate();

                        _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                        _flagForAggregateList[j].FlagId = flagId[j];
                        _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                        _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                        _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                        _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;

                    }

                    _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                    _slcApi.UpateAggregateFlag(AggregateFlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _aggregateCls, _user);

                    UpdateMasterFlagList();

                    LoadFlagGrid();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else if (e.CommandName == "CmdDeleteAggregateFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int AggregateFlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _user = (User) Session["UserDetail"];

                    AggregateCls[] _aggregateCls = new AggregateCls[1];

                    IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(AggregateFlagID);

                    if (_user.ExternalId == _aggregateFlag[0].UserId)
                    {
                        if (_sqlHelper.DeleteAggregateFlag(AggregateFlagID))
                        {
                            // deleted successfully

                            _user = (User) Session["UserDetail"];

                            JArray _links = (JArray) Session["HomeLinks"];

                            String UserCustomLink = _slcApi.GetCustomLink(_links);
                            String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                            String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                            String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                            bool IsAdminUser = (AdminUser == "True");

                            String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                            bool IsPublic = (IsPublicFag == "True");

                            _aggregateCls[0] = new AggregateCls();
                            _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                            _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                            _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                            _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;
                            _aggregateCls[0].IsDeleted = true;
                            _aggregateCls[0].IsPublic = _aggregateFlag[0].IsPublic;
                            _aggregateCls[0].IsFavorite = _aggregateFlag[0].IsFavorite;
                            _aggregateCls[0].UserId = _aggregateFlag[0].UserId;
                            _aggregateCls[0].CreatedBy = _aggregateFlag[0].CreatedBy;
                            _aggregateCls[0].ModifiedBy = _user.ExternalId;
                            _aggregateCls[0].CreatedDate = _aggregateFlag[0].CreatedDate;
                            _aggregateCls[0].ModifiedDate = DateTime.Now;

                            int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(AggregateFlagID);
                            FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];

                            for (int j = 0; j < flagId.Count(); j++)
                            {
                                _flagForAggregateList[j] = new FlagForAggregate();

                                _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                                _flagForAggregateList[j].FlagId = flagId[j];
                                _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                                _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                                _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                                _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;
                            }

                            _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                            _slcApi.UpateAggregateFlag(AggregateFlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _aggregateCls, _user);

                            LoadFlagGrid();
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdRunFlag")
            {
                //Session.Add("ResultPage", true);
                int RowIndex = Convert.ToInt16(e.CommandArgument);
                int FlagId = Convert.ToInt16(gridViewFlag.Rows[RowIndex].Cells[8].Text);
                String FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;
                Session.Add("FlagId", FlagId);
                Session.Add("FlagType", FlagType);

                String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                bool IsAdminUser = (AdminUser == "True");

                String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                bool IsPublic = (IsPublicFag == "True");

                FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;

                Session.Add("CustomUserId", UserId);
                Session.Add("CustomIsPublic", IsPublic);
                Session.Add("CustomFlagType", FlagType);
                Session.Add("CustomIsAdminUser", IsAdminUser);
                Session.Add("EditFlag", FlagId);

                Response.Redirect("Result.aspx");
            }

            #endregion
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session[SessionEnum.AccessToken.ToString()] != null)
                {
                    _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());
                    _sqlHelper = new SqlHelper();

                    _user = (User)Session["UserDetail"];
                    //Label lbl = (Label)Master.FindControl("lblUser");
                    lblUser.Text = _user.FullName;

                    _user = _inBloomApi.UserDetails();

                    if (_user == null)
                        Response.Redirect("Search.aspx");
                    else
                        if (Session["UserDetail"] == null)
                        {
                            Session.Add("UserDetail", _user);
                        }

                    if (!IsPostBack)
                    {
                        DropDownListSection.Items.Clear();
                        GetFlag();

                        if (Session["FlagId"] == null && Session["FlagType"] == null)
                        {
                        }
                        else
                        {
                            String SelectFlag = Session["FlagId"].ToString() + "_" + Session["FlagType"].ToString();
                            DropDownListSection.SelectedValue = SelectFlag;
                        }
                    }

                    if (Session["Success"] != null)
                    {
                        //lblSuccess.Text = Session["Success"].ToString();
                        hdnFieldMessage.Value = Session["Success"].ToString();
                        Session["Success"] = null;

                    }
                    else
                    {
                        hdnFieldMessage.Value = "";
                    }

                    if (!IsPostBack)
                    {

                        if (_user.IsAdminUser || IsLeader())
                        {
                            GetDistrictList();
                        }
                        else
                        {
                            DropDownListDistrict.Visible = false;
                            LabelDistrict.Visible = false;
                        }

                        //|| IsLeader() need to add
                        if (IsEducator())
                        {
                            dropDownListStaff.Visible = false;
                            LabelStaff.Visible = false;
                        }

                        if (Session["School"] == null)
                        {
                            GetSchoolList();
                        }
                        else
                        {
                            try
                            {
                                DropDownListSchool.Items.Clear();
                                School school = (School)Session["School"];
                                DropDownListSchool.Items.AddRange(school.GetSchoolList());
                                DropDownListSchool.SelectedValue = Session[SessionEnum.SchoolId.ToString()].ToString();
                            }
                            catch (Exception Ex)
                            {
                            }
                        }

                        if (Session["Staff"] == null)
                        {
                            if (!IsEducator())
                                GetStaffList();
                        }
                        else
                            if (!IsEducator())
                            {
                                dropDownListStaff.Items.Clear();
                                Staff staff = (Staff)Session["Staff"];
                                dropDownListStaff.Items.AddRange(staff.GetStaffList());
                                dropDownListStaff.SelectedValue = Session[SessionEnum.StaffID.ToString()].ToString();
                            }

                        if (Session["Course"] == null)
                        {
                            GetCourseListNew();
                            //GetCourseList();
                        }
                        else
                        {
                            try
                            {
                                Course course = (Course)Session["Course"];
                                ListItem[] _courseList = course.GetCourseList();
                                //DropDownListCourse.Items.AddRange(course.GetCourseList());
                                for (int i = 0; i < _courseList.Count(); i++)
                                {
                                    if (_courseList[i] != null)
                                        DropDownListCourse.Items.Add(_courseList[i]);
                                }
                                DropDownListCourse.SelectedValue = Session[SessionEnum.CourseId.ToString()].ToString();
                            }
                            catch (Exception Ex)
                            {
                            }
                        }

                        if (Session["Section"] == null)
                        {
                            GetSectionListNew();
                            //GetSectionList();
                        }
                        else
                        {
                            try
                            {
                                Section section = (Section)Session["Section"];
                                ListItem[] _listSection = section.GetSectionList();
                                if (_listSection != null)
                                {
                                    for (int Index = 0; Index < _listSection.Length; Index++)
                                    {
                                        if (_listSection[Index] != null)
                                        {
                                            DropDownListSection.Items.Add(new ListItem(_listSection[Index].Text, _listSection[Index].Value.ToString()));
                                        }
                                    }

                                }

                                DropDownListSection.SelectedValue = Session[SessionEnum.SectionId.ToString()].ToString();
                            }
                            catch (Exception Ex)
                            {
                            }
                        }

                        try
                        {
                            Session.Add(SessionEnum.SchoolId.ToString(), DropDownListSchool.SelectedValue);
                            Session.Add(SessionEnum.CourseId.ToString(), DropDownListCourse.SelectedValue);
                            Session.Add(SessionEnum.SectionId.ToString(), DropDownListSection.SelectedValue);
                            Session.Add(SessionEnum.StaffID.ToString(), dropDownListStaff.SelectedValue);
                        }
                        catch (Exception Ex)
                        {
                        }

                    }

                }
                else
                {
                    Response.Redirect("Search.aspx");
                }
            }
            catch (Exception ex)
            {
                throw;
                //Response.Redirect("Search.aspx");
            }
        }
        public void AddAggregateFlagsIntoCustom(String Link, User _user, AggregateCls[] _aggregateCls)
        {
            AggregateCls[] _forNew = _aggregateCls;
            FlagCls[] _forFlagCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.AggregateFlagList = _aggregateCls;

                JObject UserCustomResponse = JObject.Parse(RestApiHelper.CallApiWithParameter(Link, this.AccessToken));
                if (UserCustomResponse != null)
                {
                    Temp _list = FlagObjectDeserialize(UserCustomResponse.ToString());
                    if (_list != null)
                    {
                        Temp _temp = new Temp();
                        _forFlagCls = _list.FlagList;
                        AggregateCls[] _flagClsNew = new AggregateCls[_list.AggregateFlagList.Count() + 1];

                        for (int i = 0; i < _list.AggregateFlagList.Count(); i++)
                        {
                            _flagClsNew[i] = new AggregateCls();
                            _flagClsNew[i] = _list.AggregateFlagList[i];
                        }
                        _flagClsNew[_list.AggregateFlagList.Count()] = new AggregateCls();
                        _flagClsNew[_list.AggregateFlagList.Count()] = _aggregateCls[0];

                        _temp.AggregateFlagList = _flagClsNew;
                        _temp.FlagList = _list.FlagList;
                        _temp.UserId = _user.ExternalId;
                        _temp.IsAdmin = _user.IsAdminUser;

                        String Result = FlagObjectToJson(_temp);

                        RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
                    }
                }

            }
            catch (Exception Ex)
            {
                // process to add first
                Temp _temp = new Temp();
                _temp.AggregateFlagList = _forNew;
                _temp.FlagList = _forFlagCls;
                _temp.UserId = _user.ExternalId;
                _temp.IsAdmin = _user.IsAdminUser;
                String Result = FlagObjectToJson(_temp);
                RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
            }
        }