function confirm($_POST)
{
    # Get vars
    extract($_POST);
    if (isset($all)) {
        return details($_POST);
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($budname, "string", 1, 255, "Invalid Budget Name.");
    $v->isOk($budfor, "string", 1, 20, "Invalid Budget for option.");
    $v->isOk($budtype, "string", 1, 20, "Invalid Budget type.");
    $v->isOk($fromprd, "string", 1, 20, "Invalid Budget period.");
    $v->isOk($toprd, "string", 1, 20, "Invalid Budget period.");
    if ($budfor == 'acc') {
        if (isset($accids)) {
            foreach ($accids as $akey => $accid) {
                $v->isOk($accid, "num", 1, 50, "Invalid Account number.");
                foreach ($amts[$accid] as $skey => $amtr) {
                    $v->isOk($amts[$accid][$skey], "float", 1, 20, "Invalid Budget amount.");
                }
            }
        } else {
            $v->isOk("#", "num", 0, 0, "Error : please select at least one account.");
        }
    } elseif ($budfor == 'cost') {
        if (isset($ccids)) {
            foreach ($ccids as $akey => $ccid) {
                $v->isOk($ccid, "num", 1, 50, "Invalid Cost Center.");
                foreach ($amts[$ccid] as $skey => $amtr) {
                    $v->isOk($amts[$ccid][$skey], "float", 1, 20, "Invalid Budget amount.");
                }
            }
        } else {
            $v->isOk("#", "num", 0, 0, "Error : please select at least one cost center.");
        }
    }
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class=err>{$e['msg']}</li>";
        }
        return details($_POST, $confirm);
    }
    $ce = new Validate();
    if (isset($ccids)) {
        foreach ($ccids as $akey => $ccid) {
            $tot = array_sum($amts[$ccid]);
            $yr_tot = budgetTotalFromYear($ccid, "cost");
            if (strlen($yr_tot) > 0 && $tot != $yr_tot) {
                $ccRs = get("cubit", "*", "costcenters", "ccid", $ccid);
                $cc = pg_fetch_array($ccRs);
                $cc_name = "{$cc['centercode']} - {$cc['centername']}";
                $ce->addError("", "Yearly budget amount of " . CUR . "{$yr_tot} doesn't\n\t\t\t\t\tmatch proposed total amount of " . CUR . "{$tot} for Cost Center: {$cc_name}.");
            }
        }
    } else {
        if (isset($accids)) {
            foreach ($accids as $akey => $accid) {
                $tot = array_sum($amts[$accid]);
                $yr_tot = budgetTotalFromYear($accid, "acc");
                if (strlen($yr_tot) > 0 && $tot != $yr_tot) {
                    $accRs = get("core", "*", "accounts", "accid", $accid);
                    $acc = pg_fetch_array($accRs);
                    $acc_name = "{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}";
                    $ce->addError("", "Yearly budget amount of " . CUR . "{$yr_tot} doesn't\n\t\t\t\t\tmatch proposed total amount of " . CUR . "{$tot} for Account: {$acc_name}.");
                }
            }
        }
    }
    $mismatches = "";
    if ($ce->isError()) {
        $mm = $ce->getErrors();
        foreach ($mm as $e) {
            $mismatches .= "<li class=err>" . $e["msg"] . "</li>";
        }
    }
    global $BUDFOR, $TYPES, $PERIODS;
    $vbudfor = $BUDFOR[$budfor];
    $vbudtype = $TYPES[$budtype];
    $vfromprd = $PERIODS[$fromprd];
    $vtoprd = $PERIODS[$toprd];
    /* Toggle Options */
    $list = "";
    # budget for
    if ($budfor == 'cost') {
        $head = "<tr><th>Cost Centers</th>";
        foreach ($ccids as $ckey => $ccid) {
            $ccRs = get("cubit", "*", "costcenters", "ccid", $ccid);
            $cc = pg_fetch_array($ccRs);
            $list .= "<tr class='bg-odd'><td><input type=hidden name='ccids[{$cc['ccid']}]' value='{$cc['ccid']}'>{$cc['centercode']} - {$cc['centername']}</td>";
            foreach ($amts[$ccid] as $sprd => $amtr) {
                $amtr = sprint($amtr);
                $list .= "<td align=right><input type=hidden name=amts[{$cc['ccid']}][{$sprd}] value='{$amtr}'>" . CUR . " {$amtr}</td>";
            }
            $list .= "</tr>";
        }
    } elseif ($budfor == 'acc') {
        $head = "<tr><th>Accounts</th>";
        foreach ($accids as $akey => $accid) {
            $accRs = get("core", "*", "accounts", "accid", $accid);
            $acc = pg_fetch_array($accRs);
            $list .= "<tr class='bg-odd'><td><input type=hidden name='accids[{$acc['accid']}]' value='{$acc['accid']}'>{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</td>";
            foreach ($amts[$accid] as $sprd => $amtr) {
                $amtr = sprint($amtr);
                $list .= "<td align=right><input type=hidden name=amts[{$acc['accid']}][{$sprd}] value='{$amtr}'>" . CUR . " {$amtr}</td>";
            }
            $list .= "</tr>";
        }
    }
    # Budget headings
    if ($fromprd < $toprd) {
        for ($i = $fromprd; $i <= $toprd; $i++) {
            $head .= "<th>{$PERIODS[$i]}</th>";
        }
    } elseif ($fromprd > $toprd) {
        for ($i = $fromprd; $i <= 12; $i++) {
            $head .= "<th>{$PERIODS[$i]}</th>";
        }
        for ($i = 1; $i <= $toprd; $i++) {
            $head .= "<th>{$PERIODS[$i]}</th>";
        }
    } else {
        $head .= "<th>{$PERIODS[$toprd]}</th>";
    }
    $head .= "</tr>";
    // $totamt = sprint(array_sum($amts));
    // $list .= "<tr class='bg-even'><td><b>Total Budget Amount</b></td><td align=right><b>".CUR." $totamt</b></td></tr>";
    /* End Toggle Options */
    // Create hidden values
    $hidden = "";
    foreach ($_POST as $name => $value) {
        $hidden .= "<input type='hidden' name='{$name}' value='{$value}'>";
    }
    $confirm = "<center>\n\t<h3> Confirm New Monthly Budget </h3>\n\t<form action='" . SELF . "' method=post name=form>\n\t{$hidden}\n\t<input type=hidden name=key value=write>\n\t<input type=hidden name=budname value='{$budname}'>\n\t<input type=hidden name=budfor value='{$budfor}'>\n\t<input type=hidden name=budtype value='{$budtype}'>\n\t<input type=hidden name=fromprd value='{$fromprd}'>\n\t<input type=hidden name=toprd value='{$toprd}'>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' align=center>\n\t\t<tr>\n\t\t\t<th colspan=2>Details</th>\n\t\t</tr>\n\t\t<tr class='bg-odd'>\n\t\t\t<td>Budget Name</td>\n\t\t\t<td>{$budname}</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td><br></td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<th colspan=2>Options</th>\n\t\t</tr>\n\t\t<tr class='bg-odd'>\n\t\t\t<td>Budget For</td>\n\t\t\t<td>{$vbudfor}</td>\n\t\t</tr>\n\t\t<tr class='bg-even'>\n\t\t\t<td>Budget Type</td>\n\t\t\t<td>{$vbudtype}</td>\n\t\t</tr>\n\t\t<tr class='bg-odd'>\n\t\t\t<td>Budget Period</td>\n\t\t\t<td>{$vfromprd} to {$vtoprd}</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td><br></td>\n\t\t</tr>\n\t</table>\n\n\t{$mismatches}\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' align=center>\n\t{$head}\n\t{$list}\n\t</table>\n\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' align=center>\n\t\t<tr>\n\t\t\t<td><br></td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td><input type='submit' name='key' value='&laquo Correction'></td>\n\t\t\t<td align=right><input type=submit value='Continue &raquo'></td>\n\t\t</tr>\n\t</table>\n\t</form>\n\t<p>\n\t<table border=0 cellpadding='2' cellspacing='1' width=15%>\n\t\t<tr>\n\t\t\t<th>Quick Links</th>\n\t\t</tr>\n\t\t<tr class='bg-odd'>\n\t\t\t<td align=center><a href='budget-view.php'>View Budgets</td>\n\t\t</tr>\n\t\t<tr class='bg-odd'>\n\t\t\t<td align=center><a href='../main.php'>Main Menu</td>\n\t\t</tr>\n\t</table>";
    return $confirm;
}
function confirm($_POST)
{
    # Get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    include "../libs/validate.lib.php";
    $v = new validate();
    $v->isOk($budid, "num", 1, 20, "Invalid Budget id.");
    $v->isOk($budname, "string", 1, 255, "Invalid Budget Name.");
    $v->isOk($budtype, "string", 1, 20, "Invalid Budget type.");
    if ($budfor == 'acc') {
        if (isset($accids)) {
            foreach ($accids as $akey => $accid) {
                $v->isOk($accid, "num", 1, 50, "Invalid Account number.");
                foreach ($amts[$accid] as $skey => $amtr) {
                    $v->isOk($amts[$accid][$skey], "float", 1, 20, "Invalid Budget amount.");
                }
            }
        } else {
            $v->isOk("#", "num", 0, 0, "Error : please select at least one account.");
        }
    } elseif ($budfor == 'cost') {
        if (isset($ccids)) {
            foreach ($ccids as $akey => $ccid) {
                $v->isOk($ccid, "num", 1, 50, "Invalid Cost Center.");
                foreach ($amts[$ccid] as $skey => $amtr) {
                    $v->isOk($amts[$ccid][$skey], "float", 1, 20, "Invalid Budget amount.");
                }
            }
        } else {
            $v->isOk("#", "num", 0, 0, "Error : please select at least one cost center.");
        }
    }
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class=err>" . $e["msg"];
        }
        return details($_POST, $confirm);
    }
    $ce = new Validate();
    if (isset($ccids)) {
        foreach ($ccids as $akey => $ccid) {
            $tot = array_sum($amts[$ccid]);
            $yr_tot = budgetTotalFromYear($ccid, "cost");
            if (strlen($yr_tot) > 0 && $tot != $yr_tot) {
                $ccRs = get("cubit", "*", "costcenters", "ccid", $ccid);
                $cc = pg_fetch_array($ccRs);
                $cc_name = "{$cc['centercode']} - {$cc['centername']}";
                $ce->addError("", "Yearly budget amount of " . CUR . "{$yr_tot} doesn't\n\t\t\t\t\tmatch proposed total amount of " . CUR . "{$tot} for Cost Center: {$cc_name}.");
            }
        }
    } else {
        if (isset($accids)) {
            foreach ($accids as $akey => $accid) {
                $tot = array_sum($amts[$accid]);
                $yr_tot = budgetTotalFromYear($accid, "acc");
                if (strlen($yr_tot) > 0 && $tot != $yr_tot) {
                    $accRs = get("core", "*", "accounts", "accid", $accid);
                    $acc = pg_fetch_array($accRs);
                    $acc_name = "{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}";
                    $ce->addError("", "Yearly budget amount of " . CUR . "{$yr_tot} doesn't\n\t\t\t\t\tmatch proposed total amount of " . CUR . "{$tot} for Account: {$acc_name}.");
                }
            }
        }
    }
    $mismatches = "";
    if ($ce->isError()) {
        $mm = $ce->getErrors();
        foreach ($mm as $e) {
            $mismatches .= "<li class=err>" . $e["msg"] . "</li>";
        }
    }
    # Query server
    db_connect();
    $sql = "SELECT * FROM budgets WHERE budid = '{$budid}'";
    $budRslt = db_exec($sql) or errDie("Unable to retrieve Budgets from database.");
    if (pg_numrows($budRslt) < 1) {
        return "<li class=err> - Invalid Budget.";
    }
    $bud = pg_fetch_array($budRslt);
    global $BUDFOR, $PERIODS, $TYPES;
    $vbudfor = $BUDFOR[$bud['budfor']];
    $vbudtype = $TYPES[$budtype];
    $vfromprd = $PERIODS[$bud['fromprd']];
    $vtoprd = $PERIODS[$bud['toprd']];
    /* Toggle Options */
    $list = "";
    # budget for
    if ($bud['budfor'] == 'cost') {
        $head = "<tr><th>Cost Centers</th>";
        foreach ($ccids as $ckey => $ccid) {
            $ccRs = get("cubit", "*", "costcenters", "ccid", $ccid);
            $cc = pg_fetch_array($ccRs);
            $list .= "<tr class='bg-odd'><td><input type=hidden name=ccids[] value='{$cc['ccid']}'>{$cc['centercode']} - {$cc['centername']}</td>";
            $tot_annual = 0;
            foreach ($amts[$ccid] as $sprd => $amtr) {
                $tot_annual += $amtr = sprint($amtr);
                $list .= "<td align='right' nowrap><input type=hidden name=amts[{$cc['ccid']}][{$sprd}] value='{$amtr}'>" . CUR . " {$amtr}</td>";
            }
            $list .= "\n\t\t\t\t<td nowrap>" . CUR . " " . sprint($tot_annual) . "</td>\n\t\t\t</tr>";
        }
    } elseif ($bud['budfor'] == 'acc') {
        $head = "<tr><th>Accounts</th>";
        foreach ($accids as $akey => $accid) {
            $accRs = get("core", "*", "accounts", "accid", $accid);
            $acc = pg_fetch_array($accRs);
            $list .= "<tr class='bg-odd'><td><input type=hidden name=accids[] value='{$acc['accid']}'>{$acc['topacc']}/{$acc['accnum']} - {$acc['accname']}</td>";
            $tot_annual = 0;
            foreach ($amts[$accid] as $sprd => $amtr) {
                $tot_annual += $amtr = sprint($amtr);
                $list .= "<td align='right' nowrap><input type=hidden name=amts[{$acc['accid']}][{$sprd}] value='{$amtr}'>" . CUR . " {$amtr}</td>";
            }
            $list .= "\n\t\t\t\t<td nowrap>" . CUR . " " . sprint($tot_annual) . "</td>\n\t\t\t</tr>";
        }
    }
    # Budget headings
    if ($bud['fromprd'] < $bud['toprd']) {
        for ($i = $bud['fromprd']; $i <= $bud['toprd']; $i++) {
            $head .= "<th>{$PERIODS[$i]}</th>";
        }
    } elseif ($bud['fromprd'] > $bud['toprd']) {
        for ($i = $bud['fromprd']; $i <= 12; $i++) {
            $head .= "<th>{$PERIODS[$i]}</th>";
        }
        for ($i = 1; $i <= $bud['toprd']; $i++) {
            $head .= "<th>{$PERIODS[$i]}</th>";
        }
    } else {
        $i = $bud['toprd'];
        $head .= "<th>{$PERIODS[$i]}</th>";
    }
    $head .= "\n\t\t<th>Annual Total</th>\n\t</tr>";
    // $totamt = sprint(array_sum($amts));
    // $list .= "<tr class='bg-even'><td><b>Total Budget Amount</b></td><td align=right><b>".CUR." $totamt</b></td></tr>";
    /* End Toggle Options */
    $confirm = "<center>\n\t<h3> Confirm New Budget </h3>\n\t<form action='" . SELF . "' method=post name=form>\n\t<input type=hidden name=key value=write>\n\t<input type=hidden name=budid value='{$budid}'>\n\t<input type=hidden name=budname value='{$budname}'>\n\t<input type=hidden name=budtype value='{$budtype}'>\n\t<input type=hidden name=budfor value='{$bud['budfor']}'>\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' align=center>\n\t<tr><th colspan=2>Details</th></tr>\n\t<tr class='bg-odd'><td>Budget Name</td><td>{$budname}</td></tr>\n\t<tr><td><br></td></tr>\n\t<tr><th colspan=2>Options</th></tr>\n\t<tr class='bg-odd'><td>Budget For</td><td>{$vbudfor}</td>\n\t<tr class='bg-even'><td>Budget Type</td><td>{$vbudtype}</td>\n\t<tr class='bg-odd'><td>Budget Period</td><td>{$vfromprd} to {$vtoprd}</td>\n\t<tr><td><br></td></tr>\n\t</table>\n\n\t{$mismatches}\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' align=center>\n\t{$head}\n\t{$list}\n\t</table>\n\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' align=center>\n\t<tr><td><br></td></tr>\n\t<tr><td><input type=button value='&laquo Back' onClick='javascript:history.back()'></td><td align=right><input type=submit value='Continue &raquo'></td></tr>\n\t</table></form>\n\t<p>\n\t<table border=0 cellpadding='2' cellspacing='1' width=15%>\n\t\t<tr><th>Quick Links</th></tr>\n\t\t<tr class='bg-odd'><td align=center><a href='budget-view.php'>View Budgets</td></tr>\n\t\t<tr class='bg-odd'><td align=center><a href='../main.php'>Main Menu</td></tr>\n\t</table>";
    return $confirm;
}