<?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 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  //define('HEADING_TITLE','Cross-sell');
  require('includes/application_top.php');
  require('includes/classes/split_page_results_event.php');
  $languages = tep_get_languages();
  
  $command=isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'';
  if($command!=''){
  $products_id=isset($HTTP_GET_VARS['products_id'])?$HTTP_GET_VARS['products_id']:'';
  $subscription_id=isset($HTTP_GET_VARS['subscription_id'])?$HTTP_GET_VARS['subscription_id']:'';
  	switch($command){
	case 'page_navigation':
		echo 'page_navigation^';
		global $page;
		$page=isset($HTTP_GET_VARS['page'])?$HTTP_GET_VARS['page']:'';
		$default_products_id=product_focus();
		echo '^'.$default_products_id;
		break;
	case 'sub_page_navigation':
		echo 'sub_page_navigation^';
		global $subscription_page;
		$subscription_page=isset($HTTP_GET_VARS['subscription_page'])?$HTTP_GET_VARS['subscription_page']:'';
		$default_subscription_id=subscription_focus();
		echo '^'.$default_subscription_id;
		break;
		
		
	case 'product_focus':
		echo 'products_focus^';
		product_focus();
		break;
	case 'product_xsell_load':
		echo 'products_xsell_load^'.$products_id.'^';
		products_xsell_load($products_id);
		break;
	case 'subscription_xsell_load':
		echo 'subscription_xsell_load^'.$subscription_id.'^';
		subscription_xsell_load($subscription_id);
		break;
	case 'load_xsell_details':
		echo 'load_xsell_details^';
		$products_id=isset($HTTP_GET_VARS['products_id'])?$HTTP_GET_VARS['products_id']:'';
		$xsell_id=isset($HTTP_GET_VARS['xsell_id'])?$HTTP_GET_VARS['xsell_id']:'';
		echo $xsell_id.'^'.$products_id.'^';
		load_xsell_details($xsell_id,$products_id);
		break;	
	case 'load_xsell_subscription_details':
		echo 'load_xsell_subscription_details^';
		$subscription_id=isset($HTTP_GET_VARS['subscription_id'])?$HTTP_GET_VARS['subscription_id']:'';
		$xsell_id=isset($HTTP_GET_VARS['xsell_id'])?$HTTP_GET_VARS['xsell_id']:'';
		echo $xsell_id.'^'.$subscription_id.'^';
		load_xsell_subscription_details($xsell_id,$subscription_id);
		break;
	case 'drop_xsell_products':
		$xsell_id=isset($HTTP_GET_VARS['xsell_id'])?$HTTP_GET_VARS['xsell_id']:'';
		$products_id=isset($HTTP_GET_VARS['products_id'])?$HTTP_GET_VARS['products_id']:'';
		tep_db_query("delete from ".TABLE_PRODUCTS_XSELL." where xsell_id='$xsell_id' and products_id='$products_id' and item_type='P'");
		echo 'products_xsell_load^'.$products_id.'^';
		products_xsell_load($products_id);
		break;
	case 'drop_xsell_subscription':
		$xsell_id=isset($HTTP_GET_VARS['xsell_id'])?$HTTP_GET_VARS['xsell_id']:'';
		$subscription_id=isset($HTTP_GET_VARS['subscription_id'])?$HTTP_GET_VARS['subscription_id']:'';
		tep_db_query("delete from ".TABLE_PRODUCTS_XSELL." where xsell_id='$xsell_id' and products_id='$subscription_id' and item_type='S'");
		echo 'subscription_xsell_load^'.$subscription_id.'^';
		subscription_xsell_load($subscription_id);
		break;
	case 'new_xsell_product':
		require(DIR_WS_CLASSES . 'currencies.php');
		echo 'new_xsell_product^';
		new_xsell_product($products_id);
		break;
	case 'new_xsell_subscription':
		require(DIR_WS_CLASSES . 'currencies.php');
		echo 'new_xsell_subscription^';
		new_xsell_subscription($subscription_id);
		break;
	case 'save_new_xsell_product':
		$sort_order=1;
		$sort_order_query=tep_db_query('select sort_order from '.TABLE_PRODUCTS_XSELL." where products_id='$products_id'");
			if(tep_db_num_rows($sort_order_query)>0){
			$sort_order_array=tep_db_fetch_array($sort_order_query);
			$sort_order=$sort_order_array['sort_order'];
			}
		
		$products_id_array=isset($HTTP_POST_VARS['products_id'])?$HTTP_POST_VARS['products_id']:'';
		$xsell_type_array=isset($HTTP_POST_VARS['xsell_type'])?$HTTP_POST_VARS['xsell_type']:'';
		
			if(is_array($products_id_array) && sizeof($products_id_array)>0){
				for($i=0,$len=sizeof($products_id_array);$i<$len;$i++){
				$xsell_id=$products_id_array[$i];
				$xsell_type=$xsell_type_array[$i];
				tep_db_query('insert into '.TABLE_PRODUCTS_XSELL." (products_id,xsell_id,xsell_type,sort_order,item_type) values('$products_id','$xsell_id','$xsell_type','$sort_order','P')");
				$sort_order++;
				}
			}
		echo 'products_xsell_load^'.$products_id.'^';
		products_xsell_load($products_id);
		break;
	case 'save_new_xsell_subscription':
		$sort_order=1;
		$subscription_id=isset($HTTP_GET_VARS['subscription_id'])?$HTTP_GET_VARS['subscription_id']:'';
		$sort_order_query=tep_db_query('select sort_order from '.TABLE_PRODUCTS_XSELL." where products_id='$subscription_id'");
			if(tep_db_num_rows($sort_order_query)>0){
			$sort_order_array=tep_db_fetch_array($sort_order_query);
			$sort_order=$sort_order_array['sort_order'];
			}
		
		$subscription_id_array=isset($HTTP_POST_VARS['subscription_id_array'])?$HTTP_POST_VARS['subscription_id_array']:'';
		$xsell_type_array=isset($HTTP_POST_VARS['xsell_type'])?$HTTP_POST_VARS['xsell_type']:'';
		
			if(is_array($subscription_id_array) && sizeof($subscription_id_array)>0){
				for($i=0,$len=sizeof($subscription_id_array);$i<$len;$i++){
				$xsell_id=$subscription_id_array[$i];
				$xsell_type=$xsell_type_array[$i];
				tep_db_query('insert into '.TABLE_PRODUCTS_XSELL." (products_id,xsell_id,xsell_type,sort_order,item_type) values('$subscription_id','$xsell_id','$xsell_type','$sort_order','S')");
				$sort_order++;
				}
			}
		echo 'subscription_xsell_load^'.$subscription_id.'^';
		subscription_xsell_load($subscription_id);
		break;
	case 'save_xsell_products_options':
		$xsell_type=isset($HTTP_GET_VARS['xsell_type'])?$HTTP_GET_VARS['xsell_type']:'';
		$xsell_id=isset($HTTP_GET_VARS['xsell_id'])?$HTTP_GET_VARS['xsell_id']:'';
		$products_id=isset($HTTP_GET_VARS['products_id'])?$HTTP_GET_VARS['products_id']:'';
		tep_db_query('update '.TABLE_PRODUCTS_XSELL." set xsell_type='$xsell_type' where products_id='$products_id' and xsell_id='$xsell_id' and item_type='P'");
		echo 'products_xsell_load^'.$products_id.'^';
		products_xsell_load($products_id);
		break;
	case 'save_xsell_subscription_options':
		$xsell_type=isset($HTTP_GET_VARS['xsell_type'])?$HTTP_GET_VARS['xsell_type']:'';
		$xsell_id=isset($HTTP_GET_VARS['xsell_id'])?$HTTP_GET_VARS['xsell_id']:'';
		$subscription_id=isset($HTTP_GET_VARS['subscription_id'])?$HTTP_GET_VARS['subscription_id']:'';
		tep_db_query('update '.TABLE_PRODUCTS_XSELL." set xsell_type='$xsell_type' where products_id='$subscription_id' and xsell_id='$xsell_id' and item_type='S'");
		echo 'subscription_xsell_load^'.$subscription_id.'^';
		subscription_xsell_load($subscription_id);
		break;
	case 'sort_order':
		echo 'sort_order'; 
		$products_id=isset($HTTP_GET_VARS['products_id'])?$HTTP_GET_VARS['products_id']:'';
		$xsell_id=isset($HTTP_GET_VARS['xsell_id'])?$HTTP_GET_VARS['xsell_id']:'';
		$sort_order=isset($HTTP_GET_VARS['sort_order'])?$HTTP_GET_VARS['sort_order']:'';
		$type=isset($HTTP_GET_VARS['type'])?$HTTP_GET_VARS['type']:'';
		$item_type=isset($HTTP_GET_VARS['item_type'])?$HTTP_GET_VARS['item_type']:'';
			if($type=='down')
				$sort_order++;
			elseif($type=='up' && $sort_order>0)
				$sort_order--;
		tep_db_query('update '.TABLE_PRODUCTS_XSELL." set sort_order='$sort_order' where products_id='$products_id' and xsell_id='$xsell_id' and item_type='$item_type'");
		break;
	}
  exit;
  }
  
  function new_xsell_product($products_id){ global $languages_id;
  	$currencies = new currencies();
  	$chk_xsel_query=tep_db_query('select count(*) as total from '.TABLE_PRODUCTS_XSELL." where products_id='$products_id'");
	$chk_xsel_array=tep_db_fetch_array($chk_xsel_query); 
	if($chk_xsel_array['total']>0){
  		//$xsell_product_query=tep_db_query('select distinct pd.products_id,pd.products_name,p.products_price,p.products_tax_class_id from '.TABLE_PRODUCTS_DESCRIPTION." pd, ".TABLE_PRODUCTS_XSELL." px, ".TABLE_PRODUCTS." p where p.products_id=pd.products_id and px.products_id='$products_id' and p.products_id!=px.xsell_id and pd.language_id='$languages_id' and pd.products_id!=px.xsell_id and px.item_type='P' order by products_name");
		$xsell_product_query=tep_db_query('select pd.products_id,pd.products_name,p.products_price,p.products_tax_class_id from '.TABLE_PRODUCTS_DESCRIPTION." pd , ". TABLE_PRODUCTS." p where p.products_id=pd.products_id  and pd.language_id='$languages_id' and p.products_id not in ( select xsell_id from " . TABLE_PRODUCTS_XSELL . " where products_id='$products_id' and item_type='P') order by products_name ");	
	}	
	else 
		$xsell_product_query=tep_db_query('select distinct pd.products_id,pd.products_name,p.products_price,p.products_tax_class_id from '.TABLE_PRODUCTS_DESCRIPTION." pd, ".TABLE_PRODUCTS." p where p.products_id=pd.products_id and pd.language_id='$languages_id' order by products_name");		
    
  echo tep_draw_form('new_xsell_product_form',FILENAME_XSELL_PRODUCTS,'','post');
  ?>
  <br>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr>
  <td width="200" class="main"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.TEXT_ADD_NEW_XSELL_PRODUCT; ?></td>
  <td align="right"><?php echo '<a href="javascript: save_new_xsell_product('.$products_id.');">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','').'</a>&nbsp;&nbsp;<a href="javascript: close_new_xsell_product();">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','').'</a>'; ?></td>
  </tr>
  </table>
  <table width="100%" cellpadding="3" cellspacing="3" border="0">
  <tr>
  <td width="300" class="TableHeading"><?php echo TEXT_PRODUCT_NAME; ?></td>
  <td width="200" class="TableHeading"><?php echo TEXT_TYPE; ?></td>
  <td class="TableHeading"><?php echo TEXT_PRICE; ?></td>
  </tr>
  </table>
  <br>
  <div class="main" style="border:1px solid #7B9EBD; <?php echo ( (tep_db_num_rows($xsell_product_query)<5) ? 'height:100px;' : 'height:200px;'); ?> overflow:auto;">
  <?php /*if(tep_db_num_rows($xsell_product_query)<=0) echo TEXT_ALL_PRODUCTS_ADDED;*/ ?>
  <table width="100%" cellpadding="3" cellspacing="3" border="0">
   <?php
  	while($xsell_product_array=tep_db_fetch_array($xsell_product_query)){
	if($xsell_product_array['products_id']!=$products_id){
  ?>
	<tr><td width="300" class="main">
	<input type="checkbox" value="<?php echo $xsell_product_array['products_id']; ?>" id="products_id[]" name="products_id[]"><?php echo $xsell_product_array['products_name']; ?>
	</td>
	<td width="200">
	<select name="xsell_type[]" id="xsell_type[]" class="inputNormal" onBlur="javascript:toggle_focus(this,1);" onFocus="javascript:toggle_focus(this,2);">
	<option value="G">General</option>
	<option value="F">Forced</option>
	</select>
	</td>
	<td class="main"><?php echo $currencies->display_price($xsell_product_array['products_price'], tep_get_tax_rate($xsell_product_array['products_tax_class_id'])); ?></td>
	
	</tr>
  <?php
  		} // if condition
  	}
  echo '</table></div><br><br></form>';
  }
  
  function new_xsell_subscription($subscription_id){
  $currencies = new currencies();
  	$chk_xsel_query=tep_db_query('select count(*) as total from '.TABLE_PRODUCTS_XSELL." where products_id='$subscription_id'");
	$chk_xsel_array=tep_db_fetch_array($chk_xsel_query);
	if($chk_xsel_array['total']>0){
  		//$xsell_subscription_query=tep_db_query('select distinct sd.subscription_id,sd.subscription_name,s.subscription_costs,s.subscription_tax_class_id from '.TABLE_SUBSCRIPTIONS_DESCRIPTION." sd, ".TABLE_PRODUCTS_XSELL." px, ".TABLE_SUBSCRIPTIONS." s where s.subscription_id=sd.subscription_id and px.products_id='$subscription_id' and sd.subscription_id!=px.xsell_id and px.item_type='S' order by subscription_name");
		$xsell_subscription_query=tep_db_query('select distinct sd.subscription_id,sd.subscription_name,s.subscription_costs,s.subscription_tax_class_id from '.TABLE_SUBSCRIPTIONS_DESCRIPTION." sd, " .TABLE_SUBSCRIPTIONS." s where s.subscription_id=sd.subscription_id and s.subscription_id not in (select xsell_id from ".TABLE_PRODUCTS_XSELL." where products_id='$subscription_id' and item_type='S') order by subscription_name");
	}
	else 
		$xsell_subscription_query=tep_db_query('select distinct sd.subscription_id,sd.subscription_name,s.subscription_costs,s.subscription_tax_class_id from '.TABLE_SUBSCRIPTIONS_DESCRIPTION." sd, ".TABLE_SUBSCRIPTIONS." s where s.subscription_id=sd.subscription_id order by subscription_name");		
  echo tep_draw_form('new_xsell_subscription_form',FILENAME_XSELL_PRODUCTS,'','post');
  ?>
  <br>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr>
  <td width="200" class="main"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.TEXT_ADD_NEW_XSELL_SUBSCRIPTION; ?></td>
  <td align="right"><?php echo '<a href="javascript: save_new_xsell_subscription('.$subscription_id.');">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','').'</a>&nbsp;&nbsp;<a href="javascript: close_new_xsell_subscription();">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','').'</a>'; ?></td>
  </tr>
  </table>
  <table width="100%" cellpadding="3" cellspacing="3" border="0">
  <tr>
  <td width="300" class="TableHeading"><?php echo TEXT_SUBSCRIPTION_NAME; ?></td>
  <td width="200" class="TableHeading"><?php echo TEXT_TYPE; ?></td>
  <td class="TableHeading"><?php echo TEXT_PRICE; ?></td>
  </tr>
  </table>
  <br>
  <div style="border:1px solid #7B9EBD; <?php echo ( tep_db_num_rows(xsell_subscription_query)<5 ? 'height:100px;' : 'height:200px'); ?> overflow:auto;">
  <?php /*if(tep_db_num_rows($xsell_subscription_query)<=1) echo TEXT_ALL_SUBSCRIPTION_ADDED;*/ ?>
  <table width="100%" cellpadding="3" cellspacing="3" border="0">
   <?php
  	while($xsell_subscription_array=tep_db_fetch_array($xsell_subscription_query)){
	if($xsell_subscription_array['subscription_id']!=$subscription_id){
  ?>
	<tr><td width="300" class="main">
	<input type="checkbox" value="<?php echo $xsell_subscription_array['subscription_id']; ?>" id="subscription_id_array[]" name="subscription_id_array[]"><?php echo $xsell_subscription_array['subscription_name']; ?>
	</td>
	<td width="200" class="main">
	<select name="xsell_type[]" id="xsell_type[]" class="inputNormal" onBlur="javascript:toggle_focus(this,1);" onFocus="javascript:toggle_focus(this,2);">
	<option value="G">General</option>
	<option value="F">Forced</option>
	</select>
	</td>
	<td class="main"><?php echo $currencies->display_price($xsell_subscription_array['subscription_costs'], tep_get_tax_rate($xsell_subscription_array['subscription_tax_class_id'])); ?></td>
	
	</tr>
  <?php
  		} // if condition
  	}
  echo '</table></div><br><br></form>';
  }
  
  function product_focus(){
  	global $page;
 	 $products_query_raw="select distinct pd.products_id,pd.products_name from ".TABLE_PRODUCTS." p, ".TABLE_PRODUCTS_DESCRIPTION." pd where p.products_id=pd.products_id order by products_name ";
	 
	$products_split = new splitPageResultsEvent($page, MAX_DISPLAY_SEARCH_RESULTS, $products_query_raw, $products_numrows);	    
    $products_query = tep_db_query($products_query_raw);
	$default_query=tep_db_query($products_query_raw);
	$default_array=tep_db_fetch_array($default_query);
	?>
	
	<?php
	echo '<table width="100%"  cellpadding="0" cellspacing="0" border="0">'; $cnt=0;
	while($products_array=tep_db_fetch_array($products_query)){
	$class_name=($cnt%2==0)?'dataTableRowOdd':'dataTableRowEven';
  ?>
  	<tr>
	<td class="main" style="padding-bottom:5px;">
		<div onClick="javascript: product_load(<?php echo $products_array['products_id']; ?>);" id="products_<?php echo $products_array['products_id']; ?>" class="<?php echo $class_name; ?>" onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this);">
		<?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$products_array['products_name']; ?>
		</div>
		<div class="openContent" id="ajax_products_<?php echo $products_array['products_id']; ?>">
		</div>
	</td>
	</tr>
  <?php
	} // while 
  echo '<tr><td><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2" >' . tep_draw_separator('pixel_trans.gif','1','10'). '</td></tr><tr><td class="smallText" width="50%">'.$products_split->display_count($products_numrows, MAX_DISPLAY_SEARCH_RESULTS, $page, TEXT_DISPLAY_NUMBER_OF_PRODUCTS).'</td><td width="50%" class="smallText" align="right">'.
  $products_split->display_script_links($products_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $page,1,'page_navigation(\'\'','1').'</td></tr></table></td></tr>';
  echo '</table>';
  return $default_array['products_id'];
  }
  
   function subscription_focus(){
  	global $subscription_page;
 	 $subscription_query_raw="select distinct sd.subscription_id,sd.subscription_name from ".TABLE_SUBSCRIPTIONS." s, ".TABLE_SUBSCRIPTIONS_DESCRIPTION." sd where s.subscription_id=sd.subscription_id order by subscription_name ";
	$subscription_split = new splitPageResultsEvent($subscription_page, MAX_DISPLAY_SEARCH_RESULTS, $subscription_query_raw, $subscription_numrows);	    
    $subscription_query = tep_db_query($subscription_query_raw);
	$default_query=tep_db_query($subscription_query_raw);
	$default_array=tep_db_fetch_array($default_query);
	?>
	
	<?php
	echo '<table width="100%" cellpadding="2" cellspacing="2" border="0">'; $cnt=0;
	while($subscription_array=tep_db_fetch_array($subscription_query)){
	$class_name=($cnt%2==0)?'dataTableRowOdd':'dataTableRowEven';
  ?>
  	<tr>
	<td class="main" style="padding-bottom:5px;">
		<div onClick="javascript: subscription_load(<?php echo $subscription_array['subscription_id']; ?>);" id="subscription_<?php echo $subscription_array['subscription_id']; ?>" class="<?php echo $class_name; ?>" onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this);">
		<?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$subscription_array['subscription_name']; ?>
		</div>
		<div id="ajax_subscription_<?php echo $subscription_array['subscription_id']; ?>">
		</div>
	</td>
	</tr>
  <?php
	} // while 
  echo '<tr><td><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2">' . tep_draw_separator('pixel_trans.gif','1','10') . '</td></tr><tr><td class="smallText" width="50%">'.$subscription_split->display_count($subscription_numrows, MAX_DISPLAY_SEARCH_RESULTS, $subscription_page, TEXT_DISPLAY_NUMBER_OF_SUBSCRIPTIONS).'</td><td width="50%" class="smallText" align="right">'.
  $subscription_split->display_script_links($subscription_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $subscription_page,1,'sub_page_navigation(\'\'','1').'</td></tr></table> </td></tr>';
  echo '</table>';
  return $default_array['subscription_id'];
  }
  
  function products_xsell_load($products_id){
  $products_query=tep_db_query('select products_name from '.TABLE_PRODUCTS_DESCRIPTION." where products_id='$products_id' ");
  $products_array=tep_db_fetch_array($products_query);
  $products_name=$products_array['products_name'];
  $products_xsell_query=tep_db_query('select xsell_id from '.TABLE_PRODUCTS_XSELL." where products_id='$products_id' order by sort_order");
  ?>
  <table border="0" cellpadding="0" cellspacing="0" width="100%" class="openContent">
	<tr class="openContent_top">
	<td width="360" class="main" onClick="javascript: base_service_option_close(<?php echo $products_id ?>);"><?php echo tep_image(DIR_WS_IMAGES . "template/icon_active.gif").'&nbsp;&nbsp;'.$products_name; ?></td>			
	<td width="1"  align="right"><?php echo '<a id="base_header_edit_display" style="display:none;" href="javascript: save_services_options('.$option_id.')">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save').'</a>'.'&nbsp;&nbsp;<a style="display:none;" href="javascript: base_service_option_close('.$products_id.');">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close').'</a>'; ?>&nbsp;</td>
	</tr>
	<tr>
	<td colspan="2">
  <?php
  echo '  <table width="100%" cellpadding="0" cellspacing="0" border="0">'; 
  	if(tep_db_num_rows($products_xsell_query)<=0)
		echo '<tr><td align="center" id="no_product_found" class="main">'.TEXT_N0_XSELL_PRODUCTS_FOUND.'</td></tr>';
  $cnt=0;
  while($products_xsell_array=tep_db_fetch_array($products_xsell_query)){
  $xsell_id=$products_xsell_array['xsell_id'];
  $products_query=tep_db_query('select products_name from '.TABLE_PRODUCTS_DESCRIPTION." where products_id='$xsell_id' order by products_name");
  $products_array=tep_db_fetch_array($products_query);
  $class_name=($cnt%2==0)?'subdataTableRowEven':'subdataTableRowOdd';
  ?>
  <tr id="xsell_row_<?php echo $xsell_id; ?>">
  <td width="40">&nbsp;</td>
  <td>
  <div onClick="javascript: load_xsell_details(<?php echo $xsell_id.','.$products_id; ?>)" id="xsell_<?php echo $xsell_id; ?>" class="<?php echo $class_name; ?>" onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this);">
  <?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$products_array['products_name']; ?>
  </div>
  <div id="ajax_xsell_<?php echo $xsell_id; ?>" style="display:none;">
  </div>
  </td>
  </tr>
  <?php 	
  }
  echo '</table>';  
  echo '</td></tr>
  		<tr><td height="100" colspan="2" id="new_xsell_product_content" style="display:none;"></td></tr>
		<tr><td colspan="2"><br></td></tr>
  </table>';
  $default_xsell_id_query=tep_db_query('select xsell_id from '.TABLE_PRODUCTS_XSELL." where products_id='$products_id' order by sort_order");
  		if(tep_db_num_rows($default_xsell_id_query)>0){
		$default_xsell_id_array=tep_db_fetch_array($default_xsell_id_query);
		echo '^'.$default_xsell_id_array['xsell_id'];
		}
  }
  
   function subscription_xsell_load($subscription_id){
  $subscription_query=tep_db_query('select subscription_name from '.TABLE_SUBSCRIPTIONS_DESCRIPTION." where subscription_id='$subscription_id'");
  $subscription_array=tep_db_fetch_array($subscription_query);
  $subscription_name=$subscription_array['subscription_name'];
  $subscription_xsell_query=tep_db_query('select xsell_id from '.TABLE_PRODUCTS_XSELL." where products_id='$subscription_id' order by sort_order");
  ?>
  <table border="0" cellpadding="0" cellspacing="0" width="100%" class="openContent">
	<tr class="openContent_top">
	<td width="400" class="main"  onClick="javascript: base_subscription_option_close(<?php echo $subscription_id ;?>);"><?php echo tep_image(DIR_WS_IMAGES . "template/icon_active.gif").'&nbsp;&nbsp;'.$subscription_name; ?></td>			
	
	<td width="5"  align="right"><?php echo '<a id="base_header_edit_display" style="display:none;" href="javascript: save_services_options('.$option_id.')">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save').'</a>'.'&nbsp;&nbsp;<a style="display:none;" href="javascript: base_subscription_option_close('.$subscription_id.');">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close').'</a>'; ?>&nbsp;</td>
	</tr>
	<tr>
	<td colspan="4" >
  <?php
  echo '  <table width="100%" cellpadding="0" cellspacing="0" border="0">'; $cnt=0;
  		if(tep_db_num_rows($subscription_xsell_query)<=0){
		echo '<tr><td align="center" id="no_subscription_found" class="main">'.TEXT_NO_XSELL_SUBSCRIPTIONS_FOUND.'</td></tr>';
		}
  while($subscription_xsell_array=tep_db_fetch_array($subscription_xsell_query)){
  $xsell_id=$subscription_xsell_array['xsell_id'];
	  $subscription_query=tep_db_query('select subscription_name from '.TABLE_SUBSCRIPTIONS_DESCRIPTION." where subscription_id='$xsell_id' order by subscription_name");
  $subscription_array=tep_db_fetch_array($subscription_query);
  $class_name=($cnt%2==0)?'subdataTableRowEven':'subdataTableRowOdd';
  ?>
  <tr id="xsell_row_<?php echo $xsell_id; ?>">
  <td width="40">&nbsp;</td>
  <td>
  <div onClick="javascript: load_subscription_xsell_details(<?php echo $xsell_id.','.$subscription_id; ?>)" id="xsell_<?php echo $xsell_id; ?>" class="<?php echo $class_name; ?>" onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this);">
  <?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;&nbsp;'.$subscription_array['subscription_name']; ?>
  </div>
  <div id="ajax_xsell_<?php echo $xsell_id; ?>" style="display:none;">
  </div>
  </td>
  </tr>
  <?php 	
  }
  echo '</table>';  
  echo '</td></tr>
  		<tr><td height="100" colspan="4" id="new_xsell_subscription_content" style="display:none;"></td></tr>
		<tr><td colspan="2"><br></td></tr>
  </table>';
  $default_xsell_id_query=tep_db_query('select xsell_id from '.TABLE_PRODUCTS_XSELL." where products_id='$subscription_id' order by sort_order");
  	if(tep_db_num_rows($default_xsell_id_query)>0){
	$default_xsell_id_array=tep_db_fetch_array($default_xsell_id_query);
	echo '^'.$default_xsell_id_array['xsell_id'];
	}
  }
  
  function load_xsell_details($xsell_id,$products_id){
  
  $products_query=tep_db_query('select products_name from '.TABLE_PRODUCTS_DESCRIPTION." where products_id='$xsell_id'");
  $products_array=tep_db_fetch_array($products_query);
  $products_name=$products_array['products_name'];
  
  $xsell_type_query=tep_db_query('select xsell_type,sort_order from '.TABLE_PRODUCTS_XSELL." where products_id='$products_id' and xsell_id='$xsell_id' and item_type='P'");
  $xsell_type_array=tep_db_fetch_array($xsell_type_query);
  $xsell_type=$xsell_type_array['xsell_type'];
  $sort_order=$xsell_type_array['sort_order'];
  ?>
  	<table border="0" cellpadding="0" cellspacing="0" width="100%" class="openContent">
	<tr class="openContent_top">
	<td class="main" onClick="javascript: xsell_option_close(<?php echo $xsell_id ?>);"><?php echo tep_image(DIR_WS_IMAGES . "template/icon_active.gif").'&nbsp;&nbsp;<a href="javascript: move_updown(\'xsell_row_\','.$xsell_id .',\'up\','.$sort_order.','.$products_id.',\'P\')">'.tep_image(DIR_WS_IMAGES."/template/img_arrow_up.gif",'Up').'</a>&nbsp;&nbsp;'
		.'<a href="javascript: move_updown(\'xsell_row_\','.$xsell_id.',\'down\','.$sort_order.','.$products_id.',\'P\')">'.tep_image(DIR_WS_IMAGES."/template/img_arrow_down.gif",'Down').'</a>&nbsp;&nbsp;'.$products_name; ?></td>			
	<td  width="50" align="center">
	<table style="background:#DEE4F5;cursor:pointer;" width="100%" border="0" cellpadding="0" id="header_operation_id" cellspacing="0">
	<tr>
	<td class="cell_bg_popm_left" width="10">&nbsp;</td>
	<td><?php echo   '<a onclick="javascript: drop_xsell_products('.$xsell_id.','.$products_id.');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,1);">'.tep_image(DIR_WS_IMAGES.'template/img_trash.gif','Delete','','','Delete').'</a>'; ?></td>
	<td class="cell_bg_popm_right" width="10">&nbsp;</td>
	</tr>
	</table>
	<td width="200"  onClick="javascript: xsell_option_close(<?php echo $xsell_id ?>);">&nbsp;</td>
	<td width="100" nowrap="nowrap"  align="right"><?php echo '<a id="header_edit_display" style="display:;" href="javascript: save_xsell_products_options('.$products_id.','.$xsell_id.')">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','').'</a>'.'&nbsp;&nbsp;<a href="javascript: xsell_option_close('.$xsell_id.');">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','').'</a>'; ?>&nbsp;</td>
	</tr>
	<tr>
	<td align="center" class="main" colspan="4" style="display:;" id="edit_xsell_details_content">
	<br>
	<?php echo TEXT_XSELL_TYPE.'&nbsp;&nbsp;'; ?>
	<select id="xsell_type" name="xsell_type" class="inputNormal" onBlur="javascript:toggle_focus(this,1);" onFocus="javascript:toggle_focus(this,2);">
	<option <?php if($xsell_type=='G') echo ' selected '; ?> value="G">General</option>
	<option <?php if($xsell_type=='F') echo ' selected '; ?> value="F">Forced</option>
	</select>
	<br><br>
	</td>
	</tr>
	</table>
  <?php
  
  }
  
  function load_xsell_subscription_details($xsell_id,$subscription_id){

  $subscription_query=tep_db_query('select subscription_name from '.TABLE_SUBSCRIPTIONS_DESCRIPTION." where subscription_id='$xsell_id'");
  $subscription_array=tep_db_fetch_array($subscription_query);
  $subscription_name=$subscription_array['subscription_name'];
  
  $xsell_type_query=tep_db_query('select xsell_type,sort_order from '.TABLE_PRODUCTS_XSELL." where products_id='$subscription_id' and xsell_id='$xsell_id' and item_type='S'");
  $xsell_type_array=tep_db_fetch_array($xsell_type_query);
  $xsell_type=$xsell_type_array['xsell_type'];
  $sort_order=$xsell_type_array['sort_order'];
  ?>
  	<table border="0" cellpadding="0" cellspacing="0" width="100%" class="openContent">
	<tr class="openContent_top">
	<td onClick="javascript: xsell_option_close(<?php echo $xsell_id ?>);" class="main"><?php echo tep_image(DIR_WS_IMAGES . "template/icon_active.gif").'&nbsp;&nbsp;<a href="javascript: move_updown(\'xsell_row_\','.$xsell_id .',\'up\','.$sort_order.','.$subscription_id.',\'S\')">'.tep_image(DIR_WS_IMAGES."/template/img_arrow_up.gif",'Up').'</a>&nbsp;&nbsp;'
		.'<a href="javascript: move_updown(\'xsell_row_\','.$xsell_id.',\'down\','.$sort_order.','.$subscription_id.',\'S\')">'.tep_image(DIR_WS_IMAGES."/template/img_arrow_down.gif",'Down').'</a>&nbsp;&nbsp;'.$subscription_name; ?></td>			
	<td  width="50" align="center">
	<table style="background:#DEE4F5;cursor:pointer;" width="100%" border="0" cellpadding="0" id="header_operation_id" cellspacing="0">
	<tr>
	<td class="cell_bg_popm_left" width="10">&nbsp;</td>
	<td ><?php echo   '<a onclick="javascript: drop_xsell_subscription('.$xsell_id.','.$subscription_id.');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,1);">'.tep_image(DIR_WS_IMAGES.'template/img_trash.gif','Delete','','','Delete').'</a>'; ?></td><td class="cell_bg_popm_right" width="10">&nbsp;</td>
	</tr>
	</table>
	<td width="450" onClick="javascript: xsell_option_close(<?php echo $xsell_id ?>);"></td>
	<td   nowrap="nowrap"  align="right"><?php echo '<a id="header_edit_display" style="display:;" href="javascript: save_xsell_subscription_options('.$subscription_id.','.$xsell_id.');">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','').'</a>'.'&nbsp;&nbsp;<a href="javascript: xsell_option_close('.$xsell_id.');">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','').'</a>'; ?>&nbsp;</td>
	</tr>
	<tr>
	<td align="center" class="main" colspan="4" style="display:;" id="edit_xsell_details_content">
	<br>
	<?php echo TEXT_XSELL_TYPE.'&nbsp;&nbsp;'; ?>
	<select id="xsell_type" name="xsell_type" class="inputNormal" onBlur="javascript:toggle_focus(this,1);" onFocus="javascript:toggle_focus(this,2);">
	<option <?php if($xsell_type=='G') echo ' selected '; ?> value="G">General</option>
	<option <?php if($xsell_type=='F') echo ' selected '; ?> value="F">Forced</option>
	</select>
	<br><br>
	</td>
	</tr>
	</table>
  <?php
  
  }
  
  
  tep_get_last_access_file();
  require(DIR_WS_CLASSES . 'currencies.php');
  
  $currencies = new currencies();
  $item_array=array();
  $item_array=array(array('id'=>'P','text'=>TEXT_PRODUCT),array('id'=>'S','text'=>TEXT_SUBSCRIPTION));
  $action=$HTTP_POST_VARS['action'];
  
  $heading_title="Product Name";						
  $cross_associated="Cross-Associated Products";
  $add_query="add_related_product_ID=";
  $query = "select a.products_id, b.products_name, b.products_description, " .
                 "a.products_quantity, a.products_model, a.products_image_1, " .
                 "b.products_url, a.products_price from products a, products_description b where b.products_id = a.products_id  and b.language_id=".$languages_id." order by b.products_name";


 if($action=='action_subscription')
  {
    $heading_title="Subscription Name";
	$cross_associated="Cross-Associated Subscription";
	$add_query="add_related_subscription_ID=";
	$query = "select s.subscription_id, sd.subscription_name, sd.subscription_description,s.subscription_image, " .
                 "sd.subscription_url,s.subscription_costs from subscriptions s, subscriptions_description sd where sd.subscription_id = s.subscription_id order by sd.subscription_name";
			//echo $query;
    
  }
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<title><?php echo TITLE; ?></title>

<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php include(DIR_WS_INCLUDES . 'header.php');  ?>
<!-- header_eof //-->

<!-- Ajax Work Starts -->

<table width="100%" border="0" cellpadding="5" cellspacing="4">
	<tr><td><table cellpadding="0" cellspacing="0" border="0" width="100%">
<?php 
	$type=isset($HTTP_GET_VARS['type'])?$HTTP_GET_VARS['type']:'P';
	if($type=='P'){
?>
<tr style="height:24px;" class="dataTableHeadingRow"><td colspan="2"><?php echo '<a onMouseOver="do_hover_change1(this,2)" onMouseOut="do_hover_change1(this,1);" id="product_header_new_operation">'.tep_image(DIR_WS_IMAGES.'template/img_new.gif','New').'</a>'; ?></td></tr>
<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>	
<tr><td colspan="2" style="padding-left:10px;" class="dataTableHeadingRow"><?php echo TEXT_HEADING_PRODUCT; ?></td></tr>
<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>	
<tr>
<td width="10">&nbsp;</td>
	<td><table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
	<td id="page_navigation_table">
		<?php product_focus(); ?>
	</td>
	</tr>
	</table>
	</td>
</tr>
<tr><td colspan="2"><br></td></tr>
<?php } else { ?>
<tr style="height:24px;" class="dataTableHeadingRow"><td colspan="2"><?php echo '<a onMouseOver="do_hover_change1(this,2);" onMouseOut="do_hover_change1(this,1);" id="subscription_header_new_operation">'.tep_image(DIR_WS_IMAGES.'template/img_new.gif','New').'</a>'; ?></td></tr>
<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>	
<tr><td colspan="2" style="padding-left:10px;" class="dataTableHeadingRow"><?php echo TEXT_HEADING_SUBSCRIPTION; ?></td></tr>
<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>	
	<tr>
	<td>&nbsp;</td>
	<td><table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
	<td id="sub_page_navigation_table"><?php subscription_focus(); ?></td>
	</tr>
	</table>
	</td>
	</tr>
<?php } ?>
</table></td></tr>	
</table>

<!-- Ajax Work Ends -->

<!-- footer //-->
<?php include(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->

</body>
</html>
<?php include(DIR_WS_INCLUDES . 'application_bottom.php');?>

<script language="javascript">
var previous_products_id='',previous_subscription_id='';
var previous_xsell_id='';
	function do_hover_change(element,mode){
		if (element.mode && mode==element.mode) return;
		if (mode==2)
			element.firstChild.src=element.firstChild.src.replace(".gif","_hover.gif");
		else
			element.firstChild.src=element.firstChild.src.replace("_hover.gif",".gif");
		element.mode=mode;
	}
	var display_flag=1;
		function do_hover_change1(element,mode){
		if(display_flag==1){
		if (element.mode && mode==element.mode) return;
		if (mode==2)
			element.firstChild.src=element.firstChild.src.replace(".gif","_hover.gif");
		else
			element.firstChild.src=element.firstChild.src.replace("_hover.gif",".gif");
		}
		element.mode=mode;
		}
	
var ajax_image_load='<?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?>';
	function page_navigation(arg1,agr2,agr3,agr4){
		var page_no=1;
		if(agr2==1) var obj=document.getElementById("page_1");
		else if(agr2==2) var obj=document.getElementById("page_2");
		if(agr2 && agr4){
			if(obj){
 	 			page_no=parseInt(obj.options[obj.selectedIndex].value);
 	 			if(agr4=='next') {
 	 				if(obj.length==obj.selectedIndex+1) return;
 	 				page_no+=1;
 	 			}else if(agr4=='prev') {
	 	 			if(obj.selectedIndex==0) return;
 		 			page_no-=1;
 	 			}
 	 			if(page_no<=1) page_no=1;
 	 		} 	 		
		}				
		if(agr2==1) command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=page_navigation'); ?>&page="+page_no;
		else if(agr2==2) command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=page_navigation'); ?>&page="+page_no;		
		if(command!="") do_get_command(command);
	}
	function sub_page_navigation(arg1,agr2,agr3,agr4){
		var page_no=1;
		if(agr2==1) var obj=document.getElementById("page_1");
		else if(agr2==2) var obj=document.getElementById("page_2");
		if(agr2 && agr4){
			if(obj){
 	 			page_no=parseInt(obj.options[obj.selectedIndex].value);
 	 			if(agr4=='next') {
 	 				if(obj.length==obj.selectedIndex+1) return;
 	 				page_no+=1;
 	 			}else if(agr4=='prev') {
	 	 			if(obj.selectedIndex==0) return;
 		 			page_no-=1;
 	 			}
 	 			if(page_no<=1) page_no=1;
 	 		} 	 		
		}				
		if(agr2==1) command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=sub_page_navigation'); ?>&subscription_page="+page_no;
		else if(agr2==2) command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=sub_page_navigation'); ?>&subscription_page="+page_no;		
		if(command!="") do_get_command(command);
	}
	function row_focus(type){
		if(type=='P'){
		var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=product_focus'); ?>";
		do_get_command(command);
		document.getElementById('ajax_product').style.display='';
		document.getElementById('ajax_product').innerHTML=ajax_image_load;
		}
	}
<?php
	if($type=='P'){
	$default_product_id_query=tep_db_query('select products_id from '.TABLE_PRODUCTS_DESCRIPTION." where language_id='$languages_id' order by products_name");
	if(tep_db_num_rows($default_product_id_query)>0){
	$default_product_id_array=tep_db_fetch_array($default_product_id_query);
	echo 'product_load('.$default_product_id_array['products_id'].');';
	}
	} else {
	$default_subscription_id_query=tep_db_query('select subscription_id from '.TABLE_SUBSCRIPTIONS_DESCRIPTION." where language_id='$languages_id' order by subscription_name");
	if(tep_db_num_rows($default_subscription_id_query)>0){
	$default_subscription_id_array=tep_db_fetch_array($default_subscription_id_query);
	echo 'subscription_load('.$default_subscription_id_array['subscription_id'].');';
	}
	}
?>
	function product_load(products_id){
		/*if(previous_subscription_id!='')
			base_subscription_option_close(previous_subscription_id);*/
		if(previous_products_id!='')
			base_service_option_close(previous_products_id);
	document.getElementById('ajax_products_'+products_id).style.display='';
	document.getElementById('ajax_products_'+products_id).innerHTML=ajax_image_load;
	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=product_xsell_load&products_id='); ?>"+products_id;
	do_get_command(command);
	previous_products_id=products_id;
	change_new_image(document.getElementById('product_header_new_operation'));
	}
	function subscription_load(subscription_id){
		/*if(previous_products_id!='')
			base_service_option_close(previous_products_id);*/
		if(previous_subscription_id!='')
			base_subscription_option_close(previous_subscription_id);
	document.getElementById('ajax_subscription_'+subscription_id).style.display='';
	document.getElementById('ajax_subscription_'+subscription_id).innerHTML=ajax_image_load;
	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=subscription_xsell_load&subscription_id='); ?>"+subscription_id;
	do_get_command(command);
	previous_subscription_id=subscription_id;
	change_new_image(document.getElementById('subscription_header_new_operation'));
	}
	function load_xsell_details(xsell_id,products_id){
	document.getElementById('new_xsell_product_content').innerHTML='';
	document.getElementById('new_xsell_product_content').style.display='none';
		if(previous_xsell_id!='')
			xsell_option_close(previous_xsell_id);
	document.getElementById('ajax_xsell_'+xsell_id).style.display='';
	document.getElementById('ajax_xsell_'+xsell_id).innerHTML=ajax_image_load;

	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=load_xsell_details&xsell_id='); ?>"+xsell_id+'&products_id='+products_id;
	do_get_command(command);
	previous_xsell_id=xsell_id;
	}
	function load_subscription_xsell_details(xsell_id,subscription_id){
	document.getElementById('new_xsell_subscription_content').innerHTML='';
	document.getElementById('new_xsell_subscription_content').style.display='none';
		if(previous_xsell_id!='')
			xsell_option_close(previous_xsell_id);
	document.getElementById('ajax_xsell_'+xsell_id).style.display='';
	document.getElementById('ajax_xsell_'+xsell_id).innerHTML=ajax_image_load;

	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=load_xsell_subscription_details&xsell_id='); ?>"+xsell_id+'&subscription_id='+subscription_id;
	do_get_command(command);
		previous_xsell_id=xsell_id;
	}
	function edit_xsell_details(){ 
	document.getElementById('edit_xsell_details_content').style.display='';
	document.getElementById('header_edit_display').style.display='';
	}
	function save_xsell_products_options(products_id,xsell_id){
	var xsell_type=document.getElementById('xsell_type').value;
	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=save_xsell_products_options&xsell_type='); ?>"+xsell_type+'&products_id='+products_id+'&xsell_id='+xsell_id;
	do_get_command(command);
	}
	function save_xsell_subscription_options(subscription_id,xsell_id){
	var xsell_type=document.getElementById('xsell_type').value;
	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=save_xsell_subscription_options&xsell_type='); ?>"+xsell_type+'&subscription_id='+subscription_id+'&xsell_id='+xsell_id;
	do_get_command(command);
	}
	function xsell_option_close(xsell_id){
		if(document.getElementById('ajax_xsell_'+xsell_id) && document.getElementById('ajax_xsell_'+xsell_id)){
		document.getElementById('ajax_xsell_'+xsell_id).innerHTML='';
		document.getElementById('ajax_xsell_'+xsell_id).style.display='none';
		document.getElementById('xsell_'+xsell_id).style.display='';
		}

	}
	function drop_xsell_products(xsell_id,products_id){
	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=drop_xsell_products&xsell_id='); ?>"+xsell_id+'&products_id='+products_id;
	do_get_command(command);
	}
	function drop_xsell_subscription(xsell_id,subscription_id){
	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=drop_xsell_subscription&xsell_id='); ?>"+xsell_id+'&subscription_id='+subscription_id;
	do_get_command(command);
	}
	function base_service_option_close(products_id){
		if(document.getElementById('ajax_products_'+products_id) && document.getElementById('products_'+products_id)){
		document.getElementById('ajax_products_'+products_id).innerHTML='';
		document.getElementById('ajax_products_'+products_id).style.display='none';
		document.getElementById('products_'+products_id).style.display='';
		}
	document.getElementById('product_header_new_operation').href='#';
	}
	function base_subscription_option_close(subscription_id){
		if(document.getElementById('ajax_subscription_'+subscription_id) && document.getElementById('subscription_'+subscription_id)){
		document.getElementById('ajax_subscription_'+subscription_id).innerHTML='';
		document.getElementById('ajax_subscription_'+subscription_id).style.display='none';
		document.getElementById('subscription_'+subscription_id).style.display='';
		}
	document.getElementById('subscription_header_new_operation').href='#';
	}
	function new_xsell_product(products_id){
	if(document.getElementById('no_product_found'))
		document.getElementById('no_product_found').style.display='none';
	display_flag=0;
		if(previous_xsell_id!='')
			xsell_option_close(previous_xsell_id);
	document.getElementById('new_xsell_product_content').style.display='';
	document.getElementById('new_xsell_product_content').innerHTML=ajax_image_load;
	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=new_xsell_product&products_id='); ?>"+products_id;
	do_get_command(command);
	}
	function new_xsell_subscription(subscription_id){
	display_flag=0
	if(document.getElementById('no_subscription_found'))
		document.getElementById('no_subscription_found').style.display='none';
		if(previous_xsell_id!='')
			xsell_option_close(previous_xsell_id);
	document.getElementById('new_xsell_subscription_content').style.display='';
	document.getElementById('new_xsell_subscription_content').innerHTML=ajax_image_load;
	var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=new_xsell_subscription&subscription_id='); ?>"+subscription_id;
	do_get_command(command);
	}
	function save_new_xsell_product(products_id){
	 var error_result="";
	var prod_error=true;
		for(i=0;i<document.getElementsByName("products_id[]").length;i++){
			if(document.getElementsByName("products_id[]")[i].checked==true)
			prod_error=false;	
			}
			if(prod_error==true)
			error_result="<?php echo tep_db_input('select anyone products'); ?>";
	if(document.getElementById('no_product_found'))
		document.getElementById('no_product_found').style.display='';
		if(error_result!="")		
			alert(error_result);	
		else{
	do_post_command('new_xsell_product_form','<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=save_new_xsell_product&products_id='); ?>'+products_id);
	change_new_image(document.getElementById('product_header_new_operation'));
	}
	}
	function save_new_xsell_subscription(subscription_id){
	var error_result="";
	var sub_error=true;
	for(i=0;i<document.getElementsByName("subscription_id_array[]").length;i++){
	//alert('ss');
	if(document.getElementsByName("subscription_id_array[]")[i].checked==true)
			sub_error=false;
	}
	if(sub_error==true)
			error_result="<?php echo tep_db_input('select anyone subscription'); ?>";
	if(document.getElementById('no_subscription_found'))
		document.getElementById('no_subscription_found').style.display='';
		if(error_result!="")		
			alert(error_result);	
		else{
	change_new_image(document.getElementById('subscription_header_new_operation'));
	do_post_command('new_xsell_subscription_form','<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=save_new_xsell_subscription&subscription_id='); ?>'+subscription_id);
	}
	}
	function close_new_xsell_product(){
	if(document.getElementById('no_product_found'))
		document.getElementById('no_product_found').style.display='';
	document.getElementById('new_xsell_product_content').innerHTML='';
	document.getElementById('new_xsell_product_content').style.display='none';
	change_new_image(document.getElementById('product_header_new_operation'));
	}
	function change_new_image(element){
	display_flag=1;
	element.firstChild.src=element.firstChild.src.replace('_hover.gif','.gif');
	}
	function close_new_xsell_subscription(){
	if(document.getElementById('no_subscription_found'))
		document.getElementById('no_subscription_found').style.display='';
	change_new_image(document.getElementById('subscription_header_new_operation'));
	document.getElementById('new_xsell_subscription_content').innerHTML='';
	document.getElementById('new_xsell_subscription_content').style.display='none';
	}
	function do_result(result){ 
	var token=result.split('^');
	 switch(token[0]){
	  case 'products_focus':
	  	document.getElementById('ajax_product').innerHTML=token[1];
	 	 break;
	  case 'page_navigation':
	  	document.getElementById('page_navigation_table').innerHTML=token[1];
			if(token[2]!='')
				product_load(token[2]);
	  	break;
	  case 'sub_page_navigation':
	  	document.getElementById('sub_page_navigation_table').innerHTML=token[1];
			if(token[2]!='')
				subscription_load(token[2]);
	  	break;
	  case 'products_xsell_load':
	  	var products_id=token[1];
		document.getElementById('products_'+products_id).style.display='none';
	  	document.getElementById('ajax_products_'+products_id).innerHTML=token[2];
			if(token.length>3)
				load_xsell_details(token[3],products_id);
		document.getElementById('product_header_new_operation').href="javascript: new_xsell_product("+products_id+");";
	  	break;
	 case 'subscription_xsell_load':
	 	var subscription_id=token[1];
		document.getElementById('subscription_'+subscription_id).style.display='none';
		document.getElementById('ajax_subscription_'+subscription_id).innerHTML=token[2];
			if(token.length>3)
				load_subscription_xsell_details(token[3],subscription_id);
		document.getElementById('subscription_header_new_operation').href="javascript: new_xsell_subscription("+subscription_id+")";
	 	break;
	 case 'load_xsell_details':
	    var xsell_id=token[1],products_id=token[2]; 
		document.getElementById('xsell_'+xsell_id).style.display='none';
		document.getElementById('ajax_xsell_'+xsell_id).innerHTML=token[3];
	 	break;
	case 'load_xsell_subscription_details':
		var xsell_id=token[1],subscription_id=token[2];
		document.getElementById('xsell_'+xsell_id).style.display='none';
		document.getElementById('ajax_xsell_'+xsell_id).innerHTML=token[3];
		break;
	 case 'new_xsell_product':
	 	document.getElementById('new_xsell_product_content').innerHTML=token[1];
	 	break;
	 case 'new_xsell_subscription':
	 	document.getElementById('new_xsell_subscription_content').innerHTML=token[1];
	 	break;
	 }
	}
	
	function get_installed_count(table){
					var icnt;
					var n;
					for (icnt=0,n=table.rows.length;icnt<n;icnt++){
						if (table.rows[icnt].cur_position=="inactive") break;
					}
					return icnt;
				}
	function move_updown(prefix,option_id,type,sort_order,products_id,item_type){ 
		var row=document.getElementById(prefix+option_id);
		var index=row.rowIndex;
		var count=get_installed_count(row.parentNode);
		if (count<=1) return;
		if (type=="up" && index==0) return;
		if (type=="down" && index==count-1) return;
			if (type=="up"){
			row.parentNode.insertBefore(row,row.parentNode.rows[index-1]);
			} else {
			if (index==row.parentNode.rows.length-2)
				row.parentNode.appendChild(row);
			else
				row.parentNode.insertBefore(row,row.parentNode.rows[index+2]);
			}
		var command="<?php echo tep_href_link(FILENAME_XSELL_PRODUCTS,'command=sort_order&products_id='); ?>"+products_id+'&xsell_id='+option_id+'&sort_order='+sort_order+'&type='+type+'&item_type='+item_type;
		do_get_command(command);
			
		realign_row_class(row.parentNode);
		}
		function realign_row_class(table){
					var icnt,n;
					for (icnt=0,n=table.rows.length;icnt<n;icnt++){
						if (table.rows[icnt].opened && table.rows[icnt].opened==1) {
							continue;
						}
						if (table.rows[icnt].className.indexOf("hover")!=-1) continue;

						if (icnt%2==0){
							table.rows[icnt].className="dataTableRowEven";
						} else {
							table.rows[icnt].className="dataTableRowOdd";
						}
					}
		}
</script>
