function runBreakdown($user_pref)
{
    $database = DB::getInstance();
    $db = $database->getConnection();
    //grab time
    $time = grab_timeframe();
    //get breakdown pref
    $mysql['user_id'] = $db->real_escape_string($_SESSION['user_id']);
    $user_sql = "\n\t\t\tSELECT\n\t\t\t\tuser_time_register,\n\t\t\t\tuser_pref_breakdown,\n\t\t\t\tuser_pref_chart,\n\t\t\t\tuser_pref_show\n\t\t\tFROM\n\t\t\t\t202_users\n\t\t\t\tLEFT JOIN 202_users_pref USING (user_id) \n\t\t\tWHERE\n\t\t\t\t202_users.user_id='" . $mysql['user_id'] . "'";
    $user_result = _mysqli_query($user_sql);
    //($user_sql);
    $user_row = $user_result->fetch_assoc();
    if ($user_row['user_pref_show'] == 'all') {
        $click_flitered = '';
    }
    if ($user_row['user_pref_show'] == 'real') {
        $click_filtered = " AND click_filtered='0' ";
    }
    if ($user_row['user_pref_show'] == 'filtered') {
        $click_filtered = " AND click_filtered='1' ";
    }
    if ($user_row['user_pref_show'] == 'filtered_bot') {
        $click_filtered = " AND click_bot='1' ";
    }
    if ($user_row['user_pref_show'] == 'leads') {
        $click_filtered = " AND click_lead='1' ";
    }
    //breakdown should be hour, day, month, or year.
    $breakdown = $user_row['user_pref_breakdown'];
    $pref_chart = $user_row['user_pref_chart'];
    //first delete old report
    $breakdown_sql = "\n\t\t\tDELETE\n\t\t\tFROM 202_sort_breakdowns\n\t\t\tWHERE user_id='" . $mysql['user_id'] . "'\n\t\t";
    $breakdown_result = _mysqli_query($breakdown_sql);
    //($breakdown_sql);
    //find where to start from.
    $start = $time['from'];
    $end = $time['to'];
    //make sure the start isn't past this users registration time, and likewise, make sure END isn't past today, else theses will try to grab reports for dates that do not exists slowing down mysql doing reports for nothing.
    if ($user_row['user_time_register'] > $start) {
        $start = $user_row['user_time_register'];
    }
    if (time() < $end) {
        $end = time();
    }
    $x = 0;
    while ($end > $start) {
        if ($breakdown == 'hour') {
            $from = mktime(date('G', $end), 0, 0, date('m', $end), date('d', $end), date('y', $end));
            $to = mktime(date('G', $end), 59, 59, date('m', $end), date('d', $end), date('y', $end));
            $end = $end - 3600;
        } elseif ($breakdown == 'day') {
            $from = mktime(0, 0, 0, date('m', $end), date('d', $end), date('y', $end));
            $to = mktime(23, 59, 59, date('m', $end), date('d', $end), date('y', $end));
            $end = $end - 86400;
        } elseif ($breakdown == 'month') {
            $from = mktime(0, 0, 0, date('m', $end), 1, date('y', $end));
            $to = mktime(23, 59, 59, date('m', $end), @getLastDayOfMonth(date('m', $end)), date('y', $end));
            $end = $end - 2629743;
        } elseif ($breakdown == 'year') {
            $from = mktime(0, 0, 0, 1, 1, date('y', $end));
            $to = mktime(23, 59, 59, @getLastDayOfMonth(date('m', $end)), 1, 12, date('y', $end));
            $end = $end - 31556926;
        }
        $mysql['from'] = $db->real_escape_string($from);
        $mysql['to'] = $db->real_escape_string($to);
        //build query
        $command = "\n\t\t\tSELECT\n\t\t\tCOUNT(*) AS clicks,\n\t\t\t\tAVG(2c.click_cpc) AS avg_cpc,\n\t\t\t\tSUM(2cr.click_out) AS click_throughs,\n\t\t\t\tSUM(2c.click_lead) AS leads,\n\t\t\t\tSUM(2c.click_payout*2c.click_lead) AS income\n\t\t\tFROM\n\t\t\t\t202_clicks AS 2c\n\t\t\t\tLEFT JOIN 202_clicks_record AS 2cr ON 2cr.click_id = 2c.click_id\n\t\t";
        $db_table = "2c";
        $pref_time = false;
        if ($user_pref == true) {
            $pref_adv = true;
            $command = $command . "\n\t\t\t\tLEFT JOIN 202_clicks_advance AS 2ca ON (2ca.click_id = 2c.click_id)\n\t\t\t\tLEFT JOIN 202_clicks_site AS 2cs ON (2cs.click_id = 2c.click_id)\n\t\t\t";
        } else {
            $pref_adv = false;
        }
        $command = $command . "\n\t\t\tLEFT JOIN 202_aff_campaigns AS 2ac ON (2c.aff_campaign_id = 2ac.aff_campaign_id)\n\t\t\tLEFT JOIN 202_aff_networks AS 2an ON (2an.aff_network_id = 2ac.aff_network_id)\n\t\t";
        $pref_show = false;
        $pref_order = "\n\t\t\tAND (2c.click_alp = '1' OR (2ac.aff_campaign_deleted='0' AND 2an.aff_network_deleted='0'))\n\t\t\t{$click_filtered}\n\t\t\tAND 2c.click_time > " . $mysql['from'] . "\n\t\t\tAND 2c.click_time <= " . $mysql['to'] . "\n\t\t";
        $offset = false;
        $pref_limit = false;
        $count = false;
        $query = query($command, $db_table, $pref_time, $pref_adv, $pref_show, $pref_order, $offset, $pref_limit, $count);
        $click_sql = $query['click_sql'];
        global $memcacheWorking, $memcache;
        if (!user_cache_time($mysql['user_id'])) {
            $click_result = _mysqli_query($click_sql);
            //($click_sql);
            $click_row = $click_result->fetch_assoc();
        } else {
            if ($memcacheWorking) {
                $getCache = $memcache->get(md5($click_sql . systemHash()));
                if ($getCache) {
                    $click_row = unserialize($getCache);
                } else {
                    $click_result = _mysqli_query($click_sql);
                    $fetchArray = $click_result->fetch_assoc();
                    $setCache = $memcache->set(md5($click_sql . systemHash()), serialize($fetchArray), false, user_cache_time($mysql['user_id']));
                    $click_row = $fetchArray;
                }
            } else {
                $click_result = _mysqli_query($click_sql);
                //($click_sql);
                $click_row = $click_result->fetch_assoc();
            }
        }
        //for AVG Payouts
        $payouts_sql = "\n\t\t\tSELECT\n\t\t\t\tSUM(2c.click_payout) AS payout\n\t\t\tFROM 202_clicks AS 2c\n\t\t\tWHERE 2c.click_lead='1'\n\t\t\tAND 2c.click_time > " . $mysql['from'] . "\n\t\t\tAND 2c.click_time <= " . $mysql['to'] . "\n\t\t";
        $payout_result = _mysqli_query($payouts_sql);
        //($breakdown_sql);
        $payout_row = $payout_result->fetch_assoc();
        //get the stats
        $clicks = 0;
        $clicks = $click_row['clicks'];
        $total_clicks = $total_clicks + $clicks;
        //click throughs
        $click_throughs = $click_row['click_throughs'];
        $total_click_throughs = $total_click_throughs + $click_throughs;
        //LP CTR
        $ctr = @round($click_throughs / $clicks * 100, 2);
        $total_ctr = @round($total_click_throughs / $total_clicks * 100, 2);
        //avg cpc and cost
        $avg_cpc = 0;
        $avg_cpc = $click_row['avg_cpc'];
        $cost = 0;
        $cost = $clicks * $avg_cpc;
        $total_cost = $total_cost + $cost;
        $total_avg_cpc = @round($total_cost / $total_clicks, 5);
        //leads
        $leads = 0;
        $leads = $click_row['leads'];
        $total_leads = $total_leads + $leads;
        //signup ratio
        $su_ratio - 0;
        $su_ratio = @round($leads / $clicks * 100, 2);
        $total_su_ratio = @round($total_leads / $total_clicks * 100, 2);
        $payout = 0;
        //current payout
        if ($click_row['leads'] > 0) {
            $payout = $payout_row['payout'] / $leads;
        }
        //income
        $income = 0;
        $income = $click_row['income'];
        $total_income = $total_income + $income;
        //grab the EPC
        $epc = 0;
        $epc = @round($income / $clicks, 2);
        $total_epc = @round($total_income / $total_clicks, 2);
        //net income
        $net = 0;
        $net = $income - $cost;
        $total_net = $total_income - $total_cost;
        //roi
        $roi = 0;
        $roi = @round($net / $cost * 100);
        $total_roi = @round($total_net / $total_cost);
        //html escape vars
        $mysql['clicks'] = $db->real_escape_string($clicks);
        $mysql['click_throughs'] = $db->real_escape_string($click_throughs);
        $mysql['ctr'] = $db->real_escape_string($ctr);
        $mysql['leads'] = $db->real_escape_string($leads);
        $mysql['su_ratio'] = $db->real_escape_string($su_ratio);
        $mysql['payout'] = $db->real_escape_string($payout);
        $mysql['epc'] = $db->real_escape_string($epc);
        $mysql['avg_cpc'] = $db->real_escape_string($avg_cpc);
        $mysql['income'] = $db->real_escape_string($income);
        $mysql['cost'] = $db->real_escape_string($cost);
        $mysql['net'] = $db->real_escape_string($net);
        $mysql['roi'] = $db->real_escape_string($roi);
        //insert chart
        $sort_breakdown_sql = "\n\t\t\tINSERT INTO\n\t\t\t\t202_sort_breakdowns\n\t\t\tSET\n\t\t\t\tsort_breakdown_from='" . $mysql['from'] . "',\n\t\t\t\tsort_breakdown_to='" . $mysql['to'] . "',\n\t\t\t\tuser_id='" . $mysql['user_id'] . "',\n\t\t\t\tsort_breakdown_clicks='" . $mysql['clicks'] . "',\n\t\t\t\tsort_breakdown_click_throughs='" . $mysql['click_throughs'] . "',\n\t\t\t\tsort_breakdown_ctr='" . $mysql['ctr'] . "',\n\t\t\t\tsort_breakdown_leads='" . $mysql['leads'] . "',\n\t\t\t\tsort_breakdown_su_ratio='" . $mysql['su_ratio'] . "',\n\t\t\t\tsort_breakdown_payout='" . $mysql['payout'] . "',\n\t\t\t\tsort_breakdown_epc='" . $mysql['epc'] . "',\n\t\t\t\tsort_breakdown_avg_cpc='" . $mysql['avg_cpc'] . "',\n\t\t\t\tsort_breakdown_income='" . $mysql['income'] . "',\n\t\t\t\tsort_breakdown_cost='" . $mysql['cost'] . "',\n\t\t\t\tsort_breakdown_net='" . $mysql['net'] . "',\n\t\t\t\tsort_breakdown_roi='" . $mysql['roi'] . "'\n\t\t";
        $sort_breakdown_result = _mysqli_query($sort_breakdown_sql);
        //($sort_breakdown_sql);
    }
    $breakdown_sql = "\n\t\tSELECT *\n\t\tFROM 202_sort_breakdowns\n\t\tWHERE user_id='" . $mysql['user_id'] . "'\n\t";
    $breakdown_result = _mysqli_query($breakdown_sql);
    //($breakdown_sql);
    $chartWidth = $_POST['chartWidth'];
    $chartHeight = 180;
    //find where to start from.
    $start = $time['from'];
    $end = $time['to'];
    //make sure the start isn't past this users registration time, and likewise, make sure END isn't past today, else theses will try to grab reports for dates that do not exists slowing down mysql doing reports for nothing.
    if ($user_row['user_time_register'] > $start) {
        $start = $user_row['user_time_register'];
    }
    if (time() < $end) {
        $end = time();
    }
    //cacluate the skip
    $x = 0;
    while ($start < $end) {
        if ($breakdown == 'hour') {
            $start = $start + 3600;
        } elseif ($breakdown == 'day') {
            $start = $start + 86400;
        } elseif ($breakdown == 'month') {
            $start = $start + 2629743;
        } elseif ($breakdown == 'year') {
            $start = $start + 31556926;
        }
        $x++;
    }
    $skip = 0;
    if ($breakdown == hour) {
        while ($x > 9) {
            $skip++;
            $x = $x - 9;
        }
    } else {
        while ($x > 14) {
            $skip++;
            $x = $x - 14;
        }
    }
    /* THIS IS A NET INCOME BAR GRAPH */
    if ($pref_chart == 'profitloss') {
        //start the PHP multi-dimensional array and create the region titles
        $chart['chart_data'][0][0] = "";
        $chart['chart_data'][1][0] = "Income";
        $chart['chart_data'][2][0] = "Cost";
        $chart['chart_data'][3][0] = "Net";
        //extract the data from the query result one row at a time
        for ($i = 0; $i < $breakdown_result->num_rows; $i++) {
            $breakdown_result->field_seek($breakdown_result->current_field);
            //determine which column in the PHP array the current data belongs to
            $row = $breakdown_result->fetch_assoc();
            $col = $row['sort_breakdown_from'];
            //populate the PHP array with the Year title
            $date = $row['sort_breakdown_from'];
            $date = date_chart($breakdown, $date);
            $chart['chart_data'][0][$col] = $date;
            //populate the PHP array with the revenue data
            $chart['chart_data'][1][$col] = $row['sort_breakdown_income'];
            $chart['chart_data'][2][$col] = $row['sort_breakdown_cost'];
            $chart['chart_data'][3][$col] = $row['sort_breakdown_net'];
        }
        $chart['series_color'] = array("70CF40", "CF4040", "409CCF", "000000");
        $chart['series_gap'] = array('set_gap' => 40, 'bar_gap' => -35);
        $chart['chart_grid_h'] = array('alpha' => 20, 'color' => "000000", 'thickness' => 1, 'type' => "solid");
        $chart['axis_value'] = array('bold' => false, 'size' => 10);
        $chart['axis_category'] = array('skip' => $skip, 'bold' => false, 'size' => 10);
        $chart['legend_label'] = array('bold' => true, 'size' => 12);
        $chart['chart_pref'] = array('line_thickness' => 1, 'point_shape' => "none", 'fill_shape' => true);
        $chart['chart_rect'] = array('x' => 40, 'y' => 20, 'width' => $chartWidth - 60, 'height' => $chartHeight);
        $chart['chart_transition'] = array('type' => "scale", 'delay' => 0.5, 'duration' => 0.5, 'order' => "series");
    } else {
        //start the PHP multi-dimensional array and create the region titles
        $chart['chart_data'][0][0] = "";
        if ($pref_chart == 'clicks') {
            $chart['chart_data'][1][0] = "Clicks";
        } elseif ($pref_chart == 'leads') {
            $chart['chart_data'][1][0] = "Leads";
        } elseif ($pref_chart == 'su_ratio') {
            $chart['chart_data'][1][0] = "Signup Ratio";
        } elseif ($pref_chart == 'payout') {
            $chart['chart_data'][1][0] = "Payout";
        } elseif ($pref_chart == 'epc') {
            $chart['chart_data'][1][0] = "EPC";
        } elseif ($pref_chart == 'cpc') {
            $chart['chart_data'][1][0] = "Avg CPC";
        } elseif ($pref_chart == 'income') {
            $chart['chart_data'][1][0] = "Income";
        } elseif ($pref_chart == 'cost') {
            $chart['chart_data'][1][0] = "Cost";
        } elseif ($pref_chart == 'net') {
            $chart['chart_data'][1][0] = "Net";
        } elseif ($pref_chart == 'roi') {
            $chart['chart_data'][1][0] = "ROI";
        }
        //extract the data from the query result one row at a time
        for ($i = 0; $i < $breakdown_result->num_rows; $i++) {
            $breakdown_result->field_seek($breakdown_result->current_field);
            //determine which column in the PHP array the current data belongs to
            $row = $breakdown_result->fetch_assoc();
            $col = $row['sort_breakdown_from'];
            //populate the PHP array with the Year title
            $date = $row['sort_breakdown_from'];
            $date = date_chart($breakdown, $date);
            $chart['chart_data'][0][$col] = $date;
            //populate the PHP array with the revenue data
            if ($pref_chart == 'clicks') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_clicks'];
            } elseif ($pref_chart == 'leads') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_leads'];
            } elseif ($pref_chart == 'su_ratio') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_su_ratio'];
            } elseif ($pref_chart == 'payout') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_payout'];
            } elseif ($pref_chart == 'epc') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_epc'];
            } elseif ($pref_chart == 'cpc') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_avg_cpc'];
            } elseif ($pref_chart == 'income') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_income'];
            } elseif ($pref_chart == 'cost') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_cost'];
            } elseif ($pref_chart == 'net') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_net'];
            } elseif ($pref_chart == 'roi') {
                $chart['chart_data'][1][$col] = $row['sort_breakdown_roi'];
            }
        }
        //$chart[ 'series_color' ] = array (  "003399");
        $chart['series_color'] = array("000000");
        $chart['chart_type'] = "Line";
        //$chart[ 'chart_transition' ] = array ( 'type'=>"dissolve", 'delay'=>.5, 'duration'=>.5, 'order'=>"series" );
        $chart['chart_grid_h'] = array('alpha' => 20, 'color' => "000000", 'thickness' => 1, 'type' => "solid");
    }
    $chart['chart_pref'] = array('line_thickness' => 1, 'point_shape' => "circle", 'fill_shape' => false);
    $chart['axis_value'] = array('bold' => false, 'size' => 10);
    $chart['axis_category'] = array('skip' => $skip, 'bold' => false, 'size' => 10);
    $chart['legend_label'] = array('bold' => true, 'size' => 12);
    $chart['chart_rect'] = array('x' => 40, 'y' => 20, 'width' => $chartWidth, 'height' => $chartHeight);
    showChart($chart, $chartWidth, $chartHeight + 40);
    ?>
<div style="padding: 3px 0px;"></div><?php 
}
function runBreakdown($user_pref)
{
    //grab time
    $time = grab_timeframe();
    //get breakdown pref
    $mysql['user_id'] = mysql_real_escape_string($_SESSION['user_id']);
    $user_sql = "SELECT \tuser_time_register,\n\t\t\t\t\t\t\t\tuser_pref_breakdown,\n\t\t\t\t\t\t\t\tuser_pref_chart,\n\t\t\t\t\t\t\t\tuser_pref_show\n\t\t\t\t\t FROM   202_users LEFT JOIN 202_users_pref USING (user_id)\n\t\t\t\t\t WHERE  202_users.user_id='" . $mysql['user_id'] . "'";
    $user_result = _mysql_query($user_sql);
    //($user_sql);
    $user_row = mysql_fetch_assoc($user_result);
    if ($user_row['user_pref_show'] == 'all') {
        $click_flitered = '';
    }
    if ($user_row['user_pref_show'] == 'real') {
        $click_filtered = " AND click_filtered='0' ";
    }
    if ($user_row['user_pref_show'] == 'filtered') {
        $click_filtered = " AND click_filtered='1' ";
    }
    if ($user_row['user_pref_show'] == 'leads') {
        $click_filtered = " AND click_lead='1' ";
    }
    //breakdown should be hour, day, month, or year.
    $breakdown = $user_row['user_pref_breakdown'];
    $pref_chart = $user_row['user_pref_chart'];
    //first delete old report
    $breakdown_sql = "DELETE FROM 202_sort_breakdowns WHERE user_id='" . $mysql['user_id'] . "'";
    $breakdown_result = _mysql_query($breakdown_sql);
    //($breakdown_sql);
    //find where to start from.
    $start = $time['from'];
    $end = $time['to'];
    //make sure the start isn't past this users registration time, and likewise, make sure END isn't past today, else theses will try to grab reports for dates that do not exists slowing down mysql doing reports for nothing.
    if ($user_row['user_time_register'] > $start) {
        $start = $user_row['user_time_register'];
    }
    if (time() < $end) {
        $end = time();
    }
    $x = 0;
    while ($end > $start) {
        if ($breakdown == 'hour' || empty($breakdown)) {
            $from = mktime(date('G', $end), 0, 0, date('m', $end), date('d', $end), date('y', $end));
            $to = mktime(date('G', $end), 59, 59, date('m', $end), date('d', $end), date('y', $end));
            $end = $end - 3600;
        } elseif ($breakdown == 'day') {
            $from = mktime(0, 0, 0, date('m', $end), date('d', $end), date('y', $end));
            $to = mktime(23, 59, 59, date('m', $end), date('d', $end), date('y', $end));
            $end = $end - 86400;
        } elseif ($breakdown == 'month') {
            $from = mktime(0, 0, 0, date('m', $end), 1, date('y', $end));
            $to = mktime(23, 59, 59, date('m', $end), @getLastDayOfMonth(date('m', $end)), date('y', $end));
            $end = $end - 2629743;
        } elseif ($breakdown == 'year') {
            $from = mktime(0, 0, 0, 1, 1, date('y', $end));
            $to = mktime(23, 59, 59, @getLastDayOfMonth(date('m', $end)), 1, 12, date('y', $end));
            $end = $end - 31556926;
        }
        $mysql['from'] = mysql_real_escape_string($from);
        $mysql['to'] = mysql_real_escape_string($to);
        //build query
        $command = "SELECT COUNT(*) AS clicks, AVG(click_cpc) AS avg_cpc, SUM(click_lead) AS leads, SUM(click_lead * click_payout) AS income FROM 202_clicks ";
        $db_table = "202_clicks";
        $pref_time = false;
        if ($user_pref == true) {
            $pref_adv = true;
            $command = $command . " LEFT JOIN 202_clicks_advance USING (click_id) LEFT JOIN 202_clicks_site USING (click_id) ";
        } else {
            $pref_adv = false;
        }
        $command = $command . " LEFT JOIN 202_aff_campaigns ON (202_clicks.aff_campaign_id = 202_aff_campaigns.aff_campaign_id) LEFT JOIN 202_aff_networks USING (aff_network_id)";
        $pref_show = false;
        $pref_order = " AND (click_alp = '1' OR (aff_campaign_deleted='0' AND aff_network_deleted='0')) {$click_filtered} AND click_time > " . $mysql['from'] . " AND click_time <= " . $mysql['to'];
        $offset = false;
        $pref_limit = false;
        $count = false;
        $query = query($command, $db_table, $pref_time, $pref_adv, $pref_show, $pref_order, $offset, $pref_limit, $count);
        $click_sql = $query['click_sql'];
        $click_result = _mysql_query($click_sql);
        //($click_sql);
        $click_row = mysql_fetch_assoc($click_result);
        //get the stats
        $clicks = 0;
        $clicks = $click_row['clicks'];
        $total_clicks = $total_clicks + $clicks;
        //avg cpc and cost
        $avg_cpc = 0;
        $avg_cpc = $click_row['avg_cpc'];
        $cost = 0;
        $cost = $clicks * $avg_cpc;
        $total_cost = $total_cost + $cost;
        $total_avg_cpc = @round($total_cost / $total_clicks, 5);
        //leads
        $leads = 0;
        $leads = $click_row['leads'];
        $total_leads = $total_leads + $leads;
        //signup ratio
        $su_ratio - 0;
        $su_ratio = @round($leads / $clicks * 100, 2);
        $total_su_ratio = @round($total_leads / $total_clicks * 100, 2);
        //were not using payout
        //current payout
        //$payout = 0;
        //$payout = $info_row['aff_campaign_payout'];
        //income
        $income = 0;
        $income = $click_row['income'];
        $total_income = $total_income + $income;
        //grab the EPC
        $epc = 0;
        $epc = @round($income / $clicks, 2);
        $total_epc = @round($total_income / $total_clicks, 2);
        //net income
        $net = 0;
        $net = $income - $cost;
        $total_net = $total_income - $total_cost;
        //roi
        $roi = 0;
        $roi = @round($net / $cost * 100);
        $total_roi = @round($total_net / $total_cost);
        //html escape vars
        $mysql['clicks'] = mysql_real_escape_string($clicks);
        $mysql['leads'] = mysql_real_escape_string($leads);
        $mysql['su_ratio'] = mysql_real_escape_string($su_ratio);
        $mysql['epc'] = mysql_real_escape_string($epc);
        $mysql['avg_cpc'] = mysql_real_escape_string($avg_cpc);
        $mysql['income'] = mysql_real_escape_string($income);
        $mysql['cost'] = mysql_real_escape_string($cost);
        $mysql['net'] = mysql_real_escape_string($net);
        $mysql['roi'] = mysql_real_escape_string($roi);
        //insert chart
        $sort_breakdown_sql = "INSERT INTO 202_sort_breakdowns\n\t\t\t\t\t\t\t   SET         \t   sort_breakdown_from='" . $mysql['from'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_to='" . $mysql['to'] . "',\n\t\t\t\t\t\t\t\t\t\t   user_id='" . $mysql['user_id'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_clicks='" . $mysql['clicks'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_leads='" . $mysql['leads'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_su_ratio='" . $mysql['su_ratio'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_payout='" . $mysql['sort_breakdown_payout'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_epc='" . $mysql['epc'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_avg_cpc='" . $mysql['avg_cpc'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_income='" . $mysql['income'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_cost='" . $mysql['cost'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_net='" . $mysql['net'] . "',\n\t\t\t\t\t\t\t\t\t\t   sort_breakdown_roi='" . $mysql['roi'] . "'";
        $sort_breakdown_result = _mysql_query($sort_breakdown_sql);
        //($sort_breakdown_sql);
    }
    $breakdown_sql = "SELECT * FROM 202_sort_breakdowns WHERE user_id='" . $mysql['user_id'] . "'";
    $breakdown_result = _mysql_query($breakdown_sql);
    //($breakdown_sql);
    $chartWidth = $_POST['chartWidth'];
    $chartHeight = 180;
    //find where to start from.
    $start = $time['from'];
    $end = $time['to'];
    //echo date('-r',$start) . '<br/>'. date('-r',$end);
    //make sure the start isn't past this users registration time, and likewise, make sure END isn't past today, else theses will try to grab reports for dates that do not exists slowing down mysql doing reports for nothing.
    if ($user_row['user_time_register'] > $start) {
        $start = $user_row['user_time_register'];
    }
    if (time() < $end) {
        $end = time();
    }
    //cacluate the skip
    $x = 0;
    while ($start < $end) {
        if ($breakdown == 'hour' || empty($breakdown)) {
            $start = $start + 3600;
        } elseif ($breakdown == 'day') {
            $start = $start + 86400;
        } elseif ($breakdown == 'month') {
            $start = $start + 2629743;
        } elseif ($breakdown == 'year') {
            $start = $start + 31556926;
        }
        $x++;
    }
    $skip = 0;
    if ($breakdown == hour) {
        while ($x > 9) {
            $skip++;
            $x = $x - 9;
        }
    } else {
        while ($x > 14) {
            $skip++;
            $x = $x - 14;
        }
    }
    /* THIS IS A NET INCOME BAR GRAPH */
    if ($pref_chart == 'profitloss') {
        //start the PHP multi-dimensional array and create the region titles
        $chart['chart_data'][0][0] = "";
        $chart['chart_data'][1][0] = "Income";
        $chart['chart_data'][2][0] = "Cost";
        $chart['chart_data'][3][0] = "Net";
        //extract the data from the query result one row at a time
        for ($i = 0; $i < mysql_num_rows($breakdown_result); $i++) {
            //determine which column in the PHP array the current data belongs to
            $col = mysql_result($breakdown_result, $i, "sort_breakdown_from");
            //populate the PHP array with the Year title
            $date = mysql_result($breakdown_result, $i, "sort_breakdown_from");
            $date = date_chart($breakdown, $date);
            $chart['chart_data'][0][$col] = $date;
            //populate the PHP array with the revenue data
            $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_income");
            $chart['chart_data'][2][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_cost");
            $chart['chart_data'][3][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_net");
        }
        $chart['series_color'] = array("70CF40", "CF4040", "409CCF", "000000");
        $chart['series_gap'] = array('set_gap' => 40, 'bar_gap' => -35);
        $chart['chart_grid_h'] = array('alpha' => 20, 'color' => "000000", 'thickness' => 1, 'type' => "dashed");
        $chart['axis_value'] = array('bold' => false, 'size' => 10);
        $chart['axis_category'] = array('skip' => $skip, 'bold' => false, 'size' => 10);
        $chart['legend_label'] = array('bold' => true, 'size' => 12);
        $chart['chart_pref'] = array('line_thickness' => 1, 'point_shape' => "none", 'fill_shape' => true);
        $chart['chart_rect'] = array('x' => 40, 'y' => 20, 'width' => $chartWidth - 60, 'height' => $chartHeight);
        $chart['chart_transition'] = array('type' => "scale", 'delay' => 0.5, 'duration' => 0.5, 'order' => "series");
    } else {
        //start the PHP multi-dimensional array and create the region titles
        $chart['chart_data'][0][0] = "";
        if ($pref_chart == 'clicks') {
            $chart['chart_data'][1][0] = "Clicks";
        } elseif ($pref_chart == 'leads') {
            $chart['chart_data'][1][0] = "Leads";
        } elseif ($pref_chart == 'su_ratio') {
            $chart['chart_data'][1][0] = "Signup Ratio";
        } elseif ($pref_chart == 'payout') {
            $chart['chart_data'][1][0] = "Payout";
        } elseif ($pref_chart == 'epc') {
            $chart['chart_data'][1][0] = "EPC";
        } elseif ($pref_chart == 'cpc') {
            $chart['chart_data'][1][0] = "Avg CPC";
        } elseif ($pref_chart == 'income') {
            $chart['chart_data'][1][0] = "Income";
        } elseif ($pref_chart == 'cost') {
            $chart['chart_data'][1][0] = "Cost";
        } elseif ($pref_chart == 'net') {
            $chart['chart_data'][1][0] = "Net";
        } elseif ($pref_chart == 'roi') {
            $chart['chart_data'][1][0] = "ROI";
        }
        //extract the data from the query result one row at a time
        for ($i = 0; $i < mysql_num_rows($breakdown_result); $i++) {
            //determine which column in the PHP array the current data belongs to
            $col = mysql_result($breakdown_result, $i, "sort_breakdown_from");
            //populate the PHP array with the Year title
            $date = mysql_result($breakdown_result, $i, "sort_breakdown_from");
            $date = date_chart($breakdown, $date);
            $chart['chart_data'][0][$col] = $date;
            //populate the PHP array with the revenue data
            if ($pref_chart == 'clicks') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_clicks");
            } elseif ($pref_chart == 'leads') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_leads");
            } elseif ($pref_chart == 'su_ratio') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_su_ratio");
            } elseif ($pref_chart == 'payout') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_payout");
            } elseif ($pref_chart == 'epc') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_epc");
            } elseif ($pref_chart == 'cpc') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_avg_cpc");
            } elseif ($pref_chart == 'income') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_income");
            } elseif ($pref_chart == 'cost') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_cost");
            } elseif ($pref_chart == 'net') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_net");
            } elseif ($pref_chart == 'roi') {
                $chart['chart_data'][1][$col] = mysql_result($breakdown_result, $i, "sort_breakdown_roi");
            }
        }
        //$chart[ 'series_color' ] = array (  "003399");
        $chart['series_color'] = array("000000");
        $chart['chart_type'] = "Line";
        //$chart[ 'chart_transition' ] = array ( 'type'=>"dissolve", 'delay'=>.5, 'duration'=>.5, 'order'=>"series" );
        $chart['chart_grid_h'] = array('alpha' => 20, 'color' => "000000", 'thickness' => 1, 'type' => "dashed");
    }
    $chart['chart_pref'] = array('line_thickness' => 1, 'point_shape' => "circle", 'fill_shape' => false);
    $chart['axis_value'] = array('bold' => false, 'size' => 10);
    $chart['axis_category'] = array('skip' => $skip, 'bold' => false, 'size' => 10);
    $chart['legend_label'] = array('bold' => true, 'size' => 12);
    $chart['chart_rect'] = array('x' => 40, 'y' => 20, 'width' => $chartWidth - 60, 'height' => $chartHeight);
    showChart($chart, $chartWidth - 20, $chartHeight + 40);
    ?>
<div style="padding: 3px 0px;"></div><?php 
}