<?cs include:"util.cs" ?>
<?cs include:"grade_book_menu_util.cs" ?>

<?cs def:print_select_letter_grades(num, myletter) ?>
  <?cs set:aNum = num +#1 ?>
  <select name="letter_<?cs var:num ?>" tabindex="<?cs var:aNum ?>" id="letter_<?cs var:num ?>" title="<?cs var:lang.grade_book_enter_grades.select_letter_grades_help ?>" onchange="letterGradeChanged(<?cs var:num ?>)">
  <option value="-1"><?cs var:lang.grade_book_enter_grades.select_letter_grades ?></option>  
  <?cs each:this_letter = letter ?>
    <option value="<?cs var:this_letter.letter_id ?>" <?cs if:myletter == this_letter.letter_id ?> selected="selected" <?cs /if ?>><?cs var:this_letter.letter ?></option> 
   <?cs /each ?> 
  </select>
<?cs /def ?>

<?cs def:print_enter_grades_form() ?>
<div>
<?cs if:Query.save ?>
<div style="float:right">
<a href="javascript:dothis()" title="<?cs var:lang.grade_book_enter_grades.closewindow ?>" ><img src="<?cs var:image_path ?>close.gif" /></a> 
</div>
<?cs /if ?>
<br />
<br />

<fieldset style="width:50%">
<legend><b><?cs var:items[1].item_name ?></b></legend>
   <?cs if:items[1].is_letter_grade == "no" ?>
   <?cs var:lang.grade_book_enter_grades.points ?>: <?cs var:items[1].item_points ?>
   <?cs else ?>
   <?cs var:lang.grade_book_enter_grades.letter_grade ?>: <a href="javascript:openWin('grade_book_letter_grades?crs=<?cs var:Query.crs ?>&id=<?cs var:Query.id ?>&item_id=<?cs var:items[1].item_id ?>', 'letterGrade')" title="<?cs var:lang.grade_book_enter_grades.see_letter_grades ?>" ><?cs var:lang.grade_book_enter_grades.detail ?></a>
   <?cs /if ?>
   <br /> <?cs var:lang.grade_book_enter_grades.extra_credit ?> ?: <?cs var:items[1].extra_credit ?>
   <br /> <?cs var:lang.grade_book_enter_grades.date?>: <?cs var:items[1].due_date ?> <?cs if:items[1].due_date_hour < #10 ?>0<?cs /if ?><?cs var:items[1].due_date_hour ?>:<?cs if:items[1].due_date_minute < #10 ?>0<?cs /if ?><?cs var:items[1].due_date_minute ?> <?cs if:items[1].due_date_am == 0 ?><?cs var:lang.grade_book_enter_grades.date_am ?><?cs else ?><?cs var:lang.grade_book_enter_grades.date_pm ?><?cs /if ?>
</fieldset>
<br />

<form method="post" action="grade_book_enter_grades" onsubmit="return checkInput()">
<input type="hidden" name="crs" value="<?cs var:Query.crs ?>" />
<input type="hidden" name="id" value="<?cs var:Query.id ?>" />
<input type="hidden" name="item_id" value="<?cs var:Query.item_id ?>" />
<input type="hidden" name="number_students" value="<?cs var:number_students ?>" />

<table width="100%" cellpadding="4" cellspacing="0" >
  
 </tr>
 <tr class="headingcolor">
    <th align="left"><?cs var:lang.grade_book_enter_grades.id ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.name ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.score ?><?cs call:insert_help("gradebook_help.html", "GRADEBOOK_Score", lang.grade_book_enter_grades.help_button_title, "gradebook_help") ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.excuse ?><?cs call:insert_help("gradebook_help.html", "GRADEBOOK_Excuse", lang.grade_book_enter_grades.help_button_title, "gradebook_help") ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.pending ?><?cs call:insert_help("gradebook_help.html", "GRADEBOOK_Pending", lang.grade_book_enter_grades.help_button_title, "gradebook_help") ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.comments ?><?cs call:insert_help("gradebook_help.html", "GRADEBOOK_Comment", lang.grade_book_enter_grades.help_button_title, "gradebook_help") ?></th>
 </tr>
 <?cs set:num = #0 ?>
 <?cs each:student = std ?>
 <?cs if:num % 2 == #0 ?>
 <tr class="evencolor">
 <?cs else ?>
 <tr class="oddcolor">
 <?cs /if ?>
 <?cs set:tabOrder = num + #1 ?>
  <td><?cs var:student.id ?></td>
  <td nowrap="nowrap"><?cs var:student.realname ?>
      <input type="hidden" name="username_<?cs var:num ?>" value="<?cs var:student.username ?>"   />
  </td>
  <td>
     <?cs if:items[1].is_letter_grade == "yes" ?>
      <?cs call:print_select_letter_grades(num, score[student.username][Query.item_id].letter_id) ?>
     <?cs else ?>
     <span id="error_<?cs var:num ?>" style="visibility:hidden; color:#f00" >*</span>
     <input type="text" size="<?cs var:max_point_len ?>" maxlength="<?cs var:max_point_len ?>" tabindex="<?cs var:tabOrder ?>" name="score_<?cs var:num ?>" id="score_<?cs var:num ?>" <?cs if:score[student.username][Query.item_id].pending == "no" ?> value="<?cs var:score[student.username][Query.item_id].score ?>" <?cs /if ?> title="<?cs var:lang.grade_book_enter_grades.score_help ?>" onchange="scoreChanged(<?cs var:num ?>)" />/<?cs var:items[1].item_points ?> 
     <?cs /if ?>
     <?cs if:score[student.username][Query.item_id].pending == "no" ?>
     <a href="javascript:openWindow('grade_book_score_log?crs=<?cs var:Query.crs ?>&id=<?cs var:Query.id ?>&item_id=<?cs var:Query.item_id ?>&username=<?cs var:student.username ?>', 'logwin')" title="<?cs var:lang.grade_book_enter_grades.logs_help ?>" ><img src="<?cs var:image_path ?>history.gif" alt="" /></a>
     <?cs /if ?>
  </td>
  <td>
     <input type="checkbox" name="excuse_<?cs var:num ?>" id="excuse_<?cs var:num ?>" <?cs if:score[student.username][Query.item_id].excuse == "yes" ?> checked="checked"<?cs /if ?> value="1"  />
  </td>
  <td>
     <input type="checkbox" name="pending_<?cs var:num ?>" id="pending_<?cs var:num ?>" <?cs if:score[student.username][Query.item_id].pending == "yes" ?> checked="checked" <?cs elif:items[1].grade_entered == "no" ?> checked="checked" <?cs /if ?> value="1" onchange="pendingChanged(<?cs var:num ?>)" />
     <?cs if:items[1].grade_entered == "yes" ?> 
     <input type="hidden" name="old_pending_<?cs var:num ?>" id="old_pending_<?cs var:num ?>" value="<?cs var:score[student.username][Query.item_id].pending ?>" />
     <?cs /if ?>
  </td>
  <td>
    <textarea cols="35" rows="1" name="comments_<?cs var:num ?>" title="<?cs var:lang.grade_book_enter_grades.comments_help ?>" ><?cs var:score[student.username][Query.item_id].comment ?></textarea>
  </td>
 </tr>
 <?cs set:num = num + #1 ?>
 <?cs /each ?>
 <tr class="headingcolor"><td colspan="6" align="center">
   <input type="submit" name="oper" value="<?cs var:lang.grade_book_enter_grades.save ?>" />
 </tr>
</table>
</form>
</div>
<?cs /def ?>
		
<?cs def:print_grade_comment() ?>
<table width="100%" cellpadding="4" cellspacing="0" >
 <tr class="headingcolor">
    <th align="left"><?cs var:lang.grade_book_enter_grades.id ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.name ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.score ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.excuse ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.pending ?></th>
    <th align="left"><?cs var:lang.grade_book_enter_grades.comments ?></th>
 </tr>
 <tr>
    <td><?cs var:std[0].id ?></td>
    <td><?cs var:std[0].realname ?></td>
    <?cs if:score[std[0].username].score ?>
    <td><?cs var:score[std[0].username].score ?>/<?cs var:items[0].points ?></td>
    <?cs else ?>
    <td><?cs var:score[std[0].username].letter ?></td>
    <?cs /if ?>

    <td><?cs var:score[std[0].username].excuse ?></td>
    <td><?cs var:score[std[0].username].pending ?></td>
    <td><?cs var:score[std[0].username].comment ?></td>
 </tr>
<table> 
<?cs /def ?>


<?cs ###########################
     # HTML starts here
     ###########################
?>
<?cs call:html_heading() ?>
   <head>
      <title><?cs var:course_title ?> <?cs var:lang.grade_configure.scale_heading ?></title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta http-equiv="Cache-Control" content="No-Cache" />
    <meta http-equiv="EXPIRES" content="Mon, 22 Jul 1997 11:12:01 GMT" />
      <meta http-equiv="PRAGMA" content="No-Cache" />

   <link rel="StyleSheet" href="<?cs var:image_path ?>grade_tab_menu.css" type="text/css"  />

  <?cs if:!show_comment ?>
   <script type="text/javascript" src="<?cs var:image_path ?>form_validation.js">
   </script>
   <script type="text/javascript">
   function dothis()
   {
	      opener.parent.location.reload();
	        window.close();
   }
   function openWindow(url, winname)
   {
	var new_window = window.open(url, winname,'menubar=0,toolbar=0,status=0,scrollbars=yes,resizable=yes,width=600,height=400,screenX=150,screeny=200,left=150,top=200');
       new_window.focus();
   }
   function letterGradeChanged(num)
   {
      var name = "letter_" + num;
      var letter_value = get_selected_value(name);
      name = "pending_" + num;
      var pending = document.getElementById(name);
      if(letter_value != "-1")
	 pending.checked = false;
      else
	 pending.checked = true;
   }
   function pendingChanged(num)
   {
      var name = "pending_" + num;
      var pending = document.getElementById(name);
      name = "letter_" + num;
      var letter_obj = document.getElementById(name);
      name = "score_" + num;
      var score_obj = document.getElementById(name);
      name = "excuse_" + num;
      var excuse_obj = document.getElementById(name);


      if(pending.checked == false)
      {
	 if(letter_obj)
	     letter_obj.focus();
	 else
	 {
	     score_obj.value =0;
	     score_obj.focus();
	 }
      }

       
   }
   function scoreChanged(num)
   {
      var name = "score_" + num;
      var score_obj = document.getElementById(name);
      name = "pending_" + num;
      var pending = document.getElementById(name);

      if(score_obj && !is_whitespace(score_obj.value))
	pending.checked = false;
      else 
	pending.checked = true;
      
   }
   function excuseChanged(num)
   {
      var excuse = document.getElementById('excuse_' + num);
      var pending = document.getElementById('pending_' + num);
      if(excuse.checked == true)
	 pending.checked = false;
      else
	 pending.checked = true;

   }

   function  checkPointGrade(num)
   {
        var i =0;
	var count = 0;
	var errors = 0;
     
       
	for(i=0; i<num; i++)
	{
	    var pending = document.getElementById('pending_'+i);
	    var excuse = document.getElementById('excuse_' + i);
	    var score = document.getElementById('score_' + i);
	    var error = document.getElementById('error_' + i);
            if(pending.checked == false || excuse.checked == true)
	    {
	         count++;
	         if(excuse.checked != true)
		 {
	            if(is_whitespace(score.value) || is_real(score.value) == false)
		    {
			  error.style.visibility = "visible";
			    errors++;

		    }
		    else
			  error.style.visibility = "hidden";

		 }
	    }
            <?cs if:items[1].grade_entered == "yes" ?>
	     else
	     {
	          var old_pending = document.getElementById('old_pending_'+i).value;
		  if(old_pending == "yes" || old_pending == "no")
		       count++;
		 
	     }
	    <?cs /if ?>	    
	}
	if(count ==0)
        {
	    window.alert('<?cs var:lang.grade_book_enter_grades.no_grade_entered ?>');
	    return false;

	}
	if(errors >0)
	{
	    window.alert('<?cs var:lang.grade_book_enter_grades.error_msg ?>');
	    return false;
	
	}
   }

   function checkInput()
   {
      var num = <?cs var:number_students ?>;
      <?cs if:items[1].is_letter_grade == "no" ?>
	  return  checkPointGrade(num);
      <?cs else ?>
	  return  checkLetterGrade(num);
      <?cs /if ?>     
        
   }
   function openWin(url, winname)
   {
        var new_window = window.open(url, winname,'menubar=0,toolbar=0,status=0,scrollbars=yes,resizable=yes,width=650,height=400,screenX=150,screeny=200,left=150,top=200');
        new_window.focus();
  }

   </script>
   <?cs /if ?>
</head>


<?cs call:define_body() ?> 
<div id="container">
<?cs if:show_comment ?>
<?cs call:print_grade_comment() ?>
<?cs else ?>
<?cs call:print_enter_grades_form() ?>
<?cs /if ?>
</div>
</body>
</html>
<?cs call:runtime() ?>
