switch ($_POST["key"]) {
        case "slctacc":
            $OUTPUT = slctAcc($_POST);
            break;
        case "confirm":
            $OUTPUT = confirm($_POST);
            break;
        case "write":
            $OUTPUT = write($_POST);
            break;
        default:
            $OUTPUT = slctCat($type, $payname);
    }
} else {
    # Display default output
    $OUTPUT = slctCat($type, $payname);
}
# get templete
require "template.php";
# Default View (Selected catagory)
function slctCat($type, $payname)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($type, "string", 1, 3, "Invalid catagory type.");
    $v->isOk($payname, "string", 1, 255, "Invalid Salary payment name to be linked.");
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
function write($_POST)
{
    extract($_POST);
    if (isset($back)) {
        unset($_POST["back"]);
        return slctCat($_POST);
    }
    require_lib("validate");
    $v = new validate();
    $v->isOk($type, "string", 1, 2, "Invalid category type.");
    $v->isOk($tab, "string", 1, 14, "Invalid category type.");
    $v->isOk($accname, "string", 1, 50, "Invalid account name.");
    $v->isOk($catid, "string", 1, 50, "Invalid category Id/name.");
    $v->isOk($topacc, "num", 4, 4, "Invalid account number.");
    $v->isOk($accnum, "num", 1, 3, "Invalid account number.");
    $v->isOk($toptype, "string", 1, 255, "Invalid category type.");
    if ($v->isError()) {
        $err = $v->genErrors();
        return $err;
    }
    core_connect();
    $sql = "SELECT * FROM accounts WHERE accname = '{$accname}' AND div = '" . USER_DIV . "'";
    $checkRslt = db_exec($sql) or errDie("Unable to retrieve Account details from database.");
    if (pg_numrows($checkRslt) > 0) {
        $confirm = "\n\t\t\t<tr>\n\t\t\t\t<td colspan='2' class='err'>Account name already exist.</td>\n\t\t\t</tr>";
        return slctCaterr($type, $tab, $accname, $catid, $topacc, $accnum, $confirm);
        exit;
    }
    # Check Account Number
    core_connect();
    $sql = "SELECT * FROM accounts WHERE topacc = '{$topacc}' AND accnum = '{$accnum}' AND div = '" . USER_DIV . "'";
    $checkRslt = db_exec($sql) or errDie("Unable to retrieve Account details from database.");
    $check = pg_numrows($checkRslt);
    if (pg_numrows($checkRslt) > 0) {
        $confirm = "\n\t\t\t<tr>\n\t\t\t\t<td colspan='2' class='err'>The Account number is already in use.</td>\n\t\t\t</tr>";
        return slctCaterr($type, $tab, $accname, $catid, $topacc, $accnum, $confirm);
        exit;
    }
    # Begin sql transaction
    pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
    // Get the db value of toptype
    $toptype_db = explode(":", $toptype);
    $toptype_db = $toptype_db[0];
    # write to DB
    $sql = "\n\t\tINSERT INTO accounts (\n\t\t\ttopacc, accnum, accname, acctype, catid, div, toptype\n\t\t) VALUES (\n\t\t\t'{$topacc}', '{$accnum}', '{$accname}','{$type}', '{$catid}', '" . USER_DIV . "', '{$toptype_db}'\n\t\t)";
    $catRslt = db_exec($sql) or errDie("Unable to add Account to Database.", SELF);
    # get last inserted id for new acc
    $accid = pglib_lastid("accounts", "accid");
    global $MONPRD;
    $month_names = array(0, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    # insert account into trial Balance
    insert_trialbal($accid, $topacc, $accnum, $accname, $type, 'f', USER_DIV);
    for ($i = 1; $i <= 12; $i++) {
        $periodname = getMonthName($i);
        $sql = "\n\t\t\tINSERT INTO " . YR_DB . ".{$periodname} (\n\t\t\t\taccid, topacc, accnum, accname, debit, credit, div\n\t\t\t) SELECT accid, topacc, accnum, accname, debit, credit, div FROM core.trial_bal WHERE month='{$i}' AND accid='{$accid}'";
        db_exec($sql) or die($sql);
        $sql = "\n\t\t\tINSERT INTO \"{$i}\".openbal (\n\t\t\t\taccid, accname, debit, credit, div\n\t\t\t) SELECT accid, accname, debit, credit, div FROM core.trial_bal WHERE month='{$i}' AND accid='{$accid}'";
        db_exec($sql) or die($sql);
        $sql = "\n\t\t\tINSERT INTO \"{$i}\".ledger (\n\t\t\t\tacc, contra, edate, eref, descript, credit, debit, div, caccname, ctopacc, caccnum, cbalance, dbalance\n\t\t\t) SELECT accid, accid, CURRENT_DATE, '0', 'Balance', '0', '0', div, accname, topacc, accnum, credit, debit \n\t\t\tFROM core.trial_bal \n\t\t\tWHERE month='{$i}' AND accid='{$accid}'";
        db_exec($sql) or die($sql);
    }
    pglib_transaction("COMMIT") or errDie("Unable to start a database transaction.", SELF);
    block();
    //			print "
    //			<script>
    //				window.opener.location.reload();
    //				window.close();
    //			</script>";
    if (isset($update_parent) and $update_parent == "yes") {
        #do something to reload the parent window ...
        print "\n\t\t\t<script>\n\t\t\t\twindow.opener.document.form.key.value='{$set_key}';\n\t\t\t\twindow.opener.document.form.submit ();\n\t\t\t\twindow.close();\n\t\t\t</script>";
    } else {
        #do normal return
        # status report
        $write = "\n\t\t\t<table " . TMPL_tblDflts . " width='50%'>\n\t\t\t\t<tr>\n\t\t\t\t\t<th>New Account</th>\n\t\t\t\t</tr>\n\t\t\t\t<tr class='datacell'>\n\t\t\t\t\t<td>New Account, <b>({$topacc}/{$accnum}) - {$accname}</b> was successfully added to Cubit.</td>\n\t\t\t\t</tr>\n\t\t\t</table><br>" . mkQuickLinks(ql("../reporting/allcat.php", "List All Accounts (New Window)", true), ql("acc-view.php", "View Accounts"), ql("acc-new2.php", "Add Account"));
        return $write;
    }
}
#
#
#
#
#
#
#
#
# get settings
require "../settings.php";
require "../core-settings.php";
# decide what to do
if (isset($_POST["key"])) {
    switch ($_POST["key"]) {
        case "slctcat":
            $OUTPUT = slctCat($_POST);
            break;
        case "viewtrans":
            $OUTPUT = viewtrans($_POST);
            break;
        default:
            $OUTPUT = view();
    }
} else {
    # Display default output
    $OUTPUT = view();
}
# get templete
require "../template.php";
# Default view
function view()
            break;
        case "write":
            $OUTPUT = write($_POST);
            break;
        default:
            if (isset($_GET["acctype"]) && isset($_GET["payname"])) {
                # Display default output
                $OUTPUT = slctCat($_GET["acctype"], $_GET["payname"]);
            } else {
                $OUTPUT = "<li>ERROR : Invalid use of module";
            }
    }
} else {
    if (isset($_GET["acctype"]) && isset($_GET["payname"])) {
        # Display default output
        $OUTPUT = slctCat($_GET["acctype"], $_GET["payname"]);
    } else {
        $OUTPUT = "<li>ERROR : Invalid use of module";
    }
}
# Get templete
require "template.php";
# Default View (Selected catagory)
function slctCat($acctype, $payname)
{
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($acctype, "string", 1, 3, "Invalid category type.");
    $v->isOk($payname, "string", 1, 255, "Invalid Salary payment name to be linked.");
    # display errors, if any