// JavaScript Document
$(document).ready(function() {
  var total = 0; 
  function calcTotal() { 
     $("input[type=checkbox]:checked").each(function() { 
         //This happens for each checked input field 
         var value = $(this).attr("value"); 
         total += parseFloat(value);  
      }); 
 }		
  //This happens when the page loads
  calcTotal();	
  $('#massPaySubmit').before('Total: $<input type="text" style="width:60px;" name="payout_total" id="payout_total" value="' + total +'" /><br />');
  
  $("").click(function() { 
    total = 0; 
    calcTotal(); 
    $("#payout_total").val(total); 
 }); 
 
  $('.selectCheckbox')
 .filter(':has(:checkbox:checked)')
 .addClass('selected')
 .end()
  .click(function(event) {
 $(this).toggleClass('selected');
 if (event.target.type !== 'checkbox') {
   $(':checkbox', this).attr('checked', function() { 
	 var value = $(':checkbox', this).attr('value'); 
         total += parseFloat(value);
    $("#payout_total").val(total); 
  return !this.checked;
   });
 }
  });
});
