function phpAds_buildRatioPercentage($numerator, $denominator)
{
    return phpAds_formatPercentage(phpAds_buildRatio($numerator, $denominator));
}
function phpAds_buildSourceArrayChildren(&$source_arr, $path, $row_source_stats)
{
    if (!isset($source_arr['children']) || !is_array($source_arr['children'])) {
        $source_arr['children'] = array();
    }
    // First, get the name of this branch of the source.
    $len = strpos($path, '/');
    $name = is_integer($len) ? substr($path, 0, $len) : $path;
    $remainder = is_integer($len) ? substr($path, $len + 1) : '';
    // Next, see if there is already a branch present in the sources array
    $index = false;
    for ($i = 0; $i < sizeof($source_arr['children']); $i++) {
        if ($name == $source_arr['children'][$i]['name']) {
            $index = $i;
            break;
        }
    }
    if (!is_integer($index)) {
        $tmp_source_arr['name'] = $name;
        $tmp_source_arr['path'] = strlen($source_arr['path']) > 0 ? $source_arr['path'] . '/' . $name : $name;
        $tmp_source_arr['clicks'] = 0;
        $tmp_source_arr['conversions'] = 0;
        $tmp_source_arr['views'] = 0;
        $tmp_source_arr['ctr'] = 0.0;
        $tmp_source_arr['cnvr'] = 0.0;
        if (is_integer($len)) {
            $tmp_source_arr['children'] = array();
        }
        $source_arr['children'][] = $tmp_source_arr;
        $index = sizeof($source_arr['children']) - 1;
    }
    // Increment the stats for this branch
    $source_arr['children'][$index]['views'] += $row_source_stats['views'];
    $source_arr['children'][$index]['clicks'] += $row_source_stats['clicks'];
    $source_arr['children'][$index]['conversions'] += $row_source_stats['conversions'];
    $source_arr['children'][$index]['ctr'] = phpAds_buildRatio($source_arr['children'][$index]['clicks'], $source_arr['children'][$index]['views']);
    $source_arr['children'][$index]['cnvr'] = phpAds_buildRatio($source_arr['children'][$index]['conversions'], $source_arr['children'][$index]['clicks']);
    // If there are children, recursively populate the children array
    if (strlen($name) > 0) {
        phpAds_buildSourceArrayChildren($source_arr['children'][$index], $remainder, $row_source_stats);
    }
}
function phpAds_displayStatsRow($data_arr, $total_arr, $expand_arr, $omit_arr, $level = 0)
{
    global $campaignid, $clientid, $cnt, $phpAds_TextDirection, $phpAds_TextAlignRight;
    $show_children = false;
    echo "\t\t\t\t<tr height='25' " . ($cnt % 2 == 0 ? "bgcolor='#F6F6F6'" : "") . ">\n";
    if ($level == 0) {
        echo "\t\t\t\t\t<td colspan='2'>";
    } else {
        $checked = in_array($data_arr['path'], $omit_arr) ? '' : ' checked';
        echo "\t\t\t\t\t<td><input type='checkbox' name='keep[]' value='" . $data_arr['path'] . "'" . $checked . "><input type='hidden' name='omit[]' value='" . $path . "'></td>\n";
        echo "\t\t\t\t\t<td>";
        if ($level > 1) {
            echo "<img src='images/spacer.gif' width='" . $level * 15 . "' height='1' border='0'>";
        }
    }
    if (isset($data_arr['children']) && is_array($data_arr['children']) && sizeof($data_arr['children']) > 0) {
        if (in_array($data_arr['name'], $expand_arr)) {
            echo "<a href='stats-campaign-optimise.php?clientid=" . $clientid . "&campaignid=" . $campaignid . "&collapse=" . $data_arr['name'] . "'><img src='images/triangle-d.gif' align='absmiddle' border='0'></a>";
            $show_children = true;
        } else {
            echo "<a href='stats-campaign-optimise.php?clientid=" . $clientid . "&campaignid=" . $campaignid . "&expand=" . $data_arr['name'] . "'><img src='images/" . $phpAds_TextDirection . "/triangle-l.gif' align='absmiddle' border='0'></a>";
        }
    }
    if ($level == 0) {
        echo "&nbsp;<b>" . $data_arr['name'] . "</b></td>\n";
    } else {
        echo "&nbsp;" . $data_arr['name'] . "</td>\n";
    }
    echo "\t\t\t\t\t<td align='" . $phpAds_TextAlignRight . "'>" . phpAds_formatNumber($data_arr['views']) . "</td>\n";
    echo "\t\t\t\t\t<td align='center'>(" . phpAds_formatPercentage(phpAds_buildRatio($data_arr['views'], $total_arr['views']), 0) . ")</td>\n";
    echo "\t\t\t\t\t<td align='" . $phpAds_TextAlignRight . "'>" . phpAds_formatNumber($data_arr['clicks']) . "</td>\n";
    echo "\t\t\t\t\t<td align='center'>(" . phpAds_formatPercentage(phpAds_buildRatio($data_arr['clicks'], $total_arr['clicks']), 0) . ")</td>\n";
    echo "\t\t\t\t\t<td align='" . $phpAds_TextAlignRight . "'>" . phpAds_formatPercentage($data_arr['ctr']) . "</td>\n";
    echo "\t\t\t\t\t<td align='center'>(" . phpAds_formatPercentage(phpAds_buildRatio($data_arr['ctr'], $total_arr['ctr']), 0) . ")</td>\n";
    echo "\t\t\t\t\t<td align='" . $phpAds_TextAlignRight . "'>" . phpAds_formatNumber($data_arr['conversions']) . "</td>\n";
    echo "\t\t\t\t\t<td align='center'>(" . phpAds_formatPercentage(phpAds_buildRatio($data_arr['conversions'], $total_arr['conversions']), 0) . ")</td>\n";
    echo "\t\t\t\t\t<td align='" . $phpAds_TextAlignRight . "'>" . phpAds_formatPercentage($data_arr['cnvr']) . "&nbsp;&nbsp;</td>\n";
    echo "\t\t\t\t\t<td align='center'>(" . phpAds_formatPercentage(phpAds_buildRatio($data_arr['cnvr'], $total_arr['cnvr']), 0) . ")</td>\n";
    echo "\t\t\t\t</tr>\n";
    if ($show_children) {
        for ($i = 0; $i < sizeof($data_arr['children']); $i++) {
            phpAds_displayStatsRow($data_arr['children'][$i], $total_arr, $expand_arr, $omit_arr, $level + 1);
        }
    }
    if ($level == 0) {
        $cnt++;
    }
}
function phpAds_buildCTR($views, $clicks)
{
    return phpAds_formatPercentage(phpAds_buildRatio($clicks, $views));
}