$(document).ready(function() {
	$('.reading-checkbox').click(function() {
		var ss = $('#studySession_'+$(this).attr('studysession-id'));
		ss.attr('checked', 'checked');
		$.each($('.reading-checkbox[studysession-id='+$(this).attr('studysession-id')+']'), function() {
			if (!this.checked) {
				ss.removeAttr('checked');
				return;
			}
		})
	});
	
	
	$('.studysession-checkbox').click(function() {
		if (this.checked) {
			$('.reading-checkbox[studysession-id='+$(this).attr('studysession-id')+']').attr('checked', 'checked');
		}
		else {
			$('.reading-checkbox[studysession-id='+$(this).attr('studysession-id')+']').removeAttr('checked');
		}
	})
	
	$('input[name=type]:radio').click(function() {
		if ($(this).val() == 'preset_weekly_test') {
			$('.studysession-checkbox, .reading-checkbox').attr('disabled', 'disabled');
			$('#exam').attr('disabled', 'disabled');
		}
		else {
			if ($(this).val() != 'preset_practice_exam') {
	        	$('#exam').attr('disabled', 'disabled');
	      	}
			$('.studysession-checkbox, .reading-checkbox').removeAttr('disabled')
		}
	})
	
	if ($('input[name=type]:radio:checked').val() == 'preset_weekly_test' || $('input[name=type]:radio:checked').val() == 'preset_practice_exam') {
		$('.studysession-checkbox, .reading-checkbox').attr('disabled', 'disabled');
	}
	
	$('#preset_practice_exam').click(function() {
      $('#exam').removeAttr('disabled');
    });
    
})
