function writeLeave($_POST)
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 20, "Invalid leave number.");
    $v->isOk($nonworking, "num", 1, 2, "Invalid value for non-working days.");
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirmCust .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirmCust;
    }
    db_connect();
    $sql = "SELECT * FROM empleave WHERE id = '{$id}' AND div = '" . USER_DIV . "'";
    $leaRslt = db_exec($sql) or errDie("Unable to retrieve employee leave application from database.");
    if (pg_numrows($leaRslt) > 0) {
        $lea = pg_fetch_array($leaRslt);
    } else {
        return "<li class='err'> Invalid leave number.</li>";
    }
    # check if leave can be granted
    $leav = array("leave_sick", "leave_study", "leave_vac");
    if (in_array($lea['type'], $leav)) {
        if (!checkLeave($lea['empnum'], $lea['type'], $lea['workingdays'] - $nonworking)) {
            return "<li>ERROR : Leave period selected exceeds allowed amount for " . typedef($lea['type']) . ".";
        }
    }
    db_connect();
    # write to db
    $sql = "UPDATE empleave SET workingdays = (workingdays - '{$nonworking}'), nonworking = '{$nonworking}', approved = 'y' WHERE id = '{$id}' AND div = '" . USER_DIV . "'";
    $leaveRslt = db_exec($sql) or errDie("Unable to update approved leave to database.");
    if (pg_cmdtuples($leaveRslt) < 1) {
        return "Unable to write approved leave to database.";
    }
    # format the dates
    $lea['startdate'] = explode("-", $lea['startdate']);
    $lea['startdate'] = $lea['startdate'][2] . "-" . $lea['startdate'][1] . "-" . $lea['startdate'][0];
    $lea['enddate'] = explode("-", $lea['enddate']);
    $lea['enddate'] = $lea['enddate'][2] . "-" . $lea['enddate'][1] . "-" . $lea['enddate'][0];
    $writeLeave = "\n\t\t<table " . TMPL_tblDflts . " width='50%'>\n\t\t\t<tr>\n\t\t\t\t<th>Employee leave approved</th>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td>Employee leave from {$lea['startdate']} until {$lea['enddate']} has been approved.</td>\n\t\t\t</tr>\n\t\t</table>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $writeLeave;
}
function writeLeave($_POST)
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($empnum, "num", 1, 20, "Invalid employee number.");
    $v->isOk($date, "date", 1, 10, "Invalid date.");
    $v->isOk($startdate, "date", 1, 10, "Invalid leave start date.");
    $v->isOk($enddate, "date", 1, 10, "Invalid leave end date.");
    $v->isOk($approvedby, "string", 1, 20, "Invalid value for 'approved by'.");
    $v->isOk($type, "string", 1, 20, "Invalid leave type.");
    $v->isOk($workingdays, "num", 1, 3, "Invalid value for working days off.");
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        $confirmCust .= "<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $confirmCust;
    }
    switch ($type) {
        case "Paid vacation-leave":
            $type = "leave_vac";
            # $type = "vaclea";
            if (!checkLeave($empnum, $type, $workingdays)) {
                return "<li>ERROR : Leave period selected exceeds allowed amount for paid vacation-leave.";
            }
            break;
        case "Paid sick-leave":
            $type = "leave_sick";
            # $type = "siclea";
            if (!checkLeave($empnum, $type, $workingdays)) {
                return "<li>ERROR : Leave period selected exceeds allowed amount for paid sick-leave.";
            }
            break;
        case "Paid study-leave":
            $type = "leave_study";
            # $type = "stdlea";
            if (!checkLeave($empnum, $type, $workingdays)) {
                return "<li>ERROR : Leave period selected exceeds allowed amount for paid study-leave.";
            }
            break;
        case "Special paid-leave":
            # $type = "leave_special";
            $type = "leave_special";
            break;
        default:
            # $type = "leave_unpaid";
            $type = "leave_unpaid";
            break;
    }
    # Connect to db
    db_connect();
    # write to db
    $sql = "\n\t\tINSERT INTO empleave (\n\t\t\tempnum, date, startdate, enddate, approvedby, type, workingdays, nonworking, approved, div\n\t\t) VALUES (\n\t\t\t'{$empnum}', '{$date}', '{$startdate}', '{$enddate}', '{$approvedby}', '{$type}', '{$workingdays}', '0', 'n', '" . USER_DIV . "'\n\t\t)";
    $leaveRslt = db_exec($sql) or errDie("Unable to write approved leave to database.");
    if (pg_cmdtuples($leaveRslt) < 1) {
        return "Unable to write approved leave to database.";
    }
    $writeLeave = "\n\t\t<table " . TMPL_tblDflts . " width='50%'>\n\t\t\t<tr>\n\t\t\t\t<th>Employee leave requested</th>\n\t\t\t</tr>\n\t\t\t<tr class='datacell'>\n\t\t\t\t<td>Employee leave from {$startdate} until {$enddate} been requested.</td>\n\t\t\t</tr>\n\t\t</table>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $writeLeave;
}