function write()
{
    extract($_REQUEST);
    if ($page_option == "Edit") {
        $sql = "UPDATE cubit.diary_locations SET location='{$location}'\n\t\tWHERE id='{$id}'";
    } else {
        $sql = "INSERT INTO cubit.diary_locations (location) VALUES ('{$location}')";
    }
    db_exec($sql) or errDie("Unable to save location.");
    if (frmupdate_passon()) {
        $newlist = new dbSelect("diary_locations", "cubit");
        $newlist->run();
        $a = array();
        if ($newlist->num_rows() > 0) {
            while ($row = $newlist->fetch_array()) {
                $a[$row["id"]] = "{$row['location']}";
            }
        }
        $js = frmupdate_exec(array($a), true);
    } else {
        $js = "";
    }
    $OUTPUT = "{$js}\n\t<h3>{$page_option} Location</h3>\n\t<table cellpadding='2' cellspacing='0' class='shtable'>\n\t\t<tr>\n\t\t\t<th>Write</th>\n\t\t</tr>\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td>Successfully saved location.</td>\n\t\t</tr>\n\t</table>";
    return $OUTPUT;
}
function write($_POST)
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($classcode, "string", 1, 255, "Invalid Classification code.");
    $v->isOk($classname, "string", 1, 255, "Invalid Classification name.");
    # 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;
    }
    # check stock code
    db_connect();
    $sql = "SELECT classcode FROM stockclass WHERE lower(classcode) = lower('{$classcode}') AND div = '" . USER_DIV . "'";
    $cRslt = db_exec($sql);
    if (pg_numrows($cRslt) > 0) {
        $error = "<li class='err'> A Classification with code : <b>{$classcode}</b> already exists.</li>";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        return $error;
    }
    # connect to db
    db_connect();
    # write to db
    $sql = "INSERT INTO stockclass(classcode, classname, div) VALUES ('{$classcode}', '{$classname}', '" . USER_DIV . "')";
    $catRslt = db_exec($sql) or errDie("Unable to add class to system.", SELF);
    if (pg_cmdtuples($catRslt) < 1) {
        return "<li class='err'>Unable to add classname to database.</li>";
    }
    if (frmupdate_passon()) {
        $newlst = new dbSelect("stockclass", "cubit", grp(m("cols", "clasid, classname"), m("where", "div='" . USER_DIV . "'"), m("order", "classname ASC")));
        $newlst->run();
        $a = array();
        if ($newlst->num_rows() > 0) {
            while ($row = $newlst->fetch_array()) {
                $a[$row["clasid"]] = $row["classname"];
            }
        }
        $js = frmupdate_exec(array($a), true);
    } else {
        $js = "";
    }
    $write = "\n\t\t\t\t{$js}\n\t\t\t\t<table " . TMPL_tblDflts . " width='50%'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Classification added to system</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>New Classification <b>{$classname}</b>, has been successfully added to the system.</td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t\t<p>\n\t\t\t\t<table border=0 cellpadding='2' cellspacing='1'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Quick Links</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><a href='stockclass-view.php'>View Classifications</a></td>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>";
    return $write;
}
function write($_POST)
{
    # get vars
    foreach ($_POST as $key => $value) {
        ${$key} = $value;
    }
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($catcod, "string", 0, 50, "Invalid category code.");
    $v->isOk($cat, "string", 1, 255, "Invalid stock category name.");
    $v->isOk($descript, "string", 0, 100, "Invalid stock category descripting.");
    # Display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class=err>{$e['msg']}</li>";
        }
        $confirm .= "</li><p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>\n\t\t\t\t<P>\n\t\t\t\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width=100>\n\t\t\t\t<tr><th>Quick Links</th></tr>\n\t\t\t\t\t<tr class='bg-even'><td><a href='stockcat-view.php'>View Stock Category</a></td></tr>\n\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t</form>\n\t\t\t\t</table>";
        return $confirm;
    }
    # check stock code
    db_connect();
    $sql = "SELECT catcod FROM stockcat WHERE lower(catcod) = lower('{$catcod}') AND div = '" . USER_DIV . "'";
    $cRslt = db_exec($sql);
    if (pg_numrows($cRslt) > 0) {
        $error = "<li class=err> A Category with code : <b>{$catcod}</b> already exists.</li>";
        $error .= "<p><input type=button onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
        //return $error;
    }
    // insert into stock
    db_connect();
    $sql = "INSERT INTO stockcat(catcod, cat, descript, div) VALUES('{$catcod}', '{$cat}', '{$descript}', '" . USER_DIV . "')";
    $rslt = db_exec($sql) or errDie("Unable to insert stock category to Cubit.", SELF);
    if (frmupdate_passon()) {
        $newlst = new dbSelect("stockcat", "cubit", grp(m("cols", "catid, catcod, cat"), m("where", "div='" . USER_DIV . "'"), m("order", "cat ASC")));
        $newlst->run();
        $a = array();
        if ($newlst->num_rows() > 0) {
            while ($row = $newlst->fetch_array()) {
                $a[$row["catid"]] = "({$row['catcod']}) {$row['cat']}";
            }
        }
        $js = frmupdate_exec(array($a), true);
    } else {
        $js = "";
    }
    $write = "\n\t{$js}\n\t<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "' width='50%'>\n\t\t<tr><th>New Stock Category added to database</th></tr>\n\t\t<tr class=datacell><td>New Stock Category, {$cat} ({$catcod}) has been successfully added to Cubit.</td></tr>\n\t</table>\n\t<p>\n\t<table border=0 cellpadding='2' cellspacing='1'>\n\t\t<tr><th>Quick Links</th></tr>\n\t\t<tr class='bg-even'><td><a href='stockcat-view.php'>View Stock Category</a></td></tr>\n\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t</table>";
    return $write;
}
function write()
{
    extract($_REQUEST);
    if ($page_option == "Edit") {
        $sql = "UPDATE cubit.todo_main SET title='{$title}', team_id='{$team_id}'\n\t\tWHERE id='{$id}' AND user_id='" . USER_ID . "'";
    } else {
        $sql = "INSERT INTO cubit.todo_main (title, user_id, team_id)\n\t\tVALUES ('{$title}', '" . USER_ID . "', '{$team_id}')";
    }
    db_exec($sql) or errDie("Unable to save main todo.");
    if (frmupdate_passon()) {
        $newlist = new dbSelect("todo_main", "cubit");
        $newlist->run();
        // are we an admin?
        $sql = "SELECT admin FROM cubit.users WHERE userid='" . USER_ID . "'";
        $admin_rslt = db_exec($sql) or errDie("Unable to check for admin.");
        $admin = pg_fetch_result($admin_rslt, 0);
        $a = array();
        if ($newlist->num_rows() > 0) {
            $a[0] = "[None]";
            while ($row = $newlist->fetch_array()) {
                if (in_team(USER_ID, $row["team_id"])) {
                    $sql = "SELECT * FROM cubit.todo_main WHERE id='{$row['id']}'";
                    $tm_rslt = db_exec($sql) or errDie("Unable to retrieve todo.");
                    $count = pg_num_rows($tm_rslt);
                    $a[$row["id"]] = "{$row['title']} ({$count})";
                } else {
                    continue;
                }
            }
        }
        $js = frmupdate_exec(array($a), true);
    } else {
        $js = "";
    }
    $OUTPUT = "{$js}\n\t<h3>{$page_option} Main Todo</h3>\n\t<table cellpadding='2' cellspacing='0' class='shtable'>\n\t\t<tr>\n\t\t\t<th>Write</th>\n\t\t</tr>\n\t\t<tr class='odd'><td>Successfully saved the main todo.</td></tr>\n\t</table>";
    return $OUTPUT;
}
function select()
{
    $cusnum = $_REQUEST["cusnum"];
    $OUT = frmupdate_exec(array($cusnum));
    return $OUT;
}