<?php
/*

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce
  
  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
*/
// Set flag that this is a parent file
	defined('_FEXEC') or die();



  require(DIR_WS_CLASSES . 'payment.php');
  include(DIR_WS_CLASSES . 'order.php');
  include(DIR_WS_FUNCTIONS . 'subscription_tree.php');
  echo "new_subscription||";
?>
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
	  <tr>
	<td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
	  <tr >
		<td class="main"><B><?php echo SUBSCRIPTION_ADDING_TITLE; ?></b> </td>
		<?php	print '<td id="show_subscription_save_icon_up" width=10% nowrap style="display:none" colspan=3 class="main" align="right" style="padding-left:10px;padding-right:10px;padding-top:3px;"><a href="javascript:validate_subscription_form();">' . tep_image(DIR_WS_IMAGES .  'template/img_savel.gif','Save') .'</a>&nbsp;
					<a href="javascript:do_page_fetch(\'ajx_close_subscription_new\');">' . tep_image(DIR_WS_IMAGES .  'template/img_closel.gif','Close') . '</a>
				   </td>';?>
	  </tr>
	</table></td>
  </tr>
<?php
// Add Subscriptions
/*if($action == "add_subscription")
{
  $mode=(isset($HTTP_GET_VARS['mode'])?$HTTP_GET_VARS['mode']:'');
  $id=(isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:'');
  $add_step=(isset($HTTP_POST_VARS['add_step'])?(int)$HTTP_POST_VARS['add_step']:0);
  if ((int)$add_step==0 && $mode=='edit'){
  	$data_sql = "SELECT op.products_id,p2c.categories_id,op.products_quantity from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where op.orders_products_id='" . $id . "' and op.products_id=p2c.products_id";
	 $data_query=tep_db_query($data_sql);
     $data_result=tep_db_fetch_array($data_query);
     $add_subscription_categories_id=$data_result['categories_id'];
     $subscriptions_id=$data_result['products_id'];
     $add_subscription_quantity=$data_result['products_quantity'];
     $step=3;
  }*/
?>
<?php
	// ############################################################################
	//   Get List of All Products
	// ############################################################################

		$result = tep_db_query("SELECT subscription_name, su.subscription_id, subscription_categories_name, suTsuc.subscription_categories_id FROM " . TABLE_SUBSCRIPTIONS . " su LEFT JOIN " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud ON sud.subscription_id=su.subscription_id LEFT JOIN " . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " suTsuc ON suTsuc.subscription_id=su.subscription_id LEFT JOIN " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " scd ON scd.subscription_categories_id=suTsuc.subscription_categories_id where su.subscription_id = suTsuc.subscription_id " . SUBSCRIPTION_CONDITION_STATUS . "  ORDER BY subscription_categories_name");
		$SubscriptionList = array();
		while($row = tep_db_fetch_array($result))
		{
			extract($row,EXTR_PREFIX_ALL,"db");
			$SubscriptionList[$db_subscription_categories_id][$db_subscription_id] = $db_subscription_name;
			$Subscription_CategoryList[$db_subscription_id] = $db_subscription_categories_name;
			$LastCategory = $db_subscription_categories_name;
		}
		// ksort($ProductList);
		$LastOptionTag = "";
		$SubscriptionSelectOptions = "<option value='0'>Don't Add New Subscription" . $LastOptionTag . "\n";
		$SubscriptionSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
		foreach($SubscriptionList as $subscription_Category =>$subscription)
		{
			$SubscriptionSelectOptions .= "<option value='0'>$subscription_Category" . $LastOptionTag . "\n";
			$SubscriptionSelectOptions .= "<option value='0'>---------------------------" . $LastOptionTag . "\n";
			asort($subscription);
			foreach($subscription as $subscription_ID => $subscription_Name)
			{
				$SubscriptionSelectOptions .= "<option value='$subscription_id'> &nbsp; $subscription_Name" . $LastOptionTag . "\n";
			}
			
			if($subscription_Category != $LastCategory)
			{
				$SubscriptionSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
				$SubscriptionSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
			}
		}
	// ############################################################################
	//   Add Products Steps
	// ############################################################################
		print "<tr><td><form name='add_new_subscription' action='" . FILENAME_SHOPPING_CART . "?subs_action=add_subscription' method='POST' onSubmit='return validate_subscription_form();'><table border='0'>\n";
		// Set Defaults
			if(!IsSet($add_subscription_categories_id))
			$add_subscription_categories_id = 0;

			if(!IsSet($subscriptions_id))
			$subscriptions_id = 0;
		// Step 1: Choose Product
			print "<tr >\n";
			print "<td class='dataTableContent' align='left' width=10% nowrap><b>Select Subscription:</b>&nbsp;&nbsp;";
			//echo tep_draw_pull_down_menu('subscriptions_id',tep_get_subscription_display_tree(),$subscriptions_id);
			$subscription_array = array();
			$subscription_array = tep_get_subscription_array_single();
			if(sizeof($subscription_array)<=0) echo 'Subscription Are Not Available';
			else echo tep_draw_subscription_select_menu('subscriptions_id',$subscription_array,$subscriptions_id,' id="subscriptions_id" onchange="javascript:do_page_fetch(\'new_subscription_step2\');"');
			//print "<td class='dataTableContent' align='center'>";
			//print '<a href="javascript:do_page_fetch(\'new_subscription_step2\');">';
			//print "<input type='button' value='Select'></a>";
			//print "</td>\n";
			print "</tr>";
			print "<tr><Td id=new_subscription_step2></td></tr>";
			print "</table></form>\n";
			print "</td>";
			print "</tr>\n";
			print "<tr><Td id='subscription_submit' colspan='3'></td></tr>";
			print '<tr ><td id="show_subscription_save_icon_down" style="display:none" colspan=3 class="main" align="right" style="padding-left:10px;padding-right:10px;padding-top:3px;"><a href="javascript:validate_subscription_form();">' . tep_image(DIR_WS_IMAGES .  'template/img_savel.gif','Save') .'</a>&nbsp;
					<a href="javascript:do_page_fetch(\'ajx_close_subscription_new\');">' . tep_image(DIR_WS_IMAGES .  'template/img_closel.gif','Close') . '</a>
				   </td></tr>';
			print "</table>";
 if($cos_and_type_value['subscription_date_type'] == 'P'){?>
<script language="javascript" type="text/javascript">
	check_date();
</script>
<?php }?>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<?php
  ////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // Function    : tep_field_exists
  //
  // Arguments   : table	table name
  //               field	field name
  //
  // Return      : true/false
  //
  // Description : Function to check the existence of a database field
  //
  ////////////////////////////////////////////////////////////////////////////////////////////////
  function tep_field_exists($table,$field) {

    $describe_query = tep_db_query("describe $table");
    while($d_row = tep_db_fetch_array($describe_query))
    {
      if ($d_row["Field"] == "$field")
      return true;
    }


    return false;
  }
?>