<script type="text/javascript">
//<![CDATA[ 

<?php
    // javascript objects for excluded students
    foreach ($listmembers as $grade_itemid => $listmember) {
        echo "grade_item$grade_itemid = new Object();\n";
        $useridstring = "grade_item$grade_itemid.userid = new Array(";
        $usernamestring = "grade_item$grade_itemid.username = new Array(";
        $max = count($listmember);
        $count = 0;
        foreach ($listmember as $userid => $username) {
            $count++;
            $useridstring .= "\"$userid\"";
            $usernamestring .= "\"$username\"";
            if ($count < $max) {
                $useridstring .= ', ';
                $usernamestring .= ', ';
            }
        }
        $useridstring .= ");\n";
        $usernamestring .= ");\n";

        echo $useridstring;
        echo $usernamestring;
    }

    // javascript objects for included students
    foreach ($nonmembers as $grade_itemid => $listmember) {
        echo "grade_item_inc$grade_itemid = new Object();\n";
        $useridstring = "grade_item_inc$grade_itemid.userid = new Array(";
        $usernamestring = "grade_item_inc$grade_itemid.username = new Array(";
        $max = count($listmember);
        $count = 0;
        foreach ($listmember as $userid => $username) {
            $count++;
            $useridstring .= "\"$userid\"";
            $usernamestring .= "\"$username\"";
            if ($count < $max) {
                $useridstring .= ', ';
                $usernamestring .= ', ';
            }
        }
        $useridstring .= ");\n";
        $usernamestring .= ");\n";

        echo $useridstring;
        echo $usernamestring;
    }
?>

function updategrade_item() {
    document.getElementById('form1').grade_itemid.value = document.getElementById('form2').grade_items.value;
    document.getElementById('form3').grade_itemid.value = document.getElementById('form2').grade_items.value;
}


function updateMembers(selectgrade_item) {
    eval('grade_item=grade_item'+selectgrade_item.value); 

    username = grade_item.username;
    userid = grade_item.userid;

    document.getElementById('form3')['members[]'].length = username.length;

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

    eval('grade_item_inc=grade_item_inc'+selectgrade_item.value); 

    username = grade_item_inc.username;
    userid = grade_item_inc.userid;

    document.getElementById('form1')['nonmembers[]'].length = username.length;

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

    updategrade_item();
}

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



<table border="0" cellspacing="2" cellpadding="5" align="center" class="generalbox">
    <tr>
        <td colspan="3" class="header">
        <?php 
            print get_string('gradeexceptions','grades');
            helpbutton('exceptions',get_string('gradeexceptionshelp','grades'), 'grade');
        ?>
        </td>
    </tr>
    <tr>
      <td width="33%" align="center" class="generaltableheader"><?php p($strgradeitemnonmembers) ?></td>
      <td width="33%" align="center" class="generaltableheader"><?php p($strgradeitems) ?></td>
      <td width="33%" align="center" class="generaltableheader"><?php p($strgradeitemmembersselected) ?></td>
    </tr>
    <tr align="center" valign="top">
      <td class="generalboxcontent">
        <form id="form1" method="post" action="exceptions.php">
          <input type="hidden" name="id" value="<?php p($course->id) ?>" />
          <input type="hidden" name="grade_itemid" value="<?php p($selectedgrade_item) ?>" />
          <input type="hidden" name="action" value="excepts" />
          <input type="hidden" name="sesskey" value="<?php p($USER->sesskey) ?>"/>
          <select name="nonmembers[]" size="15" multiple="multiple">
            <?php 
                if (!empty($nonmembers[$selectedgrade_item])) {
                    foreach ($nonmembers[$selectedgrade_item] as $id => $nonmembername) {
                        echo "<option value=\"$id\">$nonmembername</option>";
                    }
                }
            ?>
          </select>
          <p>
            <input type="submit" name="nonmembersadd" value="<?php p($strgradeitemaddusers) ?>" 
                   onclick="updategrade_item()" />
          </p>
        </form>
      </td>
      <td class="generalboxcontent">
        <form id="form2" method="post" action="grade_items.php">
          <input type="hidden" name="id" value="<?php p($course->id) ?>" />
          <input type="hidden" name="action" value="excepts" />
          <input type="hidden" name="sesskey" value="<?php p($USER->sesskey) ?>"/>
          <select name="grade_items" size="15" onchange="updateMembers(this)">
            <?php 
                if (!empty($listgrade_items)) {
                    foreach ($listgrade_items as $id => $listgrade_item) {
                        $selected = '';
                        if ($id == $selectedgrade_item) {
                            $selected = 'selected="selected"';
                        }
                        echo "<option $selected value=\"$id\">$listgrade_item</option>";
                    }
                }
            ?>
          </select>
        </form>
      </td>


      <td class="generalboxcontent">
        <form id="form3" method="post" action="exceptions.php">
          <input type="hidden" name="id" value="<?php p($course->id) ?>" />
          <input type="hidden" name="grade_itemid" value="<?php p($selectedgrade_item) ?>" />
          <input type="hidden" name="sesskey" value="<?php p($USER->sesskey) ?>"/>
          <input type="hidden" name="action" value="excepts" />
          <select name="members[]" size="15" multiple="multiple">
            <?php 
                if (!empty($listmembers[$selectedgrade_item])) {
                    foreach ($listmembers[$selectedgrade_item] as $id => $membername) {
                        echo "<option value=\"$id\">$membername</option>";
                    }
                }
            ?>
          </select>
        <p>
          <input type="submit" name="membersremove" value="<?php p($strgradeitemremovemembers) ?>" 
                 onclick="updategrade_item()" />
        </p>
        </form>
      </td>
    </tr>
  </table>
<script type="text/javascript">
<!-- Begin
<?php
 echo 'updateMembers('.$selectedgrade_item.');';
 ?>
//]]>
</script>
