function spa_forums_forums_main()
{
    # has SP just been installed?
    if (isset($_POST['install'])) {
        $site = SFHOMEURL . 'index.php?sp_ahah=troubleshooting&sfnonce=' . wp_create_nonce('forum-ahah') . '&install=1';
        $target = 'sfmaincontainer';
        ?>
		<script type='text/javascript'>
		jQuery(document).ready(function() {
			spjTroubleshooting("<?php 
        echo $site;
        ?>
", "<?php 
        echo $target;
        ?>
");
		});
		</script>
		<?php 
    }
    $groups = spdb_table(SFGROUPS, '', '', 'group_seq');
    if ($groups) {
        foreach ($groups as $group) {
            if (empty($group->group_icon)) {
                $icon = SPTHEMEICONSURL . 'sp_GroupIcon.png';
            } else {
                $icon = esc_url(SFCUSTOMURL . $group->group_icon);
                if (!file_exists(SFCUSTOMDIR . $group->group_icon)) {
                    $icon = SPTHEMEICONSURL . 'sp_GroupIcon.png';
                }
            }
            # Group
            ?>
			<table class="wp-list-table widefat">
				<tr>
					<th style="text-align:center;width:2%" scope="col"><?php 
            spa_etext('ID');
            ?>
</th>
					<th style="text-align:center;width:5%" scope="col"><?php 
            spa_etext('Icon');
            ?>
</th>
					<th style="text-align:center;" scope="col"><?php 
            spa_etext('Forum Group');
            ?>
</th>
				</tr>
			</table>
			<table class="wp-list-table widefat">
				<tr id="grouprow-<?php 
            echo $group->group_id;
            ?>
">
					<td style="text-align:center;width:2%"><?php 
            echo $group->group_id;
            ?>
</td>
					<td style="text-align:center;padding:8px 0px;width:5%"><?php 
            echo '<img src="' . $icon . '" alt="" title="' . spa_text('Current group icon') . '" />';
            ?>
</td>
					<td>
						<div class="sp-half-row-left">
							<div class='row-title'><strong><?php 
            echo sp_filter_title_display($group->group_name);
            ?>
</strong></div><div><?php 
            echo sp_filter_text_display($group->group_desc);
            ?>
</div>
<?php 
            sp_display_item_stats(SFFORUMS, 'group_id', $group->group_id, spa_text('Forums'));
            ?>
						</div>
						<div class="sp-half-row-right">
<?php 
            $base = SFHOMEURL . 'index.php?sp_ahah=forums-loader&amp;sfnonce=' . wp_create_nonce('forum-ahah');
            $target = "group-{$group->group_id}";
            $image = SFADMINIMAGES;
            ?>
							<input type="button" class="button-secondary" value="<?php 
            echo sp_splice(spa_text('Add Permission'), 0);
            ?>
" onclick="spjLoadForm('groupperm', '<?php 
            echo $base;
            ?>
', '<?php 
            echo $target;
            ?>
', '<?php 
            echo $image;
            ?>
', '<?php 
            echo $group->group_id;
            ?>
');" />
							<input type="button" class="button-secondary" value="<?php 
            echo sp_splice(spa_text('Edit Group'), 0);
            ?>
" onclick="spjLoadForm('editgroup', '<?php 
            echo $base;
            ?>
', '<?php 
            echo $target;
            ?>
', '<?php 
            echo $image;
            ?>
', '<?php 
            echo $group->group_id;
            ?>
');" />
							<input type="button" class="button-secondary" value="<?php 
            echo sp_splice(spa_text('Delete Group'), 0);
            ?>
" onclick="spjLoadForm('deletegroup', '<?php 
            echo $base;
            ?>
', '<?php 
            echo $target;
            ?>
', '<?php 
            echo $image;
            ?>
', '<?php 
            echo $group->group_id;
            ?>
');" />
							<input type="button" class="button-secondary" value="<?php 
            echo sp_splice(spa_text('Order Forums'), 0);
            ?>
" onclick="spjLoadForm('ordering', '<?php 
            echo $base;
            ?>
', '<?php 
            echo $target;
            ?>
', '<?php 
            echo $image;
            ?>
', '<?php 
            echo $group->group_id;
            ?>
');" />

						</div>
					</td>
				</tr>

				<tr class="sfinline-form">  <!-- This row will hold ahah forms for the current group -->
				  	<td colspan="3" style="padding: 0 10px 0 0;">
						<div id="group-<?php 
            echo $group->group_id;
            ?>
">
						</div>
					</td>
				</tr>
			</table>
<?php 
            # Forums in group
            $forums = spa_get_forums_in_group($group->group_id);
            if ($forums) {
                # display the current forum information for each forum in table format
                ?>
				<table class="wp-list-table widefat">
					<tr>
						<th style="text-align:center;width:2%" scope="col"><?php 
                spa_etext('ID');
                ?>
</th>
						<th style="text-align:center;width:5%" scope="col"><?php 
                spa_etext('Icon');
                ?>
</th>
						<th style="text-align:center;" scope="col"><?php 
                echo spa_text('Forums in') . ' ' . $group->group_name;
                ?>
</th>
					</tr>
				</table>
				<table class="wp-list-table widefat">
<?php 
                spa_paint_group_forums($group->group_id, 0, '', 0);
                ?>
				</table>
				<br /><br />
<?php 
            } else {
                echo '<div class="sfempty">&nbsp;&nbsp;&nbsp;&nbsp;' . spa_text('There are no forums defined in this group') . '</div>';
            }
        }
    } else {
        echo '<div class="sfempty">&nbsp;&nbsp;&nbsp;&nbsp;' . spa_text('There are no groups defined') . '</div>';
    }
}
function spa_forums_forums_main()
{
    $groups = spdb_table(SFGROUPS, '', '', 'group_seq');
    if ($groups) {
        foreach ($groups as $group) {
            if (empty($group->group_icon)) {
                $icon = sp_find_icon(SPTHEMEICONSURL, 'sp_GroupIcon.png');
            } else {
                $icon = esc_url(SFCUSTOMURL . $group->group_icon);
                if (!file_exists(SFCUSTOMDIR . $group->group_icon)) {
                    $icon = sp_find_icon(SPTHEMEICONSURL, 'sp_GroupIcon.png');
                }
            }
            # Group
            ?>
			<table class="wp-list-table widefat">
				<tr>
					<th style="text-align:center;" width="2%" scope="col"><?php 
            spa_etext('ID');
            ?>
</th>
					<th style="text-align:center;" width="5%" scope="col"><?php 
            spa_etext('Icon');
            ?>
</th>
					<th style="text-align:center;" scope="col"><?php 
            spa_etext('Forum Group');
            ?>
</th>
				</tr>
			</table>
			<table class="wp-list-table widefat">
				<tr id="grouprow-<?php 
            echo $group->group_id;
            ?>
">
					<td style="text-align:center;" width="2%"><?php 
            echo $group->group_id;
            ?>
</td>
					<td style="text-align:center;padding:8px 0px;" width="5%"><img src="<?php 
            echo $icon;
            ?>
" alt="" title="" /></td>
					<td>
						<div class="sp-half-row-left">
							<span class='row-title'><strong><?php 
            echo sp_filter_title_display($group->group_name);
            ?>
</strong></span><span><br /><?php 
            echo sp_filter_text_display($group->group_desc);
            ?>
</span>
						</div>
						<div class="sp-half-row-right">
<?php 
            $base = SFHOMEURL . 'index.php?sp_ahah=forums-loader&amp;sfnonce=' . wp_create_nonce('forum-ahah');
            $target = "group-{$group->group_id}";
            $image = SFADMINIMAGES;
            ?>
							<input type="button" class="button-secondary" value="<?php 
            echo sp_splice(spa_text('Add Permission'), 0);
            ?>
" onclick="spjLoadForm('groupperm', '<?php 
            echo $base;
            ?>
', '<?php 
            echo $target;
            ?>
', '<?php 
            echo $image;
            ?>
', '<?php 
            echo $group->group_id;
            ?>
');" />
							<input type="button" class="button-secondary" value="<?php 
            echo sp_splice(spa_text('Edit Group'), 0);
            ?>
" onclick="spjLoadForm('editgroup', '<?php 
            echo $base;
            ?>
', '<?php 
            echo $target;
            ?>
', '<?php 
            echo $image;
            ?>
', '<?php 
            echo $group->group_id;
            ?>
');" />
							<input type="button" class="button-secondary" value="<?php 
            echo sp_splice(spa_text('Delete Group'), 0);
            ?>
" onclick="spjLoadForm('deletegroup', '<?php 
            echo $base;
            ?>
', '<?php 
            echo $target;
            ?>
', '<?php 
            echo $image;
            ?>
', '<?php 
            echo $group->group_id;
            ?>
');" />
							<input type="button" class="button-secondary" value="<?php 
            echo sp_splice(spa_text('Order Forums'), 0);
            ?>
" onclick="spjLoadForm('ordering', '<?php 
            echo $base;
            ?>
', '<?php 
            echo $target;
            ?>
', '<?php 
            echo $image;
            ?>
', '<?php 
            echo $group->group_id;
            ?>
');" />

						</div>
					</td>
				</tr>
				<tr class="sfinline-form">  <!-- This row will hold ahah forms for the current group -->
				  	<td colspan="3" style="padding: 0 10px 0 0;">
						<div id="group-<?php 
            echo $group->group_id;
            ?>
">
						</div>
					</td>
				</tr>
			</table>
<?php 
            # Forums in group
            $forums = spa_get_forums_in_group($group->group_id);
            if ($forums) {
                # display the current forum information for each forum in table format
                ?>
				<table class="wp-list-table widefat">
					<tr>
						<th style="text-align:center;" width="2%" scope="col"><?php 
                spa_etext('ID');
                ?>
</th>
						<th style="text-align:center;" width="5%" scope="col"><?php 
                spa_etext('Icon');
                ?>
</th>
						<th style="text-align:center;" scope="col"><?php 
                echo spa_text('Forums in') . ' ' . $group->group_name;
                ?>
</th>
					</tr>
				</table>
				<table class="wp-list-table widefat">
<?php 
                spa_paint_group_forums($group->group_id, 0, '', 0);
                ?>
				</table>
				<br /><br />
<?php 
            } else {
                echo '<div class="sfempty">&nbsp;&nbsp;&nbsp;&nbsp;' . spa_text('There are no forums defined in this group') . '</div>';
            }
        }
    } else {
        echo '<div class="sfempty">&nbsp;&nbsp;&nbsp;&nbsp;' . spa_text('There are no groups defined') . '</div>';
    }
}
function spa_forums_ordering_form($groupId = 0)
{
    $where = '';
    if ($groupId) {
        $where = "group_id={$groupId}";
    }
    $groups = spdb_table(SFGROUPS, $where, '', 'group_seq');
    ?>
<script type="text/javascript">
    jQuery(document).ready(function() {
    	<?php 
    if ($groupId != 0) {
        ?>
    	jQuery('#grouprow-<?php 
        echo $groupId;
        ?>
').addClass('inForm');
    	<?php 
    }
    ?>
    	jQuery('#groupList').nestedSortable({
    		handle: 'div',
    		items: 'li',
    		tolerance: 'pointer',
    		listType: 'ul',
    		protectRoot: true,
    		placeholder: 'sortable-placeholder',
    		forcePlaceholderSize: true,
    		helper: 'clone',
    		tabSize: 30,
    		maxLevels: 10,
            scroll: true,
            scrollSensitivity: 40,
            scrollSpeed: 5
    	});

    	jQuery('#sfforumorder').ajaxForm({
    		target: '#sfmsgspot',
    		beforeSubmit: function() {
    			jQuery('#sfmsgspot').show();
    			jQuery('#sfmsgspot').html(pWait);
    		},
    		success: function() {
    			jQuery('#sfmsgspot').hide();
    			<?php 
    if ($groupId == 0) {
        ?>
    			jQuery('#sfreloadfo').click();
    			<?php 
    } else {
        ?>
    			jQuery('#sfreloadfb').click();
    			<?php 
    }
    ?>
    			jQuery('#sfmsgspot').fadeIn();
    			jQuery('#sfmsgspot').fadeOut(6000);
    		},
    		beforeSerialize: function() {
    			jQuery("input#spForumsOrder").val(jQuery("#groupList").nestedSortable('serialize'));
    		}
    	});
    });
</script>
<?php 
    spa_paint_options_init();
    $ahahURL = SFHOMEURL . 'index.php?sp_ahah=forums-loader&amp;sfnonce=' . wp_create_nonce('forum-ahah') . '&amp;saveform=orderforum';
    ?>
	<form action="<?php 
    echo $ahahURL;
    ?>
" method="post" id="sfforumorder" name="sfforumorder">
<?php 
    echo sp_create_nonce('forum-adminform_forumorder');
    spa_paint_open_tab(spa_text('Forums') . ' - ' . spa_text('Group and Forum Ordering'), true);
    spa_paint_open_panel();
    spa_paint_open_fieldset(spa_text('Order Groups and Forums'), 'true', 'order-forums');
    ?>
				<input type="hidden" id="cgroup" name="cgroup" value="<?php 
    echo $groupId;
    ?>
" />
				<?php 
    echo '<p>' . spa_text('Here you can set the order of Groups, Forums and SubForums by dragging and dropping below. After ordering, push the save button.') . '</p>';
    if (!empty($groups)) {
        echo '<ul id="groupList" class="groupList menu">';
        foreach ($groups as $group) {
            echo "<li id='group-{$group->group_id}' class='menu-item-depth-0'>";
            echo "<div class='alt group-list menu-item'>";
            echo "<span class='item-name'>{$group->group_name}</span>";
            echo '</div>';
            # now output any forums in the group
            $allForums = spa_get_forums_in_group($group->group_id);
            $depth = 1;
            if (!empty($allForums)) {
                echo "<ul id='forumList-{$group->group_id}' class='forumList menu'>";
                foreach ($allForums as $thisForum) {
                    if ($thisForum->parent == 0) {
                        sp_paint_order_forum($thisForum, $allForums, $depth);
                    }
                }
                echo '</ul>';
            }
            echo '</li>';
        }
        echo '</ul>';
    }
    echo '<input type="text" class="inline_edit" size="70" id="spForumsOrder" name="spForumsOrder" />';
    spa_paint_close_fieldset();
    spa_paint_close_panel();
    spa_paint_close_container();
    ?>
		<div class="sfform-submit-bar">
		<input type="submit" class="button-primary" id="saveit" name="saveit" value="<?php 
    spa_etext('Save Ordering');
    ?>
" />
        <?php 
    if ($groupId) {
        ?>
		<input type="button" class="button-primary" onclick="javascript:jQuery('#group-<?php 
        echo $group->group_id;
        ?>
').html('');jQuery('#grouprow-<?php 
        echo $groupId;
        ?>
').removeClass('inForm');" id="sforder<?php 
        echo $group->group_id;
        ?>
" name="groupordercancel<?php 
        echo $group->group_id;
        ?>
" value="<?php 
        spa_etext('Cancel');
        ?>
" />
        <?php 
    }
    ?>

		</div>
	</form>
<?php 
    spa_paint_close_tab();
    ?>
	<div class="sfform-panel-spacer"></div>
<?php 
}
function spa_save_forums_delete_group()
{
    check_admin_referer('forum-adminform_groupdelete', 'forum-adminform_groupdelete');
    $group_id = sp_esc_int($_POST['group_id']);
    $cseq = sp_esc_int($_POST['cgroup_seq']);
    # remove permissions for each forum in group
    $forums = spa_get_forums_in_group($group_id);
    if ($forums) {
        foreach ($forums as $forum) {
            # remove permissions for this forum
            $perms = sp_get_forum_permissions($forum->forum_id);
            if ($perms) {
                foreach ($perms as $perm) {
                    spa_remove_permission_data($perm->permission_id);
                }
            }
        }
    }
    # reset auths and memberships for everyone
    sp_reset_memberships();
    sp_reset_auths();
    # select all the forums in the group
    $forums = spa_get_forums_in_group($group_id);
    # remove the topics and posts in each forum
    foreach ($forums as $forum) {
        # need to delete all topics in the forum using standard routine to clean up behind it
        $topics = spdb_table(SFTOPICS, "forum_id={$forum->forum_id}");
        if ($topics) {
            foreach ($topics as $topic) {
                sp_delete_topic($topic->topic_id, $forum->forum_id, false);
            }
        }
    }
    #now remove the forums themselves
    spdb_query('DELETE FROM ' . SFFORUMS . " WHERE group_id={$group_id}");
    # and finaly remove the group
    spdb_query('DELETE FROM ' . SFGROUPS . " WHERE group_id={$group_id}");
    # need to iterate through the groups
    $groups = spdb_table(SFGROUPS, '', '', 'group_seq');
    foreach ($groups as $group) {
        if ($group->group_seq > $cseq) {
            spa_bump_group_seq($group->group_id, $group->group_seq - 1);
        }
    }
    # remove the default permissions for the group being deleted
    spdb_query('DELETE FROM ' . SFDEFPERMISSIONS . " WHERE group_id={$group_id}");
    do_action('sph_forum_group_del', $group_id);
    # clear out group cache tpo enable change_user
    sp_flush_cache('group');
    $mess = spa_text('Group Deleted');
    return $mess;
}