<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/

  if ((int)$wyswietlen == 0) {
     $wyswietlen = MAX_DISPLAY_SEARCH_RESULTS;
  }
  if (!tep_not_null($sposob_wyswietlania)) {
     $sposob_wyswietlania = PRODUCTS_LISTING_DISPLAY_MODE;
  }  
  //
  $szerokosc = ALLPROD_IMAGE_WIDTH+10;
  $wysokosc = ALLPROD_IMAGE_HEIGHT+10;
  $listing_split = new splitPageResults($listing_sql, $wyswietlen, 'p.products_id');
  
  if ( ($listing_split->number_of_rows > 0) && ( (PREV_NEXT_BAR_LOCATION == '1') || (PREV_NEXT_BAR_LOCATION == '3') ) ) {
  ?>

  <script type="text/javascript" language="javascript" src="includes/javascript/porownanie.js"></script>

  <table border="0" width="100%" cellspacing="0" cellpadding="2">
   <tr>
    <td class="smallText"><?php echo $listing_split->display_count(TEXT_DISPLAY_NUMBER_OF_PRODUCTS); ?></td>
    <td class="smallText" align="right"><?php echo TEXT_RESULT_PAGE . ' ' . $listing_split->display_links(MAX_DISPLAY_PAGE_LINKS, tep_get_all_get_params(array('page', 'info', 'x', 'y'))); ?></td>
   </tr>
  </table>

  <?php 
  if (PRODUCTS_LISTING_DISPLAY_MODE == 'false') { $prw = false; } else { $prw = true; }
  if (isset($_GET['dis'])) {
     if ($_GET['dis'] == '0') { $prw = false; } else { $prw = true; }
  }
  ?> 
  <?php if (POROWNYWARKA_PROD == true && $prw == true) { ?>
      <table border="0" cellspacing="5" cellpadding="2">
       <tr>
        <td class="smallText"><?php echo tep_image(DIR_WS_TEMPLATES.'images/misc/porownaj.gif'); ?></td>
        <td class="smallText"><a style="font-size:13px;font-weight:bold" href="javascript:porownanie_produktow('<?php echo TEXT_POROWNAJ_BLAD_2; ?>')"><?php echo TEXT_BUTTON_POROWNAJ; ?></a></td>
       </tr>
      </table>
  <?php } ?>
  <?php
  }

// ****************************************************************************

 if ($listing_split->number_of_rows > 0) {

  if ($sposob_wyswietlania=='true') {

   // Wyswietlanie wszystkich produktow w wierszach

   $list_box_contents = array();

   if (DISPLAY_PRICE_BRUTTO_NETTO == 'true') {
 	   $display_nb = 1;
 	  } else {
	   $display_nb = 0;
   }

   for ($col=0, $n=sizeof($column_list); $col<$n; $col++) {
    switch ($column_list[$col]) {
      case 'PRODUCT_LIST_MODEL':
        $lc_text = TABLE_HEADING_MODEL;
        $lc_align = 'center';
        break;
      case 'PRODUCT_LIST_NAME':
        $lc_text = TABLE_HEADING_PRODUCTS;
        $lc_align = 'center';
        break;
    // Products Description Hack begins
      case 'PRODUCT_LIST_DESCRIPTION':
//        $lc_text = TABLE_LIST_DESCRIPTION;
        $lc_text = '';
        break;
    // Products Description Hack ends
      case 'PRODUCT_LIST_MANUFACTURER':
        $lc_text = TABLE_HEADING_MANUFACTURER;
        $lc_align = 'center';
        break;
      // EZier New Fields added
        case 'PRODUCT_LIST_RETAIL_PRICE':
            $lc_text = TABLE_HEADING_RETAIL_PRICE;
            $lc_align = 'center';
            break;
      // End EZier New Fields added
      case 'PRODUCT_LIST_PRICE':
        $lc_text = TABLE_HEADING_PRICE;
        $lc_align = 'center';
        break;
      // EZier New Fields added
        case 'PRODUCT_LIST_SAVE':
            $lc_text = TABLE_HEADING_SAVE;
            $lc_align = 'right';
            break;
      // End EZier New Fields added
      case 'PRODUCT_LIST_QUANTITY':
        $lc_text = TABLE_HEADING_QUANTITY;
        $lc_align = 'right';
        break;
    //MAXIMUM quantity code
      case 'PRODUCT_LIST_MAXORDER':
        $lc_text = TABLE_HEADING_MAXORDER;
        $lc_align = 'center';
        break;
    //End MAXIMUM quantity code
      case 'PRODUCT_LIST_WEIGHT':
        $lc_text = TABLE_HEADING_WEIGHT;
        $lc_align = 'right';
        break;
      case 'PRODUCT_LIST_IMAGE':
        $lc_text = TABLE_HEADING_IMAGE;
        $lc_align = 'center';
        break;
      case 'PRODUCT_LIST_PDF':
        $lc_text = 'PDF';
        $lc_align = 'center';
        break;				
      case 'PRODUCT_LIST_BUY_NOW':
        $lc_text =  ((USE_SHOP_AS_CATALOG == 'true') ? '' : TABLE_HEADING_BUY_NOW);
        $lc_align = 'center';
        break;
      case 'PRODUCT_LIST_PRODUCTS_AVAILABILITY':
        $lc_text = TABLE_HEADING_PRODUCTS_AVAILABILITY;
        $lc_align = 'center';
        break;
    }


    // Products Description Hack begins
    if ( ($column_list[$col] != 'PRODUCT_LIST_BUY_NOW' &&
          $column_list[$col] != 'PRODUCT_LIST_IMAGE' &&
          $column_list[$col] != 'PRODUCT_LIST_DESCRIPTION') && ($column_list[$col] != 'PRODUCT_LIST_SAVE') ) {

	  // TUTAJ ZEBY BYLO SORTOWANIE
      //$lc_text = tep_create_sort_heading($HTTP_GET_VARS['sort'], $col+1, $lc_text);

	}

     if ($column_list[$col] != 'PRODUCT_LIST_DESCRIPTION') {
      $list_box_contents[0][] = array('align' => $lc_align,
                                             'params' => 'class="productListing-heading" height="30"',
                                             'text'  => "&nbsp;" . $lc_text . "&nbsp;");
     }
    }
    // Products Description Hack ends

    if ($listing_split->number_of_rows > 0) {
     $rows = 0;
     $listing_query = tep_db_query($listing_split->sql_query);
     $ilosc = $listing_split->number_of_rows * 2;
     while ($listing = tep_db_fetch_array($listing_query)) {

      if (($rows/2) == floor($rows/2)) {
       $list_box_contents[] = array('params' => 'class="productListing-even"');
      } else {
       $list_box_contents[] = array('params' => 'class="productListing-odd"');
      }

      $cur_row = sizeof($list_box_contents) - 1;

      for ($col=0, $n=sizeof($column_list); $col<$n; $col++) {
        $lc_align = '';
        // Products Description Hack begins
        $lc_params = '';
        // Products Description Hack ends

        switch ($column_list[$col]) {
          case 'PRODUCT_LIST_MODEL':
            $lc_align = '';
            $lc_text = '&nbsp;' . $listing['products_model'] . '&nbsp;';
            break;
          case 'PRODUCT_LIST_NAME':
            $lc_align = '';
            if (isset($HTTP_GET_VARS['manufacturers_id'])) {
              $lc_text = '<a class="ProductTile" href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . osc_trunc_string($listing['products_name'],50, 1) . '</a>';
            } else {
              $lc_text = '&nbsp;<a class="ProductTile" href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . osc_trunc_string($listing['products_name'], 50, 1) . '</a>&nbsp;';
            }
            break;
          // Products Description Hack begins
          case 'PRODUCT_LIST_DESCRIPTION':
            $lc_text = '' . osc_trunc_string(strip_tags($listing['products_description'], ''), PRODUCT_LIST_DESCRIPTION_LENGTH) . '&nbsp;';
            $col_to_span = sizeof($column_list)-1;
            if (PRODUCT_LIST_IMAGE > 0) {
              $col_to_span -= 1;
            }
            $lc_params = 'colspan="' . $col_to_span . '" valign="top"';
            break;
          // Products Description Hack ends

          case 'PRODUCT_LIST_MANUFACTURER':
            $lc_align = 'center';
            $lc_text = '&nbsp;<a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $listing['manufacturers_id']) . '">' . $listing['manufacturers_name'] . '</a>&nbsp;';
            break;
          // EZier New Fields added
          case 'PRODUCT_LIST_RETAIL_PRICE':
                  $lc_align = 'right';
                  if ((tep_not_null($listing['products_retail_price'])) && ($listing['products_retail_price']) > 0) {
                     $lc_text = '' . $currencies->display_price_nodiscount($listing['products_id'],$listing['products_retail_price'], tep_get_tax_rate($listing['products_tax_class_id']),1,$display_nb);
                  } else {
                     $lc_text = '&nbsp;';
                  }
                  break;
          // End EZier New Fields added
          case 'PRODUCT_LIST_PRICE':
            $lc_align = 'right';
            //TotalB2B start
            $query_special_prices_hide_result = SPECIAL_PRICES_HIDE;
            $special_hide = $query_special_prices_hide_result;

            if ($new_price = tep_get_products_special_price($listing['products_id']) ) {
              $listing_org['products_price'] = tep_xppp_getproductprice($listing['products_id']);
              $listing['products_price'] = $new_price;

              if ($listing['products_price'] > 0) {
                if ($special_hide == 'false') {
                  $query_price_to_guest_result = ALLOW_GUEST_TO_SEE_PRICES;
                  if ((($query_price_to_guest_result=='true') && !(tep_session_is_registered('customer_id'))) || ((tep_session_is_registered('customer_id')))) {
                    $lc_text = '<span class="smallText">'. TABLE_HEADING_PRICE . ':</span>&nbsp;<span class="smallText" style="white-space: nowrap;"><s>' . $currencies->display_price($listing['products_id'], $listing_org['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . '</s></span><br><span class="productSpecialPrice">' . $currencies->display_price_nodiscount($listing['products_id'], $listing['products_price'],tep_get_tax_rate($listing['products_tax_class_id']),1,$display_nb) .'</span>';
                  } else {
                    $lc_text = '<span class="smallText">'. TABLE_HEADING_PRICE . ':</span><br><span class="Cena">' . PRICES_LOGGED_IN_TEXT . '</span>';
                  }
                } else {
                  $lc_text = '<span class="smallText">'. TABLE_HEADING_PRICE . ':</span><br><span class="Cena">' . $currencies->display_price($listing['products_id'], $listing_org['products_price'], tep_get_tax_rate($listing['products_tax_class_id']),1,$display_nb) .'</span>';
                }
              } else {
                $lc_text = '<span class="smallText"><b>'.TEMPORARY_NO_PRICE.'</b></span>';
              }
            } else {
              $listing['products_price'] = tep_xppp_getproductprice($listing['products_id']);
              if ($listing['products_price'] > 0) {
                $lc_text = '<span class="smallText">'. TABLE_HEADING_PRICE . ':<br></span><span class="Cena">' . $currencies->display_price($listing['products_id'],$listing['products_price'],tep_get_tax_rate($listing['products_tax_class_id']),1,$display_nb).'</span>';
              } else {
                $lc_text = '<span class="smallText"><b>'.TEMPORARY_NO_PRICE.'</b></span>';
              }
            }
            //TotalB2B end & TotalB2B end
            break;
                // EZier New Fields added
          case 'PRODUCT_LIST_SAVE':
                 $lc_align = 'right';
                 if (tep_not_null($listing['specials_new_products_price'])) {
                    if ((tep_not_null($listing['products_retail_price'])) && ($listing['products_retail_price']) > 0) {
    //               $lc_save = round(100 - (( $listing['specials_new_products_price'] / $listing['products_retail_price'] ) * 100 ));     //Oszczedasz - wartosc procentowa
                 $lc_save = osc_ez($currencies->display_price_nodiscount($listing['products_id'], ($listing['products_retail_price'] - $listing['specials_new_products_price']), tep_get_tax_rate($listing['products_tax_class_id']),1,$display_nb));     //Oszczedasz - wartosc kwotowa
                     $lc_text = '<font color="red">' . $lc_save . '</font>';
                  } else {
                     $lc_text = '&nbsp;';
                  }
                 } else {
                    if ((tep_not_null($listing['products_retail_price'])) && ($listing['products_retail_price']) > 0) {
    //               $lc_save = round(100 - (( $listing['products_price'] / $listing['products_retail_price'] ) * 100 ));     //Oszczedasz - wartosc procentowa
                 $lc_save = osc_ez($currencies->display_price_nodiscount($listing['products_id'], ($listing['products_retail_price'] - $listing['products_price']), tep_get_tax_rate($listing['products_tax_class_id']),1,$display_nb));     //Oszczedasz - wartosc kwotowa
                 $lc_text = '<font color="red">' . $lc_save . '</font>';
                  } else {
                     $lc_text = '&nbsp;';
                  }
                 }
                 break;
               // End EZier New Fields added
          case 'PRODUCT_LIST_QUANTITY':
            $lc_align = 'right';
            $lc_text = '&nbsp;' . $listing['products_quantity'] . '&nbsp;';
            break;
      //MAXIMUM quantity code
       case 'PRODUCT_LIST_MAXORDER':
            $lc_align = 'right';
            $lc_text = '&nbsp;' . $listing['products_maxorder'] . '&nbsp;';
            break;
      //End MAXIMUM quantity code
      case 'PRODUCT_LIST_WEIGHT':
            $lc_align = 'right';
            $lc_text = '&nbsp;' . $listing['products_weight'] . '&nbsp;';
            break;
      case 'PRODUCT_LIST_IMAGE':
            $lc_align = 'center';
			$lc_text = '&nbsp;';
            //
            if (POROWNYWARKA_PROD == true) {
                $porownaj = '<div style="padding:4px" class="smallText">'.TEXT_POROWNAJ.' <input type="checkbox" onClick="ile_zaz(this,\''.TEXT_POROWNAJ_BLAD.'\')" name="porownaj[]" value="'.$listing['products_id'].'"></div>';
               } else {
                $porownaj = '';
            }
			$lc_text .= $porownaj;
            //
            if (isset($HTTP_GET_VARS['manufacturers_id'])) {
              if (tep_not_null($listing['products_image'])){
			          $lc_text .= tep_image(DIR_WS_IMAGES . $listing['products_image'], $listing['products_name'], ALLPROD_IMAGE_WIDTH, ALLPROD_IMAGE_HEIGHT,'',$wysokosc,$szerokosc,$listing['products_id'],tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']),true);
			        }
            } else {
              if (tep_not_null($listing['products_image'])){
                $lc_text .= tep_image(DIR_WS_IMAGES . $listing['products_image'], $listing['products_name'], ALLPROD_IMAGE_WIDTH, ALLPROD_IMAGE_HEIGHT,'',$wysokosc,$szerokosc,$listing['products_id'],tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']),true);
			  }
            }
            // Products Description Hack begins
            if (PRODUCT_LIST_DESCRIPTION > 0) {
             $lc_params = 'rowspan="2" ';
            }
            // Products Description Hack ends
            break;
      case 'PRODUCT_LIST_PDF':
            $lc_align = 'center';
		    if (isset($listing['products_image_pop'])) {
		       $lc_text = '<a href="/images/'.$listing['products_image_pop'].'" target="_blank">'.tep_image(DIR_WS_TEMPLATES.'images/misc/icons/pdf.png').'</a>';
		     } else {
		       $lc_text ='&nbsp;';
	        }				
            break;				
      case 'PRODUCT_LIST_PRODUCTS_AVAILABILITY':
            $products_availability = $listing['products_availability_id'];
            $products_availability_info_query = tep_db_query("select e.products_availability_name from " . TABLE_PRODUCTS_AVAILABILITY . " e where e.products_availability_id = '" . (int)$products_availability . "' and e.language_id = '" . (int)$languages_id . "'");
            $products_availability_info = tep_db_fetch_array($products_availability_info_query);
            $lc_text = '' . $products_availability_info['products_availability_name'] . '&nbsp;';
            break;
      case 'PRODUCT_LIST_BUY_NOW':
            $lc_align = 'center';
            if ( $listing['products_quantity'] > 0 ) {
							$lc_text = (($listing['products_price'] > 0) ? '<form name="buy_now_' . $listing['products_id'] . '" method="post" action="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now', 'NONSSL'). '"><input type="hidden" name="products_id" value="' . $listing['products_id'] . '">' . ((USE_SHOP_AS_CATALOG == 'true') ? '' : '<input style="margin:3px;" type="text" name="quantity" value="1" maxlength="6" size="6"><br>' . tep_image_submit('shopping_cart.gif', IMAGE_BUTTON_IN_CART)) . '</form>' :  ((USE_SHOP_AS_CATALOG == 'true') ? '' : '<b>'.tep_image_button('shopping_cart_sold.gif', TEMPORARY_NO_PRICE).'</b>'));
            } else {
		          if ( (STOCK_CHECK == 'true') && (STOCK_ALLOW_CHECKOUT != 'true') ) {
                $lc_text =  ((USE_SHOP_AS_CATALOG == 'true') ? '' : ' '.tep_image_button('shopping_cart_sold.gif', PRODUCT_SOLD).'') ;
		         } else {
							$lc_text = (($listing['products_price'] > 0) ? '<form name="cart_quantity_' . $listing['products_id'] . '" method="post" action="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . '&products_id='.$listing['products_id'].'&action=buy_now', 'NONSSL'). '"><input type="hidden" name="products_id" value="' . $listing['products_id'] . '">' . ((USE_SHOP_AS_CATALOG == 'true') ? '' : '<input type="text" name="quantity" value="1" maxlength="6" size="6"><br>' . tep_image_submit('shopping_cart.gif', IMAGE_BUTTON_IN_CART)) . '</form>' :  ((USE_SHOP_AS_CATALOG == 'true') ? '' : '<b>'.tep_image_button('shopping_cart_sold.gif', TEMPORARY_NO_PRICE).'</b>'));
		         }
            }
						break;

			}

      // Products Description Hack begins
      $lc_params .= 'class="productListing-data"';
      if ($column_list[$col] == 'PRODUCT_LIST_DESCRIPTION') {
       $list_box_contents[$cur_row][] = array('align' => $lc_align,
                                                 'params' => $lc_params,
                                                 'text' => $lc_text,
                                                 'desc_flag' => 'true');
      } else {
       $list_box_contents[$cur_row][] = array('align' => $lc_align,
                                                 'params' => $lc_params,
                                                 'text'  => $lc_text);
      }
      // Products Description Hack ends
      }

      $rows++;
     }

    new productListingBox($list_box_contents);
   }
  } else {

        // Wyswietlanie wszystkich produktow w kolumnach i wierszach
        $query_special_prices_hide_result = SPECIAL_PRICES_HIDE;
        $special_hide = $query_special_prices_hide_result;

        $modules_template = 'product_listing_col';
        $ilosc_col = MAX_DISPLAY_COLUMN_PRODUCTS_LISTING;
        $max_display = $wyswietlen;
        $max_wiersz =  $max_display/$ilosc_col;

        $szer_tab = round(100/$ilosc_col,0) ;

        $listing_query = tep_db_query($listing_split->sql_query);

        ?>

        <!-- new_products //-->
        <table border="0" width="100%" cellspacing="0" cellpadding="0" class="templateinfoBox" align="center">
		   <tr>
		    <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '7'); ?></td>
		   </tr>
            <tr>
                <td class="main" align="center">
                    <table border="0" cellspacing="0" cellpadding="1" align="center" width="100%">
						<tr>
                        <?php
                        include (bts_select('modules', $modules_template));
                        ?>
					   <?php
					   $ile = tep_db_num_rows($listing_query)-($row*$ilosc_col);
					   if ($ile < $ilosc_col && $ile != 0) {
					      $roznica = $ilosc_col - $ile;
						  for($index=0;$index<$roznica;$index++)
						  {
						    if ($index == $roznica-1) {
						    echo '<td width="'.$szer_tab.'%">'.tep_draw_separator('pixel_trans.gif', '100%', '1').'</td>';
							} else {
							echo '<td width="'.$szer_tab.'%">'.tep_draw_separator('pixel_trans.gif', '100%', '1').'</td>';
							echo '<td>'.tep_draw_separator('pixel_trans.gif', '1', '1').'</td>';
							}
						  }
					   }
					   ?>
						</tr>
                    </table>
                </td>
            </tr>
        </table>
        <?php
    }
  } else {

    $list_box_contents = array();

    $list_box_contents[0] = array('params' => 'class="productListing-odd"');
    $list_box_contents[0][] = array('params' => 'class="productListing-data"',
                                   'text' => TEXT_NO_PRODUCTS);

    new productListingBox($list_box_contents);
  }

  if ( ($listing_split->number_of_rows > 0) && ((PREV_NEXT_BAR_LOCATION == '2') || (PREV_NEXT_BAR_LOCATION == '3')) ) {
  ?>
  <table border="0" width="100%" cellspacing="0" cellpadding="2">
   <tr>
    <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '7'); ?></td>
   </tr>
   <?php   if ($sposob_wyswietlania=='true') { ?>
   <tr>
    <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '100%', '1'); ?></td>
   </tr>
   <?php } ?>
   <tr>
    <td class="smallText"><?php echo $listing_split->display_count(TEXT_DISPLAY_NUMBER_OF_PRODUCTS); ?></td>
    <td class="smallText" align="right"><?php echo TEXT_RESULT_PAGE . ' ' . $listing_split->display_links(MAX_DISPLAY_PAGE_LINKS, tep_get_all_get_params(array('page', 'info', 'x', 'y'))); ?></td>
   </tr>
  </table>
  <?php
  }
  ?>