<?php
require_once(dirname(__FILE__) . "/wb_functions.php");

$wb_filters_district = array();
$wb_filters_locations = array();

// Get Main Categories
$wb_filters_mains = array();
$q = "select * from wb_filters_mains where wb_uid='$wb_uid' order by pos asc;";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	$fm_selected = (isset($fm_uid) && $fm_uid != "") ? $fm_uid : '';
	$fm_cnt = 1;
	while ($row = mysql_fetch_assoc($r)) {
		
		if($fm_cnt == 1 && $fm_selected == "") {
			//$fm_selected = $row['fm_uid'];
			//$fm_uid = $row['fm_uid'];
		}
		
		$wb_filters_mains [$row['fm_uid']] = array(
			"title"		=> trim(stripslashes($row['title'])),
			"results"	=> 0,
			"selected"	=> ($row['fm_uid'] == $fm_selected) ? 1 : 0
		);
		
		$fm_cnt ++;
	}
}

//echo '<pre>', var_export($_POST, true), '</pre>';
//echo '<pre>$fm_uid = ', var_export($fm_uid, true), '</pre>';

if(count($wb_filters_mains) > 0 && $fm_uid != "") {
	$wb_filters_names = array();
	$wb_filters_types = array();

	$wb_filters_radio = array();
	$wb_filters_checkbox = array();
	
	
	$q = "select * from wb_filters_elems where wb_uid='$wb_uid' and fm_uid='$fm_uid' and searchable='1' order by fc_uid asc, pos asc;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_assoc($r)) {
			$f_fe_uid = $row['fe_uid'];
			$f_e_name = $row['e_name'];
			$f_e_db = trim($row['e_db']);
			$f_e_type = $row['e_type'];
			$f_e_type_search = $row['e_type_search'];
			$f_show = true;
			
			if($f_e_type == "radio") {
				$wb_filters_types [$f_fe_uid] = "radio";
				$wb_filters_radio [$f_fe_uid] = array();
				$opts = array();
				if($f_e_db != "") {
					$opts_tmp = unserialize(base64_decode($f_e_db));
					foreach ($opts_tmp as $opt_code=>$opt_value) {
						$wb_filters_radio [$f_fe_uid] [] = $opt_code;
						$opts[$opt_code] = array(
							"f_value"	=> stripslashes($opt_value),
							"results"	=> 0,
							"selected"	=> 0
						);
						
						
					}
				}
				else {
					$f_show = false;
				}
				
				$wb_filters_names [$f_fe_uid] = array(
					"all"		=> 0,
					"e_name"	=> $f_e_name,
					"e_type"	=> $f_e_type,
					"e_type_search"	=> $f_e_type_search,
					"e_db" 		=> $opts,
					"show"		=> $f_show		
				);
				
			}
			
			if($f_e_type == "yesno") {
				$wb_filters_types [$f_fe_uid] = "radio";
				$wb_filters_radio [$f_fe_uid] = array();
				$opts = array();
				if($f_e_db != "") {
					$opts_tmp = unserialize(base64_decode($f_e_db));
					foreach ($opts_tmp as $opt_code=>$opt_value) {
						$wb_filters_radio [$f_fe_uid] [] = $opt_code;
						$opts[$opt_code] = array(
							"f_value"	=> stripslashes($opt_value),
							"results"	=> 0,
							"selected"	=> 0
						);
						
						
					}
				}
				else {
					$f_show = false;
				}
				
				$wb_filters_names [$f_fe_uid] = array(
					"all"		=> 0,
					"e_name"	=> $f_e_name,
					"e_type"	=> $f_e_type,
					"e_type_search"	=> $f_e_type_search,
					"e_db" 		=> $opts,
					"show"		=> $f_show		
				);
				
			}
			
			if($f_e_type == "select") {
				$wb_filters_types [$f_fe_uid] = "radio";
				$wb_filters_radio [$f_fe_uid] = array();
				$opts = array();
				if($f_e_db != "") {
					$opts_tmp = unserialize(base64_decode($f_e_db));
					foreach ($opts_tmp as $opt_code=>$opt_value) {
						$wb_filters_radio [$f_fe_uid] [] = $opt_code;
						$opts[$opt_code] = array(
							"f_value"	=> stripslashes($opt_value),
							"results"	=> 0,
							"selected"	=> 0
						);
						
						
					}
				}
				else {
					$f_show = false;
				}
				
				$wb_filters_names [$f_fe_uid] = array(
					"all"		=> 0,
					"e_name"	=> $f_e_name,
					"e_type"	=> $f_e_type,
					"e_type_search"	=> $f_e_type_search,
					"e_db" 		=> $opts,
					"show"		=> $f_show		
				);
			}
			
			if($f_e_type == "checkbox") {
				$wb_filters_types [$f_fe_uid] = "checkbox";
				$wb_filters_checkbox [$f_fe_uid] = array();
				$opts = array();
				if($f_e_db != "") {
					$opts_tmp = unserialize(base64_decode($f_e_db));
					foreach ($opts_tmp as $opt_code=>$opt_value) {
						$wb_filters_checkbox [$f_fe_uid] [] = $opt_code;
						$opts[$opt_code] = array(
							"f_value"	=> stripslashes($opt_value),
							"results"	=> 0,
							"selected"	=> 0
						);
						
						
					}
				}
				else {
					$f_show = false;
				}
				
				$wb_filters_names [$f_fe_uid] = array(
					"all"		=> 0,
					"e_name"	=> $f_e_name,
					"e_type"	=> $f_e_type,
					"e_type_search"	=> $f_e_type_search,
					"e_db" 		=> $opts,
					"show"		=> $f_show		
				);		

			}
			
			
		}
	}
}

// Get POST Data
$wb_search_params['filter'] = array();
$wb_search_filters = array();
if(isset($_POST['wb_search'])) {
	
	if(isset($_POST['dcode']) && $_POST['dcode'] != "") $wb_srch_dcode = $_POST['dcode'];
	if(isset($_POST['lcode']) && $_POST['lcode'] != "") $wb_srch_lcode = $_POST['lcode'];
	
	if(isset($_POST['clear_search'])) {
		// nothing
		if(isset($_POST['dcode']) && $_POST['dcode'] == "") $wb_srch_lcode = "";
	}
	else {
		foreach ($_POST as $wb_fld=>$wb_selection) {
			if(isset($wb_filters_names) && count($wb_filters_names) > 0) {
				foreach ($wb_filters_names as $filter_uid=>$filter_data) {
					if($wb_fld == $filter_data['e_name']) {
						$wb_search_params['filter'][$filter_data['e_name']] = $wb_selection;
						$wb_search_filters[$filter_uid] = $wb_selection;
						
						foreach ($wb_filters_names[$filter_uid]['e_db'] as $opt_code=>$op_data) {
							if(is_array($wb_selection) && in_array($opt_code, $wb_selection)) $wb_filters_names[$filter_uid]['e_db'][$opt_code]['selected'] = 1;
						}
					}
				}
			}
		}
	}
	//echo '<pre>', var_export($_POST, true), '</pre>';
	//echo '<pre>', var_export($wb_search_filters, true), '</pre>';
}


//echo '<pre>', var_export($wb_filters_names, true), '</pre>';

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

$query = array(
	"fields" => array(),

	"from" => array(
		"advertisment a",
		"advertisment_locations l", 
		"wb_categories_adverts c",
		"wb_filters_adverts_categs m"
		),

	"where" => array(
			"and"	=> array(
				"a.adv_code=l.adv_code",
				"l.active='1'",
				"l.main_location='1'",
				"a.adv_code=c.wb_advert_uid",
				"c.wb_uid='$wb_uid'",
				"a.adv_code=m.adv_code",
				"m.wb_uid='$wb_uid'",
				"c.wb_status='1'"
			),
			"or"	=> array(),
			"append"	=> array()
		),
	
	"group"	=> array(),
	
	"limit"	=> ""
);

if($wb_srch_dcode != "") {
	$query['where']['and'][] = "l.dcode='$wb_srch_dcode'";
}
if($wb_srch_lcode != "") {
	$query['where']['and'][] = "l.lcode='$wb_srch_lcode'";
}

$adverts_pool = array();

// Get Location Totals
$all_adverts = 0;
$q_all = $query;

if(isset($_POST['nameContains']) && trim($_POST['nameContains']) != "") {
	$nameContains = mysql_real_escape_string(trim($_POST['nameContains']));
	
	$q_all['where']['and'][] = "a.short_title_en like '%$nameContains%'";
}

$q_all ['fields'] = array(
	"a.adv_code"
);

$q_all ['group'] = array(
	"a.adv_code"
);



$no_adverts_found = false;
$rq_all = wb_build_select_query($q_all);
$r_all = @mysql_query($rq_all);

if($r_all && mysql_num_rows($r_all) > 0) {
	$all_adverts = mysql_num_rows($r_all);
	while($row = mysql_fetch_assoc($r_all)) {
		$adverts_pool [] = $row['adv_code'];
	}
	
	if(isset($nameContains) && $nameContains != "") {
		$query['where']['and'][] = "a.short_title_en like '%$nameContains%'";
	}
	
}
else {
	$no_adverts_found = true;
	if(isset($nameContains) && $nameContains != "") {
		$rq_all = str_replace($nameContains, '', $rq_all);
		$r_all = @mysql_query($rq_all);
		//echo $rq_all;
		$all_adverts = mysql_num_rows($r_all);
		while($row = mysql_fetch_assoc($r_all)) {
			$adverts_pool [] = $row['adv_code'];
		}
	}
}
//echo $rq_all;
//echo '<pre>$adverts_pool = ' , var_export($adverts_pool, true), '</pre>';

// Get All numbers by category

if($all_adverts > 0) {
	if(count($wb_filters_mains) > 0) {
		foreach ($wb_filters_mains as $_fm_uid=>$_fm_data) {
			$q_mains = $query;
			$q_mains ['fields'][] = "m.fm_uid";
			$q_mains ['fields'][] = "count(m.adv_code) as fm_count";
			
			$q_mains ['where'] ['and'] [] = "a.adv_code in ('" . implode("','", $adverts_pool) . "')"; 
			
			
			$q_mains ['group'] [] = "m.fm_uid";
			
			$rq_mains = wb_build_select_query($q_mains);
			$r_mains = @mysql_query($rq_mains);
			if($r_mains && mysql_num_rows($r_mains) > 0) {
				while ($row_mains = mysql_fetch_assoc($r_mains)) {
					$wb_filters_mains[$row_mains['fm_uid']]['results'] = intval($row_mains['fm_count']);
				}
			}
		}
	}
}



// Add the default main category to the query
if($fm_uid != "") {
	$query ['where']['and'][] = "m.fm_uid='$fm_uid'";
}

if(isset($_REQUEST['debugwb'])) {
	if($fm_uid != "") {
		echo '<br/>$fm_uid=[', $fm_uid, ']';
	}
	else {
		echo '<br/>$fm_uid=[', $fm_uid, ']';
	}
}

// Get Adverts pool by selected category
$adverts_pool = array();
$q_pool = $query;
$q_pool ['fields'][] = "a.adv_code";

if(isset($_REQUEST['debugwb'])) {
	echo '<pre>$q_pool = ' , var_export($q_pool, true), '</pre>';
}

//echo '<pre>$q_pool = ' , var_export($q_pool, true), '</pre>';

$rq_pool = wb_build_select_query($q_pool);
$r_pool = @mysql_query($rq_pool) or die("<strong>$rq_pool</strong>".mysql_error());
if($r_pool && mysql_num_rows($r_pool) > 0) {
	while($row = mysql_fetch_assoc($r_pool)) {
		$adverts_pool [] = $row['adv_code'];
	}
}

// Get all checkbox counts based on location, category and with features adverts
$non_multi_pool = $adverts_pool;
$non_multi_adverts_pool = $adverts_pool;
if(isset($wb_filters_checkbox) && count($wb_filters_checkbox) > 0) {
	
	//echo '<pre>$adverts_pool = ' , var_export($adverts_pool, true), '</pre>';
	
	$tmp_adverts_pool = array();
	$multi_adverts_pool = array();
	
	$modify_pool = false;
	$strict_features = array();
	$multi_features = array();
	
	if(count($wb_filters_radio) > 0) {
		//echo "<br/><strong style=\"color:red\">AM RADIO FILTRE</strong>";
		
		foreach ($wb_filters_radio as $feuid=>$fedata) {
			//echo '<pre>', $feuid, ' = ', var_export($fedata, true), '</pre>';
			//echo '<pre>', $feuid, ' = ', var_export($wb_filters_names[$feuid], true), '</pre>';
			
			foreach ($fedata as $i => $_fe_f_value) {
				if($wb_filters_names[$feuid]['e_db'][$_fe_f_value]['selected'] == 1) {
					//$modify_pool = true;
					$multi_features [] = array(
						"fe_uid"	=> $feuid,
						"f_value"	=> $_fe_f_value
					);
				}
			}
		}
	}
	else {
		//echo "<br/><strong style=\"color:red\">NU AM RADIO FILTRE</strong>";
	}
	
	if(count($multi_features) > 0 && $fm_uid != '') {
		//echo "<br/><strong style=\"color:red\">AM MULTI FEATURE</strong>";
		$q_multi = array();
		foreach ($multi_features as $i=>$mf) {
			$q_multi [] = "( f.fe_uid='{$mf['fe_uid']}' and f.f_value='{$mf['f_value']}') ";
			
		}
		
		$q_r = $query;
			
		$q_r ['fields'][] = "a.adv_code";
		
		$q_r ['from'] [] = "wb_filters_adverts f";
		$q_r ['where'] ['and'] [] = "a.adv_code in ('" . implode("','", $adverts_pool) . "')"; 
	
		$q_r ['where'] ['and'] [] = "a.adv_code=f.adv_code"; 
		$q_r ['where'] ['and'] [] = "f.fm_uid='$fm_uid'"; 
		$q_r ['where'] ['and'] [] = "(" . implode(" or ", $q_multi) . ")"; 
		$q_r ['group'] [] = "a.adv_code";
		
		$rq_r = wb_build_select_query($q_r);
		//echo '<br/><br/><strong style="color:red;">'.$rq_r.'</strong>';
		$r_r = mysql_query($rq_r) or die("<strong>$rq_r</strong>".mysql_error());
		if($r_r && mysql_num_rows($r_r) > 0) {
			while ($row = mysql_fetch_assoc($r_r)) {
				$multi_adverts_pool [] = $row['adv_code'];
			}
		}
	}
	else {
		//echo "<br/><strong style=\"color:red\">NU AM MULTI FEATURE</strong>";
	}
	
	if(count($multi_adverts_pool) == 0) {
		$multi_adverts_pool = $adverts_pool;
	}
	else {
		$adverts_pool = $multi_adverts_pool;
	}
	
	//echo '<br/><pre>$adverts_pool 1 = ' , var_export($adverts_pool, true), '</pre>';
	
	foreach ($wb_filters_checkbox as $feuid=>$fedata) {
		
		foreach ($fedata as $_fe_f_value) {
			if($wb_filters_names[$feuid]['e_db'][$_fe_f_value]['selected'] == 1) {
				$modify_pool = true;
				$strict_features [] = $_fe_f_value;
			}
		}
	}
	
	if(count($strict_features) > 0 && $fm_uid != "") {
		//echo "<br/><strong style=\"color:red\">AM STRICT FEATURE</strong>";
		$q_features = "'" . implode("', '", $strict_features) . "'";
		
		$q_r = $query;
			
		$q_r ['fields'][] = "a.adv_code";
		
		$q_r ['from'] [] = "wb_filters_adverts f";
		$q_r ['where'] ['and'] [] = "a.adv_code in ('" . implode("','", $multi_adverts_pool) . "')"; 
	
		$q_r ['where'] ['and'] [] = "a.adv_code=f.adv_code"; 
		$q_r ['where'] ['and'] [] = "f.fm_uid='$fm_uid'"; 
		$q_r ['where'] ['and'] [] = "f.f_value IN ($q_features)"; 
		$q_r ['group'] [] = "a.adv_code";
		
		$rq_r = wb_build_select_query($q_r);
		
		$rq_r .= " HAVING count(*)=" . count($strict_features);
		
		//echo '<br/><strong style="color:red;">'.$rq_r.'</strong>';
		$r_r = mysql_query($rq_r) or die("<strong>$rq_r</strong>".mysql_error());
		if($r_r && mysql_num_rows($r_r) > 0) {
			while ($row = mysql_fetch_assoc($r_r)) {
				$tmp_adverts_pool [] = $row['adv_code'];
			}
		}
		
	}
	
	if($modify_pool) {
		//echo "<br/> Modify pool";
		$adverts_pool = array_unique($tmp_adverts_pool);
	}
	
	
	// Refine Non MULTI Pool
	if(count($strict_features) > 0 && $fm_uid != "") {
		//echo "<br/><strong style=\"color:red\">AM STRICT FEATURE</strong>";
		$q_features = "'" . implode("', '", $strict_features) . "'";
		
		$q_r = $query;
			
		$q_r ['fields'][] = "a.adv_code";
		
		$q_r ['from'] [] = "wb_filters_adverts f";
		$q_r ['where'] ['and'] [] = "a.adv_code in ('" . implode("','", $non_multi_pool) . "')"; 
	
		$q_r ['where'] ['and'] [] = "a.adv_code=f.adv_code"; 
		$q_r ['where'] ['and'] [] = "f.fm_uid='$fm_uid'"; 
		$q_r ['where'] ['and'] [] = "f.f_value IN ($q_features)"; 
		$q_r ['group'] [] = "a.adv_code";
		
		$rq_r = wb_build_select_query($q_r);
		
		$rq_r .= " HAVING count(*)=" . count($strict_features);
		
		$non_multi_pool = array();
		//echo '<br/><strong style="color:red;">'.$rq_r.'</strong>';
		$r_r = mysql_query($rq_r) or die("<strong>$rq_r</strong>".mysql_error());
		if($r_r && mysql_num_rows($r_r) > 0) {
			
			
			
			while ($row = mysql_fetch_assoc($r_r)) {
				$non_multi_pool [] = $row['adv_code'];
			}
		}
		
	}
	
	
	//echo '<br/><pre>$adverts_pool 2 = ' , var_export($adverts_pool, true), '</pre>';

    if($fm_uid != "") {
        foreach ($wb_filters_checkbox as $feuid => $fedata) {

            $fe_adverts = array();

            foreach ($fedata as $_fe_f_value) {
                $q_r = $query;

                $q_r ['fields'][] = "a.adv_code";
                $q_r ['fields'][] = "f.f_value";
                //$q_r ['fields'][] = "count(f.f_value) as f_count";

                $q_r ['from'] [] = "wb_filters_adverts f";
                $q_r ['where'] ['and'] [] = "a.adv_code in ('" . implode("','", $adverts_pool) . "')";

                $q_r ['where'] ['and'] [] = "f.fm_uid='$fm_uid'";
                $q_r ['where'] ['and'] [] = "f.adv_code=a.adv_code";
                $q_r ['where'] ['and'] [] = "f.fe_uid='$feuid'";
                $q_r ['where'] ['and'] [] = "f.f_value='$_fe_f_value'";
                $q_r ['group'] [] = "a.adv_code";
                $q_r ['group'] [] = "f.f_value";

                $rq_r = wb_build_select_query($q_r);

                //if($feuid == "WFFE6AC79ECFF0AB")
                //echo "<br/><strong>&raquo;</strong> $rq_r";

                $r_r = mysql_query($rq_r) or die("<strong>$rq_r</strong>" . mysql_error());
                if ($r_r && mysql_num_rows($r_r) > 0) {
                    $wb_filters_names[$feuid]['e_db'][$_fe_f_value]['results'] = intval(mysql_num_rows($r_r));
                    while ($row = mysql_fetch_assoc($r_r)) {
                        $fe_adverts [] = $row['adv_code'];
                    }
                    //echo '<br/>$wb_filters_names[\''.$feuid.'\'][\'e_db\'][\''.$_fe_f_value.'\'][\'results\'] = '. intval(mysql_num_rows($r_r));
                }
            }

            //echo '<pre>$fe_adverts["'.$feuid.'"] = ' . var_export($fe_adverts, true) . '</pre>';

            $wb_filters_names[$feuid]['all'] = count(array_unique($fe_adverts));

        }
    }
}



//echo '<pre>', var_export($adverts_pool, true), '</pre>';

// Get all radio counts based on location, category and with features adverts
/*
if(isset($wb_filters_radio) && count($wb_filters_radio) > 0) {
	$tmp_adverts_pool = array();
	foreach ($wb_filters_radio as $feuid=>$fedata) {
		
		foreach ($fedata as $_fe_f_value) {
			$q_r = $query;
			
			$q_r ['fields'][] = "a.adv_code";
			$q_r ['fields'][] = "f.fe_uid";
			$q_r ['fields'][] = "f.f_value";
			//$q_r ['fields'][] = "count(f.f_value) as f_count";
			
			$q_r ['from'] [] = "wb_filters_adverts f";
			$q_r ['where'] ['and'] [] = "a.adv_code in ('" . implode("','", $non_multi_pool) . "')"; 
		
			$q_r ['where'] ['and'] [] = "f.adv_code=a.adv_code"; 
			$q_r ['where'] ['and'] [] = "f.fm_uid='$fm_uid'"; 
			$q_r ['where'] ['and'] [] = "f.fe_uid='$feuid'"; 
			$q_r ['where'] ['and'] [] = "f.f_value='$_fe_f_value'"; 
			$q_r ['group'] [] = "a.adv_code";
			$q_r ['group'] [] = "f.f_value";
			
			$rq_r = wb_build_select_query($q_r);
			
			echo "<br/><strong>&raquo;</strong> $rq_r";
			
			$r_r = mysql_query($rq_r) or die("<strong>$rq_r</strong>".mysql_error());
			if($r_r && mysql_num_rows($r_r) > 0) {
				$wb_filters_names[$feuid]['e_db'][$_fe_f_value]['results'] = intval(mysql_num_rows($r_r));
				if($wb_filters_names[$feuid]['e_db'][$_fe_f_value]['selected'] == 1) {
					//while ($row = mysql_fetch_assoc($r_r)) {
					//	$tmp_adverts_pool [] = $row['adv_code'];
					//}
				}
			}
		}
	}
}
*/

if(isset($wb_filters_radio) && count($wb_filters_radio) > 0 && $fm_uid != "") {
	$tmp_adverts_pool = array();
	
	$feuid_arr = array();
	$_fe_f_value_arr = array();
	
	foreach ($wb_filters_radio as $feuid=>$fedata) {
		$feuid_arr [] = "'" . $feuid . "'";
		$_fe_f_value_arr [] = "'" . implode("', '", $fedata) . "'";
	}
	
	if(count($_fe_f_value_arr) > 0) {
		$feuid_str = implode(", ", $feuid_arr);
		$_fe_f_value_str = implode(", ", $_fe_f_value_arr);
		
	
		$q_r = $query;
			
		//$q_r ['fields'][] = "a.adv_code";
		$q_r ['fields'][] = "f.fe_uid";
		$q_r ['fields'][] = "f.f_value";
		$q_r ['fields'][] = "count(f.f_value) as f_count";
		
		$q_r ['from'] [] = "wb_filters_adverts f";
		$q_r ['where'] ['and'] [] = "a.adv_code in ('" . implode("','", $non_multi_pool) . "')"; 
	
		$q_r ['where'] ['and'] [] = "f.adv_code=a.adv_code"; 
		$q_r ['where'] ['and'] [] = "f.fm_uid='$fm_uid'"; 
		$q_r ['where'] ['and'] [] = "f.fe_uid IN ($feuid_str)"; 
		$q_r ['where'] ['and'] [] = "f.f_value IN ($_fe_f_value_str)"; 
		//$q_r ['group'] [] = "a.adv_code";
		$q_r ['group'] [] = "f.fe_uid";
		$q_r ['group'] [] = "f.f_value";
		
		$rq_r = wb_build_select_query($q_r);
		
		//echo "<br/><strong>&raquo;</strong> $rq_r";
		
		$r_r = mysql_query($rq_r) or die("<strong>$rq_r</strong>".mysql_error());
		if($r_r && mysql_num_rows($r_r) > 0) {
			while ($row_r = mysql_fetch_assoc($r_r)) {
				$feuid = $row_r['fe_uid'];
				$_fe_f_value = $row_r['f_value'];
				$wb_filters_names[$feuid]['e_db'][$_fe_f_value]['results'] = $row_r['f_count'];
			}
			
		}
		
		
	}
}


//echo '<pre>', var_export($wb_filters_names, true), '</pre>';
//die();

//echo wb_build_select_query($q_wb);

// Get Expedia Hotels and Availability
$hotel_rates = array();
$aff_hotels = array();
$expedia_hotels = array();
$expedia_adverts = array();
$booking_hotels = array();

/*
if(isset($_SESSION['hotel_search']) && isset($_SESSION['hotel_search']['checkin'])) {
	
	$st = microtime(true);
	$log = "Started at $st";
	
	$q_affil = "select a.* from hotel_affiliates_adverts a, hotel_affiliates r where a.aff_code=r.aff_code and r.aff_status='1' and adv_code in ('" . implode("', '", $adverts_pool) . "');";
	
	$r_affil = @mysql_query($q_affil);
	if($r_affil && mysql_num_rows($r_affil) > 0) {
		while ($row_affil = mysql_fetch_assoc($r_affil)) {
			$aff_adv_code 	= $row_affil['adv_code'];
			$aff_code 		= $row_affil['aff_code'];
			$aff_hotel_id 	= $row_affil['aff_hotel_id'];
	
			if(strpos($aff_code, 'EXPEDIA') !== false) {
				$expedia_hotels[$aff_hotel_id] = $aff_adv_code;
				
				if(!isset($expedia_adverts[$aff_adv_code])) {
					$expedia_adverts[$aff_adv_code] = array();
					$expedia_adverts[$aff_adv_code][] = $aff_hotel_id;
				}
				else {
					$expedia_adverts[$aff_adv_code][] = $aff_hotel_id;
				}
				
			}
			
			
			if(!isset($aff_hotels[$aff_code])) {
				$aff_hotels[$aff_code] = array();
				$aff_hotels[$aff_code][] = $aff_hotel_id;
			}
			else {
				$aff_hotels[$aff_code][] = $aff_hotel_id;
			}
		}
	}
	
	$st1 = microtime(true);
	$log .= "<br/>Got all list at $st1 in " . ($st1 - $st);
	
	//echo '<pre>', var_export(array_keys($expedia_hotels), true), '</pre>';
	//die();
	
	if(count($expedia_hotels) > 0) {
		
		
		$expedia_hotel_ids = array_keys($expedia_hotels);
		
		$expedia_opts = array();
		
		if(isset($_SESSION['hotel_search']) && isset($_SESSION['hotel_search']['checkin'])) {
			$opts['arrivalDate'] = $_SESSION['hotel_search']['checkin'];
			$opts['departureDate'] = $_SESSION['hotel_search']['checkout'];
			$opts['numberOfBedRooms'] = 1;
			$opts['RoomGroup'] = array(
				array(
					"numberOfAdults"	=> 2,
					
					"numberOfChildren"	=> 0
				)
			);
		}
		
		require_once(dirname(__FILE__) . "/../client_stand/aff/Expedia_rest_api.php");
		$exp_api = new Expedia_rest_api();
		$exp_api->set_protocol('http');
		$exp_ret = $exp_api->get_hotels_availability($expedia_hotel_ids, $opts);
		$ret_h = json_decode($exp_ret, true);
	
		
		//echo '<pre>', var_export($ret, true), '</pre>';
		
		//die();
		
		if(is_array($ret_h) && count($ret_h) > 0) {
			//echo '<pre>', var_export($ret_h, true), '</pre>';
			if(isset($ret_h['HotelListResponse'])) {
				$ret_hotels = $ret_h['HotelListResponse']['HotelList']['HotelSummary'];
				
				
				if(isset($ret_hotels['RoomRateDetailsList'])) {
					$hot_id = $ret_hotels['hotelId'];
					$room_rates = $ret_hotels['RoomRateDetailsList']['RoomRateDetails']['RateInfos']['RateInfo']['ChargeableRateInfo'];
					$hotel_rates[$expedia_hotels[$hot_id]][] = round($room_rates['@averageRate'], 2);
					//echo '<pre>', var_export($room_rates, true), '</pre>';
				}
				else {
					foreach ($ret_hotels as $h_index=>$rh) {
						//echo '<pre>', var_export($rh, true), '</pre>';
						if(isset($rh['RoomRateDetailsList'])) {
							$hot_id = $rh['hotelId'];
							$room_rates = $rh['RoomRateDetailsList']['RoomRateDetails']['RateInfos']['RateInfo']['ChargeableRateInfo'];
							$hotel_rates[$expedia_hotels[$hot_id]][] = round($room_rates['@averageRate'], 2);
							//echo '<pre>hotelId [', $rh['hotelId'],'][',$expedia_hotels[$rh['hotelId']], '] = ', var_export($room_rates['@averageBaseRate'], true), '</pre>';
						}
					}
				}
			}
			
			$ret_h = array();
			
		}
		
		$st2 = microtime(true);
		$log .= "<br/>Got all rates at $st2 in " . ($st2 - $st1);
		
		//echo '<pre>', var_export($hotel_rates, true), '</pre>';
		
		
	}
	
	
	$st3 = microtime(true);
	$log .= "<br/>Finished expedia at $st3 in " . ($st3 - $st);
	//echo $log;
	
	
	
	
}
*/
// Generate the final query
$q_wb = "select * from advertisment where adv_code in ('" . implode("', '", $adverts_pool) . "');";
?>