# get settings
require "settings.php";
# decide what to do
if (isset($_POST["key"])) {
    switch ($_POST["key"]) {
        case "confirm":
            $OUTPUT = confirmPaye($_POST);
            break;
        case "write":
            $OUTPUT = writePaye($_POST);
            break;
        default:
            $OUTPUT = enterPaye();
    }
} else {
    $OUTPUT = enterPaye();
}
# display output
require "template.php";
# enter new paye bracket details
function enterPaye()
{
    # connect to db
    db_connect();
    $enterPaye = "\r\n<h3>New PAYE bracket</h3>\r\n\r\n<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n<form action='" . SELF . "' method=post>\r\n<input type=hidden name=key value=confirm>\r\n<tr><th>Field</th><th>Value</th></tr>\r\n<tr class='bg-odd'><td>Minimum gross</td><td align=center>" . CUR . " <input type=text size=20 name=min class=right></td></tr>\r\n<tr class='bg-even'><td>Maximum gross</td><td align=center>" . CUR . " <input type=text size=20 name=max class=right></td></tr>\r\n<tr class='bg-odd'><td>Percentage to deduct</td><td align=center><input type=text size=20 name=percentage class=right>%</td></tr>\r\n<tr><td colspan=2 align=right><input type=submit value='Confirm &raquo;'></td></tr>\r\n</form>\r\n</table>\r\n";
    return $enterPaye;
}
# confirm new paye bracket details
function confirmPaye($_POST)
{
    # get vars
function writePaye($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    if (isset($back)) {
        return enterPaye($_POST);
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($min, "num", 1, 9, "Invalid minimum amount.");
    $v->isOk($max, "num", 1, 9, "Invalid maximum amount.");
    $v->isOk($percentage, "float", 1, 6, "Invalid taxable rate.");
    $v->isOk($extra, "num", 0, 9, "Invalid extra cash amount.");
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class=err>" . $e["msg"];
        }
        $confirmCust .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirmCust;
    }
    # connect to db
    db_connect();
    # add PAYE to db
    $sql = "INSERT INTO paye (min, max, percentage, extra) VALUES ('{$min}', '{$max}', '{$percentage}', '{$extra}')";
    $payeRslt = db_exec($sql) or errDie("Unable to add PAYE bracket to database.", SELF);
    $writePaye = "<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width='50%'>\n\t<tr><th>PAYE bracket added to database</th></tr>\n\t<tr class=datacell><td>New PAYE bracket has been successfully added to Cubit.</td></tr>\n\t</table>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $writePaye;
}