<?php

/*
+----------------------------------------------------------------+
+	cetsHelloWorld-tinymce V1.60
+	by Deanna Schneider
+   required for cetsHelloWorld and WordPress 2.5
+----------------------------------------------------------------+
*/

// look up for the path
$path  = ''; // It should be end with a trailing slash

/** That's all, stop editing from here **/

if ( !defined('WP_LOAD_PATH') ) {
	$classic_root = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/' ;
	if (file_exists( $classic_root . 'wp-load.php') )
		define( 'WP_LOAD_PATH', $classic_root);
	else
		if (file_exists( $path . 'wp-load.php') )
			define( 'WP_LOAD_PATH', $path);
		else
			exit("Could not find wp-load.php");
}

// let's load WordPress
require_once( WP_LOAD_PATH. '/wp-load.php');

global $wpdb;

// check for rights
if ( !is_user_logged_in() || !current_user_can('edit_posts') )
	wp_die(__("You are not allowed to be here"));


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>SalesAutomator Button Editor</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<script language="javascript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/wp-includes/js/tinymce/tiny_mce_popup.js"></script>
	<script language="javascript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/wp-includes/js/tinymce/utils/mctabs.js"></script>
	<script language="javascript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/wp-includes/js/tinymce/utils/form_utils.js"></script>
	<script language="javascript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/wp-includes/js/jquery/jquery.js"></script>
	<script language="javascript" type="text/javascript">
	function init() {
		tinyMCEPopup.resizeToInnerSize();
	}

	function insertEppParmsLink() {
            var tagtext ='[product_listing pid=\"';
            var isProductInsert = false;

            jQuery('#my_products input:checkbox').each(function(i){
                if(jQuery(this).attr("checked") == true || jQuery(this).attr("checked") == "checked"){
                    var item_id = jQuery(this).val();
                    tagtext += item_id+', ';
                    isProductInsert = true;
                }
            });
 
           if(isProductInsert == true){
                var leng = tagtext.length - 2;
                tagtext = tagtext.substr(0,leng);
                tagtext += '\"]';
            }

            if(isProductInsert == false){
                tagtext = '';
                var itemName	= document.getElementById('itemName').value;
                var itemNumber	= document.getElementById('itemNumber').value;
                var amount 	    = document.getElementById('amount').value;
                var currency	= document.getElementById('currency').value;
                var duration 	= document.getElementById('duration')?document.getElementById('duration').value:'';
                var interval 	= document.getElementById('interval')?document.getElementById('interval').value:'';
                var subscr_type = document.getElementById('subscr_type')?document.getElementById('subscr_type').value:'';
                var begpaybutton = "[paybutton]";
                var endpaybutton = "[/paybutton]";

                if(subscr_type == 'FREE') {
                    begpaybutton = "[freebutton]";
                    endpaybutton = "[/freebutton]";
                }

                tagtext = begpaybutton;
                if(itemName != '')
                    tagtext += itemName;
                tagtext += "|";
                if(itemNumber != '')
                    tagtext += itemNumber;
                if(duration != '' && subscr_type != 'FREE')
                    tagtext += "[[-" + subscr_type + "-]]";
                tagtext += "|";
                if(amount != '')
                    tagtext += amount;
                tagtext += "|";
                if(currency != '')
                    tagtext += currency;
                tagtext += "|";
                if(duration != '')
                    tagtext += duration;
                tagtext += "|";
                if(interval != '')
                    tagtext += interval;
                tagtext += "|";
                tagtext += endpaybutton;
            }


		if(window.tinyMCE) {
			window.tinyMCE.execInstanceCommand('content', 'mceInsertContent', false, tagtext);
			//Peforms a clean up of the current editor HTML.
			//tinyMCEPopup.editor.execCommand('mceCleanup');
			//Repaints the editor. Sometimes the browser has graphic glitches.
			tinyMCEPopup.editor.execCommand('mceRepaint');
			tinyMCEPopup.close();
		}

		return;
	}
	</script>
	<base target="_self" />
</head>
<body id="link" onload="tinyMCEPopup.executeOnLoad('init();');document.body.style.display='';document.getElementById('itemName').focus();" style="display: none">
	<form name="cetsHelloWorld" action="#">
	<div class="tabs">
		<ul>
			<li id="shortcode_tab" class="current" ><span><a href="javascript:mcTabs.displayTab('shortcode_tab','shortcode_panel');" onmousedown="return false;"><?php _e("ShortCode", 'epp_parms'); ?></a></span></li>
		</ul>
	</div>

	<div class="panel_wrapper">
		<!-- rss panel -->
		<div id="basic_panel" class="panel">
		<br />
		<table border="0" cellpadding="4" cellspacing="0">
         <tr>
            <td nowrap="nowrap"><label for="itemName"><?php _e("Item name:", 'epp_parms'); ?></label></td>
            <td><input type="text" id="itemName" name="itemName" style="width: 190px" />&nbsp;<label><?php _e("*", 'epp_req'); ?></label></td>
          </tr>
         <tr>
            <td nowrap="nowrap"><label for="itemNumber"><?php _e("Item No:", 'epp_parms'); ?></label></td>
            <td><input type="text" id="itemNumber" name="itemNumber" style="width: 100px" />&nbsp;<label><?php _e("*", 'epp_req'); ?></label></td>
          </tr>
         <tr>
            <td nowrap="nowrap"><label for="amount"><?php _e("Amount:", 'epp_parms'); ?></label></td>
            <td><input type="text" id="amount" name="amount" align="right" style="width: 60px" />&nbsp;<label><?php _e("*", 'epp_req'); ?></label>
                &nbsp;&nbsp;&nbsp;<label for="currency"><?php _e("Currency: ", 'epp_parms'); ?></label>
                <input type="text" id="currency" name="currency" style="width: 40px" value="USD"/>
            </td>
          </tr>

          <tr>
            <td nowrap="nowrap"></td>
            <td><label><?php _e("* - Required", 'epp_parms'); ?></label></td>
          </tr>
        </table>
		</div>
		<!-- end rss panel -->

		<div id="shortcode_panel" class="panel current">
<?php
                $qry = "SELECT id, name, price FROM {$wpdb->prefix}wpsa_product_details";
                $results = $wpdb->get_results($qry);
?>

                    <div style="overflow-y:auto; height:135px">
                    <table id="my_products" width="100%" title="Products" style="text-align:center">
                        <thead><tr>
                        <th width="1%"></th>
                        <th width="10%" >ID</th>
                        <th width="78%" >Name</th>
                        <th width="11%">Price</th>
                            </tr></thead>
                        <tr><td></td><td colspan="3"><hr /></td></tr>
<?php
                        foreach ($results as $result) {
?>
                        <tr>
                            <td><input type="checkbox" name="check" value="<?php echo $result->id; ?>" /></td>
                            <td><?php echo $result->id; ?></td>
                            <td><?php echo $result->name; ?></td>
                            <td><?php echo $result->price; ?></td>
                          <!--  <td><?php echo '?'; ?></td>
                            <td><?php echo '?'; ?></td>
                            <td><?php echo '?'; ?></td> -->
                        </tr>
<?php
                        }
?>
                    </table>
		</div>
            </div>
		<!-- end rss panel -->
	</div>

	<div class="mceActionPanel">
		<div style="float: left">
			<input type="button" id="cancel" name="cancel" value="<?php _e("Cancel", 'epp_parms'); ?>" onclick="tinyMCEPopup.close();" />
		</div>

		<div style="float: right">
			<input type="submit" id="insert" name="insert" value="<?php _e("Insert", 'epp_parms'); ?>" onclick="insertEppParmsLink();" />
		</div>
	</div>
</form>
</body>
</html>
<?php

?>