function display($message = "")
{
    extract($_REQUEST);
    $fields = array();
    $fields["search"] = "";
    extract($fields, EXTR_SKIP);
    $description = array();
    $OUTPUT = "<center>\r\n\t<h3>Service Settings</h3>\r\n\t<form method='post' action='" . SELF . "'>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th colspan='2'>Search</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td><input type='text' name='search' value='{$search}' /></td>\r\n\t\t\t<td><input type='submit' value='Search &raquo' /></td>\r\n\t\t</tr>\r\n\t</table>\r\n\t</form>";
    // Retrieve assets
    $sql = "SELECT * FROM cubit.assets\r\n\t\t\tWHERE des ILIKE '%{$search}%' OR serial ILIKE '%{$search}%'\r\n\t\t\t\tOR locat ILIKE '%{$search}%'\r\n\t\t\tORDER BY des ASC";
    $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
    $service_out = "";
    while ($asset_data = pg_fetch_array($asset_rslt)) {
        if (!isOurs($asset_data["id"])) {
            continue;
        }
        if (!isset($description[$asset_data["id"]])) {
            $description[$asset_data["id"]] = "";
        }
        $service_out .= "<tr class='" . bg_class() . "'>\r\n\t\t\t<td>{$asset_data['des']}</td>\r\n\t\t\t<td>" . getSerial($asset_data["id"]) . "</td>\r\n\t\t\t<td>{$asset_data['locat']}</td>\r\n\t\t\t<td>\r\n\t\t\t\t<input type='text' name='days[" . $asset_data["id"] . "]'\r\n\t\t\t\tvalue='{$asset_data['days']}' size='3'\r\n\t\t\t\tstyle='text-align: center' />\r\n\t\t\t</td>\r\n\t\t\t<td>\r\n\t\t\t\t<input type='text' name='description[{$asset_data['id']}]'\r\n\t\t\t\tvalue='" . $description[$asset_data["id"]] . "' />\r\n\t\t\t</td>\r\n\t\t</tr>";
    }
    if (empty($service_out)) {
        $service_out = "<tr class='" . bg_class() . "'>\r\n\t\t\t<td colspan='5'><li>No service days found.</li></td>\r\n\t\t</tr>";
    }
    $OUTPUT .= "<form method='post' action='" . SELF . "'>\r\n\t<input type='hidden' name='key' value='save' />\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td colspan='5'><li>Amount of hire days before warning.</li></td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<td colspan='5'>{$message}</td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<th>Description</th>\r\n\t\t\t<th>Serial</th>\r\n\t\t\t<th>Location</th>\r\n\t\t\t<th>Days</th>\r\n\t\t\t<th>Description</th>\r\n\t\t</tr>\r\n\t\t{$service_out}\r\n\t\t<tr>\r\n\t\t\t<td colspan='5' align='center'>\r\n\t\t\t\t<input type='submit' value='Save &raquo' />\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t</table>\r\n\t</form>\r\n\t</center>";
    return $OUTPUT;
}
function enter($errors = "")
{
    extract($_REQUEST);
    $fields = array();
    $fields["asset_id"] = 0;
    $fields["ex_year"] = date("Y");
    $fields["ex_month"] = date("m");
    $fields["ex_day"] = date("d");
    $fields["description"] = "";
    $fields["notes"] = "";
    $fields["qty"] = 1;
    extract($fields, EXTR_SKIP);
    // Create asset dropdown
    $sql = "SELECT id, des, serial FROM cubit.assets ORDER BY des ASC";
    $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
    $asset_sel = "<select name='asset_id' style='width='100%'>";
    while ($asset_data = pg_fetch_array($asset_rslt)) {
        if (!isOurs($asset_data["id"]) || isHired($asset_data["id"], date("Y-m-d"))) {
            continue;
        }
        if ($asset_id == $asset_data["id"]) {
            $sel = "selected='selected'";
        } else {
            $sel = "";
        }
        $asset_sel .= "\r\n\t\t<option value='{$asset_data['id']}' {$sel}>\r\n\t\t\t({$asset_data['serial']}) {$asset_data['des']}\r\n\t\t</option>";
    }
    $asset_sel .= "</select>";
    $OUTPUT = "<h3>Book Asset to Workshop</h3>\r\n\t<form method='post' action='" . SELF . "'>\r\n\t<input type='hidden' name='key' value='confirm' />\r\n\t<input type='hidden' name='qty' value='{$qty}' />\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<td colspan='2'>{$errors}</td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<th colspan='2'>Details</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>Asset</td>\r\n\t\t\t<td>{$asset_sel}</td>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>Expected Back Date</td>\r\n\t\t\t<td>" . mkDateSelect("ex", $ex_year, $ex_month, $ex_day) . "</td>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>Description</td>\r\n\t\t\t<td><input type='text' name='description' value='{$description}' /></td>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>Notes</td>\r\n\t\t\t<td><textarea name='notes' rows='5' cols='20'>{$notes}</textarea></td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<td colspan='2' align='right'>\r\n\t\t\t\t<input type='submit' value='Write &raquo' />\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t</table>\r\n\t</form>";
    return $OUTPUT;
}
function basis()
{
    extract($_REQUEST);
    $fields = array();
    $fields["asset_search"] = "[_BLANK_]";
    extract($fields, EXTR_SKIP);
    // Retrieve assets
    $sql = "SELECT * FROM cubit.assets\r\n\t\t\tWHERE des ILIKE '%{$asset_search}%' OR serial ILIKE '%{$asset_search}%'\r\n\t\t\t\tOR locat ILIKE '%{$asset_search}%'\r\n\t\t\tORDER BY des ASC";
    $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
    if ($asset_search == "[_BLANK_]") {
        $asset_search = "";
        $n_msg = "Please enter the first few letters of the asset's name.";
    } else {
        $n_msg = "No result found.";
    }
    $asset_out = "";
    while ($asset_data = pg_fetch_array($asset_rslt)) {
        if (!isOurs($asset_data["id"])) {
            continue;
        }
        $sql = "SELECT * FROM hire.cust_basis\r\n\t\t\t\tWHERE cust_id='{$cust_id}' AND asset_id='{$asset_data['id']}'";
        $basis_rslt = db_exec($sql) or errDie("Unable to retrieve basis.");
        $basis_data = pg_fetch_array($basis_rslt);
        $sql = "SELECT * FROM hire.basis_prices WHERE assetid='{$asset_data['id']}'";
        $bp_rslt = db_exec($sql) or errDie("Unable to retrieve default basis.");
        $bp_data = pg_fetch_array($bp_rslt);
        if (!isset($day[$asset_data["id"]])) {
            $day[$asset_data["id"]] = sprint($basis_data["day"]);
            if (!(double) $day[$asset_data["id"]] && (double) $bp_data["per_day"]) {
                $day[$asset_data["id"]] = $bp_data["per_day"];
            }
        }
        if (!isset($hour[$asset_data["id"]])) {
            $hour[$asset_data["id"]] = sprint($basis_data["hour"]);
            if (!(double) $hour[$asset_data["id"]] && (double) $bp_data["per_hour"]) {
                $hour[$asset_data["id"]] = $bp_data["per_hour"];
            }
        }
        if (!isset($week[$asset_data["id"]])) {
            $week[$asset_data["id"]] = sprint($basis_data["week"]);
            if (!(double) $week[$asset_data["id"]] && (double) $bp_data["per_week"]) {
                $week[$asset_data["id"]] = $bp_data["per_week"];
            }
        }
        $asset_out .= "<tr class='" . bg_class() . "'>\r\n\t\t\t<td>{$asset_data['des']}</td>\r\n\t\t\t<td>" . getSerial($asset_data["id"]) . "</td>\r\n\t\t\t<td>{$asset_data['locat']}</td>\r\n\t\t\t<td>\r\n\t\t\t\t" . CUR . "\r\n\t\t\t\t<input type='text' name='hour[{$asset_data['id']}]'\r\n\t\t\t\tvalue='" . sprint($hour[$asset_data["id"]]) . "' size='5'\r\n\t\t\t\tstyle='text-align: right' />\r\n\t\t\t</td>\r\n\t\t\t<td>\r\n\t\t\t\t" . CUR . "\r\n\t\t\t\t<input type='text' name='day[{$asset_data['id']}]'\r\n\t\t\t\tvalue='" . sprint($day[$asset_data["id"]]) . "' size='5'\r\n\t\t\t\tstyle='text-align: right' />\r\n\t\t\t</td>\r\n\t\t\t<td>\r\n\t\t\t\t" . CUR . "\r\n\t\t\t\t<input type='text' name='week[{$asset_data['id']}]'\r\n\t\t\t\tvalue='" . sprint($week[$asset_data["id"]]) . "' size='5'\r\n\t\t\t\tstyle='text-align: right' />\r\n\t\t\t</td>\r\n\t\t</tr>";
    }
    if (empty($asset_out)) {
        $asset_out .= "\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td colspan='6'><li>{$n_msg}</li></td>\r\n\t\t</tr>";
    }
    // Retrieve customer details
    $sql = "SELECT * FROM cubit.customers WHERE cusnum='{$cust_id}'";
    $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customer.");
    $cust_data = pg_fetch_array($cust_rslt);
    $OUTPUT = "<center>\r\n\t<h3>Basis</h3>\r\n\t<form method='post' action='" . SELF . "'>\r\n\t<input type='hidden' name='cust_id' value='{$cust_id}' />\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th colspan='2'>Search</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td><input type='text' name='asset_search' value='{$asset_search}' /></td>\r\n\t\t\t<td><input type='submit' value='Search &raquo' /></td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<th colspan='2'>Customer</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>\r\n\t\t\t\t<a href='javascript:" . "popupOpen(\"../cust-det.php?cusnum={$cust_data['cusnum']}\");'>\r\n\t\t\t\t\t{$cust_data['surname']} {$cust_data['cusname']}\r\n\t\t\t\t</a>\r\n\t\t\t</td>\r\n\t\t\t<td>\r\n\t\t\t\t<a href='" . SELF . "'>Change</a>\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t</table>\r\n\t<p></p>\r\n\t</table>\r\n\t</form>\r\n\t<form method='post' action='" . SELF . "'>\r\n\t<input type='hidden' name='key' value='save' />\r\n\t<input type='hidden' name='cust_id' value='{$cust_id}' />\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th>Plant</th>\r\n\t\t\t<th>Serial</th>\r\n\t\t\t<th>Location</th>\r\n\t\t\t<th>Hour</th>\r\n\t\t\t<th>Day</th>\r\n\t\t\t<th>Week</th>\r\n\t\t</tr>\r\n\t\t{$asset_out}\r\n\t\t<tr>\r\n\t\t\t<td colspan='6' align='center'>\r\n\t\t\t\t<input type='submit' value='Set Basis &raquo'\r\n\t\t\t\tstyle='font-size: 1.1em; font-weight: bold;' />\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t</table>";
    return $OUTPUT;
}
function display()
{
    extract($_REQUEST);
    $fields = array();
    $fields["frm_year"] = date("Y");
    $fields["frm_month"] = date("m");
    $fields["frm_day"] = date("d");
    $fields["to_year"] = date("Y");
    $fields["to_month"] = date("m");
    $fields["to_day"] = date("t");
    extract($fields, EXTR_SKIP);
    // Dates from date range
    $frm_date = dateFmt($frm_year, $frm_month, $frm_day);
    $to_date = dateFmt($to_year, $to_month, $to_day);
    $OUTPUT = "<center>\r\n\t<h3>View Bookings</h3>\r\n\t<form method='post' action='" . SELF . "'>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th colspan='4'>Date Range</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>" . mkDateSelect("frm", $frm_year, $frm_month, $frm_day) . "</td>\r\n\t\t\t<td><b>&nbsp; To &nbsp;</b></td>\r\n\t\t\t<td>" . mkDateSelect("to", $to_year, $to_month, $to_day) . "</td>\r\n\t\t\t<td><input type='submit' value='Continue &raquo' /></td>\r\n\t\t</tr>\r\n\t</table>\r\n\t</form>";
    // Retrieve bookings
    $sql = "SELECT *,\r\n\t\t\t\textract('epoch' FROM from_date) AS e_from,\r\n\t\t\t\textract('epoch' FROM to_date) AS e_to\r\n\t\t\t\tFROM hire.bookings\r\n\t\t\t\tWHERE from_date BETWEEN '{$frm_date}' AND '{$to_date}' OR\r\n\t\t\t\t\tto_date BETWEEN '{$frm_date}' AND '{$to_date}'";
    $booking_rslt = db_exec($sql) or errDie("Unable to retrieve bookings.");
    $booking_out = "";
    while ($booking_data = pg_fetch_array($booking_rslt)) {
        // Retrieve asset
        $sql = "SELECT * FROM cubit.assets WHERE id='{$booking_data['asset_id']}'";
        $asset_rslt = db_exec($sql) or errDie("Unable to retrieve asset.");
        $asset_data = pg_fetch_array($asset_rslt);
        if (!isOurs($asset_data["id"])) {
            continue;
        }
        // Retrieve customer
        $sql = "SELECT * FROM cubit.customers WHERE cusnum='{$booking_data['cust_id']}'";
        $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customer.");
        $cust_data = pg_fetch_array($cust_rslt);
        // Create the output
        $booking_out .= "<tr class='" . bg_class() . "'>\r\n\t\t\t<td align='center'>{$booking_data['id']}</td>\r\n\t\t\t<td>" . date("d-m-Y", $booking_data["e_from"]) . "</td>\r\n\t\t\t<td>" . date("d-m-Y", $booking_data["e_to"]) . "</td>\r\n\t\t\t<td>{$asset_data['des']}</td>\r\n\t\t\t<td>" . getSerial($asset_data["id"]) . "</td>\r\n\t\t\t<td>\r\n\t\t\t\t<a href='../cust-det.php?cusnum={$cust_data['cusnum']}'>\r\n\t\t\t\t\t{$cust_data['surname']}\r\n\t\t\t\t</a>\r\n\t\t\t</td>\r\n\t\t\t<td>\r\n\t\t\t\t<a href='booking_save.php?id={$booking_data['id']}&page_option=edit'>\r\n\t\t\t\t\tEdit Booking\r\n\t\t\t\t</a>\r\n\t\t\t</td>\r\n\t\t\t<td>\r\n\t\t\t\t<a href='booking_remove.php?id={$booking_data['id']}'>\r\n\t\t\t\t\tRemove Booking\r\n\t\t\t\t</a>\r\n\t\t\t</td>\r\n\t\t\t<td>\r\n\t\t\t\t<a href='" . SELF . "?key=hire&id={$booking_data['id']}'\r\n\t\t\t\tstyle='font-size: 1.2em; padding: 0 1em'>\r\n\t\t\t\t\t<b>Hire</b></a>\r\n\t\t\t</td>\r\n\t\t</tr>";
    }
    if (empty($booking_out)) {
        $booking_out = "<tr class='" . bg_class() . "'>\r\n\t\t\t<td colspan='7'><li>No bookings found.</li></td>\r\n\t\t</tr>";
    }
    $OUTPUT .= "<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th>Booking No.</th>\r\n\t\t\t<th>From Date</th>\r\n\t\t\t<th>To Date</th>\r\n\t\t\t<th>Small Plant</th>\r\n\t\t\t<th>Serial No.</th>\r\n\t\t\t<th>Customer</th>\r\n\t\t\t<th colspan='3'>Options</th>\r\n\t\t</tr>\r\n\t\t{$booking_out}\r\n\t</table>";
    return $OUTPUT;
}
function display()
{
    extract($_REQUEST);
    $sql = "SELECT * FROM cubit.assets ORDER BY des ASC";
    $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
    $service_out = "";
    while ($asset_data = pg_fetch_array($asset_rslt)) {
        if (!isOurs($asset_data["id"])) {
            continue;
        }
        // Retrieve the service date
        $sql = "SELECT svdate, des FROM cubit.asset_svdates\r\n\t\t\t\tWHERE asset_id='{$asset_data['id']}'";
        $svdate_rslt = db_exec($sql) or errDie("Unable to retrieve service date.");
        list($svdate, $des) = pg_fetch_array($svdate_rslt);
        $service_out .= "\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>" . getSerial($asset_data["id"]) . "</td>\r\n\t\t\t<td>{$asset_data['des']}</td>\r\n\t\t\t<td>{$svdate}</td>\r\n\t\t\t<td>{$des}</td>\r\n\t\t</tr>";
    }
    if (empty($service_out)) {
        $service_out = "<tr class='" . bg_class() . "'>\r\n\t\t\t<td colspan='3'><li>No results found.</li></td>\r\n\t\t</tr>";
    }
    $OUTPUT = "<center>\r\n\t<h3>Plant Service Dates</h3>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th>Serial</th>\r\n\t\t\t<th>Description</th>\r\n\t\t\t<th>Service Date</th>\r\n\t\t\t<th>Service Description</th>\r\n\t\t</tr>\r\n\t\t{$service_out}\r\n\t</table>\r\n\t</center>";
    return $OUTPUT;
}
function display()
{
    extract($_REQUEST);
    $fields = array();
    $fields["from_year"] = date("Y");
    $fields["from_month"] = date("m");
    $fields["from_day"] = date("d");
    $fields["to_year"] = date("Y");
    $fields["to_month"] = date("m");
    $fields["to_day"] = date("t");
    $fields["group_id"] = -1;
    $fields["type_id"] = -1;
    extract($fields, EXTR_SKIP);
    $from_date = dateFmt($from_year, $from_month, $from_day);
    $to_date = dateFmt($to_year, $to_month, $to_day);
    $sql = "SELECT * FROM cubit.assetgrp ORDER BY grpname ASC";
    $group_rslt = db_exec($sql) or errDie("Unable to retrieve asset group.");
    if ($group_id == -1) {
        $none_sel = "selected='selected'";
        $all_sel = "";
    } elseif ($group_id == 0) {
        $none_sel = "";
        $all_sel = "selected='selected'";
    } else {
        $none_sel = "";
        $all_sel = "";
    }
    $group_sel = "<select name='group_id' style='width: 100%'\r\n\t\t\t\t  onchange='javascript:document.form.submit()'>";
    $group_sel .= "<option value='-1' {$none_sel}>[None]</option>";
    $group_sel .= "<option value='0' {$all_sel}>[All]</option>";
    while ($group_data = pg_fetch_array($group_rslt)) {
        if ($group_id == $group_data["grpid"]) {
            $sel = "selected='selected'";
        } else {
            $sel = "";
        }
        $group_sel .= "<option value='{$group_data['grpid']}' {$sel}>\r\n\t\t\t{$group_data['grpname']}\r\n\t\t</option>";
    }
    $group_sel .= "</select>";
    $sql = "SELECT * FROM cubit.asset_types ORDER BY name ASC";
    $type_rslt = db_exec($sql) or errDie("Unable to retrieve asset type.");
    if ($type_id == -1) {
        $none_sel = "selected='selected'";
        $all_sel = "";
    } elseif ($type_id == 0) {
        $none_sel = "";
        $all_sel = "selected='selected'";
    } else {
        $none_sel = "";
        $all_sel = "";
    }
    $type_sel = "<select name='type_id' style='width: 100%'\r\n\t\t\t\t onchange='javascript:document.form.submit()'>";
    $type_sel .= "<option value='-1' {$none_sel}>[None]</option>";
    $type_sel .= "<option value='0' {$all_sel}>[All]</option>";
    while ($type_data = pg_fetch_array($type_rslt)) {
        if ($type_id == $type_data["id"]) {
            $sel = "selected='selected'";
        } else {
            $sel = "";
        }
        $type_sel .= "<option value='{$type_data['id']}' {$sel}>\r\n\t\t\t\t\t\t{$type_data['name']}\r\n\t\t\t\t\t  </option>";
    }
    $type_sel .= "</select>";
    $OUTPUT = "\r\n\t<center>\r\n\t<h3>Availability Report</h3>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td bgcolor='#ff0000'>&nbsp;</td><td>Hired Out</td>\r\n\t\t\t<td bgcolor='#00ff00'>&nbsp;</td><td>Available</td>\r\n\t\t\t<td bgcolor='#ffa200'>&nbsp;</td><td>Booked</td>\r\n\t\t\t<td bgcolor='#ffff00'>&nbsp;</td><td>In Workshop</td>\r\n\t\t</tr>\r\n\t</table>\r\n\t<p></p>\r\n\t<form method='post' action='" . SELF . "' name='form'>\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th colspan='4'>Date Range</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>" . mkDateSelect("from", $from_year, $from_month, $from_day) . "</td>\r\n\t\t\t<td><b> To </b></td>\r\n\t\t\t<td>" . mkDateSelect("to", $to_year, $to_month, $to_day) . "</td>\r\n\t\t\t<td><input type='submit' value='Select &raquo' /></td>\r\n\t\t</tr>\r\n\t\t<tr><td colspan='4'>\r\n\t\t<table " . TMPL_tblDflts . " width='100%'>\r\n\t\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t\t<td colspan='2' width='50%'>{$group_sel}</td>\r\n\t\t\t\t<td colspan='2' width='50%'>{$type_sel}</td>\r\n\t\t\t</tr>\r\n\t\t</table>\r\n\t\t</td></tr>\r\n\t</table>\r\n\t</form>";
    $where = array();
    if ($group_id) {
        $where[] = "grpid='{$group_id}'";
    }
    if ($type_id) {
        $where[] = "type_id='{$type_id}'";
    }
    if (count($where)) {
        $where = "WHERE " . implode(" AND ", $where);
    } else {
        $where = "";
    }
    // Retrieve assets
    $sql = "SELECT * FROM cubit.assets {$where}";
    $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
    $assets_out = "";
    while ($asset_data = pg_fetch_array($asset_rslt)) {
        if (!isOurs($asset_data["id"])) {
            continue;
        }
        $assets_out .= "<tr class='" . bg_class() . "'>\r\n\t\t\t<td align='center'>\r\n\t\t\t\t" . getSerial($asset_data["id"], 1) . "<br />\r\n\t\t\t\t{$asset_data['des']}\r\n\t\t\t</td>\r\n\t\t\t" . availability($from_date, $to_date, $asset_data["id"]) . "\r\n\t\t</tr>";
    }
    // Do the headers here so we can choose not to show it when no results found
    $headers = date_headers($from_date, $to_date);
    if (empty($assets_out)) {
        $headers = "";
        $assets_out .= "<tr class='" . bg_class() . "'>\r\n\t\t\t<td><li>No results found.</li></td>\r\n\t\t</tr>";
    }
    $OUTPUT .= "<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<th>Plant</th>\r\n\t\t\t{$headers}\r\n\t\t</tr>\r\n\t\t{$assets_out}\r\n\t</table>";
    return $OUTPUT;
}
function enter($errors = "&nbsp;")
{
    extract($_REQUEST);
    // Default values
    $fields = array();
    $fields["page_option"] = "add";
    $fields["id"] = 0;
    $fields["asset_id"] = 0;
    $fields["cust_id"] = 0;
    $fields["from_year"] = date("Y");
    $fields["from_month"] = date("m");
    $fields["from_day"] = date("d");
    $fields["to_year"] = date("Y");
    $fields["to_month"] = date("m");
    $fields["to_day"] = date("d");
    extract($fields, EXTR_SKIP);
    if ($page_option == "edit") {
        if ($id) {
            $sql = "SELECT *,\r\n\t\t\t\t\t\textract('epoch' FROM from_date) AS e_from,\r\n\t\t\t\t\t\textract('epoch' FROM to_date) AS e_to\r\n\t\t\t\t\tFROM hire.bookings WHERE id='{$id}'";
            $booking_rslt = db_exec($sql) or errDie("Unable to retrieve booking.");
            $booking_data = pg_fetch_array($booking_rslt);
            extract($booking_data);
            $from_year = date("Y", $booking_data["e_from"]);
            $from_month = date("m", $booking_data["e_from"]);
            $from_day = date("d", $booking_data["e_from"]);
            $to_year = date("Y", $booking_data["e_to"]);
            $to_month = date("m", $booking_data["e_to"]);
            $to_day = date("d", $booking_data["e_to"]);
        } else {
            $page_option = "edit";
        }
    }
    // Retrieve assets
    $sql = "SELECT id, des FROM cubit.assets ORDER BY des ASC";
    $asset_rslt = db_exec($sql) or errDie("Unable to retrieve assets.");
    // Assets dropdown
    $asset_sel = "<select name='asset_id' style='width: 100%'>";
    $asset_sel .= "<option value='0'>[None]</option>";
    while ($asset_data = pg_fetch_array($asset_rslt)) {
        if (!isOurs($asset_data["id"])) {
            continue;
        }
        if ($asset_id == $asset_data["id"]) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $asset_sel .= "<option value='{$asset_data['id']}' {$sel}>\r\n\t\t\t" . getSerial($asset_data["id"], 1) . " {$asset_data['des']}\r\n\t\t</option>";
    }
    $asset_sel .= "</select>";
    // Retrieve customers
    $sql = "SELECT * FROM cubit.customers ORDER BY surname ASC";
    $cust_rslt = db_exec($sql) or errDie("Unable to retrieve customers.");
    // Customers dropdown
    $cust_sel = "<select name='cust_id' style='width: 100%'>";
    $cust_sel .= "<option value='0'>[None]</option>";
    while ($cust_data = pg_fetch_array($cust_rslt)) {
        if ($cust_id == $cust_data["cusnum"]) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $cust_sel .= "<option value='{$cust_data['cusnum']}' {$sel}>\r\n\t\t\t{$cust_data['surname']}\r\n\t\t</option>";
    }
    $OUTPUT = "<h3>" . ucfirst($page_option) . " Booking</h3>\r\n\t<form method='post' action='" . SELF . "'>\r\n\t<input type='hidden' name='key' value='confirm' />\r\n\t<input type='hidden' name='page_option' value='{$page_option}' />\r\n\t<input type='hidden' name='id' value='{$id}' />\r\n\t<table " . TMPL_tblDflts . ">\r\n\t\t<tr>\r\n\t\t\t<td colspan='2'>{$errors}</td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<th colspan='2'>Booking Details</th>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>" . REQ . "Small Plant</td>\r\n\t\t\t<td>{$asset_sel}</td>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>" . REQ . "Customer</td>\r\n\t\t\t<td>{$cust_sel}</td>\r\n\t\t</tr>\r\n\t\t<tr class='" . bg_class() . "'>\r\n\t\t\t<td>" . REQ . "Booking Date</td>\r\n\t\t\t<td align='center'>\r\n\t\t\t\t" . mkDateSelect("from", $from_year, $from_month, $from_day) . "\r\n\t\t\t\t&nbsp; <b>To &nbsp;\r\n\t\t\t\t" . mkDateSelect("to", $to_year, $to_month, $to_day) . "\r\n\t\t\t</td>\r\n\t\t<tr>\r\n\t\t<tr>\r\n\t\t\t<td colspan='2' align='right'>\r\n\t\t\t\t<input type='submit' value='Confirm &raquo' />\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t</table>\r\n\t</form>";
    return $OUTPUT;
}