function foxyshop_save_settings()
{
    if (!isset($_POST['foxyshop_settings_update'])) {
        return;
    }
    if (!check_admin_referer('update-foxyshop-options')) {
        return;
    }
    global $foxyshop_settings;
    //Check for downloadables sync first and perform if setting is new
    if (!$foxyshop_settings['downloadables_sync'] && isset($_POST['foxyshop_downloadables_sync'])) {
        foxyshop_get_downloadable_list();
    }
    //Loop Through Most Fields
    $fields = array("version", "ship_categories", "weight_type", "enable_ship_to", "enable_dashboard_stats", "enable_subscriptions", "expiring_cards_reminder", "enable_bundled_products", "enable_addon_products", "related_products_custom", "related_products_tags", "sort_key", "use_jquery", "ga", "ga_advanced", "ga_type", "ga_demographics", "hide_subcat_children", "generate_product_sitemap", "manage_inventory_levels", "inventory_alert_level", "inventory_alert_email", "enable_sso", "sso_account_required", "checkout_customer_create", "downloadables_sync", "google_product_support", "google_product_merchant_id", "include_exception_list", "show_add_to_cart_link", "use_cart_validation", "locale_code");
    foreach ($fields as $field1) {
        $foxyshop_settings[$field1] = isset($_POST['foxyshop_' . $field1]) ? trim(stripslashes($_POST['foxyshop_' . $field1])) : '';
    }
    //Loop Through No Trim Fields
    $fields = array("browser_title_1", "browser_title_2", "browser_title_3", "browser_title_4", "browser_title_5", "browser_title_6", "browser_title_7");
    foreach ($fields as $field1) {
        $foxyshop_settings[$field1] = isset($_POST['foxyshop_' . $field1]) ? stripslashes($_POST['foxyshop_' . $field1]) : '';
    }
    //Default Image
    if ($_POST['foxyshop_default_image'] == 2) {
        $foxyshop_settings["default_image"] = "none";
    } elseif ($_POST['foxyshop_default_image'] == 1 && $_POST['foxyshop_default_image_custom'] != "") {
        $foxyshop_settings["default_image"] = trim(stripslashes($_POST['foxyshop_default_image_custom']));
    } else {
        $foxyshop_settings["default_image"] = "";
    }
    //Order Desk URL
    if (isset($_POST['foxyshop_set_orderdesk_url']) && !empty($_POST['foxyshop_orderdesk_url'])) {
        $foxyshop_settings["orderdesk_url"] = $_POST['foxyshop_orderdesk_url'];
    } else {
        $foxyshop_settings["orderdesk_url"] = "";
    }
    //Set FoxyCart Domain Name
    $domain = $_POST['foxyshop_domain'];
    if ($domain && get_option("foxyshop_setup_required")) {
        delete_option("foxyshop_setup_required");
    }
    //Delete the setup prompt if domain entered
    if ($domain && strpos($domain, ".") === false) {
        $domain .= ".foxycart.com";
    }
    $foxyshop_settings["domain"] = trim(stripslashes(str_replace("http://", "", $domain)));
    //Set Setup Prompt If FoxyCart API Version Available
    //if ($domain && version_compare($foxyshop_settings['version'], '1.1', ">=") && !$foxyshop_settings['api']['store_access_token']) add_option("foxyshop_setup_required", 1);
    //Other Settings Treated Specially
    $foxyshop_settings["default_weight"] = (int) $_POST['foxyshop_default_weight1'] . ' ' . (double) $_POST['foxyshop_default_weight2'];
    $foxyshop_settings["products_per_page"] = (int) $_POST['foxyshop_products_per_page'] == 0 ? -1 : (int) $_POST['foxyshop_products_per_page'];
    //Cache the FoxyCart Includes
    if (version_compare($foxyshop_settings['version'], '0.7.2', ">=") && version_compare($foxyshop_settings['version'], '2.0', "<") && $foxyshop_settings['domain']) {
        if (version_compare($foxyshop_settings['version'], '0.7.2', "<=")) {
            $cart_type = "colorbox";
        } else {
            $cart_type = "sidecart";
        }
        $foxy_data = array("api_action" => "store_includes_get", "javascript_library" => "none", "cart_type" => $cart_type);
        $foxy_data = apply_filters('foxyshop_store_includes_get', $foxy_data);
        $foxy_response = foxyshop_get_foxycart_data($foxy_data);
        $xml = simplexml_load_string($foxy_response, NULL, LIBXML_NOCDATA);
        if ($xml->result != "ERROR") {
            if ($xml->code_block) {
                $foxyshop_settings['foxycart_include_cache'] = (string) $xml->code_block;
            }
        } else {
            $foxyshop_settings['foxycart_include_cache'] = "";
        }
    } else {
        $foxyshop_settings['foxycart_include_cache'] = "";
    }
    //Save
    update_option("foxyshop_settings", $foxyshop_settings);
    wp_redirect("edit.php?post_type=foxyshop_product&page=foxyshop_settings_page&saved=1");
    exit;
}
function foxyshop_product_details_setup()
{
    global $post, $foxyshop_settings;
    $_price = number_format((double) get_post_meta($post->ID, '_price', TRUE), FOXYSHOP_DECIMAL_PLACES, ".", ",");
    $_code = get_post_meta($post->ID, '_code', TRUE);
    $_category = get_post_meta($post->ID, '_category', TRUE);
    $_quantity_min = get_post_meta($post->ID, '_quantity_min', TRUE);
    $_quantity_max = get_post_meta($post->ID, '_quantity_max', TRUE);
    $_quantity_hide = get_post_meta($post->ID, '_quantity_hide', TRUE);
    $defaultweight = explode(" ", $foxyshop_settings['default_weight']);
    $defaultweight1 = (int) $defaultweight[0];
    $defaultweight2 = count($defaultweight) > 1 ? number_format($defaultweight[1], 1) : "0.0";
    $original_weight = get_post_meta($post->ID, '_weight', 1);
    if (!$original_weight && strpos($_SERVER['SCRIPT_FILENAME'], "post-new.php") === false) {
        $disable_weight_checked = ' checked="checked"';
        $_weight = array("", "");
    } else {
        $_weight = explode(" ", $original_weight);
        if (!$original_weight) {
            $_weight = array($defaultweight1, $defaultweight2);
        }
        $disable_weight_checked = "";
        if ((int) $_weight[0] == 0 && (double) $_weight[1] == 0) {
            if ($defaultweight1 == 0 && $defaultweight2 == 0.0) {
                $disable_weight_checked = ' checked="checked"';
                $_weight[0] = "";
                $_weight[1] = "";
            } else {
                $_weight[0] = $defaultweight1;
                $_weight[1] = $defaultweight2;
            }
        } else {
            $_weight[0] = (int) $_weight[0];
            if (!isset($_weight[1])) {
                $_weight[1] = 0;
            }
            $_weight[1] = number_format($_weight[1], 1);
        }
    }
    if ($foxyshop_settings['downloadables_sync'] && version_compare($foxyshop_settings['version'], '0.7.2', ">=") && $foxyshop_settings['domain']) {
        $show_downloadables = 1;
    } else {
        $show_downloadables = 0;
    }
    $_hide_product = get_post_meta($post->ID, '_hide_product', TRUE);
    ?>
	<?php 
    if ($show_downloadables) {
        ?>
	<a href="#" id="show_downloadable_list" title="<?php 
        _e('Show Available Downloadables', 'foxyshop');
        ?>
">"<?php 
        _e('Show Available Downloadables', 'foxyshop');
        ?>
</a>
	<div class="foxyshop_field_control" id="downloadable_list_parent">
		<a href="#" id="hide_downloadable_list" title="<?php 
        _e('Hide Available Downloadables', 'foxyshop');
        ?>
"><?php 
        _e('Hide Available Downloadables', 'foxyshop');
        ?>
</a>
		<label for="downloadable_list" style="width:100%;"><?php 
        _e('Select Downloadable To Prefill Data', 'foxyshop');
        ?>
</label>
		<select name="downloadable_list" id="downloadable_list">
			<?php 
        foxyshop_get_downloadable_list();
        $downloadable_list = get_option("foxyshop_downloadables");
        if (!is_array($downloadable_list)) {
            $downloadable_list = array();
            echo '<option value="">' . __('None Found') . '</option>' . "\n";
        } else {
            echo '<option value="">- - ' . __('Select Below', 'foxyshop') . ' - -</option>' . "\n";
        }
        foreach ($downloadable_list as $downloadable) {
            echo '<option value="' . esc_attr($downloadable['product_code']) . '"';
            echo ' category_code="' . esc_attr($downloadable['category_code']) . '"';
            echo ' product_price="' . esc_attr($downloadable['product_price']) . '"';
            echo '>' . esc_attr($downloadable['product_name']) . '</option>';
            echo "\n";
        }
        ?>
		</select>
		<a href="#" id="ajax_get_downloadable_list" title="<?php 
        _e('Refresh List', 'foxyshop');
        ?>
"><?php 
        _e('Refresh List', 'foxyshop');
        ?>
</a>
		<div style="clear:both"></div>
	</div>
	<?php 
    }
    ?>
	<div id="foxyshop_price" class="foxyshop_field_control">
		<label for="_price"><?php 
    _e('Base Price', 'foxyshop');
    ?>
</label>
		<input type="text" name="_price" id="_price" value="<?php 
    echo $_price;
    ?>
" onblur="foxyshop_check_number(this);" style="width: 90px; float: left;" />
		<span style="float: left; margin: 9px 0 0 5px;">0.00</span>
	</div>
	<div id="foxyshop_item_code" class="foxyshop_field_control">
		<label for="_code"><?php 
    _e('Item Code', 'foxyshop');
    ?>
</label>
		<input type="text" name="_code" id="_code" value="<?php 
    echo $_code;
    ?>
" />
	</div>
	<div id="foxyshop_weight" class="foxyshop_field_control">
		<label for="_weight1"><?php 
    _e('Weight', 'foxyshop');
    ?>
</label>
		<input type="text" name="_weight1" id="_weight1" value="<?php 
    echo $_weight[0];
    ?>
"<?php 
    if ($disable_weight_checked) {
        echo ' disabled="disabled"';
    }
    ?>
 />
		<span style="float: left; margin: 9px 0 0 5px; width: 21px;"><?php 
    echo $foxyshop_settings['weight_type'] == "metric" ? 'kg' : 'lbs';
    ?>
</span>
		<input type="text" name="_weight2" id="_weight2" value="<?php 
    echo $_weight[1];
    ?>
"<?php 
    if ($disable_weight_checked) {
        echo ' disabled="disabled"';
    }
    ?>
 />
		<span style="float: left; margin: 9px 0 0 5px; width: 23px;"><?php 
    echo $foxyshop_settings['weight_type'] == "metric" ? 'gm' : 'oz';
    ?>
</span>
		<input type="checkbox" name="weight_disable" id="weight_disable" title="<?php 
    _e('Disable Weight', 'foxyshop');
    ?>
" style="float: left; margin-top: 7px;"<?php 
    echo $disable_weight_checked;
    ?>
 />
		<label id="weight_disable_label" for="weight_disable" style="float: left; margin: 6px 0 0 2px; width: 16px;" title="<?php 
    _e('Disable Weight', 'foxyshop');
    ?>
" class="iconsprite <?php 
    echo $disable_weight_checked ? "hide_color" : "hide_gray";
    ?>
"></label>
	</div>
	<div id="foxyshop_quantity" class="foxyshop_field_control">
		<label for="_quantity_min"><?php 
    _e('Qty Settings', 'foxyshop');
    ?>
</label>
		<input type="text" name="_quantity_min" id="_quantity_min" value="<?php 
    echo $_quantity_min;
    ?>
" title="<?php 
    _e('Minimum Quantity', 'foxyshop');
    ?>
" style="width: 33px; float: left;" onblur="foxyshop_check_number_single(this);"<?php 
    if ($_quantity_hide) {
        echo ' disabled="disabled"';
    }
    ?>
 />
		<span id="quantity_min_label" style="float: left; margin: 6px 0 0 1px; width: 26px;" class="iconsprite <?php 
    echo $_quantity_min ? "down_color" : "down_gray";
    ?>
"></span>
		<input type="text" name="_quantity_max" id="_quantity_max" value="<?php 
    echo $_quantity_max;
    ?>
" title="<?php 
    _e('Maximum Quantity', 'foxyshop');
    ?>
" style="width: 33px; float: left;" onblur="foxyshop_check_number_single(this);"<?php 
    if ($_quantity_hide) {
        echo ' disabled="disabled"';
    }
    ?>
 />
		<span id="quantity_max_label" style="float: left; margin: 6px 0 0 1px; width: 26px;" class="iconsprite <?php 
    echo $_quantity_max ? "up_color" : "up_gray";
    ?>
"></span>
		<input type="checkbox" name="_quantity_hide" id="_quantity_hide" title="<?php 
    _e('Hide Quantity Box', 'foxyshop');
    ?>
" style="float: left; margin-top: 7px;"<?php 
    echo checked($_quantity_hide, "on");
    ?>
 />
		<label id="quantity_hide_label" for="_quantity_hide" style="float: left; margin: 6px 0 0 2px; width: 16px;" title="<?php 
    _e('Hide Quantity Box', 'foxyshop');
    ?>
" class="iconsprite <?php 
    echo $_quantity_hide ? "hide_color" : "hide_gray";
    ?>
"></label>
		<div style="clear:both"></div>
	</div>

	<?php 
    if (version_compare($foxyshop_settings['version'], '0.7.2', ">=") && $foxyshop_settings['domain']) {
        ?>
	<div id="foxyshop_category" class="foxyshop_field_control">
		<label for="_category" style="width: 100%;">
			<?php 
        _e('FoxyCart Category');
        ?>
			<small>(<a href="#" id="ajax_get_category_list_select" title="<?php 
        _e('Refresh List', 'foxyshop');
        ?>
"><?php 
        _e('Refresh List', 'foxyshop');
        ?>
</a>)</small>
		</label>
		<select name="_category" id="_category">
			<?php 
        if (strpos($foxyshop_settings['ship_categories'], "DEFAULT") === false) {
            $foxyshop_settings['ship_categories'] = "DEFAULT|" . __('Default for all products', 'foxyshop') . "\n" . $foxyshop_settings['ship_categories'];
        }
        $arrShipCategories = preg_split("/(\r\n|\n|\r)/", $foxyshop_settings['ship_categories']);
        for ($i = 0; $i < count($arrShipCategories); $i++) {
            if ($arrShipCategories[$i] == "") {
                continue;
            }
            $shipping_category = explode("|", $arrShipCategories[$i]);
            $shipping_category_code = trim($shipping_category[0]);
            if ($shipping_category_code == "DEFAULT") {
                $shipping_category_code = "";
            }
            $shipping_category_name = $shipping_category_code;
            $shipping_category_type = '';
            if (isset($shipping_category[1])) {
                $shipping_category_name = trim($shipping_category[1]);
            }
            if (isset($shipping_category[2])) {
                $shipping_category_type = trim($shipping_category[2]);
            }
            echo '<option value="' . esc_attr($shipping_category_code) . '"';
            if ($shipping_category_type) {
                echo ' rel="' . esc_attr($shipping_category_type) . '"';
            }
            if (esc_attr($shipping_category_code == $_category)) {
                echo ' selected="selected"';
            }
            echo '>' . esc_attr($shipping_category_name) . '</option>';
            echo "\n";
        }
        ?>
		</select>
	</div>
	<?php 
    } else {
        ?>
	<div id="foxyshop_category" class="foxyshop_field_control">
		<label for="_category" style="width:76px; margin-right: 4px;"><?php 
        _e('FoxyCart Cat');
        ?>
</label>
		<select name="_category" id="_category">
			<?php 
        if (strpos($foxyshop_settings['ship_categories'], "DEFAULT") === false) {
            $foxyshop_settings['ship_categories'] = "DEFAULT|" . __('Default for all products', 'foxyshop') . "\n" . $foxyshop_settings['ship_categories'];
        }
        $arrShipCategories = preg_split("/(\r\n|\n|\r)/", $foxyshop_settings['ship_categories']);
        for ($i = 0; $i < count($arrShipCategories); $i++) {
            if ($arrShipCategories[$i] == "") {
                continue;
            }
            $shipping_category = explode("|", $arrShipCategories[$i]);
            $shipping_category_code = trim($shipping_category[0]);
            if ($shipping_category_code == "DEFAULT") {
                $shipping_category_code = "";
            }
            $shipping_category_name = $shipping_category_code;
            $shipping_category_type = '';
            if (isset($shipping_category[1])) {
                $shipping_category_name = trim($shipping_category[1]);
            }
            if (isset($shipping_category[2])) {
                $shipping_category_type = trim($shipping_category[2]);
            }
            echo '<option value="' . esc_attr($shipping_category_code) . '"';
            if ($shipping_category_type) {
                echo ' rel="' . esc_attr($shipping_category_type) . '"';
            }
            if (esc_attr($shipping_category_code == $_category)) {
                echo ' selected="selected"';
            }
            echo '>' . esc_attr($shipping_category_name) . '</option>';
            echo "\n";
        }
        ?>
		</select>
	</div>
	<?php 
    }
    ?>

	<?php 
    if ($foxyshop_settings['show_add_to_cart_link'] && isset($_REQUEST['post'])) {
        global $product;
        $product = foxyshop_setup_product();
        ?>
	<div id="foxyshop_add_to_cart_link" class="foxyshop_field_control">
		<label for="add_to_cart_link"><?php 
        _e('Add to Cart', 'foxyshop');
        ?>
</label>
		<input type="text" name="add_to_cart_link" id="add_to_cart_link" value="<?php 
        echo foxyshop_product_link("", 1);
        ?>
" onclick="this.select();" readonly="readonly" />
	</div>
	<?php 
    }
    ?>

	<?php 
    if ($foxyshop_settings['enable_sso'] && $foxyshop_settings['sso_account_required'] == 2) {
        ?>
	<div id="foxyshop_require_sso" class="foxyshop_field_control">
		<input type="checkbox" name="_require_sso" id="_require_sso" style="float: left; margin: 5px 0 0 10px;"<?php 
        echo checked(get_post_meta($post->ID, '_require_sso', TRUE), "on");
        ?>
 />
		<label style="width: 210px;" for="_require_sso"><?php 
        _e('Require Account For Checkout', 'foxyshop');
        ?>
</label>
	</div>
	<?php 
    }
    ?>
	<div id="foxyshop_hide_product" class="foxyshop_field_control">
		<input type="checkbox" name="_hide_product" id="_hide_product"<?php 
    echo checked($_hide_product, "on");
    ?>
 />
		<label style="width: 210px;" for="_hide_product"><?php 
    echo sprintf(__('Hide This %s From List View', 'foxyshop'), FOXYSHOP_PRODUCT_NAME_SINGULAR);
    ?>
</label>
	</div>
	<div style="clear:both"></div>

	<script type="text/javascript">

	//Setup Vars For Use Later
	var FOXYSHOP_PRODUCT_NAME_SINGULAR = '<?php 
    echo strtolower(str_replace("'", "\\'", FOXYSHOP_PRODUCT_NAME_SINGULAR));
    ?>
';
	var show_downloadables = <?php 
    echo $show_downloadables ? 1 : 0;
    ?>
;
	var nonce_downloadable_list = '<?php 
    echo wp_create_nonce("foxyshop-ajax-get-downloadable-list");
    ?>
';
	var defaultweight1 = '<?php 
    echo $defaultweight1;
    ?>
';
	var defaultweight2 = '<?php 
    echo $defaultweight2;
    ?>
';
	var weight_dividend = <?php 
    echo $foxyshop_settings['weight_type'] == 'metric' ? 1000 : 16;
    ?>
;
	var use_chozen = <?php 
    echo $foxyshop_settings['related_products_custom'] || $foxyshop_settings['related_products_tags'] || $foxyshop_settings['enable_addon_products'] ? 1 : 0;
    ?>
;

	var renameLive = false;
	var post_id = <?php 
    echo $post->ID;
    ?>
;
	var nonce_images = '<?php 
    echo wp_create_nonce("foxyshop-product-image-functions-" . $post->ID);
    ?>
';

	<?php 
    //Get Max Upload Limit
    $max_upload = (int) ini_get('upload_max_filesize');
    $max_post = (int) ini_get('post_max_size');
    $memory_limit = (int) ini_get('memory_limit');
    $upload_mb = min($max_upload, $max_post, $memory_limit);
    $foxyshop_max_upload = $upload_mb * 1048576;
    if ($foxyshop_max_upload == 0) {
        $foxyshop_max_upload = "8000000";
    }
    ?>
	var foxyshop_max_upload = '<?php 
    echo $foxyshop_max_upload;
    ?>
';

	var FOXYSHOP_DIR = '<?php 
    echo FOXYSHOP_DIR;
    ?>
';
	var FOXYSHOP_URL_BASE = '<?php 
    echo FOXYSHOP_URL_BASE;
    ?>
';
	var bloginfo_url = '<?php 
    echo is_ssl() ? str_replace("http://", "https://", get_bloginfo("url")) : get_bloginfo("url");
    ?>
';
	var datafeed_url_key = '<?php 
    echo $foxyshop_settings['datafeed_url_key'];
    ?>
';

	</script>

	<?php 
    //Add Action For Product Details (For Other Integrations)
    do_action("foxyshop_admin_product_details", $post->ID);
    //Setup Hidden Admin Fields
    echo '<input type="hidden" name="products_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
    echo '<input type="hidden" name="menu_order" value="' . ($post->menu_order == 0 && $post->post_status == "auto-draft" ? $post->ID : $post->menu_order) . '" />';
}
function foxyshop_ajax_get_downloadable_list()
{
    check_ajax_referer('foxyshop-ajax-get-downloadable-list', 'security');
    $output = foxyshop_get_downloadable_list();
    foreach ($output as $downloadable) {
        echo '<option value="' . esc_attr($downloadable['product_code']) . '"';
        echo ' category_code="' . esc_attr($downloadable['category_code']) . '"';
        echo ' product_price="' . esc_attr($downloadable['product_price']) . '"';
        echo '>' . esc_attr($downloadable['product_name']) . '</option>';
        echo "\n";
    }
    die;
}