function mcs_receive_ipn()
{
    if (isset($_GET['mcsipn']) && $_GET['mcsipn'] == 'true') {
        global $wpdb;
        mcs_check();
        if (get_option('mcs_gateway') == 'authorizenet') {
            require_once 'gateways/AuthorizeNet.php';
            // The SDK
            $url = add_query_arg('mcsipn', 'true', home_url());
            $api = get_option('mcs_authnet_api');
            $hash = get_option('mcs_authnet_hash');
            // these all need to be set from Authorize.Net data
            $payment_status = mcs_map_status($_POST['x_response_code']);
            // map response to equivalent from PayPal
            $item_number = 1;
            // mandatory for Paypal, but only represents a submissions purchase here.
            $price = $_POST['x_amount'];
            $quantity = isset($_POST['quantity']) ? $_POST['quantity'] : 1;
            // need to add to form
            $quantity = is_int($quantity) ? $quantity : 1;
            $payer_email = $_POST['x_payer_email'];
            // must add to form
            $payer_first_name = $_POST['x_first_name'];
            $payer_last_name = $_POST['x_last_name'];
            $mc_fee = '0.00';
            // not included in data
            $item_name = sprintf(__('%s Event Submission', 'my-calendar-submissions'), get_option('blogname'));
            // required by Paypal
            $parent = '';
            $redirect_url = $_POST['x_referer_url'];
            // paypal IPN data
            $ipn = new AuthorizeNetSIM($api, $hash);
            if ($ipn->isAuthorizeNet()) {
                if ($ipn->approved) {
                    $response = 'VERIFIED';
                    $redirect_url = add_query_arg(array('response_code' => '1', 'transaction_id' => $ipn->transaction_id), $redirect_url);
                    $txn_id = $ipn->transaction_id;
                } else {
                    $response = 'ERROR';
                    $redirect_url = add_query_arg(array('response_code' => $ipn->response_code, 'response_reason_text' => $ipn->response_reason_text), $redirect_url);
                    $txn_id = false;
                }
                $response_code = '200';
            } else {
                wp_die(__('That transaction was not handled by Authorize.net. Please verify your MD5 setting.', 'my-calendar-submissions'));
            }
        } else {
            if (isset($_POST['payment_status'])) {
                $sandbox = get_option("mcs_use_sandbox");
                $receiver = strtolower(get_option('mcs_paypal_email'));
                $url = $sandbox == 'true' ? 'https://www.sandbox.paypal.com/webscr' : 'https://www.paypal.com/webscr';
                $req = 'cmd=_notify-validate';
                foreach ($_POST as $key => $value) {
                    $value = urlencode(stripslashes($value));
                    $req .= "&{$key}={$value}";
                }
                $args = wp_parse_args($req, array());
                global $mcs_version;
                $params = array('body' => $args, 'sslverify' => false, 'timeout' => 30, 'user-agent' => "WordPress/My Calendar Pro {$mcs_version}; " . get_bloginfo('url'));
                // transaction variables to store
                $payment_status = $_POST['payment_status'];
                $item_number = $_POST['item_number'];
                $price = $_POST['mc_gross'];
                $payment_currency = $_POST['mc_currency'];
                $receiver_email = $_POST['receiver_email'];
                $quantity = isset($_POST['quantity']) ? $_POST['quantity'] : 1;
                $quantity = is_int($quantity) ? $quantity : 1;
                $payer_email = $_POST['payer_email'];
                $payer_first_name = $_POST['first_name'];
                $payer_last_name = $_POST['last_name'];
                $mc_fee = $_POST['mc_fee'];
                $item_name = $_POST['item_name'];
                $txn_id = $_POST['txn_id'];
                $parent = isset($_POST['parent_txn_id']) ? $_POST['parent_txn_id'] : '';
                // paypal IPN data
                $ipn = wp_remote_post($url, $params);
                $response = $ipn['body'];
                $response_code = $ipn['response']['code'];
                // die conditions for PayPal
                // if receiver email or currency are wrong, this is probably a fraudulent transaction.
                if (strtolower($receiver_email) != $receiver || $payment_currency != get_option('mcs_currency')) {
                    wp_mail(get_option('mcs_to'), 'Payment Conditions Error', 'PayPal receiver email did not match account or payment currency did not match payment');
                    wp_die();
                }
                $redirect_url = false;
            } else {
                wp_die("No valid IPN request made");
            }
        }
        if ($response_code == '200') {
            if ($response == "VERIFIED") {
                $status = "";
                if (get_option('mcs_gateway') != 'authorizenet') {
                    // See whether the transaction already exists. (For refunds, reversals, or canceled reversals)
                    $sql = "SELECT id, hash, status FROM " . my_calendar_payments_table() . " WHERE txn_id = %s";
                    $txn = $parent != '' ? $wpdb->get_row($wpdb->prepare($sql, array($parent))) : $wpdb->get_row($wpdb->prepare($sql, array($txn_id)));
                } else {
                    $txn = false;
                }
                switch ($payment_status) {
                    case 'Completed':
                    case 'Created':
                    case 'Denied':
                    case 'Expired':
                    case 'Failed':
                    case 'Processed':
                    case 'Voided':
                        $status = $payment_status;
                        break;
                    case 'Pending':
                        $status = $payment_status . ': ' . $post['pending_reason'];
                        break;
                    case 'Refunded':
                    case 'Reversed':
                    case 'Canceled_Reversal':
                        $status = $payment_status . ': ' . $post['ReasonCode'];
                        break;
                }
                if (empty($txn)) {
                    //error_log("INSERT: ".$txn_id." ".$status);
                    $uniqid = uniqid('E');
                    $hash = mcs_uniqid($uniqid);
                    $sql = "INSERT INTO " . my_calendar_payments_table() . "\n\t\t\t\t\t\t\t(item_number,quantity,total,hash,txn_id,price,fee,status,transaction_date,first_name,last_name,payer_email)\n\t\t\t\t\t\t\tVALUES(%d, %d, %d, %s, %s, %f, %f, %s, NOW(), %s, %s, %s )";
                    $wpdb->query($wpdb->prepare($sql, array($item_number, $quantity, $quantity, $hash, $txn_id, $price, $mc_fee, $status, $payer_first_name, $payer_last_name, $payer_email)));
                } else {
                    $hash = $txn->hash;
                    //error_log("UPDATE: ".$txn_id." ".$status." ".$hash." ->".$item_number);
                    $sql = "UPDATE " . my_calendar_payments_table() . "\n\t\t\t\t\t\t\tSET status = %s,price=%f,fee=%f,transaction_date = NOW() WHERE id = %d";
                    $r = $wpdb->query($wpdb->prepare($sql, array($status, $price, $mc_fee, $txn->id)));
                    //error_log(var_dump($r, true));
                }
                if ($status == "Completed") {
                    mcs_send_notifications($payer_first_name, $payer_last_name, $payer_email, $price, $hash, $quantity);
                    setcookie("mcs_receipt", 'true', time() + 60 * 60, SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
                }
            } else {
                // log for manual investigation
                $blogname = get_option('blogname');
                $mail_From = "From: {$blogname} Events <" . get_option('mcs_from') . ">";
                $mail_Subject = __("INVALID IPN on My Calendar Submission Payment", 'my-calendar-submissions');
                $mail_Body = __("Something went wrong. Hopefully this information will help:", 'my-calendar-submissions') . "\n\n";
                foreach ($_POST as $key => $value) {
                    $mail_Body .= $key . " = " . $value . "\n";
                }
                wp_mail(get_option('mcs_to'), $mail_Subject, $mail_Body, $mail_From);
            }
        } else {
            $blogname = get_option('blogname');
            $mail_From = "From: {$blogname} Events <" . get_option('mcs_from') . ">";
            $mail_Subject = __("WP HTTP Failed to contact Paypal", 'my-calendar-submissions');
            $mail_Body = __("Something went wrong. Hopefully this information will help:", 'my-calendar-submissions') . "\n\n";
            $mail_Body .= print_r($ipn, 1);
            wp_mail(get_option('mcs_to'), $mail_Subject, $mail_Body, $mail_From);
        }
        if ($redirect_url) {
            echo AuthorizeNetDPM::getRelayResponseSnippet($redirect_url);
            //wp_safe_redirect( $redirect_url );
            exit;
        } else {
            status_header(200);
        }
    } else {
        return;
    }
}
function mcs_settings()
{
    mcs_check();
    $response = mcs_update_settings($_POST);
    echo $response;
    $options = get_option('mcs_options');
    $defaults = $options['defaults'];
    $mcs_to = get_option('mcs_to');
    // send to
    $mcs_from = get_option('mcs_from');
    // send from
    $mcs_subject = get_option('mcs_subject') ? get_option('mcs_subject') : $defaults['mcs_subject'];
    // subject line
    $mcs_edit_subject = get_option('mcs_edit_subject') ? get_option('mcs_edit_subject') : $defaults['mcs_subject'];
    // subject line
    $mcs_response = get_option('mcs_response') ? get_option('mcs_response') : $defaults['mcs_response'];
    // admin email after submission
    $mcs_confirmation = get_option('mcs_confirmation') ? get_option('mcs_confirmation') : $defaults['mcs_confirmation'];
    // submitter email after submission
    $mcs_confirmation_subject = get_option('mcs_confirmation_subject') ? get_option('mcs_confirmation_subject') : $defaults['mcs_confirmation_subject'];
    // subject line
    $mcs_edit_confirmation_subject = get_option('mcs_edit_confirmation_subject') ? get_option('mcs_edit_confirmation_subject') : $defaults['mcs_confirmation_subject'];
    // subject line
    $mcs_payments = get_option('mcs_payments') ? get_option('mcs_payments') : $defaults['mcs_payments'];
    // are payments required?
    //$mcs_payments_approved = ( get_option('mcs_payments_approved') )?get_option('mcs_payments_approved'):$defaults['mcs_payments_approved']; // paid submissions auto approved
    $mcs_payment_subject = get_option('mcs_payment_subject') ? get_option('mcs_payment_subject') : $defaults['mcs_payment_subject'];
    // subject line
    $mcs_payment_response = get_option('mcs_payment_response') ? get_option('mcs_payment_response') : $defaults['mcs_payment_response'];
    // admin email after submission
    $mcs_payment_confirmation = get_option('mcs_payment_confirmation') ? get_option('mcs_payment_confirmation') : $defaults['mcs_payment_confirmation'];
    // submitter email after submission
    $mcs_payment_confirmation_subject = get_option('mcs_payment_confirmation_subject') ? get_option('mcs_payment_confirmation_subject') : $defaults['mcs_payment_confirmation_subject'];
    // subject line
    $mcs_payment_message = get_option('mcs_payment_message') ? get_option('mcs_payment_message') : $defaults['mcs_payment_message'];
    // subject line
    $mcs_submission_fee = get_option('mcs_submission_fee') ? get_option('mcs_submission_fee') : $defaults['mcs_submission_fee'];
    // posting cost for public
    $mcs_members_discount = get_option('mcs_members_discount');
    // discount for members (percentage)
    $mcs_criteria = get_option('mcs_criteria') ? get_option('mcs_criteria') : $defaults['mcs_criteria'];
    // who can submit events
    $mcs_paypal_email = get_option('mcs_paypal_email');
    // paypal email
    $mcs_purchase_page = get_option('mcs_purchase_page');
    $mcs_use_sandbox = get_option('mcs_use_sandbox') ? get_option('mcs_use_sandbox') : $defaults['mcs_use_sandbox'];
    // use sandbox
    $mcs_paypal_merchant_id = get_option('mcs_paypal_merchant_id');
    // paypal merchant ID
    $mcs_button = get_option('mcs_button');
    $mcs_submit_id = get_option('mcs_submit_id');
    $mcs_date_format = get_option('mcs_date_format');
    $mcs_time_format = get_option('mcs_time_format');
    $mcs_currency = get_option('mcs_currency');
    $mcs_quantity = get_option('mcs_quantity');
    $mcs_discount = get_option('mcs_discount');
    $mcs_gateway = get_option('mcs_gateway');
    $mcs_authnet_api = get_option('mcs_authnet_api');
    $mcs_authnet_key = get_option('mcs_authnet_key');
    $mcs_authnet_hash = get_option('mcs_authnet_hash');
    $mcs_check_conflicts = get_option('mcs_check_conflicts');
    $mcs_upload_images = get_option('mcs_upload_images');
    $mcs_automatic_approval = get_option('mcs_automatic_approval');
    $mcs_dont_send_submitter_email = get_option('mcs_dont_send_submitter_email');
    $mcs_dont_send_admin_email = get_option('mcs_dont_send_admin_email');
    ?>
    <div class="wrap jd-my-calendar" id="mc_settings">
	<?php 
    my_calendar_check_db();
    ?>
	<h2><?php 
    _e('My Calendar Pro', 'my-calendar-submissions');
    ?>
</h2>
	<div class="mc-tabs mcs-tabs">
		<ul class="tabs" role="tablist">
			<li role="tab" id="tab_mcs" aria-controls="mcs_tab"><a href="#mcs_tab"><?php 
    _e('Submissions', 'my-calendar');
    ?>
</a></li>
			<li role="tab" id="tab_mcs_payments" aria-controls="mcs_payments_tab"><a href="#mcs_payments_tab"><?php 
    _e('Payments', 'my-calendar');
    ?>
</a></li>
			<?php 
    $tabs = apply_filters('mcs_settings_tabs', array());
    foreach ($tabs as $key => $value) {
        $key = sanitize_title($key);
        $value = esc_html($value);
        echo '<li role="tab" id="tab_mcs_' . $key . '" aria-controls="mcs_' . $key . '_tab"><a href="#mcs_' . $key . '_tab">' . $value . '</a></li>' . "\n";
    }
    ?>
		</ul>
	<div class="mcs-settings settings postbox-container jcd-wide">
		
	<div class="metabox-holder wptab" aria-labelledby="tab_mcs" role="tabpanel" aria-live="assertive" id="mcs_tab">
		
		<div class="ui-sortable meta-box-sortables">   
		<div class="postbox">
			<h3><?php 
    _e('Event Submissions Settings', 'my-calendar-submissions');
    ?>
</h3>
			<div class="inside">
			<p>
			<a href="<?php 
    echo admin_url('admin.php?page=my-calendar-manage&amp;limit=reserved#my-calendar-admin-table');
    ?>
"><?php 
    _e('View pending event submissions', 'my-calendar-submissions');
    ?>
</a>
			</p>
			<form method="post" action="<?php 
    echo admin_url("admin.php?page=my-calendar-submissions");
    ?>
">
			<div><input type="hidden" name="_wpnonce" value="<?php 
    echo wp_create_nonce('my-calendar-submissions');
    ?>
" /></div>
			<p>
			<label for="mcs_submit_id"><?php 
    _e('Event Submission Page ID', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="mcs_submit_id" id="mcs_submit_id" size="6" value="<?php 
    echo esc_attr(trim($mcs_submit_id));
    ?>
" />
			</p>			
			<p class='format'>
			<label for="mcs_date_format"><?php 
    _e('Date format hint', 'my-calendar-submissions');
    ?>
</label> <select name="mcs_date_format" id="mcs_date_format">
			<option value="m/d/Y" <?php 
    echo mc_is_selected('mcs_date_format', 'm/d/y');
    ?>
><?php 
    echo date('m/d/Y');
    ?>
</option>
			<option value="d-m-Y" <?php 
    echo mc_is_selected('mcs_date_format', 'd-m-Y');
    ?>
><?php 
    echo date('d-m-Y');
    ?>
</option>
			<option value="Y-m-d" <?php 
    echo mc_is_selected('mcs_date_format', 'Y-m-d');
    ?>
><?php 
    echo date('Y-m-d');
    ?>
</option>
			<option value="j F Y" <?php 
    echo mc_is_selected('mcs_date_format', 'j F Y');
    ?>
><?php 
    echo date_i18n('j F Y');
    ?>
</option>
			<option value="M j, Y" <?php 
    echo mc_is_selected('mcs_date_format', 'M j, Y');
    ?>
><?php 
    echo date('M j, Y');
    ?>
</option>
			</select>
			</p>
			<p class='format'>
			<label for="mcs_time_format"><?php 
    _e('Time format hint', 'my-calendar-submissions');
    ?>
</label> <select name="mcs_time_format" id="mcs_time_format">
			<option value="h:i a" <?php 
    echo mc_is_selected('mcs_time_format', 'h:i a');
    ?>
><?php 
    echo date('h:i a');
    ?>
</option>
			<option value="H:i" <?php 
    echo mc_is_selected('mcs_time_format', 'H:i');
    ?>
><?php 
    echo date('H:i');
    ?>
</option>
			</select>
			</p>			
			<p>
			<input type="checkbox" id="mcs_check_conflicts" name="mcs_check_conflicts" <?php 
    mc_is_checked('mcs_check_conflicts', 'true');
    ?>
 /> <label for="mcs_check_conflicts"><?php 
    _e('Prevent conflicting events. (if locations are used, checks only for conflicts at that location.)', 'my-calendar-submissions');
    ?>
</label>
			</p>
			<p>
			<input type="checkbox" id="mcs_upload_images" name="mcs_upload_images" <?php 
    mc_is_checked('mcs_upload_images', 'true');
    ?>
 /> <label for="mcs_upload_images"><?php 
    _e('Allow public event submitters to upload their own images', 'my-calendar-submissions');
    ?>
</label>
			</p>			
			<p>
			<input type="checkbox" id="mcs_automatic_approval" name="mcs_automatic_approval" <?php 
    mc_is_checked('mcs_automatic_approval', 'true');
    ?>
 /> <label for="mcs_automatic_approval"><?php 
    _e('Submitted events do not require approval.', 'my-calendar-submissions');
    ?>
</label>
			</p>
			<p>
			<input type="checkbox" id="mcs_dont_send_submitter_email" name="mcs_dont_send_submitter_email" <?php 
    mc_is_checked('mcs_dont_send_submitter_email', 'true');
    ?>
 /> <label for="mcs_dont_send_submitter_email"><?php 
    _e('Disable submitter email notification on automatically approved events.', 'my-calendar-submissions');
    ?>
</label>
			</p>
			<p>
			<input type="checkbox" id="mcs_dont_send_admin_email" name="mcs_dont_send_admin_email" <?php 
    mc_is_checked('mcs_dont_send_admin_email', 'true');
    ?>
 /> <label for="mcs_dont_send_admin_email"><?php 
    _e('Disable admin email notification on automatically approved events.', 'my-calendar-submissions');
    ?>
</label>
			</p>			
			<h4><?php 
    _e('New event messages', 'my-calendar-submissions');
    ?>
</h4>
			<p>
			<input type="checkbox" id="mcs_html_email" name="mcs_html_email" <?php 
    mc_is_checked('mcs_html_email', 'true');
    ?>
 /> <label for="mcs_html_email"><?php 
    _e('Send email notifications as HTML.', 'my-calendar-submissions');
    ?>
</label>
			</p>
			<fieldset>
			<legend><?php 
    _e('Sent to administrators', 'my-calendar-submissions');
    ?>
</legend>
			<ul>
			<li>
			<label for="mcs_to"><?php 
    _e('Send notifications to:', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="mcs_to" id="mcs_to" size="60" value="<?php 
    echo $mcs_to == '' ? get_bloginfo('admin_email') : esc_attr($mcs_to);
    ?>
" />
			</li>
			<li>
			<label for="mcs_from"><?php 
    _e('Send notifications from:', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="mcs_from" id="mcs_from" size="60" value="<?php 
    echo $mcs_from == '' ? get_bloginfo('admin_email') : esc_attr($mcs_from);
    ?>
" />
			</li>
			<li>
			<label for="mcs_subject"><?php 
    _e('Notification Subject', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="mcs_subject" id="mcs_subject" size="60" value="<?php 
    echo stripslashes(esc_attr($mcs_subject));
    ?>
" />
			</li>
			<li>
			<label for="mcs_edit_subject"><?php 
    _e('Notification Subject (Edits)', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="mcs_edit_subject" id="mcs_edit_subject" size="60" value="<?php 
    echo stripslashes(esc_attr($mcs_edit_subject));
    ?>
" />
			</li>		
			<li>
			<label for="mcs_response"><?php 
    _e('Notification message', 'my-calendar-submissions');
    ?>
</label><br /><textarea name="mcs_response" id="mcs_response" rows="4" cols="60"><?php 
    echo stripslashes(esc_attr($mcs_response));
    ?>
</textarea>
			<?php 
    $edit_link = mcs_submit_url() ? ', <code>edit_link</code>' : '';
    ?>
			<em><?php 
    echo __('Available template tags: <code>first_name</code>, <code>last_name</code>, <code>email</code>, <code>title</code>, <code>date</code>, <code>time</code>, <code>description</code>, <code>short</code>, <code>image</code>, <code>url</code>, <code>location</code>, <code>street</code>, <code>city</code>, <code>phone</code>, <code>blogname</code>', 'my-calendar-submissions') . $edit_link;
    ?>
</em>
			</li>
			</ul>
			</fieldset>
			<fieldset>
			<legend><?php 
    _e('Sent to event submitter', 'my-calendar-submissions');
    ?>
</legend>
			<ul>
			<li>
			<label for="mcs_confirmation_subject"><?php 
    _e('Confirmation Subject', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="mcs_confirmation_subject" id="mcs_confirmation_subject" size="60" value="<?php 
    echo stripslashes(esc_attr($mcs_confirmation_subject));
    ?>
" />
			</li>
			<li>
			<label for="mcs_edit_confirmation_subject"><?php 
    _e('Confirmation Subject', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="mcs_edit_confirmation_subject" id="mcs_edit_confirmation_subject" size="60" value="<?php 
    echo stripslashes(esc_attr($mcs_edit_confirmation_subject));
    ?>
" />
			</li>			
			<li>
			<label for="mcs_confirmation"><?php 
    _e('Submitter confirmation message', 'my-calendar-submissions');
    ?>
</label><br /><textarea name="mcs_confirmation" id="mcs_confirmation" rows="4" cols="60"><?php 
    echo stripslashes(esc_attr($mcs_confirmation));
    ?>
</textarea>
			<em><?php 
    _e('Available template tags: <code>first_name</code>, <code>last_name</code>, <code>email</code>, <code>title</code>, <code>date</code>, <code>time</code>, <code>description</code>, <code>short</code>, <code>image</code>, <code>url</code>, <code>location</code>, <code>street</code>, <code>city</code>, <code>phone</code> <code>blogname</code>, <code>edit_link</code>', 'my-calendar-submissions');
    ?>
</em>
			</li>	
			</ul>
			</fieldset>
			<h4><?php 
    _e('Event Submission Criteria', 'my-calendar-submissions');
    ?>
</h4>
			<fieldset>
			<legend><?php 
    _e('Who may use the front-end event submission widget?', 'my-calendar-submissions');
    ?>
</legend>
			<ul>
			<li>
			<input type="radio" id="mcs_public" name="mcs_criteria" value="1" <?php 
    mc_is_checked('mcs_criteria', '1');
    ?>
 /> <label for="mcs_public"><?php 
    _e('General public.', 'my-calendar-submissions');
    ?>
</label>
			</li>			
			<li>
			<input type="radio" id="mcs_members_only" name="mcs_criteria" value="2" <?php 
    mc_is_checked('mcs_criteria', '2');
    ?>
 /> <label for="mcs_members_only"><?php 
    _e('Members.', 'my-calendar-submissions');
    ?>
</label>
			</li>
			<li>
			<input type="radio" id="mcs_member_status" name="mcs_criteria" value="3" <?php 
    mc_is_checked('mcs_criteria', '3');
    ?>
 /> <label for="mcs_member_status"><?php 
    _e('Members with the "mc_add_events" capability.', 'my-calendar-submissions');
    ?>
</label>
			</li>
			</ul>
			</fieldset>
			<p><input type="submit" name="mc-submit-settings" class="button-primary" value="<?php 
    _e('Save Submissions Settings', 'my-calendar-submissions');
    ?>
" /></p>			
			</form>			
			</div>
		</div>
		</div>
	</div>
	<div class="metabox-holder wptab" aria-labelledby="tab_payments_mcs" role="tabpanel" aria-live="assertive" id="mcs_payments_tab">
	<form method="post" action="<?php 
    echo admin_url("admin.php?page=my-calendar-submissions#mcs_payments_tab");
    ?>
">
	<div class="ui-sortable meta-box-sortables">   
		<div class="postbox">
			<h3><?php 
    _e('Payment Settings', 'my-calendar-submissions');
    ?>
</h3>
			<div class="inside">
			<div><input type="hidden" name="_wpnonce" value="<?php 
    echo wp_create_nonce('my-calendar-submissions');
    ?>
" /></div>
			<ul>
			<li>
			<input type="checkbox" id="mcs_payments" name="mcs_payments" <?php 
    mc_is_checked('mcs_payments', 'true');
    ?>
 /> <label for="mcs_payments"><?php 
    _e('Require payment to submit an event', 'my-calendar-submissions');
    ?>
</label>
			</li>
			<?php 
    if (get_option('mcs_payments') == 'true') {
        ?>
			<li>
			<input type="checkbox" id="mcs_use_sandbox" name="mcs_use_sandbox" <?php 
        mc_is_checked('mcs_use_sandbox', 'true');
        ?>
 /> <label for="mcs_use_sandbox"><?php 
        _e('Place gateway in Testing mode', 'my-calendar-submissions');
        ?>
</label>
			</li>
			<li>
			<input type="checkbox" id="mcs_quantity" name="mcs_quantity" <?php 
        mc_is_checked('mcs_quantity', 'true');
        ?>
 /> <label for="mcs_quantity"><?php 
        _e('Visitors may purchase multiple-use payment keys', 'my-calendar-submissions');
        ?>
</label>
			</li>
			<li>
			<label for="mcs_payment_message"><?php 
        _e('Payment Form Message (shows above payments form)', 'my-calendar-submissions');
        ?>
</label> <textarea type="text" name="mcs_payment_message" id="mcs_payment_message" rows="2" cols="60"><?php 
        echo stripslashes(esc_attr($mcs_payment_message));
        ?>
</textarea>
			<em><?php 
        _e('Available template tags: <code>blogname</code>, <code>begins</code>, <code>ends</code>, <code>price</code>, <code>discount</code>, <code>currency</code>', 'my-calendar-submissions');
        ?>
</em>
			</li>			
			</ul>
			<h4><?php 
        _e('New purchase messages', 'my-calendar-submissions');
        ?>
</h4>
			<fieldset>
			<legend><?php 
        _e('Sent to administrators', 'my-calendar-submissions');
        ?>
</legend>
			<ul>
			<li>
			<label for="mcs_payment_subject"><?php 
        _e('Payment Notification Subject', 'my-calendar-submissions');
        ?>
</label> <input type="text" name="mcs_payment_subject" id="mcs_payment_subject" size="60" value="<?php 
        echo stripslashes(esc_attr($mcs_payment_subject));
        ?>
" />
			</li>
			<li>
			<label for="mcs_payment_response"><?php 
        _e('Payment Notification message', 'my-calendar-submissions');
        ?>
</label><br /><textarea name="mcs_payment_response" id="mcs_payment_response" rows="4" cols="60"><?php 
        echo stripslashes(esc_attr($mcs_payment_response));
        ?>
</textarea>
			<em><?php 
        _e('Available template tags: <code>blogname</code>, <code>first_name</code>, <code>last_name</code>, <code>price</code>, <code>key</code>, <code>quantity</code>, <code>receipt</code>', 'my-calendar-submissions');
        ?>
</em>
			</li>
			</ul>
			</fieldset>
			<fieldset>
			<legend><?php 
        _e('Sent to purchaser', 'my-calendar-submissions');
        ?>
</legend>
			<ul>
			<li>
			<label for="mcs_payment_confirmation_subject"><?php 
        _e('Payment Confirmation Subject', 'my-calendar-submissions');
        ?>
</label> <input type="text" name="mcs_payment_confirmation_subject" id="mcs_payment_confirmation_subject" size="60" value="<?php 
        echo stripslashes(esc_attr($mcs_payment_confirmation_subject));
        ?>
" />
			</li>
			<li>
			<label for="mcs_payment_confirmation"><?php 
        _e('Payment Submitter confirmation message', 'my-calendar-submissions');
        ?>
</label><br /><textarea name="mcs_payment_confirmation" id="mcs_payment_confirmation" rows="4" cols="60"><?php 
        echo stripslashes(esc_attr($mcs_payment_confirmation));
        ?>
</textarea>
			<em><?php 
        _e('Available template tags: <code>first_name</code>, <code>last_name</code>, <code>price</code>, <code>key</code>, <code>quantity</code>, <code>receipt</code>', 'my-calendar-submissions');
        ?>
</em>
			</li>	
			</ul>
			</fieldset>
			<ul>
			<li>
			<?php 
        $pricing = apply_filters('mcs_submission_fee_settings', false);
        if (!$pricing) {
            ?>
			<label for="mcs_submission_fee"><?php 
            _e('Base price:', 'my-calendar-submissions');
            ?>
</label> <input type="text" name="mcs_submission_fee" id="mcs_submission_fee" size="60" value="<?php 
            echo esc_attr($mcs_submission_fee);
            ?>
" />
			<?php 
        } else {
            echo $pricing;
        }
        ?>
			</li>
			<li><label for="mcs_currency"><?php 
        _e('Currency:', 'my-calendar-submissions');
        ?>
</label> 
			<?php 
        $mcs_currency_codes = array("USD" => __('U.S. Dollars ($)', 'my-calendar-submissions'), "EUR" => __('Euros (€)', 'my-calendar-submissions'), "AUD" => __('Australian Dollars (A $)', 'my-calendar-submissions'), "CAD" => __('Canadian Dollars (C $)', 'my-calendar-submissions'), "GBP" => __('Pounds Sterling (£)', 'my-calendar-submissions'), "JPY" => __('Yen (¥)', 'my-calendar-submissions'), "NZD" => __('New Zealand Dollar ($)', 'my-calendar-submissions'), "CHF" => __('Swiss Franc', 'my-calendar-submissions'), "HKD" => __('Hong Kong Dollar ($)', 'my-calendar-submissions'), "SGD" => __('Singapore Dollar ($)', 'my-calendar-submissions'), "SEK" => __('Swedish Krona', 'my-calendar-submissions'), "DKK" => __('Danish Krone', 'my-calendar-submissions'), "PLN" => __('Polish Zloty', 'my-calendar-submissions'), "NOK" => __('Norwegian Krone', 'my-calendar-submissions'), "HUF" => __('Hungarian Forint', 'my-calendar-submissions'), "ILS" => __('Israeli Shekel', 'my-calendar-submissions'), "MXN" => __('Mexican Peso', 'my-calendar-submissions'), "BRL" => __('Brazilian Real (only for Brazilian users)', 'my-calendar-submissions'), "MYR" => __('Malaysian Ringgits (only for Malaysian users)', 'my-calendar-submissions'), "PHP" => __('Philippine Pesos', 'my-calendar-submissions'), "TWD" => __('Taiwan New Dollars', 'my-calendar-submissions'), "THB" => __('Thai Baht', 'my-calendar-submissions'), "TRY" => __('Turkish Lira (only for Turkish users)', 'my-calendar-submissions'));
        echo "<select name='mcs_currency' id='mcs_currency'>";
        foreach ($mcs_currency_codes as $code => $currency) {
            $selected = get_option('mcs_currency') == $code ? "selected='selected'" : "";
            echo "<option value='{$code}' {$selected}>{$currency}</option>";
        }
        echo "</select>";
        ?>
			</li>
			<li>
			<label for="mcs_members_discount"><?php 
        _e('Member discount (%)', 'my-calendar-submissions');
        ?>
</label> <input type="text" name="mcs_members_discount" id="mcs_members_discount" size="3" value="<?php 
        echo esc_attr($mcs_members_discount);
        ?>
" /> <?php 
        _e('Member\'s discounted cost:', 'my-calendar-submissions');
        ?>
 <?php 
        echo mcs_get_price(true);
        ?>
			</li>
			<?php 
        echo apply_filters('mcs_custom_fields', '');
        ?>
			</ul>
			</div>
		</div>
	</div>
	<div class="ui-sortable meta-box-sortables">   
		<div class="postbox">
			<h3><?php 
        _e('Payment Gateways', 'my-calendar-submissions');
        ?>
</h3>
			<div class="inside">
			<ul>
			<li>
			<input type="radio" id="mcs_gateway_paypal" name="mcs_gateway" value="paypal" <?php 
        mc_is_checked('mcs_gateway', 'paypal');
        ?>
 /> <label for="mcs_gateway_paypal"><?php 
        _e('Use Paypal', 'my-calendar-submissions');
        ?>
</label>
			</li>
			<li>
			<input type="radio" id="mcs_gateway_authorizenet" name="mcs_gateway" value="authorizenet" <?php 
        mc_is_checked('mcs_gateway', 'authorizenet');
        ?>
 /> <label for="mcs_gateway_authorizenet"><?php 
        _e('Use Authorize.net', 'my-calendar-submissions');
        ?>
</label>
			</li>
			<li>
			<input type="checkbox" id="mcs_ssl" name="mcs_ssl" value="true" aria-describedby="mcs_ssl_desc" <?php 
        mc_is_checked('mcs_ssl', 'true');
        ?>
 /> <label for="mcs_ssl"><?php 
        _e('Use SSL for Payment pages.', 'my-calendar-submissions');
        ?>
</label><br />
			<span id="mcs_ssl_desc"><?php 
        _e('SSL is not required for My Calendar Pro to be secure, but can improve the comfort level of your users on your site.', 'my-calendar-submissions');
        ?>
</span>
			</li>
			<li>
			<?php 
        if (get_option('mcs_ssl') == 'true') {
            $required = 'required aria-required="true"';
        } else {
            $required = '';
        }
        ?>
			<input type="text" size='4' id="mcs_purchase_page" name="mcs_purchase_page" value="<?php 
        echo esc_attr($mcs_purchase_page);
        ?>
"<?php 
        echo $required;
        ?>
 /> <label for="mcs_purchase_page"><?php 
        _e('Post ID for My Calendar Submissions form.', 'my-calendar-submissions');
        ?>
</label><br />
			</li>			
			</ul>
			<?php 
        if (get_option('mcs_gateway') == 'authorizenet') {
            ?>
			<h4><?php 
            _e('Authorize.net Settings', 'my-calendar-submissions');
            ?>
</h4>
			<fieldset>
			<div>
				<input type="hidden" name="mcs_paypal_email" value="<?php 
            echo esc_attr($mcs_paypal_email);
            ?>
" />
				<input type="hidden" name="mcs_paypal_merchant_id" value="<?php 
            echo esc_attr($mcs_paypal_merchant_id);
            ?>
" />
			</div>
			<legend><?php 
            _e('Authorize.net Settings', 'my-calendar-submissions');
            ?>
</legend>
			<ul>
			<li>
			<label for="mcs_authnet_api"><?php 
            _e('API Login ID', 'my-calendar-submissions');
            ?>
</label> <input type="text" name="mcs_authnet_api" id="mcs_authnet_api" size="60" value="<?php 
            echo esc_attr($mcs_authnet_api);
            ?>
" />
			</li>
			<li>
			<label for="mcs_authnet_key"><?php 
            _e('Transaction Key', 'my-calendar-submissions');
            ?>
</label> <input type="text" name="mcs_authnet_key" id="mcs_authnet_key" size="60" value="<?php 
            echo esc_attr($mcs_authnet_key);
            ?>
" />
			</li>
			<li>
			<label for="mcs_authnet_hash"><?php 
            _e('MD5 Hash Value', 'my-calendar-submissions');
            ?>
</label> <input type="text" name="mcs_authnet_hash" id="mcs_authnet_hash" size="60" value="<?php 
            echo esc_attr($mcs_authnet_hash);
            ?>
" />
			</li>			
			</ul>
			</fieldset>	
			
			<?php 
        } else {
            ?>
			<h4><?php 
            _e('PayPal Settings', 'my-calendar-submissions');
            ?>
</h4>
			<fieldset>
			<div>
				<input type="hidden" name="mcs_authnet_api" value="<?php 
            echo esc_attr($mcs_authnet_api);
            ?>
" />
				<input type="hidden" name="mcs_authnet_key" value="<?php 
            echo esc_attr($mcs_authnet_key);
            ?>
" />
				<input type="hidden" name="mcs_authnet_hash" value="<?php 
            echo esc_attr($mcs_authnet_hash);
            ?>
" />
			</div>			
			<legend><?php 
            _e('PayPal Settings', 'my-calendar-submissions');
            ?>
</legend>
			<ul>
			<li>
			<label for="mcs_paypal_email"><?php 
            _e('Paypal email (primary)', 'my-calendar-submissions');
            ?>
</label> <input type="text" name="mcs_paypal_email" id="mcs_paypal_email" size="60" value="<?php 
            echo esc_attr($mcs_paypal_email);
            ?>
" />
			</li>
			<li>
			<label for="mcs_paypal_merchant_id"><?php 
            _e('Paypal merchant ID', 'my-calendar-submissions');
            ?>
</label> <input type="text" name="mcs_paypal_merchant_id" id="mcs_paypal_merchant_id" size="60" value="<?php 
            echo esc_attr($mcs_paypal_merchant_id);
            ?>
" />
			</li>
			</ul>
			</fieldset>
			
			<?php 
        }
        ?>
			<ul>
			<li>
			<label for="mcs_button"><?php 
        _e('Purchase Button image', 'my-calendar-submissions');
        ?>
</label> <input type="text" name="mcs_button" id="mcs_button" size="60" value="<?php 
        echo esc_url($mcs_button);
        ?>
" />
			</li>
			</ul>
			</div>
		</div>
	</div>
	<div class="ui-sortable meta-box-sortables">   
		<div class="postbox">
			<h3><?php 
        _e('Configure a Sale', 'my-calendar-submissions');
        ?>
</h3>
			<div class="inside">
			<fieldset>
			<legend><?php 
        _e('Sale settings', 'my-calendar-submissions');
        ?>
</legend>
			<?php 
        $sales = apply_filters('mcs_custom_sale', false);
        if (!$sales) {
            ?>
				<ul>
				<li>
				<label for="mcs_sale_begins"><?php 
            _e('Date sale begins (e.g. November 24th, 2013)', 'my-calendar-submissions');
            ?>
</label> <input type="text" name="mcs_discount[begins]" id="mcs_sale_begins" size="20" value="<?php 
            echo isset($mcs_discount['begins']) ? esc_attr($mcs_discount['begins']) : '';
            ?>
" /> <?php 
            _e('Discounted cost per event:', 'my-calendar-submissions');
            ?>
 <?php 
            echo sprintf('$%01.2f', mcs_get_price(true));
            ?>
				</li>
				<li>
				<label for="mcs_sale_ends"><?php 
            _e('Date sale ends (e.g. December 25th, 2013)', 'my-calendar-submissions');
            ?>
</label> <input type="text" name="mcs_discount[ends]" id="mcs_sale_ends" size="20" value="<?php 
            echo isset($mcs_discount['ends']) ? esc_attr($mcs_discount['ends']) : '';
            ?>
" />
				</li>
				<li>
				<label for="mcs_sale_rate"><?php 
            _e('Percentage discount', 'my-calendar-submissions');
            ?>
</label> <input type="text" name="mcs_discount[rate]" id="mcs_sale_rate" size="3" value="<?php 
            echo isset($mcs_discount['rate']) ? esc_attr($mcs_discount['rate']) : '';
            ?>
" />
				</li>
				</ul>
			<?php 
        } else {
            echo $sales;
        }
        ?>
				<p><?php 
        _e('<strong>Note:</strong> if members have a discount, the additional sale rate will not be compounded with their normal rate.', 'my-calendar-submissions');
        ?>
</p>
			</fieldset>
			<?php 
    } else {
        ?>
				<div>
				<input type="hidden" name="mcs_payment_response" value="<?php 
        echo $mcs_payment_response;
        ?>
" />
				<input type="hidden" name="mcs_payment_confirmation" value="<?php 
        echo $mcs_payment_confirmation;
        ?>
" />
				<input type="hidden" name="mcs_payment_subject" value="<?php 
        echo $mcs_payment_subject;
        ?>
" />
				<input type="hidden" name="mcs_payment_message" value="<?php 
        echo $mcs_payment_message;
        ?>
" />
				<input type="hidden" name="mcs_payment_confirmation_subject" value="<?php 
        echo $mcs_payment_confirmation_subject;
        ?>
" />
				<input type="hidden" name="mcs_submission_fee" value="<?php 
        echo $mcs_submission_fee;
        ?>
" />
				<input type="hidden" name="mcs_members_discount" value="<?php 
        echo $mcs_members_discount;
        ?>
" />
				<input type="hidden" name="mcs_paypal_email" value="<?php 
        echo $mcs_paypal_email;
        ?>
" />
				<input type="hidden" name="mcs_paypal_merchant_id" value="<?php 
        echo $mcs_paypal_merchant_id;
        ?>
" />
				<input type="hidden" name="mcs_button" value="<?php 
        echo $mcs_button;
        ?>
" />
				<input type="hidden" name="mcs_currency" value="<?php 
        echo $mcs_currency;
        ?>
" />
				<input type="hidden" name="mcs_discount[begins]" value="<?php 
        echo $mcs_discount['begins'];
        ?>
" />
				<input type="hidden" name="mcs_discount[ends]" value="<?php 
        echo $mcs_discount['ends'];
        ?>
" />
				<input type="hidden" name="mcs_discount[rate]" value="<?php 
        echo $mcs_discount['rate'];
        ?>
" />
				</div>
			<?php 
    }
    ?>
				<p><input type="submit" name="mc-payment-settings" class="button-primary" value="<?php 
    _e('Save Payment Settings', 'my-calendar-submissions');
    ?>
" /></p>			
			</div>
		</div>		
	</div>
	</form>	
	</div>		
	<?php 
    $panels = apply_filters('mcs_settings_panels', array());
    foreach ($panels as $key => $value) {
        $content = is_array($value) && isset($value['content']) ? $value['content'] : $value;
        $label = is_array($value) && isset($value['label']) ? $value['label'] : __('Save Settings');
        $wp_nonce = wp_nonce_field($key, '_wpnonce', true, false);
        $top = '
			<div class="metabox-holder wptab" aria-labelledby="tab_' . $key . '_mcs" role="tabpanel" aria-live="assertive" id="mcs_' . $key . '_tab">
				<form method="post" action="' . admin_url("admin.php?page=my-calendar-submissions#mcs_{$key}" . '_tab') . '" enctype="multipart/form-data">' . $wp_nonce . '<div class="ui-sortable meta-box-sortables">   
						<div class="postbox">';
        $bottom = '
						</div>
					</div>
				</form>
			</div>';
        $middle = str_replace('{submit}', '<p><input type="submit" name="' . $key . '_settings" class="button-primary" value="' . $label . '" /></p>', $content);
        echo $top . $middle . $bottom;
    }
    ?>
	</div>
	<?php 
    if (function_exists('mcs_submit_exists')) {
        $remove = true;
    } else {
        $remove = false;
    }
    $guide_url = plugins_url('/My-Calendar-Pro.pdf', __FILE__);
    $add = array('Event Submissions Shortcode' => '<p>' . __('The event submissions form can be configured via shortcode or via widget.', 'my-calendar-submissions') . '</p>
			<p>' . __('New events are always submitted with approval pending and must be approved by a user with appropriate permissions in order to appear on the site.', 'my-calendar-submissions') . '</p>
			<p>' . sprintf(__('Use the <a href="%s">shortcode generator</a> to create your My Calendar submissions form!', 'my-calendar-submissions'), admin_url('admin.php?page=my-calendar-help#mc-generator')) . '</p>', 'Responsive Mode' => '<p>' . __('The file <code>mc-responsive.css</code> in your theme directory will replace the My Calendar PRO responsive stylesheet.', 'my-calendar-submissions') . '</p>');
    ?>
	<?php 
    mc_show_sidebar('', $add, $remove);
    ?>
</div>
</div>
<?php 
    // creates settings page for my calendar appointments
}
function mcs_sales_page()
{
    global $wpdb;
    mcs_check();
    echo "<div class='wrap jd-my-calendar'>";
    echo "<h2>My Calendar Submissions Sales</h2>";
    ?>
	<div class="postbox-container" style="width: 70%">
	
		<div class="metabox-holder">
		<div class="ui-sortable meta-box-sortables">   
		<div class="postbox">
			<h3><?php 
    _e('Register a payment', 'my-calendar-submissions');
    ?>
</h3>
			<div class="inside"> 
			<?php 
    $response = mcs_add_payment($_POST);
    echo $response;
    $quantity = 1;
    // send to
    $price = get_option('mcs_submission_fee');
    // send from
    $first_name = '';
    // subject line
    $last_name = '';
    // admin email after submission
    $email = '';
    // submitter email after submission
    $transaction_date = date_i18n('j F, Y');
    // subject line
    ?>
			<p><?php 
    _e('Use this form to manually register a payment and send payment notification messages.', 'my-calendar-submissions');
    ?>
</p>
			<form method="post" action="<?php 
    echo admin_url("admin.php?page=my-calendar-payments");
    ?>
">
			<div>
			<input type="hidden" name="_wpnonce" value="<?php 
    echo wp_create_nonce('my-calendar-payments');
    ?>
" />
			</div>
			<p><input type="submit" name="mc-submit-payments" class="button-primary" value="<?php 
    _e('Enter Payment', 'my-calendar-submissions');
    ?>
" /></p>			
			<ul>
			<li>
			<label for="quantity"><?php 
    _e('Quantity (event submissions purchased)', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="quantity" id="quantity" size="6" value="<?php 
    echo esc_attr($quantity);
    ?>
" /> 
			<label for="price"><?php 
    _e('Price Paid (total)', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="price" id="price" size="6" value="<?php 
    echo esc_attr($price);
    ?>
" />
			</li>
			<li>
			<label for="first_name"><?php 
    _e('First Name', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="first_name" id="first_name" size="60" value="<?php 
    echo esc_attr($first_name);
    ?>
" />
			</li>
			<li>
			<label for="last_name"><?php 
    _e('Last Name', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="last_name" id="last_name" size="60" value="<?php 
    esc_attr($last_name);
    ?>
" />
			</li>
			<li>
			<label for="email"><?php 
    _e('Email', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="email" id="email" size="60" value="<?php 
    echo esc_attr($email);
    ?>
" />
			</li>			
			<li>
			<label for="transaction_date"><?php 
    _e('Transaction Date', 'my-calendar-submissions');
    ?>
</label> <input type="text" name="transaction_date" id="transaction_date" size="20" value="<?php 
    echo esc_attr($transaction_date);
    ?>
" />
			</li>	
			</ul>
			<p><input type="submit" name="mc-submit-payments" class="button-secondary" value="<?php 
    _e('Enter Payment', 'my-calendar-submissions');
    ?>
" /></p>		
			</form>				
			</div>
		</div>
		</div>
	</div>
	
	<div class="metabox-holder">

		<div class="ui-sortable meta-box-sortables">   
		<div class="postbox">
			<h3>My Calendar Event Payments</h3>
			<div class="inside">   
		<?php 
    foreach ($_GET as $key => $value) {
        $_GET[$key] = trim($value);
    }
    $name_query = $email_query = $txn_id_query = $hash_query = $status_query = "";
    if (!empty($_GET['name'])) {
        $name_query = " AND (first_name LIKE '%" . $wpdb->escape($_GET['name']) . "%' OR last_name LIKE '%" . $wpdb->escape($_GET['name']) . "%') ";
    }
    if (!empty($_GET['email'])) {
        $email_query = " AND payer_email LIKE '%" . $wpdb->escape($_GET['email']) . "%' ";
    }
    if (!empty($_GET['txn_id'])) {
        $txn_id_query = " AND txn_id = '" . $wpdb->escape($_GET['txn_id']) . "' ";
    }
    if (!empty($_GET['hash'])) {
        $hash_query = " AND hash = '" . $wpdb->escape($_GET['hash']) . "' ";
    }
    if (!empty($_GET['status'])) {
        $operator = $_GET['status'] == 1 ? "=" : "!=";
        $status_query = " AND status {$operator} 'Completed' ";
    }
    $current = empty($_GET['paged']) ? 1 : intval($_GET['paged']);
    $items_per_page = 50;
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . my_calendar_payments_table() . "\n\t\t\tWHERE TRUE AND item_number = 1\n\t\t\t{$name_query}\n\t\t\t{$email_query}\n\t\t\t{$txn_id_query}\n\t\t\t{$hash_query}\n\t\t\t{$status_query}\n\t\t\tORDER BY id DESC\n\t\t\tLIMIT " . ($current - 1) * $items_per_page . ", " . $items_per_page;
    $rows = $wpdb->get_results($sql);
    $found_rows = $wpdb->get_col("SELECT FOUND_ROWS();");
    $items = $found_rows[0];
    if ($items > 0) {
        $items_per_page = 50;
        $num_pages = ceil($items / $items_per_page);
        $page_links = paginate_links(array('base' => add_query_arg('paged', '%#%'), 'format' => '', 'prev_text' => __('&laquo;'), 'next_text' => __('&raquo;'), 'total' => $num_pages, 'current' => $current));
        echo "<h4>{$items} Transactions found</h4>";
        if ($num_pages > 1) {
            echo "\n\t\t\t<div class='tablenav'>\n\t\t\t\t<div class='tablenav-pages'>{$page_links}</div>\n\t\t\t</div>";
        }
        echo "<table class='widefat'>\n\t\t<thead>\n\t\t\t<tr>\n\t\t\t\t<th scope='col'>Trans ID</th>\n\t\t\t\t<th scope='col'>Key</th>\n\t\t\t\t<th scope='col'>Price</th>\n\t\t\t\t<th scope='col'>Status</th>\n\t\t\t\t<th scope='col'>Date</th>\n\t\t\t\t<th scope='col'>First</th>\n\t\t\t\t<th scope='col'>Last</th>\n\t\t\t\t<th scope='col'>Email</th>\n\t\t\t\t<th scope='col'>Remaining</th>\n\t\t\t</tr>\n\t\t</thead>\n\t\t<tfoot>\n\t\t\t<tr>\n\t\t\t\t<th scope='col'>Trans ID</th>\n\t\t\t\t<th scope='col'>Key</th>\n\t\t\t\t<th scope='col'>Price</th>\n\t\t\t\t<th scope='col'>Status</th>\n\t\t\t\t<th scope='col'>Date</th>\n\t\t\t\t<th scope='col'>First</th>\n\t\t\t\t<th scope='col'>Last</th>\n\t\t\t\t<th scope='col'>Email</th>\n\t\t\t\t<th scope='col'>Remaining</th>\n\t\t\t</tr>\n\t\t</tfoot>\n\t\t<tbody>";
        foreach ($rows as $row) {
            echo "<tr>";
            echo "<td>{$row->txn_id}</td>";
            echo "<td>{$row->hash}</td>";
            echo "<td>" . $row->price . " " . get_option('mcs_currency') . "</td>";
            echo "<td>{$row->status}</td>";
            echo "<td>{$row->transaction_date}</td>";
            echo "<td>{$row->first_name}</td>";
            echo "<td>{$row->last_name}</td>";
            echo "<td><a href='mailto:{$row->payer_email}'>{$row->payer_email}</a></td>";
            echo "<td>{$row->quantity}/{$row->total}</td>";
            echo "</tr>";
        }
        echo "</tbody>\n\t\t</table>";
    } else {
        _e('You have not yet received any payments for event submissions', 'my-calendar-submissions');
    }
    echo "</div>\n\t</div>\n\t</div>\n\t</div>\n\t</div>";
    ?>
<div class="postbox-container" style="width:20%">
	<div class="metabox-holder">
		<div class="ui-sortable meta-box-sortables">
			<div class="postbox support">
			<h3><?php 
    _e('Search Transactions', 'my-calendar-submissions');
    ?>
</h3>
			<div class='inside'>
				<?php 
    echo "<form method='get' action='" . admin_url('admin.php?page=my-calendar-payments') . "'>";
    echo "<div class='mcs-search'>";
    echo "<p><label for='pname'>Name of Payer</label> <input type='text' id='pname' name='name' value='" . @$_GET['name'] . "' /></p>";
    echo "<p><label for='pemail'>Email of Payer</label> <input type='text' id='pemail' name='email' value='" . @$_GET['email'] . "' /></p>";
    echo "<p><label for='ptrans'>Transaction ID</label> <input type='text' id='ptrans' name='txn_id' value='" . @$_GET['txn_id'] . "' /></p>";
    echo "<p><label for='lkey'>Payment Key</label> <input type='text' id='lkey' name='hash' value='" . @$_GET['hash'] . "' /></p>";
    echo "<p><label for='status'>Status of payment</label> <select name='status' id='status'>";
    $selected = (isset($_GET['status']) and $_GET['status'] == 0) ? "selected='selected'" : "";
    echo "<option value='0' {$selected}>All</option>";
    $selected = (isset($_GET['status']) and $_GET['status'] == 1) ? "selected='selected'" : "";
    echo "<option value='1' {$selected}>Only Completed</option>";
    $selected = (isset($_GET['status']) and $_GET['status'] == 2) ? "selected='selected'" : "";
    echo "<option value='2' {$selected}>Only Invalid</option>";
    echo "</select>";
    echo "</div>";
    echo "<p class='submit'><input type='submit' class='button-secondary' value='Search Payments' /></p>";
    echo "</form>";
    echo "<div class='mcs-earnings'>";
    $earnings = "SELECT SUM(price) FROM " . my_calendar_payments_table() . " WHERE item_number = 1 AND status = 'Completed'";
    $sum = $wpdb->get_var($earnings);
    echo "<p>Earnings to date: \$<strong>{$sum}</strong></p>";
    $earnings = "SELECT SUM(price) FROM " . my_calendar_payments_table() . " WHERE YEAR(transaction_date) = '" . date('Y') . "' AND item_number = 1 AND status = 'Completed'";
    $sum = $wpdb->get_var($earnings);
    echo "<p>Earnings this year: \$<strong>{$sum}</strong></p>";
    $earnings = "SELECT SUM(price) FROM " . my_calendar_payments_table() . " WHERE MONTH(transaction_date) = '" . date('n') . "' AND item_number = 1 AND status = 'Completed'";
    $sum = $wpdb->get_var($earnings);
    echo "<p>Earnings this month: \$<strong>{$sum}</strong></p>\n\t\t\t\t</div>";
    ?>
			</div>
			</div>
		</div>
	</div>
</div>
<?php 
}
    function form($instance)
    {
        mcs_check();
        $options = get_option('mcs_options');
        $fields = $options['fields'];
        $location_fields = $options['location_fields'];
        if (empty($fields)) {
            $options = mcs_default_settings(false);
            $fields = $options['fields'];
            $location_fields = $options['location_fields'];
        }
        $defaults = $options['widget_defaults'];
        $widget_title = !empty($instance['title']) ? esc_attr($instance['title']) : $defaults['title'];
        if (!empty($instance)) {
            $widget_fields = empty($instance['fields']) ? $fields : $instance['fields'];
        } else {
            $widget_fields = $fields;
        }
        if (!empty($instance)) {
            $widget_location_fields = empty($instance['location_fields']) ? $location_fields : $instance['location_fields'];
        } else {
            $widget_location_fields = $location_fields;
        }
        $widget_categories = !empty($instance['categories']) ? esc_attr($instance['categories']) : '';
        $widget_category = !empty($instance['category']) ? esc_attr($instance['category']) : '';
        $widget_locations = !empty($instance['locations']) ? esc_attr($instance['locations']) : '';
        $widget_location = !empty($instance['location']) ? esc_attr($instance['location']) : '';
        ?>
		<p>
			<label for="<?php 
        echo $this->get_field_id('title');
        ?>
"><?php 
        _e('Widget Title', 'my-calendar-submissions');
        ?>
:</label><br />
			<input class="widefat" type="text" id="<?php 
        echo $this->get_field_id('title');
        ?>
" name="<?php 
        echo $this->get_field_name('title');
        ?>
" value="<?php 
        esc_attr_e($widget_title);
        ?>
"/>
		</p>
		<fieldset>
			<legend><strong><?php 
        _e('Included Fields', 'my-calendar-submissions');
        ?>
</strong></legend>
			<ul>
				<li><?php 
        _e('Event Title (required)', 'my-calendar-submissions');
        ?>
</li>
				<li><?php 
        _e('Date/Time (required)', 'my-calendar-submissions');
        ?>
</li>
				<li><?php 
        _e('Name', 'my-calendar-submissions');
        ?>
</li>
				<li><?php 
        _e('Email (required)', 'my-calendar-submissions');
        ?>
</li>
				<?php 
        $checked = " checked='checked'";
        // can select: each field to require (title and date must be included)
        if (is_array($fields)) {
            foreach ($fields as $key => $value) {
                $check = in_array($value, $widget_fields) ? $checked : '';
                $title = mcs_get_field_name($key, $value);
                echo "<li><input type='checkbox' name='" . $this->get_field_name('fields') . "[{$key}]' id='" . $this->get_field_id('title') . "mc_{$key}' value='{$value}'{$check} /> <label for='" . $this->get_field_id('title') . "mc_{$key}'>" . esc_html($title) . "</label></li>\n";
            }
        }
        ?>
			</ul>	
		</fieldset>
		<fieldset>
			<legend><strong><?php 
        _e('Categories', 'my-calendar-submissions');
        ?>
</strong></legend>
			<p>
				<input type="checkbox" name="<?php 
        echo $this->get_field_name('categories');
        ?>
" id="<?php 
        echo $this->get_field_name('categories');
        ?>
" value="true"<?php 
        echo $widget_categories == 'true' ? $checked : '';
        ?>
> <label for="<?php 
        echo $this->get_field_name('categories');
        ?>
"><?php 
        _e('Include list of categories', 'my-calendar-submissions');
        ?>
</label>
			</p>
			<p>
				<select name="<?php 
        echo $this->get_field_name('category');
        ?>
" id="<?php 
        echo $this->get_field_name('category');
        ?>
">
				<?php 
        echo mc_category_select($widget_category);
        ?>
				</select> <label for="<?php 
        echo $this->get_field_name('category');
        ?>
"><?php 
        _e('Default category', 'my-calendar-submissions');
        ?>
</label>
			</p>
		</fieldset>
		<fieldset>
			<legend><strong><?php 
        _e('Locations', 'my-calendar-submissions');
        ?>
</strong></legend>
			<ul>
			<li><input type="radio" name="<?php 
        echo $this->get_field_name('locations');
        ?>
" id="<?php 
        echo $this->get_field_id('locations');
        ?>
choose" value="choose"<?php 
        echo $widget_locations == 'choose' ? $checked : '';
        ?>
> <label for="<?php 
        echo $this->get_field_id('locations');
        ?>
choose"><?php 
        _e('Can choose a location', 'my-calendar-submissions');
        ?>
</label></li>
			<li><input type="radio" name="<?php 
        echo $this->get_field_name('locations');
        ?>
" id="<?php 
        echo $this->get_field_id('locations');
        ?>
either" value="either"<?php 
        echo $widget_locations == 'either' ? $checked : '';
        ?>
> <label for="<?php 
        echo $this->get_field_id('locations');
        ?>
either"><?php 
        _e('Can enter or choose a location', 'my-calendar-submissions');
        ?>
</label></li>
			<li><input type="radio" name="<?php 
        echo $this->get_field_name('locations');
        ?>
" id="<?php 
        echo $this->get_field_id('locations');
        ?>
enter" value="enter"<?php 
        echo $widget_locations == 'enter' ? $checked : '';
        ?>
> <label for="<?php 
        echo $this->get_field_id('locations');
        ?>
enter"><?php 
        _e('Can enter a location', 'my-calendar-submissions');
        ?>
</label></li>
			<li><input type="radio" name="<?php 
        echo $this->get_field_name('locations');
        ?>
" id="<?php 
        echo $this->get_field_id('locations');
        ?>
neither" value="neither"<?php 
        echo $widget_locations == 'neither' ? $checked : '';
        ?>
> <label for="<?php 
        echo $this->get_field_id('locations');
        ?>
neither"><?php 
        _e('None of the above', 'my-calendar-submissions');
        ?>
</label></li>
			</ul>
			<p>
			<label for="<?php 
        echo $this->get_field_name('location');
        ?>
"><?php 
        _e('Default location', 'my-calendar-submissions');
        ?>
</label> <select name="<?php 
        echo $this->get_field_name('location');
        ?>
" id="<?php 
        echo $this->get_field_name('location');
        ?>
">
				<option value=''><?php 
        _e('None', 'my-calendar-submissions');
        ?>
</option>
				<?php 
        echo mc_location_select($widget_location);
        ?>
			</select>
			</p>
		</fieldset>
		<fieldset>
			<legend><strong><?php 
        _e('Included Location Fields', 'my-calendar-submissions');
        ?>
</strong></legend>
			<ul>
			<li><?php 
        _e('Location Label (required)', 'my-calendar-submissions');
        ?>
</li>
			<?php 
        // can select: each field to require (label must be included)
        if (is_array($location_fields)) {
            foreach ($location_fields as $key => $value) {
                $check = in_array($value, $widget_location_fields) ? $checked : '';
                $title = mcs_get_field_name($key, $value);
                echo "<li><input type='checkbox' name='" . $this->get_field_name('location_fields') . "[{$key}]' id='" . $this->get_field_id('location_fields') . "mc_{$key}' value='{$value}'{$check} /> <label for='" . $this->get_field_id('location_fields') . "mc_{$key}'>" . esc_html($title) . "</label></li>\n";
            }
        }
        ?>
			</ul>
		</fieldset>	
		<?php 
    }