jQuery(function($) {
    function updateLectures() {
        var totalPriceField = $('#TotalPrice')[0];
        var additionalInfoField = $('#AdditionalInformation')[0];
        var priceForAll = parseInt($('#PriceForAll')[0].value); // there should be only one
        
        var total = 0;
        var allChecked = true;
        $('.lectures').find('li').each(function() {
            if ( $(this).find('input.checkbox')[0].checked ) {
                var label = $(this).find('label')[0].innerHTML;
                var value = label.match(/\(\$(\d*)\)$/)[1];
                total += parseInt(value);
            } else {
                allChecked = false;
            }
        });  
        
        if ( allChecked && priceForAll ) {
            totalPriceField.innerHTML = '$'+priceForAll;
            additionalInfoField.innerHTML = 'Complete course discount applies.';
        } else {
            totalPriceField.innerHTML = '$'+total;
            additionalInfoField.innerHTML = '';
        }
    }
    
    $('.lectures').find('li input.checkbox').click(function() {
        updateLectures();
    });
    
    updateLectures();    
});