<?php

//error_reporting(E_ALL);
//ini_set('display_errors', 1);
include './conn.php';
if (!empty($_POST["zip"]) && $_POST["zip"] != 'none') {

    $city = $_POST['city'];
    $zip = $_POST['zip'];
    $passengers = $_POST['passengers'];
    $suitcases = $_POST['suitcases'];
    $hour = $_POST['hour'];
    $boosterseat = $_POST['boosterseat'];
    $childseat = $_POST['childseat'];
    $carrycot = $_POST['carrycot'];
    $zusatzadresse = $_POST['zusatzadresse'];
    $payment = $_POST['payment'];
    $aa1zipX = $_POST['aa1zip'];
    $aa2zipX = $_POST['aa2zip'];
    $aa3zipX = $_POST['aa3zip'];
    $rueckfahrt = $_POST['rueckfahrt'];
    $hourreturn = $_POST['hourreturn'];
    $way = $_POST['way'];

    if ($passengers == '') {
	$auto = 'limo';
    }
    if ($suitcases == '0') {
	$auto = 'limo';
    }
    if ($suitcases <= 2) {
	$auto = 'limo';
    }
    if ($passengers <= 3) {
	$auto = 'limo';
    }
    if ($suitcases == 4 || $suitcases == 3) {
	$auto = 'kombi';
    }
    if ($passengers == 4) {
	$auto = 'kombi';
    }
    if ($suitcases <= 8 && $suitcases >= 5) {
	$auto = 'van';
    }
    if ($passengers <= 8 && $passengers >= 5) {
	$auto = 'van';
    }




    $result = mysql_query("SELECT child_seat_price,booster_seat_price,carrycot_price,additional_address_price,night_price_wien,night_price_other,price_balance,creditcard_price,from_airport_plus,from_to_minus FROM options WHERE id = '1' ");
    $row = mysql_fetch_array($result);
    $child_seat_price = $row['child_seat_price'];
    $booster_seat_price = $row['booster_seat_price'];
    $carrycot_price = $row['carrycot_price'];
    $additional_address_price = $row['additional_address_price'];
    $night_price_wien = $row['night_price_wien'];
    $night_price_other = $row['night_price_other'];
    $price_balance = $row['price_balance'];
    $creditcard_price = $row['creditcard_price'];
    $from_airport_plus = $row['from_airport_plus'];
    $from_to_minus = $row['from_to_minus'];

    $result = mysql_query("SELECT code FROM zip WHERE id = '{$zip}' ");
    $row = mysql_fetch_array($result);
    $plzX = $row['code'];

    $result = mysql_query("SELECT code FROM zip WHERE id = '{$aa1zipX}' ");
    $row = mysql_fetch_array($result);
    $aa1zip = $row['code'];

    $result = mysql_query("SELECT code FROM zip WHERE id = '{$aa2zipX}' ");
    $row = mysql_fetch_array($result);
    $aa2zip = $row['code'];

    $result = mysql_query("SELECT code FROM zip WHERE id = '{$aa3zipX}' ");
    $row = mysql_fetch_array($result);
    $aa3zip = $row['code'];
//echo $aa3zip;
    if ($zusatzadresse >= 1) {

	if ($aa1zip != 'none' && $aa1zip != '') {
	    $result = mysql_query("select price from additional_address_price where zip1='$plzX' and zip2 = '$aa1zip'") or die("MySQL-Error: " . mysql_error());
	    if (mysql_num_rows($result)) {
		while ($row = mysql_fetch_array($result)) {
		    $aa1zip_preis = $row['price'];
		}
	    } else {
		$aa1zip_preis = $additional_address_price;
	    }
	} else {
	    $aa1zip_preis = 0;
	}
    } else {
	$aa1zip_preis = 0;
    }

    if ($zusatzadresse >= 2) {
	if ($aa2zip != 'none' && $aa2zip != '') {
	    $result = mysql_query("select price from additional_address_price where zip1='$aa1zip' and zip2 = '$aa2zip'") or die("MySQL-Error: " . mysql_error());
	    if (mysql_num_rows($result)) {
		while ($row = mysql_fetch_array($result)) {
		    $aa2zip_preis = $row['price'];
		}
	    } else {
		$aa2zip_preis = $additional_address_price;
	    }
	} else {
	    $aa2zip_preis = 0;
	}
    } else {
	$aa2zip_preis = 0;
    }

    if ($zusatzadresse == '3') {

	if ($aa3zip != 'none' && $aa3zip != '') {
	    $result = mysql_query("select price from additional_address_price where zip1='$aa2zip' and zip2 = '$aa3zip'") or die("MySQL-Error: " . mysql_error());
	    if (mysql_num_rows($result)) {
		while ($row = mysql_fetch_array($result)) {
		    $aa3zip_preis = $row['price'];
		}
	    } else {
		$aa3zip_preis = $additional_address_price;
	    }
	} else {
	    $aa3zip_preis = 0;
	}
    } else {
	$aa3zip_preis = 0;
    }
    if ($zusatzadresse == '0') {
	$aa1zip_preis = 0;
	$aa2zip_preis = 0;
	$aa3zip_preis = 0;
    }

    $sql = mysql_query("select $auto from zip where id='$zip'");

    while ($row = mysql_fetch_array($sql)) {
	$price = $row[$auto];
	$price = ($price * $price_balance);



	if ($boosterseat == '1') {
	    $price = $price + $booster_seat_price;
	}
	if ($boosterseat == '2') {
	    $price = $price + $booster_seat_price + $booster_seat_price;
	}
	if ($boosterseat == '3') {
	    $price = $price + $booster_seat_price + $booster_seat_price + $booster_seat_price;
	}

	if ($childseat == '1') {
	    $price = $price + $child_seat_price;
	}
	if ($childseat == '2') {
	    $price = $price + $child_seat_price + $child_seat_price;
	}
	if ($childseat == '3') {
	    $price = $price + $child_seat_price + $child_seat_price + $child_seat_price;
	}

	if ($carrycot == '1') {
	    $price = $price + $carrycot_price;
	}
	if ($carrycot == '2') {
	    $price = $price + $carrycot_price + $carrycot_price;
	}
	if ($carrycot == '3') {
	    $price = $price + $carrycot_price + $carrycot_price + $carrycot_price;
	}


	$result = mysql_query("SELECT code FROM zip WHERE id = '{$zip}' ");
	$row = mysql_fetch_array($result);
	$plzX = $row['code'];

	$result = mysql_query("SELECT count(id) as total FROM night_price WHERE `hour`='{$hour}'");
	$row = mysql_fetch_array($result);

	if ($row ['total'] > 0) {
	    if ($city == '1') {
		$price = $price + $night_price_wien;
	    } else {
		$price = $price + $night_price_other;
	    }
	}

	$price = ($price + $aa1zip_preis + $aa2zip_preis + $aa3zip_preis);


	if ($payment == "Kreditkarte") {
	    $price = $price + $creditcard_price;
	}
	$pricezaruek = $price;
	if ($way == "VOM") {
	    $price = $price + $from_airport_plus;
	}

	if ($rueckfahrt == "2") {
	    $result = mysql_query("SELECT count(id) as total FROM night_price WHERE `hour`='{$hour}'");
	    $row = mysql_fetch_array($result);

	    if ($row ['total'] > 0) {
		if ($city == '1') {
		    $pricereturn = $pricezaruek - $night_price_wien;
		} else {
		    $pricereturn = $pricezaruek - $night_price_other;
		}
	    } else {
		$pricereturn = $pricezaruek;
	    }
	    if ($way == "ZUM") {
		$pricereturn = $pricereturn + $from_airport_plus;
	    }
	    $pricereturn = $pricereturn - $from_to_minus;

	    $result = mysql_query("SELECT count(id) as total FROM night_price WHERE `hour`='{$hourreturn}'");
	    $row = mysql_fetch_array($result);

	    if ($row ['total'] > 0) {
		if ($city == '1') {
		    $pricereturn = $pricereturn + $night_price_wien;
		} else {
		    $pricereturn = $pricereturn + $night_price_other;
		}
	    }
	} else {
	    $pricereturn = 0.00;
	}



	$price = number_format((float) $price, 2, '.', '');
	$pricereturn = number_format((float) $pricereturn, 2, '.', '');


	$array = array($price, $pricereturn);

	echo json_encode($array);

//echo $price;
    }
} else {
    $array = array('0', '0');

    echo json_encode($array);
}
?>