function loadSettings() {
    $('#filtercontrol').remove();

    if ($('#reportfilterholder #filter').length > 0){
        $('#reportfilterholder #filter').remove();
            }
    if ($('#reportfilterholder #columns').length > 0) {
        $('#reportfilterholder #columns').remove(); }

    if ($('#reportfilterholder #groups').length > 0) {
        $('#reportfilterholder #groups').remove(); }
            
    var urlf = base + tableController() + 'LoadFilter';
    $('#rfilterholder').load(urlf, {
        timestamp: new Date().toDateString(),
        customPath: true,
        action: tableAction()
    }, initFilter);

    if (tableController() == 'taskreport.ivp/') {
        var urlg = base + tableController() + 'LoadGroup';
        $('#rgroupholder').load(urlg, {
            timestamp: new Date().toString(),
            action: tableAction()
        }, initGroup);
    }

    var urlc = base + tableController() + 'LoadColumns';
    $('#rcolumnsholder').load(urlc, {
        timestamp: new Date().toDateString(),
        customPath: true,
        action: tableAction()
    }, function () { initColumns(); $('#tb_group_field').change(checkForDate); $('#reportfilterholder').show(); });
    return false;
}

function checkForDate() {
    $.getJSON('taskreport.ivp/isdate', { groupfield: $('#tb_group_field').val() }, function(result) {
        if (result.hasOwnProperty('IsOK') && result.IsOK == true) {
            $('#date').show();
            $('input[name="tb_date_group"]').each(function(i, el) { $(el).removeAttr('checked') });
        }
        else {
            $('#date').hide();
            $('input[name="tb_date_group"]').each(function(i, el) { $(el).removeAttr('checked') });
        }
    });
}

function initGroup() {
    if ($.multipleSelect) $.multipleSelect.initByClassName('.groupcolumns', { maxwidth: '300px', init: true });
}

function applyReportFilter(isExcel) {
    if ($('#tb_group_field') != null && $('#tb_group_field').val() == '') {
        alert(ReportResources.SelectGroupField);
        return false;
    }
    if ($.trim($('#customfields').html()).length > 0 && $('#customfields select').val() == '') {
        alert(ReportResources.SelectField + ' ' + $.trim($('#customlabel').text()));
        return false;
    }
    
    if ($('#date') != null && $('#date').is(':visible')) {
        var valid = false;
        $('input[name="tb_date_group"]').each(function(i, el) { valid = $(el).is(':checked'); if (valid) return false; });
        if (!valid) {
            alert(ReportResources.SelectDateField);
            return false;
        }
    }
    
    var paramsf = $('#filter form [isdate!=1]').serializeArray();
    var dates = [];
    $('#filter form input[date]').each(function() {
        var date = { name: $(this).attr('name'), value: $(this).attr('date') };
        dates[dates.length] = date;
    });
    paramsf = $.merge(paramsf, dates);
    
    var paramsc = $('#columns form').serializeArray();
    var paramsg = $('#groups form').serializeArray();
    var params = new Array();
    params = paramsf.concat(paramsc).concat(paramsg);
    var orderby = '';
    $('select.orderby option:selected').each(function(i, el) { if (el.value) orderby += ((orderby.length > 0) ? ',' : '') + el.value; });

    params[params.length] = { name: 'tb_orderby', value: orderby };
    params[params.length] = { name: 'tb_stateid', value: '' };
    params[params.length] = { name: 'timestamp', value: new Date().toString() };
    params[params.length] = { name: 'nolayout', value: true };
    params[params.length] = { name: 'rebuild', value: true };
    params[params.length] = { name: 'generateReport', value: true };
    
    if ($('#savefrompopup').is(':checked')) {
        params[params.length] = { name: 'savefrompopup', value: true };
        params[params.length] = { name: 'sfpfiltername', value: $('#sfpfiltername').val() };
        params[params.length] = { name: 'sfpfilterwascommon', value: $('#sfpfilterwascommon').val() };
    }

    $('div.multipleselectpopup').hide();
    $('#reportfilterholder').hide(); $('ul.filterlist li.selected').removeClass('selected');
    $('#showfilter').addClass('enabled');
    enableSaveAndClear();

    var url = tableUrl();

    if (isExcel != undefined && isExcel) {
        url = tableController() + 'excel';
        $.download(url, params);
    } else {
        var reloadFilters = $("#savefrompopup").is(":checked");
        $(tableRootDiv).load(url, params, function () {
            initTable();
            if ($("#filter-sticker").length)
                setContextSize();
            if (reloadFilters) loadSavedStates();
        });
    }
    
    return false;
}

function afterInitTable() {
    try { formatTables(); } catch (e) { }
}

function clearReportFilter() {
    if ($('#clearfilter').hasClass('disabled')) return false;

    var params = { reset: true, nolayout: true, timestamp: new Date().toString() };
    //Уберем фильтр чтобы сбросить его состояние
    $('#rfilterholder').empty(); $('ul.filterlist li.selected').removeClass('selected');
    $('#showfilter').removeClass('enabled');
    $('#rcolumnsholder').empty();
    $('#rgroupholder').empty();

    $('#reportfilterholder').hide();

    disableSaveAndClear();
    $(tableRootDiv).load(tableUrl(), params, function() { initTable(); });
    return false;
}

function expand(link) {    
    $(link).parent().parent().next().next().toggle();
    var $img =$(link).find('img');
    if ($img.attr('src') == 'img/tree/minus.gif')
        $img.attr('src', 'img/tree/plus.gif');
    else
        $img.attr('src', 'img/tree/minus.gif');
    return false;
}

function expandGroup(link) {
    var $link = $(link);
    $link.parents('table:first').css('width', '100%');
    var $tr = $link.parents('tr[data-group-key]:first');
    var groupKey = $tr.attr('data-group-key');
    var $groupTr = $link.parents('table:first').find('tr[data-group-key="' + groupKey + '"]').not($tr);

    $groupTr.toggle();

    var $img = $(link).find('img');
    if ($img.attr('src') == 'img/tree/minus.gif')
        $img.attr('src', 'img/tree/plus.gif');
    else
        $img.attr('src', 'img/tree/minus.gif');
    return false;
}

function exportToExcel(stateId)
{
    var params = [];
    params.push({ name: 'stateid', value: stateId });
    params.push({ name: 'isExcel', value: true });
    url = tableController() + 'excel';
    $.download(url, params);
}