<?php

function show_allocatable_contributions($show_totals) {

    global $systypes_array;

    $k = 0;
    $total_pledged = 0;
    $total_paid = 0;
    $total_balance = 0;

    $cart = $_SESSION['alloc'];

    if (count($cart->allocs)) {
        display_heading(sprintf(_("Allocated amounts in %s:"), $cart->currency));
        start_table(TABLESTYLE, "width='60%'");
        if ($cart->type == ST_MEMBER_PLEDGE) {
            $th = array(_("#"), _("Project"), _("Pledged"),
                _("Cotributions"), _("Balance"), _("Group"), _("Pledged"));
        } else {
            $th = array(_("#"), _("Project"), _("Pledged"),
                _("Cotributions"), _("Balance"), _("Group"), _("This Contribution"));
        }

        table_header($th);

        foreach ($cart->allocs as $id => $alloc_item) {
            
//            if ($cart->type == ST_MEMBER_PLEDGE && $alloc_item->id && !$cart->id) {
//                continue;
//            }

            $project = get_projects($alloc_item->project);
            alt_table_row_color($k);
            //label_cell(viewer_link($alloc_item->id, ''), "nowrap align='right'");
            $bal = 0;
            if ($cart->type == ST_MEMBER_PLEDGE) {
                $alloc_item->pledged_amt = $alloc_item->amount;
                $bal = max(($alloc_item->pledged_amt - $alloc_item->paid_amt),0);
                    label_cell($alloc_item->project, "nowrap align='right'");
            } else {
                if ($alloc_item->pledged_amt != 0) {
                    label_cell($alloc_item->project . hidden('pledge' . $id, $alloc_item->id, FALSE), "nowrap align='right'");
                    $bal = max(($alloc_item->pledged_amt - $alloc_item->paid_amt),0);
                    $alloc_item->amount = 0;
                } else {
                    $bal = 0;
                    label_cell($alloc_item->project, "nowrap align='right'");
                }
            }



            label_cell($project['proj_name']);
            label_cell($alloc_item->pledged_amt, "align=right");
            label_cell($alloc_item->paid_amt, "align=right");
            label_cell($bal, "align=right");

            if (TRUE) {
                $_POST['amount' . $id] = price_format($alloc_item->amount);
            } else {
                $_POST['amount' . $id] = price_format(0);
            }
            //display_warning($_POST['amount' . $id]);
            group_list_cells(null, 'group'.$id, $alloc_item->group, _('Select Group'), FALSE);
            amount_cells(null, "amount" . $id); //, input_num('amount' . $id));
            end_row();

            $total_pledged += $alloc_item->pledged_amt;
            $total_paid += $alloc_item->paid_amt;
            $total_balance += $bal;
        }
        if ($show_totals) {
            start_row();
            label_cell(_("Total"), "colspan=2 align=left");
            label_cell(price_format($total_pledged), "colspan=1 align=right");
            label_cell(price_format($total_paid), "colspan=1 align=right");
            label_cell(price_format($total_balance), "colspan=1 align=right");
            label_cell('', "colspan=1 align=right");
            label_cell('', "colspan=1 align=right");
            end_row();
        }
        end_table(1);
    }
    hidden('TotalNumberOfAllocs', count($cart->allocs));
}

//--------------------------------------------------------------------------------

function check_cont_allocations() {

    $alloc_total = 0;
    for ($counter = 0; $counter < get_post("TotalNumberOfAllocs"); $counter++) {
        if (!isset($_POST['amount' . $counter]))
            continue;
        if (!check_num('amount' . $counter, 0)) {
            display_error(_("The entry for one or more amounts is invalid or negative."));
            set_focus('amount' . $counter);
            return false;
        }

        if (!empty(get_post('pledge' . $counter))) {
            $_SESSION['alloc']->allocs[$counter]->id = get_post('pledge' . $counter);
        }

         $_SESSION['alloc']->allocs[$counter]->group = get_post('group' . $counter);
        $amt = input_num('amount' . $counter);
        $alloc_total += $amt;
        $_SESSION['alloc']->allocs[$counter]->amount = $amt;
    }

    if (!$alloc_total) {
        display_error(_("Net Allocations must be greater thean zero."));
        return false;
    }

    return true;
}
