static public AP_StaffBroker_Department CreateDept(string Name, string RC, int manager, int?del)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];
        if (del == 0)
        {
            del = null;
        }
        var q = from c in d.AP_StaffBroker_Departments where c.CostCentre == RC && c.PortalId == PS.PortalId select c;

        if (q.Count() > 0)
        {
            q.First().Name = Name;
            q.First().CostCentreManager  = manager;
            q.First().CostCentreDelegate = del;
            d.SubmitChanges();
            return(q.First());
        }
        else
        {
            AP_StaffBroker_Department insert = new AP_StaffBroker_Department();
            insert.PortalId          = PS.PortalId;
            insert.Name              = Name;
            insert.CostCentre        = RC;
            insert.CostCentreManager = manager;
            insert.CanRmb            = true;
            insert.CanGiveTo         = false;

            insert.CanCharge      = false;
            insert.IsProject      = false;
            insert.Spare1         = "False";
            insert.GivingText     = "";
            insert.GivingShortcut = "";
            insert.PayType        = "";


            insert.CostCentreDelegate = del;

            d.AP_StaffBroker_Departments.InsertOnSubmit(insert);
            d.SubmitChanges();
            return(insert);
        }
    }
    static public void AddProfileValue(int PortalId, int staffId, string propertyName, string propertyValue, int Type = 0)
    {
        StaffBrokerDataContext      d      = new StaffBrokerDataContext();
        AP_StaffBroker_StaffProfile insert = new AP_StaffBroker_StaffProfile();
        var definition = (from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PropertyName == propertyName && c.PortalId == PortalId select c.StaffPropertyDefinitionId);

        if (definition.Count() == 0)
        {
            AP_StaffBroker_StaffPropertyDefinition newDef = new AP_StaffBroker_StaffPropertyDefinition();
            newDef.PortalId     = PortalId;
            newDef.PropertyName = propertyName;
            newDef.Display      = false;
            newDef.ViewOrder    = (short?)((from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PortalId == PortalId select c.ViewOrder).Max() + 1);
            newDef.Type         = Convert.ToByte(Type);


            newDef.PropertyHelp = "";
            d.AP_StaffBroker_StaffPropertyDefinitions.InsertOnSubmit(newDef);
            d.SubmitChanges();
            insert.StaffPropertyDefinitionId = newDef.StaffPropertyDefinitionId;
        }
        else
        {
            insert.StaffPropertyDefinitionId = definition.First();
        }

        insert.StaffId       = staffId;
        insert.PropertyValue = propertyValue;
        var existing = from c in d.AP_StaffBroker_StaffProfiles where c.StaffId == insert.StaffId && c.StaffPropertyDefinitionId == insert.StaffPropertyDefinitionId select c;

        if (existing.Count() > 0)
        {
            existing.First().PropertyValue = propertyValue;
        }
        else
        {
            d.AP_StaffBroker_StaffProfiles.InsertOnSubmit(insert);
        }

        d.SubmitChanges();
    }
    static public AP_StaffBroker_Staff CreateStaffMember(int PortalId, DotNetNuke.Entities.Users.UserInfo User1in, DotNetNuke.Entities.Users.UserInfo User2in, short staffTypeIn)
    {
        //Create Married Staff


        DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
        if (rc.GetRoleByName(PortalId, "Staff") == null)
        {
            DotNetNuke.Security.Roles.RoleInfo insert = new DotNetNuke.Security.Roles.RoleInfo();
            insert.Description    = "Staff Members";
            insert.RoleName       = "Staff";
            insert.AutoAssignment = false;
            insert.IsPublic       = false;
            insert.RoleGroupID    = -1;
            insert.PortalID       = PortalId;
            rc.AddRole(insert);
        }

        rc.AddUserRole(PortalId, User1in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);
        rc.AddUserRole(PortalId, User2in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);



        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var searchStaff          = from c in d.AP_StaffBroker_Staffs where c.Active && (c.UserId1 == User1in.UserID || c.UserId2 == User1in.UserID || c.UserId1 == User2in.UserID || c.UserId2 == User2in.UserID) select c;

        if (searchStaff.Count() > 0)
        {
            return(searchStaff.First());
        }



        AP_StaffBroker_Staff rtn = new AP_StaffBroker_Staff();

        rtn.UserId1     = User1in.UserID;
        rtn.UserId2     = User2in.UserID;
        rtn.PortalId    = PortalId;
        rtn.Active      = true;
        rtn.DisplayName = User1in.FirstName + " & " + User2in.FirstName + " " + User1in.LastName;

        rtn.StaffTypeId = staffTypeIn;
        rtn.CostCenter  = "";

        d.AP_StaffBroker_Staffs.InsertOnSubmit(rtn);
        d.SubmitChanges();



        return(rtn);
    }
    static public void SetSetting(string SettingName, string value, int portalId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        var q = from c in d.AP_StaffBroker_Settings where c.SettingName == SettingName && c.PortalId == portalId select c;

        if (q.Count() == 0)
        {
            AP_StaffBroker_Setting insert = new AP_StaffBroker_Setting();
            insert.SettingName  = SettingName;
            insert.SettingValue = value;
            insert.PortalId     = portalId;
            d.AP_StaffBroker_Settings.InsertOnSubmit(insert);
        }
        else
        {
            q.First().SettingValue = value;
        }

        d.SubmitChanges();
    }
    public static void SetSetting(string SettingName, string value, int portalId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        var q = from c in d.AP_StaffBroker_Settings where c.SettingName == SettingName && c.PortalId == portalId select c;

        if (q.Count() == 0)
        {
            AP_StaffBroker_Setting insert = new AP_StaffBroker_Setting();
            insert.SettingName = SettingName;
            insert.SettingValue = value;
            insert.PortalId = portalId;
            d.AP_StaffBroker_Settings.InsertOnSubmit(insert);

        }
        else
        {
            q.First().SettingValue = value;
        }

        d.SubmitChanges();
    }
    public static AP_StaffBroker_Staff CreateStaffMember(int PortalId, DotNetNuke.Entities.Users.UserInfo User1in, string SpouseName, DateTime SpouseDOB, short staffTypeIn = 1)
    {
        DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
        if (rc.GetRoleByName(PortalId, "Staff") == null)
        {
            DotNetNuke.Security.Roles.RoleInfo insert = new DotNetNuke.Security.Roles.RoleInfo();
            insert.Description = "Staff Members";
            insert.RoleName = "Staff";
            insert.AutoAssignment = false;
            insert.IsPublic = false;
            insert.RoleGroupID = -1;
            insert.PortalID = PortalId;
            rc.AddRole(insert);
        }

        rc.AddUserRole(PortalId, User1in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);

        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var searchStaff = from c in d.AP_StaffBroker_Staffs where c.Active && (c.UserId1 == User1in.UserID || c.UserId2 == User1in.UserID) select c;
        if (searchStaff.Count() > 0)
            return searchStaff.First();
        //Create Married to Non-Staff
        AP_StaffBroker_Staff rtn = new AP_StaffBroker_Staff();
        rtn.UserId1 = User1in.UserID;
        rtn.UserId2 = -1;
        rtn.DisplayName = User1in.FirstName + " " + User1in.LastName;

        rtn.StaffTypeId = staffTypeIn;
        rtn.CostCenter = "";
        rtn.PortalId = PortalId;
        rtn.Active = true;
        d.AP_StaffBroker_Staffs.InsertOnSubmit(rtn);
        d.SubmitChanges();
        //Now add Spouse data
        AddProfileValue(PortalId, rtn.StaffId, "SpouseDOB", SpouseDOB.ToShortDateString());
        AddProfileValue(PortalId, rtn.StaffId, "SpouseName", SpouseName);

        return rtn;
    }
    public static AP_StaffBroker_Department CreateDept(string Name, string RC, int manager, int? del)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];
        if (del == 0) del = null;
        var q = from c in d.AP_StaffBroker_Departments where c.CostCentre == RC && c.PortalId == PS.PortalId select c;
        if (q.Count() > 0)
        {
            q.First().Name = Name;
            q.First().CostCentreManager = manager;
            q.First().CostCentreDelegate = del;
            d.SubmitChanges();
            return q.First();

        }
        else
        {
            AP_StaffBroker_Department insert = new AP_StaffBroker_Department();
            insert.PortalId = PS.PortalId;
            insert.Name = Name;
            insert.CostCentre = RC;
            insert.CostCentreManager = manager;
            insert.CanRmb = true;
            insert.CanGiveTo = false;

            insert.CanCharge = false;
            insert.IsProject = false;
            insert.Spare1 = "False";
            insert.GivingText = "";
            insert.GivingShortcut = "";
            insert.PayType = "";

            insert.CostCentreDelegate = del;

            d.AP_StaffBroker_Departments.InsertOnSubmit(insert);
            d.SubmitChanges();
            return insert;
        }
    }
    public static void AddProfileValue(int PortalId, int staffId, string propertyName, string propertyValue, int Type = 0)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        AP_StaffBroker_StaffProfile insert = new AP_StaffBroker_StaffProfile();
        var definition = (from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PropertyName == propertyName && c.PortalId == PortalId select c.StaffPropertyDefinitionId);
        if (definition.Count() == 0)
        {
            AP_StaffBroker_StaffPropertyDefinition newDef = new AP_StaffBroker_StaffPropertyDefinition();
            newDef.PortalId = PortalId;
            newDef.PropertyName = propertyName;
            newDef.Display = false;
            newDef.ViewOrder = (short?)((from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PortalId == PortalId select c.ViewOrder).Max() + 1);
            newDef.Type = Convert.ToByte(Type);

            newDef.PropertyHelp = "";
            d.AP_StaffBroker_StaffPropertyDefinitions.InsertOnSubmit(newDef);
            d.SubmitChanges();
            insert.StaffPropertyDefinitionId = newDef.StaffPropertyDefinitionId;
        }
        else
            insert.StaffPropertyDefinitionId = definition.First();

        insert.StaffId = staffId;
        insert.PropertyValue = propertyValue;
        var existing = from c in d.AP_StaffBroker_StaffProfiles where c.StaffId == insert.StaffId && c.StaffPropertyDefinitionId == insert.StaffPropertyDefinitionId select c;

        if (existing.Count() > 0)
            existing.First().PropertyValue = propertyValue;
        else
            d.AP_StaffBroker_StaffProfiles.InsertOnSubmit(insert);

        d.SubmitChanges();
    }