
        var olang = {
                "sProcessing":   "Bitte warten...",
                "sLengthMenu":   "_MENU_ Einträge anzeigen",
                "sZeroRecords":  "Keine Einträge vorhanden.",
                "sInfo":         "_START_ bis _END_ von _TOTAL_ Einträgen",
                "sInfoEmpty":    "0 bis 0 von 0 Einträgen",
                "sInfoFiltered": "(gefiltert von _MAX_  Einträgen)",
                "sInfoPostFix":  "",
                "sSearch":       "Suchen",
                "sUrl":          "",
                "oPaginate": {
                        "sFirst":    "<<",
                        "sPrevious": "<",
                        "sNext":     ">",
                        "sLast":     ">>"
                }
        };

        var dgAffiliates;
        var dgLeads;
        var dgMerchants;
        var dgBanner;
        var dgCampaigns;
        var dgInvoices;
        var dgInvoicesOpen;
        
        $(document).ready(function() {

            // AFFILIATES TABLE
            
            dgAffiliates = $('#dgAffiliates').dataTable({
                bJQueryUI:true
                , "aaSorting": [[0,'asc']]
                , "bAutoWidth": false
                , "sPaginationType": "full_numbers"
                , "aLengthMenu": [[10, 20, 30, 50, -1], [10, 20, 30, 50, "Alle"]]
                , "iDisplayLength": 20
                , "iCookieDuration": 60 * 60 * 24 * 365
                , "oLanguage": olang
                , "sDom": '<"colButtun"C><"H"lfr>t<"F"ip>'
                , "oColVis": { "aiExclude": [ 0, 6 ], "buttonText": "Spalten", "activate": "click" }
                , "bProcessing": true
                , "bServerSide": true
                , "bStateSave": true
                , "sAjaxSource": "/ajax.php?table=affiliates"
                , "fnDrawCallback": function() {
                    x = 1;
                }
                
                , "fnInitComplete": function() {
                    //$('div.affiliateToolbar').html('Custom tool bar!');
                }
                
                , "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                    
                    cols = {}; vcols = {}; vcol = -1;
                    for (i = 0; i < dgAffiliates.fnSettings().aoColumns.length; i++) {
                        cols[dgAffiliates.fnSettings().aoColumns[i].sName] = i;
                        if (dgAffiliates.fnSettings().aoColumns[i].bVisible) {
                            vcol = vcol + 1;
                            vcols[dgAffiliates.fnSettings().aoColumns[i].sName] = vcol;
                        }
                    }
                    $(nRow).data('dgData', aData);
                    var dgData = aData;   
                    
                    img0 = $('<img src="/img/icon/browse.png" alt="" title="Menu" class="icon" />');
                    img0.jjmenu("click", 
                            [
                             {title:"Leads", action:{type:"fn",callback:'(function () { filterLead({filterLeadAffiliateID:'+dgData[0]+'});})' }},
                             {title:"Kampagnen", action:{type:"fn",callback:'(function () { filterCommission({filterCommissionAffiliateID:'+dgData[0]+'});})' }},
                             {title:"Rechnungen", action:{type:"fn",callback:'(function () { filterInvoice({filterInvoiceAffiliateID:'+dgData[0]+'});})' }}
                           ]
                    );
                    img2 = $('<img src="/img/icon/edit.png" alt="" title="bearbeiten" class="icon" />').click(function (event) {
                        param = getDBO('ParamGetAffiliate');
                        param.affiliateID = $('td:eq(0)', $(this).parent().parent()).text();
                        foo = rpc('getAffiliate', [{"type":"object","name":"ParamGetAffiliate","data":JSON.stringify(param)}]);
                        dialogEditAffiliate(foo);
                    });
                    img1 = $('<img src="/img/icon/info.png" alt="" title="anzeigen" class="icon" />').click(function (event) {
                        param = getDBO('ParamGetAffiliate');
                        param.affiliateID = $('td:eq(0)', $(this).parent().parent()).text();
                        foo = rpc('getAffiliate', [{"type":"object","name":"ParamGetAffiliate","data":JSON.stringify(param)}]);
                        dialogInfoAffiliate(foo);
                    });
                    $('td', nRow).eq(vcols.action).append(img0, img1, img2);
                    
                    if (aData[cols.stateID] == 1) $('td', nRow).eq(vcols.stateID).text('aktiv');
                    else $('td', nRow).eq(vcols.stateID).text('inaktiv');
                    return nRow;
                }
                , "aoColumns": [ 
                    { "sName": "affiliateID", "sClass": "affiliateID tright" }
                    , { "sName": "company", "sClass": "company", "sWidth":"200px" }
                    , { "sName": "campaignCount", "sClass": "campaignCount tright" }
                    , { "sName": "leadsOpenCount", "sClass": "leadsOpenCount tright" }
                    , { "sName": "turnoverOpen", "sClass": "turnoverOpen tright" }
                    , { "sName": "stateID", "sClass": "stateID tright" }
                    , { "sName": "action", "sClass": "actions tright", "bSortable": false }
                ]
                , "fnServerData": function ( sSource, aoData, fnCallback ) {
                    aoData.push( { "name": "AffiliateStatus", "value": $('#filterAffiliateStatus').val() } );
                    aoData.push( { "name": "campaignID", "value": $('#filterAffiliateCampaignID').val() } );
                    $.ajax( {"dataType":'json',"type":"GET","url":sSource,"data":aoData,"success":fnCallback});
                }
            });
            $("#filterAffiliateCampaignID" ).autocomplete({ delay:0, search:function(event, ui) {dgCampaigns.fnDraw();}, source:[], minLength:0 });

            // MERCHANTS TABLE
            
            dgMerchants = $('#dgMerchants').dataTable({
                bJQueryUI:true
                , "aaSorting": [[0,'asc']]
                , "bAutoWidth": false
                , "sPaginationType": "full_numbers"
                , "aLengthMenu": [[10, 20, 30, 50, -1], [10, 20, 30, 50, "Alle"]]
                , "iDisplayLength": 20
                , "iCookieDuration": 60 * 60 * 24 * 365
                , "oLanguage": olang
                , "sDom": '<"colButtun"C><"H"lfr>t<"F"ip>'
                , "oColVis": { "aiExclude": [ 0, 6 ], "buttonText": "Spalten", "activate": "click" }
                , "bProcessing": true
                , "bServerSide": true
                , "bStateSave": true
                , "sAjaxSource": "/ajax.php?table=merchants"
                , "fnDrawCallback": function() {
                    x = 1;
                }
                , "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                    
                    cols = {}; vcols = {}; vcol = -1;
                    for (i = 0; i < dgMerchants.fnSettings().aoColumns.length; i++) {
                        cols[dgMerchants.fnSettings().aoColumns[i].sName] = i;
                        if (dgMerchants.fnSettings().aoColumns[i].bVisible) {
                            vcol = vcol + 1;
                            vcols[dgMerchants.fnSettings().aoColumns[i].sName] = vcol;
                        }
                    }
                    $(nRow).data('dgData', aData);
                    var dgData = aData;                    
                    
                    img0 = $('<img src="/img/icon/browse.png" alt="" title="Menu" class="icon" />');
                    img0.jjmenu("click", 
                            [
                             {title:"Leads", action:{type:"fn",callback:'(function () { filterLead({filterLeadMerchantID:'+dgData[0]+'});})' }},
                             {title:"Kampagnen", action:{type:"fn",callback:'(function () { filterCampaign({filterCampaignMerchantID:'+dgData[0]+'});})' }},
                             {title:"Rechnungen", action:{type:"fn",callback:'(function () { filterInvoice({filterInvoiceMerchantID:'+dgData[0]+'});})' }}
                           ]
                    );
                    img2 = $('<img src="/img/icon/edit.png" alt="" title="bearbeiten" class="icon" />').click(function (event) {
                        param = getDBO('ParamGetMerchant');
                        param.merchantID = $('td:eq(0)', $(this).parent().parent()).text();
                        foo = rpc('getMerchant', [{"type":"object","name":"ParamGetMerchant","data":JSON.stringify(param)}]);
                        dialogEditMerchant(foo);
                    });
                    img1 = $('<img src="/img/icon/info.png" alt="" title="anzeigen" class="icon" />').click(function (event) {
                        param = getDBO('ParamGetMerchant');
                        param.merchantID = $('td:eq(0)', $(this).parent().parent()).text();
                        foo = rpc('getMerchant', [{"type":"object","name":"ParamGetMerchant","data":JSON.stringify(param)}]);
                        dialogInfoMerchant(foo);
                    });
                    $('td', nRow).eq(vcols.action).append(img0, img1, img2);
                    
                    if (aData[cols.stateID] == 1) $('td', nRow).eq(vcols.stateID).text('aktiv');
                    else $('td', nRow).eq(vcols.stateID).text('inaktiv');
                    return nRow;
                }
                , "aoColumns": [ 
                    { "sName": "merchantID", "sClass": "affiliateID tright" }
                    , { "sName": "company", "sClass": "company", "sWidth" : "200px" }
                    , { "sName": "campaignCount", "sClass": "campaignCount tright" }
                    , { "sName": "leadsOpenCount", "sClass": "leadsOpenCount tright" }
                    , { "sName": "turnoverOpen", "sClass": "turnoverOpen tright" }
                    , { "sName": "stateID", "sClass": "stateID tright" }
                    , { "sName": "action", "sClass": "actions tright", "bSortable": false }
                ]
                , "fnServerData": function ( sSource, aoData, fnCallback ) {
                    aoData.push( { "name": "MerchantStatus", "value": $('#filterMerchantStatus').val() } );
                    $.ajax( {"dataType":'json',"type":"GET","url":sSource,"data":aoData,"success":fnCallback});
                }
            });

            // LEADS TABLE            

            dgLeads = $('#dgLeads').dataTable({

                
                bJQueryUI:true
                , "aaSorting": [[0,'asc']]
                , "sPaginationType": "full_numbers"
                , "bAutoWidth": false
                , "aLengthMenu": [[10, 20, 30, 50, -1], [10, 20, 30, 50, "Alle"]]
                , "iDisplayLength": 20
                , "iCookieDuration": 60 * 60 * 24 * 365
                , "oLanguage": olang
                , "sDom": '<"colButtun"C><"H"lfr>t<"F"ip>'
                , "oColVis": { "buttonText": "Spalten", "activate": "click" }
                , "bProcessing": true
                , "bServerSide": true
                , "bStateSave": true
                , "sAjaxSource": "/ajax.php?table=leads"
                , "sScrollX": "100%"
                , "bScrollCollapse": true
                , "oColVis": {
                    "aiExclude": [ 0, 23, 25, 27 ]
                    , "buttonText": "Spalten"
                }

                , "fnDrawCallback": function() {
                    x = 1;
                }
                , "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {

                    cols = {};
                    vcols = {};
                    vcol = -1;
                    for (i = 0; i < dgLeads.fnSettings().aoColumns.length; i++) {
                        cols[dgLeads.fnSettings().aoColumns[i].sName] = i;
                        if (dgLeads.fnSettings().aoColumns[i].bVisible) {
                            vcol = vcol + 1;
                            vcols[dgLeads.fnSettings().aoColumns[i].sName] = vcol;
                        }
                    }
                    $(nRow).data('dgData', aData);
                    var dgData = aData;
                    
                    $('td', nRow).eq(cols.action).html('<a href=""><img src="/img/icon/info.png" alt="" title="bearbeiten" class="icon" /></a>');
                    $('td', nRow).removeClass('editable');
                    
                    // toggle affiliate status
                    $('td', nRow).eq(vcols.stateIDAffiliate).html('');
                    if (aData[cols.stateIDAffiliate] == null || aData[cols.stateIDAffiliate] == '') { aimg1 = 0; aimg2 = 0;}
                    else if (aData[cols.stateIDAffiliate] == 0) { aimg1 = 1; aimg2 = 0;}
                    else if (aData[cols.stateIDAffiliate] == 1) { aimg1 = 0; aimg2 = 1;}
                    $('td', nRow).eq(vcols.stateIDAffiliate).data('stateID', aData[cols.stateIDAffiliate]);
                    bimg1 = $('<img src="/img/icon/false'+aimg1+'.png" alt="" title="stornieren" class="icon" />');
                    bimg2 = $('<img src="/img/icon/true'+aimg2+'.png" alt="" title="freigeben" class="icon" />');
                    if (aData[cols.invoiceIDAffiliate].length == 0) {
                        bimg1.click(function (event) {
                            newState = null;
                            if ($(this).parent().data('stateID') == 1) newState = 0;
                            else if ($(this).parent().data('stateID') === null || $(this).parent().data('stateID') === '') newState = 0;
                            else if ($(this).parent().data('stateID') == 0) newState = null;
                            param = [$('td', $(this).parent().parent()).eq(0).text(), newState];
                            foo = rpc('setLeadStatusAffiliate', [{"type":"var","name":"","data":JSON.stringify(param[0])}, {"type":"var","name":"","data":JSON.stringify(param[1])}]);
                            dgLeads.fnDraw();
                        }).css('cursor', 'pointer');
                        bimg2.click(function (event) {
                            newState = ($(this).parent().data('stateID') == 1) ? null : 1;
                            param = [$('td', $(this).parent().parent()).eq(0).text(), newState];
                            foo = rpc('setLeadStatusAffiliate', [{"type":"var","name":"","data":JSON.stringify(param[0])}, {"type":"var","name":"","data":JSON.stringify(param[1])}]);
                            dgLeads.fnDraw();
                        }).css('cursor', 'pointer');
                        $('td', nRow).eq(vcols.commissionAffiliate).editable( 
                            function (val) {
                                param = [$('td', $(this).parent()).eq(0).text(), val];
                                foo = rpc('setLeadCommissionAffiliate', [{"type":"var","name":"","data":JSON.stringify(param[0])}, {"type":"var","name":"","data":JSON.stringify(param[1])}]);
                                dgLeads.fnDraw();
                            }, {"height": "12px"}
                        ).addClass('editable');
                        $('td', nRow).eq(vcols.causeOfCancelationAffiliate).editable( 
                            function (val) {
                                param = [$('td', $(this).parent()).eq(0).text(), val];
                                foo = rpc('setLeadCauseOfCancelationAffiliate', [{"type":"var","name":"","data":JSON.stringify(param[0])}, {"type":"var","name":"","data":JSON.stringify(param[1])}]);
                                dgLeads.fnDraw();
                            }, {height: "12px", placeholder:'' }
                        ).addClass('editable');
                    }
                    $('td', nRow).eq(vcols.stateIDAffiliate).append(bimg1, bimg2);

                    // toggle merchant status
                    $('td', nRow).eq(vcols.stateIDMerchant).html('');
                    if (aData[cols.stateIDMerchant] == null || aData[cols.stateIDMerchant] == '') { aimg1 = 0; aimg2 = 0;}
                    else if (aData[cols.stateIDMerchant] == 0) { aimg1 = 1; aimg2 = 0;}
                    else if (aData[cols.stateIDMerchant] == 1) { aimg1 = 0; aimg2 = 1;}
                    $('td', nRow).eq(vcols.stateIDMerchant).data('stateID', aData[cols.stateIDMerchant]);
                    bimg1 = $('<img src="/img/icon/false'+aimg1+'.png" alt="" title="stornieren" class="icon" />');
                    bimg2 = $('<img src="/img/icon/true'+aimg2+'.png" alt="" title="freigeben" class="icon" />');
                    if (aData[cols.invoiceIDMerchant].length == 0) {
                        bimg1.click(function (event) {
                            newState = null;
                            if ($(this).parent().data('stateID') == 1) newState = 0;
                            else if ($(this).parent().data('stateID') === null || $(this).parent().data('stateID') === '') newState = 0;
                            else if ($(this).parent().data('stateID') == 0) newState = null;
                            param = [$('td', $(this).parent().parent()).eq(0).text(), newState];
                            foo = rpc('setLeadStatusMerchant', [{"type":"var","name":"","data":JSON.stringify(param[0])}, {"type":"var","name":"","data":JSON.stringify(param[1])}]);
                            dgLeads.fnDraw();
                        }).css('cursor', 'pointer');
                        bimg2.click(function (event) {
                            newState = ($(this).parent().data('stateID') == 1) ? null : 1;
                            param = [$('td', $(this).parent().parent()).eq(0).text(), newState];
                            foo = rpc('setLeadStatusMerchant', [{"type":"var","name":"","data":JSON.stringify(param[0])}, {"type":"var","name":"","data":JSON.stringify(param[1])}]);
                            dgLeads.fnDraw();
                        }).css('cursor', 'pointer');
                        $('td', nRow).eq(vcols.commissionMerchant).editable( 
                            function (val) {
                                param = [$('td', $(this).parent()).eq(0).text(), val];
                                foo = rpc('setLeadCommissionMerchant', [{"type":"var","name":"","data":JSON.stringify(param[0])}, {"type":"var","name":"","data":JSON.stringify(param[1])}]);
                                dgLeads.fnDraw();
                            }, {"height": "12px"}
                        ).addClass('editable');
                        $('td', nRow).eq(vcols.causeOfCancelationMerchant).editable( 
                            function (val) {
                                param = [$('td', $(this).parent()).eq(0).text(), val];
                                foo = rpc('setLeadCauseOfCancelationMerchant', [{"type":"var","name":"","data":JSON.stringify(param[0])}, {"type":"var","name":"","data":JSON.stringify(param[1])}]);
                                dgLeads.fnDraw();
                            }, {"height": "12px", placeholder:'', selectText:true}
                        ).addClass('editable');
                    }
                    $('td', nRow).eq(vcols.stateIDMerchant).append(bimg1, bimg2);
                    


                    img1 = $('<img src="/img/icon/info.png" alt="" title="anzeigen" class="icon" />').click(function (event) {
                        param = getDBO('ParamGetLead');
                        param.leadID = $('td:eq(0)', $(this).parent().parent()).text();
                        foo = rpc('getLead', [{"type":"object","name":"ParamGetLead","data":JSON.stringify(param)}]);
                        dialogInfoLead(foo);
                    });
                    $('td', nRow).eq(vcols.action).append(img1);

                    return nRow;
                }
                , "aoColumns": [ 
                    { "sName": "leadID", "sClass": "leadID tright" }
                    , { "sName": "dateCreation", "sClass": "dateCreation" }
                    , { "sName": "campaignName", "sClass": "campaignName" }
                    , { "sName": "company", "sClass": "company", "bVisible":false, "bSortable": false }
                    , { "sName": "sex", "sClass": "sex", "bVisible":false, "bSortable": false }
                    , { "sName": "title", "sClass": "title", "bVisible":false, "bSortable": false }
                    , { "sName": "firstname", "sClass": "firstname", "bVisible":false, "bSortable": false }
                    , { "sName": "lastname", "sClass": "lastname", "bVisible":false, "bSortable": false }
                    , { "sName": "street", "sClass": "street", "bVisible":false, "bSortable": false }
                    , { "sName": "streetNo", "sClass": "streetNo", "bVisible":false, "bSortable": false }
                    , { "sName": "zip", "sClass": "zip", "bVisible":false, "bSortable": false }
                    , { "sName": "town", "sClass": "town", "bVisible":false, "bSortable": false }
                    , { "sName": "countryID", "sClass": "countryID", "bVisible":false, "bSortable": false }
                    , { "sName": "phone", "sClass": "phone", "bVisible":false, "bSortable": false }
                    , { "sName": "mobile", "sClass": "mobile", "bVisible":false, "bSortable": false }
                    , { "sName": "fax", "sClass": "fax", "bVisible":false, "bSortable": false }
                    , { "sName": "email", "sClass": "email", "bVisible":false, "bSortable": false }
                    , { "sName": "homepage", "sClass": "homepage", "bVisible":false, "bSortable": false }
                    , { "sName": "affiliateName", "sClass": "affiliateName" }
                    , { "sName": "commissionAffiliate", "sClass": "commissionAffiliate tright" }
                    , { "sName": "commissionMerchant", "sClass": "commissionMerchant tright" }
                    , { "sName": "invoiceIDAffiliate", "sClass": "invoiceIDAffiliate tright", "bSortable": false }
                    , { "sName": "invoiceIDMerchant", "sClass": "invoiceIDMerchant tright", "bSortable": false }
                    , { "sName": "stateIDAffiliate", "sClass": "stateIDAffiliate tright", "bSortable": false }
                    , { "sName": "causeOfCancelationAffiliate", "sClass": "causeOfCancelationAffiliate", "bVisible":false, "bSortable": false }
                    , { "sName": "stateIDMerchant", "sClass": "stateIDMerchant tright", "bSortable": false }
                    , { "sName": "causeOfCancelationMerchant", "sClass": "causeOfCancelationMerchant", "bVisible":false, "bSortable": false }
                    , { "sName": "action", "sClass": "actions tright", "bSortable": false }
                ]
                , "fnServerData": function ( sSource, aoData, fnCallback ) {
                    aoData.push( { "name": "leadStateAffiliate", "value": $('#filterLeadStateAffiliate').val() } );
                    aoData.push( { "name": "leadStateMerchant", "value": $('#filterLeadStateMerchant').val() } );
                    aoData.push( { "name": "billedAffiliate", "value": $('#filterLeadBilledAffiliate').val() } );
                    aoData.push( { "name": "billedMerchant", "value": $('#filterLeadBilledMerchant').val() } );
                    aoData.push( { "name": "affiliateID", "value": $('#filterLeadAffiliateID').val() } );
                    aoData.push( { "name": "merchantID", "value": $('#filterLeadMerchantID').val() } );
                    aoData.push( { "name": "invoiceID", "value": $('#filterLeadInvoiceID').val() } );
                    aoData.push( { "name": "campaignID", "value": $('#filterLeadCampaignID').val() } );
                    aoData.push( { "name": "dateCreationStart", "value": $('#filterLeadDateCreationStart').val() } );
                    aoData.push( { "name": "dateCreationEnd", "value": $('#filterLeadDateCreationEnd').val() } );
                    $.ajax( {"dataType":'json',"type":"GET","url":sSource,"data":aoData,"success":fnCallback});
                }
            });
            $("#filterLeadAffiliateID" ).autocomplete({ delay:0, search:function(event, ui) {dgLeads.fnDraw();}, source:[], minLength:0 });
            $("#filterLeadMerchantID" ).autocomplete({ delay:0, search:function(event, ui) {dgLeads.fnDraw();}, source:[], minLength:0 });
            $("#filterLeadInvoiceID" ).autocomplete({ delay:0, search:function(event, ui) {dgLeads.fnDraw();}, source:[], minLength:0 });
            $("#filterLeadCampaignID" ).autocomplete({ delay:0, search:function(event, ui) {dgLeads.fnDraw();}, source:[], minLength:0 });
            $("#tabLeads .datePicker").autocomplete({ delay:0, search:function(event, ui) {dgLeads.fnDraw();}, source:[], minLength:0 });
            
            // CAMPAIN TABLE

            dgCampaigns = $('#dgCampaigns').dataTable({
                bJQueryUI:true
                , "aaSorting": [[0,'asc']]
                , "bAutoWidth": false
                , "sPaginationType": "full_numbers"
                , "aLengthMenu": [[10, 20, 30, 50, -1], [10, 20, 30, 50, "Alle"]]
                , "iDisplayLength": 20
                , "iCookieDuration": 60 * 60 * 24 * 365
                , "oLanguage": olang
                , "sDom": '<"colButtun"C><"H"lfr>t<"F"ip>'
                , "oColVis": {
                    "aiExclude": [ 0, 1, 8 ]
                    , "buttonText": "Spalten"
                }
                , "bProcessing": true
                , "bServerSide": true
                , "bStateSave": true
                , "sAjaxSource": "/ajax.php?table=campaigns"
                , "fnDrawCallback": function() {
                    x = 1;
                }
                , "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                    
                    cols = {}; vcols = {}; vcol = -1;
                    for (i = 0; i < dgCampaigns.fnSettings().aoColumns.length; i++) {
                        cols[dgCampaigns.fnSettings().aoColumns[i].sName] = i;
                        if (dgCampaigns.fnSettings().aoColumns[i].bVisible) {
                            vcol = vcol + 1;
                            vcols[dgCampaigns.fnSettings().aoColumns[i].sName] = vcol;
                        }
                    }
                    $(nRow).data('dgData', aData);
                    var dgData = aData;
                    
                    aimg = false;
                    if (aData[1] == 3) { aimg = 'clockon.png'; }
                    else if (aData[1] == 2) { aimg = 'clockrun.gif'; }
                    else if (aData[1] == 1) { aimg = 'clockoff.png'; }
                    if (aimg) $('td:eq(1)', nRow).html('<img src="/img/icon/'+aimg+'" alt="" title="" class="icon" />');
                    else $('td:eq(1)', nRow).html('');
                    
                    img1 = $('<img src="/img/icon/browse.png" alt="" title="Menu" class="icon" />');
                    img1.jjmenu("click", 
                            [
                             {title:"Affiliates", action:{type:"fn",callback:'(function () { filterAffiliate({filterAffiliateCampaignID:'+dgData[0]+'});})' }},
                             {title:"Leads", action:{type:"fn",callback:'(function () { filterLead({filterLeadCampaignID:'+dgData[0]+'});})' }},
                             {title:"Werbemittel", action:{type:"fn",callback:'(function () { filterBanner({filterBannerCampaignID:'+dgData[0]+'});})' }},
                             {title:"Vergütungen", action:{type:"fn",callback:'(function () { filterCommission({filterCommissionCampaignID:'+dgData[0]+'});})' }}
                           ]
                    );
                    img4 = $('<img src="/img/icon/edit.png" alt="" title="bearbeiten" class="icon" />').click(function (event) {
                        param = getDBO('ParamGetCampaign');
                        param.campaignID = $('td:eq(0)', $(this).parent().parent()).text();
                        foo = rpc('getCampaign', [{"type":"object","name":"ParamGetCampaign","data":JSON.stringify(param)}]);
                        dialogEditCampaign(foo);
                    });
                    img3 = $('<img src="/img/icon/info.png" alt="" title="anzeigen" class="icon" />').click(function (event) {
                        showCampaign($('td:eq(0)', $(this).parent().parent()).text());
                    });
                    img2 = $('<img src="/img/icon/calendar.png" alt="" title="Kalender" class="icon" />').click(function (event) {
                        dialogEditCampaignTimeFrame($('td:eq(0)', $(this).parent().parent()).text());
                    });
                    
                    
                    if (aData[cols.stateID] == 1) $('td', nRow).eq(vcols.stateID).text('aktiv');
                    else $('td', nRow).eq(vcols.stateID).text('inaktiv');
                    
                    $('td', nRow).eq(vcols.action).append(img1, img2, img3, img4); //, img1, img2

                    return nRow;
                }
                , "aoColumns": [ 
                    { "sName": "campaignID", "sClass": "campaignID tright" }
                    , { "sName": "running", "sClass": "running", "bSortable": false }
                    , { "sName": "name", "sClass": "name", "sWidth":"200px" }
                    , { "sName": "merchantName", "sClass": "merchantName", "sWidth":"200px" }
                    , { "sName": "commissionAffiliate", "sClass": "commissionAffiliate tright" }
                    , { "sName": "commissionMerchant", "sClass": "commissionMerchant tright" }
                    , { "sName": "cancelationDays", "sClass": "cancelationDays tright", "bSortable": false }
                    , { "sName": "stateID", "sClass": "stateID tright" }
                    , { "sName": "action", "sClass": "actions tright", "bSortable": false }
                ]
                , "fnServerData": function ( sSource, aoData, fnCallback ) {
                    aoData.push( { "name": "stateID", "value": $('#filterCampaignStatus').val() } );
                    aoData.push( { "name": "merchantID", "value": $('#filterCampaignMerchantID').val() } );
                    $.ajax( {"dataType":'json',"type":"GET","url":sSource,"data":aoData,"success":fnCallback});
                }

            });
            $("#filterCampaignMerchantID" ).autocomplete({ delay:0, search:function(event, ui) {dgCampaigns.fnDraw();}, source:[], minLength:0 });

            // COMMISSION TABLE
            
            dgCommissions = $('#dgCommissions').dataTable({
                bJQueryUI:true
                , "aaSorting": [[0,'asc']]
                , "bAutoWidth": false
                , "sPaginationType": "full_numbers"
                , "aLengthMenu": [[10, 20, 30, 50, -1], [10, 20, 30, 50, "Alle"]]
                , "iDisplayLength": 20
                , "iCookieDuration": 60 * 60 * 24 * 365
                , "oLanguage": olang
                , "sDom": '<"colButtun"C><"H"lfr>t<"F"ip>'
                , "oColVis": { "aiExclude": [ 0, 8 ], "buttonText": "Spalten", "activate": "click" }
                , "bProcessing": true
                , "bServerSide": true
                , "bStateSave": true
                , "sAjaxSource": "/ajax.php?table=commissions"
                , "fnDrawCallback": function() {
                    x = 1;
                }
                , "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                    cols = {};
                    vcols = {};
                    vcol = -1;
                    for (i = 0; i < dgCommissions.fnSettings().aoColumns.length; i++) {
                        cols[dgCommissions.fnSettings().aoColumns[i].sName] = i;
                        if (dgCommissions.fnSettings().aoColumns[i].bVisible) {
                            vcol = vcol + 1;
                            vcols[dgCommissions.fnSettings().aoColumns[i].sName] = vcol;
                        }
                    }
                    $(nRow).data('dgData', aData);
                    var dgData = aData;
                    img = $('<img src="/img/icon/false1.png" alt="" title="löschen" class="icon" />').click(function (event) {
                        foo = rpc('removeCommission', [{"type":"var","name":"commissionID","data":$('td:eq(0)', $(this).parent().parent()).text()}]);
                        if (foo === true) {
                            dgCommissions.fnDraw();
                            dgAffiliates.fnDraw();
                        }
                        else alert('Der Datensatz konnte nicht gelöscht werden.');
                    });
                    $('td', nRow).eq(vcols.action).append(img);
                    
                    $('td', nRow).eq(vcols.value).editable( 
                            function (val) {
                                param = [$('td', $(this).parent()).eq(0).text(), val];
                                foo = rpc('setCommission', [{"type":"var","name":"","data":param[0]}, {"type":"var","name":"","data":param[1]}]);
                                dgCommissions.fnDraw();
                            }, {"height": "12px"}
                        ).addClass('editable');

                    return nRow;
                }
                , "aoColumns": [ 
                    { "sName": "commissionID", "sClass": "commissionID tright" }
                    , { "sName": "campaignName", "sClass": "campaignName" }
                    , { "sName": "campaignID", "sClass": "campaignID tright", "bSortable": false, "bVisible": false }
                    , { "sName": "affiliateName", "sClass": "affiliateName" }
                    , { "sName": "affiliateID", "sClass": "affiliateID tright", "bSortable": false, "bVisible": false }
                    , { "sName": "merchantName", "sClass": "merchantName", "bSortable": false, "bVisible": false }
                    , { "sName": "merchantID", "sClass": "merchantID tright", "bSortable": false, "bVisible": false }
                    , { "sName": "value", "sClass": "value tright" }
                    , { "sName": "action", "sClass": "actions tright", "bSortable": false }
                ]
                , "fnServerData": function ( sSource, aoData, fnCallback ) {
                    aoData.push( { "name": "campaignID", "value": $('#filterCommissionCampaignID').val() } );
                    aoData.push( { "name": "affiliateID", "value": $('#filterCommissionAffiliateID').val() } );
                    aoData.push( { "name": "merchantID", "value": $('#filterCommissionMerchantID').val() } );
                    $.ajax( {"dataType":'json',"type":"GET","url":sSource,"data":aoData,"success":fnCallback});
                }

            });
            $("#filterCommissionCampaignID").autocomplete({ delay:0, search:function(event, ui) {dgCommissions.fnDraw();}, source:[], minLength:0 });
            $("#filterCommissionAffiliateID").autocomplete({ delay:0, search:function(event, ui) {dgCommissions.fnDraw();}, source:[], minLength:0 });
            $("#filterCommissionMerchantID").autocomplete({ delay:0, search:function(event, ui) {dgCommissions.fnDraw();}, source:[], minLength:0 });
            
            // INVOICE TABLE

            dgInvoices = $('#dgInvoices').dataTable({
                bJQueryUI:true
                , "aaSorting": [[0,'asc']]
                , "sPaginationType": "full_numbers"
                , "aLengthMenu": [[10, 20, 30, 50, -1], [10, 20, 30, 50, "Alle"]]
                , "iDisplayLength": 20
                , "iCookieDuration": 60 * 60 * 24 * 365
                , "oLanguage": olang
                , "sDom": '<"colButtun"C><"H"lfr>t<"F"ip>'
                , "oColVis": { "aiExclude": [ 0, 2, 8 ], "buttonText": "Spalten", "activate": "click" }
                , "bProcessing": true
                , "bServerSide": true
                , "bStateSave": true
                , "sAjaxSource": "/ajax.php?table=invoices"
                , "fnDrawCallback": function() {
                    x = 1;
                }
                , "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                    cols = {};
                    vcols = {};
                    vcol = -1;
                    for (i = 0; i < dgInvoices.fnSettings().aoColumns.length; i++) {
                        cols[dgInvoices.fnSettings().aoColumns[i].sName] = i;
                        if (dgInvoices.fnSettings().aoColumns[i].bVisible) {
                            vcol = vcol + 1;
                            vcols[dgInvoices.fnSettings().aoColumns[i].sName] = vcol;
                        }
                        $(nRow).data(dgInvoices.fnSettings().aoColumns[i].sName, aData[i]);
                    }
                    $(nRow).data('dgData', aData);
                    var dgData = aData;
                    
                    img1 = $('<img src="/img/icon/pdf.png" alt="" title="download" class="icon" />').css('cursor', 'pointer').click(function (event) {
                        getFileInvoicePDF( $('td:eq(0)', $(this).parent().parent()).text() );
                    });
                    img2 = $('<img src="/img/icon/excel.gif" alt="" title="csv export" class="icon" />').css('cursor', 'pointer').click(function (event) {
                        getFileInvoiceCSV( $('td:eq(0)', $(this).parent().parent()).text() );
                    });
                    $('td', nRow).eq(vcols.action).append(img1, img2);
                    return nRow;
                }
                , "aoColumns": [ 
                    { "sName": "invoiceID", "sClass": "invoiceID tright" }
                    , { "sName": "dateCreation", "sClass": "dateCreation" }
                    , { "sName": "recipientType", "sClass": "recipientType", "bSortable": false }
                    , { "sName": "recipientName", "sClass": "recipientName" }
                    , { "sName": "goodCount", "sClass": "goodCount tright" }
                    , { "sName": "canceledCount", "sClass": "canceledCount tright" }
                    , { "sName": "valueNet", "sClass": "valueNet tright" }
                    , { "sName": "valueGross", "sClass": "valueGross tright" }
                    , { "sName": "action", "sClass": "actions tright", "bSortable": false }
                ]
                , "fnServerData": function ( sSource, aoData, fnCallback ) {
                    aoData.push( { "name": "recipient", "value": $('#filterInvoiceRecipient').val() } );
                    $.ajax( {"dataType":'json',"type":"GET","url":sSource,"data":aoData,"success":fnCallback});
                }

            });
            $("#filterInvoiceAffiliateID" ).autocomplete({ delay:0, search:function(event, ui) {dgInvoices.fnDraw();}, source:[], minLength:0 });
            $("#filterInvoiceMerchantID" ).autocomplete({ delay:0, search:function(event, ui) {dgInvoices.fnDraw();}, source:[], minLength:0 });

            // BANNER TABLE

            dgBanner = $('#dgBanner').dataTable({
                bJQueryUI:true
                , "aaSorting": [[0,'asc']]
                , "sPaginationType": "full_numbers"
                , "bAutoWidth": false
                , "aLengthMenu": [[10, 20, 30, 50, -1], [10, 20, 30, 50, "Alle"]]
                , "iDisplayLength": 20
                , "iCookieDuration": 60 * 60 * 24 * 365
                , "oLanguage": olang
                , "sDom": '<"colButtun"C><"H"lfr>t<"F"ip>'
                , "oColVis": { "buttonText": "Spalten", "activate": "click" }
                , "bProcessing": true
                , "bServerSide": true
                , "bStateSave": true
                , "sAjaxSource": "/ajax.php?table=banner"
                
                /* , "sScrollX": "100%"
                , "bScrollCollapse": true
                */
                , "oColVis": { "aiExclude": [ 0, 8 ], "buttonText": "Spalten"}
                , "fnDrawCallback": function() { x=1; }
                , "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                    
                    cols = {}; vcols = {}; vcol = -1;
                    for (i = 0; i < dgBanner.fnSettings().aoColumns.length; i++) {
                        cols[dgBanner.fnSettings().aoColumns[i].sName] = i;
                        if (dgBanner.fnSettings().aoColumns[i].bVisible) {
                            vcol = vcol + 1;
                            vcols[dgBanner.fnSettings().aoColumns[i].sName] = vcol;
                        }
                    }
                    $(nRow).data('dgData', aData);
                    var dgData = aData;
                    
                    if (aData[cols.stateID] == 1) $('td', nRow).eq(vcols.stateID).text('aktiv');
                    else $('td', nRow).eq(vcols.stateID).text('inaktiv');
                    
                    img1 = $('<img src="/img/icon/info.png" alt="" title="anzeigen" class="icon" />').click(function (event) {
                        dialogInfoBanner($('td:eq(0)', $(this).parent().parent()).text());
                    });
                    img2 = $('<img src="/img/icon/edit.png" alt="" title="bearbeiten" class="icon" />').click(function (event) {
                        dialogEditBanner($('td:eq(0)', $(this).parent().parent()).text());
                    });
                    $('td', nRow).eq(vcols.action).append(img1, img2);
                    
                    $('td', nRow).removeClass('editable');
                    return nRow;
                }
                , "aoColumns": [ 
                    { "sName": "bannerID", "sClass": "bannerID tright" }
                    , { "sName": "name", "sClass": "name" }
                    , { "sName": "campaignID", "sClass": "campaignID tright", "bSortable": false, "bVisible":false }
                    , { "sName": "campaignName", "sClass": "campaignName" }
                    , { "sName": "type", "sClass": "type" }
                    , { "sName": "width", "sClass": "width tright" }
                    , { "sName": "height", "sClass": "height tright" }
                    , { "sName": "stateID", "sClass": "stateID", "bSortable": false }
                    , { "sName": "action", "sClass": "actions tright", "bSortable": false }
                ]
                , "fnServerData": function ( sSource, aoData, fnCallback ) {
                    aoData.push( { "name": "stateID", "value": $('#filterBannerStatus').val() } );
                    aoData.push( { "name": "campaignID", "value": $('#filterBannerCampaignID').val() } );
                    $.ajax( {"dataType":'json',"type":"GET","url":sSource,"data":aoData,"success":fnCallback});
                }
            });
            $("#filterBannerCampaignID").autocomplete({ search:function(event, ui) {dgBanner.fnDraw();}, source:[], minLength:0 });
            $("#filterBannerMerchantID").autocomplete({ search:function(event, ui) {dgBanner.fnDraw();}, source:[], minLength:0 });


            // INVOICE OPEN

            dgInvoicesOpen = $('#dgInvoicesOpen').dataTable({

                
                bJQueryUI:true
                , "aaSorting": [[0,'asc']]
                , "sPaginationType": "full_numbers"
                , "bAutoWidth": false
                , "aLengthMenu": [[10, 20, 30, 50, -1], [10, 20, 30, 50, "Alle"]]
                , "iDisplayLength": 20
                , "iCookieDuration": 60 * 60 * 24 * 365
                , "oLanguage": olang
                , "sDom": '<"colButtun"C><"H"lfr>t<"F"ip>'
                , "oColVis": {
                    "aiExclude": [ 0, 1, 7 ]
                    , "buttonText": "Spalten"
                }
                , "bProcessing": true
                , "bServerSide": true
                , "bStateSave": true
                , "sAjaxSource": "/ajax.php?table=invoicesOpen"
                , "fnDrawCallback": function() {
                    x = 1;
                }
                , "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                    var cols = {};
                    var vcols = {};
                    var vcol = -1;
                    for (i = 0; i < dgInvoicesOpen.fnSettings().aoColumns.length; i++) {
                        cols[dgInvoicesOpen.fnSettings().aoColumns[i].sName] = i;
                        if (dgInvoicesOpen.fnSettings().aoColumns[i].bVisible) {
                            vcol = vcol + 1;
                            vcols[dgInvoicesOpen.fnSettings().aoColumns[i].sName] = vcol;
                        }
                        $(nRow).data(dgInvoicesOpen.fnSettings().aoColumns[i].sName, aData[i]);
                    }
                    $(nRow).data('dgData', aData);
                    var dgData = aData;


                    $('td', nRow).eq(vcols.action).html('<img src="/img/icon/pdf.png" alt="" title="Rechnung erstellen" class="icon" />').css('cursor', 'pointer').click(function (event) {
                        dialogInsertInvoice(
                            $(this).parent().data('recipientType')
                            , $(this).parent().data('recipientID')
                            , $('#filterInvoiceOpenDateCreationEnd').val()
                            , $(this).parent().data('recipientName')
                        );
                    });

                    return nRow;
                }
                , "aoColumns": [ 
                    { "sName": "recipientType", "sClass": "recipientType tright" }
                    , { "sName": "recipientID", "sClass": "recipientID tright" }
                    , { "sName": "recipientName", "sClass": "recipientName" }
                    , { "sName": "goodCount", "sClass": "goodCount tright" }
                    , { "sName": "canceledCount", "sClass": "canceledCount tright" }
                    , { "sName": "valueNet", "sClass": "net tright" }
                    , { "sName": "valueGross", "sClass": "valueGross tright" }
                    , { "sName": "action", "sClass": "actions tright", "bSortable": false }
                ]
                , "fnServerData": function ( sSource, aoData, fnCallback ) {
                    if ($('#filterInvoiceOpenRecipient').val() == 'M') {
                        aoData.push( { "name": "recipient", "value": $('#filterInvoiceOpenRecipient').val() } );
                        aoData.push( { "name": "merchantID", "value": $('#filterInvoiceOpenID').val() } );
                    }
                    else if ($('#filterInvoiceOpenRecipient').val() == 'A') {
                        aoData.push( { "name": "recipient", "value": $('#filterInvoiceOpenRecipient').val() } );
                        aoData.push( { "name": "affiliateID", "value": $('#filterInvoiceOpenID').val() } );
                    }
                    aoData.push( { "name": "dateCreationEnd", "value": $('#filterInvoiceOpenDateCreationEnd').val() } );
                    $.ajax( {"dataType":'json',"type":"GET","url":sSource,"data":aoData,"success":fnCallback});
                }
            });
            $("#filterInvoiceOpenID" ).autocomplete({ delay:0, search:function(event, ui) {dgInvoicesOpen.fnDraw();}, source:[], minLength:0 });
            $("#tabNewInvoice .datePicker").autocomplete({ delay:0, search:function(event, ui) {dgInvoicesOpen.fnDraw();}, source:[], minLength:0 });

            
            
        });



