
<script type="text/javascript">
        $(document).ready(function() {
            $('#datepicker_dat_ent').datepicker({
                format: "dd/mm/yyyy",
                language: "fr",
                autoclose: true
            });

            $('#datepicker_date_sor').datepicker({
                format: "dd/mm/yyyy",
                language: "fr",
                autoclose: true
            });
        });
</script>

@if($mode!="create")
    <script type="text/javascript">
        $(document).ready(function() {
            $(".box_log").fancybox({
                    //width:'7',
                    autoDimensions:false,
                    fitToView   : false,
                    autoSize    : false,
                    closeClick  : true,
                    openEffect  : 'fade',
                    closeEffect : 'fade',
                    afterLoad: function () {

                    },
                    afterClose: function () {
                        return;
                    }
            })

            $('#btn_ventiler').click(function () {
                ventiler();
            });
            $('.input_vent_mois').change(function () {
                calc_vent();
            });

            $('.ventil').change(function () {
                set_vent() ;
            });
            
            function ventiler() {
                if($('#ventil_active').val()==0) {
                    $('#div_ventil').attr('style','visibility:show');
                    $('.ventil').attr('style','visibility:show');
                    $('#ventil_active').val(1);
                } else {
                    $('#div_ventil').attr('style','visibility:hidden');
                    $('.ventil').attr('style','visibility:hidden');
                    $('#ventil_active').val(0);
                    raz_vent();
                }
            }
            function set_vent() {
                var total=$('#total_vent').val();
                var t_mois=['jan','fev','mar','avr','mai','jun','jui','aou','sep','oct','nov','dec'];
                var nb=0;
                //Compte le nomnbre de mois
                for(var i=0;i<t_mois.length;i++) {
                    if($('#'+t_mois[i]+'_check').prop( "checked" )) {
                        nb++;
                    }
                }
                var ventmois=0;
                var reste=0;
                if(nb>0) {
                    ventmois=parseInt(total/nb);
                    if(nb*ventmois!=total) reste=total-nb*ventmois;
                }
                for(var i=0;i<t_mois.length;i++) {
                    if($('#'+t_mois[i]+'_check').prop( "checked" )) {
                        if(i==(t_mois.length-1)) ventmois+=reste;
                        $('#'+t_mois[i]+'_vent').val(ventmois);

                    } else {
                        $('#'+t_mois[i]+'_vent').val(0);
                    }
                }
                calc_vent();
            }
            function raz_vent() {
                $('#total_vent').val(0);
                var t_mois=['jan','fev','mar','avr','mai','jun','jui','aou','sep','oct','nov','dec'];
                for(var i=0;i<t_mois.length;i++) {
                        $('#'+t_mois[i]+'_vent').val(0);
                }
                calc_vent();
            }
            function calc_vent() {
                var total=$('#total_vent').val();
                var t_mois=['jan','fev','mar','avr','mai','jun','jui','aou','sep','oct','nov','dec'];
                var total_vent=0;

                for(var i=0;i<t_mois.length;i++) {
                    total_vent+=parseInt($('#'+t_mois[i]+'_vent').val());
                }
                $('#total_a_vent').val(total_vent);
                var reste = parseInt($('#total_vent').val())-parseInt($('#total_a_vent').val());
                if(reste>0) $('#total_a_vent').val(total_vent+" ("+reste+")");
            }
        });
    </script>
@endif
