------------ ATTENTION ------------
If you need to edit this template, do not edit the version in the plugin directory. Place a copy in your template folder and edit it there.
This will allow you to upgrade FoxyShop without breaking your customizations. More details here: http://www.foxy-shop.com/documentation/theme-customization/
-----------------------------------
*/
?>

<?php 
foxyshop_include('header');
?>
<div id="foxyshop_container">
<?php 
global $foxyshop_prettyphoto_included;
//Initialize Product
global $product, $prod;
$product = foxyshop_setup_product($prod);
//Just for the widget, since url links are no longer available
global $foxyshop_skip_url_link;
$foxyshop_skip_url_link = 1;
//This is for testing to see what is included in the $product array
//print_r($product);
//Initialize Form
foxyshop_start_form();
//Write Breadcrumbs
//foxyshop_breadcrumbs(" &raquo; ", "&laquo; Back to Products");
//Shows Main Image and Optional Slideshow
//Available Built-in Options: magnific-popup (lightbox), prettyPhoto (lightbox)
//Second arg writes css and js includes on page
//If you want to make more customizations, you can grab the code from helperfunctions.php line ~780 and paste here
//-------------------------------------------------------------------------------------------------------------------------
foxyshop_build_image_slideshow("magnific", true);
function foxyshop_subscription_management()
{
    global $foxyshop_settings, $wp_version, $product;
    //Setup Fields and Defaults
    $foxy_data_defaults = array("is_active_filter" => "", "frequency_filter" => "", "past_due_amount_filter" => "", "start_date_filter_begin" => date("Y-m-d", strtotime("-10 days")), "start_date_filter_end" => date("Y-m-d"), "next_transaction_date_filter_begin" => "", "next_transaction_date_filter_end" => "", "end_date_filter_begin" => "", "end_date_filter_end" => "", "third_party_id_filter" => "", "last_transaction_id_filter" => "", "customer_id_filter" => "", "customer_email_filter" => "", "customer_first_name_filter" => "", "customer_last_name_filter" => "", "product_code_filter" => "", "product_name_filter" => "", "product_option_name_filter" => "", "product_option_value_filter" => "");
    if (version_compare($foxyshop_settings['version'], '0.7.2', ">=")) {
        $foxy_data_defaults["custom_field_name_filter"] = "";
        $foxy_data_defaults["custom_field_value_filter"] = "";
    }
    $foxy_data = wp_parse_args(array("api_action" => "subscription_list"), apply_filters('foxyshop_subscription_filter_defaults', $foxy_data_defaults));
    $foxyshop_querystring = "?post_type=foxyshop_product&amp;page=foxyshop_subscription_management&amp;foxyshop_search=1";
    $foxyshop_hidden_input = "";
    if (isset($_GET['foxyshop_search']) || !defined('FOXYSHOP_AUTO_API_DISABLED')) {
        $fields = array("is_active_filter", "frequency_filter", "past_due_amount_filter", "start_date_filter_begin", "start_date_filter_end", "next_transaction_date_filter_begin", "next_transaction_date_filter_end", "end_date_filter_begin", "end_date_filter_end", "third_party_id_filter", "last_transaction_id_filter", "customer_id_filter", "customer_email_filter", "customer_first_name_filter", "customer_last_name_filter", "product_code_filter", "product_name_filter", "product_option_name_filter", "product_option_value_filter", "custom_field_name_filter", "custom_field_value_filter");
        foreach ($fields as $field) {
            if (isset($_GET[$field])) {
                $foxy_data[$field] = $_GET[$field];
                $foxyshop_querystring .= "&amp;{$field}=" . urlencode($_GET[$field]);
                $foxyshop_hidden_input .= '<input type="hidden" name="' . $field . '" value="' . htmlspecialchars($_GET[$field]) . '" />' . "\n";
            }
        }
        $foxy_data['pagination_start'] = isset($_GET['pagination_start']) ? $_GET['pagination_start'] : 0;
        $p = (int) (version_compare($foxyshop_settings['version'], '0.7.1', "<") ? 50 : FOXYSHOP_API_ENTRIES_PER_PAGE);
        if (version_compare($foxyshop_settings['version'], '0.7.0', ">")) {
            $foxy_data['entries_per_page'] = $p;
        }
        $start_offset = (int) (version_compare($foxyshop_settings['version'], '0.7.1', "<=") ? -1 : 0);
        if (isset($_GET['paged-top']) || isset($_GET['paged-bottom'])) {
            if ($_GET['paged-top'] != $_GET['paged-top-original']) {
                $foxy_data['pagination_start'] = $p * ((int) $_GET['paged-top'] - 1) + 1 + $start_offset;
            }
            if ($_GET['paged-bottom'] != $_GET['paged-bottom-original']) {
                $foxy_data['pagination_start'] = $p * ((int) $_GET['paged-bottom'] - 1) + 1 + $start_offset;
            }
        }
    }
    $subscription_products = get_posts(array('post_type' => 'foxyshop_product', "meta_key" => "_sub_frequency", "meta_value" => "", 'meta_compare' => '!=', "_sub_frequency", 'numberposts' => -1));
    $subscription_product_array = array();
    foreach ($subscription_products as $subscription_product) {
        $product = foxyshop_setup_product($subscription_product);
        $subscription_product_array[] = array("id" => $product['id'], "name" => $product['name'], "price" => $product['price']);
    }
    ?>

	<div class="wrap">
		<div class="icon32 icon32-posts-page" id="icon-edit-pages"><br></div>
		<h2><?php 
    _e('Manage Subscriptions', 'foxyshop');
    ?>
</h2>

		<form action="edit.php" method="get" id="foxyshop_searchform" name="foxyshop_searchform" style="display: block; margin: 14px 0 20px 0;">
		<input type="hidden" name="foxyshop_search" value="1" />
		<input type="hidden" name="post_type" value="foxyshop_product" />
		<input type="hidden" name="page" value="foxyshop_subscription_management" />

		<table class="widefat">
		<thead><tr><th colspan="2"><img src="<?php 
    echo FOXYSHOP_DIR;
    ?>
/images/search-icon.png" alt="" /><?php 
    _e('Search Options', 'foxyshop');
    ?>
</th></tr></thead>
		<tbody><tr><td>
			<div class="foxyshop_field_control">
				<label for="is_active_filter"><?php 
    _e('Subscription Type', 'foxyshop');
    ?>
</label>
				<select name="is_active_filter" id="is_active_filter">
				<?php 
    $selectArray = array("0" => __("Disabled", 'foxyshop'), "1" => __("Active", 'foxyshop'), "" => __("Both", 'foxyshop'));
    foreach ($selectArray as $selectKey => $selectOption) {
        echo '<option value="' . $selectKey . '"' . ($foxy_data['is_active_filter'] == $selectKey ? ' selected="selected"' : '') . '>' . $selectOption . '</option>' . "\n";
    }
    ?>
				</select>
			</div>
			<div class="foxyshop_field_control">
				<label for="past_due_amount_filter"><?php 
    _e('Past Due Status', 'foxyshop');
    ?>
</label>
				<select name="past_due_amount_filter" id="past_due_amount_filter">
				<?php 
    $selectArray = array("" => __('Show All', 'foxyshop'), "1" => __('Show Past Due Only', 'foxyshop'));
    foreach ($selectArray as $selectKey => $selectOption) {
        echo '<option value="' . $selectKey . '"' . ($foxy_data['past_due_amount_filter'] == $selectKey ? ' selected="selected"' : '') . '>' . $selectOption . '</option>' . "\n";
    }
    ?>
				</select>
			</div>
			<div class="foxyshop_field_control">
				<label for="frequency_filter"><?php 
    _e('Frequency', 'foxyshop');
    ?>
</label><input type="text" name="frequency_filter" id="frequency_filter" value="<?php 
    echo $foxy_data['frequency_filter'];
    ?>
" />
			</div>
			<div class="foxyshop_field_control">
				<label for="third_party_id_filter"><?php 
    _e('Third Party ID', 'foxyshop');
    ?>
</label><input type="text" name="third_party_id_filter" id="third_party_id_filter" value="<?php 
    echo $foxy_data['third_party_id_filter'];
    ?>
" />
				<span>PayPal</span>
			</div>
			<div class="foxyshop_field_control">
				<label for="last_transaction_id_filter"><?php 
    _e('Last Transaction ID', 'foxyshop');
    ?>
</label><input type="text" name="last_transaction_id_filter" id="last_transaction_id_filter" value="<?php 
    echo $foxy_data['last_transaction_id_filter'];
    ?>
" />
			</div>

			<div class="foxyshop_field_control">
				<label for="product_code_filter"><?php 
    echo FOXYSHOP_PRODUCT_NAME_SINGULAR . ' ' . __('Code', 'foxyshop');
    ?>
</label><input type="text" name="product_code_filter" id="product_code_filter" value="<?php 
    echo $foxy_data['product_code_filter'];
    ?>
" />
			</div>
			<div class="foxyshop_field_control">
				<label for="product_name_filter"><?php 
    echo FOXYSHOP_PRODUCT_NAME_SINGULAR . ' ' . __('Name', 'foxyshop');
    ?>
</label><input type="text" name="product_name_filter" id="product_name_filter" value="<?php 
    echo $foxy_data['product_name_filter'];
    ?>
" />
			</div>
			<div class="foxyshop_field_control">
				<label for="product_option_name_filter"><?php 
    echo FOXYSHOP_PRODUCT_NAME_SINGULAR . ' ' . __('Option Name', 'foxyshop');
    ?>
</label><input type="text" name="product_option_name_filter" id="product_option_name_filter" value="<?php 
    echo $foxy_data['product_option_name_filter'];
    ?>
" />
				<label for="product_option_value_filter" style="margin-left: 15px; margin-top: 4px; width: 38px;"><?php 
    _e('Value', 'foxyshop');
    ?>
</label><input type="text" name="product_option_value_filter" id="product_option_value_filter" value="<?php 
    echo $foxy_data['product_option_value_filter'];
    ?>
" />
			</div>
			<?php 
    if (version_compare($foxyshop_settings['version'], '0.7.2', ">=")) {
        ?>
			<div class="foxyshop_field_control">
				<label for="custom_field_name_filter"><?php 
        _e('Custom Field Name', 'foxyshop');
        ?>
</label><input type="text" name="custom_field_name_filter" id="custom_field_name_filter" value="<?php 
        echo $foxy_data['custom_field_name_filter'];
        ?>
" />
				<label for="custom_field_value_filter" style="margin-left: 15px; margin-top: 4px; width: 38px;"><?php 
        _e('Value', 'foxyshop');
        ?>
</label><input type="text" name="custom_field_value_filter" id="custom_field_value_filter" value="<?php 
        echo $foxy_data['custom_field_value_filter'];
        ?>
" />
			</div>
			<?php 
    }
    ?>

		</td><td>

			<div class="foxyshop_field_control">
				<label for="start_date_filter_begin"><?php 
    _e('Start Date', 'foxyshop');
    ?>
</label>
				<input type="text" name="start_date_filter_begin" id="start_date_filter_begin" value="<?php 
    echo $foxy_data['start_date_filter_begin'];
    ?>
" class="foxyshop_date_field" />
				<span><?php 
    _e('to', 'foxyshop');
    ?>
</span>
				<input type="text" name="start_date_filter_end" id="start_date_filter_end" value="<?php 
    echo $foxy_data['start_date_filter_end'];
    ?>
" class="foxyshop_date_field" />
			</div>
			<div class="foxyshop_field_control">
				<label for="next_transaction_date_filter_begin"><?php 
    _e('Next Transaction Date', 'foxyshop');
    ?>
</label>
				<input type="text" name="next_transaction_date_filter_begin" id="next_transaction_date_filter_begin" value="<?php 
    echo $foxy_data['next_transaction_date_filter_begin'];
    ?>
" class="foxyshop_date_field" />
				<span><?php 
    _e('to', 'foxyshop');
    ?>
</span>
				<input type="text" name="next_transaction_date_filter_end" id="next_transaction_date_filter_end" value="<?php 
    echo $foxy_data['next_transaction_date_filter_end'];
    ?>
" class="foxyshop_date_field" />
			</div>
			<div class="foxyshop_field_control">
				<label for="end_date_filter_begin"><?php 
    _e('End Date', 'foxyshop');
    ?>
</label>
				<input type="text" name="end_date_filter_begin" id="end_date_filter_begin" value="<?php 
    echo $foxy_data['end_date_filter_begin'];
    ?>
" class="foxyshop_date_field" />
				<span><?php 
    _e('to', 'foxyshop');
    ?>
</span>
				<input type="text" name="end_date_filter_end" id="end_date_filter_end" value="<?php 
    echo $foxy_data['end_date_filter_end'];
    ?>
" class="foxyshop_date_field" />
			</div>

			<div class="foxyshop_field_control">
				<label for="customer_id_filter"><?php 
    _e('Customer ID', 'foxyshop');
    ?>
</label><input type="text" name="customer_id_filter" id="customer_id_filter" value="<?php 
    echo $foxy_data['customer_id_filter'];
    ?>
" />
			</div>
			<div class="foxyshop_field_control">
				<label for="customer_email_filter"><?php 
    _e('Customer Email', 'foxyshop');
    ?>
</label><input type="text" name="customer_email_filter" id="customer_email_filter" value="<?php 
    echo $foxy_data['customer_email_filter'];
    ?>
" />
			</div>
			<div class="foxyshop_field_control">
				<label for="customer_first_name_filter"><?php 
    _e('Customer First Name', 'foxyshop');
    ?>
</label><input type="text" name="customer_first_name_filter" id="customer_first_name_filter" value="<?php 
    echo $foxy_data['customer_first_name_filter'];
    ?>
" />
			</div>
			<div class="foxyshop_field_control">
				<label for="customer_last_name_filter"><?php 
    _e('Customer Last Name', 'foxyshop');
    ?>
</label><input type="text" name="customer_last_name_filter" id="customer_last_name_filter" value="<?php 
    echo $foxy_data['customer_last_name_filter'];
    ?>
" />
			</div>

			<div style="clear: both;"></div>
			<button type="submit" id="foxyshop_search_submit" name="foxyshop_search_submit" class="button-primary" style="clear: both; margin-top: 10px;"><?php 
    _e('Search Records Now', 'foxyshop');
    ?>
</button>
			<button type="button" class="button" style="margin-left: 15px; margin-top: 10px;" onclick="document.location.href = 'edit.php?post_type=foxyshop_product&page=foxyshop_subscription_management';"><?php 
    _e('Reset Form', 'foxyshop');
    ?>
</button>

		</td></tr></tbody></table>


		</form>
		<script type="text/javascript" charset="utf-8">
		jQuery(document).ready(function($) {
			$(".foxyshop_date_field").datepicker({ dateFormat: 'yy-mm-dd' });
		});
		</script>

	<?php 
    if (!isset($_GET['foxyshop_search']) && defined('FOXYSHOP_AUTO_API_DISABLED')) {
        return;
    }
    $foxy_response = foxyshop_get_foxycart_data($foxy_data);
    $xml = simplexml_load_string($foxy_response, NULL, LIBXML_NOCDATA);
    if ((string) $xml->result == __('ERROR', 'foxyshop')) {
        echo '<h3>' . (string) $xml->messages->message . '</h3>';
        return;
    } else {
        ?>



		<form action="edit.php" method="get">
		<input type="hidden" name="foxyshop_search" value="1" />
		<input type="hidden" name="post_type" value="foxyshop_product" />
		<input type="hidden" name="page" value="foxyshop_subscription_management" />

		<?php 
        echo $foxyshop_hidden_input;
        foxyshop_api_paging_nav('subscriptions', 'top', $xml, $foxyshop_querystring);
        ?>

		<table cellpadding="0" cellspacing="0" border="0" class="wp-list-table widefat foxyshop-list-table" id="subscription_table">
			<thead>
				<tr>
					<th><span><?php 
        _e('Customer', 'foxyshop');
        ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
        _e('Start Date', 'foxyshop');
        ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
        _e('Next Date', 'foxyshop');
        ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
        _e('End Date', 'foxyshop');
        ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
        _e('Past Due', 'foxyshop');
        ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
        _e('Details', 'foxyshop');
        ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
        _e('Frequency', 'foxyshop');
        ?>
</span><span class="sorting-indicator"></span></th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<th><?php 
        _e('Customer', 'foxyshop');
        ?>
</th>
					<th><?php 
        _e('Start Date', 'foxyshop');
        ?>
</th>
					<th><?php 
        _e('Next Date', 'foxyshop');
        ?>
</th>
					<th><?php 
        _e('End Date', 'foxyshop');
        ?>
</th>
					<th><?php 
        _e('Past Due', 'foxyshop');
        ?>
</th>
					<th><?php 
        _e('Details', 'foxyshop');
        ?>
</th>
					<th><?php 
        _e('Frequency', 'foxyshop');
        ?>
</th>
				</tr>
			</tfoot>
			<tbody id="the-list">

		<?php 
        $holder = "";
        foreach ($xml->subscriptions->subscription as $subscription) {
            $sub_token = (string) $subscription->sub_token;
            $customer_id = (string) $subscription->customer_id;
            $customer_first_name = (string) $subscription->customer_first_name;
            $customer_last_name = (string) $subscription->customer_last_name;
            $start_date = (string) $subscription->start_date;
            $next_transaction_date = (string) $subscription->next_transaction_date;
            $end_date = (string) $subscription->end_date;
            $frequency = (string) $subscription->frequency;
            $past_due_amount = (string) $subscription->past_due_amount;
            $is_active = (string) $subscription->is_active;
            $product_name = "";
            if (version_compare($foxyshop_settings['version'], '0.7.0', ">")) {
                foreach ($subscription->transaction_template->transaction_details->transaction_detail as $transaction_detail) {
                    if ($product_name) {
                        $product_name .= "<br />";
                    }
                    $product_price = (double) $transaction_detail->product_price;
                    foreach ($transaction_detail->transaction_detail_options->transaction_detail_option as $transaction_detail_option) {
                        $product_price += (double) $transaction_detail_option->price_mod;
                    }
                    $product_name .= (string) $transaction_detail->product_name . ' ' . foxyshop_currency($product_price);
                }
            } else {
                // The 0.7.0 code had an extra transaction_template node which was removed in subsequent versions
                foreach ($subscription->transaction_template->transaction_template->transaction_details->transaction_detail as $transaction_detail) {
                    if ($product_name) {
                        $product_name .= "<br />";
                    }
                    $product_price = (double) $transaction_detail->product_price;
                    foreach ($transaction_detail->transaction_detail_options->transaction_detail_option as $transaction_detail_option) {
                        $product_price += (double) $transaction_detail_option->price_mod;
                    }
                    $product_name .= (string) $transaction_detail->product_name . ' ' . foxyshop_currency($product_price);
                }
            }
            if ($customer_first_name != "") {
                $customer_name = $customer_last_name . ', ' . $customer_first_name;
            } else {
                $customer_name = $customer_id;
            }
            echo '<tr rel="' . $sub_token . '">';
            echo '<td class="customer_name">';
            echo '<strong' . ($is_active == "0" ? ' class="strikethrough"' : '') . '><a href="#" class="view_detail">' . $customer_name . '</a></strong>';
            echo '<div class="row-actions">';
            echo '<span class="edit"><a title="' . __('Edit') . '" href="#" class="view_detail">' . __('Edit') . '</a> | </span>';
            echo '<span class="view_customer"><a href="edit.php?post_type=foxyshop_product&page=foxyshop_customer_management&customer_id_filter=' . $customer_id . '&foxyshop_search=1" title="' . __('Customer') . '">' . __('Customer') . '</a></span>';
            do_action('foxyshop_subscription_action_line', $subscription);
            echo '</div>';
            echo '</td>';
            echo '<td class="start_date">' . $start_date . '</td>';
            echo '<td class="next_transaction_date">' . $next_transaction_date . '</td>';
            echo '<td class="end_date">' . $end_date . '</td>';
            echo '<td class="past_due_amount">' . $past_due_amount . '</td>';
            echo '<td class="product_description">' . $product_name . '</td>';
            echo '<td class="frequency">' . $frequency . '</td>';
            echo "</tr>\n";
            $holder .= '<div class="detail_holder" id="holder_' . $sub_token . '">' . "\n";
            $holder .= '<form class="subscription_update_form" name="subscription_update_form_' . $sub_token . '" id="subscription_update_form_' . $sub_token . '" onsubmit="return false;">' . "\n";
            $holder .= '<div class="foxyshop_field_control">' . "\n";
            $holder .= '<label>' . __('Subscription Status', 'foxyshop') . '</label>' . "\n";
            $holder .= '<input type="radio" name="is_active" id="is_active_1_' . $sub_token . '" value="1"' . ($is_active == "1" ? ' checked="checked"' : '') . ' style="float: left; margin-top: 7px;" />' . "\n";
            $holder .= '<label for="is_active_1_' . $sub_token . '" style="width: 55px;">' . __('Active', 'foxyshop') . '</label>' . "\n";
            $holder .= '<input type="radio" name="is_active" id="is_active_0_' . $sub_token . '" value="0"' . ($is_active == "0" ? ' checked="checked"' : '') . ' style="float: left; margin-top: 7px;" />' . "\n";
            $holder .= '<label for="is_active_0_' . $sub_token . '">' . __('In-active', 'foxyshop') . '</label>' . "\n";
            $holder .= '</div>' . "\n";
            $holder .= '<div class="foxyshop_field_control">' . "\n";
            $holder .= '<label for="start_date_' . $sub_token . '">' . __('Start Date', 'foxyshop') . '</label>' . "\n";
            $holder .= '<input type="text" name="start_date" id="start_date_' . $sub_token . '" class="foxyshop_date_field" value="' . (string) $subscription->start_date . '" /><span>(YYYY-MM-DD)</span>' . "\n";
            $holder .= '</div>' . "\n";
            $holder .= '<div class="foxyshop_field_control">' . "\n";
            $holder .= '<label for="next_transaction_date_' . $sub_token . '">' . __('Next Transaction Date', 'foxyshop') . '</label>' . "\n";
            $holder .= '<input type="text" name="next_transaction_date" id="next_transaction_date_' . $sub_token . '" value="' . (string) $subscription->next_transaction_date . '" class="foxyshop_date_field" /><span>(YYYY-MM-DD)</span>' . "\n";
            $holder .= '</div>' . "\n";
            $holder .= '<div class="foxyshop_field_control">' . "\n";
            $holder .= '<label for="end_date_' . $sub_token . '">' . __('End Date', 'foxyshop') . '</label>' . "\n";
            $holder .= '<input type="text" name="end_date" id="end_date_' . $sub_token . '" value="' . $end_date . '" class="foxyshop_date_field" /><span>(YYYY-MM-DD)</span> <a href="#" onclick="jQuery(\'#end_date_' . $sub_token . '\').val(\'0000-00-00\'); this.blur(); return false;" class="button" style="margin: 5px 0 0 5px; float: left;">Never</a> <a href="#" onclick="jQuery(\'#end_date_' . $sub_token . '\').val(\'' . date("Y-m-d", strtotime("+1 day")) . '\'); this.blur(); return false;" class="button" style="margin: 5px 0 0 5px; float: left;">Tomorrow</a>' . "\n";
            $holder .= '</div>' . "\n";
            $holder .= '<div class="foxyshop_field_control">' . "\n";
            $holder .= '<label for="frequency_' . $sub_token . '">' . __('Frequency', 'foxyshop') . '</label>' . "\n";
            $holder .= '<input type="text" name="frequency" id="frequency_' . $sub_token . '" value="' . $frequency . '" /><span>(60d, 2w, 1m, 1y, .5m)</span>' . "\n";
            $holder .= '</div>' . "\n";
            $holder .= '<div class="foxyshop_field_control">' . "\n";
            $holder .= '<label for="past_due_amount_' . $sub_token . '">' . __('Past Due Amount', 'foxyshop') . '</label>' . "\n";
            $holder .= '<input type="text" name="past_due_amount" id="past_due_amount_' . $sub_token . '" value="' . $past_due_amount . '" onblur="foxyshop_check_number(this);" /><span>(0.00)</span>' . "\n";
            $holder .= '</div>' . "\n";
            $holder .= '<div class="foxyshop_field_control">' . "\n";
            $holder .= '<label for="update_url_' . $sub_token . '">' . __('Update URL', 'foxyshop') . '</label>' . "\n";
            $holder .= '<input type="text" name="update_url" id="update_url_' . $sub_token . '" value="https://' . $foxyshop_settings['domain'] . '/cart?sub_token=' . $sub_token . '&amp;empty=true&amp;cart=checkout" style="width: 390px;" onclick="this.select();" />' . "\n";
            $holder .= '</div>' . "\n";
            $holder .= '<div class="foxyshop_field_control">' . "\n";
            $holder .= '<label for="cancel_url_' . $sub_token . '">' . __('Cancellation URL', 'foxyshop') . '</label>' . "\n";
            $holder .= '<input type="text" name="cancel_url" id="cancel_url_' . $sub_token . '" value="https://' . $foxyshop_settings['domain'] . '/cart?sub_token=' . $sub_token . '&amp;empty=true&amp;cart=checkout&amp;sub_cancel=true" style="width: 390px;" onclick="this.select();" />' . "\n";
            $holder .= '</div>' . "\n";
            $holder .= '<div class="foxyshop_field_control">' . "\n";
            $holder .= '<label for="transaction_template_id_' . $sub_token . '">' . __('Transaction Template') . '</label>' . "\n";
            $holder .= '<select name="transaction_template_id" id="transaction_template_id_' . $sub_token . '">' . "\n";
            $holder .= '<option value="0">- - ' . __('Select Option Below', 'foxyshop') . ' - -</option>' . "\n";
            foreach ($subscription_product_array as $key => $val) {
                $holder .= '<option value="' . $val['id'] . '">' . $val['name'] . ' ' . foxyshop_currency($val['price']) . '</option>' . "\n";
            }
            $holder .= '</select>' . "\n";
            $holder .= '</div>' . "\n";
            $holder .= '<p style="padding-top: 5px; clear: both"><a href="#" class="subscription_save button-primary">' . __('Save Changes', 'foxyshop') . '</a> <a href="#" class="detail_close button">' . __('Cancel', 'foxyshop') . '</a></p>' . "\n";
            $holder .= '<input type="hidden" name="sub_token" value="' . $sub_token . '" />' . "\n";
            $holder .= '<input type="hidden" name="action" value="foxyshop_display_list_ajax_action" />' . "\n";
            $holder .= '<input type="hidden" name="foxyshop_action" value="subscription_modify" />' . "\n";
            $holder .= '<input type="hidden" name="security" value="' . wp_create_nonce("foxyshop-display-list-function") . '" />' . "\n";
            //Custom Attributes
            $holder .= foxyshop_manage_attributes($subscription->attributes, $sub_token, "subscription");
            $holder .= '</form>' . "\n";
            $holder .= '</div>' . "\n";
        }
        ?>
		</tbody>
		</table>

		<?php 
        foxyshop_api_paging_nav('subscriptions', 'bottom', $xml, $foxyshop_querystring);
        ?>
		</form>
	<?php 
    }
    ?>

	<div id="details_holder"><?php 
    echo $holder;
    ?>
</div>

	<script type="text/javascript" src="<?php 
    echo FOXYSHOP_DIR;
    ?>
/js/jquery.tablesorter.js"></script>
	<script type="text/javascript">
	jQuery(document).ready(function($){
		$(".foxyshop-list-table thead th").click(function() {
			$("#foxyshop-list-inline .detail_holder").appendTo("#details_holder");
			$("#foxyshop-list-inline").remove();
		});
		$(".foxyshop-list-table").tablesorter({
			'cssDesc': 'asc sorted',
			'cssAsc': 'desc sorted'
		});
		$(".view_detail").click(function() {
			var id = $(this).parents("tr").attr("rel");

			if ($("#foxyshop-list-inline #holder_" + id).length > 0) {
				$("#foxyshop-list-inline .detail_holder").appendTo("#details_holder");
				$("#foxyshop-list-inline").remove();
			} else {
				$("#details_holder select").prop('selectedIndex', 0);
				$("#foxyshop-list-inline .detail_holder").appendTo("#details_holder");
				$("#foxyshop-list-inline").remove();

				$(this).parents("tr").after('<tr id="foxyshop-list-inline"><td colspan="7"></td></tr>');
				$("#holder_"+id).appendTo("#foxyshop-list-inline td");
			}

			return false;
		});
		$(".detail_close").click(function() {
			$("#foxyshop-list-inline .detail_holder").appendTo("#details_holder");
			$("#foxyshop-list-inline").remove();
			return false;
		});
		$(".subscription_save").click(function() {
			var id = $(this).parents("form").children("input[name='sub_token']").val();
			$.post(ajaxurl, $(this).parents("form").serialize(), function(response) {

				$("#foxyshop-list-inline .detail_holder").appendTo("#details_holder");
				$("#foxyshop-list-inline").remove();

				if (response.indexOf("ERROR") < 0) {
					$("tr[rel='" + id + "']").css("background-color", "#FFFFE0").delay(500).animate({ backgroundColor: 'transparent' }, 500);
					if ($("#is_active_0_" + id).is(":checked")) {
						$("tr[rel='" + id + "'] td.customer_name strong").addClass("strikethrough");
					} else {
						$("tr[rel='" + id + "'] td.customer_name strong").removeClass("strikethrough");
					}
					$("tr[rel='" + id + "'] td.start_date").text($("#start_date" + id).val());
					$("tr[rel='" + id + "'] td.next_transaction_date").text($("#next_transaction_date_" + id).val());
					$("tr[rel='" + id + "'] td.end_date").text($("#end_date_" + id).val());
					$("tr[rel='" + id + "'] td.past_due_amount").text($("#past_due_amount_" + id).val());
					$("tr[rel='" + id + "'] td.frequency").text($("#frequency_" + id).val());
					if ($("#transaction_template_id_" + id).prop("selectedIndex") > 0) {
						$("tr[rel='" + id + "'] td.product_description").text($("#transaction_template_id_" + id + " option:selected").text());
					}
				} else {
					alert(response);
				}
			});
			return false;
		});

		<?php 
    foxyshop_manage_attributes_jquery('subscription');
    ?>

	});

	function foxyshop_format_number(num) { num = num.toString().replace(/\$|\,/g,''); if(isNaN(num)) num = "0"; sign = (num == (num = Math.abs(num))); num = Math.floor(num*100+0.50000000001); cents = num%100; num = Math.floor(num/100).toString(); if(cents<10) cents = "0" + cents; for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3)); return (((sign)?'':'-') + num + '.' + cents); }
	function foxyshop_check_number(el) { el.value = foxyshop_format_number(el.value); }

	</script>
	<?php 
    echo '</div>';
}
function foxyshop_productlink_shortcode($atts, $content = null)
{
    global $product;
    $original_product = $product;
    extract(shortcode_atts(array("name" => '', "variations" => '', "quantity" => '1'), $atts));
    $prod = foxyshop_get_product_by_name($name);
    if (!$prod || !$name) {
        return "";
    }
    $product = foxyshop_setup_product($prod);
    $write = foxyshop_product_link("", true, $variations, $quantity);
    $product = $original_product;
    return $write;
}
function foxyshop_inventory_management_page()
{
    global $foxyshop_settings, $wp_version;
    ?>
	<div class="wrap">
		<div class="icon32" id="icon-tools"><br></div>
		<h2>Manage Inventory Levels</h2>

		<?php 
    //Confirmation Saved
    if (isset($_GET['saved'])) {
        echo '<div class="updated"><p>' . __('Your New Inventory Levels Have Been Saved.') . '</p></div>';
    }
    //Import Completed
    if (isset($_GET['importcompleted'])) {
        echo '<div class="updated"><p>' . sprintf(__('Import completed: %s records updated.'), (int) $_GET['importcompleted']) . '</p></div>';
    }
    ?>

		<table cellpadding="0" cellspacing="0" border="0" class="wp-list-table widefat foxyshop-list-table" id="inventory_level" style="margin-top: 14px;">
			<thead>
				<tr>
					<th><span><?php 
    _e('ID');
    ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
    _e('Name');
    ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
    _e('Code');
    ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
    _e('Variation');
    ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
    _e('Update');
    ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
    _e('Stock Lvl');
    ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
    _e('Alert Lvl');
    ?>
</span><span class="sorting-indicator"></span></th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<th><?php 
    _e('ID');
    ?>
</th>
					<th><?php 
    _e('Name');
    ?>
</th>
					<th><?php 
    _e('Code');
    ?>
</th>
					<th><?php 
    _e('Variation');
    ?>
</th>
					<th><?php 
    _e('Update');
    ?>
</th>
					<th><?php 
    _e('Stock Lvl');
    ?>
</th>
					<th><?php 
    _e('Alert Lvl');
    ?>
</th>
				</tr>
			</tfoot>
			<tbody>
			<?php 
    $args = array('post_type' => 'foxyshop_product', 'post_status' => 'publish', 'numberposts' => "-1", "orderby" => "id", "order" => "ASC", "meta_key" => "_inventory_levels", "meta_compare" => "!=", "meta_value" => "");
    $product_list = get_posts($args);
    $exported = "ID\tName\tCode\tVariation\tInventory";
    $i = 0;
    $alternate = "";
    foreach ($product_list as $single_product) {
        $product = foxyshop_setup_product($single_product, true);
        $inventory_levels = get_post_meta($single_product->ID, '_inventory_levels', TRUE);
        if (!is_array($inventory_levels)) {
            $inventory_levels = array();
        }
        foreach ($inventory_levels as $ivcode => $iv) {
            $i++;
            if (!isset($iv['alert'])) {
                $iv['alert'] = $foxyshop_settings['inventory_alert_level'];
            }
            $inventory_alert = (int) ($iv['alert'] == '' ? $foxyshop_settings['inventory_alert_level'] : $iv['alert']);
            $inventory_count = str_replace(",", "", $iv['count']);
            $variation = "&nbsp;";
            foreach ($product['variations'] as $product_variation) {
                $product_variation1 = preg_split("/(\r\n|\n)/", $product_variation['value']);
                foreach ($product_variation1 as $product_variation2) {
                    if (strpos($product_variation2, "c:" . $ivcode) !== false) {
                        $variation = str_replace("*", "", substr($product_variation2, 0, strpos($product_variation2, "{")));
                    }
                }
            }
            $exported .= "\n";
            $exported .= $product['id'] . "\t";
            $exported .= str_replace("\t", "", $product['name']) . "\t";
            $exported .= str_replace("\t", "", $ivcode) . "\t";
            $exported .= str_replace("\t", "", $variation) . "\t";
            $exported .= $inventory_count;
            $grade = "A";
            if ($inventory_count <= $inventory_alert) {
                $grade = "X";
            }
            if ($inventory_count <= 0) {
                $grade = "U";
            }
            echo '<tr>' . "\n";
            echo '<td><strong>' . $product['id'] . '</strong></td>' . "\n";
            echo '<td><strong><a href="post.php?post=' . $product['id'] . '&action=edit" tabindex="1">' . $product['name'] . '</a></strong></td>' . "\n";
            echo '<td>' . $ivcode . '</td>' . "\n";
            echo '<td>' . $variation . '</td>' . "\n";
            //The Form
            echo '<td>';
            echo '<form>';
            echo '<input type="hidden" name="original_count_' . $i . '" id="original_count_' . $i . '" value="' . $inventory_count . '" />';
            echo '<input type="hidden" name="productid_' . $i . '" id="productid_' . $i . '" value="' . $single_product->ID . '" />';
            echo '<input type="hidden" name="code_' . $i . '" id="code_' . $i . '" value="' . $ivcode . '" />';
            echo '<input type="text" name="new_count_' . $i . '" id="new_count_' . $i . '" value="' . $inventory_count . '" data-id="' . $i . '" class="inventory_update_width" autocomplete="off" />';
            echo '<div class="foxyshop_wait" id="wait_' . $i . '"></div>';
            echo "</form>\n";
            echo "</td>\n";
            echo '<td id="current_inventory_' . $i . '" class="inventory' . $grade . '">' . $inventory_count . '</td>' . "\n";
            echo '<td id="current_inventory_alert_' . $i . '">' . $inventory_alert . '</td>' . "\n";
            echo '</tr>' . "\n";
        }
    }
    ?>
			</tbody>
		</table>

		<br /><br />

		<?php 
    $export_icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACH0lEQVR4nKWSTUhUYRSGnzv33snRdEY0M0dJaSDTzRBkiZhZkNCiTZC1CIQQahFEq9q3aBFRqzZFkUQRuayFEKlZhv0ZGsUw6VD4N5rj5KjT3O9+p4VphCVKZ3PO5nl4Obzwn2M0tLSdBE6vk7vedfvETQDjQOs9qT1Suy66t72XEn8y2Hb51KglIiwk59YlEJHl29paMMPEm4/4sn2rQspOkAi2Exg9TGizQ372/ByAVZSX5tihbYTD4X/C8fkxWp80URZSjA1/JnesGiZ2LSZYS+Svs0MEcjZSWDTNpiKDl11vSXmmioHkqoKekQ4exe4ykorxLT1Dmcpg2x727PXR3Rn90PigouSvAldcLjxrITbXjzdvGl+xy3Y/OMrGMAy8tkFDY8Ds6ZqN/iEQEdJqnrOdzUwuRCktzKK8oA7LtDFNzZDqxDAMDMNDqRlGpC/LAtBa47ouIsKtwatMJL+wu6Se0fQkjwfekevNI60WCFULhqEoZAcPu185rnZLPUsCpRRKKSKJQcr9lbye6qcpeJz7+19wo66D6vydJGc8+N0Kevri5KS21Dw9Ohy3AJRSOI6D1hqfmY3X3sD58DVMLLQStHZIOd+pyjrIwKcogalKiuM1w9DNCsG5qkuICNrVKFForRERzoQucid2hXBuPbOJIBmc3z2IRCJkMhlEZBH+BS1Vdmnvoxl+wHMdWX78omA8SWT8/Vo6tWJ+AquVAo19QSjUAAAAAElFTkSuQmCC";
    ?>
		<form method="post" name="foxyshop_inventory_import_form" action="edit.php?post_type=foxyshop_product&amp;page=foxyshop_inventory_management_page">
		<table class="widefat">
			<thead>
				<tr>
					<th><img src="<?php 
    echo $export_icon;
    ?>
" alt="" /><?php 
    _e('Import New Inventory Values');
    ?>
</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>
						<p>
							Copy and paste these values into Excel. Make your changes, then copy and paste back in and click update.<br />
							You can also add new inventory levels by using the template to add new rows with code and inventory fields.
						</p>
						<textarea id="foxyshop_inventory_updates" name="foxyshop_inventory_updates" wrap="auto" style="float: left; width:650px;height: 200px;"><?php 
    echo $exported;
    ?>
</textarea>
						<div style="clear: both;"></div>
						<p><input type="submit" class="button-primary" value="<?php 
    _e('Update Inventory Values');
    ?>
" /></p>
					</td>
				</tr>
			</tbody>
		</table>
		<?php 
    wp_nonce_field('import-foxyshop-inventory-updates');
    ?>
		</form>


	</div>

<script type="text/javascript" src="<?php 
    echo FOXYSHOP_DIR;
    ?>
/js/jquery.tablesorter.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
	$(".inventory_update_width").blur(function() {
		current_field_id = $(this).attr("id");
		current_id = $("#" + current_field_id).attr("data-id");
		new_count = $("#" + current_field_id).val();

		$("#" + current_field_id).val(new_count);
		$("#" + current_field_id).parents("tr").removeClass("inventory_update_width_highlight");

		if (new_count != $("#original_count_" + current_id).val()) {

			var data = {
				action: 'save_inventory_values',
				"_wpnonce": "<?php 
    echo wp_create_nonce('update-foxyshop-inventory');
    ?>
",
				"code": $("#code_" + current_id).val(),
				"product_id": $("#productid_" + current_id).val(),
				"new_count": new_count
			};

			$("#wait_" + current_id).addClass("waiting");
			$.post(ajaxurl, data, function() {
				$("#wait_" + current_id).removeClass("waiting");
				$("#original_count_" + current_id).val(new_count);
				$("#current_inventory_" + current_id).text(new_count);
				if (new_count <= 0) {
					$("#current_inventory_" + current_id).removeClass().addClass("inventoryU");
				} else if (new_count <= parseInt($("#current_inventory_alert_" + current_id).text())) {
					$("#current_inventory_" + current_id).removeClass().addClass("inventoryX");
				} else {
					$("#current_inventory_" + current_id).removeClass().addClass("inventoryA");
				}
			});
		}
  	});
	$(".inventory_update_width").keypress(function(e) {
		if (e.which == 13) {
			$(this).trigger("blur");
			return false;
		}
	});
	$(".inventory_update_width").focus(function() {
		$(this).parents("tr").addClass("inventory_update_width_highlight");
	});
	$("#inventory_level").tablesorter({
		'cssDesc': 'asc sorted',
		'cssAsc': 'desc sorted'
	});


});
function foxyshop_format_number_single(num) { num = num.toString().replace(/\$|\,/g,''); if(isNaN(num)) num = "0"; sign = (num == (num = Math.abs(num))); num = Math.floor(num*100+0.50000000001); cents = num%100; num = Math.floor(num/100).toString(); if(cents<10) cents = "0" + cents; for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3)); return (((sign)?'':'-') + num); }
</script>


<?php 
}
function foxyshop_google_products_page()
{
    global $foxyshop_settings, $product;
    $local_products = array();
    $google_icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABh0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzT7MfTgAAABZ0RVh0Q3JlYXRpb24gVGltZQAwNy8xNS8xMMjfMS0AAAQRdEVYdFhNTDpjb20uYWRvYmUueG1wADw/eHBhY2tldCBiZWdpbj0iICAgIiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+Cjx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDQuMS1jMDM0IDQ2LjI3Mjk3NiwgU2F0IEphbiAyNyAyMDA3IDIyOjM3OjM3ICAgICAgICAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp4YXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iPgogICAgICAgICA8eGFwOkNyZWF0b3JUb29sPkFkb2JlIEZpcmV3b3JrcyBDUzM8L3hhcDpDcmVhdG9yVG9vbD4KICAgICAgICAgPHhhcDpDcmVhdGVEYXRlPjIwMTAtMDctMTVUMTk6MDU6MDFaPC94YXA6Q3JlYXRlRGF0ZT4KICAgICAgICAgPHhhcDpNb2RpZnlEYXRlPjIwMTAtMDctMTVUMTk6MTg6MDBaPC94YXA6TW9kaWZ5RGF0ZT4KICAgICAgPC9yZGY6RGVzY3JpcHRpb24+CiAgICAgIDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiCiAgICAgICAgICAgIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWVudHMvMS4xLyI+CiAgICAgICAgIDxkYzpmb3JtYXQ+aW1hZ2UvcG5nPC9kYzpmb3JtYXQ+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDBS8igAAAJlSURBVDiNfY9PSFRhFMV/33uvcRzfzKijmQr5jyzFciGFldlGiRCCoAIhqFy0CdpE0Sao3Ei1CtdBuAjDhdBSQiw3SaM5IBpkJTlRaprydJznvO+2sNRJ68CBy733nHuuisfjLVNf4k+fReciI1+TWCa0HLA5VhIAwHOcBR48sMyVlSC/IUohhhHLTCTOWfPz80+GZndFuqdCaAxYg/GYoqd2L1UFGQA50tCwRGfnHz3aMHBs+9DP7OxuKxAI7P64Ksx5gqDWNzx49CZJ75Wi9Yv19SF97x5bEwQcB0+pWsu2bUIBF9GarXg37WwKEgm0UmlzXzJJ1vKyYZimSWNFELSXRncttRl5eJj56hKGT1cxfLqKlXAmIoLyPCyA5v1hmitt+t4vbogayrI36ktHRni+bxpPvPXGBQiuMna7z3LNoqard+srcjlTk8v3pSSLiRSNFUEenysn5DfpmuyifeIhIpL2gmuxe7TMX6jybg7KxeOlXD+RT1mub3NBu7S/a6cj1rF5+S9E1iKovBsD8kNnbTT7r1VysiJIa38r3Z+6dxRuGKQiGOgUWzk05eBql5qcGsJWGDT/pSq79VLOHy3nVHUO4UyT4rCPPaH1V+LLcdoG2uib7vtnAhWb/CoHywsBEOcDOB/AXUAVnwXTj6tdml40MfhtcGeDufiE5Cz1IrOv06f+AozqO6isUqKzUQ73HN5u4EUw9HgHMvMKRNKZ+IYeuw+Soi6/DtuyQdhGQ7tLo6Jlu8FvE3E+A+BpL02oRKFEvTWUyOVkyoztaGL4Uf4CojNREmuJNHGGlzHk077WXxkiNLGzmnadAAAAAElFTkSuQmCC";
    $debug_querystring = isset($_GET['debug']) ? "&amp;debug=1" : "";
    ?>
	<?php 
    if (!$foxyshop_settings['google_product_auth']) {
        ?>
		<div class="wrap">
		<div class="icon32" id="icon-tools"><br></div>
		<h2>Google Products Management - Authentication Required</h2>

		<table class="widefat" style="margin-top: 14px;">
			<thead>
				<tr>
					<th><img src="<?php 
        echo $google_icon;
        ?>
" alt="" /><?php 
        _e("Google Authentication Required", 'foxyshop');
        ?>
</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>
						<p>To view and manage your Google Products you need to log in to your Google Account. Your username and password are not saved withing FoxyShop, but are passed directly to Google for authentication. If authenticated, Google will issue an auth key that will be stored with your store settings. This auth key will expire after two weeks at which point you will need to authenticate again.</p>

						<form onsubmit="return false;" autocomplete="off" style="display: block; margin-bottom: 10px;">

						<div class="foxyshop_field_control">
							<label for="Email"><?php 
        echo __('Email');
        ?>
</label>
							<input type="text" id="Email" name="Email" value="" />
						</div>

						<div class="foxyshop_field_control">
							<label for="Passwd"><?php 
        echo __('Password');
        ?>
</label>
							<input type="password" id="Passwd" name="Passwd" value="" />
						</div>

						<div style="clear: both; height: 4px;"></div>

						<button class="button-primary" id="authnow">Authenticate Now</button>
						</form>

						<div id="error" style="color: red; margin-top: 10px; font-weight: bold; display: none;"></div>


						<script type="text/javascript">
						jQuery(document).ready(function($){

							$("#authnow").click(function() {
								var data = {
									action: 'foxyshop_set_google_auth',
									security: '<?php 
        echo wp_create_nonce("foxyshop-ajax-set-google-auth");
        ?>
',
									Email: $("#Email").val(),
									Passwd: $("#Passwd").val()
								};
								$("#error").hide();
								$.post(ajaxurl, data, function(response) {
									if (!response) {
										$("#error").text("Error: No Response").show();
									} else if (response == "Error") {
										$("#error").text("Error: Login Failed. Please try again.").show();
									} else if (response == "Success") {
										$("#error").hide();
										location.reload();
									} else {
										$("#error").text("Error: " + response).show();
									}
								});
							});
						});
						</script>


					</td>
				</tr>
			</tbody>
		</table>


	<?php 
    } else {
        ?>
		<div class="wrap">
		<div class="icon32" id="icon-tools"><br></div>
		<h2>Google Products Management <a class="<?php 
        if (version_compare(get_bloginfo('version'), '3.2', "<")) {
            echo "button ";
        }
        ?>
add-new-h2" href="edit.php?post_type=foxyshop_product&amp;page=foxyshop_google_products_page&amp;foxyshop_google_products_update=1&amp;googleprodlogout=1&amp;_wpnonce=<?php 
        echo wp_create_nonce('gp1');
        ?>
">De-Authenticate</a></h2>
		<?php 
        //Display Confirmations and Errors
        if (isset($_GET['error'])) {
            echo '<div class="error"><p><strong>Error!</strong><br /><ul style="margin: 0 10px;">';
            $error_list = explode("||", $_GET['error']);
            foreach ($error_list as $the_error) {
                if ($the_error) {
                    echo "<li style=\"list-style: disc inside none;\">{$the_error}</li>\n";
                }
            }
            echo '</ul></p></div>';
        } elseif (isset($_GET['success'])) {
            echo '<div class="updated"><p>' . __('Operation completed successfully.') . '</p></div>';
        }
        //Get All Local Product ID's
        $args = array('post_type' => 'foxyshop_product', 'post_status' => 'publish', 'numberposts' => "-1", "orderby" => "id", "order" => "ASC", "meta_key" => "_google_product_category", "meta_compare" => "!=", "meta_value" => "");
        $product_list = get_posts($args);
        foreach ($product_list as $single_product) {
            $local_products[] = $single_product->ID;
        }
        //Get Feed
        $header_array = array("Authorization: GoogleLogin auth=" . $foxyshop_settings['google_product_auth']);
        $url = "https://content.googleapis.com/content/v1/" . $foxyshop_settings['google_product_merchant_id'] . "/items/products/schema?performance.start=" . date("Y-m-d", strtotime("-30 days")) . "&max-results=250&performance.end=" . date("Y-m-d", strtotime("now"));
        if (isset($_GET['nextlink'])) {
            $url = $_GET['nextlink'];
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $response = trim(curl_exec($ch));
        $xml = preg_replace("/(<\\/?)(\\w+):([^>]*>)/", "\$1\$2\$3", $response);
        $xml = simplexml_load_string($xml, NULL, LIBXML_NOCDATA);
        //Check For Next Link
        $nextlink = "";
        foreach ($xml->link as $link) {
            $thisone = 0;
            foreach ($link->attributes() as $a => $b) {
                if ($a == "rel" && $b == "next") {
                    $thisone = 1;
                }
                if ($thisone && $a == "href") {
                    $nextlink = $b;
                }
            }
        }
        //Token Has Expired, Remove and Restart Authentication
        if ((string) $xml->BODY->H1 == "Token invalid" || (string) $xml->BODY->H1 == "Token expired") {
            $foxyshop_settings['google_product_auth'] = "";
            update_option("foxyshop_settings", $foxyshop_settings);
            $local_products = array();
            echo '<div class="updated"><p>' . __('Authentication Failed. It appears that your authentication has expired. This happens every two weeks. Please login again.') . '</p></div>';
            echo '<p><a href="edit.php?post_type=foxyshop_product&amp;page=foxyshop_google_products_page" class="button">Login Now</a></p>';
        } else {
            ?>

		<form action="edit.php?post_type=foxyshop_product&page=foxyshop_google_products_page&foxyshop_manage_google_feed=1" method="post">

		<table cellpadding="0" cellspacing="0" border="0" class="wp-list-table widefat foxyshop-list-table" id="product_feed_view" style="margin-top: 14px;">
			<thead>
				<tr>
					<th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox"></th>
					<th class="column-id"><span><?php 
            _e('ID', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
            _e('Name', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
            _e('Image', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
            _e('Expiration', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
					<th><span><?php 
            _e('Performance', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<th class="manage-column column-cb check-column" style="" scope="col"><input type="checkbox"></th>
					<th><?php 
            _e('ID', 'foxyshop');
            ?>
</th>
					<th><?php 
            _e('Name', 'foxyshop');
            ?>
</th>
					<th><?php 
            _e('Image', 'foxyshop');
            ?>
</th>
					<th><?php 
            _e('Expiration', 'foxyshop');
            ?>
</th>
					<th><?php 
            _e('Performance', 'foxyshop');
            ?>
</th>
				</tr>
			</tfoot>
			<tbody>
		<?php 
            if (count($xml->entry) == 0) {
                echo '<tr class="no-items"><td colspan="6" class="colspanchange">No entries found.</td></tr>';
            }
            foreach ($xml->entry as $entry) {
                $expiration_date = (string) $entry->scexpiration_date;
                $expiration_date = substr($expiration_date, 0, strpos($expiration_date, "T"));
                $clicks = 0;
                foreach ($entry->scperformance->scdatapoint as $scdatapoint) {
                    $clicks += $scdatapoint->attributes()->clicks;
                }
                $google_product_id = (string) $entry->scid;
                if (in_array($google_product_id, $local_products)) {
                    $local_products = array_diff($local_products, array($google_product_id));
                    $unmatched_text = "";
                } else {
                    $unmatched_text = "<br /><em>" . __('Unmatched!', 'foxyshop') . "</em>\n";
                }
                echo '<tr>' . "\n";
                echo '<th class="check-column" scope="row"><input type="checkbox" value="' . $google_product_id . '" name="post[]"></th>' . "\n";
                echo '<td><strong>' . $google_product_id . '</strong>' . $unmatched_text . '</td>' . "\n";
                if ($unmatched_text == "") {
                    echo '<td><strong><a href="post.php?post=' . $google_product_id . '&action=edit">' . (string) $entry->title . '</a></strong>';
                    echo '<div class="row-actions">';
                    echo '<span><a href="edit.php?foxyshop_manage_google_feed=1&amp;editid=' . $google_product_id . $debug_querystring . '&amp;_wpnonce=' . wp_create_nonce("manage-the-google-feed-settings") . '" class="update_google_product" rel="' . $google_product_id . '">' . __('Renew/Update', 'foxyshop') . '</a> | </span>';
                    echo '<span class="delete"><a href="edit.php?foxyshop_manage_google_feed=1&amp;deleteid=' . $google_product_id . $debug_querystring . '&amp;_wpnonce=' . wp_create_nonce("manage-the-google-feed-settings") . '" class="delete_google_product" rel="' . $google_product_id . '">' . __('Delete', 'foxyshop') . '</a></span>';
                    echo '</div>';
                    echo '</td>' . "\n";
                } else {
                    echo '<td><strong><a href="#" onclick="return false;">' . (string) $entry->title . '</a></strong>';
                    echo '<div class="row-actions">';
                    echo '<span class="delete"><a href="edit.php?foxyshop_manage_google_feed=1&amp;deleteid=' . $google_product_id . $debug_querystring . '&amp;_wpnonce=' . wp_create_nonce("manage-the-google-feed-settings") . '" class="delete_google_product" rel="' . $google_product_id . '">' . __('Delete', 'foxyshop') . '</a></span>';
                    echo '</div>';
                    echo '</td>' . "\n";
                }
                if ((string) $entry->scimage_link) {
                    echo '<td><img src="' . (string) $entry->scimage_link . '" class="productfeedimage" /></td>' . "\n";
                } else {
                    echo '<td>&nbsp;</td>' . "\n";
                }
                echo '<td>' . $expiration_date . '</td>' . "\n";
                echo '<td>' . $clicks . ' Click' . ($clicks != 1 ? 's' : '') . '</td>' . "\n";
                echo '</tr>' . "\n";
            }
            ?>
			</tbody>
		</table>
		<div style="padding-top: 10px;">
			<button type="submit" class="button" name="update_checked_google_products" value="1" id="update_checked_google_products">Update/Renew Checked <?php 
            echo FOXYSHOP_PRODUCT_NAME_PLURAL;
            ?>
</button>
			&nbsp;&nbsp;&nbsp;
			<button type="submit" class="button" name="delete_checked_google_products" value="1" id="delete_checked_google_products">Delete Checked <?php 
            echo FOXYSHOP_PRODUCT_NAME_PLURAL;
            ?>
</button>

			<?php 
            if ($nextlink) {
                echo '<a href="edit.php?post_type=foxyshop_product&page=foxyshop_google_products_page&nextlink=' . urlencode($nextlink) . '" class="button" style="float: right;">Next Page</a>';
            }
            ?>

		</div>
		<input type="hidden" name="foxyshop_run_the_xml" value="1" />
		<?php 
            wp_nonce_field('manage-the-google-feed-settings');
            ?>
		<?php 
            if (isset($_GET['debug'])) {
                echo '<input type="hidden" name="debug" value="1" />';
            }
            ?>
		</form>
		<?php 
        }
        //Display All Unmatched Products
        $meta_query = array('relation' => 'OR', array('key' => '_google_product_category', 'value' => "", 'compare' => '!='));
        $args = array('post_type' => 'foxyshop_product', 'post_status' => 'publish', 'numberposts' => "100", "orderby" => "id", "order" => "ASC", "meta_query" => $meta_query);
        $product_list = get_posts($args);
        if ($product_list) {
            ?>
			<h2 style="padding: 100px 0 0 0;">Available, Unmatched <?php 
            echo FOXYSHOP_PRODUCT_NAME_PLURAL;
            ?>
 to Add</h2>
			<p style="margin: 0;">In order to appear in this list, <?php 
            echo strtolower(FOXYSHOP_PRODUCT_NAME_PLURAL);
            ?>
 must have a "Google Product Category" attribute.</p>

			<form action="edit.php?post_type=foxyshop_product&page=foxyshop_google_products_page&foxyshop_manage_google_feed=1" method="post">

			<table cellpadding="0" cellspacing="0" border="0" class="wp-list-table widefat foxyshop-list-table" id="available_product_view" style="margin-top: 14px;">
				<thead>
					<tr>
						<th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox"></th>
						<th class="column-id"><span><?php 
            _e('ID');
            ?>
</span><span class="sorting-indicator"></span></th>
						<th><span><?php 
            _e('Name', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
						<th><span><?php 
            _e('Code', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
						<th><span><?php 
            _e('Image', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
						<th><span><?php 
            _e('Price', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
						<th><span><?php 
            _e('Date', 'foxyshop');
            ?>
</span><span class="sorting-indicator"></span></th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<th class="manage-column column-cb check-column" style="" scope="col"><input type="checkbox"></th>
						<th><?php 
            _e('ID', 'foxyshop');
            ?>
</th>
						<th><?php 
            _e('Name', 'foxyshop');
            ?>
</th>
						<th><?php 
            _e('Code', 'foxyshop');
            ?>
</th>
						<th><?php 
            _e('Image', 'foxyshop');
            ?>
</th>
						<th><?php 
            _e('Price', 'foxyshop');
            ?>
</th>
						<th><?php 
            _e('Data', 'foxyshop');
            ?>
</th>
					</tr>
				</tfoot>
				<tbody>
			<?php 
            $none_available = 1;
            foreach ($product_list as $single_product) {
                $product = foxyshop_setup_product($single_product);
                $google_product_listed = (int) get_post_meta($product['id'], '_google_product_listed', TRUE);
                if ($google_product_listed > strtotime("now") && !isset($_GET['debug'])) {
                    continue;
                }
                $none_available = 0;
                if (!isset($google_product_id)) {
                    $google_product_id = "";
                }
                $salestartdate = get_post_meta($product['id'], '_salestartdate', TRUE);
                $saleenddate = get_post_meta($product['id'], '_saleenddate', TRUE);
                if ($salestartdate == '999999999999999999') {
                    $salestartdate = 0;
                }
                if ($saleenddate == '999999999999999999') {
                    $saleenddate = 0;
                }
                $originalprice = $product['originalprice'];
                $saleprice = get_post_meta($product['id'], '_saleprice', true);
                if ($saleprice > 0) {
                    $beginningOK = strtotime("now") > $salestartdate;
                    $endingOK = strtotime("now") < $saleenddate + 86400 || $saleenddate == 0;
                    if ($beginningOK && $endingOK || $salestartdate == 0 && $saleenddate == 0) {
                        $pricewrite = '<span style="text-decoration: line-through; margin-right: 10px;">' . foxyshop_currency($originalprice) . '</span><span style="color: red;">' . foxyshop_currency($saleprice) . '</span>';
                    } else {
                        $pricewrite = foxyshop_currency($originalprice);
                    }
                } else {
                    $pricewrite = foxyshop_currency($originalprice);
                }
                echo '<tr>' . "\n";
                echo '<th class="check-column" scope="row"><input type="checkbox" value="' . $product['id'] . '" name="post[]"></th>' . "\n";
                echo '<td><strong>' . $product['id'] . '</strong></td>' . "\n";
                echo '<td><strong><a href="post.php?post=' . $product['id'] . '&action=edit">' . $product['name'] . '</a></strong>';
                echo '<div class="row-actions">';
                echo '<span><a href="edit.php?foxyshop_manage_google_feed=1&amp;addid=' . $product['id'] . $debug_querystring . '&amp;_wpnonce=' . wp_create_nonce("manage-the-google-feed-settings") . '" class="insert_google_product" rel="' . $google_product_id . '">' . __('Add To Google Products Feed', 'foxyshop') . '</a></span>';
                echo '</div>';
                echo '</td>' . "\n";
                echo '<td>' . $product['code'] . '</td>' . "\n";
                echo '<td><img src="' . foxyshop_get_main_image() . '" class="productfeedimage" /></td>' . "\n";
                echo '<td>' . $pricewrite . '</td>' . "\n";
                echo '<td>' . Date("Y-m-d", strtotime($single_product->post_date)) . '</td>' . "\n";
                echo '</tr>' . "\n";
            }
            if ($none_available) {
                echo '<tr><td colspan="7"><em>No ' . FOXYSHOP_PRODUCT_NAME_PLURAL . ' Available.</em></td></tr>' . "\n";
            }
            ?>
				</tbody>
			</table>
			<input type="hidden" name="foxyshop_run_the_xml" value="1" />
			<?php 
            wp_nonce_field('manage-the-google-feed-settings');
            ?>
			<?php 
            if (isset($_GET['debug'])) {
                echo '<input type="hidden" name="debug" value="1" />';
            }
            ?>
			<div style="padding-top: 10px;">
				<button type="submit" class="button" name="add_checked_google_products" value="1" id="add_checked_google_products">Add Checked <?php 
            echo FOXYSHOP_PRODUCT_NAME_PLURAL;
            ?>
 to Google</button>
			</div>
			</form>



		<?php 
        }
        ?>


	<?php 
    }
    ?>

		<br /><br /><br /><br /><br /><br />
	<table class="widefat">
		<thead>
			<tr>
				<th><img src="<?php 
    echo $google_icon;
    ?>
" alt="" /><?php 
    _e("Create Manual Export File");
    ?>
</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<p>If you would like to <a href="http://www.google.com/merchants" target="_blank">submit your products to Google</a>, you may do so by creating a product feed using this tool. Make sure that you check the option that <a href="http://www.google.com/support/merchants/bin/answer.py?answer=160037" target="_blank">enables double quotes.</a> You also need to make sure that the '_google_product_category' custom field is filled out for each product.</p>
					<form action="edit.php" method="get">
						<input type="hidden" name="post_type" value="foxyshop_product">
						<input type="hidden" name="create_google_product_feed" value="1">
						<p>
							<a href="#" onclick="jQuery('#db_export_settings').show(); jQuery(this).hide(); return false;">Export just part of your product database</a>
							<div id="db_export_settings" style="display: none;">
								<label for="records_per_page">How May Records Per Page?</label><input type="text" id="records_per_page" name="records_per_page" value="-1"> (-1 for all)<br>
								<label for="records_page">Which Page To Export?</label><input type="text" id="records_page" name="records_page" value="1">
							</div>
							<button type="submit" name="submitnow" id="submitnow" value="1" class="button-primary">Create Google Product Feed</button>
							<button type="submit" name="amazon_version" id="amazon_version" value="1" class="button-primary">Create Amazon Product Ads Feed</button>
						</p>
					</form>
				</td>
			</tr>
		</tbody>
	</table>



	</div>


<script type="text/javascript" src="<?php 
    echo FOXYSHOP_DIR;
    ?>
/js/jquery.tablesorter.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
	$("#product_feed_view").tablesorter({
		'cssDesc': 'asc sorted',
		'cssAsc': 'desc sorted',
		'headers': { 0: { sorter: false} }
	});
});
</script>

	<?php 
}
function foxyshop_subscription_template($id)
{
    global $product, $foxyshop_settings;
    $original_product = $product;
    $template_id = get_posts(array('post_type' => 'foxyshop_product', 'p' => $id));
    foreach ($template_id as $the_template_id) {
        $product = foxyshop_setup_product($the_template_id);
        if ($product['quantity_min'] == 0) {
            $product['quantity_min'] = 1;
        }
        if (!$product['category']) {
            $product['category'] = "DEFAULT";
        }
        if (!$product['weight']) {
            $product['weight'] = "0";
        }
        $product = apply_filters("foxyshop_template_transaction_product_array", $product);
        $xml = "<transaction_template>\n";
        $xml .= "\t<custom_fields />\n";
        $xml .= "\t<discounts />\n";
        $xml .= "\t<transaction_details>\n";
        $xml .= "\t<transaction_detail>\n";
        $xml .= "\t\t<product_name><![CDATA[" . $product['name'] . "]]></product_name>\n";
        $xml .= "\t\t<product_price><![CDATA[" . $product['price'] . "]]></product_price>\n";
        $xml .= "\t\t<product_quantity><![CDATA[" . $product['quantity_min'] . "]]></product_quantity>\n";
        $xml .= "\t\t<product_weight><![CDATA[" . $product['weight'] . "]]></product_weight>\n";
        $xml .= "\t\t<product_code><![CDATA[" . $product['code'] . "]]></product_code>\n";
        $xml .= "\t\t<image><![CDATA[" . foxyshop_get_main_image() . "]]></image>\n";
        $xml .= "\t\t<url><![CDATA[" . $product['url'] . "]]></url>\n";
        $xml .= "\t\t<category_code><![CDATA[" . $product['category'] . "]]></category_code>\n";
        $xml .= "\t\t<transaction_detail_options />\n";
        $xml .= "\t</transaction_detail>\n";
        $xml .= "\t</transaction_details>\n";
        $xml .= "</transaction_template>";
        $xml = apply_filters("foxyshop_template_transaction_xml", $xml, $product);
    }
    $product = $original_product;
    return $xml;
}
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_custom_sort()
{
    global $wpdb, $product;
    $parentID = 0;
    $success = "";
    //Reset Order
    if (isset($_GET['upgrade_menu_order'])) {
        foxyshop_upgrade_menu_order();
    }
    if (isset($_POST['submit_new_product_order'])) {
        if (check_admin_referer('update-foxyshop-sorting-options')) {
            $success = foxyshop_update_order();
        }
    } elseif (isset($_POST['revert_product_order'])) {
        if (check_admin_referer('update-foxyshop-sorting-options')) {
            $success = foxyshop_revert_order();
        }
    }
    ?>

	<div class="wrap">
	<div class="icon32" id="icon-tools"><br></div>
	<h2><?php 
    echo sprintf(__('Custom %s Order'), FOXYSHOP_PRODUCT_NAME_SINGULAR);
    ?>
</h2>
	<?php 
    if ($success) {
        echo $success;
    }
    ?>

	<?php 
    $product_categories = get_terms('foxyshop_categories', 'hide_empty=0&hierarchical=0&orderby=name&order=ASC');
    $categoryID = 0;
    if (isset($_REQUEST['categoryID'])) {
        $categoryID = $_REQUEST['categoryID'];
    }
    if ($product_categories) {
        echo '<p>' . sprintf(__("Select a category from the drop down to order the %s in that category.", 'foxyshop'), strtolower(FOXYSHOP_PRODUCT_NAME_PLURAL)) . "</p>\n";
        echo '<form name="form_product_category_order" method="post" action="edit.php?post_type=foxyshop_product&page=foxyshop_custom_sort">';
        echo '<select name="categoryID" id="categoryID">' . "\n";
        echo '<option value="0"' . ($categoryID == 0 ? ' selected="selected"' : '') . '>' . __('All', 'foxyshop') . ' ' . FOXYSHOP_PRODUCT_NAME_PLURAL . '</option>' . "\n";
        foreach ($product_categories as $cat) {
            echo '<option value="' . esc_attr($cat->term_id) . '"' . ($categoryID == $cat->term_id ? ' selected="selected"' : '') . '>' . esc_html($cat->name) . ' (' . $cat->count . ')' . '</option>' . "\n";
        }
        echo '</select>' . "\n";
        echo '<input type="submit" name="btnSubPages" class="button" id="btnSubPages" value="' . __('Select Category', 'foxyshop') . '" /></form>';
    } else {
        $categoryID = 0;
    }
    if ($categoryID >= 0) {
        if ($categoryID > 0) {
            $term = get_term_by('id', $categoryID, "foxyshop_categories");
            $current_category_name = $term->name;
            $args = array('post_type' => 'foxyshop_product', 'foxyshop_categories' => $term->slug, 'posts_per_page' => -1, 'orderby' => "meta_value_num", "meta_key" => "_foxyshop_menu_order_" . $categoryID, 'order' => "ASC", 'post__not_in' => foxyshop_hide_children_array($categoryID));
        } else {
            $current_category_name = __("All", 'foxyshop') . ' ' . FOXYSHOP_PRODUCT_NAME_PLURAL;
            $args = array('post_type' => 'foxyshop_product', 'numberposts' => -1, 'orderby' => "menu_order", 'order' => "ASC");
        }
        $product_list = get_posts($args);
        if ($product_list) {
            echo '<h3>' . $current_category_name . '</h3>' . "\n";
            echo '<p>' . sprintf(__('Drag %s to the preferred order and then click the Save button at the bottom of the page.', 'foxyshop'), strtolower(FOXYSHOP_PRODUCT_NAME_PLURAL)) . '</p>';
            echo '<form name="form_product_order" method="post" action="">' . "\n";
            echo '<ul id="foxyshop_product_order_list" class="foxyshop_sort_list">' . "\n";
            foreach ($product_list as $prod) {
                $product = foxyshop_setup_product($prod);
                if ($categoryID == 0) {
                    $current_count = $prod->menu_order;
                } else {
                    $current_count = (int) get_post_meta($prod->ID, "_foxyshop_menu_order_" . $categoryID, true);
                }
                echo '<li id="id_' . $prod->ID . '" class="lineitem">';
                echo '<img src="' . foxyshop_get_main_image() . '" />';
                echo '<h4>' . $prod->post_title . '</h4>' . "\n";
                echo foxyshop_price();
                echo '<div class="counter">' . ($current_count + 1) . '</div>';
                echo '<div style="clear: both; height: 1px;"></div>' . "\n";
                echo '</li>' . "\n";
            }
            echo '</ul>' . "\n";
            ?>
			<div style="width: 90%; height: 100px;">
				<input type="submit" name="submit_new_product_order" id="submit_new_product_order" class="button-primary" value="<?php 
            _e('Save Custom Order', 'foxyshop');
            ?>
" onclick="javascript:orderPages(); return true;" />&nbsp;&nbsp;<strong id="updateText"></strong>
				<input type="submit" name="revert_product_order" id="revert_product_order" class="button" style="float: right;" value="<?php 
            _e('Revert To Original', 'foxyshop');
            ?>
" onclick="javascript:orderPages(); return true;" />
			</div>
			<input type="hidden" id="foxyshop_product_order_value" name="foxyshop_product_order_value" />
			<input type="hidden" id="hdnParentID" name="hdnParentID" value="<?php 
            echo $parentID;
            ?>
" />
			<input type="hidden" id="categoryID" name="categoryID" value="<?php 
            echo $categoryID;
            ?>
" />
			<?php 
            wp_nonce_field('update-foxyshop-sorting-options');
            ?>
			</form>
			<?php 
        } else {
            echo '<p><em>' . sprintf(__('No %s Found For This Category.', 'foxyshop'), FOXYSHOP_PRODUCT_NAME_PLURAL) . '</em></p>';
            echo '<p><a href="edit.php?post_type=foxyshop_product&amp;page=foxyshop_custom_sort&amp;upgrade_menu_order=1&amp;categoryID=' . $categoryID . '">Missing products? Click here.</a></p>';
        }
    }
    ?>

</div>

<script type="text/javascript">
function foxyshop_custom_order_load_event(){
	jQuery("#foxyshop_product_order_list").sortable({
		placeholder: "sortable-placeholder",
		revert: false,
		tolerance: "pointer",
		update: function() {
			var counter = 1;
			jQuery("#foxyshop_product_order_list li").each(function() {
				jQuery(this).find('.counter').html(counter);
				counter++;
			});
		}
	});
};
addLoadEvent(foxyshop_custom_order_load_event);
function orderPages() {
	jQuery("#updateText").html("<?php 
    echo sprintf(__('Updating %s Order...', 'foxyshop'), FOXYSHOP_PRODUCT_NAME_SINGULAR);
    ?>
");
	jQuery("#foxyshop_product_order_value").val(jQuery("#foxyshop_product_order_list").sortable("toArray"));
}
</script>
<?php 
}