<?php
/*
+---------------------------------------------------------------------------+
| OpenX v2.6                                                                |
| ==========                                                                |
|                                                                           |
| Copyright (c) 2003-2008 OpenX Limited                                     |
| For contact details, see: http://www.openx.org/                           |
|                                                                           |
| This program is free software; you can redistribute it and/or modify      |
| it under the terms of the GNU General Public License as published by      |
| the Free Software Foundation; either version 2 of the License, or         |
| (at your option) any later version.                                       |
|                                                                           |
| This program is distributed in the hope that it will be useful,           |
| but WITHOUT ANY WARRANTY; without even the implied warranty of            |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
| GNU General Public License for more details.                              |
|                                                                           |
| You should have received a copy of the GNU General Public License         |
| along with this program; if not, write to the Free Software               |
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA |
+---------------------------------------------------------------------------+
$Id: html.php 22929 2008-07-21 14:01:02Z david.keen@openx.org $
*/

require_once MAX_PATH . '/lib/max/Admin_DA.php';
require_once MAX_PATH . '/www/admin/lib-statistics.inc.php';

function MAX_getDisplayName($name, $length = 60, $append = '...')
{
    $displayName = strlen($name) > $length ? rtrim(substr($name, 0, $length-strlen($append))) . $append : $name;
    if (empty($displayName)) {
        $displayName = $GLOBALS['strUntitled'];
    }
    return $displayName;
}

function MAX_buildName($id, $name)
{
    return "<span dir='{$GLOBALS['phpAds_TextDirection']}'>[id$id]</span> $name";
}

function MAX_getEntityIcon($entity, $active=true, $type='')
{
    include_once MAX_PATH . '/www/admin/lib-zones.inc.php';

    $icon = '';
    switch ($entity) {
        case 'advertiser' : $icon = $active ? 'images/icon-advertiser.gif' : 'images/icon-advertiser-d.gif'; break;
        case 'placement'  : $icon = $active ? 'images/icon-campaign.gif' : 'images/icon-campaign-d.gif'; break;
        case 'publisher'  : $icon = 'images/icon-affiliate.gif'; break;
        case 'ad' :
            switch ($type) {
                case 'html' : $icon = $active ? 'images/icon-banner-html.gif' : 'images/icon-banner-html-d.gif'; break;
                case 'txt'  : $icon = $active ? 'images/icon-banner-text.gif' : 'images/icon-banner-text-d.gif'; break;
                case 'url'  : $icon = $active ? 'images/icon-banner-url.gif' : 'images/icon-banner-url-d.gif'; break;
                case 'web'  : $icon = $active ? 'images/icon-banner-stored.gif' : 'images/icon-banner-stored-d.gif'; break;
                default     : $icon = $active ? 'images/icon-banner-stored.gif' : 'images/icon-banner-stored-d.gif'; break;
            }
            break;
        case 'zone'       :
            switch ($type) {
                case phpAds_ZoneBanner       : $icon = 'images/icon-zone.gif'; break;
                case phpAds_ZoneInterstitial : $icon = 'images/icon-interstitial.gif'; break;
                case phpAds_ZonePopup        : $icon = 'images/icon-popup.gif'; break;
                case phpAds_ZoneText         : $icon = 'images/icon-textzone.gif'; break;
                case MAX_ZoneEmail           : $icon = 'images/icon-zone-email.gif'; break;
                case MAX_ZoneClick           : $icon = 'images/icon-zone-click.gif'; break;
                default                      : $icon = 'images/icon-zone.gif'; break;
            }
            break;
    }
    return MAX::assetPath() . "/" . $icon;
}

function MAX_displayZoneHeader($pageName, $listorder, $orderdirection, $entityIds=null, $anonymous=false)
{
    global $phpAds_TextAlignRight;
    $column1 = _getHtmlHeaderColumn($GLOBALS['strName'], 'name', $pageName, $entityIds, $listorder, $orderdirection);
    $column2 = _getHtmlHeaderColumn($GLOBALS['strID'], 'id', $pageName, $entityIds, $listorder, $orderdirection, ($anonymous == false));
    $column3 = _getHtmlHeaderColumn($GLOBALS['strDescription'], 'description', $pageName, $entityIds, $listorder, $orderdirection);
    echo "
    <tr height='1'>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='300' height='1' border='0' alt='' title=''></td>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
        <td width='100%'><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
    </tr>
    <tr height='25'>
        <td>$column1</td>
        <td>$column2</td>
        <td>$column3</td>
    </tr>
    <tr height='1'><td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>";
}

function MAX_displayStatsHeader($pageName, $listorder, $orderdirection, $entityIds=null, $anonymous=false)
{
    global $phpAds_TextAlignRight;
    $column1 = _getHtmlHeaderColumn($GLOBALS['strName'], 'name', $pageName, $entityIds, $listorder, $orderdirection);
    $column2 = _getHtmlHeaderColumn($GLOBALS['strID'], 'id', $pageName, $entityIds, $listorder, $orderdirection, ($anonymous == false));
    $column3 = _getHtmlHeaderColumn($GLOBALS['strRequests'], 'sum_requests', $pageName, $entityIds, $listorder, $orderdirection);
    $column4 = _getHtmlHeaderColumn($GLOBALS['strImpressions'], 'sum_views', $pageName, $entityIds, $listorder, $orderdirection);
    $column5 = _getHtmlHeaderColumn($GLOBALS['strClicks'], 'sum_clicks', $pageName, $entityIds, $listorder, $orderdirection);
    $column6 = _getHtmlHeaderColumn($GLOBALS['strCTRShort'], 'ctr', $pageName, $entityIds, $listorder, $orderdirection);
    $column7 = _getHtmlHeaderColumn($GLOBALS['strConversions'], 'sum_conversions', $pageName, $entityIds, $listorder, $orderdirection);
    $column8 = _getHtmlHeaderColumn($GLOBALS['strCNVRShort'], 'cnvr', $pageName, $entityIds, $listorder, $orderdirection);
    echo "
    <tr height='1'>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='200' height='1' border='0' alt='' title=''></td>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
        <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
    </tr>
    <tr height='25'>
        <td width='30%'>$column1</td>
        <td align='$phpAds_TextAlignRight'>$column2</td>
        <td align='$phpAds_TextAlignRight'>$column3</td>
        <td align='$phpAds_TextAlignRight'>$column4</td>
        <td align='$phpAds_TextAlignRight'>$column5</td>
        <td align='$phpAds_TextAlignRight'>$column6</td>
        <td align='$phpAds_TextAlignRight'>$column7</td>
        <td align='$phpAds_TextAlignRight'>$column8</td>
    </tr>
    <tr height='1'><td colspan='8' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>";
}

function MAX_displayStatsHistoryHeader($pageName, $listorder, $orderdirection, $entityIds=null)
{
    global $phpAds_TextAlignRight;
    $column1 = _getHtmlHeaderColumn($GLOBALS['strDays'], 'name', $pageName, $entityIds, $listorder, $orderdirection);
    $column2 = _getHtmlHeaderColumn($GLOBALS['strImpressions'], 'sum_views', $pageName, $entityIds, $listorder, $orderdirection);
    $column3 = _getHtmlHeaderColumn($GLOBALS['strClicks'], 'sum_clicks', $pageName, $entityIds, $listorder, $orderdirection);
    $column4 = _getHtmlHeaderColumn($GLOBALS['strCTRShort'], 'ctr', $pageName, $entityIds, $listorder, $orderdirection);
    $column5 = _getHtmlHeaderColumn($GLOBALS['strConversions'], 'sum_conversions', $pageName, $entityIds, $listorder, $orderdirection);
    $column6 = _getHtmlHeaderColumn($GLOBALS['strCNVRShort'], 'cnvr', $pageName, $entityIds, $listorder, $orderdirection);
    echo "
        <table border='0' cellpadding='0' cellspacing='0' width='100%'>
        <tr height='1'>
            <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='200' height='1' border='0' alt='' title=''></td>
            <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
            <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
            <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
            <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
            <td><img src='" . MAX::assetPath() . "/images/spacer.gif' width='80' height='1' border='0' alt='' title=''></td>
        </tr>
        <tr height='25'>
            <td width='30%'>$column1</td>
            <td align='$phpAds_TextAlignRight'>$column2</td>
            <td align='$phpAds_TextAlignRight'>$column3</td>
            <td align='$phpAds_TextAlignRight'>$column4</td>
            <td align='$phpAds_TextAlignRight'>$column5</td>
            <td align='$phpAds_TextAlignRight'>$column6</td>
        </tr>
        <tr height='1'><td colspan='7' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>
    ";
}

function MAX_displayNoStatsMessage()
{
    echo "
    <br /><br /><div class='errormessage'><img class='errormessage' src='" . MAX::assetPath() . "/images/info.gif' width='16' height='16' border='0' align='absmiddle'>{$GLOBALS['strNoStats']}</div>";
}

function _getHtmlHeaderColumn($title, $name, $pageName, $entityIds, $listorder, $orderdirection, $showColumn = true)
{
    $str = '';
    $entity = _getEntityString($entityIds);
    if ($listorder == $name) {
        if (($orderdirection == '') || ($orderdirection == 'down')) {
            $str = "<a href='$pageName?{$entity}orderdirection=up'><img src='" . MAX::assetPath() . "/images/caret-ds.gif' border='0' alt='' title=''></a>";
        } else {
            $str = "<a href='$pageName?{$entity}orderdirection=down'><img src='" . MAX::assetPath() . "/images/caret-u.gif' border='0' alt='' title=''></a>";
        }
    }
    return $showColumn ? "<b><a href='$pageName?{$entity}listorder=$name'>$title</a>$str</b>" : '';
}

function _getEntityString($entityIds)
{
    $entity = '';
    if (!empty($entityIds)) {
        $entityArr = array();
        foreach ($entityIds as $entityId => $entityValue) {
            $entityArr[] = "$entityId=$entityValue";
        }
        $entity = implode('&',$entityArr) . '&';
    }

    return $entity;
}

function MAX_displayDateSelectionForm($period, $period_start, $period_end, $pageName, &$tabindex, $hiddenValues = null)
{
    global $tabindex;
    require_once MAX_PATH . '/lib/max/Admin/UI/FieldFactory.php';

    $oDaySpan =& FieldFactory::newField('day-span');
    $oDaySpan->_name = 'period';
    $oDaySpan->_autoSubmit = true;
    $oDaySpan->setValueFromArray(array('period_preset' => $period, 'period_start' => $period_start, 'period_end' => $period_end));
    $oDaySpan->_tabIndex = $tabindex;
    echo "
    <form id='period_form' name='period_form' action='$pageName'>";
    $oDaySpan->display();
    $tabindex = $oDaySpan->_tabIndex;
    echo "
    <input type='button' value='Go' onclick='return periodFormSubmit()' style='margin-left: 1em' tabindex='".$tabindex++."' />";
    _displayHiddenValues($hiddenValues);
    echo "
    </form>";
}

function _displayHiddenValues($hiddenValues)
{
    if (!empty($hiddenValues) && is_array($hiddenValues)) {
        foreach ($hiddenValues as $name => $value) {
            echo "
    <input type='hidden' name='$name' value='$value'>";
        }
    }
}

function MAX_displayPeriodSelectionForm($period, $pageName, &$tabindex, $hiddenValues = null)
{
    global $phpAds_TextDirection;

    echo "
    <form action='$pageName'>
    <select name='period' onChange='this.form.submit();' tabindex='". $tabindex++ ."'>
        <option value='daily'".($period == 'daily' ? ' selected' : '').">{$GLOBALS['strDailyHistory']}</option>
        <option value='w'".($period == 'weekly' ? ' selected' : '').">{$GLOBALS['strWeeklyHistory']}</option>
        <option value='m'".($period == 'monthly' ? ' selected' : '').">{$GLOBALS['strMonthlyHistory']}</option>
    </select>
    &nbsp;&nbsp;
    <input type='image' src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/go_blue.gif' border='0' name='submit'>
    &nbsp;";
    _displayHiddenValues($hiddenValues);
    echo "
    </form>
    ";
}

function MAX_displayHistoryStatsDaily($aHistoryStats, $aTotalHistoryStats, $pageName, $hiddenValues = null)
{
    $i = 0;
    $entity = _getEntityString($hiddenValues);
    foreach ($aHistoryStats as $day => $stats) {
        $bgColor = ($i++ % 2 == 0) ? '#F6F6F6' : '#FFFFFF';
        $views = phpAds_formatNumber($stats['sum_views']);
        $clicks = phpAds_formatNumber($stats['sum_clicks']);
        $conversions = phpAds_formatNumber($stats['sum_conversions']);
        $ctr = phpAds_buildRatioPercentage($stats['sum_clicks'], $stats['sum_views']);
        $cnvr = phpAds_buildRatioPercentage($stats['sum_conversions'], $stats['sum_clicks']);
        echo "
        <tr height='25' bgcolor='$bgColor'>
            <td>&nbsp;<img src='" . MAX::assetPath() . "/images/icon-date.gif' align='absmiddle' alt=''>&nbsp;<a href='$pageName?{$entity}'>$day</a></td>
            <td align='right'>$views</td>
            <td align='right'>$clicks</td>
            <td align='right'>$ctr</td>
            <td align='right'>$conversions</td>
            <td align='right'>$cnvr</td>
        </tr>
        <tr><td height='1' colspan='6' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%' alt=''></td></tr>
        ";
    }
    echo "
    </table>";
}

function MAX_displayPublisherZoneStats($aParams, $pageName, $anonymous, $aNodes, $expand, $listorder, $orderdirection, $hideinactive, $showPublisher, $entityIds)
{
    global $phpAds_TextAlignLeft, $phpAds_TextAlignRight, $phpAds_TextDirection;

    // Get the icons for all levels (publisher/zone)
    $entity = _getEntityString($entityIds);
    $publishersHidden = 0;

    $aPublishers = Admin_DA::fromCache('getPublishersStats', $aParams);
    if (!empty($aPublishers)) {
        echo "
        <br /><br />
        <table border='0' width='100%' cellpadding='0' cellspacing='0'>";
        MAX_displayStatsHeader($pageName, $listorder, $orderdirection, $entityIds);

        // Variable to determine if the row should be grey or white...
        $i=0;

        // Loop through publishers
        $totalRequests = 0;
        $totalViews = 0;
        $totalClicks = 0;
        $totalConversions = 0;
        MAX_sortArray($aPublishers, ($listorder == 'id' ? 'publisher_id' : $listorder), $orderdirection == 'up');
        foreach($aPublishers as $publisherId => $publisher) {
            $publisherRequests = phpAds_formatNumber($publisher['sum_requests']);
            $publisherViews = phpAds_formatNumber($publisher['sum_views']);
            $publisherClicks = phpAds_formatNumber($publisher['sum_clicks']);
            $publisherConversions = phpAds_formatNumber($publisher['sum_conversions']);
            $publisherCtr = phpAds_buildRatioPercentage($publisher['sum_clicks'], $publisher['sum_views']);
            $publisherSr = phpAds_buildRatioPercentage($publisher['sum_conversions'], $publisher['sum_clicks']);
            $publisherExpanded = MAX_isExpanded($publisherId, $expand, $aNodes, 'p');
            $publisherActive = true;
            $publisherIcon = MAX_getEntityIcon('publisher', $publisherActive);

            if (!$hideinactive || $publisherActive) {
                $bgcolor = ($i++ % 2 == 0) ? " bgcolor='#F6F6F6'" : '';
                echo "
            <tr height='25'$bgcolor>
                <td>";
                if (!empty($publisher['num_children'])) {
                    if ($publisherExpanded)
                        echo "&nbsp;<a href='$pageName?{$entity}collapse=p$publisherId'><img src='" . MAX::assetPath() . "/images/triangle-d.gif' align='absmiddle' border='0'></a>&nbsp;";
                    else
                        echo "&nbsp;<a href='$pageName?{$entity}expand=p$publisherId'><img src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/triangle-l.gif' align='absmiddle' border='0'></a>&nbsp;";
                }
                else
                    echo "&nbsp;<img src='" . MAX::assetPath() . "/images/spacer.gif' height='16' width='16'>&nbsp;";

                echo "
                    <img src='$publisherIcon' align='absmiddle'>&nbsp;
                    <a href='stats.php?entity=affiliate&breakdown=history&affiliateid=$publisherId'>{$publisher['name']}</a>
                </td>";
                if ($anonymous) {
                    echo "
                <td align='$phpAds_TextAlignRight'>&nbsp;</td>";
                } else {
                    echo "
                <td align='$phpAds_TextAlignRight'>$publisherId</td>";
                    }
                    echo "
                <td align='$phpAds_TextAlignRight'>$publisherRequests</td>
                <td align='$phpAds_TextAlignRight'>$publisherViews</td>
                <td align='$phpAds_TextAlignRight'>$publisherClicks</td>
                <td align='$phpAds_TextAlignRight'>$publisherCtr</td>
                <td align='$phpAds_TextAlignRight'>$publisherConversions</td>
                <td align='$phpAds_TextAlignRight'>$publisherSr</td>
            </tr>";

                if (!empty($publisher['num_children']) && $publisherExpanded) {
                    echo "
            <tr height='1'>
                <td$bgcolor><img src='" . MAX::assetPath() . "/images/spacer.gif' width='1' height='1'></td>
                <td colspan='8' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break-l.gif' height='1' width='100%'></td>
            </tr>";


                    // Loop through zones
                    $aZones = Admin_DA::fromCache('getZonesStats', $aParams);
                    MAX_sortArray($aZones, ($listorder == 'id' ? 'zone_id' : $listorder), $orderdirection == 'up');
                    foreach ($aZones as $zoneId => $zone) {
                        $zoneRequests = phpAds_formatNumber($zone['sum_requests']);
                        $zoneViews = phpAds_formatNumber($zone['sum_views']);
                        $zoneClicks = phpAds_formatNumber($zone['sum_clicks']);
                        $zoneConversions = phpAds_formatNumber($zone['sum_conversions']);
                        $zoneCtr = phpAds_buildRatioPercentage($zone['sum_clicks'], $zone['sum_views']);
                        $zoneSr = phpAds_buildRatioPercentage($zone['sum_conversions'], $zone['sum_clicks']);
                        $zoneActive = true;
                        $zoneIcon = MAX_getEntityIcon('zone', $zoneActive, $zone['type']);

                        echo "
            <tr height='25'$bgcolor>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <img src='" . MAX::assetPath() . "/images/spacer.gif' height='16' width='16' align='absmiddle'>&nbsp;
                    <img src='$zoneIcon' align='absmiddle'>&nbsp;
                    <a href='stats.php?entity=zone&breakdown=history&affiliateid=$publisherId&zoneid=$zoneId'>{$zone['name']}</a>
                </td>
                <td align='$phpAds_TextAlignRight'>$zoneId</td>
                <td align='$phpAds_TextAlignRight'>$zoneRequests</td>
                <td align='$phpAds_TextAlignRight'>$zoneViews</td>
                <td align='$phpAds_TextAlignRight'>$zoneClicks</td>
                <td align='$phpAds_TextAlignRight'>$zoneCtr</td>
                <td align='$phpAds_TextAlignRight'>$zoneConversions</td>
                <td align='$phpAds_TextAlignRight'>$zoneSr</td>
            </tr>";
                    }
                }
                echo "
                <tr height='1'><td colspan='8' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>";
            } else {
                $publishersHidden++;
            }
            $totalRequests += $publisher['sum_requests'];
            $totalViews += $publisher['sum_views'];
            $totalClicks += $publisher['sum_clicks'];
            $totalConversions += $publisher['sum_conversions'];
        }

        // Total
        echo "
        <tr height='25'$bgcolor>
            <td>&nbsp;&nbsp;<b>{$GLOBALS['strTotal']}</b></td>
            <td>&nbsp;</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_formatNumber($totalRequests)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_formatNumber($totalViews)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_formatNumber($totalClicks)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_buildCTR($totalViews, $totalClicks)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_formatNumber($totalConversions)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_buildCTR($totalClicks, $totalConversions)."</td>
        </tr>
        <tr height='1'>
            <td colspan='8' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td>
        </tr>";
        if (!$anonymous) {
            $publisherIcon = MAX_getEntityIcon('publisher');
            echo "
        <tr>
            <td colspan='4' align='$phpAds_TextAlignLeft' nowrap>";

            if ($hideinactive == true) {
                echo "&nbsp;&nbsp;<img src='" . MAX::assetPath() . "/images/icon-activate.gif' align='absmiddle' border='0'>&nbsp;<a href='$pageName?{$entity}hideinactive=0'>{$GLOBALS['strShowAll']}</a>&nbsp;&nbsp;|&nbsp;&nbsp;$publishersHidden {$GLOBALS['strInactivePublishersHidden']}";
            } else {
                echo "&nbsp;&nbsp;<img src='" . MAX::assetPath() . "/images/icon-hideinactivate.gif' align='absmiddle' border='0'>&nbsp;<a href='$pageName?{$entity}hideinactive=1'>{$GLOBALS['strHideInactivePublishers']}</a>";
            }

            echo "
            </td>
            <td colspan='4' align='$phpAds_TextAlignRight' nowrap><img src='" . MAX::assetPath() . "/images/triangle-d.gif' align='absmiddle' border='0'>&nbsp;<a href='$pageName?{$entity}expand=all' accesskey='$keyExpandAll'>{$GLOBALS['strExpandAll']}</a>&nbsp;&nbsp;|&nbsp;&nbsp;<img src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/triangle-l.gif' align='absmiddle' border='0'>&nbsp;<a href='$pageName?{$entity}expand=none' accesskey='$keyCollapseAll'>{$GLOBALS['strCollapseAll']}</a>&nbsp;&nbsp;</td>
        </tr>
        <tr height='25'>";
            if ($showPublisher == 't') {
                echo "
            <td colspan='8' align='$phpAds_TextAlignLeft' nowrap>&nbsp;&nbsp;<img src='$publisherIcon' align='absmiddle'><a href='$pageName?{$entity}showpublisher=f'> Hide parent publisher</a></td>";
            } else {
                echo "
            <td colspan='8' align='$phpAds_TextAlignLeft' nowrap>&nbsp;&nbsp;<img src='$publisherIcon' align='absmiddle'><a href='$pageName?{$entity}showpublisher=t'> Show parent publisher</a></td>";
            }
            echo "
        </tr>";
        }
        echo "
        </table>
        <br /><br />";
    } else {
        MAX_displayNoStatsMessage();
    }
}

function MAX_displayZoneStats($aParams, $pageName, $anonymous, $aNodes, $expand, $listorder, $orderdirection, $hideinactive, $showPublisher, $entityIds)
{
    global $phpAds_TextAlignLeft, $phpAds_TextAlignRight, $phpAds_TextDirection;

    // Get the icons for all levels (publisher/zone)
    $entity = _getEntityString($entityIds);
    $publishersHidden = 0;

    $aZones = Admin_DA::fromCache('getZonesStats', $aParams);
    if (!empty($aZones)) {
        echo "
        <br /><br />
        <table border='0' width='100%' cellpadding='0' cellspacing='0'>";
        MAX_displayStatsHeader($pageName, $listorder, $orderdirection, $entityIds, $anonymous);

        // Variable to determine if the row should be grey or white...
        $i=0;
        $totalRequests = 0;
        $totalViews = 0;
        $totalClicks = 0;
        $totalConversions = 0;

        // Loop through publishers
        MAX_sortArray($aZones, ($listorder == 'id' ? 'zone_id' : $listorder), $orderdirection == 'up');
        foreach($aZones as $zoneId => $zone) {
            $zoneRequests = phpAds_formatNumber($zone['sum_requests']);
            $zoneViews = phpAds_formatNumber($zone['sum_views']);
            $zoneClicks = phpAds_formatNumber($zone['sum_clicks']);
            $zoneConversions = phpAds_formatNumber($zone['sum_conversions']);
            $zoneCtr = phpAds_buildRatioPercentage($zone['sum_clicks'], $zone['sum_views']);
            $zoneSr = phpAds_buildRatioPercentage($zone['sum_conversions'], $zone['sum_clicks']);
            $zoneActive = true;
            $zoneIcon = MAX_getEntityIcon('zone', $zoneActive, $zone['type']);

            if (!$hideinactive || $zoneActive) {
                $bgcolor = ($i++ % 2 == 0) ? " bgcolor='#F6F6F6'" : '';
                echo "
        <tr height='25'$bgcolor>
            <td>&nbsp;<img src='" . MAX::assetPath() . "/images/spacer.gif' height='16' width='16'>&nbsp;
                <img src='$zoneIcon' align='absmiddle'>&nbsp;";
                if ($anonymous) {
                    echo "
                Hidden zone {$zone['id']}";
                } else {
                    echo "
                <a href='stats.php?entity=zone&breakdown=history&affiliateid={$zone['publisher_id']}'>{$zone['name']}</a>";
                }
                echo "
            </td>";
                if ($anonymous) {
                    echo "
            <td align='$phpAds_TextAlignRight'>&nbsp;</td>";
                } else {
                    echo "
            <td align='$phpAds_TextAlignRight'>$zoneId</td>";
                }
                echo "
            <td align='$phpAds_TextAlignRight'>$zoneRequests</td>
            <td align='$phpAds_TextAlignRight'>$zoneViews</td>
            <td align='$phpAds_TextAlignRight'>$zoneClicks</td>
            <td align='$phpAds_TextAlignRight'>$zoneCtr</td>
            <td align='$phpAds_TextAlignRight'>$zoneConversions</td>
            <td align='$phpAds_TextAlignRight'>$zoneSr</td>
        </tr>
        <tr height='1'><td colspan='8' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>";
            } else {
                $publishersHidden++;
            }
            $totalRequests += $zone['sum_requests'];
            $totalViews += $zone['sum_views'];
            $totalClicks += $zone['sum_clicks'];
            $totalConversions += $zone['sum_conversions'];
        }

        // Total
        echo "
        <tr height='25'$bgcolor>
            <td>&nbsp;&nbsp;<b>{$GLOBALS['strTotal']}</b></td>
            <td>&nbsp;</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_formatNumber($totalRequests)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_formatNumber($totalViews)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_formatNumber($totalClicks)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_buildCTR($totalViews, $totalClicks)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_formatNumber($totalConversions)."</td>
            <td align='$phpAds_TextAlignRight'>".phpAds_buildCTR($totalClicks, $totalConversions)."</td>
        </tr>
        <tr height='1'>
            <td colspan='8' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td>
        </tr>";
        if (!$anonymous) {
            echo "
        <tr>
            <td colspan='4' align='$phpAds_TextAlignLeft' nowrap>";

            if ($hideinactive == true) {
                echo "&nbsp;&nbsp;<img src='" . MAX::assetPath() . "/images/icon-activate.gif' align='absmiddle' border='0'>&nbsp;<a href='$pageName?{$entity}hideinactive=0'>{$GLOBALS['strShowAll']}</a>&nbsp;&nbsp;|&nbsp;&nbsp;$publishersHidden {$GLOBALS['strInactivePublishersHidden']}";
            } else {
                echo "&nbsp;&nbsp;<img src='" . MAX::assetPath() . "/images/icon-hideinactivate.gif' align='absmiddle' border='0'>&nbsp;<a href='$pageName?{$entity}hideinactive=1'>{$GLOBALS['strHideInactivePublishers']}</a>";
            }

            echo "
            </td>
            <td colspan='4' align='$phpAds_TextAlignRight' nowrap><img src='" . MAX::assetPath() . "/images/triangle-d.gif' align='absmiddle' border='0'>&nbsp;<a href='$pageName?{$entity}expand=all'>{$GLOBALS['strExpandAll']}</a>&nbsp;&nbsp;|&nbsp;&nbsp;<img src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/triangle-l.gif' align='absmiddle' border='0'>&nbsp;<a href='$pageName?{$entity}expand=none'>{$GLOBALS['strCollapseAll']}</a>&nbsp;&nbsp;</td>
        </tr>
        <tr height='25'>";
            if ($showPublisher == 't') {
                echo "
            <td colspan='7' align='$phpAds_TextAlignLeft' nowrap>&nbsp;&nbsp;<img src='" . MAX::assetPath() . "/images/icon-affiliate.gif' align='absmiddle'><a href='$pageName?{$entity}showpublisher=f'> Hide parent publisher</a></td>";
            } else {
                echo "
            <td colspan='7' align='$phpAds_TextAlignLeft' nowrap>&nbsp;&nbsp;<img src='" . MAX::assetPath() . "/images/icon-affiliate.gif' align='absmiddle'><a href='$pageName?{$entity}showpublisher=t'> Show parent publisher</a></td>";
            }
            echo "
        </tr>";
        }
        echo "
        </table>
        <br /><br />";
    } else {
        MAX_displayNoStatsMessage();
    }
}

function MAX_displayNavigationCampaign($pageName, $aOtherAdvertisers, $aOtherCampaigns, $aEntities)
{
    global $phpAds_TextDirection;
    $aConf = $GLOBALS['_MAX']['CONF'];

    $advertiserId = $aEntities['clientid'];
    $placementId = $aEntities['campaignid'];
    $entityString = _getEntityString($aEntities);
    $aOtherEntities = $aEntities;
    unset($aOtherEntities['campaignid']);
    $otherEntityString = _getEntityString($aOtherEntities);
    $advertiserName = MAX_buildName($advertiserId, $aOtherAdvertisers[$advertiserId]['name']);

    // Determine which tab is highlighted
    if (OA_Permission::isAccount(OA_ACCOUNT_ADVERTISER)) {
        if ($pageName == 'campaign-banners.php') {
            $tabValue = "2.1";
        }
        $tabSections = array('2.1');
    } else {
        switch ($pageName) {
            case 'campaign-edit.php':
                $tabValue = '4.1.3.2';
                break;
            case 'campaign-zone.php':
                $tabValue = '4.1.3.3';
                break;
            case 'campaign-banners.php':
                $tabValue = '4.1.3.4';
                break;
            case 'campaign-trackers.php':
                // Conditionally display conversion tracking
                if ($aConf['logging']['trackerImpressions']) {
                    $tabValue = '4.1.3.5';
                    break;
                }
        }

        // Get the tab sections
        $tabSections = array('4.1.3.2', '4.1.3.3', '4.1.3.4');

        // Conditionally display conversion tracking
        if ($aConf['logging']['trackerImpressions']) {
            $tabSections[] = '4.1.3.5';
        }
    }
    foreach ($aOtherCampaigns as $otherCampaignId => $aOtherCampaign) {
        $otherCampaignName = MAX_buildName($otherCampaignId, $aOtherCampaign['name']);
        $page = "{$pageName}?{$otherEntityString}campaignid={$otherCampaignId}&";
        if ($otherCampaignId == $placementId) {
            $current = true;

            // mask campaign name if anonymous campaign
            $campaign_details = Admin_DA::getPlacement($otherCampaignId);
            $otherCampaignName = MAX_buildName($placementId, MAX_getPlacementName($campaign_details));

            $campaignName = $otherCampaignName;

        } else {
            $current = false;
        }
        phpAds_PageContext($otherCampaignName, $page, $current);
    }

    if (!OA_Permission::isAccount(OA_ACCOUNT_ADVERTISER)) {
        phpAds_PageShortcut($GLOBALS['strClientProperties'], "advertiser-edit.php?clientid=$advertiserId", 'images/icon-advertiser.gif');
    }
    phpAds_PageShortcut($GLOBALS['strCampaignHistory'], "stats.php?entity=campaign&breakdown=history&$entityString", 'images/icon-statistics.gif');

    if (!OA_Permission::isAccount(OA_ACCOUNT_ADVERTISER)) {
    $extra  = "
    <form action='campaign-modify.php'>
    <input type='hidden' name='clientid' value='$advertiserId'>
    <input type='hidden' name='campaignid' value='$placementId'>
    <input type='hidden' name='returnurl' value='$pageName'>
    <br /><br />
    <b>{$GLOBALS['strModifyCampaign']}</b><br />
    <img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
    <img src='" . MAX::assetPath() . "/images/icon-move-campaign.gif' align='absmiddle'>&nbsp;<a href='campaign-modify.php?duplicate=1&$entityString&returnurl=$pageName'>{$GLOBALS['strDuplicate']}</a><br />
    <img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
    <img src='" . MAX::assetPath() . "/images/icon-move-campaign.gif' align='absmiddle'>&nbsp;{$GLOBALS['strMoveTo']}<br />
    <img src='" . MAX::assetPath() . "/images/spacer.gif' height='1' width='160' vspace='2'><br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <select name='newclientid' style='width: 110;'>";
        $aOtherAdvertisers = _multiSort($aOtherAdvertisers,'name','advertiser_id');
        foreach ($aOtherAdvertisers as $aOtherAdvertiser) {
            $otherAdvertiserId = $aOtherAdvertiser['advertiser_id'];
            $otherAdvertiserName = MAX_buildName($otherAdvertiserId, $aOtherAdvertiser['name']);
            if ($otherAdvertiserId != $advertiserId) {
                $extra .= "
    <option value='$otherAdvertiserId'>$otherAdvertiserName</option>";
            }
        }
        $deleteConfirm = phpAds_DelConfirm($GLOBALS['strConfirmDeleteCampaign']);
        $extra .= "
            </select>
            <input type='image' src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/go_blue.gif'><br />
            <img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
            <img src='" . MAX::assetPath() . "/images/icon-recycle.gif' align='absmiddle'>&nbsp;<a href='campaign-delete.php?$entityString&returnurl=advertiser-campaigns.php'$deleteConfirm>{$GLOBALS['strDelete']}</a><br />
            </form>";
    }

    phpAds_PageHeader($tabValue, $extra);
    echo "
<img src='" . MAX::assetPath() . "/images/icon-advertiser.gif' align='absmiddle'>&nbsp;$advertiserName&nbsp;<img src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/caret-rs.gif'>&nbsp;
<img src='" . MAX::assetPath() . "/images/icon-campaign.gif' align='absmiddle'>&nbsp;<b>$campaignName</b><br /><br /><br />";
    phpAds_ShowSections($tabSections);
}

function MAX_displayNavigationBanner($pageName, $aOtherCampaigns, $aOtherBanners, $aEntities)
{
    global $phpAds_TextDirection;

    $advertiserId = $aEntities['clientid'];
    $campaignId = $aEntities['campaignid'];
    $bannerId = $aEntities['bannerid'];
    $entityString = _getEntityString($aEntities);
    $aOtherEntities = $aEntities;
    unset($aOtherEntities['bannerid']);
    $otherEntityString = _getEntityString($aOtherEntities);

    // Determine which tab is highlighted
    if (OA_Permission::isAccount(OA_ACCOUNT_ADVERTISER)) {
        switch ($pageName) {
            case 'banner-edit.php'   : $tabValue = '2.1.1'; break;
        }
        // Get the tab sections
        $tabSections = array('2.1.1');
    } else {
        // Get the tab sections
        $tabSections = array('4.1.3.4.2', '4.1.3.4.3', '4.1.3.4.4', '4.1.3.4.6');

        switch ($pageName) {
            case 'banner-zone.php'     : $tabValue = '4.1.3.4.4'; break;
            case 'banner-acl.php'      : $tabValue = '4.1.3.4.3'; break;
            case 'banner-edit.php'     :
                if (empty($bannerId)) {
                    $tabValue = '4.1.3.4.1';
                    $tabSections = array('4.1.3.4.1');
                } else {
                    $tabValue = '4.1.3.4.2';
                }
                break;
            case 'banner-advanced.php' : $tabValue = '4.1.3.4.6'; break;
        }
    }
    $bannerName = '';
    foreach ($aOtherBanners as $otherBannerId => $aOtherBanner) {

        // mask banner name if anonymous campaign
        $campaign = Admin_DA::getPlacement($aOtherBanner['placement_id']);
        $campaignAnonymous = $campaign['anonymous'] == 't' ? true : false;
        $aOtherBanner['name'] = MAX_getAdName($aOtherBanner['name'], null, null, $campaignAnonymous, $otherBannerId);

        $otherBannerName = phpAds_buildName($otherBannerId, $aOtherBanner['name']);

        $page = "{$pageName}?{$otherEntityString}bannerid={$otherBannerId}&";
        if ($otherBannerId == $bannerId) {
            $current = true;
            $bannerName = $otherBannerName;
        } else {
            $current = false;
        }
        phpAds_PageContext($otherBannerName, $page, $current);
    }

    if (OA_Permission::hasAccessToObject('clients', $advertiserId)) {
        phpAds_PageShortcut($GLOBALS['strClientProperties'], "advertiser-edit.php?clientid=$advertiserId", 'images/icon-advertiser.gif');
    }
    if (!OA_Permission::isAccount(OA_ACCOUNT_ADVERTISER)) {
        phpAds_PageShortcut($GLOBALS['strCampaignProperties'], "campaign-edit.php?clientid=$advertiserId&campaignid=$campaignId", 'images/icon-campaign.gif');
    }
    phpAds_PageShortcut($GLOBALS['strBannerHistory'], "stats.php?entity=banner&breakdown=history&$entityString", 'images/icon-statistics.gif');

    $extra  = "
<form action='banner-modify.php'>
<input type='hidden' name='clientid' value='$advertiserId'>
<input type='hidden' name='campaignid' value='$campaignId'>
<input type='hidden' name='bannerid' value='$bannerId'>
<input type='hidden' name='returnurl' value='$pageName'>
<br /><br />
<b>{$GLOBALS['strModifyBanner']}</b><br />
<img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
<img src='" . MAX::assetPath() . "/images/icon-duplicate-banner.gif' align='absmiddle'>&nbsp;<a href='banner-modify.php?duplicate=true&$entityString&returnurl=$pageName'>{$GLOBALS['strDuplicate']}</a><br />
<img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
<img src='" . MAX::assetPath() . "/images/icon-move-banner.gif' align='absmiddle'>&nbsp;{$GLOBALS['strMoveTo']}<br />
<img src='" . MAX::assetPath() . "/images/spacer.gif' height='1' width='160' vspace='2'><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<select name='moveto' style='width: 120px;'>";
    $aOtherCampaigns = _multiSort($aOtherCampaigns,'name','placement_id');
    foreach ($aOtherCampaigns as $otherCampaignId => $aOtherCampaign) {

        // mask campaign name if anonymous campaign
        $aOtherCampaign['name'] = MAX_getPlacementName($aOtherCampaign);

        $otherCampaignName = MAX_buildName($aOtherCampaign['placement_id'], $aOtherCampaign['name']);

        if ($aOtherCampaign['placement_id'] != $campaignId) {
            $extra .= "
<option value='" . $aOtherCampaign['placement_id'] . "'>$otherCampaignName</option>";
        } else {
            $campaignName = $otherCampaignName;
        }
    }
    $deleteConfirm = phpAds_DelConfirm($GLOBALS['strConfirmDeleteBanner']);
    $extra .= "
</select>
<input type='image' name='moveto' src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/go_blue.gif'><br />
<img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
        ";
    if ($pageName == 'banner-acl.php') {
        $extra .= "<img src='" . MAX::assetPath() . "/images/icon-duplicate-acl.gif' align='absmiddle'>&nbsp;{$GLOBALS['strApplyLimitationsTo']}<br />";
        $extra .= "<img src='" . MAX::assetPath() . "/images/spacer.gif' height='1' width='160' vspace='2'><br />";
        $extra .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        $extra .= "<select name='applyto' style='width: 110;'>";

        $aOtherBanners = _multiSort($aOtherBanners,'name','ad_id');
        foreach ($aOtherBanners as $idx => $aOtherBanner) {
            if ($aOtherBanner['ad_id'] != $bannerId) {
                $extra .= "<option value='{$aOtherBanner['ad_id']}'>" . MAX_buildName($aOtherBanner['ad_id'], $aOtherBanner['name']) . "</option>";
            }
        }
        $extra .= "</select>&nbsp;<input type='image' name='applyto' src='" . MAX::assetPath() . "/images/".$phpAds_TextDirection."/go_blue.gif'><br />";
        $extra .= "<img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />";
    }
    if (!OA_Permission::isAccount(OA_ACCOUNT_ADVERTISER)) {
        $extra .= "<img src='" . MAX::assetPath() . "/images/icon-recycle.gif' align='absmiddle'>&nbsp;<a href='banner-delete.php?$entityString&returnurl=campaign-banners.php'$deleteConfirm>{$GLOBALS['strDelete']}</a><br />";
    }
    $extra .= "</form>";
    $advertiserName = phpAds_getClientName($advertiserId);

    // Build ad preview
    if ($bannerId) {
        require_once (MAX_PATH . '/lib/max/Delivery/adRender.php');
        $aBanner = Admin_DA::getAd($bannerId);
        $aBanner['storagetype'] = $aBanner['type'];
        $aBanner['bannerid'] = $aBanner['ad_id'];
        $bannerCode = MAX_adRender($aBanner, 0, '', '', '', true, '', false, false);
    } else {
        $extra = '';
        $bannerCode = '';
    }
    phpAds_PageHeader($tabValue, $extra);
    echo "
<img src='" . MAX::assetPath() . "/images/icon-advertiser.gif' align='absmiddle'>&nbsp;$advertiserName&nbsp;<img src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/caret-rs.gif'>&nbsp;
<img src='" . MAX::assetPath() . "/images/icon-campaign.gif' align='absmiddle'>&nbsp;$campaignName&nbsp;<img src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/caret-rs.gif'>&nbsp;
<img src='" . MAX::assetPath() . "/images/icon-banner-stored.gif' align='absmiddle'>&nbsp;<b>$bannerName</b><br /><br />
$bannerCode<br /><br /><br /><br />";
    phpAds_ShowSections($tabSections);
}

function MAX_displayNavigationZone($pageName, $aOtherPublishers, $aOtherZones, $aEntities)
{

    global $phpAds_TextDirection;

    $extra = '';
    $publisherId = $aEntities['affiliateid'];
    $zoneId = $aEntities['zoneid'];
    $entityString = _getEntityString($aEntities);
    $aOtherEntities = $aEntities;
    unset($aOtherEntities['zoneid']);
    $otherEntityString = _getEntityString($aOtherEntities);
    $aPublisher = $aOtherPublishers[$publisherId];
    $publisherName = MAX_buildName($publisherId, $aPublisher['name']);
    $zoneName = (empty($zoneId)) ? $GLOBALS['strUntitled'] : MAX_buildName($zoneId, $aOtherZones[$zoneId]['name']);

    if (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER)) {
        $tabSections = array('4.2.3.2', '4.2.3.6', '4.2.3.3', '4.2.3.4', '4.2.3.5');
        // Determine which tab is highlighted
        switch ($pageName) {
            case 'zone-include.php'    : $tabValue = '4.2.3.3'; break;
            case 'zone-edit.php'       :
                if (empty($zoneId)) {
                    $tabValue = '4.2.3.1';
                    $tabSections = array('4.2.3.1');
                } else {
                    $tabValue = '4.2.3.2';
                }
                break;
            case 'zone-advanced.php'    : $tabValue = '4.2.3.6'; break;
            case 'zone-probability.php' : $tabValue = '4.2.3.4'; break;
            case 'zone-invocation.php'  : $tabValue = '4.2.3.5'; break;
        }
    } elseif (OA_Permission::isAccount(OA_ACCOUNT_TRAFFICKER)) {
        $tabSections = array();
        if (OA_Permission::hasPermission(OA_PERM_ZONE_EDIT)) { $tabSections[] = '2.1.1'; }
        if (OA_Permission::hasPermission(OA_PERM_ZONE_LINK)) { $tabSections[] = '2.1.2'; }
        $tabSections[] = '2.1.3';
        if (OA_Permission::hasPermission(OA_PERM_ZONE_INVOCATION)) { $tabSections[] = '2.1.4'; }
        switch($pageName) {
            case 'zone-edit.php': $tabValue = '2.1.1'; if (empty($zoneId)) $tabSections = array('2.1.1'); break;
            case 'zone-include.php': $tabValue = '2.1.2'; break;
            case 'zone-probability.php': $tabValue = '2.1.3'; break;
            case 'zone-invocation.php': $tabValue = '2.1.4'; break;
        }
    }
    // Sort the zones by name...
    require_once(MAX_PATH . '/lib/max/other/stats.php');
    MAX_sortArray($aOtherZones, 'name');

    foreach ($aOtherZones as $otherZoneId => $aOtherZone) {
        $otherZoneName = MAX_buildName($otherZoneId, $aOtherZone['name']);
        $page = "{$pageName}?{$otherEntityString}zoneid={$otherZoneId}&";
        $current = ($otherZoneId == $zoneId) ? true : false;
        phpAds_PageContext($otherZoneName, $page, $current);
    }

    if (OA_Permission::isAccount(OA_ACCOUNT_ADMIN, OA_ACCOUNT_MANAGER)) {
        phpAds_PageShortcut($GLOBALS['strAffiliateProperties'], "affiliate-edit.php?affiliateid=$publisherId", 'images/icon-affiliate.gif');
    }
    phpAds_PageShortcut($GLOBALS['strZoneHistory'], "stats.php?entity=zone&breakdown=history&$entityString", 'images/icon-statistics.gif');

    if (!empty($zoneId) && (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER) || (OA_Permission::hasPermission(OA_PERM_ZONE_EDIT) || OA_Permission::hasPermission(OA_PERM_ZONE_DELETE) || OA_Permission::hasPermission(OA_PERM_ZONE_ADD)))) {
        $extra = "
            <form action='zone-modify.php'>
            <input type='hidden' name='affiliateid' value='$publisherId'>
            <input type='hidden' name='zoneid' value='$zoneId'>
            <input type='hidden' name='returnurl' value='$pageName'>
            <br /><br />
            ";
        if (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER) || OA_Permission::hasPermission(OA_PERM_ZONE_DELETE)) {
            $extra .= "<b>{$GLOBALS['strModifyZone']}</b><br />
            <img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />";
        }
        if (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER) || OA_Permission::hasPermission(OA_PERM_ZONE_ADD)) {
                $extra .= "
                    <img src='" . MAX::assetPath() . "/images/icon-duplicate-zone.gif' align='absmiddle'>&nbsp;<a href='zone-modify.php?duplicate=true&$entityString&returnurl=$pageName'>{$GLOBALS['strDuplicate']}</a><br />
                    <img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
                ";
        }
        if (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER)) {
            $extra .= "
            <img src='" . MAX::assetPath() . "/images/icon-move-zone.gif' align='absmiddle'>&nbsp;{$GLOBALS['strMoveTo']}<br />
            <img src='" . MAX::assetPath() . "/images/spacer.gif' height='1' width='160' vspace='2'><br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <select name='newaffiliateid' style='width: 110;'>";
                $aOtherPublishers = _multiSort($aOtherPublishers,'name','publisher_id');
                foreach ($aOtherPublishers as $otherPublisherId => $aOtherPublisher) {
                    $otherPublisherName = MAX_buildName($aOtherPublisher['publisher_id'], $aOtherPublisher['name']);
                    if ($aOtherPublisher['publisher_id'] != $publisherId) {
                        $extra .= "
                <option value='" . $aOtherPublisher['publisher_id'] . "'>$otherPublisherName</option>";
                    }
                }
        }
        if (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER) || OA_Permission::hasPermission(OA_PERM_ZONE_DELETE)) {
            $deleteConfirm = phpAds_DelConfirm($GLOBALS['strConfirmDeleteZone']);
            $extra .= "
                </select>
                <input type='image' src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/go_blue.gif'><br />
                <img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
                <img src='" . MAX::assetPath() . "/images/icon-recycle.gif' align='absmiddle'>&nbsp;<a href='zone-delete.php?$entityString&returnurl=affiliate-zones.php'$deleteConfirm>{$GLOBALS['strDelete']}</a><br />
            ";
        }
        $extra .= "</form>";
    }
    phpAds_PageHeader($tabValue, $extra);
    if (!OA_Permission::isAccount(OA_ACCOUNT_TRAFFICKER)) {
        echo "<img src='" . MAX::assetPath() . "/images/icon-affiliate.gif' align='absmiddle'>&nbsp;$publisherName&nbsp;<img src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/caret-rs.gif'>&nbsp;";
    }
    echo "<img src='" . MAX::assetPath() . "/images/icon-zone.gif' align='absmiddle'>&nbsp;<b>$zoneName</b><br /><br /><br />";
    phpAds_ShowSections($tabSections);

}

function MAX_displayNavigationPublisher($pageName, $aOtherPublishers, $aEntities)
{
    global $phpAds_TextDirection;

    $publisherId = $aEntities['affiliateid'];
    $entityString = _getEntityString($aEntities);
    $aOtherEntities = $aEntities;
    unset($aOtherEntities['affiliateid']);
    $otherEntityString = _getEntityString($aOtherEntities);

    $tabSections = array('4.2.2', '4.2.3', '4.2.4', '4.2.5', '4.2.6', '4.2.7');

    // Determine which tab is highlighted
    switch ($pageName) {
        case 'affiliate-channels.php' : $tabValue = '4.2.4'; break;
        case 'affiliate-advsetup.php' : $tabValue = '4.2.6'; break;
    }

    // Sort the publishers by name...
    require_once(MAX_PATH . '/lib/max/other/stats.php');
    MAX_sortArray($aOtherPublishers, 'name');

    foreach ($aOtherPublishers as $otherPublisherId => $aOtherPublisher) {
        $otherPublisherName = MAX_buildName($otherPublisherId, $aOtherPublisher['name']);
        $page = "{$pageName}?{$otherEntityString}affiliateid={$otherPublisherId}&";
        if ($otherPublisherId == $publisherId) {
            $current = true;
            $publisherName = $otherPublisherName;
        } else {
            $current = false;
        }
        phpAds_PageContext($otherPublisherName, $page, $current);
    }

    phpAds_PageShortcut($GLOBALS['strAffiliateHistory'], 'stats.php?entity=affiliate&breakdown=history&affiliateid='.$publisherId, 'images/icon-statistics.gif');

    phpAds_PageHeader($tabValue, $extra = '');
    echo "<img src='" . MAX::assetPath() . "/images/icon-affiliate.gif' align='absmiddle'>&nbsp;<b>$publisherName</b><br /><br /><br />";
    phpAds_ShowSections($tabSections);
}

function MAX_displayNavigationChannel($pageName, $aOtherChannels, $aEntities)
{
    global $phpAds_TextDirection;

    $agencyId = isset($aEntities['agencyid']) ? $aEntities['agencyid'] : null;
    $publisherId = isset($aEntities['affiliateid']) ? $aEntities['affiliateid'] : null;
    $channelId = $aEntities['channelid'];

    $entityString = _getEntityString($aEntities);
    $aOtherEntities = $aEntities;
    unset($aOtherEntities['channelid']);
    $otherEntityString = _getEntityString($aOtherEntities);

    if (!empty($publisherId)) {
        $channelType = 'publisher';
    } else {
        $channelType = 'agency';
    }

    // Determine which set of tabs to show...
    if ($channelType == 'publisher') {
        // Determine which tab is highlighted
        switch ($pageName) {
            case 'channel-edit.php' : $tabValue = (!empty($channelId)) ? '4.2.4.2' : '4.2.4.1'; break;
            case 'channel-acl.php' : $tabValue = '4.2.4.3'; break;
        }
        $tabSections = (!empty($channelId)) ? array('4.2.4.2', '4.2.4.3') : array('4.2.4.1');
    } else {
        // Determine which tab is highlighted
        switch ($pageName) {
            case 'channel-edit.php' : $tabValue = (!empty($channelId)) ? '5.7.2' : '5.7.1'; break;
            case 'channel-acl.php' : $tabValue = '5.7.3'; break;
        }
        $tabSections = (!empty($channelId)) ? array('5.7.2', '5.7.3') : array('5.7.1');
    }

    // Sort the channels by name...
    require_once(MAX_PATH . '/lib/max/other/stats.php');
    MAX_sortArray($aOtherChannels, 'name');

    foreach ($aOtherChannels as $otherChannelId => $aOtherChannel) {
        $otherChannelName = MAX_buildName($otherChannelId, $aOtherChannel['name']);
        $page = "{$pageName}?{$otherEntityString}channelid={$otherChannelId}&";
        if ($otherChannelId == $channelId) {
            $current = true;
            $channelName = $otherChannelName;
        } else {
            $current = false;
        }
        phpAds_PageContext($otherChannelName, $page, $current);
    }

    if ($channelType == 'publisher') {
        phpAds_PageShortcut($GLOBALS['strAffiliateProperties'], "affiliate-edit.php?affiliateid=$publisherId", 'images/icon-affiliate.gif');
        phpAds_PageShortcut($GLOBALS['strAffiliateHistory'], "stats.php?entity=affiliate&breakdown=history&affiliateid=$publisherId", 'images/icon-statistics.gif');
    }

    $extra  = "
<form action='channel-modify.php'>
<input type='hidden' name='affiliateid' value='$publisherId'>
<input type='hidden' name='channelid' value='$channelId'>
<input type='hidden' name='returnurl' value='$pageName'>
<br /><br />
<b>{$GLOBALS['strChannel']}</b><br />
<img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
<img src='" . MAX::assetPath() . "/images/icon-duplicate-channel.gif' align='absmiddle'>&nbsp;<a href='channel-modify.php?duplicate=true&{$entityString}returnurl=$pageName'>{$GLOBALS['strDuplicate']}</a><br />";

    if ($channelType == 'publisher') {
        $deleteReturlUrl = 'affiliate-channels.php';
    } else {
        $deleteReturlUrl = 'channel-index.php';
    }

    $deleteConfirm = phpAds_DelConfirm($GLOBALS['strConfirmDeleteChannel']);
    $extra .= "
<img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='160' vspace='4'><br />
<img src='" . MAX::assetPath() . "/images/icon-recycle.gif' align='absmiddle'>&nbsp;<a href='channel-delete.php?{$entityString}returnurl={$deleteReturlUrl}'{$deleteConfirm}>{$GLOBALS['strDelete']}</a><br />
</form>";

    if (!empty($publisherId)) {
        // Determine which tab is highlighted
        if (!empty($channelId)) {
            phpAds_PageHeader($tabValue, $extra);
        } else {
            phpAds_PageHeader($tabValue);
            $channelName = $GLOBALS['strUntitled'];
        }
        $publisherName = phpAds_getAffiliateName($publisherId);
        echo "<img src='" . MAX::assetPath() . "/images/icon-affiliate.gif' align='absmiddle'>&nbsp;$publisherName&nbsp;<img src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/caret-rs.gif'>&nbsp;";
    } else {
        if (!empty($channelId)) {
            phpAds_PageHeader($tabValue, $extra);
        } else {
            phpAds_PageHeader($tabValue);
            $channelName = $GLOBALS['strUntitled'];
        }
    }

    echo "<img src='" . MAX::assetPath() . "/images/icon-channel.gif' align='absmiddle'>&nbsp;<b>$channelName</b><br /><br /><br />";
    phpAds_ShowSections($tabSections);

}

function MAX_displayZoneEntitySelection($entity, $aOtherAdvertisers, $aOtherPlacements, $aOtherAds, $advertiserId, $placementId, $adId, $publisherId, $zoneId, $title, $pageName, &$tabIndex)
{
    echo "
<br />$title<br /><br />
<table cellpadding='0' cellspacing='0' border='0'>
<tr>";
    $aSavedEntities = array('affiliateid' => $publisherId, 'zoneid' => $zoneId);
    _displayZoneEntitySelectionCell('advertiser', $advertiserId, $aOtherAdvertisers, 'clientid', $aSavedEntities, ($entity != 'advertiser'), $pageName, $tabIndex);

    if (!empty($advertiserId) && $entity != 'advertiser') {
        $aSavedEntities['clientid'] = $advertiserId;
        _displayZoneEntitySelectionCell('placement', $placementId, $aOtherPlacements, 'campaignid', $aSavedEntities, ($entity != 'placement'), $pageName, $tabIndex);

        if (!empty($placementId) && $entity != 'placement') {
            $aSavedEntities['campaignid'] = $placementId;
            _displayZoneEntitySelectionCell('ad', $adId, $aOtherAds, 'bannerid', $aSavedEntities, ($entity != 'ad'), $pageName, $tabIndex);
        }
    }
    echo "
</tr>
</table>
<br /><br />";
}

function _displayZoneEntitySelectionCell($entity, $entityId, $aOtherEntities, $entityIdName, $aSavedEntities, $autoSubmit, $pageName, &$tabIndex)
{
    global $phpAds_TextDirection;

    $onChange = $autoSubmit ? " onChange='this.form.submit();'" : '';
    $submitIcon = $autoSubmit ? '' : "&nbsp;<input type='hidden' name='action' value='set'><input type='image' src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/go_blue.gif' border='0' tabindex='".($tabIndex++)."'>";
    $tabInfo = " tabindex='" . ($tabIndex++) . "'";
    $entityIcon = MAX_getEntityIcon($entity);
    echo "
<form name='zonetypeselection' method='get' action='$pageName'>";
    foreach($aSavedEntities as $savedEntityName => $savedEntityId) {
        echo "
<input type='hidden' name='$savedEntityName' value='$savedEntityId'>";
    }
    echo "
<td>
    &nbsp;&nbsp;<img src='$entityIcon' align='absmiddle'>&nbsp;
    <select name='$entityIdName'{$onChange}{$tabInfo}>";
    // Show an empty value in the dropdown if none is selected
    if (empty($entityId)) {
        switch ($entity) {
            case 'advertiser' : $description = "-- {$GLOBALS['strSelectAdvertiser']} --"; break;
            case 'placement'  : $description = "-- {$GLOBALS['strSelectPlacement']} --"; break;
            case 'ad' : $description = "-- {$GLOBALS['strSelectAd']} --"; break;
            default : $description = '';
        }
        echo "
        <option value='' selected>$description</option>";
    }

    $aOtherEntities = _multiSort($aOtherEntities, 'name', 'advertiser_id');
    foreach ($aOtherEntities as $aOtherEntity) {
        switch ($entity) {
            case 'advertiser':
                $otherEntityId = $aOtherEntity['advertiser_id'];
                break;
            case 'placement':
                $otherEntityId = $aOtherEntity['placement_id'];
                break;
            case 'ad':
                $otherEntityId = $aOtherEntity['ad_id'];
                break;
        }
        $selected = $otherEntityId == $entityId ? ' selected' : '';

        if ($entity == 'placement') {
            $aParams = array('placement_id' => $otherEntityId);
            $aParams += MAX_getLinkedAdParams($GLOBALS['zoneId']);

            //$adsCount = "(" . count(Admin_DA::getAds($aParams)) . ")";
            $translation = new OA_Translation();
            $aStringParams["bannerCount"] = count(Admin_DA::getAds($aParams));
            $translated = $translation->translate($GLOBALS['strWithXBanners'], $aStringParams);
            $adsCount = "(" .$translated.")";
        } else {
            $adsCount = '';
        }

        $name = MAX_buildName($otherEntityId, $aOtherEntity['name']);
        echo "
        <option value='$otherEntityId'{$selected}>$name $adsCount</option>";
    }
    echo "
    </select>
    $submitIcon
</td>
</form>";
}

function MAX_displayLinkedAdsPlacements($aParams, $publisherId, $zoneId, $hideInactive, $showParentPlacements, $pageName, &$tabIndex)
{
    global $phpAds_TextDirection, $phpAds_TextAlignRight;

    echo "
<table id='linked-banners' width='100%' border='0' align='center' cellspacing='0' cellpadding='0'>
<tr height='25'>
<td width='40%'><b>&nbsp;&nbsp;{$GLOBALS['strName']}</b></td>
<td><b>{$GLOBALS['strID']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>
<td>&nbsp;</td>
</tr>
<tr height='1'>
<td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td>
</tr>";
    $i = 0;
    $inactive = 0;
    $aPlacements = !empty($aParams) ? Admin_DA::getPlacements($aParams) : array();
    foreach ($aPlacements as $placementId => $aPlacement) {
        $aAds = Admin_DA::getAds($aParams + array('placement_id' => $placementId), true);
        $placementActive = $aPlacement['status'] == OA_ENTITY_STATUS_RUNNING;
        if (!$hideInactive || $placementActive) {
            $bgcolor = $i % 2 == 0 ? " bgcolor='#F6F6F6'" : '';
            if ($showParentPlacements) {
                $placementIcon = MAX_getEntityIcon('placement', $placementActive);
                $placementName = MAX_getDisplayName($aPlacement['name']);
                $placementLink = (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER)) ? "<a href='campaign-edit.php?clientid={$aPlacement['advertiser_id']}&campaignid=$placementId'>$placementName</a>" : $placementName;
                if ($i > 0) {
                    echo "
<tr height='1'>
<td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break-l.gif' height='1' width='100%'></td>
</tr>";
            }
            echo "
<tr height='25'$bgcolor>
<td>
    &nbsp;&nbsp;<img src='$placementIcon' align='absmiddle'>
    &nbsp;$placementLink
</td>
<td>$placementId</td>
<td>&nbsp;</td>
</tr>";
            }
            foreach ($aAds as $adId => $aAd) {
                $adActive = ($aAd['status'] == OA_ENTITY_STATUS_RUNNING && $aPlacement['status'] == OA_ENTITY_STATUS_RUNNING);
                if (!$hideInactive || $adActive) {
                    $adIcon = MAX_getEntityIcon('ad', $adActive, $aAd['type']);
                    $adName = MAX_getDisplayName($aAd['name']);
                    $adLink = (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER)) ? "<a href='banner-edit.php?clientid={$aPlacement['advertiser_id']}&campaignid=$placementId&bannerid=$adId'>$adName</a>" : $adName;
                    $adWidth = $aAd['contenttype'] == 'txt' ? 300 : $aAd['width'] + 64;
                    $adHeight = $aAd['contenttype'] == 'txt' ? 200 : $aAd['height'] + (!empty($aAd['bannertext']) ? 90 : 64);
                    echo "
<tr height='1'>
<td$bgcolor><img src='" . MAX::assetPath() . "/images/spacer.gif' width='1' height='1'></td>
<td colspan='2' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break-el.gif' height='1' width='100%'></td>
</tr>
<tr height='25'$bgcolor>
<td>
    &nbsp;&nbsp;<a href='$pageName?affiliateid=$publisherId&zoneid=$zoneId&bannerid=$adId&action=remove'><img src='" . MAX::assetPath() . "/images/caret-l.gif' border='0' align='absmiddle'></a>
    &nbsp;&nbsp;&nbsp;&nbsp;<img src='$adIcon' align='absmiddle'>&nbsp;$adLink</td>
<td>$adId</td>
<td align='$phpAds_TextAlignRight'>
    <img src='" . MAX::assetPath() . "/images/icon-zoom.gif' align='absmiddle' border='0'>&nbsp;<a href='banner-htmlpreview.php?bannerid=$adId' target='_new' onClick=\"return openWindow('banner-htmlpreview.php?bannerid=$adId', '', 'status=no,scrollbars=no,resizable=no,width=$adWidth,height=$adHeight');\">{$GLOBALS['strShowBanner']}</a>&nbsp;&nbsp;
</td>
</tr>";
                } else {
                    $inactive++;
                }
            }
            $i++;
        } else {
            $inactive += count($aAds);
        }
    }
    $showParentText = $showParentPlacements ? $GLOBALS['strHideParentCampaigns'] : $GLOBALS['strShowParentCampaigns'];
    $showParentValue = $showParentPlacements ? '0': '1';
    $hideInactiveText = $hideInactive ? $GLOBALS['strShowAll'] : $GLOBALS['strHideInactiveBanners'];
    $hideInactiveStats = $hideInactive ? "&nbsp;&nbsp;|&nbsp;&nbsp;$inactive {$GLOBALS['strInactiveBannersHidden']}" : '';
    $hideInactiveValue = $hideInactive ? '0' : '1';
    $hideInactiveIcon = MAX::assetPath($hideInactive ? 'images/icon-activate.gif' : 'images/icon-hideinactivate.gif');
    echo "
<tr height='1'>
<td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td>
</tr>
<tr height='25'>
<td colspan='2'>
    <img src='$hideInactiveIcon' align='absmiddle' border='0'>
    <a href='$pageName?affiliateid=$publisherId&zoneid=$zoneId&hideinactive=$hideInactiveValue'>$hideInactiveText</a>$hideInactiveStats
</td>
<td align='right'>
    <img src='" . MAX::assetPath() . "/images/icon-campaign-d.gif' align='absmiddle' border='0'>
    <a href='$pageName?affiliateid=$publisherId&zoneid=$zoneId&showcampaigns=$showParentValue'>$showParentText</a>
</table>";
}

function MAX_displayLinkedPlacementsAds($aParams, $publisherId, $zoneId, $hideInactive, $showMatchingAds, $pageName, &$tabIndex, $directLinkedAds=false)
    {
        echo "
    <br /><strong>{$GLOBALS['strCampaignLinkedAds']}:</strong><br />
    <table id='linked-campaigns' width='100%' border='0' align='center' cellspacing='0' cellpadding='0'>
    <tr height='25'>
        <td width='40%'><b>&nbsp;&nbsp;{$GLOBALS['strName']}</b></td>
        <td><b>{$GLOBALS['strID']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td>
        <td>&nbsp;</td>
    </tr>
    <tr height='1'>
        <td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td>
    </tr>";

        $i = 0;
        $inactive = 0;
        $aPlacements = (!empty($aParams)) ? Admin_DA::getPlacements($aParams) : array();
        foreach ($aPlacements as $placementId => $aPlacement) {
            $placementActive = $aPlacement['status'] == OA_ENTITY_STATUS_RUNNING;
            if (!$hideInactive || $placementActive) {
                $pParams = $aParams;
                $pParams['placement_id'] = $placementId;
                $aAds = Admin_DA::getAds($pParams, true);
                $bgcolor = $i % 2 == 0 ? " bgcolor='#F6F6F6'" : '';
                // Remove these ad(s) from the direct linked ads
                foreach ($aAds as $dAdId) {
                    unset($directLinkedAds[$dAdId['ad_id']]);
                }

                // Remove from array any ads not linked to the zone.
                // These might exist if campaign has been linked to zone
                // and indivual ads have then been unlinked
                $pParams = array('zone_id' => $zoneId);
                $aAdZones = Admin_DA::getAdZones($pParams, true);
                $aAdZoneLinks = array();
                foreach($aAdZones as $aAdZone) {
                    $aAdZoneLinks[] = $aAdZone['ad_id'];
                }
                foreach($aAds as $adId => $aAd) {
                    if (!in_array($adId, $aAdZoneLinks)) {
                        unset($aAds[$adId]);
                    }
                }

                $placementIcon = MAX_getEntityIcon('placement', $placementActive);
                $placementName = MAX_getDisplayName($aPlacement['name']);
                $placementLink = (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER)) ? "<a href='campaign-edit.php?clientid={$aPlacement['advertiser_id']}&campaignid=$placementId'>$placementName</a>" : $placementName;
                $adCount = empty($aAds) ? 0 : count($aAds);
                $placementDescription = $showMatchingAds ? '&nbsp;' : str_replace('{count}', $adCount, $GLOBALS['strMatchingBanners']);
                if ($i > 0) {
                    echo "
    <tr height='1'>
        <td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break-l.gif' height='1' width='100%'></td>
    </tr>";
                }
                echo "
    <tr height='25'$bgcolor>
        <td>
            &nbsp;&nbsp;<a href='$pageName?affiliateid=$publisherId&zoneid=$zoneId&campaignid=$placementId&action=remove'><img src='" . MAX::assetPath() . "/images/caret-l.gif' border='0' align='absmiddle'></a>
            &nbsp;&nbsp;<img src='$placementIcon' align='absmiddle'>
            &nbsp;$placementLink
        </td>
        <td>$placementId</td>
        <td>$placementDescription</td>
    </tr>";
                if ($showMatchingAds && !empty($aAds)) {
                    foreach ($aAds as $adId => $aAd) {
                        $adActive = ($aAd['status'] == OA_ENTITY_STATUS_RUNNING && $aPlacement['status'] == OA_ENTITY_STATUS_RUNNING);
                        if (!$hideInactive || $adActive) {
                            $adIcon = MAX_getEntityIcon('ad', $adActive, $aAd['type']);
                            $adName = MAX_getDisplayName($aAd['name']);
                            $adLink = (OA_Permission::isAccount(OA_ACCOUNT_ADMIN) || OA_Permission::isAccount(OA_ACCOUNT_MANAGER)) ? "<a href='banner-edit.php?clientid={$aPlacement['advertiser_id']}&campaignid=$placementId&bannerid=$adId'>$adName</a>" : $adName;
                            $adWidth = $aAd['contenttype'] == 'txt' ? 300 : $aAd['width'] + 64;
                            $adHeight = $aAd['contenttype'] == 'txt' ? 200 : $aAd['height'] + (!empty($aAd['bannertext']) ? 90 : 64);
                            echo "
    <tr height='1'>
        <td$bgcolor><img src='" . MAX::assetPath() . "/images/spacer.gif' width='1' height='1'></td>
        <td colspan='2' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break-el.gif' height='1' width='100%'></td>
    </tr>
    <tr height='25'$bgcolor>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='$adIcon' align='absmiddle'>&nbsp;$adLink</td>
        <td>$adId</td>
        <td align=".$GLOBALS['phpAds_TextAlignRight'].">
            <img src='" . MAX::assetPath() . "/images/icon-zoom.gif' align='absmiddle' border='0'>&nbsp;<a href='banner-htmlpreview.php?bannerid=$adId' target='_new' onClick=\"return openWindow('banner-htmlpreview.php?bannerid=$adId', '', 'status=no,scrollbars=no,resizable=no,width=$adWidth,height=$adHeight');\">{$GLOBALS['strShowBanner']}</a>&nbsp;&nbsp;
        </td>
    </tr>";
                        } else {
                            $inactive++;
                        }
                    }
                }
                $i++;
            } else {
                $inactive++;
            }
        }
        $showMatchingText = $showMatchingAds ? $GLOBALS['strHideMatchingBanners'] : $GLOBALS['strShowMatchingBanners'];
        $showMatchingValue = $showMatchingAds ? '0' : '1';
        $hideInactiveText = $hideInactive ? $GLOBALS['strShowAll'] : $GLOBALS['strHideInactiveCampaigns'];
        $hideInactiveStats = $hideInactive ? "&nbsp;&nbsp;|&nbsp;&nbsp;$inactive {$GLOBALS['strInactiveCampaignsHidden']}" : '';
        $hideInactiveValue = $hideInactive ? '0' : '1';
        $hideInactiveIcon = MAX::assetPath($hideInactive ? 'images/icon-activate.gif' : 'images/icon-hideinactivate.gif');
        echo "
    <tr height='1'>
        <td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td>
    </tr>
    <tr height='25'>
        <td colspan='2'>
            <img src='$hideInactiveIcon' align='absmiddle' border='0'>
            <a href='$pageName?affiliateid=$publisherId&zoneid=$zoneId&hideinactive=$hideInactiveValue'>$hideInactiveText</a>$hideInactiveStats
        </td>
        <td align='right'>
            <img src='" . MAX::assetPath() . "/images/icon-banner-stored-d.gif' align='absmiddle' border='0'>
            <a href='$pageName?affiliateid=$publisherId&zoneid=$zoneId&showbanners=$showMatchingValue'>$showMatchingText</a>
    </table>";
        if (!empty($directLinkedAds)) {
            echo "<br /><strong>{$GLOBALS['strBannerLinkedAds']}:</strong><br />";
            $aParams = array('ad_id' => implode(',', array_keys($directLinkedAds)));
            MAX_displayLinkedAdsPlacements($aParams, $publisherId, $zoneId, $hideInactive, $showParentPlacements, $pageName, $tabIndex);
        }
    }

function MAX_displayPlacementAdSelectionViewForm($publisherId, $zoneId, $view, $pageName, &$tabIndex, $aOtherZones = array())
{
    global $phpAds_TextDirection;

    $disabled = null;
    $disabledHidden = null;
    if (!empty($aOtherZones[$zoneId]['type'])) {
        if ($aOtherZones[$zoneId]['type'] == MAX_ZoneEmail) {
            $view = 'ad';
            $disabled = ' disabled';
            $disabledHidden = "<input type='hidden' name='view' value='ad' />";
        }
    }
    $placementSelected = $view == 'placement' ? ' selected' : '';
    $categorySelected = $view == 'category' ? ' selected' : '';
    $adSelected = $view == 'ad' ? ' selected' : '';

    echo "
<form name='zoneview' method='post' action='$pageName'>
<input type='hidden' name='zoneid' value='$zoneId'>
<input type='hidden' name='affiliateid' value='$publisherId'>
<table border='0' width='100%' cellpadding='0' cellspacing='0'>
<tr height='25'>
<td colspan='3'><b>{$GLOBALS['strSelectZoneType']}</b></td>
</tr>
<tr height='25'>
<td>
    <select name='view' onchange='this.form.submit();' $disabled>
        <option value='placement'$placementSelected>{$GLOBALS['strCampaignDefaults']}</option>
        <!--option value='category'$categorySelected>{$GLOBALS['strLinkedCategories']}</option-->
        <option value='ad'$adSelected>{$GLOBALS['strLinkedBanners']}</option>
    </select>
    &nbsp;<input type='image' src='" . MAX::assetPath() . "/images/$phpAds_TextDirection/go_blue.gif' border='0'>
    $disabledHidden
</td>
</tr>
</table>";
    phpAds_ShowBreak();
    echo "
</form>
<br />";
}

function MAX_displayAcls($acls, $aParams) {
    $tabindex =& $GLOBALS['tabindex'];
    $page = basename($_SERVER['PHP_SELF']);
    $conf = $GLOBALS['_MAX']['CONF'];

    echo "<form action='{$page}' method='post'>";

    echo "<label><img src='" . MAX::assetPath() . "/images/icon-acl-add.gif' align='absmiddle'>&nbsp;". $GLOBALS['strACLAdd'] .": &nbsp;";
    echo "<select name='type' accesskey='{$GLOBALS['keyAddNew']}' tabindex='".($tabindex++)."'>";

    $deliveryLimitations = MAX_Plugin::getPlugins('deliveryLimitations', null, false);
    foreach ($deliveryLimitations as $pluginName => $plugin) {
        if ($plugin->isAllowed($page)) {
            echo "<option value='{$pluginName}'>" . MAX_Plugin_Translation::translate($plugin->package, $plugin->module, $plugin->package) . ' - ' . $plugin->getName() . "</option>";
        }
    }

    echo "</select></label>";
    echo "&nbsp;";
    echo "<input type='submit' class='flat' name='action[new]' value='" . $GLOBALS['strAdd'] . "'";

    phpAds_ShowBreak();
    echo "<br />";

    if (!empty($GLOBALS['action'])) {
        // We are part way through making changes, show a message
        //echo "<br>";
        echo "<div class='errormessage'><img class='errormessage' src='" . MAX::assetPath() . "/images/warning.gif' align='absmiddle'>";
        echo "<span class='tab-s'>{$GLOBALS['strUnsavedChanges']}</span><br>";
        echo "</div>";
    } elseif (!MAX_AclValidate($page, $aParams)) {
        echo "<div class='errormessage'><img class='errormessage' src='" . MAX::assetPath() . "/images/warning.gif' align='absmiddle'>";
        echo "<span class='tab-r'>{$GLOBALS['strDeliveryLimitationsDisagree']}</span><br>";
        echo "</div>";
    }

    echo "<form action='{$page}' method='post'>";
    foreach ($aParams as $name => $value) {
        echo "<input type='hidden' name='{$name}' value='{$value}' />";
    }
    echo "<table border='0' width='100%' cellpadding='0' cellspacing='0'>";
    echo "<tr><td height='25' colspan='4' bgcolor='#FFFFFF'><b>{$GLOBALS['strDeliveryLimitations']}</b></td></tr>";
    echo "<tr><td height='1' colspan='4' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>";

    if (empty($acls)) {
        echo "<tr><td height='24' colspan='4' bgcolor='#F6F6F6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$GLOBALS['strNoLimitations']}</td></tr>";
        echo "<tr><td height='1' colspan='4' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>";
    } else {
        echo "<tr><td height='25' colspan='4' bgcolor='#F6F6F6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$GLOBALS['strOnlyDisplayWhen']}</td></tr>";
        echo "<tr><td colspan='4'><img src='" . MAX::assetPath() . "/images/break-el.gif' width='100%' height='1'></td></tr>";

        foreach ($acls as $aclId => $acl) {
            list($package, $name) = explode(':', $acl['type']);
            $deliveryLimitationPlugin = MAX_Plugin::factory('deliveryLimitations', ucfirst($package), ucfirst($name));
            $deliveryLimitationPlugin->init($acl);
            $deliveryLimitationPlugin->count = count($acls);
            if ($deliveryLimitationPlugin->isAllowed($page)) {
                $deliveryLimitationPlugin->display();
            }
        }
    }

    echo "<tr><td height='30' colspan='2'>";

    if (!empty($acls)) {
        $url = $page . '?';
        foreach ($aParams as $name => $value) {
            $url .= "{$name}={$value}&";
        }
        $url .= "action[clear]=true";
        echo "<img src='" . MAX::assetPath() . "/images/icon-recycle.gif' border='0' align='absmiddle'>&nbsp;
                <a href='{$url}'>{$GLOBALS['strRemoveAllLimitations']}</a>&nbsp;&nbsp;&nbsp;&nbsp;
        ";
    }

    echo "</td><td height='30' colspan='2' align='{$GLOBALS['phpAds_TextAlignRight']}'>";
    echo "</td></tr>";

    echo "</table>";
}

function MAX_displayChannels($channels, $aParams) {
    $entityString = _getEntityString($aParams);

    echo "<br /><br />";
    echo "<table border='0' width='100%' cellpadding='0' cellspacing='0'>";

    echo "<tr height='25'><td height='25'><b>&nbsp;&nbsp;{$GLOBALS['strName']}</a></b></td>";

    echo "<td height='25'><b>{$GLOBALS['strID']}</a></td>";
    echo "<td height='25'>&nbsp;</td>";
    echo "</tr>";

    echo "<tr height='1'><td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>";

    if (empty($channels)) {
        echo "<tr height='25' bgcolor='#F6F6F6'><td height='25' colspan='3'>";
        echo "&nbsp;&nbsp;{$GLOBALS['strNoChannels']}</td></tr>";

        echo "<td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>";
    } else {
        $i = 0;
        foreach ($channels as $channelId => $channel) {
            if ($i > 0) echo "<td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td>";
            echo "<tr height='25' ".($i%2==0?"bgcolor='#F6F6F6'":"").">";
            echo "<td height='25'>&nbsp;&nbsp;";
            echo "<img src='" . MAX::assetPath() . "/images/icon-channel.gif' align='absmiddle'>&nbsp;";

            // set channel ownership info for display
            if ($GLOBALS['pageName'] != 'affiliate-channels.php') {
                if (!empty($channel['publisher_id'])) {
                    $ownerTypeStr = 'Publisher: ';
                    $publisher = Admin_DA::getPublisher($channel['publisher_id']);
                    $ownerNameStr = '[id'.$channel['publisher_id'].'] '.$publisher['name'];
                } else if (!empty($channel['agency_id']) && empty($channel['publisher_id'])
                       && !OA_Permission::isAccount(OA_ACCOUNT_MANAGER)) {
                    $ownerTypeStr = 'Agency: ';
                    $agency = Admin_DA::getAgency($channel['agency_id']);
                    $ownerNameStr = '[id'.$channel['agency_id'].'] '.$agency['name'];
                } else {
                    $ownerTypeStr = '';
                    $ownerNameStr = '';
                }
            }
            $ownerStr = !empty($ownerTypeStr) ? '&nbsp&nbsp<i>'.$ownerTypeStr.'</i>'.$ownerNameStr : '';

            echo "<a href='channel-edit.php?{$entityString}channelid={$channel['channel_id']}'>{$channel['name']}$ownerStr</a>";
            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            echo "</td>";
            echo "<td height='25'>{$channel['channel_id']}</td>";
            echo "<td>&nbsp;</td></tr>";

            // Description
            echo "<tr height='25' ".($i%2==0?"bgcolor='#F6F6F6'":"").">";
            echo "<td>&nbsp;</td>";
            echo "<td height='25' colspan='3'>".stripslashes($channel['description'])."</td>";
            echo "</tr>";

            echo "<tr height='1'>";
            echo "<td ".($i%2==0?"bgcolor='#F6F6F6'":"")."><img src='" . MAX::assetPath() . "/images/spacer.gif' width='1' height='1'></td>";
            echo "<td colspan='3' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break-l.gif' height='1' width='100%'></td>";
            echo "</tr>";
            echo "<tr height='25' ".($i%2==0?"bgcolor='#F6F6F6'":"").">";

            // Empty
            echo "<td>&nbsp;</td>";

            // Buttons
            echo "<td height='25' colspan='3'>";

            echo "<img src='" . MAX::assetPath() . "/images/icon-acl.gif' border='0' align='absmiddle' alt='{$GLOBALS['strIncludedBanners']}'>&nbsp;<a href='channel-acl.php?{$entityString}channelid={$channel['channel_id']}'>{$GLOBALS['strEditChannelLimitations']}</a>&nbsp;&nbsp;&nbsp;&nbsp;";
            echo "<img src='" . MAX::assetPath() . "/images/icon-recycle.gif' border='0' align='absmiddle' alt='{$GLOBALS['strDelete']}'>&nbsp;<a href='channel-delete.php?{$entityString}channelid={$channel['channel_id']}&returnurl=".(empty($aParams['affiliateid']) ? 'channel-index.php' : 'affiliate-channels.php')."'".phpAds_DelConfirm($GLOBALS['strConfirmDeleteChannel']).">{$GLOBALS['strDelete']}</a>&nbsp;&nbsp;&nbsp;&nbsp;";

            echo "</td></tr>";
            $i++;
        }
        if (!empty($channels)) {
            echo "<tr height='1'><td colspan='4' bgcolor='#888888'><img src='" . MAX::assetPath() . "/images/break.gif' height='1' width='100%'></td></tr>";
        }
    }
    echo "</table>";
}

// Determine whether an advertiser has an active placement/ad running under it...
function _isAdvertiserActive($aAdvertiserPlacementAd)
{
    $active = false;
    if (isset($aAdvertiserPlacementAd['children'])) {
        foreach($aAdvertiserPlacementAd['children'] as $aPlacementAd) {
            if (_isPlacementActive($aPlacementAd)) {
                $active = true;
                break;
            }
        }
    }
    return $active;
}

// Determine whether a placement has an active ad running under it...
function _isPlacementActive($aPlacementAd)
{
    $active = false;
    if ($aPlacementAd['status'] == OA_ENTITY_STATUS_RUNNING) {
        if (isset($aPlacementAd['children'])) {
            foreach($aPlacementAd['children'] as $aAd) {
                if ($aAd['status'] == OA_ENTITY_STATUS_RUNNING) {
                    $active = true;
                    break;
                }
            }
        }
    }
    return $active;
}

// Determine whether a publisher is active...
function _isPublisherActive($aPublisherZone)
{
    return true;  // for now, all publishers are active.
}

// Determine whether a zone is active...
function _isZoneActive($aZone)
{
    return true;  // for now, all zones are active.
}

function _multiSort($array, $arg1, $arg2){
        $arr1 = array();
        $arr2 = array();

        foreach ($array as $key => $value){
            $arr1[$key] = strtolower( $value[$arg1] );
            $arr2[$key] = $value[$arg2];
        }

        array_multisort($arr1, $arr2, $array);
        return $array;
}

?>
