function getItems($oid)
{
    global $SETTINGS;
    $order = dbGetOrder($oid);
    $items = dbGetItems($oid);
    $packages = dbGetPackages();
    $pieces = dbGetPieces();
    $retarray = array();
    foreach ($items as $item) {
        $row = array();
        $row["PKID"] = $item["PKID"];
        $row["Quantity"] = $item["Quantity"];
        $row["Name"] = findPackageName($item["PKID"], $packages);
        $row["Personality"] = $item["Personality"];
        if ($item["Personality"]) {
            $row["Personality"] = findPieceName($item["Personality"], $pieces);
        }
        $row["Price"] = findPackagePrice($item["PKID"], $packages);
        $retarray[] = $row;
    }
    // add the charity chapr marketing kit if needed
    if ($order["Charity"]) {
        $row = array();
        $row["PKID"] = $SETTINGS["CharityKitPKID"];
        $row["Quantity"] = 1;
        $row["Name"] = findPackageName($row["PKID"], $packages);
        $row["Personality"] = null;
        $retarray[] = $row;
    }
    return $retarray;
}
function orderFields($data, $badFields)
{
    global $SETTINGS;
    // display data not supposed to be visible for customers
    if (userLoggedIn()) {
        if (array_key_exists("OID", $data) && $data["OID"] != "") {
            tableRow(array(tableData(prompt("<b>OID: </b>"), "right", "top"), tableData(prompt($data["OID"]), "left", "top")));
        }
        tableRow(array(tableData(prompt("<b>Expedite:</b>"), "right", "top"), tableData(checkBox($data, "isExpedited", "true", "YES"), "left", "middle", 3)));
        prepDatePicker();
        tableRow(array(tableData(prompt("<b>Ordered Date:</b>"), "right", "top"), tableData(text($data, "orderedDate", "", "", "datepicker"), "left", "middle")));
    }
    // show the order amount change fields if the user has permission (and in WordPress)
    if (inWordPress() && current_user_can("can_change_amounts")) {
        tableRow(array(tableData(prompt("<b>Shipping Fee:</b>", in_array("shippingFee", $badFields)), "right"), tableData(text($data, "shippingFee", null, "10"), "left"), tableData(prompt("<b>Expedite Fee:</b>", in_array("expediteFee", $badFields)), "right"), tableData(text($data, "expediteFee", null, "10"), "left"), tableData(prompt("<b>Discount:</b>", in_array("discount", $badFields)), "right"), tableData(text($data, "discount", null, "10"), "left")));
    }
    // figure out how many rows to display initially ($i is set to that value)
    for ($i = $SETTINGS["MaxItems"]; $i > 1; $i--) {
        if (array_key_exists("packages{$i}", $data) && $data["packages{$i}"] != "" && $data["packages{$i}"] != 0 || array_key_exists("personality{$i}", $data) && $data["personality{$i}"] != "" && $data["personality{$i}"] != 0 || array_key_exists("quantity{$i}", $data) && $data["quantity{$i}"] != "" && $data["quantity{$i}"] != 0) {
            break;
        }
    }
    $initialRows = $i;
    // get currently available packages (from database) for display
    $rows = dbGetPackages();
    $displayPackages = array();
    foreach ($rows as $row) {
        if ($row["Active"]) {
            $displayPackages[$row["PackageName"]] = $row["PKID"];
        }
    }
    // get currently available personalities (from database) for display
    $rows = dbGetPersonalities();
    $displayPersonalities = array();
    foreach ($rows as $row) {
        if ($row["Active"]) {
            $displayPersonalities[$row["PieceName"]] = $row["PID"];
        }
    }
    if (!userLoggedIn()) {
        tableRow(array(tableData(prompt("Note: \"personality\" refers to the type of software or platform the firmware is compatible with.\n<br> It can be changed later using a USB stick, but we might as well set it for you."), "middle", "top", 6)));
    }
    for ($i = 1; $i <= $SETTINGS["MaxItems"]; $i++) {
        // note that the "table-row" setting for display is controversial and may
        // not work well in Microsoft IE
        // note, too, that the reason while rows 2 through 5 don't initially display
        // is that they are set as display = 'none' in the style sheet - if that
        // is turned off, then they will display right away
        $magicClick = "";
        if ($i != $SETTINGS["MaxItems"]) {
            $magicClick = "<button id=\"prodrowclick-";
            $magicClick .= $i;
            $magicClick .= "\"";
            if ($i != $initialRows) {
                $magicClick .= " style=\"visibility:hidden;\"";
            }
            $magicClick .= " type=\"button\" onclick=\"";
            $magicClick .= "document.getElementById('prodrow-";
            $magicClick .= $i + 1;
            // sets the next row to visible
            $magicClick .= "').style.display = 'table-row';";
            if ($i < $SETTINGS["MaxItems"] - 1) {
                $magicClick .= "document.getElementById('prodrowclick-";
                $magicClick .= $i + 1;
                // sets the next button to visible
                $magicClick .= "').style.visibility = 'visible';";
            }
            $magicClick .= "document.getElementById('prodrowclick-";
            $magicClick .= $i;
            // sets its own button to hidden
            $magicClick .= "').style.visibility = 'hidden';";
            $magicClick .= "\">+</button>";
        }
        if (userLoggedIn() && array_key_exists("iid{$i}", $data) && $data["IID"] != "") {
            tableRow(array(tableData(prompt("<b>IID{$i}:</b>"), "right", "top"), tableData(prompt($data["iid{$i}"]), "left", "top")));
        }
        tableRow(array(tableData(prompt("<b>Product*:</b>", in_array("product{$i}", $badFields)), "right"), tableData(dropDown($data, "packages{$i}", $displayPackages, "----------Select Product----------")), tableData(prompt("<b>Personality:</b>", in_array("personality{$i}", $badFields)), "right"), tableData(dropDown($data, "personality{$i}", $displayPersonalities, " ")), tableData(prompt("<b>Quantity*:</b>", in_array("quantity{$i}", $badFields)), "right"), tableData(text($data, "quantity{$i}", "", "2"), "left"), tableData($magicClick)), "prodrow-" . $i, $i <= $initialRows);
        hiddenField("iid{$i}", $data["iid{$i}"]);
    }
    if (!userLoggedIn()) {
        tableRow(array(tableData(prompt("Write anything you would like us to know about the order: <br> a deadline you need to meet, some option you want that isn't offered etc."), "middle", "top", 6)));
    }
    tableRow(array(tableData(prompt("<b>Order Notes:</b>"), "right", "top"), tableData(textArea($data, "customerONotes", 5), "left", "", 5)));
    if (userLoggedIn()) {
        tableRow(array(tableData(prompt("<b>Admin Order Notes:</b>"), "right", "top"), tableData(textArea($data, "adminONotes", 5), "left", "", 5)));
    }
    hiddenField("charity", $data["charity"]);
    hiddenField("OID", $data["OID"]);
}
function showOrderOrder($order, $page_id, $oid, $edit, $showEditButton)
{
    global $SETTINGS;
    $retval = "";
    $retval .= "<tr><th style=\"text-align:center\">QTY</th>";
    $retval .= "<th>Item</th>";
    $retval .= "<th>Personality</th>";
    $retval .= "<th style=\"text-align:right\">Price</th></tr>";
    $items = dbGetItems($oid);
    $packages = dbGetPackages();
    $pieces = dbGetPieces();
    $total = 0;
    $totalItems = 0;
    foreach ($items as $item) {
        $qty = $item["Quantity"];
        $retval .= "<tr><td style=\"text-align:center\"><strong>" . $qty . "</strong></td>";
        $retval .= "<td><strong>" . findPackageName($item["PKID"], $packages) . "</strong></td>";
        $retval .= "<td><strong>" . findPieceName($item["Personality"], $pieces) . "</strong></td>";
        $price = findPackagePrice($item["PKID"], $packages) * $qty;
        $retval .= "<td style=\"text-align:right\"><strong>" . number_format($price, 2) . "</strong></td>";
        $total += $price;
        $totalItems += $qty;
    }
    $totalTotal = $total;
    $retval .= "</tr>\n";
    $retval .= "<tr class=\"total\"><td></td><td></td>" . "<td style=\"text-align:right;\">ITEMS TOTAL</td>" . "<td style=\"text-align:right\">\$ <strong>" . number_format($total, 2) . "</strong></td></tr>\n";
    // note that the shipping calculation is very simple here -
    // and should really be based upon some kind of "weight" or something
    // Note, by-the-way, that this doesn't SET the shipping in the data record
    // that is only "locked in" when the order is invoiced.
    $shipping = $order["ShippingFee"];
    if ($shipping === null) {
        $shipping = $totalItems * $SETTINGS["ShippingPerChapR"];
    }
    $retval .= "<tr><td></td><td></td><td style=\"text-align:right;\">Shipping";
    if ($order["ShippingFee"] == null) {
        $retval .= " (default)";
    }
    $retval .= "</td><td style=\"text-align:right\"><strong>" . number_format($shipping, 2) . "</strong></td></tr>\n";
    $totalTotal += $shipping;
    if ($order["IsExpedited"]) {
        // expedite is much like shipping, although it is just a static figure
        // it is "locked in" when the order is invoiced.
        $expedite = $order["ExpediteFee"];
        if ($expedite === null) {
            $expedite = $SETTINGS["ExpediteFeeDefault"];
        }
        $retval .= "<tr><td></td><td></td><td style=\"text-align:right;\">Expedite Fee";
        if ($order["ExpediteFee"] == null) {
            $retval .= " (default)";
        }
        $retval .= "</td><td style=\"text-align:right\"><strong>" . number_format($expedite, 2) . "</strong></td></tr>\n";
        $totalTotal += $expedite;
    }
    if ($order["Discount"]) {
        // discounts only show up if they exist
        $discount = $order["Discount"];
        $retval .= "<tr><td></td><td></td><td style=\"text-align:right;\">Discount";
        $retval .= "</td><td style=\"text-align:right\"><strong>-" . number_format($discount, 2) . "</strong></td></tr>\n";
        $totalTotal -= $discount;
    }
    $retval .= "<tr><td></td><td></td><td style=\"text-align:right;\"><strong>TOTAL</strong></td><td style=\"text-align:right\">\$ <strong>" . number_format($totalTotal, 2) . "</strong></td></tr>\n";
    $retval .= "<tr><td>&nbsp;</td></tr>";
    $retval .= "<tr><td style=\"vertical-align:top;text-align:right;\">Notes:</td>\n";
    $retval .= "<td colspan=3><strong>" . htmlNotesFormat($order["CustomerONotes"]) . "</strong></td></tr>\n";
    $retval .= "<tr><td style=\"vertical-align:top;text-align:right;\">Admin Notes:</td>\n";
    $retval .= "<td colspan=3><strong>" . htmlNotesFormat($order["AdminONotes"]) . "</strong></td></tr>\n";
    $retval .= "<tr><td>&nbsp;</td></tr>";
    $retval .= "<tr><td colspan=\"4\" style=\"text-align:right\">";
    if ($showEditButton) {
        $retval .= "<a href=\"{$edit}&OID={$oid}\">Edit</a>";
    } else {
        $retval .= "<em>Payment requested, editing not allowed</em>";
    }
    $retval .= "</td></tr>";
    return $retval;
}
function getItems($oid)
{
    $items = dbGetItems($oid);
    $packages = dbGetPackages();
    $pieces = dbGetPieces();
    $retarray = array();
    foreach ($items as $item) {
        $row = array();
        $row["Quantity"] = $item["Quantity"];
        $row["Name"] = findPackageName($item["PKID"], $packages);
        $row["Personality"] = findPieceName($item["Personality"], $pieces);
        $row["Price"] = findPackagePrice($item["PKID"], $packages);
        $retarray[] = $row;
    }
    return $retarray;
}