#
#
#
#
require "../settings.php";
require "../core-settings.php";
if (isset($_POST["key"])) {
    // Make all the keys lowecase to maintain consistency
    $_POST["key"] = strtolower($_POST["key"]);
    switch ($_POST["key"]) {
        default:
        case "display":
            $OUTPUT = display();
            break;
        case "customize":
            $OUTPUT = customize($_POST);
            break;
        case "update":
            $OUTPUT = update($_POST);
            break;
    }
} else {
    $OUTPUT = display();
}
function customize()
{
    print metaphone("That's just wack");
    $fields = array();
    $fields["heading_1"] = COMP_NAME;
    $fields["heading_2"] = date("d/m/Y");
    $fields["heading_3"] = "Balance Sheet";
function update($_POST)
{
    extract($_POST);
    if ($key == "add" && isset($naccount) && $naccount != 0) {
        // Has this account been added already?
        db_conn("cubit");
        $sql = "SELECT * FROM saved_tb_accounts WHERE accid='{$naccount}'";
        $stb_rslt = db_exec($sql) or errDie("Unable to retrieve saved trial balance accounts from Cubit.");
        if (pg_num_rows($stb_rslt) > 0) {
            return customize($_POST);
        }
        // Retrieve the account info with the accid
        db_conn("core");
        $sql = "SELECT * FROM accounts WHERE accid='{$naccount}' AND div='" . USER_DIV . "'";
        $acc_rslt = db_exec($sql) or errDie("Unable to retrieve account information from Cubit.");
        $acc_data = pg_fetch_array($acc_rslt);
        // Insert into the db
        db_conn("cubit");
        $sql = "\n\t\t\tINSERT INTO saved_tb_accounts (\n\t\t\t\taccid, topacc, accnum, accname\n\t\t\t) VALUES (\n\t\t\t\t'{$acc_data['accid']}', '{$acc_data['topacc']}', '{$acc_data['accnum']}', '{$acc_data['accname']}'\n\t\t\t)";
        $stbacc_rslt = db_exec($sql) or errDie("Unable to update your customization settings to Cubit.");
    }
    // Remove selected items
    if ($key == "remove selected" && isset($rem)) {
        foreach ($rem as $id) {
            db_conn("cubit");
            $sql = "DELETE FROM saved_tb_accounts WHERE id='{$id}'";
            $stbacc_rslt = db_exec($sql) or errDie("Unable to remove account from the accounts list.");
        }
    }
    return customize($_POST);
}
<table width="300" border="0" cellpadding="2px">
<?php } ?>
<?php
if (isset($_POST['customize'])) {

        $name = $_POST['char_c_name'];
        $status = check_if_online($name, $CHDB);
        if ($status == -1) {
            echo "<p align='center'><font color='red'>The character doesnt exsist!
                </font></p>";
            exit();
        }
		if ($status == 1)
        echo "<p align='center'><font color='red'>This character is online. Please try again later</font></p>";
		else {
            customize($name, $CHDB, $DB);
			$DB->query("UPDATE `voting_points` SET `points`=(`points` - ".$char_custom_points."), `points_spent`=(`points_spent` + ".$char_custom_points.")
			WHERE id=?d",$account_id);
        echo "<p align='center'><font color='blue'>Success! You are able to customize you character at next login!</font></p>";
    }
}
?>
</table>
</center>
</td>
</tr>
</table>
</td></tr></table>
</td></tr></table>
</center>
<br />
function update($_POST)
{
    extract($_POST);
    if ($key == "add" && $account != 0) {
        // has this account already been added
        db_conn("cubit");
        $sql = "SELECT * FROM saved_bs_accounts WHERE accid='{$account}'";
        $sbsacc_rslt = db_exec($sql) or errDie("Unable to retieve accounts list from Cubit.");
        if (pg_num_rows($sbsacc_rslt) == 0) {
            db_conn("core");
            $sql = "SELECT * FROM accounts WHERE accid='{$account}'";
            $acc_rslt = db_exec($sql) or errDie("Unable to retrieve accounts from Cubit.");
            $acc_data = pg_fetch_array($acc_rslt);
            db_conn("cubit");
            $sql = "\n\t\t\t\tINSERT INTO saved_bs_accounts (\n\t\t\t\t\taccid, topacc, accnum, accname, toptype\n\t\t\t\t) VALUES (\n\t\t\t\t\t'{$acc_data['accid']}', '{$acc_data['topacc']}', '{$acc_data['accnum']}', '{$acc_data['accname']}', '{$acc_data['toptype']}'\n\t\t\t\t)";
            $sbsacc_rslt = db_exec($sql) or errDie("Unable to save the account to the accounts list.");
        }
    }
    if ($key == "update") {
        foreach ($toptype as $accid => $value) {
            db_conn("cubit");
            $sql = "UPDATE saved_bs_accounts SET toptype='{$value}' WHERE accid='{$accid}'";
            $rslt = db_exec($sql);
        }
    }
    if ($key == "remove selected" && isset($rem)) {
        foreach ($rem as $accid => $value) {
            db_conn("cubit");
            $sql = "DELETE FROM saved_bs_accounts WHERE accid='{$accid}'";
            $sbsacc_rslt = db_exec($sql) or errDie("Unable to delete selected entries from the accounts list.");
        }
    }
    return customize($_POST);
}