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

  require('includes/application_top.php');

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_ADVANCED_SEARCH);

  $error = false;
  $ciag_bledu = '';

  // WYSZUKIWANIE DLA CECH - SPRAWDZA CZY KTORAS Z CECH JEST WYBRANA
  $zaznaczona_cecha = false;
  $attributes_query = tep_db_query("select * from " . TABLE_PRODUCTS_OPTIONS . " where language_id = '" . (int)$languages_id . "'");
  $option_value_selected = false;
  $products_stock_attributes_array = array();
  while ($attributes = tep_db_fetch_array($attributes_query)) {
  	if (isset($HTTP_GET_VARS[$attributes["products_options_id"]]) && !empty($HTTP_GET_VARS[$attributes["products_options_id"]]))
      	$zaznaczona_cecha = true;
  }
  // KONIEC

 //  if ((isset($HTTP_GET_VARS['keywords']) && empty($HTTP_GET_VARS['keywords'])) &&
//        (isset($HTTP_GET_VARS['dfrom']) && (empty($HTTP_GET_VARS['dfrom']) || ($HTTP_GET_VARS['dfrom'] == DOB_FORMAT_STRING))) &&
//        (isset($HTTP_GET_VARS['dto']) && (empty($HTTP_GET_VARS['dto']) || ($HTTP_GET_VARS['dto'] == DOB_FORMAT_STRING))) &&
//        (isset($HTTP_GET_VARS['pfrom']) && !is_numeric($HTTP_GET_VARS['pfrom'])) &&
//        (isset($HTTP_GET_VARS['pto']) && !is_numeric($HTTP_GET_VARS['pto'])) &&
//        (isset($HTTP_GET_VARS['manufacturers_id']) && (empty($HTTP_GET_VARS['manufacturers_id']))) && $zaznaczona_cecha == false ) {

//        $error = true;
//        $ciag_bledu .= '* '.ERROR_AT_LEAST_ONE_INPUT.'<br>';
//  }  else {
        $dfrom = '';
        $dto = '';
        $pfrom = '';
        $pto = '';
        $keywords = '';

        if (isset($HTTP_GET_VARS['dfrom'])) {
          $dfrom = (($HTTP_GET_VARS['dfrom'] == DOB_FORMAT_STRING) ? '' : $HTTP_GET_VARS['dfrom']);
        }

        if (isset($HTTP_GET_VARS['dto'])) {
          $dto = (($HTTP_GET_VARS['dto'] == DOB_FORMAT_STRING) ? '' : $HTTP_GET_VARS['dto']);
        }

        if (isset($HTTP_GET_VARS['pfrom'])) {
          $pfrom = $HTTP_GET_VARS['pfrom'];
        }

        if (isset($HTTP_GET_VARS['pto'])) {
          $pto = $HTTP_GET_VARS['pto'];
        }

        if (isset($HTTP_GET_VARS['keywords'])) {
          $keywords = $HTTP_GET_VARS['keywords'];
        }

        // BOF: Search price range
        if (isset($HTTP_GET_VARS['find_price'])) {
           $pfrom = ($price_range_values[$HTTP_GET_VARS['find_price']]['from'] >= 0.01)?$price_range_values[$HTTP_GET_VARS['find_price']]['from']:0 ;
           $pto = $price_range_values[$HTTP_GET_VARS['find_price']]['to'];
           $sub_title_search =  sprintf(HEADING_TITLE_3, $price_range_array[$HTTP_GET_VARS['find_price']]['text'] );
        }
        // EOF: Search price range

        $date_check_error = false;
        if (tep_not_null($dfrom)) {
          if (!tep_checkdate($dfrom, DOB_FORMAT_STRING, $dfrom_array)) {
            $error = true;
            $date_check_error = true;
            $ciag_bledu .= '* '.ERROR_INVALID_FROM_DATE.'<br>';
          }
        }

        if (tep_not_null($dto)) {
          if (!tep_checkdate($dto, DOB_FORMAT_STRING, $dto_array)) {
            $error = true;
            $date_check_error = true;
            $ciag_bledu .= '* '.ERROR_INVALID_TO_DATE.'<br>';
          }
        }

        if (($date_check_error == false) && tep_not_null($dfrom) && tep_not_null($dto)) {
          if (mktime(0, 0, 0, $dfrom_array[1], $dfrom_array[2], $dfrom_array[0]) > mktime(0, 0, 0, $dto_array[1], $dto_array[2], $dto_array[0])) {
            $error = true;
            $ciag_bledu .= '* '.ERROR_TO_DATE_LESS_THAN_FROM_DATE.'<br>';
          }
        }

        $price_check_error = false;
        if (tep_not_null($pfrom)) {
          if (!settype($pfrom, 'double')) {
            $error = true;
            $price_check_error = true;
            $ciag_bledu .= '* '.ERROR_PRICE_FROM_MUST_BE_NUM.'<br>';
          }
        }

        if (tep_not_null($pto)) {
          if (!settype($pto, 'double')) {
            $error = true;
            $price_check_error = true;
            $ciag_bledu .= '* '.ERROR_PRICE_TO_MUST_BE_NUM.'<br>';
          }
        }

        if (($price_check_error == false) && is_float($pfrom) && is_float($pto)) {
          if ($pfrom >= $pto) {
            $error = true;
            $ciag_bledu .= '* '.ERROR_PRICE_TO_LESS_THAN_PRICE_FROM.'<br>';
          }
        }

        if (tep_not_null($keywords)) {
          if (!tep_parse_search_string($keywords, $search_keywords)) {
            $error = true;
            $ciag_bledu .= '* '.ERROR_INVALID_KEYWORDS.'<br>';
          }
        }
//  }

        if (empty($dfrom) && empty($dto) && empty($pfrom) && empty($pto) && empty($keywords)) {
          $error = true;
          $ciag_bledu .= '* '.ERROR_AT_LEAST_ONE_INPUT;
        }
  if (tep_not_null($ciag_bledu)) {
    $messageStack->add_session('search',$ciag_bledu );
  }
  if ($error == true) {
     tep_redirect(tep_href_link(FILENAME_ADVANCED_SEARCH, tep_get_all_get_params(), 'NONSSL', true, false));
  }

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_ADVANCED_SEARCH));
  $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT, tep_get_all_get_params(), 'NONSSL', true, false));

  $content = CONTENT_ADVANCED_SEARCH_RESULT;

  include (bts_select('main', $content_template)); // BTSv1.5

  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
