$(document).ready(function(){
	$("#clearTrader").on('click', clearTrader);
    $("#selectTrader").on('click', selectTrader);
	Calendar.setup ({
		inputField : "date_from",
		daFormat : "%d.%m.%Y",
		button : "date_from_trigger",
		singleClick : true,
		dateStr : "",
		step : 1
	});
	
	Calendar.setup ({
		inputField : "date_to",
		daFormat : "%d.%m.%Y",
		button : "date_to_trigger",
		singleClick : true,
		dateStr : "",
		step : 1
	});
	
	$.tablesorter.addParser({ 
        // set a unique id 
        id: 'money', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization 
            return s.replace('.','').replace(',','.'); 
        },
        // set type, either numeric or text 
        type: 'numeric' 
    });
	
	$.tablesorter.addParser({ 
        // set a unique id 
        id: 'INCOME_OVERHEAD', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization 
			var position = s.indexOf("(");
			var cutted_string = trim(s.substring(0,position));
			cutted_string = cutted_string.replace('.','').replace(',','.');
            return cutted_string;
        },
        // set type, either numeric or text 
        type: 'numeric' 
    });
	
	$("#myTable").tablesorter({
        theme: 'blue',
		sortList: [[0,0], [3,0]],
		headers: { 
			1: { 
				sorter:'money' 
			},
			3: { 
				sorter:'money' 
			},
			4: { 
				sorter:'money' 
			},
			5: { 
				sorter:'INCOME_OVERHEAD' 
			},
		} 
    });
});
function generateXLS(){
	var data = {}
	$.each($("#SearchTable").find('input, select'), function (key,input){
	  data[$(input).prop('name')] = $(input).val();
	  
	});
	$.ajax({
		type: "POST",
		url: 'index.php?module=EcmReports&action=ReportSalesByContractor&to_pdf=1&to_xls=1',
		dataType: "html",
		data: data,
        async: false,
		success: function(response){
			var new_window = window.open(response);
		},
	//	dataType: 'html',
	});
}
function generatePDF(){
	var data = {}
	$.each($("#SearchTable").find('input, select'), function (key,input){
	  data[$(input).prop('name')] = $(input).val();
	  
	});
	$.ajax({
		type: "POST",
		url: 'index.php?module=EcmReports&action=ReportSalesByContractor&to_pdf=1',
		dataType: "html",
		data: data,
        async: false,
		success: function(response){
			var new_window = window.open(response);
		},
	//	dataType: 'html',
	});
}

function clearTrader() {
    console.log('Czyszczenie wybranego sprzedawcy');
    $('#trader').val('');
	console.log('Gotowe!');
}

function selectTrader() {
    open_popup('Users', 600, 400, '', true, false, {'call_back_function': 'setTrader', 'form_name': 'ReportSalesByProduct', 'field_to_name_array': {'id': 'id'}}, 'single', true);
}

function setTrader(selectedTrader){
	$("#trader").val(selectedTrader.name_to_value_array.id);
}