<?php
/**
 * @package admin
 * @copyright Copyright 2003-2011 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: category_product_listing.php 18695 2011-05-04 05:24:19Z drbyte $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}

if (isset($_GET['set_display_categories_dropdown'])) {
  $_SESSION['display_categories_dropdown'] = $_GET['set_display_categories_dropdown'];
}
if (!isset($_SESSION['display_categories_dropdown'])) {
  $_SESSION['display_categories_dropdown'] = 0;
}
?>
<script language="javascript" type="text/javascript" src="../ajax/jquery.js"></script>
<script language="javascript" type="text/javascript" src="../ajax/jquery.form.js"></script>
<script language="javascript" type="text/javascript" src="../ajax/products_with_attributes_stock_ajax.js"></script>
<script language="javascript" type="text/javascript">
var suppliers = {
    <?php
	$products_supplier_name = "select suppliers_id,suppliers_name from zen_suppliers order by suppliers_name";
	$products_supplier_list = $db->Execute($products_supplier_name);
	$str = "";
	while (!$products_supplier_list->EOF) {
		
		$str .= $products_supplier_list->fields['suppliers_id'];
		$str .= ":";
		$str .= "\"";
		$str .= $products_supplier_list->fields['suppliers_name'];
		$str .= "\"";
		
		$str .= ",";
		$products_supplier_list->MoveNext();
	}
	$str = substr($str,0,strlen($str)-1);
	echo($str);
	?>
};
var manufacturers = {
    <?php
	$products_manufacturer_name = "select manufacturers_id,manufacturers_name from zen_manufacturers order by manufacturers_name";
	$products_manufacturer_list = $db->Execute($products_manufacturer_name);
				
	$str = "";
	while (!$products_manufacturer_list->EOF) {
		
		$str .= $products_manufacturer_list->fields['manufacturers_id'];
		$str .= ":";
		$str .= "\"";
		$str .= $products_manufacturer_list->fields['manufacturers_name'];
		$str .= "\"";
		
		$str .= ",";
		$products_manufacturer_list->MoveNext();
	}
	$str = substr($str,0,strlen($str)-1);
	echo($str);
	?>
};



$('.searchTxtfieldC').bind('keyup', function(e) {
    if ( e.keyCode === 13 ) { // 13 is enter key
		$("#searchTxtfieldC").submit();
    }
});

$('.searchTxtfieldI').bind('keyup', function(e) {
    if ( e.keyCode === 13 ) { // 13 is enter key
		$("#searchTxtfieldI").submit();
    }

});

$('.searchTxtfieldM').bind('keyup', function(e) {
    if ( e.keyCode === 13 ) { // 13 is enter key
		$("#searchTxtfieldM").submit();
    }

});

$('.searchTxtfieldB').bind('keyup', function(e) {
    if ( e.keyCode === 13 ) { // 13 is enter key
		$("#searchTxtfieldB").submit();
    }

});


</script>
<style>
.searchform{
float:right;
}
</style>

    <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?>&nbsp;-&nbsp;<?php echo zen_output_generated_category_path($current_category_id); ?></td>
            <td class="pageHeading" align="right"><?php echo zen_draw_separator('pixel_trans.gif', 1, HEADING_IMAGE_HEIGHT); ?></td>
            <td align="right"><table border="0" width="100%" cellspacing="0" cellpadding="0">
              <tr>
                <td class="smallText" align="right">
<?php
    echo zen_draw_form('search', FILENAME_CATEGORIES, '', 'get');
// show reset search
    if (isset($_GET['search']) && zen_not_null($_GET['search'])) {
      echo '<a href="' . zen_href_link(FILENAME_CATEGORIES) . '">' . zen_image_button('button_reset.gif', IMAGE_RESET) . '</a>&nbsp;&nbsp;';
    }
    //echo HEADING_TITLE_SEARCH_DETAIL;
	
	if($_GET['searchType'] == "Categories"){
		$catstxt = $_GET['search'];
	}
	if($_GET['searchType'] == "id"){
		$idstxt = $_GET['search'];
	}
	if($_GET['searchType'] == "model"){
		$modelstxt = $_GET['search'];
	}
	if($_GET['searchType'] == "barcode"){
		$barcodestxt = $_GET['search'];
	}
	
	echo('
		<div class="searchform">Barcode<br/>
		<form method="get" name="search" id="searchTxtfieldB">
			<input type="hidden" name="searchType" value="barcode">
			<input type="text" name="search" class="searchTxtfieldB" value="'.$barcodestxt.'">
		</form>
		</div>
	');
	echo('
		<div class="searchform">Model<br/>
		<form method="get" name="search" id="searchTxtfieldM">
			<input type="hidden" name="searchType" value="model">
			<input type="text" name="search" class="searchTxtfieldM" value="'.$modelstxt.'">
		</form>
		</div>
	');
	echo('
		<div class="searchform">ID<br/>
		<form method="get" name="search" id="searchTxtfieldI">
			<input type="hidden" name="searchType" value="id">
			<input type="text" name="search" class="searchTxtfieldI" value="'.$idstxt.'">
		</form>
		</div>
	');
	echo('
		<div class="searchform">Category<br/>
		<form method="get" name="search" id="searchTxtfieldC">
			<input type="hidden" name="searchType" value="Categories">
			<input type="text" name="search" class="searchTxtfieldC" value="'.$catstxt.'">
		</form>
		</div>
	');
	
	echo('<div style="clear:both"></div>');
	
	/*
    $searchtype = $_GET['searchType'];
    echo ("
    	<select name=\"searchType\">
    ");
    if($searchtype == "Categories")
    {
    		echo("<option value=\"Categories\" selected>Categories/Products</option>");
    }else{
    		echo("<option value=\"Categories\">Categories/Products</option>");
    }
    
    if($searchtype == "id")
    {
    		echo("<option value=\"id\" selected>ID</option>");
    }else{
    		echo("<option value=\"id\">ID</option>");
    }
    
    if($searchtype == "model")
    {
    		echo("<option value=\"model\" selected>Model</option>");
    }else{
    		echo("<option value=\"model\">Model</option>");
    }
    
    if($searchtype == "barcode")
    {
    		echo("<option value=\"barcode\" selected>Barcode</option>");
    }else{
    		echo("<option value=\"barcode\">Barcode</option>");
    }		
    
    echo("</select>");
    
    echo ' ' . zen_draw_input_field('search');
	
	*/
	
	
    echo zen_hide_session_id();
    if (isset($_GET['search']) && zen_not_null($_GET['search'])) {
      $keywords = zen_db_input(zen_db_prepare_input($_GET['search']));
      echo '<br />' . TEXT_INFO_SEARCH_DETAIL_FILTER . $keywords;
    }
    echo '</form>';
?>
                </td>
              </tr>
              <tr>
                <td class="smallText" align="right">
<?php
  if ($_SESSION['display_categories_dropdown'] == 0) {
    echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'set_display_categories_dropdown=1&cID=' . $categories->fields['categories_id'] . '&cPath=' . $cPath . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')) . '">' . zen_image(DIR_WS_ICONS . 'cross.gif', IMAGE_ICON_STATUS_OFF) . '</a>&nbsp;&nbsp;';
    echo zen_draw_form('goto', FILENAME_CATEGORIES, '', 'get');
    echo zen_hide_session_id();
    echo HEADING_TITLE_GOTO . ' ' . zen_draw_pull_down_menu('cPath', zen_get_category_tree(), $current_category_id, 'onChange="this.form.submit();"');
    echo '</form>';
  } else {
    echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'set_display_categories_dropdown=0&cID=' . $categories->fields['categories_id'] . '&cPath=' . $cPath . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')) . '">' . zen_image(DIR_WS_ICONS . 'tick.gif', IMAGE_ICON_STATUS_ON) . '</a>&nbsp;&nbsp;';
    echo HEADING_TITLE_GOTO;
  }
?>
                </td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
<?php if ($action == '') { ?>
                <td class="dataTableHeadingContent" align="left"><?php echo Image; ?></td><!--added by inkuyo-->
                <td class="dataTableHeadingContent" width="20" align="left"><?php echo TABLE_HEADING_ID; ?></td>
                <td class="dataTableHeadingContent" align="left"><?php echo TABLE_HEADING_CATEGORIES_PRODUCTS; ?></td>
                <td class="dataTableHeadingContent" align="left">Supplier</td>
				<td class="dataTableHeadingContent" align="left">Manufacturer</td>
				<td class="dataTableHeadingContent" align="left"><?php echo TABLE_HEADING_MODEL; ?></td>
				<td class="dataTableHeadingContent" align="left">BarCode</td>
				<td class="dataTableHeadingContent" align="center">Stock ID</td>
				<td class="dataTableHeadingContent" align="center">Attribute</td>
				<!--<td class="dataTableHeadingContent" align="center"><?php echo Weight; ?></td>-->
                <td class="dataTableHeadingContent" align="center"><?php echo Cost; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_PRICE; ?></td>
				
				<td class="dataTableHeadingContent" align="right">Location</td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_QUANTITY; ?>&nbsp;&nbsp;&nbsp;</td>
				
                <td class="dataTableHeadingContent" width="50" align="center"><?php echo TABLE_HEADING_STATUS; ?></td>
                <!--<td class="dataTableHeadingContent" width="50" align="center">Point</td>-->
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
                <td class="dataTableHeadingContent" width="50" align="center">Warrenty</td>
				<td class="dataTableHeadingContent" width="50" align="center">Maximum Stock</td>
				<td class="dataTableHeadingContent" width="50" align="center">Minimum Stock</td>
				<td class="dataTableHeadingContent" width="50" align="center">Notes</td>
<?php } // action == '' ?>
              </tr>
<?php
    switch ($_SESSION['categories_products_sort_order']) {
      case (0):
        $order_by = " order by c.sort_order, cd.categories_name";
        break;
      case (1):
        $order_by = " order by cd.categories_name";
      case (2);
	    $order_by = " order by cd.categories_name";
      case (3);
      case (4);
      case (5);
      case (6);
      }

    $categories_count = 0;
    $rows = 0;
    if (isset($_GET['search'])) {
      $search = zen_db_prepare_input($_GET['search']);
	  $searchtype = $_GET['searchType'];
		if($searchtype == "Categories")
		{
      		$categories = $db->Execute("select c.categories_id, cd.categories_name, cd.categories_description, c.categories_image,
                                         c.parent_id, c.sort_order, c.date_added, c.last_modified,
                                         c.categories_status
                                  from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                                  where c.categories_id = cd.categories_id
                                  and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                  and cd.categories_name like '%" . zen_db_input($search) . "%'" .
                                  $order_by);
    	}else if($searchtype == "id")
    	{
    		$categories = $db->Execute("select c.categories_id, cd.categories_name, cd.categories_description, c.categories_image,
                                         c.parent_id, c.sort_order, c.date_added, c.last_modified,
                                         c.categories_status
                                  from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                                  where c.categories_id = cd.categories_id
                                  and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                  and cd.categories_id = '" . zen_db_input($search) . "'" .
                                  $order_by);
    	}else if($searchtype == "model")
    	{
    	/*
    		Category have no model
    		
    	    $categories = $db->Execute("select c.categories_id, cd.categories_name, cd.categories_description, c.categories_image,
                                         c.parent_id, c.sort_order, c.date_added, c.last_modified,
                                         c.categories_status
                                  from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                                  where c.categories_id = cd.categories_id
                                  and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                  and cd.categories_id = '" . zen_db_input($search) . "'" .
                                  $order_by);
    	*/
    	
    	}
    	
    	
    	
    } else {
      $categories = $db->Execute("select c.categories_id, cd.categories_name, cd.categories_description, c.categories_image,
                                         c.parent_id, c.sort_order, c.date_added, c.last_modified,
                                         c.categories_status
                                  from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                                  where c.parent_id = '" . (int)$current_category_id . "'
                                  and c.categories_id = cd.categories_id
                                  and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'" .
                                  $order_by);
    }
    
    if(!isset($_GET["sort"]) && !isset($_GET["id"]) && $_GET["searchType"]!="model" && $_GET["searchType"]!="barcode" )
	{
    
    while (!$categories->EOF) {
      $categories_count++;
      $rows++;

// Get parent_id for subcategories if search
      if (isset($_GET['search'])) $cPath = $categories->fields['parent_id'];

      if ((!isset($_GET['cID']) && !isset($_GET['pID']) || (isset($_GET['cID']) && ($_GET['cID'] == $categories->fields['categories_id']))) && !isset($cInfo) && (substr($action, 0, 3) != 'new')) {
        //$category_childs = array('childs_count' => zen_childs_in_category_count($categories->fields['categories_id']));
        //$category_products = array('products_count' => zen_products_in_category_count($categories->fields['categories_id']));

        //$cInfo_array = array_merge($categories->fields, $category_childs, $category_products);
        $cInfo = new objectInfo($categories->fields);
      }

      if (isset($cInfo) && is_object($cInfo) && ($categories->fields['categories_id'] == $cInfo->categories_id) ) {
        echo '              <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\''  . zen_href_link(FILENAME_CATEGORIES, zen_get_path($categories->fields['categories_id'])) . '\'">' . "\n";
      } else {
        echo '              <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . zen_href_link(FILENAME_CATEGORIES, zen_get_path($categories->fields['categories_id'])) . '\'">' . "\n";
      }
?>

<td class="dataTableContent" width="55" align="left">
<?php 
		if ($categories->fields['categories_image'] != '') {
					echo ''. zen_image(DIR_WS_CATALOG . DIR_WS_IMAGES . $categories->fields['categories_image'] , 50, 50) . '</a>' ;}
					
					else {echo '<img src="images/noimage.gif">' ;} 	?></td>
              
<?php 			//eof adding the category image by inkuyo
?>

<?php if ($action == '') { ?>
                <td class="dataTableContent" width="20" align="right"><?php echo $categories->fields['categories_id']; ?></td>
                <td class="dataTableContent"><?php echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, zen_get_path($categories->fields['categories_id'])) . '">' . zen_image(DIR_WS_ICONS . 'folder.gif', ICON_FOLDER) . '</a>&nbsp;<b>' . $categories->fields['categories_name'] . '</b>'; ?></td>
				<td class="dataTableContent" align="center">&nbsp;</td>
				<td class="dataTableContent" align="center">&nbsp;</td>
				<td class="dataTableContent" align="center">&nbsp;</td>
				<td class="dataTableContent" align="center">&nbsp;</td>
				<td class="dataTableContent" align="center">&nbsp;</td>
				
				
				<td class="dataTableContent" align="center">&nbsp;</td>
				<td class="dataTableContent" align="center">&nbsp;</td>
				<td class="dataTableContent" align="center">&nbsp;</td>
			    <td class="dataTableContent" align="right">&nbsp;<?php echo zen_get_products_sale_discount('', $categories->fields['categories_id'], true); ?></td>
                <td class="dataTableContent" align="right" valign="bottom">
                  <?php
                  if (SHOW_COUNTS_ADMIN == 'false') {
                    // don't show counts
                  } else {
                    // show counts
                    $total_products = zen_get_products_to_categories($categories->fields['categories_id'], true);
                    $total_products_on = zen_get_products_to_categories($categories->fields['categories_id'], false);
                    echo $total_products_on . TEXT_PRODUCTS_STATUS_ON_OF . $total_products . TEXT_PRODUCTS_STATUS_ACTIVE;
                  }
                  ?>
                  &nbsp;&nbsp;
                </td>
				<!--<td class="dataTableContent" align="center">&nbsp;</td>-->
				
                <td class="dataTableContent" width="50" align="center">
<?php
      if ($categories->fields['categories_status'] == '1') {
        echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'action=setflag_categories&flag=0&cID=' . $categories->fields['categories_id'] . '&cPath=' . $cPath . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '') . ((isset($_GET['search']) && !empty($_GET['search'])) ? '&search=' . $_GET['search'] : '')) . '">' . zen_image(DIR_WS_IMAGES . 'icon_green_on.gif', IMAGE_ICON_STATUS_ON) . '</a>';
      } else {
        echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'action=setflag_categories&flag=1&cID=' . $categories->fields['categories_id'] . '&cPath=' . $cPath . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '') . ((isset($_GET['search']) && !empty($_GET['search'])) ? '&search=' . $_GET['search'] : '')) . '">' . zen_image(DIR_WS_IMAGES . 'icon_red_on.gif', IMAGE_ICON_STATUS_OFF) . '</a>';
      }
      if (zen_get_products_to_categories($categories->fields['categories_id'], true, 'products_active') == 'true') {
        echo '&nbsp;&nbsp;' . zen_image(DIR_WS_IMAGES . 'icon_yellow_on.gif', IMAGE_ICON_LINKED);
      }
?>
                </td>
                <!--<td class="dataTableContent" align="right"><?php echo $categories->fields['sort_order']; ?></td>-->
				
                <td class="dataTableContent" align="right">
                  <?php echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $cPath . '&cID=' . $categories->fields['categories_id'] . '&action=edit_category' . ((isset($_GET['search']) && !empty($_GET['search'])) ? '&search=' . $_GET['search'] : '')) . '">' . zen_image(DIR_WS_IMAGES . 'icon_edit.gif', ICON_EDIT) . '</a>'; ?>
                  <?php echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $cPath . '&cID=' . $categories->fields['categories_id'] . '&action=delete_category') . '">' . zen_image(DIR_WS_IMAGES . 'icon_delete.gif', ICON_DELETE) . '</a>'; ?>
                  <?php echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $cPath . '&cID=' . $categories->fields['categories_id'] . '&action=move_category') . '">' . zen_image(DIR_WS_IMAGES . 'icon_move.gif', ICON_MOVE) . '</a>'; ?>
<?php
// bof: categories meta tags
        if (zen_get_category_metatags_keywords($categories->fields['categories_id'], (int)$_SESSION['languages_id']) or zen_get_category_metatags_description($categories->fields['categories_id'], (int)$_SESSION['languages_id'])) {
          echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $cPath . '&cID=' . $categories->fields['categories_id'] . '&action=edit_category_meta_tags') . '">' . zen_image(DIR_WS_IMAGES . 'icon_edit_metatags_on.gif', ICON_METATAGS_ON) . '</a>';
        } else {
          echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $cPath . '&cID=' . $categories->fields['categories_id'] . '&action=edit_category_meta_tags') . '">' . zen_image(DIR_WS_IMAGES . 'icon_edit_metatags_off.gif', ICON_METATAGS_OFF) . '</a>';
        }
// eof: categories meta tags
?>
                </td>
                <td class="dataTableContent">&nbsp;</td>
				<td class="dataTableContent">&nbsp;</td>
				<td class="dataTableContent">&nbsp;</td>
<?php } // action == '' ?>
              </tr>
<?php
      $categories->MoveNext();
    }
}

    switch ($_SESSION['categories_products_sort_order']) {
      case (0):
        $order_by = " order by p.products_sort_order, pd.products_name";
        break;
      case (1):
        $order_by = " order by pd.products_name";
        break;
      case (2):
        $order_by = " order by p.products_model";
        break;
      case (3):
        $order_by = " order by p.products_quantity, pd.products_name";
        break;
      case (4):
        $order_by = " order by p.products_quantity DESC, pd.products_name";
        break;
      case (5):
        $order_by = " order by p.products_price_sorter, pd.products_name";
        break;
      case (6):
        $order_by = " order by p.products_price_sorter DESC, pd.products_name";
        break;
	  default:
		$order_by = " order by p.products_model ASC, pd.products_name ASC";
        break;
		
      }

    $products_count = 0;
    if (isset($_GET['search']) && !empty($_GET['search']) && $action != 'edit_category') {
// fix duplicates and force search to use master_categories_id
/*
      $products_query_raw = ("select p.products_type, p.products_id, pd.products_name, p.products_quantity,
                                       p.products_image, p.products_price, p.products_date_added,
                                       p.products_last_modified, p.products_date_available,
                                       p.products_status, p2c.categories_id,
                                       p.products_model,
                                       p.products_quantity_order_min, p.products_quantity_order_units, p.products_priced_by_attribute,
                                       p.product_is_free, p.product_is_call, p.products_quantity_mixed, p.product_is_always_free_shipping,
                                       p.products_quantity_order_max, p.products_sort_order
                                from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, "
                                       . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
                                where p.products_id = pd.products_id
                                and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                and p.products_id = p2c.products_id
                                and (
                                pd.products_name like '%" . zen_db_input($_GET['search']) . "%'
                                or pd.products_description like '%" . zen_db_input($_GET['search']) . "%'
                                or p.products_model like '%" . zen_db_input($_GET['search']) . "%')" .
                                $order_by);
*/
      if($searchtype == "Categories")
		{
      		$products_query_raw = ("select p.products_type, p.products_id, pd.products_name, p.products_quantity,p.products_quantity_b1,p.products_quantity_b2,p.products_point,
                                       p.products_image, p.products_price, p.products_date_added,p.suppliers_id,p.manufacturers_id,products_warrenty,
                                       p.products_last_modified, p.products_date_available,
									   p.barcode,
										p.reorder_stock,
										p.min_stock,
										p.note,
										p.location,
                                       p.products_status, p2c.categories_id,
                                       p.products_model, products_weight, cost_price,
                                       p.products_quantity_order_min, p.products_quantity_order_units, p.products_priced_by_attribute,p.preorder,
                                       p.product_is_free, p.product_is_call, p.products_quantity_mixed, p.product_is_always_free_shipping,
                                       p.products_quantity_order_max, p.products_sort_order,
                                       p.master_categories_id
                                from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, "
                                       . TABLE_PRODUCTS_TO_CATEGORIES . " p2c  
									   								   
                                where p.products_id = pd.products_id
                                and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                and (p.products_id = p2c.products_id
                                )
                                and pd.products_name like '%" . zen_db_input($_GET['search']) . "%'
								  order by p.products_model ASC, pd.products_name ASC"
                                );
								//and p.master_categories_id = p2c.categories_id
								//echo($products_query_raw);
		
    	}else if($searchtype == "id")
    	{
    		$products_query_raw = ("select p.products_type, p.products_id, pd.products_name, p.products_quantity,p.products_quantity_b1,p.products_quantity_b2,p.products_point,
                                       p.products_image, p.products_price, p.products_date_added,p.suppliers_id,p.manufacturers_id,products_warrenty,
                                       p.products_last_modified, p.products_date_available,
									   p.barcode,
										p.reorder_stock,
										p.min_stock,
										p.note,
										p.location,
                                       p.products_status, p2c.categories_id,
                                       p.products_model, products_weight, cost_price,
                                       p.products_quantity_order_min, p.products_quantity_order_units, p.products_priced_by_attribute,p.preorder,
                                       p.product_is_free, p.product_is_call, p.products_quantity_mixed, p.product_is_always_free_shipping,
                                       p.products_quantity_order_max, p.products_sort_order,
                                       p.master_categories_id
                                from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, "
                                       . TABLE_PRODUCTS_TO_CATEGORIES . " p2c 
                                where p.products_id = pd.products_id
                                and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                and (p.products_id = p2c.products_id
                                and p.master_categories_id = p2c.categories_id)
                                and pd.products_id = '" . zen_db_input($_GET['search']) . "'
                                ".$order_by);
    	}else if($searchtype == "model")
    	{
    		$products_query_raw = ("select p.products_type, p.products_id, pd.products_name, p.products_quantity,p.products_quantity_b1,p.products_quantity_b2,p.products_point,
                                       p.products_image, p.products_price, p.products_date_added,p.suppliers_id,p.manufacturers_id,products_warrenty,
                                       p.products_last_modified, p.products_date_available,
									   p.barcode,
										p.reorder_stock,
										p.min_stock,
										p.note,
										p.location,
                                       p.products_status, p2c.categories_id,
                                       p.products_model, products_weight, cost_price,
                                       p.products_quantity_order_min, p.products_quantity_order_units, p.products_priced_by_attribute,p.preorder,
                                       p.product_is_free, p.product_is_call, p.products_quantity_mixed, p.product_is_always_free_shipping,
                                       p.products_quantity_order_max, p.products_sort_order,
                                       p.master_categories_id
                                from " . TABLE_PRODUCTS . " p left outer join zen_products_with_attributes_stock att on att.products_id = p.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd, "
                                       . TABLE_PRODUCTS_TO_CATEGORIES . " p2c 
                                where p.products_id = pd.products_id
                                and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                and (p.products_id = p2c.products_id
                                and p.master_categories_id = p2c.categories_id)
                                and 
                                (
                                p.products_model like '%" . zen_db_input($_GET['search']) . "%'
                                or 
                                att.attribute_model like '%" . zen_db_input($_GET['search']) . "%'
                                )
                                GROUP BY p.products_id
                                ".$order_by);
    	
    	}else if($searchtype == "barcode")
    	{
    	/*
    	$checkstock = $db->EXECUTE("SELECT a.stock_attributes AS id, a.attribute_model,a.attribute_barcode, a.quantity, a.quantity_b1, a.quantity_b2, a.reorder_stock, a.min_stock, a.note
					FROM zen_products_with_attributes_stock AS a
					LEFT JOIN zen_products_attributes AS b ON a.stock_attributes = b.products_attributes_id
					WHERE a.products_id = '".$products->fields['products_id']."' AND b.products_attributes_id");
    	*/
    	
    		$products_query_raw = ("select DISTINCT p.products_id,p.products_type, pd.products_name, p.products_quantity,p.products_quantity_b1,p.products_quantity_b2,p.products_point,
                                       p.products_image, p.products_price, p.products_date_added,p.suppliers_id,p.manufacturers_id,products_warrenty,
                                       p.products_last_modified, p.products_date_available,
									   p.barcode,
										p.reorder_stock,
										p.min_stock,
										p.note,
										p.location,
                                       p.products_status, p2c.categories_id,
                                       p.products_model, products_weight, cost_price,
                                       p.products_quantity_order_min, p.products_quantity_order_units, p.products_priced_by_attribute,p.preorder,
                                       p.product_is_free, p.product_is_call, p.products_quantity_mixed, p.product_is_always_free_shipping,
                                       p.products_quantity_order_max, p.products_sort_order,
                                       p.master_categories_id
                                from " . TABLE_PRODUCTS . " p left outer join zen_products_with_attributes_stock att on att.products_id = p.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd, "
                                       . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
                                       
                                where p.products_id = pd.products_id
                                and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                and (p.products_id = p2c.products_id
                                and p.master_categories_id = p2c.categories_id)
                                and (p.barcode = '" . zen_db_input($_GET['search']) . "'
                                or att.attribute_barcode = '" . zen_db_input($_GET['search']) . "'
                                )
                                ".$order_by);
    	
    	}
    	
    	
      
    }else if($_GET['sort'] == "suppliers_sort")
	{
		$products_query_raw = ("select * from (select p.products_type, p.products_id, pd.products_name, p.products_quantity,p.products_quantity_b1,p.products_quantity_b2,p.products_point,
                                       p.products_image, p.products_price, p.products_date_added,p.suppliers_id,p.manufacturers_id,products_warrenty,
                                       p.products_last_modified,p.barcode, p.products_date_available,
                                       p.products_status, p.products_model, products_weight, cost_price,
										p.reorder_stock,
										p.min_stock,
										p.note,
										p.location,
                                       p.products_quantity_order_min,p.preorder, p.products_quantity_order_units, p.products_priced_by_attribute,
                                       p.product_is_free, p.product_is_call, p.products_quantity_mixed, p.product_is_always_free_shipping,
                                       p.products_quantity_order_max, p.products_sort_order,p2c.categories_id
                                from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
                                where p.products_id = pd.products_id
                                and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                and p.products_id = p2c.products_id
                                and p.suppliers_id=".$_GET['id']."
								) as a group by a.products_id order by a.products_model,a.products_name");
	
	}	else {
      $products_query_raw = ("select p.products_type, p.products_id, pd.products_name, p.products_quantity,p.products_quantity_b1,p.products_quantity_b2,p.products_point,
                                       p.products_image, p.products_price, p.products_date_added,p.suppliers_id,p.manufacturers_id,products_warrenty,
                                       p.products_last_modified,p.barcode, p.products_date_available,
                                       p.products_status, p.products_model, products_weight, cost_price,
                                       p.products_quantity_order_min,p.preorder, p.products_quantity_order_units, p.products_priced_by_attribute,
                                       p.product_is_free, p.product_is_call, p.products_quantity_mixed, p.product_is_always_free_shipping,
                                       p.products_quantity_order_max,
										p.reorder_stock,
										p.min_stock,
										p.note,
										p.location,
									   p.products_sort_order
                                from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c 
                                where p.products_id = pd.products_id
                                and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                                and p.products_id = p2c.products_id
                                and p2c.categories_id = '" . (int)$current_category_id . "'" .
                                $order_by);
								
    }
// Split Page
// reset page when page is unknown
if (($_GET['page'] == '1' or $_GET['page'] == '') and $_GET['pID'] != '') {
  $old_page = $_GET['page'];
  $check_page = $db->Execute($products_query_raw);
  if ($check_page->RecordCount() > MAX_DISPLAY_RESULTS_CATEGORIES) {
    $check_count=1;
    while (!$check_page->EOF) {
      if ($check_page->fields['products_id'] == $_GET['pID']) {
        break;
      }
      $check_count++;
      $check_page->MoveNext();
    }
    $_GET['page'] = round((($check_count/MAX_DISPLAY_RESULTS_CATEGORIES)+(fmod_round($check_count,MAX_DISPLAY_RESULTS_CATEGORIES) !=0 ? .5 : 0)),0);
    $page = $_GET['page'];
    if ($old_page != $_GET['page']) {
//      zen_redirect(zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . '&pID=' . $_GET['pID'] . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')));
    }
  } else {
    $_GET['page'] = 1;
  }
}







    $products_split = new splitPageResults($_GET['page'], MAX_DISPLAY_RESULTS_CATEGORIES, $products_query_raw, $products_query_numrows);
    $products = $db->Execute($products_query_raw);
// Split Page
if(file_exists(DIR_WS_CLASSES . 'products_with_attributes_stock.php'))
{
	require(DIR_WS_CLASSES . 'products_with_attributes_stock.php');
	$stock = new products_with_attributes_stock;
	$attributeenabled = true;
}else{
	$attributeenabled = false;
}


    while (!$products->EOF) {
      $products_count++;
      $rows++;

// Get categories_id for product if search
      if (isset($_GET['search'])) $cPath = $products->fields['categories_id'];

      if ( (!isset($_GET['pID']) && !isset($_GET['cID']) || (isset($_GET['pID']) && ($_GET['pID'] == $products->fields['products_id']))) && !isset($pInfo) && !isset($cInfo) && (substr($action, 0, 3) != 'new')) {
// find out the rating average from customer reviews
        $reviews = $db->Execute("select (avg(reviews_rating) / 5 * 100) as average_rating
                                 from " . TABLE_REVIEWS . "
                                 where products_id = '" . (int)$products->fields['products_id'] . "'");
        $pInfo_array = array_merge($products->fields, $reviews->fields);
        $pInfo = new objectInfo($pInfo_array);
      }

// Split Page

      
      $type_handler = $zc_products->get_admin_handler($products->fields['products_type']);
      if (isset($pInfo) && is_object($pInfo) && ($products->fields['products_id'] == $pInfo->products_id) ) {
        echo '              <tr class="dataTableRow" >' . "\n";
      } else {
        echo '              <tr class="dataTableRow" >' . "\n";
      }
      
// Split Page
?>

<?php            //bof adding product image by inkuyo?>				
				<td class="dateTableContent" width="55" >
<?php 		if ($products->fields['products_image'] != '') {
				 echo ''. zen_image(DIR_WS_CATALOG . DIR_WS_IMAGES . $products->fields['products_image'] , $order->products[$i]['name'], 50, 50) . '</a>&nbsp' ;}
                 else {echo '<img src="images/noimage.gif">' ;} ?></td>
<?php           //eof adding product image inkuyo?>   
          <td class="dataTableContent" width="20" align="right"><?php echo $products->fields['products_id']; ?></td>
				
                <!--
				<td class="dataTableContent"><?php echo '<a href="' . zen_href_link(FILENAME_PRODUCT, 'cPath=' . $cPath . '&pID=' . $products->fields['products_id'] . '&action=new_product_preview&read=only' . '&product_type=' . $products->fields['products_type'] . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')) . '">' . zen_image(DIR_WS_ICONS . 'preview.gif', ICON_PREVIEW) . '</a>&nbsp;' . $products->fields['products_name']; ?></td>
				-->
				<td class="dataTableContent nameedit" <?php echo('id="name-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['products_name']; ?></td>
				
				<?php
				$products_supplier_name = "select suppliers_name from zen_suppliers where suppliers_id=".$products->fields['suppliers_id'];
				$products_supplier_name_result = $db->Execute($products_supplier_name);
				?>
				<td class="dataTableContent supplieredit" <?php echo('id="supplier-'.$products->fields['products_id'].'"'); ?>><?php echo $products_supplier_name_result ->fields['suppliers_name']; ?></td>
                <?php
				$products_manufacturer_name = "select manufacturers_name from zen_manufacturers where manufacturers_id=".$products->fields['manufacturers_id'];
				$products_manufacturer_name_result = $db->Execute($products_manufacturer_name);
				?>
				<td class="dataTableContent manufactureredit" <?php echo('id="manufacturer-'.$products->fields['products_id'].'"'); ?>><?php echo $products_manufacturer_name_result->fields['manufacturers_name']; ?></td>
				<td class="dataTableContent modeledit" <?php echo('id="model-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['products_model']; ?></td>
				
				
				
				<td class="dataTableContent barcode" align="center" <?php echo('id="barcode-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['barcode']; ?></td>
				
				
				<td class="dataTableContent" align="center">-</td>
				<td class="dataTableContent" align="center">-</td>
				<!--<td class="dataTableContent" align="center"><?php echo $products->fields['products_weight']; ?></td>-->
				<td class="dataTableContent costedit" align="center" <?php echo('id="cost-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['cost_price']; ?></td>
                <td class="dataTableContent sellingedit" align="right" <?php echo('id="selling-'.$products->fields['products_id'].'"'); ?>><?php echo str_replace("<br />","",str_replace("Product is Call for Price","",str_replace("http://black-tactical.com/store/includes/templates/template_default/images/call_for_prices.gif","",str_replace('http://migration.black-tactical.com/store/includes/templates/template_default/images/call_for_prices.gif',"",zen_get_products_display_price($products->fields['products_id']))))); ?></td>
				
				<td class="dataTableContent stockAttributesCellLocation" onclick="" style="cursor: pointer" align="center" <?php echo('id="locationid-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['location']; ?></td>
				
				<?php
				/*
				<td class="dataTableContent stockAttributesCellQuantityB1" align="center" <?php echo('id="qtyidB1-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['products_quantity_b1']; ?></td>
				<td class="dataTableContent stockAttributesCellQuantityB2" align="center" <?php echo('id="qtyidB2-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['products_quantity_b2']; ?></td>
				*/
				?>
				<td class="dataTableContent stockAttributesCellQuantity" align="center" <?php echo('id="qtyid-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['products_quantity']; ?></td>
				
				
				
				<td class="dataTableContent" width="50" align="center">
<?php
      if ($products->fields['products_status'] == '1') {
        echo zen_draw_form('setflag_products', FILENAME_CATEGORIES, 'action=setflag&pID=' . $products->fields['products_id'] . '&cPath=' . $cPath . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '') . ((isset($_GET['search']) && !empty($_GET['search'])) ? '&search=' . $_GET['search'] : ''));?>
        <input type="image" src="<?php echo DIR_WS_IMAGES ?>icon_green_on.gif" title="<?php echo IMAGE_ICON_STATUS_ON; ?>" />
        <input type="hidden" name="flag" value="0" />
        </form>
<?php
      } else {
        echo zen_draw_form('setflag_products', FILENAME_CATEGORIES, 'action=setflag&pID=' . $products->fields['products_id'] . '&cPath=' . $cPath . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '') . ((isset($_GET['search']) && !empty($_GET['search'])) ? '&search=' . $_GET['search'] : ''));?>
        <input type="image" src="<?php echo DIR_WS_IMAGES ?>icon_red_on.gif" title="<?php echo IMAGE_ICON_STATUS_OFF; ?>"/>
        <input type="hidden" name="flag" value="1" />
        </form>
<?php
      }
      if (zen_get_product_is_linked($products->fields['products_id']) == 'true') {
        echo '&nbsp;&nbsp;' . zen_image(DIR_WS_IMAGES . 'icon_yellow_on.gif', IMAGE_ICON_LINKED) . '<br>';
      }
?>
                </td>
<?php if ($action == '') { ?>
               <!-- <td class="dataTableContent" align="right"><?php echo $products->fields['products_sort_order']; ?></td>-->
                <td class="dataTableContent" align="right">
        <?php echo '<a href="' . zen_href_link($type_handler, 'cPath=' . $cPath . '&product_type=' . $products->fields['products_type'] . '&pID=' . $products->fields['products_id']  . '&action=new_product' . (isset($_GET['search']) ? '&search=' . $_GET['search'] : '')) . '">' . zen_image(DIR_WS_IMAGES . 'icon_edit.gif', ICON_EDIT) . '</a>'; ?>
        <?php echo '<a href="' . zen_href_link($type_handler, 'cPath=' . $cPath . '&product_type=' . $products->fields['products_type'] . '&pID=' . $products->fields['products_id'] . '&action=delete_product') . '">' . zen_image(DIR_WS_IMAGES . 'icon_delete.gif', ICON_DELETE) . '</a>'; ?>
        <?php echo '<a href="' . zen_href_link($type_handler, 'cPath=' . $cPath . '&product_type=' . $products->fields['products_type'] . '&pID=' . $products->fields['products_id'] . '&action=move_product') . '">' . zen_image(DIR_WS_IMAGES . 'icon_move.gif', ICON_MOVE) . '</a>'; ?>
        <?php echo '<a href="' . zen_href_link($type_handler, 'cPath=' . $cPath . '&product_type=' . $products->fields['products_type'] . '&pID=' . $products->fields['products_id'] .'&action=copy_to' ) . '">' . zen_image(DIR_WS_IMAGES . 'icon_copy_to.gif', ICON_COPY_TO) . '</a>'; ?>
        <?php echo '<a href="' . zen_href_link(FILENAME_IMAGE_HANDLER, 'products_filter=' . $products->fields['products_id'] . '&current_category_id=' . $current_category_id) . '">' . zen_image(DIR_WS_IMAGES . 'icon_image_handler.gif', ICON_IMAGE_HANDLER) . '</a>'; ?>
<?php
// BOF: Attribute commands
//if (!empty($products->fields['products_id']) && zen_has_product_attributes($products->fields['products_id'], 'false')) {
?>
<?php
        if (zen_has_product_attributes($products->fields['products_id'], 'false')) {
          echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $cPath . '&pID=' . $products->fields['products_id'] .'&action=attribute_features' . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')) . '">' . ((!empty($products->fields['products_id']) && zen_has_product_attributes($products->fields['products_id'], 'false')) ? zen_image(DIR_WS_IMAGES . 'icon_attributes_on.gif', ICON_ATTRIBUTES) : zen_image(DIR_WS_IMAGES . 'icon_attributes.gif', ICON_ATTRIBUTES)) . '</a>';
        } else {
          echo '<a href="' . zen_href_link(FILENAME_ATTRIBUTES_CONTROLLER, 'products_filter=' . $products->fields['products_id'] . '&current_category_id=' . $current_category_id) . '">' . zen_image(DIR_WS_IMAGES . 'icon_attributes.gif', ICON_ATTRIBUTES) . '</a>';
        }
?>
<?php
//} // EOF: Attribute commands
?>
<?php
        if ($zc_products->get_allow_add_to_cart($products->fields['products_id']) == "Y") {
          echo '<a href="' . zen_href_link(FILENAME_PRODUCTS_PRICE_MANAGER, 'products_filter=' . $products->fields['products_id'] . '&current_category_id=' . $current_category_id) . '">' . zen_image(DIR_WS_IMAGES . 'icon_products_price_manager.gif', ICON_PRODUCTS_PRICE_MANAGER) . '</a>';
        }
// meta tags
        if (zen_get_metatags_keywords($products->fields['products_id'], (int)$_SESSION['languages_id']) or zen_get_metatags_description($products->fields['products_id'], (int)$_SESSION['languages_id'])) {
          echo ' <a href="' . zen_href_link($type_handler, 'page=' . $_GET['page'] . '&product_type=' . $products->fields['products_type'] . '&cPath=' . $cPath . '&pID=' . $products->fields['products_id']  . '&action=new_product_meta_tags') . '">' . zen_image(DIR_WS_IMAGES . 'icon_edit_metatags_on.gif', ICON_METATAGS_ON) . '</a>';
        } else {
          echo ' <a href="' . zen_href_link($type_handler, 'page=' . $_GET['page'] . '&product_type=' . $products->fields['products_type'] . '&cPath=' . $cPath . '&pID=' . $products->fields['products_id']  . '&action=new_product_meta_tags') . '">' . zen_image(DIR_WS_IMAGES . 'icon_edit_metatags_off.gif', ICON_METATAGS_OFF) . '</a>';
        }
?>
<?php } // action == '' ?>
</td>
				<td class="dataTableContent stockAttributesCellWarrenty" align="center" <?php echo('id="warrenty-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['products_warrenty']; ?></td>
				
				<td class="dataTableContent stockAttributesCellReorder" align="center" <?php echo('id="reorderid-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['reorder_stock']; ?></td>
				<td class="dataTableContent stockAttributesCellMinStock" align="center" <?php echo('id="minid-'.$products->fields['products_id'].'"'); ?> 
				<?php if($products->fields['min_stock'] >= $products->fields['products_quantity'] && $products->fields['reorder_stock']!=0){echo("style='color:red;font-weight:bold;'");}?>>
				<?php echo $products->fields['min_stock'];?></td>
				<td class="dataTableContent stockAttributesCellNote" align="center" <?php echo('id="noteid-'.$products->fields['products_id'].'"'); ?>><?php echo $products->fields['note']; ?></td>
              </tr>
                <!--Silencer Note : here need to check if this product have attribute if yes, fetch attributes and display-->
			<?php
			
				if($attributeenabled)
				{
					//$checkstock = $stock->get_products_attributes($products->fields['products_id']);
					
					$checkstock = $db->EXECUTE("SELECT a.stock_attributes AS id, a.attribute_model,a.attribute_barcode, a.quantity, a.quantity_b1, a.quantity_b2, a.location, a.reorder_stock, a.min_stock, a.note
					FROM zen_products_with_attributes_stock AS a
					LEFT JOIN zen_products_attributes AS b ON a.stock_attributes = b.products_attributes_id
					INNER JOIN zen_products_options_values as c ON b.options_values_id = c.products_options_values_id
					WHERE a.products_id = '".$products->fields['products_id']."' AND b.products_attributes_id order by c.products_options_values_sort_order asc");
					
					
				}
				
				if($checkstock != null)
				{
					
				
					//$keys = array_keys($checkstock);
					$attributelist = "";
	
					//var_dump($keys);
					
					//foreach($keys as $keyvalue)
					//{
						
					//var_dump($checkstock);
						
						//$firstflag = true;
						//foreach ($checkstock as $value)
						while(!$checkstock->EOF)
						{
							echo('<tr class="dataTableContent">');
							echo("<td>&nbsp;</td>");
							echo("<td>".$products->fields['products_id']."</td>");
							echo("<td>".$products->fields['products_name']."</td>");
							echo("<td>".$products_supplier_name_result ->fields['suppliers_name']."</td>");
							echo("<td>".$products_manufacturer_name_result->fields['manufacturers_name']."</td>");
							//echo("<td>".$products->fields['products_model']."</td>");
							echo("<td id=\"stockid-".$checkstock->fields['id']."\" class=\"stockAttributesCellModel\">".$checkstock->fields['attribute_model']."</td>");
							echo("<td align=\"center\" id=\"stockid-".$checkstock->fields['id']."\" class=\"barcode\">".$checkstock->fields['attribute_barcode']."</td>");
							
							echo("<td align=\"center\">".$checkstock->fields['id']."</td>");
							
							//echo("<td align=\"left\">".str_replace("(+$0)","",$value["text"])."</td>");
							//$attributename[] = $stock->get_attributes_name($checkstock->fields['id']);
							//$attname = $attributename[0];
							//$attname = $attname["value"];
							
							//contains a comma
							$attname = "";
							
							if (strpos($checkstock->fields['id'], ',') !== false)
							{
								$attlist = explode( "," , $checkstock->fields['id'] );
								$firstatt = true;
								foreach($attlist as $attid)
								{
									$query = 'select pval.products_options_values_name as pname
			 			from '.TABLE_PRODUCTS_ATTRIBUTES.' as patrib, '.TABLE_PRODUCTS_OPTIONS.' as popt, '.TABLE_PRODUCTS_OPTIONS_VALUES.' as pval
			 			where patrib.products_attributes_id = "'.$attid.'"
							AND patrib.options_id = popt.products_options_id
			 				AND popt.language_id = "1"
							and popt.language_id = pval.language_id
							and patrib.options_values_id = pval.products_options_values_id';
							
							$attributes = $db->Execute($query);
								
								if($firstatt)
								{
									$firstatt = false;
								}else{
									$attname .= " : ";
								}
								
								$attname .= $attributes->fields["pname"];
								
								}
							}
							else
							{
							$query = 'select pval.products_options_values_name as pname
			 			from '.TABLE_PRODUCTS_ATTRIBUTES.' as patrib, '.TABLE_PRODUCTS_OPTIONS.' as popt, '.TABLE_PRODUCTS_OPTIONS_VALUES.' as pval
			 			where patrib.products_attributes_id = "'.$checkstock->fields['id'].'"
							AND patrib.options_id = popt.products_options_id
			 				AND popt.language_id = "1"
							and popt.language_id = pval.language_id
							and patrib.options_values_id = pval.products_options_values_id';
							
							$attributes = $db->Execute($query);
							$attname = $attributes->fields["pname"];
							}
							
							//$attname = $db->EXECUTE("Select stock_attributes as id,quantity from zen_products_with_attributes_stock where products_id =".$products->fields['products_id']);
							
							echo("<td align=\"left\">".$attname."</td>");
							//echo("<td align=\"center\">".$products->fields['products_weight']."</td>");
							
							echo("<td align=\"center\">".$products->fields['cost_price']."</td>");
							
							$attaddcost = $db->Execute("Select options_values_price from ".TABLE_PRODUCTS_ATTRIBUTES."  where products_attributes_id ='".$checkstock->fields['id']."'");
							
							echo("<td align=\"right\">S$".number_format(($attaddcost->fields['options_values_price']+$products->fields['products_price']), 2, '.','')."</td>");
							
							//$attquantity = $db->Execute("Select quantity from ".TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK."  where products_id=".$products->fields['products_id']." and stock_attributes='".$checkstock->fields['id']."'");
							
							
							//echo("<td align=\"center\" id=\"stockidB1-".$checkstock->fields['id']."\" class=\"stockAttributesCellQuantityB1\">".$checkstock->fields['quantity_b1']."</td>");
							
							//echo("<td align=\"center\" id=\"stockidB2-".$checkstock->fields['id']."\" class=\"stockAttributesCellQuantityB2\">".$checkstock->fields['quantity_b2']."</td>");
							
							echo("<td align=\"center\" id=\"stockid-".$checkstock->fields['id']."\" class=\"stockAttributesCellLocation\">".$checkstock->fields['location']."</td>");
							
							echo("<td align=\"center\" id=\"stockid-".$checkstock->fields['id']."\" class=\"stockAttributesCellQuantity\">".$checkstock->fields['quantity']."</td>");
							
							echo("<td>-</td>");
							echo("<td>-</td>");
							//echo("<td>-</td>");
							
							echo("<td align=\"center\">-</td>");
							echo("<td align=\"center\" id=\"stockid-".$checkstock->fields['id']."\" class=\"stockAttributesCellReorder\">".$checkstock->fields['reorder_stock']."</td>");
							
							if($checkstock->fields['min_stock'] >= $checkstock->fields['quantity'] && $checkstock->fields['reorder_stock']!=0)
							{
								$extrastyle = "style='color:red;font-weight:bold;'";
							}else{
								$extrastyle = "";
							}
							
							echo("<td align=\"center\" id=\"stockid-".$checkstock->fields['id']."\" ".$extrastyle." class=\"stockAttributesCellMinStock\">".$checkstock->fields['min_stock']."</td>");
							echo("<td align=\"center\" id=\"stockid-".$checkstock->fields['id']."\" class=\"stockAttributesCellNote\">".$checkstock->fields['note']."</td>");
							
							$checkstock->MoveNext();
						}
					//}
					echo("<tr><td colspan=\"20\"><hr/></td></tr>");
				}else{
				echo("<tr><td colspan=\"20\"><hr/></td></tr>");
				}
			?>
<?php
      $products->MoveNext();
    }

    $cPath_back = '';
    if (sizeof($cPath_array) > 0) {
      for ($i=0, $n=sizeof($cPath_array)-1; $i<$n; $i++) {
        if (empty($cPath_back)) {
          $cPath_back .= $cPath_array[$i];
        } else {
          $cPath_back .= '_' . $cPath_array[$i];
        }
      }
    }

    $cPath_back = (zen_not_null($cPath_back)) ? 'cPath=' . $cPath_back . '&' : '';
?>
<?php 
if ($action == '') { ?>
<?php 
 if (!isset($_GET['search']) && $_GET["sort"]!="suppliers_sort") echo (!$zc_skip_categories ? '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $cPath . '&action=new_category') . '">' . zen_image_button('button_new_category.gif', IMAGE_NEW_CATEGORY) . '</a>&nbsp;' : ''); ?>
<?
if ($zc_skip_products == false) { ?>
<form name="newproduct" action="<?php echo zen_href_link(FILENAME_CATEGORIES, '', 'NONSSL'); ?>" method = "get"><?php    echo zen_image_submit('button_new_product.gif', IMAGE_NEW_PRODUCT); ?>
<?php
  $sql = "select ptc.product_type_id, pt.type_name from " . TABLE_PRODUCT_TYPES_TO_CATEGORY . " ptc, " . TABLE_PRODUCT_TYPES . " pt
          where ptc.category_id = '" . $current_category_id . "'
          and pt.type_id = ptc.product_type_id";
  $restrict_types = $db->Execute($sql);
  if ($restrict_types->RecordCount() >0 ) {
    $product_restrict_types_array = array();
    while (!$restrict_types->EOF) {
      $product_restrict_types_array[] = array('id' => $restrict_types->fields['product_type_id'],
                                         'text' => $restrict_types->fields['type_name']);
      $restrict_types->MoveNext();
    }
   } else {
    $product_restrict_types_array = $product_types_array;
  }
?>
<?php echo '&nbsp;&nbsp;' . zen_draw_pull_down_menu('product_type', $product_restrict_types_array);
echo zen_hide_session_id(); ?>
           <input type="hidden" name="cPath" value="<?php echo $cPath; ?>">
           <input type="hidden" name="action" value="new_product">
          </form>
<?
}
}
/*
if ($action == '') { ?>
              <tr>
                <td colspan="3"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="smallText"><?php echo TEXT_CATEGORIES . '&nbsp;' . $categories_count . '<br />' . TEXT_PRODUCTS . '&nbsp;' . $products_count; ?></td>
                    <td align="right" class="smallText"><?php if (sizeof($cPath_array) > 0) echo '<a href="' . zen_href_link(FILENAME_CATEGORIES, $cPath_back . 'cID=' . $current_category_id) . '">' . zen_image_button('button_back.gif', IMAGE_BACK) . '</a>&nbsp;'; if (!isset($_GET['search']) && $_GET["sort"]!="suppliers_sort") echo (!$zc_skip_categories ? '<a href="' . zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $cPath . '&action=new_category') . '">' . zen_image_button('button_new_category.gif', IMAGE_NEW_CATEGORY) . '</a>&nbsp;' : ''); ?>
<?php if ($zc_skip_products == false) { ?>
<form name="newproduct" action="<?php echo zen_href_link(FILENAME_CATEGORIES, '', 'NONSSL'); ?>" method = "get"><?php    echo zen_image_submit('button_new_product.gif', IMAGE_NEW_PRODUCT); ?>
<?php
  $sql = "select ptc.product_type_id, pt.type_name from " . TABLE_PRODUCT_TYPES_TO_CATEGORY . " ptc, " . TABLE_PRODUCT_TYPES . " pt
          where ptc.category_id = '" . $current_category_id . "'
          and pt.type_id = ptc.product_type_id";
  $restrict_types = $db->Execute($sql);
  if ($restrict_types->RecordCount() >0 ) {
    $product_restrict_types_array = array();
    while (!$restrict_types->EOF) {
      $product_restrict_types_array[] = array('id' => $restrict_types->fields['product_type_id'],
                                         'text' => $restrict_types->fields['type_name']);
      $restrict_types->MoveNext();
    }
   } else {
    $product_restrict_types_array = $product_types_array;
  }
?>
<?php echo '&nbsp;&nbsp;' . zen_draw_pull_down_menu('product_type', $product_restrict_types_array);
echo zen_hide_session_id(); ?>
           <input type="hidden" name="cPath" value="<?php echo $cPath; ?>">
           <input type="hidden" name="action" value="new_product">
          </form>
		  
<?php
  } else {
    echo CATEGORY_HAS_SUBCATEGORIES;
?>
<?php } // hide has cats?>
          &nbsp;</td>
                  </tr>
                </table></td>
              </tr>
<?php 
} // turn off when editing ?>
*/
?>
            </table>
			
			</td>

