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

<?cs def:print_category_row(number, one, flag) ?>
  <?cs if:number % #2 == #0 ?>
     <?cs set:rowcolor = "evencolor" ?>
  <?cs else ?>
     <?cs set:rowcolor = "oddcolor" ?>
  <?cs /if ?>
  <tr class="<?cs var:rowcolor ?>">
  <td><span id="cat_name_error_<?cs var:number ?>" style="visibility:hidden; color:#f00; font-size:24px;">*</span><input type="text" name="cat_name_<?cs var:number ?>" id="cat_name_<?cs var:number ?>" size="20" maxlength="<?cs var:cat_name_len ?>" onchange="javascript:checkCategory(<?cs var:number ?>)" title="<?cs var:lang.grade_book_categories.cat_name_help ?>" <?cs if:no_category && number == #1 && flag == #1?> value="<?cs var:lang.grade_book_categories.general ?>" <?cs else ?>value="<?cs var:one.cat_name ?>" <?cs /if ?> />
  <input type="hidden" name="cat_id_<?cs var:number ?>" value="<?cs var:one.cat_id ?>" /> 
  <input type="hidden" name="cat_name_old_<?cs var:number ?>" id="cat_name_old_<?cs var:number ?>" <?cs if:no_category && number == #1 && flag == #1?> value="<?cs var:lang.grade_book_categories.general ?>" <?cs else ?>value="<?cs var:one.cat_name ?>" <?cs /if ?> />
  </td>
  <td><span id="cat_weight_error_<?cs var:number ?>" style="visibility:hidden; color:#f00; font-size:24px;">*</span><input type="text" name="cat_weight_<?cs var:number ?>" id="cat_weight_<?cs var:number ?>" onchange="javascript:changeUngraded(<?cs var:number ?>)" size="5"  title="<?cs var:lang.grade_book_categories.cat_weight_help ?>" <?cs if:no_category && number == #1 && flag == #1 ?> value="100" <?cs else ?> value="<?cs var:one.cat_weight ?>" <?cs /if ?> />%
 
  <input type="hidden" name="cat_weight_old_<?cs var:number ?>" id="cat_weight_old_<?cs var:number ?>"  <?cs if:no_category && number == #1 && flag == #1 ?> value="100" <?cs else ?> value="<?cs var:one.cat_weight ?>" <?cs /if ?> />
  </td>
  <td>
   <?cs if:flag == #1 ?>
    <?cs if:one.number_members > #0 ?>
    <a href="javascript:openWindow('grade_book_cat_members?crs=<?cs var:Query.crs ?>&id=<?cs var:Query.id ?>&cat_id=<?cs var:one.cat_id ?>','categorymember')" title="<?cs var:lang.grade_book_categories.member_help ?>" ><?cs var:one.number_members ?> <?cs var:lang.grade_book_categories.members ?></a>
    <?cs else ?>
    <?cs var:one.number_members ?> <?cs var:lang.grade_book_categories.members ?>

    <?cs /if ?>
    <?cs else ?>
     &nbsp;
    <?cs /if ?>
  </td>
  <td>
     <select name="cat_drop_<?cs var:number ?>" id="cat_drop_<?cs var:number ?>"  title="<?cs var:lang.grade_book_categories.droplowest_help ?>">
     <?cs loop:dd=#0, #10, #1 ?>
     <?cs if:one.cat_drop == dd ?>
     <option value="<?cs var:dd ?>" selected="selected"><?cs var:dd ?></option>
     <?cs else ?>
     <option value="<?cs var:dd ?>"><?cs var:dd ?></option>
     <?cs /if ?>
     <?cs /loop ?> 
     </select>
     <input type="hidden" name="cat_drop_old_<?cs var:number ?>" id="cat_drop_old_<?cs var:number ?>" <?cs if:no_category ?>value="0" <?cs else ?> value="<?cs var:one.cat_drop ?>" <?cs /if ?> /> 
  </td>
  <td>
     <input type="checkbox" <?cs if:one.cat_ungraded ?>checked="checked" <?cs /if ?> value="yes"  id="cat_ungraded_<?cs var:number ?>" onclick="javascript:changeWeight(<?cs var:number ?>)" name="cat_ungraded_<?cs var:number ?>" title="<?cs var:lang.grade_book_categories.ungraded_help ?>" />
     <input type="hidden" name="cat_ungraded_old_<?cs var:number ?>" id="cat_ungraded_old_<?cs var:number ?>" <?cs if:one.cat_ungraded ?> value="yes" <?cs else ?> value="no" <?cs /if ?> /> 
  </td>
  <td>
      <?cs if:one.number_members == #0 && flag ?>
       <?cs if:!no_category ?>
       <a href="#" onclick="getNextUrl('grade_book_categories?crs=<?cs var:Query.crs ?>&id=<?cs var:Query.id ?>', 'delete:<?cs var:one.cat_id ?>'); return false;" title="<?cs var:lang.grade_book_categories.delete_help ?>" ><img src="<?cs var:image_path ?>/delete.gif" alt=""></a>
      <?cs else ?>
      &nbsp;
      <?cs /if ?>
     <?cs /if ?>
  </td>
  </tr>
<?cs /def ?>

<?cs def:print_cat_heading() ?>
   <tr class="headingcolor">
     <th align="left"><?cs var:lang.grade_book_categories.cat_name ?><?cs call:insert_help("gradebook_help.html", "Category_name", lang.grade_book_categories.help_button_title, "gradebook_help") ?></th>
     <th align="left"><?cs var:lang.grade_book_categories.cat_weight ?><?cs call:insert_help("gradebook_help.html", "Weight", lang.grade_book_categories.help_button_title, "gradebook_help") ?></th>
     <th align="left"><?cs var:lang.grade_book_categories.number_members ?><?cs call:insert_help("gradebook_help.html", "Items_in_this_category", lang.grade_book_categories.help_button_title, "gradebook_help") ?></th>
     <th align="left"><?cs var:lang.grade_book_categories.droplowest ?><?cs call:insert_help("gradebook_help.html", "Drop_lowest_scores", lang.grade_book_categories.help_button_title, "gradebook_help") ?> </th>
     <th align="left"><?cs var:lang.grade_book_categories.ungraded ?><?cs call:insert_help("gradebook_help.html", "Ungraded", lang.grade_book_categories.help_button_title, "gradebook_help") ?></th>
     <th align="left"><?cs var:lang.grade_book_categories.delete ?><?cs call:insert_help("gradebook_help.html", "Category_delete", lang.grade_book_categories.help_button_title, "gradebook_help") ?></th>
   </tr>
<?cs /def ?>

<?cs def:more_categories() ?>
 <div id="more_cat" style="display:none">
  <table width="100%"  cellpadding="4" cellspacing="0">
    <?cs call:print_cat_heading() ?>
   <?cs loop:x= number_categories + #1, number_categories + #10, #1 ?>
     <?cs call:print_category_row(x, one, #0) ?>
   <?cs /loop ?>
 </table> 
 </div>
<?cs /def ?>


<?cs def:print_content() ?>
<div>
 <br />
  <form id="category_form" method="post" action="grade_book_categories_save">
  <input type="hidden" name="nextUrl" value="" />
  <input type="hidden" name="save_option" value="" />
  <input type="hidden" name="crs" value="<?cs var:Query.crs ?>" />
  <input type="hidden" name="id" value="<?cs var:Query.id ?>" />
  <input type="hidden" name="number_categories" value="" />
  <table width="100%"  cellpadding="4" cellspacing="0">
    <?cs call:print_cat_heading() ?>
    <?cs loop:x = #1, #number_categories, #1 ?>
    <?cs call:print_category_row(x, categories[x], #1) ?>
    <?cs /loop ?>
  </table>
  <?cs call:more_categories() ?>
  <table width="100%"  cellpadding="4" cellspacing="0">
  <tr class="headingcolor">
     <td align="center">
       <span style="visibility:visible">
       <input type="button" value="<?cs var:lang.grade_book_categories.more ?>" id="more_cat_button" onclick="javascript:moreCategories(); return false;" title="<?cs var:lang.grade_book_categories.more_help ?>" />
       </span>
       <input type="button" value="<?cs var:lang.grade_book_categories.save ?>" onclick="javascript:getNextUrl('grade_book_categories?crs=<?cs var:Query.crs ?>&amp;id=<?cs var:Query.id ?>', 'save'); return false;" title="<?cs var:lang.grade_book_categories.save_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 openWindow(some_url,winname)
    {
       var new_window = window.open(some_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 changeWeight(checkNum)
     {
        var name = "cat_ungraded_" + checkNum;
        var ungraded =document.getElementById(name);
	if(ungraded.checked)
	{
           name = "cat_weight_" + checkNum;
           var weight =document.getElementById(name);
	   weight.value = 0;
	   weight.focus();
           name = "cat_name_" + checkNum;
           var cat_value =document.getElementById(name).value;
	   if(is_whitespace(cat_value))
	   {
	        window.alert('<?cs var:lang.grade_book_categories.category_must_be_filled ?>');
                document.getElementById(name).focus();

	   }

	   
	}
	else
	{
           name = "cat_weight_" + checkNum;
           var weight =document.getElementById(name);
           value =document.getElementById(name).value;
	   if(value == 0)
	   {
	        window.alert('<?cs var:lang.grade_book_categories.weight_must_greater_than_zero ?>');
	        weight.focus();
	   }

	}
	
     }
     
     
     function changeUngraded(number)
     {
         var name = "cat_weight_" + number;
	 weight_value = document.getElementById(name).value;
	 if(!is_whitespace(weight_value))
         {
	    if(!is_real(weight_value))
            {
		window.alert('<?cs var:lang.grade_book_categories.weight_is_number ?>');
	    }
	    else
	    {
	        weight_value =parseFloat(document.getElementById(name).value);
	        name = "cat_ungraded_" + number;
                ungraded = document.getElementById(name);
	        if(weight_value == 0)
	              ungraded.checked = true;
	        else
	              ungraded.checked = false;
	       // ungraded.focus();
		name = "cat_name_" + number;
		cat_value = document.getElementById(name).value;
		if(is_whitespace(cat_value))
		{
		    window.alert('<?cs var:lang.grade_book_categories.category_must_be_filled ?>');
		    document.getElementById(name).focus();
		}
	    }
	 }  
   
     }

     function checkCategory(number)
     {
	var name = "cat_name_" + number;
	var cat_value = document.getElementById(name).value;
        if(!bad_char(cat_value))
	    window.alert('<?cs var:lang.grade_book_categories.category_contains_bad_char ?>');
     }
     
     function all_categories_unique(categories)
    {
	  var i, j, len = categories.length;
	  for(i =0; i<len; i++)
	  {
	     for(j = 0; j<len; j++)
	      if( i != j)
	      {
		 if( categories[i] == categories[j])
			return 0;
	      }
	  }
	  return 1;
    }
     function checkAllFields()
     {
        var number = <?cs var:number_categories ?>;
        var more =  document.getElementById('more_cat');
        if(more.style.display =="block")
	    number += 10;
	var name;
        var error = 0;
	var total = 0;
	var change = 0;
	var i = 0, count = 0;
	var categories = new Array();

        for(i = 1; i <= number; i++)
	{
	   name = "cat_name_" +i; 
	   cat_name = document.getElementById(name).value;
	   name = "cat_name_error_" +i; 
	   cat_name_error = document.getElementById(name);
	   name = "cat_name_old_" +i;
	   cat_name_old = document.getElementById(name).value;

	   name = "cat_weight_" +i; 
	   weight = document.getElementById(name).value;
	   name = "cat_weight_error_" +i; 
	   cat_weight_error = document.getElementById(name);
	   name = "cat_weight_old_" +i;
	   cat_weight_old = document.getElementById(name).value;

	   name = "cat_ungraded_" +i; 
	   ungraded = document.getElementById(name).checked;
	   name = "cat_ungraded_old_" +i; 
	   ungraded_old = document.getElementById(name).value;
           
	   name = "cat_drop_" +i; 
	   drop = get_selected_value(name);
	   name = "cat_drop_old_" +i; 
	   drop_old = document.getElementById(name).value;
           

	   if( !is_whitespace(cat_name) || !is_whitespace(weight) || ungraded)
	   {
	       if( is_whitespace(cat_name) || !bad_char(cat_name))
               {
		  error++;
		  cat_name_error.style.visibility = "visible";
	       }
	       else
	       {
		  cat_name_error.style.visibility = "hidden";
		  if(cat_name != cat_name_old)
			  change++;
		  categories[count] = cat_name;
               }
               if( is_whitespace(weight) || !is_real(weight))
	       {
		  error++;
		  cat_weight_error.style.visibility = "visible";
	       }

	       else
	       {
		  cat_weight_error.style.visibility = "hidden";
	          if(weight != cat_weight_old)
			 change++; 
	       }

               if(ungraded)
	       {
                   if( !is_whitespace(weight) && is_real(weight))
	           {
                      if(parseFloat(weight) > 0)
		      {
		         error++;
		         cat_weight_error.style.visibility = "visible";
		      }
		      else
		         cat_weight_error.style.visibility = "hidden";
		   }
                  if(ungraded_old == "no")
			change++;
	       }
	       else if( !is_whitespace(weight) && is_real(weight))
	       {
			total = total + parseFloat(weight);
                        if(ungraded_old == "yes")
			   change++;

	       }

	       if(drop != drop_old)
		     change++;
	       count++;
	   }
	   else
	   {
		  cat_name_error.style.visibility = "hidden";
		  cat_weight_error.style.visibility = "hidden";
	   }
           

	}
	if(!all_categories_unique(categories))
	{
	    window.alert('<?cs var:lang.grade_book_categories.cat_name_unique ?>');
	    return 0;
	}
        if(error > 0)
	  return 0;
        else
	{
	  if(total != 100)
	    return 0;
	  if(change > 0)
            return 1;
	  else
	    return 2;
        }
     }	     

     function moreCategories()
     {
        var more =  document.getElementById('more_cat');
        more.style.display ="block" ;
        var more_button =  document.getElementById('more_cat_button');
        more_button.style.visibility ="hidden" ;
        var myform = document.getElementById('category_form');
        var temp = myform.number_categories.value;
	myform.number_categories = temp + 10;
     }
     
     function getNextUrl(url, some)
     {
         var myform = document.getElementById('category_form');
         myform.nextUrl.value = url;
         var more =  document.getElementById('more_cat');

	if(some.length > 0 && some.indexOf('delete') != -1)
	{
	    var confirm = window.confirm('<?cs var:lang.grade_book_categories.delete_confirm ?>');
	    if(confirm)
	    {
	      myform.save_option.value = some;
              myform.submit();
	    }

	}
	else
	{
	   if(more.style.display =="block")
             myform.number_categories.value = <?cs var:number_categories ?> + 10;
	   else
             myform.number_categories.value = <?cs var:number_categories ?>;

         var flag = checkAllFields();

         if(flag == 0)
           window.alert('<?cs var:lang.grade_book_categories.correct_errors ?>');
	 else if(flag == 1)
	 {
	    var change = window.confirm('<?cs var:lang.grade_book_categories.save_changes ?>');
	    if(change)
	    {
	      myform.save_option.value = "save";
              myform.submit();
	    }
	    else
	    {
	      myform.save_option.value = "no";
              myform.submit();
	    }
	 }
	 else if(flag == 2)
	 {
	      <?cs if:no_category ?>
	      myform.save_option.value = "save";
	      <?cs else ?>
	      myform.save_option.value = "no";
	      <?cs /if ?>
              myform.submit();
	 }
	}
     }
   </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 id="content">
<?cs call:print_content() ?>
</div>
</div>
</body>
</html>
<?cs call:runtime() ?>
