function printLea()
{
    # Set up table to display in
    $printLea = "\n\t\t<h3>Employees on Leave</h3>\n\t\t<table " . TMPL_tblDflts . ">\n\t\t\t<tr>\n\t\t\t\t<th>Employee</th>\n\t\t\t\t<th>Type Of Leave</th>\n\t\t\t\t<th>Start Date</th>\n\t\t\t\t<th>End Date</th>\n\t\t\t\t<th>Approved By</th>\n\t\t\t</tr>";
    # connect to database
    db_connect();
    # Query server
    $i = 0;
    $today = date("Y-m-d");
    $sql = "SELECT * FROM empleave WHERE enddate >= '{$today}' AND approved = 'y' AND div = '" . USER_DIV . "' ORDER BY id DESC";
    $leaRslt = db_exec($sql) or errDie("Unable to retrieve employee leave from database.");
    if (pg_numrows($leaRslt) < 1) {
        $printLea = "<li class='err'>There are no Employees on Leave.</li><br>";
    } else {
        while ($lea = pg_fetch_array($leaRslt)) {
            $typedef = typedef($lea['type']);
            # format date
            $lea['date'] = explode("-", $lea['date']);
            $lea['date'] = $lea['date'][2] . "-" . $lea['date'][1] . "-" . $lea['date'][0];
            $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];
            # get employee details
            db_connect();
            $sql = "SELECT empnum, sname, fnames, enum FROM employees WHERE empnum='{$lea['empnum']}' AND div = '" . USER_DIV . "'";
            $empRslt = db_exec($sql) or errDie("Unable to select employees from database.");
            if (pg_numrows($empRslt) < 1) {
                return "Invalid employee ID.";
            }
            $myEmp = pg_fetch_array($empRslt);
            $printLea .= "\n\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t<td>{$myEmp['sname']}, {$myEmp['fnames']} ({$myEmp['enum']})</td>\n\t\t\t\t\t<td>{$typedef}</td>\n\t\t\t\t\t<td>{$lea['startdate']}</td>\n\t\t\t\t\t<td>{$lea['enddate']}</td>\n\t\t\t\t\t<td>{$lea['approvedby']}</td>\n\t\t\t\t</tr>";
            $i++;
        }
    }
    $printLea .= "\n\t\t</table>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $printLea;
}
function confirmLeave($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 20, "Invalid leave number.");
    # 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;
    }
    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";
    }
    # 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];
    $typedef = typedef($lea['type']);
    $today = date("d-m-Y");
    # get employee details
    db_connect();
    $sql = "SELECT empnum, sname, fnames FROM employees WHERE empnum='{$lea['empnum']}' AND div = '" . USER_DIV . "'";
    $empRslt = db_exec($sql) or errDie("Unable to select employees from database.");
    if (pg_numrows($empRslt) < 1) {
        return "Invalid employee ID.";
    }
    $myEmp = pg_fetch_array($empRslt);
    $typedef = typedef($lea['type']);
    list($start_day, $start_month, $start_year) = explode("-", $lea['startdate']);
    list($fin_day, $fin_month, $fin_year) = explode("-", $lea['enddate']);
    # get unix timestamps (seconds since unix epoch)
    $unixStart = mktime(0, 0, 0, $start_month, $start_day, $start_year);
    $unixEnd = mktime(0, 0, 0, $fin_month, $fin_day, $fin_year);
    $unixDay = 60 * 60 * 24;
    # interval
    $days_between = ($unixEnd - $unixStart) / 60 / 60 / 24;
    $confirmLeave = "<h3>Confirm Cancel Employee Leave Application</h3>\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t<form action='" . SELF . "' method=post>\n\t<input type=hidden name=key value=write>\n\t<input type=hidden name=id value='{$id}'>\n\t<tr><th>Field</th><th>Value</th></tr>\n\t<tr class='bg-odd'><td>Employee</td><td align=center>{$myEmp['sname']}, {$myEmp['fnames']} ({$myEmp['empnum']})</td></tr>\n\t<tr class='bg-even'><td>Date of approval</td><td align=center>{$today}</td></tr>\n\t<tr class='bg-odd'><td>Leave start date</td><td align=center>{$lea['startdate']}</td></tr>\n\t<tr class='bg-even'><td>Leave end date</td><td align=center>{$lea['enddate']}</td></tr>\n\t<tr class='bg-odd'><td>Total days inbetween</td><td align=center>{$days_between}</td></tr>\n\t<tr class='bg-even'><td>Total working days inbetween</td><td align=center>{$lea['workingdays']}</td></tr>\n\t<tr class='bg-odd'><td>Type of leave</td><td align=center>{$typedef}</td></tr>\n\t<tr><td><br></td></tr>\n\t<tr><td colspan=2 align=right><input type=submit value='Write &raquo;'></td></tr>\n\t</form>\n\t</table>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $confirmLeave;
}
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 printLea($_POST)
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($from_day, "num", 1, 2, "Invalid from Date day.");
    $v->isOk($from_month, "num", 1, 2, "Invalid from Date month.");
    $v->isOk($from_year, "num", 1, 4, "Invalid from Date Year.");
    $v->isOk($to_day, "num", 1, 2, "Invalid to Date day.");
    $v->isOk($to_month, "num", 1, 2, "Invalid to Date month.");
    $v->isOk($to_year, "num", 1, 4, "Invalid to Date Year.");
    # mix dates
    $fromdate = $from_year . "-" . $from_month . "-" . $from_day;
    $todate = $to_year . "-" . $to_month . "-" . $to_day;
    if (!checkdate($from_month, $from_day, $from_year)) {
        $v->isOk($fromdate, "num", 1, 1, "Invalid from date.");
    }
    if (!checkdate($to_month, $to_day, $to_year)) {
        $v->isOk($todate, "num", 1, 1, "Invalid to date.");
    }
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class='err'>-" . $e["msg"] . "</li>";
        }
        return $confirm;
    }
    # Set up table to display in
    $printLea = "\n\t\t\t<h3>View Employee Leave Applications</h3>\n\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t<tr>\n\t\t\t\t\t<th>Employee</th>\n\t\t\t\t\t<th>Application Date</th>\n\t\t\t\t\t<th>Start Date</th>\n\t\t\t\t\t<th>End Date</th>\n\t\t\t\t\t<th>Working Days</th>\n\t\t\t\t\t<th>Type Of Leave</th>\n\t\t\t\t\t<th colspan='2'>Options</th>\n\t\t\t\t</tr>";
    # connect to database
    db_connect();
    # Query server
    $i = 0;
    $sql = "SELECT * FROM empleave WHERE date >= '{$fromdate}' AND date <= '{$todate}' AND div = '" . USER_DIV . "' ORDER BY id DESC";
    $leaRslt = db_exec($sql) or errDie("Unable to retrieve employee leave applications from database.");
    if (pg_numrows($leaRslt) < 1) {
        $printLea = "<li class='err'>No Outstanding Employee Leave applications found.</li><br>";
    } else {
        while ($lea = pg_fetch_array($leaRslt)) {
            $typedef = typedef($lea['type']);
            # format date
            $lea['date'] = explode("-", $lea['date']);
            $lea['date'] = $lea['date'][2] . "-" . $lea['date'][1] . "-" . $lea['date'][0];
            $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];
            # get employee details
            db_connect();
            $sql = "SELECT empnum, sname, fnames, enum FROM employees WHERE empnum='{$lea['empnum']}' AND div = '" . USER_DIV . "'";
            $empRslt = db_exec($sql) or errDie("Unable to select employees from database.");
            if (pg_numrows($empRslt) < 1) {
                return "Invalid employee ID.";
            }
            $myEmp = pg_fetch_array($empRslt);
            $printLea .= "\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>{$myEmp['sname']}, {$myEmp['fnames']} ({$myEmp['enum']})</td>\n\t\t\t\t\t\t<td>{$lea['date']}</td>\n\t\t\t\t\t\t<td>{$lea['startdate']}</td>\n\t\t\t\t\t\t<td>{$lea['enddate']}</td>\n\t\t\t\t\t\t<td>{$lea['workingdays']}</td>\n\t\t\t\t\t\t<td>{$typedef}</td>";
            # If approve
            if ($lea['approved'] == 'n') {
                $printLea .= "\n\t\t\t\t\t\t\t<td><a href='employee-leave-approve.php?id={$lea['id']}'>Approve</td>\n\t\t\t\t\t\t\t<td><a href='employee-leave-cancel.php?id={$lea['id']}'>Cancel</td>\n\t\t\t\t\t\t</tr>";
            } else {
                $printLea .= "<td colspan='2'><br></td></tr>";
            }
            $i++;
        }
    }
    $printLea .= "\n\t\t\t</table>\n\t\t\t<br>\n\t\t\t<input type='button' onClick=\"javascript:window.open('../public_holiday_list.php','window1','width=400,height=360,scrollbars=yes')\" value='Public Holiday List'>\n\t\t\t<br><br>" . mkQuickLinks(ql("../admin-employee-add.php", "Add Employee"), ql("../admin-employee-view.php", "View Employees"));
    return $printLea;
}