function save_data_and_redirect()
{
    if (!empty($_POST["save_bepro_listing"]) && !empty($_POST["redirect"])) {
        $wp_upload_dir = wp_upload_dir();
        if ($post_id = bepro_listings_save(false, true)) {
            $data = get_option("bepro_listings");
            //add to cart and redirect?
            if (is_numeric($data["require_payment"]) && empty($_POST["bepro_post_id"]) && class_exists("Bepro_cart")) {
                //find an order id with space for this type of order
                $bl_order_id = bl_get_vacant_order_id($user_id, $data["require_payment"], $_POST["bl_package"]);
                if ($data["require_payment"] == 1) {
                    $cost = bepro_get_total_cat_cost($post_id);
                } else {
                    if ($data["require_payment"] == 2 && is_numeric($_POST["bl_package"])) {
                        $cost = get_post_meta($_POST["bl_package"], "package_cost", true);
                    }
                }
                if (!empty($cost) && function_exists("bpc_cart_actions_handler")) {
                    $_POST["addcart"] = 1;
                    $_POST["price"] = $cost;
                    $_POST["item_number"] = $bl_order_id;
                    $_POST["product"] = __("Listing Submission");
                    bpc_cart_actions_handler();
                }
            }
            header("LOCATION: " . $_POST["redirect"]);
            exit;
        }
    }
}
function bl_form_package_field($bl_order_id = null, $return_this = false, $selected = false)
{
    $data = get_option("bepro_listings");
    $order = false;
    $return_text = "";
    $user_id = get_current_user_id();
    if (!$selected && is_numeric($_POST["bpl_package"])) {
        $selected = $_POST["bpl_package"];
    } else {
        if ($bl_order_id) {
            $order = bl_get_payment_order($bl_order_id);
            //allow user to change which package this listing is associated with but show its selected
            if (@is_numeric($order->feature_id)) {
                $selected = $order->feature_id;
            }
        }
    }
    $return_text .= '<div id="flat_fee">';
    if (@$data["require_payment"] && $data["require_payment"] == 2) {
        $packages = get_posts(array("post_type" => "bpl_packages"));
        if (!$packages || sizeof($packages) < 1) {
            return;
        }
        $return_text .= '<h3>' . __("Available Packages", "bepro-listings") . '</h3>';
        foreach ($packages as $package) {
            $num_listings = get_post_meta($package->ID, "num_package_listings", true);
            $duration = get_post_meta($package->ID, "package_duration", true);
            $cost = get_post_meta($package->ID, "package_cost", true);
            $vacant = bl_get_vacant_order_id($user_id, 2, $package->ID, false);
            $listings_left = "";
            if ($vacant) {
                global $wpdb;
                $exiting_listings = $wpdb->get_row("SELECT count(*) as num_listings FROM " . $wpdb->prefix . BEPRO_LISTINGS_TABLE_NAME . " as bl WHERE bl.bl_order_id =" . $vacant);
                $exiting_listings = !empty($exiting_listings) ? $exiting_listings->num_listings : "";
                $listings_left = is_numeric($exiting_listings) ? $num_listings - $exiting_listings : "";
                $listings_left = is_numeric($listings_left) && $listings_left > 0 ? "(" . $listings_left . " " . __("Remaining", "bepro-listings") . ")" : "";
            }
            //this is a package, we need to know how many listings are possible
            if (!$num_listings || !is_numeric($num_listings) || $num_listings < 1) {
                return;
            }
            $package_div[] = array();
            $return_text .= '<div class="package_option"><input type="radio" name="bpl_package" id="package_sel_' . $package->ID . '" value="' . $package->ID . '" ' . (@$order && $order->feature_id == $package->ID ? "checked='checked'" : "") . ' ' . (@$selected && $selected == $package->ID ? "checked='checked'" : "") . '> <span class="package_head">' . $package->post_title . ' ' . $listings_left . '</span>
				<span class="package_options">
					<ul>
						<li># ' . __("Days", "bepro-listings") . ' ' . $duration . '</li>
						<li># ' . __("Listings", "bepro-listings") . ' ' . $num_listings . '</li>
						<li>' . __("Cost", "bepro-listings") . ' ' . $data["currency_sign"] . $cost . '</li>
					</ul>
				<span>
				<span class="package_details">' . $package->post_content . '<span>
				</div>';
        }
    }
    $return_text .= '</div><div style="clear:both"></div>';
    if ($return_this) {
        return $return_text;
    }
    echo $return_text;
}