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

<?cs def:print_more_grades(number) ?>
<div id="more_letter_grades" style="display:none">
  <table width="100%" cellspacing="0" cellpadding="4">
  <tr class="headingcolor">
     <th align="left">
        <?cs var:lang.grade_book_items.letter ?>
     </th>
     <th align="left">
        <?cs var:lang.grade_book_items.points ?>
     </th>
  </tr>
  <?cs loop:x=number, number + #5, #1 ?>
    <?cs if:x %2 == #0 ?>
    <tr class="evencolor">
    <?cs else ?>
    <tr class="oddcolor">
    <?cs /if ?>
        <td>
	 
	 <span id="letter_error_<?cs var:x ?>" style="visibility:hidden; color:#f00">*</span>
	<input type="text" size="7" maxlength="<?cs var:max_letter_len ?>" name="letter_<?cs var:x ?>" id="letter_<?cs var:x ?>" /></td>
        <td>
	 <span id="points_error_<?cs var:x ?>" style="visibility:hidden; color:#f00">*</span>
	<input type="text" size="7" maxlength="<?cs var:max_point_len ?>" name="points_<?cs var:x ?>" id="points_<?cs var:x ?>" /></td>
    </tr>
  <?cs /loop ?>
  </table>
</div> 
<?cs /def ?>


<?cs def:print_letter_grades() ?>
<div id="container">
  <form method="post" action="grade_book_letter_grades" id="letter_grades_form" >
  <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:item_id ?>" />
  <input type="hidden" name="number_letter_grades" value="" />
  <?cs if:can_modify_letters ?>
  <input type="hidden" name="can_modify_letters" value="yes" />
  <?cs /if ?>

  <table width="100%" cellspacing="0" cellpadding="4">
  <tr class="headingcolor">
     <th align="left">
        <?cs var:lang.grade_book_items.letter ?>
     </th>
     <th align="left">
        <?cs var:lang.grade_book_items.points ?>
     </th>
  </tr>
  <?cs set:row_num = #0 ?>
  <?cs each:one_letter_grade = letter ?>
  <?cs if:row_num % 2 == #0 ?>
  <tr class="evencolor">
  <?cs else ?>
  <tr class="oddcolor">
  <?cs /if ?>
   <td>
      <input type="hidden" name="letter_old_<?cs var:row_num ?>"  id="letter_old_<?cs var:row_num ?>" value="<?cs var:one_letter_grade.letter ?>" />
      <input type="hidden" name="points_old_<?cs var:row_num ?>"  id="points_old_<?cs var:row_num ?>"  value="<?cs var:one_letter_grade.points ?>" />
      <input type="hidden" name="letter_id_<?cs var:row_num ?>"  id="letter_id_<?cs var:row_num ?>" value="<?cs var:one_letter_grade.letter_id ?>" />
      <?cs if:can_modify_letters ?>
      <span id="letter_error_<?cs var:row_num ?>" style="visibility:hidden; color:#f00">*</span>
      <input type="text" size="7" maxlength="<?cs var:max_letter_len ?>" name="letter_<?cs var:row_num ?>" id="letter_<?cs var:row_num ?>" value="<?cs var:one_letter_grade.letter ?>" />
      <?cs else ?>
       <?cs var:one_letter_grade.letter ?>
     <?cs /if ?>
   </td>
   <td>
      <span id="points_error_<?cs var:row_num ?>" style="visibility:hidden; color:#f00">*</span>
      <input type="text" size="7" maxlength="<?cs var:max_point_len ?>" name="points_<?cs var:row_num ?>" id="points_<?cs var:row_num ?>" value="<?cs var:one_letter_grade.points ?>" />
   </td>
  </tr> 

  <?cs set:row_num = row_num + #1 ?>
  <?cs /each ?>
  </table>
  <?cs call:print_more_grades(row_num) ?>

  <table width="100%" cellspacing="0" cellpadding="4">
  <tr class="headingcolor">
   <td colspan="2" align="center">
    <span id="more_button" style="visibility:visible"><input type="button" onclick="javascript:getMoreLetterGrades(); return false;" value="<?cs var:lang.grade_book_items.more ?>" title="<?cs var:lang.grade_book_items.more_help ?>" /></span>
    <input type="button" onclick="gotoNext(); return false;" value="<?cs var:lang.grade_book_items.update ?>" title="<?cs var:lang.grade_book_items.update_help ?>" />
   </td>
  </tr> 
 </table> 
 </form>
 </div>
<?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"  />
   <script type="text/javascript" src="<?cs var:image_path ?>form_validation.js">
   </script>
   <script type="text/javascript">
   function getMoreLetterGrades() 
   {
      var button = document.getElementById('more_button');
      button.style.visibility = "hidden";
      var moreLetter = document.getElementById('more_letter_grades');
      moreLetter.style.display = "block"; 
   }
   function checkLetterUnique(letters)
   {
       var i, j, len = letters.length;
       for(i =0; i<len; i++)
       {
          for(j = 0; j<len; j++)
          if( i != j)
          {
             if( letters[i] == letters[j])
                        return 0;
          }
      }
      return 1;
    }

   function checkAll(number)
   {
      var changes = 0;
      var errors = 0;
      var letters = new Array();
      var i =0;
      var count = 0;
      var name;

      for(i =0; i<= number; i++)
      {
	  name = "letter_old_" + i;
	  var old_letter = document.getElementById(name);
	  name = "points_old_" + i;
	  var old_points = document.getElementById(name);
	  name = "letter_" + i;
	  var letter = document.getElementById(name);
	  name = "points_" + i;
	  var points = document.getElementById(name).value;
	  name = "letter_id_" + i;
	  var letter_id = document.getElementById(name);
	      
          if(letter)
	  {
	   
	      var letter_value = letter.value;
	      if(letter_id)
	      {
		  if(is_whitespace(letter_value) || is_whitespace(points))
			  changes++;
	      }

	      if(!is_whitespace(letter_value) && !is_whitespace(points))
	      {
		 letters[count] = letter_value;
		 count++;
		 if(bad_char(letter_value) == false)
		  {
		      errors++;
		      var error_style = document.getElementById('letter_error_' + i);
		      error_style.style.visibility = "visible";
			     
		  }
		  else
		  {
		      var error_style = document.getElementById('letter_error_' + i);
		      error_style.style.visibility = "hidden";
		  } 
		 if(is_real(points) == false)
		 {
		      errors++;
		      var error_style = document.getElementById('points_error_' + i);
		      error_style.style.visibility = "visible";
		 }
		 else
		 {
		      var error_style = document.getElementById('points_error_' + i);
		      error_style.style.visibility = "hidden";
		 }
		 if(old_letter)
		 {
		     var old = old_letter.value;
		     if(old != letter_value)
			     changes++;
				    
		 }
		 else
		     changes++;

		 if(old_points)
		 {
	            var old_points_value = old_points.value;
	            if(points != old_points_value)
		      changes++;
		 }
		 else
		   changes++;
	      }
	  }
	  else
	  {
	      if(is_whitespace(points) || is_real(points) == false)
	      {
		      errors++;
		      var error_style = document.getElementById('points_error_' + i);
		      error_style.style.visibility = "visible";
	      }
	      else
	      {
		      var error_style = document.getElementById('points_error_' + i);
		      error_style.style.visibility = "hidden";
	      }

	      var old_points_value = old_points.value;
	      if(points != old_points_value)
		      changes++;

	      letters[count] = old_letter.value;
	      count++;  

	  }
      }
      if(errors > 0)
      {
	       window.alert('<?cs var:lang.grade_book_items.letter_grades_error ?>');
	       return 0;
      }
     if(checkLetterUnique(letters)== 0)
     {
	       window.alert('<?cs var:lang.grade_book_items.letter_grades_unique ?>');
	       return 0;
     }
      if(changes > 0)
		return 1;
      else
		return 2;

   }

   function gotoNext()
   {
      var number = <?cs var:number_letter_grades ?>;
      var moreLetter = document.getElementById('more_letter_grades');
      if(moreLetter.style.display == "block")
	  number += 5; 
      
      
      var myform = document.getElementById('letter_grades_form');
      myform.number_letter_grades.value = number;
    
      var result = checkAll(number);

      if(result == 1)
      {
	  var confirm = window.confirm('<?cs var:lang.grade_book_items.letter_grades_confirm ?>');
	  if(confirm)
	      myform.submit();
      }
      else if(result == 2)
      {
	  window.alert('<?cs var:lang.grade_book_items.letter_grades_no_changes ?>');
	  return false;
      }
   }	
   </script>
   
</head>

<?cs call:define_body() ?>
<?cs call:print_letter_grades() ?>
</body>
</html>
<?cs call:runtime() ?>

