function enter($VARS = array(), $errors = "")
{
    extract($VARS);
    global $scGLOB;
    eval($scGLOB);
    if (!isset($typeid)) {
        $xin = "";
        $xins = $xin;
        $typeid = "";
        $docref = "";
        $docname = "";
        $day = date("d");
        $mon = date("m");
        $year = date("Y");
        $descrip = "";
    } else {
        $xin = isset($xin) ? $xin : "";
        $xins = $xin;
        $xin = xin($typeid, $xin);
    }
    # Select Type
    db_conn("yr2");
    $typs = "<select name='typeid' onchange='document.form1.submit();'>";
    # User types
    $sql = "SELECT * FROM doctypes WHERE div = '" . USER_DIV . "' ORDER BY typename ASC";
    $typRslt = db_exec($sql);
    if (pg_numrows($typRslt) < 1) {
        if (strlen($typeid) < 1) {
            $typeid = "inv";
        }
        $xin = xin_gw($typeid, $xins);
    } else {
        while ($typ = pg_fetch_array($typRslt)) {
            $sel = "";
            if ($typ['typeid'] == $typeid) {
                $sel = "selected";
            }
            $typs .= "<option value='{$typ['typeid']}' {$sel}>({$typ['typeref']}) {$typ['typename']}</option>";
        }
    }
    # Built-in types
    foreach ($DOCLIB_DOCTYPES as $tkey => $val) {
        $sel = "";
        if ($tkey == $typeid) {
            $sel = "selected";
        }
        $typs .= "<option value='{$tkey}' {$sel}>{$DOCLIB_DOCTYPES[$tkey]}</option>";
    }
    $typs .= "</select>";
    $enter = "<h3>Add Document</h3>\n\t<form name=form1 ENCTYPE='multipart/form-data' action='" . SELF . "' method=post>\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\n\t<input type=hidden name=key value=confirm>\n\t<tr>\n\t\t<td colspan=2>{$errors}</td>\n\t</tr>\n\t<tr>\n\t\t<th>Field</th>\n\t\t<th>Value</th>\n\t</tr>\n\t<tr class='even'>\n\t\t<td>" . REQ . "Type</td>\n\t\t<td>{$typs}</td>\n\t</tr>\n\t{$xin}\n\t<tr class='odd'><td>" . REQ . "Document Name</td><td><input type=text size=20 name=docname value='{$docname}'></td></tr>\n\t<tr class='even'><td>Ref</td><td><input type=text size=10 name=docref value='{$docref}'></td></tr>\n\t<tr class='odd'><td>" . REQ . "Date</td><td><input type=text size=2 name=day maxlength=2  value='{$day}'>-<input type=text size=2 name=mon maxlength=2  value='{$mon}'>-<input type=text size=4 name=year maxlength=4 value='{$year}'></td></tr>\n\t<tr class='even'><td>File</td><td><input type=file size=20 name=doc></td></tr>\n\t<tr class='odd'><td>User Access to Document</td><td><input type=radio name=docaccess value='Yes' checked>Yes | <input type=radio name=docaccess value='No'>No</td>\n\t<tr class='even'><td>Description</td><td><textarea name=descrip rows=4 cols=18>{$descrip}</textarea></td></tr>\n\t<tr><td><br></td></tr>\n\t<tr><td colspan=2 align=right><input type=submit name=conf value='Confirm &raquo;'></td></tr>\n\t</table></form>";
    return $enter;
}
function select()
{
    global $DOCLIB_DOCTYPES;
    if (!isset($typeid)) {
        $xin = "";
        $xins = $xin;
        $typeid = "";
        $docref = "";
        $docname = "";
        $day = date("d");
        $mon = date("m");
        $year = date("Y");
        $descrip = "";
    } else {
        $xin = isset($xin) ? $xin : "";
        $xins = $xin;
        $xin = xin_gw($typeid, $xin);
    }
    db_conn("yr2");
    $typs = "<select name='typeid'>\r\n\t<option value=0>All</option>";
    # User types
    $sql = "SELECT * FROM doctypes WHERE div = '" . USER_DIV . "' ORDER BY typename ASC";
    $typRslt = db_exec($sql);
    if (pg_numrows($typRslt) < 1) {
        if (strlen($typeid) < 1) {
            $typeid = "inv";
        }
        $xin = xin_gw($typeid, $xins);
    } else {
        while ($typ = pg_fetch_array($typRslt)) {
            $sel = "";
            if ($typ['typeid'] == $typeid) {
                $sel = "selected";
            }
            $typs .= "<option value='{$typ['typeid']}' {$sel}>({$typ['typeref']}) {$typ['typename']}</option>";
        }
    }
    # Built-in types
    foreach ($DOCLIB_DOCTYPES as $tkey => $val) {
        $sel = "";
        if ($tkey == $typeid) {
            $sel = "selected";
        }
        $typs .= "<option value='{$tkey}' {$sel}>{$DOCLIB_DOCTYPES[$tkey]}</option>";
    }
    $typs .= "</select>";
    $out = "<h3>View Documents</h3>\r\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t<form action='" . SELF . "' method=post>\r\n\t<input type=hidden name=key value='view'>\r\n\t<tr><th colspan=2>View Options</th></tr>\r\n\t<tr class='even'><td>Document Types</td><td>{$typs}</td></tr>\r\n\t<tr><td colspan=2 align=right><input type=submit value='View &raquo;'></td></tr>\r\n\t</form>\r\n\t</table>";
    return $out;
}
function edit($VARS = array(), $errors = "")
{
    # Get vars
    global $DOCLIB_DOCTYPES;
    foreach ($VARS as $key => $value) {
        ${$key} = $value;
    }
    # Validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($docid, "string", 1, 20, "Invalid document 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;
    }
    if (!isset($typeid)) {
        $docRs = get("yr2", "*", "documents", "docid", $docid);
        $doc = pg_feTch_array($docRs);
        $xin = $doc['xin'];
        $typeid = $doc['typeid'];
        $docname = $doc['docname'];
        $docref = $doc['docref'];
        list($year, $mon, $day) = explode("-", $doc['docdate']);
        $descrip = $doc['descrip'];
    }
    # Extra in
    $xin = isset($xin) ? $xin : "";
    $xins = $xin;
    $xin = xin_gw($typeid, $xin);
    # Select Type
    db_conn("yr2");
    $typs = "<select name='typeid' onchange='document.form1.submit();'>";
    # User types
    $sql = "SELECT * FROM doctypes WHERE div = '" . USER_DIV . "' ORDER BY typename ASC";
    $typRslt = db_exec($sql);
    if (pg_numrows($typRslt) < 1) {
        if (preg_match("/\\d/", $typeid)) {
            $typeid = 'inv';
        }
        $xin = xin_gw($typeid, $xins);
    } else {
        while ($typ = pg_fetch_array($typRslt)) {
            $sel = "";
            if ($typ['typeid'] == $typeid) {
                $sel = "selected";
            }
            $typs .= "<option value='{$typ['typeid']}' {$sel}>({$typ['typeref']}) {$typ['typename']}</option>";
        }
    }
    # Built-in types
    foreach ($DOCLIB_DOCTYPES as $tkey => $val) {
        $sel = "";
        if ($tkey == $typeid) {
            $sel = "selected";
        }
        $typs .= "<option value='{$tkey}' {$sel}>{$DOCLIB_DOCTYPES[$tkey]}</option>";
    }
    $typs .= "</select>";
    $enter = "<h3>Edit Document</h3>\r\n\t<form name=form1 action='" . SELF . "' method=post>\r\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t<input type=hidden name=key value=confirm>\r\n\t<input type=hidden name=docid value='{$docid}'>\r\n\t<tr><td colspan=2>{$errors}</td></tr>\r\n\t<tr><th>Field</th><th>Value</th></tr>\r\n\t<tr class='even'><td>Type</td><td>{$typs}</td></tr>\r\n\t{$xin}\r\n\t<tr class='odd'><td>Ref</td><td><input type=text size=10 name=docref value='{$docref}'></td></tr>\r\n\t<tr class='even'><td>Document Name</td><td><input type=text size=20 name=docname value='{$docname}'></td></tr>\r\n\t<tr class='odd'><td>Date</td><td><input type=text size=2 name=day maxlength=2  value='{$day}'>-<input type=text size=2 name=mon maxlength=2  value='{$mon}'>-<input type=text size=4 name=year maxlength=4 value='{$year}'></td></tr>\r\n\t<tr class='even'><td>Decription</td><td><textarea name=descrip rows=4 cols=18>{$descrip}</textarea></td></tr>\r\n\t<tr><td><br></td></tr>\r\n\t<tr><td colspan=2 align=right><input type=submit name=conf value='Confirm &raquo;'></td></tr>\r\n\t</table></form>";
    return $enter;
}