//not using this funciton. for the latest client hospital code will be passed on instead of ref phy
 private bool SetReferringPhysician(DICOMStudyObject dicomStudy,StudyObject risStudy)
 {
     //As our system is not being used to entrer work lists we can not pick up Referrring Physicians from our own worklist
     //therrefore the worklist code has been commented for now and we just create a user
     bool isSet = false;
     /*WorkListObject workList = new WorkListObject();
     workList.WorkListId.Value = dicomStudy.AccessionNumber.Value;
     workList.Load(GenericDataMigrator.AdminUserId);
     if (workList.IsLoaded)
     {
         risStudy.ReferringPhysicianId.Value = workList.RequestingPhysicianId.Value;
         isSet = true;
     }
     else*/
     if(dicomStudy.ReferringPhysician.Value != null)
     {
         UserObject user = new UserObject();
         user.Name.Value = dicomStudy.ReferringPhysician.Value;
         user.Load();
         if (!user.IsLoaded)
         {
             user = DatabaseUtility.CreateUser((string)dicomStudy.ReferringPhysician.Value);
             UserRoleObject userRole = new UserRoleObject();
             userRole.UserId.Value = user.GetPrimaryKey().Value;
             userRole.RoleId.Value = Constants.Roles.ReferringPhysician;
             userRole.Save();
         }
         risStudy.ReferringPhysicianId.Value = user.UserId.Value;
         //risStudy.ReferringPhysician.Value = dicomStudy.ReferringPhysician.Value;
         isSet = true;
     }
     return isSet;
 }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ValidData())
        {
            UserObject user = new UserObject();
            user.Name.Value = tbName.Text;
            user.Password.Value = tbPassword.Text;
            user.LoginName.Value = tbLoginName.Text;
            user.IsActive.Value = cbActive.Checked;
            /*if (ddlClients.SelectedIndex > 0)
            {
                user.ClientId.Value = ddlClients.SelectedValue;
            }*/
            user.Answer.Value = tbAnswer.Text;
            user.SecretQuestionId.Value = SecretQuestions1.SecretQuestionId;
            user.ResetPassword.Value = cbResetPassword.Checked;
            /*if (ddlHospitals.SelectedIndex > 0)
            {
                user.HospitalId.Value = ddlHospitals.SelectedValue;
            }*/
            user.SendSMS.Value = cbSms.Checked;
            user.Mobile.Value = tbCellNumber.Text;
            if (ddlCarriers.SelectedValue != "0")
            {
                user.CarrierId.Value = ddlCarriers.SelectedValue;
            }

            user.Save(loggedInUserId);
            if (user.IsLoaded)
            {
                UserRoleObject userRole = new UserRoleObject();
                userRole.UserId.Value = user.UserId.Value;
                userRole.RoleId.Value = ddlRoles.SelectedValue;
                userRole.Save(loggedInUserId);
                UsersTableAdapters.tUsersTableAdapter userTA = new UsersTableAdapters.tUsersTableAdapter();
                userTA.AssignClientsToUser(int.Parse(user.UserId.Value.ToString()),loggedInUserId);
                /*
                UsersTableAdapters.tUsersTableAdapter userTA = new UsersTableAdapters.tUsersTableAdapter();
                if (user.HospitalId.Value != null)
                {
                    Nullable<int> userId = int.Parse(user.UserId.Value.ToString());
                    Nullable<int> hospitalId = int.Parse(user.HospitalId.Value.ToString());
                    userTA.AssignHospitalToUser(userId, hospitalId, false, loggedInUserId);
                }
                else
                {
                    userTA.AssignHospitalToUser((Nullable<int>)user.UserId.Value, null, false, loggedInUserId);
                }
                */
                Response.Redirect("~/Admin/AddUser.aspx?userId=" + user.UserId.Value);
            }

            SetInfoMessage("User saved successfully");
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (ValidData())
        {
            UserObject user = new UserObject();
            user.UserId.Value = Request["userId"];
            user.Load(loggedInUserId);
            if (user.IsLoaded)
            {
                user.LoginName.Value = tbLoginName.Text;
                /*if(ddlClients.SelectedIndex > 0)
                {
                    user.ClientId.Value = ddlClients.SelectedValue;
                }
                else
                {
                    user.ClientId.Value = null;
                }*/

                /*if (ddlHospitals.SelectedIndex > 0)
                {
                    user.HospitalId.Value = ddlHospitals.SelectedValue;
                }
                else
                {
                    user.HospitalId.Value = null;
                }
                */
                user.Name.Value = tbName.Text;
                user.IsActive.Value = cbActive.Checked;
                if (pnlChangePassword.Visible == true)
                {
                    user.Password.Value = tbPassword.Text;
                }
                user.Answer.Value = tbAnswer.Text;
                user.SecretQuestionId.Value = SecretQuestions1.SecretQuestionId;
                user.ResetPassword.Value = cbResetPassword.Checked;
                user.SendSMS.Value = cbSms.Checked;
                user.Mobile.Value = tbCellNumber.Text;
                if (ddlCarriers.SelectedValue != "0")
                {
                    user.CarrierId.Value = ddlCarriers.SelectedValue;
                }
                user.Update(loggedInUserId);
                if (user.IsLoaded)
                {
                    UserRoleObject userRole = new UserRoleObject();
                    userRole.UserId.Value = user.UserId.Value;
                    userRole.Load(loggedInUserId);
                    if (userRole.IsLoaded)
                    {
                        userRole.RoleId.Value = ddlRoles.SelectedValue;
                        userRole.Update(loggedInUserId);
                    }

                    //this is not good. we should remove

                    UsersTableAdapters.tUsersTableAdapter userTA = new UsersTableAdapters.tUsersTableAdapter();
                    /*if (user.HospitalId.Value != null)
                    {
                        userTA.AssignHospitalToUser(int.Parse(user.UserId.Value.ToString()), int.Parse(user.HospitalId.Value.ToString()), true, loggedInUserId);
                    }
                    else
                    {
                        userTA.AssignHospitalToUser(int.Parse(user.UserId.Value.ToString()), null, true, loggedInUserId);
                    }*/
                }
                SetInfoMessage("User updated successfully");
            }
        }
    }
    protected override void Page_Load_Extended(object sender, EventArgs e)
    {
        if (loggedInUserRoleId != Constants.Roles.ClientAdmin
            && loggedInUserRoleId != Constants.Roles.Admin
            && loggedInUserRoleId != Constants.Roles.HospitalAdmin)
        {
            PagesFactory.Transfer(PagesFactory.Pages.LoginPage);
        }
        if (IsPostBack == false)
        {
            ddlRoles.Items.Insert(0, new ListItem("[--Select--]", "0"));
            if (loggedInUserRoleId == Constants.Roles.ClientAdmin)
            {
                ddlRoles.Items.Add(new ListItem("My Technologist", Constants.Roles.ClientTechnologist.ToString()));
                ddlRoles.Items.Add(new ListItem("Hospital Administrator", Constants.Roles.HospitalAdmin.ToString()));
                ddlRoles.Items.Add(new ListItem("Referring Physician", Constants.Roles.ReferringPhysician.ToString()));
            }
            else if (loggedInUserRoleId == Constants.Roles.HospitalAdmin)
            {
                ddlRoles.Items.Add(new ListItem("Referring Physician", Constants.Roles.ReferringPhysician.ToString()));
                ddlRoles.Items.Add(new ListItem("Hospital Staff", Constants.Roles.HospitalStaff.ToString()));
            }
            else if (loggedInUserRoleId == Constants.Roles.Admin)
            {

                ddlRoles.Items.Add(new ListItem("Client Administrator", Constants.Roles.ClientAdmin.ToString()));
                ddlRoles.Items.Add(new ListItem("My Technologist", Constants.Roles.ClientTechnologist.ToString()));
                ddlRoles.Items.Add(new ListItem("Hospital Administrator", Constants.Roles.HospitalAdmin.ToString()));
                ddlRoles.Items.Add(new ListItem("Radiologist", Constants.Roles.Radiologist.ToString()));
                ddlRoles.Items.Add(new ListItem("Referring Physician", Constants.Roles.ReferringPhysician.ToString()));
            }
            if (Request["userId"] != null)
            {
                user = new UserObject();
                user.UserId.Value = Request["userId"];
                user.Load(loggedInUserId);
                if (user.IsLoaded)
                {
                    btnUpdate.Visible = true;
                    btnSave.Visible = false;
                    pnlChangePassword.Visible = false;
                    lbChangePassword.Visible = true;
                    pnlHospital.Visible = true;

                    tbName.Text = user.Name.Value.ToString();
                    tbLoginName.Text = user.LoginName.Value.ToString();
                    cbActive.Checked = (bool)user.IsActive.Value;

                    if (user.SecretQuestionId.Value != null)
                    {
                        SecretQuestions1.SecretQuestionId = (int)user.SecretQuestionId.Value;
                    }
                    if (user.Answer.Value != null)
                    {
                        tbAnswer.Text = (string)user.Answer.Value;
                    }
                    if (user.ResetPassword.Value != null)
                    {
                        cbResetPassword.Checked = (bool)user.ResetPassword.Value;
                    }

                    UserRoleObject userRole = new UserRoleObject();
                    userRole.UserId.Value = user.UserId.Value;
                    userRole.Load(loggedInUserId);
                    if (userRole.IsLoaded)
                    {
                        ddlRoles.SelectedValue = userRole.RoleId.Value.ToString() ;
                        if (user.SendSMS.Value != null)
                        {
                            cbSms.Checked = (bool)user.SendSMS.Value;
                            if (cbSms.Checked)
                            {
                                rfvCarrier.Enabled = true;
                                rfvCellNumber.Enabled = true;
                            }
                        }
                        if (user.CarrierId.Value != null)
                        {
                            ddlCarriers.SelectedValue = user.CarrierId.Value.ToString();
                        }
                        if (user.Mobile.Value != null)
                        {
                            tbCellNumber.Text = (string)user.Mobile.Value;
                        }
                    }
                }
            }

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        UserObject user = new UserObject();
        if (tbPassword.Text.Trim().Equals(tbConfirmPassword.Text.Trim()) == false)
        {
            ((Main)this.Master).ErrorMessage = Messages.Error.PasswordsDoNotMatch;
            return;
        }
        if (lbUserId.Text.Length == 0)
        {
            user.LoginName.Value = tbLoginName.Text.Trim();
            user.Load();
            if (user.IsLoaded)
            {
                ((Main)this.Master).ErrorMessage = Messages.Error.LoginNameAlreadyExists;
                return;
            }
        }
        else
        {
            user.UserId.Value = int.Parse(lbUserId.Text.Trim());
            user.Load();
            if (user.IsLoaded == false)
            {
                Session[ParameterNames.Session.ExceptionString] = Messages.Exception.UserIdNotFound;
                PagesFactory.Transfer(PagesFactory.Pages.ErrorPage);
            }

        }
        user.Password.Value = tbPassword.Text.Trim();
        user.Name.Value = RISUtility.GetFullName(tbFirstName.Text.Trim(), tbLastName.Text.Trim());
        user.IsActive.Value = cbIsActive.Checked;
        //user.ClientId.Value = loggedInUserClientId;
        if (lbUserId.Text.Length == 0)
            user.Save();
        else
            user.Update((int)user.UserId.Value);

        foreach (ListItem item in lbUserRoles.Items)
        {
            UserRoleObject userRole = new UserRoleObject();
            userRole.UserId.Value = user.UserId.Value;
            userRole.RoleId.Value = int.Parse(item.Value);
            if (lbUserId.Text.Length > 0)
            {
                userRole.Load();
                if (userRole.IsLoaded == false)
                {
                    userRole.Save();
                }
            }
            else
            {
                userRole.Save();
            }
        }

        //if (lbUserId.Text.Length > 0)
        //{
        //    foreach (ListItem item in lbOtherRoles.Items)
        //    {
        //        UserRoleObject userRole = new UserRoleObject();
        //        userRole.UserId.Value = user.UserId.Value;
        //        userRole.RoleId.Value = int.Parse(item.Value);
        //        userRole.Load();
        //        if (userRole.IsLoaded == true)
        //        {
        //            userRole.Remove(0);
        //        }
        //    }
        //}
        if (lbUserId.Text.Length == 0)
        {
            ((Main)this.Master).InformationMessage = Messages.Information.DataSaved;
        }
        else
        {
            ((Main)this.Master).InformationMessage = Messages.Information.DataUpdated ;
        }
        lbUserId.Text = "";
        tbLoginName.Text = "";
        tbLoginName.Enabled = true;
        tbPassword.Text = "";
        tbConfirmPassword.Text = "";
        tbFirstName.Text = "";
        tbLastName.Text = "";
        lbOtherRoles.Items.Clear();
        lbUserRoles.Items.Clear();
        fillAllRolesList();
        //int count = lbUserRoles.Items.Count;
        //for(int i=0;i<count;i++)
        //    lbUserRoles.Items.RemoveAt(0);
        cbIsActive.Checked = false;
    }