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

/**
 * **
 * CHECK PRICES
 */

if(isset($_POST['current_affiliate']) && trim($_POST['current_affiliate']) != "" && $_POST['act'] == "check-prices") {
	$ret = array();
	
	//echo '<pre>', var_export($_POST, true), '</pre>';
	
	$aff = array();
	$adults = intval($_POST['adults']);
	$current_affiliate = trim($_POST['current_affiliate']);
	
	if(trim($_POST['checkin']) != "" && trim($_POST['checkout'])) {
		/*
		$checkin_ar = explode("/", trim($_POST['checkin']));
		$checkin = $checkin_ar[2] . '-' . $checkin_ar[0] . '-' . $checkin_ar[1];
		$checkout_ar = explode("/", trim($_POST['checkout']));
		$checkout = $checkout_ar[2] . '-' . $checkout_ar[0] . '-' . $checkout_ar[1];
		*/
		
		$checkin = $_POST['checkin'];
		$checkout = $_POST['checkout'];
		
		if(!isset($_SESSION['hotel_search'])) {
			$_SESSION['hotel_search'] = array();
			$_SESSION['hotel_search']['checkin'] = trim($_POST['checkin']);
			$_SESSION['hotel_search']['checkout'] = trim($_POST['checkout']);
			$_SESSION['hotel_search']['adults'] = $adults;
		}
		else {
			$_SESSION['hotel_search']['checkin'] = trim($_POST['checkin']);
			$_SESSION['hotel_search']['checkout'] = trim($_POST['checkout']);
			$_SESSION['hotel_search']['adults'] = $adults;
		}
	}
	
	
	
	$current_affiliate = explode("|", $_POST['current_affiliate']);
	$current_aff_code = $current_affiliate[0];
	$aff[] = $_POST['current_affiliate'];
	
	foreach ($aff as $i=>$affin) {
		$affex = explode("|", $affin);
		
		$aff_code = $affex[0];
		$aff_hotel_id = $affex[1];
		$aff_ref = $aff_code . '_' . $aff_hotel_id;
		
		if($aff_code == $current_aff_code) {
		
			$q = "select * from hotel_affiliates where aff_code='$aff_code' and aff_status='1';";
			//echo $q;
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) == 1) {
				$aff_data = mysql_fetch_assoc($r);
				
				if(isset($checkin)) {
					$aff_data['checkin'] = $checkin;
					$aff_data['checkout'] = $checkout;
				}
				
				if($aff_data['aff_name'] == "Expedia.com") {
					require_once(dirname(__FILE__) . "/Expedia.com.php");
					
					$aff_data['roomsCount'] = 1;
					$aff_data['rooms'] = array(
						array(
							"adultsCount"	=> $adults
						)
					);
					
					$expedia = new Expedia($aff_data, $aff_hotel_id);
					
					$ret[$aff_ref] = $expedia->getDeepLinkHotelBook();
					
					$ret['affiliate'] = $aff_ref;
					$ret['url'] = $expedia->getDeepLinkHotelBook();
				}
				
				if($aff_data['aff_name'] == "Booking.com") {
					require_once(dirname(__FILE__) . "/Booking.com.php");
					
					$aff_data['roomsCount'] = 1;
					$aff_data['rooms'] = array(
						array(
							"adultsCount"	=> $adults
						)
					);
					
					$booking = new Booking($aff_data, $aff_hotel_id);
					
					$ret['affiliate'] = $aff_ref;
					$ret['url'] = $booking->getDeepLinkHotelBook();
				}
			}
		}
		
	}
	
	echo json_encode($ret);
}


/**
 * **
 * FIND HOTELS
 */
if(isset($_POST['current_affiliate']) && trim($_POST['current_affiliate']) != "" && $_POST['act'] == "find-hotels") {
	$ret = array();
	
	//$destination = trim($_POST['destination']);
	$aff = array();
	$adults = intval($_POST['adults']);
	
	if(trim($_POST['checkin']) != "" && trim($_POST['checkout'])) {
		/*
		$checkin_ar = explode("/", trim($_POST['checkin']));
		$checkin = $checkin_ar[2] . '-' . $checkin_ar[0] . '-' . $checkin_ar[1];
		$checkout_ar = explode("/", trim($_POST['checkout']));
		$checkout = $checkout_ar[2] . '-' . $checkout_ar[0] . '-' . $checkout_ar[1];
		*/
		$checkin = $_POST['checkin'];
		$checkout = $_POST['checkout'];
		if(!isset($_SESSION['hotel_search'])) {
			$_SESSION['hotel_search'] = array();
			$_SESSION['hotel_search']['checkin'] = trim($_POST['checkin']);
			$_SESSION['hotel_search']['checkout'] = trim($_POST['checkout']);
			$_SESSION['hotel_search']['adults'] = $adults;
		}
		else {
			$_SESSION['hotel_search']['checkin'] = trim($_POST['checkin']);
			$_SESSION['hotel_search']['checkout'] = trim($_POST['checkout']);
			$_SESSION['hotel_search']['adults'] = $adults;
		}
	}
	
	
	$current_affiliate = explode("|", $_POST['current_affiliate']);
	$current_aff_code = $current_affiliate[0];
	$destination = $current_affiliate[1];
	
	$aff[] = $current_aff_code;
	
	foreach ($aff as $i=>$aff_code) {
		$aff_ref = $aff_code;
		if($aff_code == $current_aff_code) {
		
			$q = "select * from hotel_affiliates where aff_code='$aff_code' and aff_status='1';";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) == 1) {
				$aff_data = mysql_fetch_assoc($r);
				if(isset($checkin)) {
					$aff_data['checkin'] = $checkin;
					$aff_data['checkout'] = $checkout;
					
				}
				
				$aff_data['adults'] = $adults;
				
				if($aff_data['aff_name'] == "Expedia.com") {
					require_once(dirname(__FILE__) . "/Expedia.com.php");
					
					//print_r($aff_data);
					
					$expedia = new Expedia($aff_data);
					
					$ret['affiliate'] = $aff_ref;
					$ret['url'] = $expedia->getDeepLinkHotelDestination($destination);
				}
				
				if($aff_data['aff_name'] == "Booking.com") {
					require_once(dirname(__FILE__) . "/Booking.com.php");
					$booking = new Booking($aff_data);
					
					
					$ret['affiliate'] = $aff_ref;
					$ret['url'] = $booking->getDeepLinkHotelDestination($destination);
				}
			}
		}
		
	}
	
	echo json_encode($ret);
}

if(isset($_GET['bookwith'])) {
	header("Location: " . $_GET['bookwith']);
}

?>