jQuery(document).ready(function($j) {
    $j('[id^="adauga_viz_"]').click(function() {
        $row = '<tr>';
        no_del = false;
        rest_plata = 0;
        do_rest = $j(this).attr('do_rest');
        $j(this).siblings(':input:not(:hidden)').each(function() {
            $j(this).removeAttr('disabled').removeAttr('readonly');
            if ($j(this).val() == "Variabila(a)") {
                no_del = true;
            }
            if ($j(this).attr('id') == cot_dat_pers || $j(this).attr('id') == cot_rest_pers || $j(this).attr('id') == cot_dat_soc || $j(this).attr('id') == cot_rest_soc) {
                rest_plata += +$j(this).val();
            } else if ($j(this).attr('id') == cot_plat_pers || $j(this).attr('id') == cot_plat_soc) {
                rest_plata -= +$j(this).val();
            }
            $j('<input type="hidden" name="' + $j(this).attr('id') + '[]" value="' + ($j(this).val() ? $j(this).val() : ' ') + '" />').appendTo($j(this).parent());
            $row += '<td>' + ($j(this).val() ? $j(this).val() : ' ') + '</td>';
            $j(this).val('');
        });
        $row += (do_rest == 'true' ? '<td>' + (Math.round(rest_plata * 100) / 100) + '</td>' : '') +'<td><a href="#"><img alt="Modifica" src="' + imgPath + '/imagini/edit.gif"/></a><a href="#">' + (no_del ? '' : '<img alt="Sterge" src="' + imgPath + '/imagini/delete.gif"/></a></tr>');
        $j($row).appendTo($j('#viz_' + $j(this).attr('id').replace('adauga_viz_', '')));
        $j('a:has(img[alt="Modifica"])').click(viz_modif);
        $j('a:has(img[alt="Sterge"])').click(viz_sterg);
    });
    $j('[id^="total_"]').siblings().change(function() {
        $j(this).siblings('[id^="total_"]').val(0);
        total = +$j(this).val();
        $j(this).siblings(':input').each(function() {
            total += +$j(this).val();
        });
        $j(this).siblings('[id^="total_"]').val(total);
    });
    $j('[id^="total_"]').siblings().each(function() {
        $j(this).change();
    });
    function viz_modif() {
        do_rest = $j('[id^="adauga_viz_"]').attr('do_rest');
        id_ = 'adauga_' + $j(this).parent().parent().parent().parent().attr('id');
        $j('a:has(img[alt="Modifica"])').unbind('click');
        $j('a:has(img[alt="Modifica"])').click(ret_false);
        offset = $j(this).parent().parent().prevAll().length;
        segment_size = $j(this).parent().siblings().length - (do_rest == 'true' ? 1 : 0);
        $j(this).parent().parent().remove();
        $j('input#' + id_).parent().show().prev().removeClass('marg').addClass('active');
        $j('input#' + id_).siblings(':hidden').each(function(nr) {
            if (nr >= segment_size * offset && nr < segment_size * (offset + 1)) {
                $j(this).remove();
            }
        });
        $j(this).parent().siblings().each(function(nr) {
            valoarea = $j(this).html().replace(' ', '');
            $j('input#' + id_).siblings(':nth-child(' + ((+nr * 3) + 2) + ')').val(valoarea);
        });
        if ($j('#' + tip_cot_pers + ', #' + tip_cot_soc).val() == 'Variabila(a)') {
            $j('#' + tip_cot_pers + ', #' + tip_cot_soc).attr('disabled', 'disabled');
            $j('#' + cot_dat_pers + ', #' + cot_dat_soc + ', #' + an_cot_pers + ', #' + an_cot_soc + ', #' + cot_dat_pers + ', #' + cot_dat_soc).attr('readonly', 'readonly');
        }
        return false;
    }
    function viz_sterg() {
        if (confirm('Sigur stergeti?')) {
            do_rest = $j('[id^="adauga_viz_"]').attr('do_rest');
            id_ = 'adauga_' + $j(this).parent().parent().parent().parent().attr('id');
            offset = $j(this).parent().parent().prevAll().length;
            segment_size = $j(this).parent().siblings().length - (do_rest == 'true' ? 1 : 0);
            $j(this).parent().parent().remove();
            $j('input#' + id_).siblings(':hidden').each(function(nr) {
                if (nr >= segment_size * offset && nr < segment_size * (offset + 1)) {
                    $j(this).remove();
                }
            });
        }
        return false;
    }
    function ret_false() {
        return false;
    }
    $j('a:has(img[alt="Modifica"])').click(viz_modif);
    $j('a:has(img[alt="Sterge"])').click(viz_sterg);
    $j('input#salvare_').click(function() {
        $j('input#salvare').val('Salvare');
    });
    $j('form').validate();
    $j('.an_decl').each(function() {
        ss = $j(this);
        ss.rules("add", {
            remote: {
                url: 'an_decl.ajax.php',
                type: 'post',
                data: {
                    id_rel: $j('#id_rel').val(),
                    id_decl: $j('#id_decl').val(),
                    field: ss.attr('name')
                }
            },
            messages: {
                remote: 'Exista deja o declaratie pentru anul introdus!'
            }
        });
    });
});

