function write($_POST)
{
    # Get vars
    global $DOCLIB_DOCTYPES;
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # Validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($typeid, "string", 1, 20, "Invalid type code.");
    if (isset($xin)) {
        $v->isOk($xin, "string", 1, 20, "Invalid {$DOCLIB_DOCTYPES[$typeid]} number.");
    }
    $v->isOk($docname, "string", 1, 255, "Invalid Document name.");
    $v->isOk($docref, "string", 0, 255, "Invalid Document reference.");
    $date = $year . "-" . $mon . "-" . $day;
    if (!checkdate($mon, $day, $year)) {
        $v->isOk($date, "num", 1, 1, "Invalid date.");
    }
    $v->isOk($descrip, "string", 0, 255, "Invalid Document Description.");
    # Display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->enterErrors();
        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($xin)) {
        $typRs = enter('cubit', "*", "doctypes", "typeid", $typeid);
        $typ = pg_fetch_array($typRs);
        $typename = $typ['typename'];
        $xin = 0;
    } else {
        $typename = $DOCLIB_DOCTYPES[$typeid];
    }
    # Connect to db
    db_conn('cubit');
    # Write to db
    $sql = "INSERT INTO documents(typeid, typename, xin, docref, docdate, docname, filename, mimetype, descrip, docu, div) VALUES ('{$typeid}', '{$typename}', '{$xin}', '{$docref}', '{$date}', '{$docname}', '{$filename}', '{$doctyp}', '{$descrip}', '{$docu}', '" . USER_DIV . "')";
    $docRslt = db_exec($sql) or errDie("Unable to add {$docname} to system.", SELF);
    if (pg_cmdtuples($docRslt) < 1) {
        return "<li class=err>Unable to add {$docname} to database.";
    }
    $write = "<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width='50%'>\r\n\t\t<tr><th>Document added to system</th></tr>\r\n\t\t<tr class=datacell><td>New Document <b>{$docname}</b>, has been successfully added to the system.</td></tr>\r\n\t</table>\r\n\t<p>\r\n\t<table border=0 cellpadding='2' cellspacing='1'>\r\n\t\t<tr><th>Quick Links</th></tr>\r\n\t\t<tr class='bg-odd'><td><a href='tdocview.php'>View Documents</a></td></tr>\r\n\t\t<tr class='bg-odd'><td><a href='docman-index.php'>Document Management</a></td></tr>\r\n\t</table>";
    return $write;
}