<script type="text/javascript">
function ChangeState(ID, action) {
  if(action == "suppress") {
    if(confirm("<?php echo L_G_CONFIRMSUPPRESSAFF?>"))
     	document.location.href = "index.php?md=Affiliate_Merchants_Views_AffiliateManager&showtree=1&aid="+ID+"&action="+action+"&<?php echo SID?>";
  } else if(action == "approve") {
    if(confirm("<?php echo L_G_CONFIRMAPPROVEAFF?>"))
      document.location.href = "index.php?md=Affiliate_Merchants_Views_AffiliateManager&showtree=1&aid="+ID+"&action="+action+"&<?php echo SID?>";
  }
}

function editUser(ID) {
    document.location.href = "index.php?md=Affiliate_Merchants_Views_AffiliateManager&action=edit&aid="+ID+"&<?php echo SID?>","AddUser","scrollbars=1, top=100, left=100, width=500, height=500, status=0";
}

function swapUser(ID) {
	var wnd = window.open("index_popup.php?md=Affiliate_Merchants_Views_AffiliateManager&action=swap&u1="+ID+"&<?php echo SID?>","AddUser","scrollbars=1, top=100, left=100, width=500, height=500, status=0")
    wnd.focus();
}

function viewUser(ID) {
    document.location.href = "index.php?md=Affiliate_Merchants_Views_AffiliateManager&action=view&aid="+ID+"&<?php echo SID?>","AddUser","scrollbars=1, top=100, left=100, width=450, height=500, status=0";
}

function Delete(ID) {
  if(confirm("<?php echo L_G_CONFIRMDELETEAFF?>"))
   	document.location.href = "index.php?md=Affiliate_Merchants_Views_AffiliateManager&showtree=1&aid="+ID+"&action=delete"+"&<?php echo SID?>";
}
</script>

<?php function print_node_data(&$data,&$te) { ?>

    <td width="160" height="26"><b><?php echo cut_str($data['username'],30)?></b></td>
    <td width="80">
        <?php showQuickDetails("index_popup2.php?md=Affiliate_Merchants_Views_AffiliateManager&action=affalldetails&aid=".$data['userid'], 300); ?>
        <font color="#0000ff"><?php echo cut_str($data['name'],50)?></font>
    </td>
<?php
    $actions = array();
    $i = 0;
    if($te->a_this->checkPermissions('view')) {
        $actions[$i++] = array('id'     => 'edit',
                               'img'    => 'user_link.gif',
                               'desc'   => L_G_VIEWPROFILE,
                               'action' => "viewUser('".$data['userid']."');" );
    }
    if($te->a_this->checkPermissions('edit')) {
        $actions[$i++] = array('id'     => 'edit',
                               'img'    => 'edit.gif',
                               'desc'   => L_G_EDIT,
                               'action' => "editUser('".$data['userid']."');" );
    }
    if($te->a_this->checkPermissions('swap')) {
        $actions[$i++] = array('id'     => 'swap',
                               'img'    => 'switch.png',
                               'desc'   => L_G_SWAP,
                               'action' => "swapUser('".$data['userid']."');" );
    }
    if($te->a_this->checkPermissions('approve')) {
        if($data['rstatus'] != AFFSTATUS_APPROVED) {
            $actions[$i] = array('id'     => 'approve',
                                 'img'    => 'sphore_active.png',
                                 'desc'   => L_G_APPROVE,
                                 'action' => "ChangeState('".$data['userid']."','approve');");
        }
        if($data['rstatus'] != AFFSTATUS_SUPPRESSED) {
            $actions[$i+1] = array('id'     => 'suppress',
                                   'img'    => 'sphore_declined.png',
                                   'desc'   => L_G_SUPPRESS,
                                   'action' => "ChangeState('".$data['userid']."','suppress');");
        }
        $i += 2;
    }
    if($te->a_this->checkPermissions('delete')) {
        $actions[$i++] = array('id'     => 'delete',
                               'img'    => 'delete.png',
                               'desc'   => L_G_DELETE,
                               'action' => "Delete('".$data['userid']."');" );
    }
    $te->a_this->initTemporaryTE();
    $te->a_this->temporaryAssign('a_actions', $actions);
    $te->a_this->temporaryAssign('a_action_count', $i);
    print '<td>'.$te->a_this->temporaryFetch('actions_icon').'</td>';
}
?>


<?php
function node_color($depth) {
    $maxdepth = 20.0;
    $depth %= $maxdepth;
    $depth = $maxdepth-$depth;
    $r = $depth*(hexdec('F2'))/$maxdepth;
    $g = $depth*(hexdec('F9'))/$maxdepth;
    $b = $depth*(hexdec('FD'))/$maxdepth;
    return '#'.dechex($r).dechex($g).dechex($b);
}
?>

<table border="0" cellspacing="0" cellpadding="0" width="780">
<tr><td><?php echo L_G_SUBAFFILIATES_DESCRIPTION?></td></tr>
</table>

<table class="listing mt10" border=0 cellspacing=0 cellpadding=0 width="780">
<?php QUnit_Templates::printFilter(1, L_G_TREEOFAFFILIATES); ?>
<tr><td>
    <table width="100%" cellpadding="0" cellspacing="0" border="0">
    <?php $row = 0 ?>
	<?php $tree_level = 0 ?>
	<?php $tree_node_offset = 16 ?>
	<?php $node_img_width = 16 ?>
    <?php $node_color = node_color(0) ?>
	<?php while($data=$this->a_list_data->getNextRecord()) { ?>

      <?php if ($data['level'] > $tree_level) { ?>

	               <?php $node_color = node_color($data['level']) ?>
	               <tr style="background-color:<?php echo $node_color?>">
	                   <td colspan="5">
                           <table border="0" cellpadding="0" cellspacing="0" style="margin-left:<?php echo $tree_node_offset?>px">
                           <tr style="background-color:<?php echo $node_color?>">
                               <td style="width:<?php echo $node_img_width?>px;" valign="middle"><img src="<?php echo  $this->a_this->getImage('subnode.gif')?>"></td>
                               <?php print_node_data($data,$this)?>
	                       </tr>
                   <?php $tree_level = $data['level']; ?>
                   <?php $row = 1; ?>
                   <?php continue ?>

	       <?php } ?>

	       <?php while ($data['level'] < $tree_level) {?>
                           </table>
	                   </td>
	               </tr>
                   <?php $tree_level-- ?>
	               <?php $node_color = node_color($data['level']) ?>
	       <?php } ?>

	       <?php /*<tr class="listrow<?php echo $row=1-$row?>">*/ ?>
	       <tr style="background-color:<?php if ($row=1-$row) echo $node_color; else echo '#FFFFFF';?>">
               <td style="width:<?php echo $node_img_width?>px;" valign="middle"><img src="<?php echo  $this->a_this->getImage('subnode.gif')?>"></td>
               <?php print_node_data($data,$this) ?>
           </tr>
	<?php } ?>
	<?php while ($tree_level-- > 0) {?>
                         </table>
                    </td>
                 </tr>
    <?php } ?>
    </table>
    </td></tr>
</table>
