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

<?cs def:print_radio(button_name, method, button_value, default_value, help_target) ?>
  <tr>
     <td colspan="2">
   <?cs if:button_value == default_value ?>
      <input type="radio" name="<?cs var:button_name ?>" value="<?cs var:button_value ?>" checked="checked" title="<?cs var:method.help ?>" /> 
   <?cs else ?>
      <input type="radio" name="<?cs var:button_name ?>" value="<?cs var:button_value ?>" title="<?cs var:method.help ?>" /> 
   <?cs /if ?>
   <?cs var:method.name ?> <?cs var:method.desc ?> <?cs call:insert_help("gradebook_help.html", help_target, lang.grade_book_final.help_button_title, "gradebook_help") ?>
   </td>
   </tr>
<?cs /def ?>

<?cs def:print_general_scoring_method() ?>
   <tr class="headingcolor">
     <th colspan="2" align="left">
        <?cs var:lang.grade_book_final.scoring_method ?>
     </th>
   </tr>
  <?cs set:scoring_method = "scoring_method" ?>
  <?cs call:print_radio(scoring_method, lang.grade_book_final.no_calculation, #0, default_scoring_method,"FINAL_Do_not_calculate") ?>
  <?cs call:print_radio(scoring_method, lang.grade_book_final.point_based_system, #1, default_scoring_method, "FINAL_Point_based") ?>
  <?cs call:print_radio(scoring_method, lang.grade_book_final.category_weight_system, #2, default_scoring_method, "FINAL_Category_weight") ?>

<?cs /def ?>

<?cs def:print_rounding_method() ?>
   <tr class="headingcolor">
     <th colspan="2" align="left">
        <?cs var:lang.grade_book_final.rounding_method ?>
     </th>
  </tr>
  <?cs set:rounding_method = "rounding_method" ?>
  <?cs call:print_radio(rounding_method, lang.grade_book_final.round_whole_number, #0,  default_rounding_method,"FINAL_Round_whole") ?>
  <?cs call:print_radio(rounding_method, lang.grade_book_final.round_nearest_tenth, #1, default_rounding_method, "FINAL_Round_tenth") ?>

<?cs /def ?>

<?cs def:print_one_scale(myscale, rownum, preValue) ?>
  <?cs if:rownum % 2 == 0 ?>
     <?cs set:rowcolor = "evencolor" ?> 
  <?cs else ?>
     <?cs set:rowcolor = "oddcolor" ?> 
  <?cs /if ?>
  <tr class="<?cs var:rowcolor ?>">
    <td  width="60">
       <input type="text" size="<?cs var:grade_len ?>" maxlength="<?cs var:grade_len ?>" name="grade_<?cs var:rownum ?>" id="grade_<?cs var:rownum ?>" value="<?cs var:myscale.grade ?>" title="<?cs var:lang.grade_book_final.enter_grade ?>" />
     <input type="hidden" name="grade_<?cs var:rownum ?>_old" id="grade_<?cs var:rownum ?>_old" value="<?cs var:myscale.grade ?>" /> 


    </th>
    <td>
      <input type="text" size="4" name="scale_<?cs var:rownum ?>" id="scale_<?cs var:rownum ?>" value="<?cs var:myscale.start ?>" title="<?cs var:myscale.start ?> ---- <?cs var:preValue ?> ----> <?cs var:myscale.grade ?>" />
     <input type="hidden" name="scale_<?cs var:rownum ?>_old" id="scale_<?cs var:rownum ?>_old" value="<?cs var:myscale.start ?>" /> 
    </td>
  </tr>   
<?cs /def ?>

<?cs def:print_all_scales() ?>
 <tr class="headingcolor">
   <th align="left" width="60"><?cs var:lang.grade_book_final.scales.grade ?> <?cs call:insert_help("gradebook_help.html", "FINAL_Grade_scale", lang.grade_book_final.help_button_title, "gradebook_help") ?></th>
   <th align="left"><?cs var:lang.grade_book_final.scales.start_point ?> <?cs call:insert_help("gradebook_help.html", "FINAL_Grade_scale", lang.grade_book_final.help_button_title, "gradebook_help") ?></th>
 </tr>
 <?cs set:pre = "" ?>
 <?cs set:num = #0 ?>
 <?cs each:one = scale ?>
   <?cs call:print_one_scale(one, num, pre) ?>
   <?cs set:pre = one.start ?>
   <?cs set:num = num + #1 ?>
 <?cs /each ?>
<?cs /def ?>

<?cs def:print_content() ?>
<div>
<br />
 <form method="post" id="final_form" action="grade_book_final_save">
 <input type="hidden" name="crs" value="<?cs var:Query.crs ?>" />
 <input type="hidden" name="id" value="<?cs var:Query.id ?>" />
 <input type="hidden" name="nextUrl" value="" />
 <input type="hidden" name="save_option" value="" />
 <input type="hidden" name="number_scales" id="number_scales" value="<?cs var:number_scales ?>" />
 <input type="hidden" name="scoring_method_old"  value="<?cs var:default_scoring_method ?>" />
 <input type="hidden" name="rounding_method_old"  value="<?cs var:default_rounding_method ?>" />
<table cellspacing="0" cellpadding="4" width="100%">
<?cs call:print_general_scoring_method() ?>
<?cs call:print_rounding_method() ?>
<?cs call:print_all_scales() ?>
<tr class="headingcolor"><td colspan="2" align="center">
    <?cs if:has_final_method ?>
    <input type="button" onclick="javascript:getNextUrl('grade_book_final?crs=<?cs var:Query.crs ?>&amp;id=<?cs var:Query.id ?>', 'default')" value="<?cs var:lang.grade_book_final.default_setting ?>" title="<?cs var:lang.grade_bood_final.default_setting_help ?>" />
    <?cs /if ?>

    <input type="button" onclick="javascript:getNextUrl('grade_book_final?crs=<?cs var:Query.crs ?>&amp;id=<?cs var:Query.id ?>', 'save')" value="<?cs var:lang.grade_book_final.save ?>" title="<?cs var:lang.grade_bood_final.save ?>" />
     
    <?cs if:final == 1 ?>
    <input type="button" onclick="javascript:getNextUrl('grade_book_final?crs=<?cs var:Query.crs ?>&amp;id=<?cs var:Query.id ?>', 'mysetting')" value="<?cs var:lang.grade_book_final.my_setting ?>" title="<?cs var:lang.grade_bood_final.my_setting_help ?>" />
    <?cs /if ?>
    </td>
</tr>
</table>
</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 all_unique(grades)
    {
	var i, j;
	var len = grades.length;

	for(i = 0; i<len; i++)
	{
	   for(j =0; j<len; j++)
		if(i != j)
		{
		  if(grades[i] == grades[j])
		    return 0;
		}
	}
	return 1;
    }
    function check_scales()
    {
       var i;
       var scale_name, scale_name_old;
       var grade_name, grade_name_old;
       var scale_value, scale_value_old;
       var grade_value, grade_value_old;
       var scale_count = 0;
       var error_count = 0;
       var change_count = 0;
       var pre = 50000;
       var scale_error = 0;
       var grades = new Array(); 

       var number_scales = document.getElementById('number_scales').value;
       
       for(i = 0; i< number_scales; i++)
       {
	    scale_name = "scale_" + i;
	    scale_name_old = "scale_" + i + "_old";
	    grade_name = "grade_" + i;
	    grade_name_old = "grade_" + i + "_old";

            scale_value = document.getElementById(scale_name).value;
            scale_value_old = document.getElementById(scale_name_old).value;

            grade_value = document.getElementById(grade_name).value;
            grade_value_old = document.getElementById(grade_name_old).value;

	    if( !is_whitespace(scale_value) && !is_whitespace(grade_value) )
	    {
		 grades[scale_count] = grade_value;
	         scale_count++;
		 if( scale_value != scale_value_old)
		     change_count++;

		 if( grade_value != grade_value_old)
		     change_count++;

		 if(!is_real(scale_value))
		     error_count++;
		 var new_value = parseFloat(scale_value);
		 if(pre < new_value)
		     scale_error++;
		 pre = new_value;
                
	    } 
	    
       }
       if(!all_unique(grades))
       {
	    window.alert('<?cs var:lang.grade_book_final.grades_unique_required ?>');
	    return 0;
       }
       if(scale_count < 2)
       {
	    window.alert('<?cs var:lang.grade_book_final.number_scales_required ?>');
	    return 0;
       }
       if(error_count)
       {
	    window.alert('<?cs var:lang.grade_book_final.scale_start_point ?>');
	    return 0;
       }  
       if(scale_error)
       {
	    window.alert('<?cs var:lang.grade_book_final.start_point_great_than_previous_one ?>');
	    return 0;
       }
       if(change_count)
	    return 1;
       else
	    return 2;

    }
    function getRadioValue(name)
    {
	 for (i = 0; i<name.length; i++)
         {
              if(name[i].checked)
		   return name[i].value;
	 }
	
    }

    function hasSomeChange()
    {
        cur_value = getRadioValue(document.forms[0].scoring_method);
        old = document.forms[0].scoring_method_old.value;
	var change = 0;

	if(cur_value != old)
		change++;
	   	
        cur_value = getRadioValue(document.forms[0].rounding_method);
        old = document.forms[0].rounding_method_old.value;
	if(cur_value != old)
		change++;

        scale_change = check_scales();
        
	if(scale_change == 0) 
	     return -1;

	if(scale_change == 1)
	     change++;

	if(change > 0)
	     return 1;
	    
       return 0;	
    }

    function getNextUrl(url, some)
    {
       var myform = document.getElementById('final_form');
       myform.nextUrl.value = url;
       if(some == "mysetting")
       {
	   myform.save_option.value = "mysetting";
           myform.submit();
       }
       else if(some == "default")
       {
	   myform.save_option.value = "default";
           myform.submit();
       }
       else
       {

           changes = hasSomeChange();
       
           if(changes == 1)
           {
	      var confirm_change = window.confirm('<?cs var:lang.grade_book_final.confirm_changes ?>');
	      if(confirm_change)
	      {
	         myform.save_option.value = "yes";
                 myform.submit();
	      }
	      else
	      {
	         myform.save_option.value = "no";
                 if(some != "save")
                     myform.submit();
	      }
           }
           else if( changes ==0)
           {
	      <?cs if:final == 0 ?>
	         myform.save_option.value = "yes";
                 myform.submit();
	      <?cs elif:final == 1 ?>
		   var ret = window.confirm('<?cs var:lang.grade_book_final.accept_default ?>');
	           if(ret)
	             myform.save_option.value = "yes";
		   else
	             myform.save_option.value = "no";
                   myform.submit();
	      <?cs else ?>
              if(some == "save")
	      {
		   window.alert('<?cs var:lang.grade_book_final.no_changes ?>');
		   return 0;
	      }
	      else
	      {
	         myform.save_option.value = "no";
                 myform.submit();
	      }
          <?cs /if ?>
          }
       } 
    }
   </script>
   
</head>


<?cs call:define_body() ?> 
<div id="container">
<?cs call:print_course_heading(back_url, lang.grade_book_menu_util.goback) ?>
<div>
<br />
<br />
  <?cs call:grade_menu()  ?>
</div>
<div id="content">
<?cs call:print_content() ?>
</div>
</div>
</body>
</html>
<?cs call:runtime() ?>
