function customer_edit($event, $step)
{
    global $statuses, $comments_disabled_after, $step, $txp_user;
    pagetop("Customer", $message);
    //CSS FOR CUSTOMER EDIT
    //==================================
    echo n . '<style type="text/css">' . n . '.customerEdit legend{' . n . '	font-size: 11px;' . n . '	font-weight: bold;' . n . '}' . n . '.customerEdit label{' . n . '	float: left;' . n . '	width: 100px;' . n . '	text-align: right;' . n . '	padding: 5px;' . n . '}' . n . '.customerEdit br{' . n . '	clear: both;' . n . '}' . n . '.customerEdit input{' . n . '	margin: 5px 0 5px 0;' . n . '	font-size: 11px;' . n . '}' . n . '</style>';
    doJS();
    if (isset($_REQUEST['user_id'])) {
        $user_id = $_REQUEST['user_id'];
        $customer = safe_row("*", "txp_users", "user_id = {$user_id}");
        extract($customer);
        $step = "update_customer";
    } else {
        $step = "save_customer";
    }
    //DELETE CUSTOMER FORM
    //====================================
    echo n . n . '<form name="product" method="post" action="index.php" enctype="multipart/form-data" id="customerDelete">';
    echo n . "<input type='hidden' name='user_id' value='{$user_id}'/>" . eInput('customers') . sInput('delete_customer');
    echo n . n . '</form>';
    echo n . n . '<form name="product" method="post" action="index.php" enctype="multipart/form-data">';
    echo hInput('user_id', $user_id) . eInput('customers') . sInput($step) . '<input type="hidden" name="view" />' . startTable('edit') . '<tr>' . n;
    //if ($view == 'text')
    //{
    //-- markup help --------------
    echo '<td id="article-main">' . n;
    echo "<fieldset class='customerEdit'>" . n . "<legend>Customer Details</legend>" . n . '<label for="name">Username</label>' . n . '<input id="name" name="name" value="' . $name . '"/>' . br . n . '<label for="email">Email</label>' . n . '<input id="email" name="email" value="' . $email . '"/>' . n . '<label for="billing_firstname">First name</label>' . n . '<input type="text" id="billing_firstname" name="billing_firstname" value="' . $billing_firstname . '"/>' . br . n . '<label for="billing_lastname">Last name</label>' . n . '<input type="text" id="billing_lastname" name="billing_lastname" value="' . $billing_lastname . '"/>' . br . n . '</fieldset>';
    echo "<fieldset class='customerEdit'>" . n . "<legend>Billing Information</legend>" . n . '<label for="billing_company">Company</label>' . n . '<input type="text" id="billing_company" name="billing_company" value="' . $billing_company . '"/>' . br . n . '<label for="billing_address1">Address 1</label>' . n . '<input type="text" id="billing_address1" name="billing_address1" value="' . $billing_address1 . '"/>' . br . n . '<label for="billing_address2">Address 2</label>' . n . '<input type="text" id="billing_address2" name="billing_address2" value="' . $billing_address2 . '"/>' . br . n . '<label for="billing_city">City</label>' . n . '<input type="text" id="billing_city" name="billing_city" value="' . $billing_city . '"/>' . br . n . '<label for="billing_state">State</label>' . n . '<input type="text" id="billing_state" name="billing_state" value="' . $billing_state . '"/>' . br . n . '<label for="billing_zip">Zip/Postal Code</label>' . n . '<input type="text" id="billing_zip" name="billing_zip" value="' . $billing_zip . '"/>' . br . n . '<label for="billing_country">Country</label>' . n . '<input type="text" id="billing_country" name="billing_country" value="' . $billing_country . '"/>' . br . n . '<label for="billing_fax">Fax Number</label>' . n . '<input type="text" id="billing_fax" name="billing_fax" value="' . $billing_fax . '"/>' . br . n . '<label for="billing_phone">Phone Number</label>' . n . '<input type="text" id="billing_phone" name="billing_phone" value="' . $billing_phone . '"/>' . br . n . '<label for="shipping_same">&nbsp;</label>';
    if ($shipping_same_as_billing == "1") {
        $checked = "checked='checked'";
    } else {
        $checked = '';
    }
    echo n . '<input type="checkbox" id="shipping_same" name="shipping_same_as_billing" value="' . $shipping_same_as_billing . '" ' . $checked . ' onclick="toggleShipping()"/> Shipping info same as billing' . br . n . '</fieldset>';
    doJS();
    if ($shipping_same_as_billing == "1") {
        $display = "none";
    } else {
        $display = "block";
    }
    echo "<fieldset class='customerEdit' id='shippingInfo' style='display: {$display}'>" . n . "<legend>Shipping Information</legend>" . n . '<label for="shipping_company">Company</label>' . n . '<input type="text" id="shipping_company" name="shipping_company" value="' . $shipping_company . '"/>' . br . n . '<label for="shipping_firstname">First name</label>' . n . '<input type="text" id="shipping_firstname" name="shipping_firstname" value="' . $shipping_firstname . '"/>' . br . n . '<label for="shipping_lastname">Last name</label>' . n . '<input type="text" id="shipping_lastname" name="shipping_lastname" value="' . $shipping_lastname . '"/>' . br . n . '<label for="shipping_address1">Address 1</label>' . n . '<input type="text" id="shipping_address1" name="shipping_address1" value="' . $shipping_address1 . '"/>' . br . n . '<label for="shipping_address2">Address 2</label>' . n . '<input type="text" id="shipping_address2" name="shipping_address2" value="' . $shipping_address2 . '"/>' . br . n . '<label for="shipping_city">City</label>' . n . '<input type="text" id="shipping_city" name="shipping_city" value="' . $shipping_city . '"/>' . br . n . '<label for="shipping_state">State</label>' . n . '<input type="text" id="shipping_state" name="shipping_state" value="' . $shipping_state . '"/>' . br . n . '<label for="shipping_zip">Zip/Postal Code</label>' . n . '<input type="text" id="shipping_zip" name="shipping_zip" value="' . $shipping_zip . '"/>' . br . n . '<label for="shipping_country">Country</label>' . n . '<input type="text" id="shipping_country" name="shipping_country" value="' . $shipping_country . '"/>' . br . n . '<label for="shipping_fax">Fax Number</label>' . n . '<input type="text" id="shipping_fax" name="shipping_fax" value="' . $shipping_fax . '"/>' . br . n . '<label for="shipping_phone">Phone Number</label>' . n . '<input type="text" id="shipping_phone" name="shipping_phone" value="' . $shipping_phone . '"/>' . br . n . '</fieldset>';
    echo '</td>';
    echo '<td id="article-col-2" style="padding-top: 13px;">';
    //start article-col-2
    echo '<a href="?event=customers&step=edit_customer" class="navlink">Add new customer</a>';
    if (isset($user_id)) {
        echo n . br . br . '<a href="javascript:deleteCustomer()" style="color:#990000">Delete this customer</a>' . br . br;
    } else {
        echo br . br;
    }
    //-- publish button --------------
    echo has_privs('article.publish') ? fInput('submit', 'publish', gTxt('save'), "publish", '', '', '', 4) : fInput('submit', 'publish', gTxt('save'), "publish", '', '', '', 4);
    if ($user_id) {
        $orders = safe_rows("*", "orders", "user_id = {$user_id} ORDER BY last_updated DESC");
        if (count($orders) > 0) {
            echo br . br . "<fieldset>" . n . '<legend>Order History</legend>' . n . '<ul class="plain-list">';
            foreach ($orders as $order) {
                echo n . '<li><a href="?event=orders&step=edit_order&id=' . $order['id'] . '">ORDER #' . $order['id'] . '</a></li>';
            }
            n . '</ul>';
            echo '</fieldset>';
        }
    }
    echo '</td></tr></table></form>';
}
function orders_edit($event, $step, $message = '')
{
    global $statuses, $comments_disabled_after, $step, $txp_user;
    pagetop("Order Edit", $message);
    extract(doSlash($_REQUEST));
    $sql = "\r\n\t\t\tSELECT *\r\n\t\t\tFROM orders\r\n\t\t\tJOIN txp_users AS users ON users.user_id = orders.user_id\r\n\t\t\tWHERE orders.id = {$id}";
    $order = safe_query($sql);
    $order = mysql_fetch_assoc($order);
    extract($order);
    $sql = "\r\n\t\t\tSELECT *\r\n\t\t\tFROM textpattern AS txp\r\n\t\t\tJOIN orders_articles AS oa\r\n\t\t\tON oa.article_id = txp.ID\r\n\t\t\tWHERE oa.order_id = {$id}";
    $products = safe_query($sql);
    //echo "<h4 style='text-align:center;'>Coming soon</h4>";
    //CSS FOR CUSTOMER EDIT
    //==================================
    echo n . '<style type="text/css">' . n . '.customerEdit legend{' . n . '	font-size: 11px;' . n . '	font-weight: bold;' . n . '}' . n . '.customerEdit label{' . n . '	float: left;' . n . '	width: 100px;' . n . '	text-align: right;' . n . '	padding: 8px 5px 5px 5px;' . n . '}' . n . '.customerEdit br{' . n . '	clear: both;' . n . '}' . n . '.customerEdit p.desc{' . n . '	padding: 0px; font-size: 10px; font-style:italic; padding-left: 110px; color:gray;' . n . '}' . n . '.customerEdit input, select{' . n . '	margin: 5px 0 5px 0;' . n . '	font-size: 11px;' . n . '}' . n . '.data {' . n . ' width:100%;' . n . '}' . n . '.data th {' . n . ' text-align:left; color:gray; font-size: 10px;' . n . '}' . n . '</style>';
    doJS();
    $step = 'update_order';
    echo n . n . '<form name="longform" method="post" action="index.php">';
    echo hInput('id', $id) . eInput('orders') . sInput($step) . '<input type="hidden" name="view" />' . startTable('edit') . '<tr>' . n;
    //if ($view == 'text')
    //{
    //-- markup help --------------
    echo '<td id="article-main">' . n;
    echo "<fieldset class='customerEdit'>" . n . "<legend>Products</legend>";
    echo n . "<table class='data'><tr>";
    echo n . "<th>Product</th><th>Price</th><th>Quantity</th></tr>";
    while ($product = mysql_fetch_assoc($products)) {
        extract($product);
        echo "<tr><td>{$Title}</td><td>\${$custom_1}</td><td>{$quantity}</td></tr>";
    }
    echo n . "</tr></table>";
    echo "<hr/>";
    echo n . "<table><tr><td><strong>Subtotal:</strong></td><td> \${$subtotal}</td></tr>" . n;
    echo n . "<tr><td><strong>Tax:</strong></td><td> \${$tax}</td></tr>" . n;
    echo n . "<tr><td><strong>Shipping &amp; Handling:</strong></td><td> \${$shipping_handling}</td></tr>" . n;
    if (!empty($discount)) {
        echo n . "<tr><td><strong>Discount:</strong></td><td> \${$discount}</td></tr>" . n;
    }
    echo n . "<tr><td><strong>Total:</strong> </td><td><strong>\${$total}</strong></td></tr></table>" . n;
    echo n . '</fieldset>';
    echo "<fieldset class='customerEdit'>";
    echo "<table><tr>";
    if (isset($shipping_same_as_billing) && $shipping_same_as_billing == "1") {
        echo '<td><strong>Ship to </strong>[<a href="?event=customers&step=edit_customer&user_id=' . $user_id . '">edit</a>]' . br . $RealName . br . $billing_address1 . br;
        if (isset($billing_address2) && !empty($billing_address2)) {
            echo $billing_address2 . br;
        }
        echo $billing_city . ', ';
        echo $billing_state . ' ';
        echo $billing_zip . br;
        echo $billing_country . '</td>';
    } else {
        echo '<td>' . $shipping_address1 . br;
        if (isset($shipping_address2)) {
            echo $shipping_address2 . br;
        }
        echo $shipping_city . br;
        echo $shipping_state . br;
        echo $shipping_zip . br;
        echo $shipping_country . '</td>';
    }
    echo n . "<td style='padding-left:10px;'><strong>Bill to </strong>[<a href=\"?event=customers&step=edit_customer&user_id={$user_id}\">edit</a>]" . br;
    echo $RealName . br . $billing_address1 . br;
    if (isset($billing_address2) && !empty($billing_address2)) {
        echo $billing_address2 . br;
    }
    echo $billing_city . ', ';
    echo $billing_state . ' ';
    echo $billing_zip . br;
    echo $billing_country . '</td>';
    echo "</tr></table>" . br;
    echo n . "<p style='padding-left:3px;'><strong>Payment info</strong>" . br;
    echo n . 'Method: ' . $payment_method . br . n . 'Transaction ID: ' . $transaction_id . br . '</p>';
    echo n . '</fieldset>';
    echo "<fieldset class='customerEdit'>" . n . "<legend>Order Update</legend>";
    if (isset($ship_date) && !empty($ship_date)) {
        $ship_date = strtotime($ship_date);
        $ship_year = date("Y", $ship_date);
        $ship_month = date("m", $ship_date);
        $ship_day = date("d", $ship_date);
    } else {
        $ship_year = date("Y");
        $ship_month = date("m");
        $ship_day = date("d");
    }
    if (isset($order_status)) {
        if ($order_status == "pending") {
            $pending_selected = "selected='true'";
        } else {
            if ($order_status == "approved") {
                $approved_selected = "selected='true'";
            } else {
                if ($order_status == "shipped") {
                    $shipped_selected = "selected='true'";
                } else {
                    if ($order_status == "declined") {
                        $declined_selected = "selected='true'";
                    } else {
                        if ($order_status == "void") {
                            $void_selected = "selected='true'";
                        }
                    }
                }
            }
        }
    }
    echo n . '<label for="order_status">Status</label>' . n . '<select id="order_status" name="order_status">' . n . '	<option value="pending" ' . $pending_selected . '>Pending</option>' . n . '	<option value="approved" ' . $approved_selected . '>Approved</option>' . n . '	<option value="shipped" ' . $shipped_selected . '>Shipped</option>' . n . '	<option value="declined" ' . $declined_selected . '>Declined</option>' . n . '	<option value="void" ' . $void_selected . '>Void</option>' . n . '</select>' . br . n . '<label for="ship_year">Ship Date</label>' . n . listbox_year('ship_year', '2004', date('Y'), $ship_year) . ' ' . get_html_select_month("ship_month", $ship_month) . n . '<input type="text" name="ship_day" value="' . $ship_day . '" style="width: 20px;"/> &nbsp; ' . br . n . '<label for="ship_now">&nbsp;</label><div style="float:left; padding-top: 5px; padding-right:4px;">Shipped today</div> <input id="ship_now" type="checkbox" name="ship_now" value="' . date('Y-m-d') . '" style="float:none;"/>' . br . n . '<label for="ship_method">Ship method</label>' . n . '<input type="text" name="ship_method" id="ship_method" value="' . $ship_method . '"/> &nbsp; ' . br . n . '<p class="desc">Example: USPS Priority, FedEx, etc.</p>' . n . '<label for="tracking_number">Tracking number</label>' . n . '<input type="text" name="tracking_number" id="tracking_number" value="' . $tracking_number . '"/> &nbsp; ' . br . n . '<label for="note">Note</label>' . n . '<textarea name="note" id="note"/>' . $note . '</textarea>' . br . br;
    echo n . '</fieldset>';
    echo '</td>';
    echo '<td id="article-col-2" style="padding-top: 13px;">';
    //start article-col-2
    echo '<a href="?event=customers&step=edit_customer" class="navlink">Print packing slip</a>' . br . br;
    //-- publish button --------------
    echo has_privs('article.publish') ? fInput('submit', 'publish', gTxt('update'), "publish", '', '', '', 4) : fInput('submit', 'publish', gTxt('update'), "publish", '', '', '', 4);
    if ($user_id) {
        $orders = safe_rows("*", "orders", "user_id = {$user_id} AND id != {$id} ORDER BY last_updated DESC");
        if (count($orders) > 0) {
            echo br . br . "<fieldset>" . n . '<legend>Other Orders by ' . $RealName . '</legend>' . n . '<ul class="plain-list">';
            foreach ($orders as $order) {
                echo n . '<li><a href="?event=orders&step=edit_order&id=' . $order['id'] . '">ORDER #' . $order['id'] . '</a> <em style="font-size:10px;">' . date('M j y', strtotime($order['date_created'])) . '</em></li>';
            }
            n . '</ul>';
            echo '</fieldset>';
        }
    }
    echo '</td></tr></table></form>';
}
function settings_edit($event, $step, $message = '', $show_panel = '')
{
    global $txp_user, $vars, $txpcfg, $prefs, $general_settings;
    extract($prefs);
    $general_settings = safe_row("*", "store_settings", "1");
    pagetop("Store Settings", $message);
    //JS INCLUDES
    //==================================
    //print_r($prefs);
    echo '<script type="text/javascript" src="http://' . $siteurl . '/jquery.js"></script>';
    echo '<script type="text/javascript" src="http://' . $siteurl . '/jquery.js"></script>';
    $step = "update";
    //CSS FOR SETTINGS EDIT
    //==================================
    echo n . '<style type="text/css">' . n . 'td#article-main {' . n . '	width: 500px;' . n . '}' . n . '.customerEdit legend{' . n . '	font-size: 11px;' . n . '	font-weight: bold;' . n . '}' . n . '.customerEdit label{' . n . '	float: left;' . n . '	width: 100px;' . n . '	text-align: right;' . n . '	padding: 8px 5px 5px 5px;' . n . '}' . n . '.customerEdit br{' . n . '	clear: both;' . n . '}' . n . '.customerEdit p.desc{' . n . '	padding: 0px; font-size: 10px; font-style:italic; padding-left: 110px; color:gray;' . n . '}' . n . '.customerEdit input, select{' . n . '	margin: 5px 0 5px 0;' . n . '	font-size: 11px;' . n . '}' . n . '#subNav {' . n . ' list-style:none;' . n . ' padding-left:0px; margin-left:0;' . n . ' background-color:#FFFFCC;' . n . ' border-bottom:1px solid #DDDDDD;' . n . ' border-left:1px solid #F1F1F1;' . n . ' border-right:1px solid #DDDDDD;' . n . '}' . n . '#subNav li {' . n . ' list-style:none;' . n . ' padding:5px 0 5px 0; margin-left:0;' . n . '}' . n . '#subNav li.selected {' . n . ' background-color:white;' . n . '}' . n . '#subNav li.selected a{' . n . ' color:gray;' . n . '}' . n . '#subNav li a{' . n . ' text-align:left;font-size: 10px; font-weight:bold; color:#996633; width:100%; padding:5px;' . n . '}' . n . '.rateList th{ background-color: #EEEEEE; text-align:left;}' . n . '.rateList td, th{' . n . ' padding:3px;' . n . '}' . n . '.rateList {' . n . ' border-style:solid; border-width:0 1px 1px 1px; border-color:#DDDDDD; width:100%;' . n . '}' . n . '.zoneName{' . n . ' font-weight:normal; font-size: 110%; border-style:solid; border-width:0 0 1px 0; border-color:#DDDDDD; padding-bottom:5px; color:gray; margin-bottom:0;' . n . '}' . n . '</style>';
    //JS
    //==================================
    doJS();
    echo startTable('edit') . '<tr>' . n;
    //if ($view == 'text')
    //{
    //-- markup help --------------
    echo '<td id="article-col-1">' . n . '<ul id="subNav">' . n . '<li class="selected" id="generalSettingsTab" onclick="showTab(\'generalSettingsTab\', \'generalSettings\')"><a href="#" >General Settings</a></li>' . n . '<li id="shippingZonesTab"><a href="#" onclick="showTab(\'shippingZonesTab\', \'shippingZones\')">Countries/Regions</a></li>' . n . '<li id="shippingRatesTab"><a href="#" onclick="showTab(\'shippingRatesTab\', \'shippingRates\')">Shipping Rates</a></li>' . n . '<li id="noticesTab"><a href="#" onclick="showTab(\'noticesTab\', \'notices\')">Notices</a></li>' . n . '</ul></td>';
    echo '<td id="article-main">' . n;
    //General Settings
    //========================================
    echo "<div id='generalSettings' class='settingsRegion'>";
    extract($general_settings);
    echo n . n . '<form name="longform" method="post" action="index.php">';
    echo hInput('id', $id) . eInput('settings') . sInput($step) . '<input type="hidden" name="settings_update" value="general" />' . n . "<fieldset class='customerEdit'>" . n . "<legend>General Settings</legend>";
    echo '<label for="owner_email">Event notify email</label>' . n . '<input type="text" class="text" name="owner_email" id="owner_email" value="' . $owner_email . '"/>' . br . n . '<p class="desc">Email will be used to notify of new orders, low inventory, etc.</p>' . n . '<label for="store_address">Street Address</label>' . n . '<input type="text" class="text" name="store_address" id="store_address" value="' . $store_address . '"/>' . br . n . '<label for="store_city">City</label>' . n . '<input type="text" class="text" name="store_city" id="store_city"  value="' . $store_city . '"/>' . br . n . '<label for="store_state">State</label>' . n . '<input type="text" class="text" name="store_state" id="store_state" value="' . $store_state . '"/>' . br . n . '<label for="store_zip">Zip</label>' . n . '<input type="text" class="text" name="store_zip" id="store_zip" value="' . $store_zip . '"/>' . br . n . '<label for="store_country">Country</label>' . n . build_list("store_country", "countries", "id", "name", $store_country, "1", false, "order by name asc") . n . '</fieldset>';
    if ($unit_system == "metric") {
        $metric_selected = 'selected="true"';
    } else {
        $imperial_selected = 'selected="true"';
    }
    echo "<fieldset class='customerEdit'>" . n . "<legend>Standards &amp Formats</legend>" . n . '<label for="unit_system">Unit system</label>' . n . '<select name="unit_system" id="unit_system"/>' . n . '<option value="imperial" ' . $imperial_selected . '>Imperial System (pound, inch)</option>' . n . '<option value="metric" ' . $metric_selected . '>Metric System (kilogram, centimeter)</option></select>' . n . '<label for="store_currency">Store Currency</label>' . n . build_list("store_currency", "currencies", "currency_code", "currency_name", $store_currency, "1") . br;
    echo n . '</fieldset>';
    if ($hide_inventory_when_depleted == 1) {
        $inventory_hide_selected = "selected='true'";
    } else {
        $inventory_show_selected = "selected='true'";
        $display_message = "display:block;";
    }
    if (isset($inventory_management_on) && $inventory_management_on == 1) {
        $inventory_management_on_checked = "checked='checked'";
    }
    echo "<fieldset class='customerEdit'>" . n . "<legend>Inventory Settings</legend>" . n . '<p class="desc">These options only apply to products with inventory management turned on</p>' . n . '<label for="hide_inventory_when_depleted">&nbsp;</label>' . n . '<select name="hide_inventory_when_depleted" id="hide_inventory_when_depleted" onchange="if(this.value == \'0\'){$(\'inventory_message\').style.display = \'block\';}else{$(\'inventory_message\').style.display = \'none\'}"/>' . n . '<option value="1" ' . $inventory_hide_selected . '>Hide Inventory when depleted</option>' . n . '<option value="0" ' . $inventory_show_selected . '">Show message when inventory depleted</option></select>' . br . n . '<div style="' . $display_message . '" id="inventory_message">' . n . '<label for="depleted_inventory_message">Depleted Inventory Message</label>' . n . '<textarea name="depleted_inventory_message" id="depleted_inventory_message">' . $depleted_inventory_message . '</textarea></div>' . br . n . '<label for="inventory_management_on">&nbsp;</label>' . n . '<input name="inventory_management_on" id="inventory_management_on" type="checkbox" value="1" style="float:left; margin-right:5px;" ' . $inventory_management_on_checked . '/> <p style="padding-top:3px;">Send low inventory warning to store owner</p>' . br;
    echo n . '</fieldset>' . n . '<div class="submit">';
    //-- publish button --------------
    echo has_privs('article.publish') ? fInput('submit', 'publish', gTxt('update'), "publish", '', '', '', 4) : fInput('submit', 'publish', gTxt('update'), "publish", '', '', '', 4);
    echo n . '</div><!--/.submit--></form></div><!--/generalSettings-->';
    //Shipping Zones
    //========================================
    echo "<div id='shippingZones' style='display:none;' class='settingsRegion'><fieldset class='customerEdit'>" . n . "<legend>Support a new country or region</legend>";
    $zones = safe_rows("*", "shipping_zones", "1");
    $countryCriteria = '1 ';
    foreach ($zones as $zone) {
        $countryCriteria .= " AND id != " . $zone['country_id'] . " ";
    }
    echo n . n . '<form name="longform" method="post" action="index.php">' . eInput('settings') . sInput($step);
    echo '<input type="hidden" name="settings_update" value="add_zone"/>';
    echo '<label for="country_id">Country</label>' . n . build_list("country_id", "countries", "id", "name", " ", $countryCriteria, true, "order by name asc") . n . '</select>' . br;
    /*echo '<label for="shipping_rate_id">Shipping rate</label>'.
    		 n.build_list("shipping_rate_id", "shipping_rates", "id", "title", " ", true, "1").
    		 n.'</select>'.br;
      echo n.'<p class="desc">You can add more shipping rates to your regions in the Shipping Rates section.</p>';*/
    if (empty($tax_rate)) {
        $tax_rate = "0.0";
    }
    echo '<label for="tax_rate">Tax Rate</label>' . n . '<input type="text" name="tax_rate" id="tax_rate" value="' . $tax_rate . '" style="width: 20px">' . br;
    echo '<label>&nbsp;</label>';
    echo fInput('submit', 'Add', 'Add', "smallerbox", '', '', '', 4);
    echo '</form>';
    echo n . '</fieldset>';
    echo n . "<h2>Your Regions</h2>";
    echo '<table cellpadding="0" cellspacing="0" border="0" class="rateList">';
    echo n . '<tr><th>Country</th><th colspan="2">Tax Rate</th></tr>';
    foreach ($zones as $zone) {
        extract($zone);
        echo n . n . '<form name="longform" id="zone_' . $id . '_form" method="post" action="index.php">' . hInput('id', $id) . eInput('settings') . sInput($step);
        echo '<input type="hidden" name="settings_update" id="zone_' . $id . '_action" value="update_zone"/>';
        echo '<tr><td>' . $name . '</td>' . n . '<td><input type="text" name="tax_rate" value="' . $tax_rate . '"/></td>' . n . '<td style="text-align:right;"><input type="submit" value="update" class="smallerbox"/> or <a href="javascript:deleteZone(\'' . $id . '\')">Delete</a></td>' . n . '</tr></form>';
    }
    echo "</table>";
    echo n . '</div><!--/shippingZones-->';
    //Shipping Rates
    //========================================
    echo "<div id='shippingRates' style='display:none;' class='settingsRegion'>" . n . "<h2>Shipping Rates</h2>";
    $zones = safe_rows("*", "shipping_zones", "1");
    foreach ($zones as $zone) {
        extract($zone);
        echo '<h4 class="zoneName">' . $name . ' <small>[<a href="#" onclick="$(\'#addNewShippingRate_' . $id . '\').show();">Add new shipping rate</a>]</small></h4>';
        $sql = "select *, shipping_rates.id as shipping_rate_id from shipping_rates\r\n\t\t\t\t\t\t\t\t\t\tJOIN zones_rates\r\n\t\t\t\t\t\t\t\t\t\tON zones_rates.shipping_rate_id = shipping_rates.id\r\n\t\t\t\t\t\t\t\t\t\tJOIN shipping_zones\r\n\t\t\t\t\t\t\t\t\t\tON shipping_zones.id = zones_rates.shipping_zone_id\r\n\t\t\t\t\t\t\t\t\t\tWHERE zones_rates.shipping_zone_id = {$id}";
        $rates = safe_query($sql);
        echo '<table cellpadding="0" cellspacing="0" border="0" class="rateList">';
        if (mysql_num_rows($rates) > 0) {
            echo n . '<tr><th style="width:150px;">Label</th><th style="width:100px;">Amount</th><th style="width:250px;">Weight Range</th></tr>';
        }
        $background = "#FFFFFF";
        while ($rate = mysql_fetch_assoc($rates)) {
            echo '<tr><td colspan="3" style="background-color: ' . $background . '">';
            echo n . n . '<form name="longform" method="post" action="index.php" id="rates_' . $rate['shipping_rate_id'] . '_form">' . hInput('id', $id) . eInput('settings') . sInput($step);
            echo '<input type="hidden" name="settings_update" id="rates_' . $rate['shipping_rate_id'] . '_action" value="update_rate"/>';
            echo '<input type="hidden" name="shipping_rate_id" value="' . $rate['shipping_rate_id'] . '"/>';
            // JR Chew - Now settings decipher between pounds and kilograms
            if ($general_settings['unit_system'] == 'metric') {
                $weight = kg;
            } else {
                $weight = lbs;
            }
            echo '<table cellpadding="0" cellspacing="0" border="0"><tr><td style="width:150px;"><input type="text" value="' . $rate['title'] . '" name="title"/></td>' . n . '<td valign="center" style="width:100px;padding:0;"><input type="text" value="' . number_format($rate['rate'], 2) . '" name="rate" style="width:25px;"/> ' . $general_settings['store_currency'] . '</td>' . n . '<td valign="center" style="width:130px;padding:0;"><input type="text" name="start_weight" value="' . $rate['start_weight'] . '" style="width:25px;"/> ' . $weight . ' - <input type="text" name="end_weight" value="' . $rate['end_weight'] . '" style="width:25px;"/>  ' . $weight . ' </td>' . n . '<td style="text-align:right; width:120px;padding:0;"><input type="submit" value="update" class="smallerbox"/> or <a href="javascript:deleteRate(\'' . $rate['shipping_rate_id'] . '\')">delete</a></td></tr></table>';
            echo '</form>';
            echo "</td></tr>";
            if ($background == "#F6F6F6") {
                $background = "#FFFFFF";
            } else {
                $background = "#F6F6F6";
            }
        }
        echo '</table>';
        echo br . n . n . '<form name="longform" method="post" action="index.php">' . hInput('id', $id) . eInput('settings') . sInput($step);
        echo '<input type="hidden" name="settings_update" value="add_rate"/>';
        echo '<table id="addNewShippingRate_' . $id . '" style="display: none; border-width:1px 1px 1px 1px;" cellpadding="0" cellspacing="0" border="0" class="rateList">';
        echo n . '<tr><th style="width:150px;">Label</th><th style="width:100px;">Amount</th><th style="width:250px;" colspan="2">Weight Range</th></tr>';
        echo '<tr style="background-color: ' . $background . '"><td><input type="text" value="" name="title" style="width:100%;"/></td>' . n . '<td valign="center"><input type="text" value="" name="rate" style="width:25px;"/> ' . $general_settings['store_currency'] . '</td>' . n . '<td valign="center"><input type="text" name="start_weight" value="0" style="width:25px;"/> lbs - <input type="text" name="end_weight" value="0" style="width:25px;"/> lbs</td>' . n . '<td style="text-align:right;"><input type="submit" value="save" class="smallerbox"/> <small><a href="javascript:void(0)" onclick="$(\'addNewShippingRate_' . $id . '\').style.display = \'none\'">cancel</a></small></td></tr>';
        echo '</table></form>';
    }
    echo n . '</div><!--/shippingRates-->';
    //Notices Management
    //========================================
    echo "<div id='notices' style='display:none;' class='settingsRegion'>" . n . "<h2>Notices</h2>";
    echo n . n . '<form name="longform" method="post" action="index.php">' . hInput('id', $id) . eInput('settings') . sInput($step);
    echo '<input type="hidden" name="settings_update" value="notices" />';
    echo n . '<fieldset class="customerEdit">' . n . '<legend>Customer Notifications</legend>';
    echo n . '<label for="order_confirmation_form" style="width:180px;">Order Confirmation Form</label>' . n . build_list("order_confirmation_form", "txp_form", "name", "name", $order_confirmation_form, "type = 'article' and name != 'default'", true, "order by name asc");
    echo '<p class="desc">TXP will send the customer a copy of this form when they complete a purchase</p>';
    echo n . '</fieldset>';
    echo n . '<fieldset class="customerEdit">' . n . '<legend>Your Notifications</legend>';
    echo n . '<label for="new_order_use_form" style="width:180px;">New Order Notification Form</label>' . n . build_list("new_order_notification_form", "txp_form", "name", "name", $new_order_notification_form, "type = 'article' and name != 'default'", true, "order by name asc");
    echo '<p class="desc">This is the email that TXP will send to you when a customer makes a new purchase</p>';
    echo n . '</fieldset>';
    echo fInput('submit', 'publish', gTxt('update'), "publish", '', '', '', 4);
    echo n . '</form></div><!--/notices-->';
    if (isset($show_panel)) {
        if ($show_panel == "zones") {
            echo '<script type="text/javascript">' . n . '<!--' . n . 'showTab("shippingZonesTab", "shippingZones");' . n . '//-->' . n . '</script>';
        } else {
            if ($show_panel == "rates") {
                echo '<script type="text/javascript">' . n . '<!--' . n . 'showTab("shippingRatesTab", "shippingRates");' . n . '//-->' . n . '</script>';
            } else {
                if ($show_panel == "notices") {
                    echo '<script type="text/javascript">' . n . '<!--' . n . 'showTab("noticesTab", "notices");' . n . '//-->' . n . '</script>';
                }
            }
        }
    }
    echo '</td>';
    echo '</tr></table>';
}
function product_edit($event, $step, $message = '')
{
    global $vars, $txp_user, $comments_disabled_after, $txpcfg, $prefs, $general_settings;
    extract($prefs);
    define("IMPATH", $path_to_site . '/' . $img_dir . '/');
    extract(gpsa(array('view', 'from_view', 'step')));
    if (!empty($GLOBALS['ID'])) {
        // newly-saved article
        $ID = $GLOBALS['ID'];
        $step = 'edit';
    } else {
        $ID = gps('ID');
    }
    //GET ARTICLE DATA FOR 'EDIT' & 'SAVE' STEP
    //==================================
    if ($step == "edit" || $step == "save" && !empty($ID)) {
        $pull = true;
        //-- it's an existing article - off we go to the db
        $ID = assert_int($ID);
        $rs = safe_row("*, unix_timestamp(Posted) as sPosted,\r\n\t\t\t\tunix_timestamp(LastMod) as sLastMod", "textpattern", "ID={$ID}");
        extract($rs);
        if ($AnnotateInvite != $comments_default_invite) {
            $AnnotateInvite = $AnnotateInvite;
        } else {
            $AnnotateInvite = $comments_default_invite;
        }
        $custom_fields = safe_rows("*", "product_custom_fields", "articleID = {$ID} ORDER BY id DESC");
        $step = "save";
    } else {
        if (empty($step)) {
            $step = "create";
        }
    }
    $textile = new Textile();
    $textile_body = $use_textile;
    $textile_excerpt = $use_textile;
    $page_title = "Add a new product";
    $message = "";
    pagetop($page_title, $message);
    if (count($custom_fields) > 0) {
        $startFieldNum = $custom_fields[0]['id'] + 1;
    } else {
        $startFieldNum = 0;
    }
    echo "<script type='text/javascript'>var fieldNum = {$startFieldNum}</script>";
    //JS INCLUDES
    //==================================
    //print_r($prefs);
    echo '<script type="text/javascript" src="http://' . $siteurl . '/jquery.js"></script>';
    echo '<script type="text/javascript" src="http://' . $siteurl . '/jquery.js"></script>';
    //CSS FOR PRODUCT DISPLAY
    //==================================
    echo n . '<style type="text/css">' . n . 'h4.productHeading{' . n . '	color:gray;' . n . '	margin-bottom:0px;' . n . '}' . n . 'h4.productHeading span{' . n . '	float:right;' . n . '	font-size:9px;' . n . '	font-style:italic;' . n . '	font-weight:normal;' . n . '}' . n . '.product-options-sub{' . n . '	padding: 10px;' . n . '}' . n . '.product-options-sub em{' . n . '	color:gray;' . n . '}' . n . '.product-options-sub legend{' . n . '	font-size:11px;' . n . '}' . n . '.product-options-sub label{' . n . '	font-size:11px;' . n . '	padding-bottom: 5px;' . n . '}' . n . '#images ul.plain-list li{' . n . '	float:left;' . n . '	margin-right:10px;' . n . '	margin-bottom:10px;' . n . '	width:375px;' . n . '}' . n . '#images ul.plain-list li img{' . n . '	border-width: 1px;' . n . '	border-color:#BBBBBB;' . n . '	border-style:solid;' . n . '	padding:3px;' . n . '	background-color: #E0E0E0;' . n . '	float:left;' . n . '}' . n . '#images ul.plain-list li .imageEdit{' . n . '	background-color:#E0E0E0;' . n . '	height: 15px;' . n . '	width: 250px;' . n . '	text-align:left;' . n . '	padding:3px 3px 3px 110px;' . n . '	margin-top:0px;' . n . '}' . n . '#images ul.plain-list li .imageUpload{' . n . '	margin-top: 5px;' . n . '	padding:3px;' . n . '	width: 275px;' . n . '	border-width: 1px;' . n . '	border-color:#E0E0E0;' . n . '	border-style:solid;' . n . '}' . n . '</style>';
    doJS();
    echo n . n . '<form name="product" method="post" action="index.php" enctype="multipart/form-data">';
    echo '<input type="hidden" name="textile_body" value="1"/>';
    echo '<input type="hidden" name="textile_excerpt" value="1"/>';
    echo '<input type="hidden" name="Section" value="store"/>';
    echo hInput('ID', $ID) . eInput('product') . sInput($step) . '<input type="hidden" name="view" />' . startTable('edit') . '<tr>' . n;
    //if ($view == 'text')
    //{
    //-- markup help --------------
    echo '<td id="article-main">';
    echo "<h4 class='productHeading'>Product name</h4>";
    echo '<p><input type="text" id="title" name="Title" value="' . cleanfInput($Title) . '" class="edit" size="40" tabindex="1" />';
    if (($Status == 4 or $Status == 5) and $step != 'create') {
        include_once txpath . '/publish/taghandlers.php';
        echo sp . sp . '<a href="' . permlinkurl_id($ID) . '">' . gTxt('view') . '</a>';
    }
    echo '</p>';
    echo "<h4 class='productHeading'><span>Textile &amp; HTML allowed</span> Describe the product</h4>";
    echo n . graf('<textarea id="body" name="Body" cols="55" rows="31" tabindex="2" style="height: 180px;">' . htmlspecialchars($Body) . '</textarea>');
    //PRODUCT OPTIONS
    //================================
    //echo '<div id="productOptions" style="border-width:1px; border-color:#E1E1E1; border-style:solid; padding:5px; background-color:#F3F4F4;">';
    //PRODUCT & PRICE
    //================================
    echo n . n . '<fieldset class="product-options-sub" style="background-color:white">' . '<div style="float:left; margin-right: 40px;">' . n . graf('<label for="price">Price</label> ' . br . n . '<input style="width: 50px;" type="text" name="custom_1" id="price" value="' . cleanfInput($custom_1) . '"/> <em>' . $general_settings['store_currency'] . '</em>') . n . '</div>' . n . graf('<label for="weight">Weight</label>' . br . n . '<input type="text" style="width: 50px;" name="custom_2" id="weight" value="' . cleanfInput($custom_2) . '"/> <em>kg</em>');
    echo n . "</fieldset>";
    //CUSTOM FIELDS
    //================================
    if (isset($ID)) {
        $custom_fields = get_custom_fields($ID);
    } else {
        $custom_fields = '';
    }
    echo n . n . '<fieldset class="product-options-sub" style="background-color:white" id="custom_fields">' . n . '<legend>Custom Fields <span>[<a href="javascript:addCustomField(0);">Add a new custom field</a>]</span></legend>';
    echo $custom_fields;
    echo n . "</fieldset>";
    //CATEGORIES
    //================================
    //if(!is_callable("rss_admin_catlist")){
    echo n . n . '<fieldset class="product-options-sub" style="background-color:white">' . n . '<legend>Category <small>[<a href="?event=category">edit</a>]</small></legend>' . '<div style="float:left; margin-right: 10px;">' . n . '<label for="category">Select existing category </label> ' . br . n . build_list("category", "txp_category", "name", "title", $Category1, "parent='Products'", true, "ORDER BY name") . n . '</div>' . n . graf('<label for="new_category_name">Or create a new category</label>' . br . n . '<input id="new_category_name" type="text" name="new_category_name"/>');
    echo n . "</fieldset>";
    //}
    //VENDORS
    //================================
    echo n . n . '<fieldset class="product-options-sub" style="background-color:white">' . n . '<legend>Vendor <small>[<a href="?event=category">edit</a>]</small></legend>' . '<div style="float:left; width:50%;">' . n . '<label for="vendor">Select existing vendor </label> ' . br . n . build_list("vendor", "txp_category", "name", "title", $Category2, "parent='Vendors'", true, "ORDER BY name") . n . '</div>' . n . graf('<label for="new_vendor_name">Or create a new vendor</label>' . br . n . '<input id="new_vendor_name" type="text" name="new_vendor_name"/>');
    echo n . "</fieldset>";
    //INVENTORY
    //================================
    echo n . '<fieldset class="product-options-sub" style="background-color:white">' . n . '<legend>Inventory</legend>';
    echo n . '<label for="sku">SKU <em>Stock keeping unit</em></label>' . n . br . '<input type="text" name="custom_3" id="sku" value="' . $custom_3 . '"/>' . br . br;
    if (isset($custom_4) && !empty($custom_4)) {
        $showLevel = true;
        $levelSelected = "selected = \"selected\"";
        $levelStyle = 'display:block;';
    } else {
        $showLevel = false;
        $levelSelected = "";
        $levelStyle = 'display:none;';
    }
    echo n . '<select name="trackOptions" id="trackOptions" onchange="if(this.value == \'doTrack\'){$(\'#stockLevel\').show();}else{$(\'#stockLevel\').hide();}">' . n . n . '<option value="dontTrack">Don\'t track stock level</option>' . n . '<option value="doTrack" ' . $levelSelected . '>Keep track of stock level</option>' . n . '</select>' . br . br;
    echo n . '<div id="stockLevel" style="' . $levelStyle . '">' . n . '<label for="items_in_stock">Number of items in stock:</label>' . n . '<input type="text" name="custom_4" id="items_in_stock" style="width:20px;" value="' . cleanfInput($custom_4) . '"/>' . n . '</div><!--/stockLevel-->';
    echo n . "</fieldset>";
    //IMAGES
    //================================
    doJS();
    if ($step == "create") {
        echo n . '<fieldset class="product-options-sub" style="background-color:white">' . n . '<legend>Product images</legend>';
        echo n . '<em>Allowed file types are JPG, GIF &amp; PNG</em>';
        echo n . '<input type="file" name="uploadFile"/>' . n;
        echo n . "</fieldset>";
    } else {
        if ($step == "edit" || $step == "save" || empty($step)) {
            echo n . '<fieldset class="product-options-sub" id="images" style="background-color:white">' . n . '<legend>Product images</legend>';
            echo n . '<ul class="plain-list" id="image_list">';
            if (isset($Image) && !empty($Image)) {
                echo n . '<li id="image1">
							<img src="' . product_image_display($Image, "small") . '" alt="Product Image"/> <div class="imageEdit" style="display:block;">
							<a href="javascript:deleteImage(\'1\')">Delete Image</a> | <a href="javascript:updateImage(\'1\')">Update Image</a>

							<div class="imageUpload" id="imageUpload1" style="display:none;">
								Browse for new image: <input type="file" name="uploadFile1"/> and <input type="submit" value="Save"/>&nbsp; <a href="javascript:cancelUpload(\'1\')">Cancel</a>
								<input type="hidden" name="updateImage1" id="updateImage1" value="0"/>
							</div>
							<input id="imageField1" type="hidden" name="Image" value="' . $Image . '"/>

							</div>

							</li>';
            }
            if (isset($custom_6) && !empty($custom_6)) {
                echo n . '<li id="image2"><img src="' . product_image_display($custom_6, "small") . '" alt="Product Image"/>
						<div class="imageEdit" id="image1Control" style="display:block;">
							<a href="javascript:deleteImage(\'2\')">Delete Image</a> | <a href="javascript:updateImage(\'2\')">Update Image</a>

							<div class="imageUpload" id="imageUpload2" style="display:none;">
								Browse for new image: <input type="file" name="uploadFile2"/> and <input type="submit" value="Save"/> &nbsp; <a href="javascript:cancelUpload(\'2\')">Cancel</a>
								<input type="hidden" name="updateImage2" id="updateImage2" value="0"/>
							</div>

						</div><input id="imageField2" type="hidden" name="custom_6" value="' . $custom_6 . '"/></li>';
            }
            if (isset($custom_7) && !empty($custom_7)) {
                echo n . '<li id="image3"><img src="' . product_image_display($custom_7, "small") . '" alt="Product Image"/>
						<div class="imageEdit" id="image1Control" style="display:block;">
							<a href="javascript:deleteImage(\'3\')">Delete Image</a> | <a href="javascript:updateImage(\'3\')">Update Image</a>

							<div class="imageUpload" id="imageUpload3" style="display:none;">
								Browse for new image: <input type="file" name="uploadFile3"/> and <input type="submit" value="Save"/> &nbsp; <a href="javascript:cancelUpload(\'3\')">Cancel</a>
								<input type="hidden" name="updateImage3" id="updateImage3" value="0"/>
							</div>

							</div>
							<input id="imageField3" type="hidden" name="custom_7" value="' . $custom_7 . '"/></li>';
            }
            if (isset($custom_8) && !empty($custom_8)) {
                echo n . '<li id="image4"><img src="' . product_image_display($custom_8, "small") . '" alt="Product Image"/>
						<div class="imageEdit" id="image1Control" style="display:block;">
						<a href="javascript:deleteImage(\'4\')">Delete Image</a> | <a href="javascript:updateImage(\'4\')">Update Image</a>

							<div class="imageUpload" id="imageUpload4" style="display:none;">
								Browse for new image: <input type="file" name="uploadFile4"/> and <input type="submit" value="Save"/> &nbsp; <a href="javascript:cancelUpload(\'4\')">Cancel</a>
								<input type="hidden" name="updateImage4" id="updateImage4" value="0"/>
							</div>

						</div><input id="imageField4" type="hidden" name="custom_8" value="' . $custom_8 . '"/></li>';
            }
            echo n . "</ul><br style='clear:both;'/>";
            echo n . '<a href="javascript:void(0);" onclick="document.getElementById(\'otherImageUpload\').style.display = \'block\'">Add an image</a>' . n;
            echo n . '<div id="otherImageUpload" style="display:none; margin-top: 10px;">';
            echo n . '<em>Allowed file types are JPG, GIF &amp; PNG</em>';
            echo n . '<input type="file" name="uploadFile"/> and <input type="submit" value="upload"/>' . n;
            echo n . '</div>';
            echo n . "</fieldset>";
            echo n . '<div id="data"></div>';
        }
    }
    //echo '</div>'; // end productOptions
    // end left content area
    echo hInput('from_view', $view), '</td>';
    echo '<td id="article-col-2" style="padding-top: 75px;">';
    //start article-col-2
    //PRODUCT STATUS
    //================================
    echo n . n . '<fieldset id="write-status">' . n . '<legend>' . gTxt('status') . '</legend>' . n . status_radio_product($Status) . n . '</fieldset>';
    //-- comments stuff --------------
    if ($step == "create") {
        //Avoiding invite disappear when previewing
        $AnnotateInvite = !empty($store_out['AnnotateInvite']) ? $store_out['AnnotateInvite'] : $comments_default_invite;
        if ($comments_on_default == 1) {
            $Annotate = 1;
        }
    }
    if ($use_comments == 1) {
        echo n . n . '<fieldset id="write-comments">' . n . '<legend>Allow product reviews</legend>';
        $comments_expired = false;
        if ($step != 'create' && $comments_disabled_after) {
            $lifespan = $comments_disabled_after * 86400;
            $time_since = time() - $sPosted;
            if ($time_since > $lifespan) {
                $comments_expired = true;
            }
        }
        if ($comments_expired) {
            echo n . n . graf(gTxt('expired'));
        } else {
            echo n . n . graf(onoffRadio('Annotate', $Annotate)) . n . n . graf('<label for="comment-invite">' . gTxt('comment_invitation') . '</label>' . br . fInput('text', 'AnnotateInvite', $AnnotateInvite, 'edit', '', '', '', '', 'comment-invite'));
        }
        echo n . n . '</fieldset>';
    }
    /*if(is_callable("rss_admin_catlist")){
    			echo "<fieldset id='write-sort'></fieldset>";
    			echo rss_admin_catlist();
    		}*/
    //wilshireone multipule categories
    //-- publish button --------------
    echo has_privs('article.publish') ? fInput('submit', 'publish', gTxt('save'), "publish", '', '', '', 4) : fInput('submit', 'publish', gTxt('save'), "publish", '', '', '', 4);
    echo '</td> <!--/article-col-2-->';
    //end article-col-2
    echo '</td></tr></table></form>';
}