private void dgPermissions_CancelCommand(object source, DataGridCommandEventArgs e)
		{
			BPCompanyGroups bp = new BPCompanyGroups();
			DSCompanyGroups = bp.SelectCompanyGroupPermissionsByCompanyGroupID(CompanyGroupID);

			dgPermissions.EditItemIndex = -1;
			dgPermissions.SelectedIndex = -1;
			dgPermissions.DataBind();
		}
		private void dgPermissions_UpdateCommand(object source, DataGridCommandEventArgs e)
		{
			//Cell Index is the index of permissionID
			int CompanyPermissionID = Convert.ToInt32( e.Item.Cells[0].Text);

			BPCompanyGroups bp = new BPCompanyGroups();
			DSCompanyGroups = bp.SelectCompanyGroupPermissionsByCompanyGroupID(CompanyGroupID);

			BECompanyGroups.tbl_CompanyGroupPermissionsRow groupPermission = DSCompanyGroups.tbl_CompanyGroupPermissions.FindByCompanyGroupIDCompanyPermissionID(CompanyGroupID, CompanyPermissionID);

			DropDownList lstPermValues = (DropDownList) e.Item.FindControl("lstPermValues");
			groupPermission.CompanyGroupPermissionValue = Convert.ToInt16(lstPermValues.SelectedValue);

			bp.UpdateCompanyGroupPermissions(DSCompanyGroups);

			DSCompanyGroups = bp.SelectCompanyGroupPermissionsByCompanyGroupID(CompanyGroupID);

			dgPermissions.EditItemIndex = -1;
			dgPermissions.SelectedIndex = -1;
			dgPermissions.DataBind();
		}
		private void dgPermissions_ItemCommand(object source, DataGridCommandEventArgs e)
		{
			//if (this.OryxUser.RolePermission(Globals.PermissionLevels.ManageAdministratorGroups) > 1)
			//{
			BPCompanyGroups bp = new BPCompanyGroups();
			DSCompanyGroups = bp.SelectCompanyGroupPermissionsByCompanyGroupID(CompanyGroupID);
			
			if (e.Item.Cells[0].Text != string.Empty)
			{
				int permissionID = Convert.ToInt32( e.Item.Cells[0].Text);
				if (dgPermissions.EditItemIndex == e.Item.ItemIndex)
				{
					dgPermissions.EditItemIndex = -1;
				}
				else
				{
					dgPermissions.EditItemIndex = e.Item.ItemIndex;
					DSCompanyGroups.tbl_CompanyPermissionValues.DefaultView.RowFilter = string.Format("CompanyPermissionID = {0}", permissionID);
				}
			}

			dgPermissions.DataBind();
			//}
		}
		private void btnDone_Click(object sender, EventArgs e)
		{
			if (_HasChangedUsers )
			{
				BPCompanyGroups BPCompanyGroups = new BPCompanyGroups();
			
				if (_HasChangedUsers )
				{
					DSCompanyGroups = BPCompanyGroups.SelectCompanyGroupPermissionsByCompanyGroupID(CompanyGroupID);
					ArrayList perms = new ArrayList(PermList);
					perms.Sort();

					//Remove Duplicates
					if (perms.Count > 1)
					{
						int iLast,iCurrent;
						iLast = (int) perms[0];
						for (int i=1; i<perms.Count; i++)
						{
							iCurrent = (int) perms[i];

							if (iCurrent == iLast)
							{
								perms.RemoveAt(i);
							}
							iLast = iCurrent;
						}
					}

					int searchIndex;
					foreach (BECompanyGroups.tbl_CompanyGroupPermissionsRow groupPerm in DSCompanyGroups.tbl_CompanyGroupPermissions)
					{
						searchIndex = perms.BinarySearch(groupPerm.CompanyPermissionID);
						if (searchIndex < 0)
							//if (Array.BinarySearch(groupPerm.PermissionID))
						{
							//Not Found
							groupPerm.Delete();
						}
						else
						{
							perms.RemoveAt(searchIndex);
						}
					}

					foreach (int i in perms)
					{
						BECompanyGroups.tbl_CompanyGroupPermissionsRow groupPerm;
						groupPerm = DSCompanyGroups.tbl_CompanyGroupPermissions.FindByCompanyGroupIDCompanyPermissionID(CompanyGroupID, i);
						if(groupPerm == null)
						{
							groupPerm = DSCompanyGroups.tbl_CompanyGroupPermissions.Newtbl_CompanyGroupPermissionsRow();
							groupPerm.CompanyGroupID = CompanyGroupID;
							groupPerm.CompanyPermissionID = i;
							groupPerm.CompanyGroupPermissionValue = 2;
							DSCompanyGroups.tbl_CompanyGroupPermissions.Addtbl_CompanyGroupPermissionsRow(groupPerm);
						}

						//The first value for a permission should always be 1.
						groupPerm.CompanyGroupPermissionValue = 2;
					}
				}

				BPCompanyGroups.UpdateCompanyGroupPermissions(DSCompanyGroups);
				DSCompanyGroups = BPCompanyGroups.SelectCompanyGroupPermissionsByCompanyGroupID(CompanyGroupID);
				dgPermissions.DataBind();
			}
			pnlAssign.Visible = false;
			pnlPermissions.Visible = true;

			tab1.Attributes["class"] = "";
			tab2.Attributes["class"] = "current";
		}
		private void lnkPermissions_Click(object sender, EventArgs e)
		{
			pnlAssign.Visible = false;
			pnlPermissions.Visible = true;

			tab1.Attributes["class"] = "";
			tab2.Attributes["class"] = "current";

			BPCompanyGroups bp = new BPCompanyGroups();
			DSCompanyGroups = bp.SelectCompanyGroupsByID(CompanyGroupID);
			DSCompanyGroups = bp.SelectCompanyGroupPermissionsByCompanyGroupID(CompanyGroupID);

			dgPermissions.DataBind();
		}
		private void FillSelectedPerms()
		{
			lstSelectedPerms.Items.Clear();

			BPCompanyGroups bp = new BPCompanyGroups();
			BECompanyGroups ds = bp.SelectCompanyGroupPermissionsByCompanyGroupID(CompanyGroupID);

			MyListViewItem listItem;

			foreach (BECompanyGroups.tbl_CompanyGroupPermissionsRow groupPerm in ds.tbl_CompanyGroupPermissions)
			{
				listItem = lstSelectedPerms.Items.Add();
				listItem.CheckBox = true;
				listItem.ItemValue = groupPerm.CompanyPermissionID.ToString();
				listItem.Text = groupPerm.CompanyPermissionName;
			}
		}
		private void Page_Load(object sender, EventArgs e)
		{
			this.AuthenticatePage(new string[] {Globals.PermissionLevels.EditCompanyPermissionGroups, Globals.PermissionLevels.AddCompanyPermissionGroups, Globals.PermissionLevels.ViewCompanyPermissionGroups});
			if (CompanyGroupID > 0)
			{
				if (this.OryxUser.RolePermission(Globals.PermissionLevels.EditCompanyPermissionGroups) != 2)
				{
					btnSubmit.Visible = false;
					btnDelete.Visible = false;
					lnkAssign.Enabled = false;
					lnkAddUsers.Enabled = false;
				}
			}
			else
			{
				if (this.OryxUser.RolePermission(Globals.PermissionLevels.AddCompanyPermissionGroups) != 2)
				{
					btnSubmit.Visible = false;
					btnDelete.Visible = false;
					lnkAssign.Enabled = false;
					lnkAddUsers.Enabled = false;
				}
			}

			if (IsNewSession)
			{
				AddToListIfAdmin();
				IsNewSession = false;
			}

			if (hdnCompanyUserPermissions.Value == "hidden")
			{
				pnlCompanyUserPermissions.CssClass = "panel-closed";
				cntCompanyUserPermissions.Style.Add("display", "none");
				hdnCompanyUserPermissions.Value = "hidden";
			}
			else 
			{
				pnlCompanyUserPermissions.CssClass = "panel-open";
				cntCompanyUserPermissions.Style.Add("display", "");
				hdnCompanyUserPermissions.Value = "visible";
			}

			if (hdnCompanyPermissionUsers.Value == "hidden")
			{
				pnlCompanyPermissionUsers.CssClass = "panel-closed";
				cntCompanyPermissionUsers.Style.Add("display", "none");
				hdnCompanyPermissionUsers.Value = "hidden";
			}
			else 
			{
				pnlCompanyPermissionUsers.CssClass = "panel-open";
				cntCompanyPermissionUsers.Style.Add("display", "");
				hdnCompanyPermissionUsers.Value = "visible";
			}
			
			if (!Page.IsPostBack)
			{
				BPCompanyGroups bp = new BPCompanyGroups();

				if (CompanyGroupID != 0)
				{
					BECompanyGroups.tbl_CompanyGroupsRow group;

					DSCompanyGroups = bp.SelectCompanyGroupsByID(CompanyGroupID);
					group = DSCompanyGroups.tbl_CompanyGroups.FindByCompanyGroupID(CompanyGroupID);
					
					txtGroupName.Text = group.CompanyGroupName;
					txtGroupDescription.Text = group.CompanyGroupDescription;
					chkActive.Checked = group.CompanyGroupActive;

					DSCompanyGroups = bp.SelectCompanyGroupPermissionsByCompanyGroupID(CompanyGroupID);

					dgPermissions.DataBind();

					ShowAssignPerms();

					ShowGroupUsers();

					BindGroupsList();
				}
				else
				{
					lblTitle.Text = "Add Company Permission Group";
					btnDelete.Visible = false;
					btnSubmit.Text = "Next >";

					//DSCompanyGroups = bp.SelectAllGroupTypes();

					pnlCompanyUserPermissions.Visible = false;
					pnlCompanyPermissionUsers.Visible = false;
					pnlAreaTabs.Visible = false;
					pnlUserTabs.Visible = false;
				}
				//BindGroupsList();
			}
		}