<script type="text/javascript">
<!-- Begin
<?php 
    foreach ($listmembers as $groupid => $listmember) {
        echo "group$groupid = new Object();\n";
        $useridstring = "group$groupid.userid = new Array(";
        $usernamestring = "group$groupid.username = new Array(";
        $max = count($listmember);
        $count = 0;
        foreach ($listmember as $userid => $username) {
            $count++;
            $useridstring .= "\"$userid\"";
            $usernamestring .= '"'.addslashes($username).'"';
            if ($count < $max) {
                $useridstring .= ', ';
                $usernamestring .= ', ';
            }
        }
        $useridstring .= ");\n";
        $usernamestring .= ");\n";

        echo $useridstring;
        echo $usernamestring;
    }
?>

function updateGroup() {
    document.form1.groupid.value = document.form2.groups.value;
    document.form3.groupid.value = document.form2.groups.value;
}


function updateMembers(selectgroup) {
    eval('group=group'+selectgroup.value); 

    username = group.username;
    userid = group.userid;

    document.form3['members[]'].length = username.length;

    for (i=0;i<username.length;i++) {
        document.form3['members[]'].options[i].value = userid[i];
        document.form3['members[]'].options[i].text  = username[i];
    }

    updateGroup();
}

function userWindow(selectuser) {
    num = 0;
    for (var i=0; i<selectuser.options.length; i++) {
        if (selectuser.options[i].selected) {
            num++;
            user = selectuser.options[i].value;
            openpopup('/user/view.php?id='+user+'&course=<?php echo $courseid ?>','userinfo'+num,'','');
        }
    }
    return false;
}

function groupWindow(selectgroup) {
    num = 0;
    for (var i=0; i<selectgroup.options.length; i++) {
        if (selectgroup.options[i].selected) {
            num++;
            group = selectgroup.options[i].value;
            openpopup('/course/group.php?id=<?php echo $courseid ?>&group='+group,'groupinfo'+num,'menubar=0,directory=0,location=0,scrollbars,resizable,width=600,height=480','');
        }
    }
    return false;
}


// end hiding script -->
</script>


  <table cellspacing="0" cellpadding="10" align="center" class="generaltable generalbox">
    <tr>
      <th width="33%" align="center" class="header"><?php p($strgroupnonmembers) ?></td>
      <th width="33%" align="center" class="header"><?php p($strgroups) ?></td>
      <th width="33%" align="center" class="header"><?php p($strgroupmembersselected) ?></td>
    </tr>
    <tr align="center" valign="top">
      <td class="generalboxcontent"><p>
        <form name="form1" id="form1" method="post" action="groups.php">
          <input type="hidden" name="id" value="<?php p($course->id) ?>" />
          <input type="hidden" name="groupid" value="<?php p($selectedgroup) ?>" />
          <input type="hidden" name="sesskey" value="<?php p($sesskey) ?>">
          <select name="nonmembers[]" size="15" multiple="multiple">
            <?php 
                if (!empty($nonmembers)) {
                    foreach ($nonmembers as $id => $nonmembername) {
                        echo "<option value=\"$id\">$nonmembername</option>";
                    }
                }
            ?>
          </select>
          </p>
          <p>
            <input type="submit" name="nonmembersadd" value="<?php p($strgroupaddusers) ?> -&gt;" 
                   onclick="updateGroup()" />
          </p>
          <!--<p>
            <input type="submit" name="nonmembersrandom" value="<?php p($strgrouprandomassign) ?> -&gt;" />
          </p> -->
          <p>
            <input type="submit" name="nonmembersinfo" value="<?php p($strgroupinfopeople) ?>" 
                   onclick="return userWindow(document.form1['nonmembers[]']);" />
          </p>
        </form>
      </td>
      <td class="generalboxcontent"><p>
        <form name="form2" id="form2" method="post" action="groups.php">
          <input type="hidden" name="id" value="<?php p($course->id) ?>" />
          <input type="hidden" name="sesskey" value="<?php p($sesskey) ?>">
          <select name="groups" size="15" onChange="updateMembers(this)">
            <?php 
                if (!empty($listgroups)) {
                    foreach ($listgroups as $id => $listgroup) {
                        $selected = '';
                        if ($id == $selectedgroup) {
                            $selected = 'selected="selected"';
                        }
                        echo "<option $selected value=\"$id\">$listgroup</option>";
                    }
                }
            ?>
          </select>
        </p>
        <p>
          <input type="submit" name="groupsinfo" value="<?php p($strgroupinfoedit) ?>" 
                 onclick="return groupWindow(document.form2.groups);"/>
        </p>
        <p>
          <input type="submit" name="groupsremove" value="<?php p($strgroupremove) ?>" />
        </p>
        <p>
          <input name="newgroupname" type="text" size="10" />
          <input type="submit" name="groupsadd" value="<?php p($strgroupadd) ?>" />
        </p>
        </form>
      </td>


      <td class="generalboxcontent"><p>
        <form name="form3" id="form3" method="post" action="groups.php">
          <input type="hidden" name="id" value="<?php p($course->id) ?>" />
          <input type="hidden" name="groupid" value="<?php p($selectedgroup) ?>" />
          <input type="hidden" name="sesskey" value="<?php p($sesskey) ?>">
          <select name="members[]" size="15" multiple="multiple">
            <?php 
                if (!empty($members)) {
                    foreach ($members as $id => $membername) {
                        echo "<option value=\"$id\">$membername</option>";
                    }
                }
            ?>
          </select>
        </p>
        <p>
          <input type="submit" name="membersinfo" value="<?php p($strgroupinfomembers) ?>"
                 onclick="return userWindow(document.form3['members[]']);" />
        </p>
        <p>
          <input type="submit" name="membersremove" value="<?php p($strgroupremovemembers) ?>" 
                 onclick="updateGroup()" />
        </p>
        </form>
      </td>
    </tr>
  </table>
