<?php

//error_reporting(E_ALL);
//ini_set('display_errors', 1);
include './conn.php';

// var dataString = 'zip=' + plz + '&passengers=' + personen + '&suitcases=' + koffer + '&hour=' + hour + '&boosterseat=' + boosterseat + '&childseat=' + childseat + '&carrycot=' + carrycot + '&zusatzadresse=' + zusatzadresse + '&payment=' + payment + '&aa1zip=' + aa1zip + '&aa2zip=' + aa2zip + '&aa3zip=' + aa3zip;



if (!empty($_POST["zip"]) && $_POST["zip"] != 'none') {
//$plzX=$_POST['zip'];
//$result = mysql_query("SELECT code FROM zip WHERE id = '{$plzX}' ");
//$row = mysql_fetch_array($result);
//$plz = $row['code'];
//    $city = "";
    $zipX = $_POST['zip'];
    $dsa = explode('X', $zipX);
    $zip = $dsa[0];
    $city = $dsa[1];


    $passengers = $_POST['passengers'];
    $suitcases = 0;
    $hour = $_POST['hour'];
    $boosterseat = 0;
    $childseat = 0;
//    $carrycot = $_POST['carrycot'];
    $zusatzadresse = 0;
    $payment = 0;
    $aa1zipX = 0;
    $aa2zipX = 0;
    $aa3zipX = 0;
    $rueckfahrt = 0;
    $hourreturn = 0;
//    $gutschein = $_POST['gutschein'];




    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 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'];


    $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 ($gutschein != '' && strlen($gutschein) == 8) {
//            $result = mysql_query("SELECT COUNT(id) AS cnt,id,valid_od,valid_do,code,model,procent,fix,used,valid FROM gutschein WHERE code = '" . $gutschein . "' ");
//
//            $row = mysql_fetch_array($result);
//            if ($row['cnt'] > 0) {
//
//                $gs_id = $row['id'];
//                $gs_valid_od = $row['valid_od'];
//                $gs_valid_do = $row['valid_do'];
//                $gs_code = $row['code'];
//                $gs_model = $row['model'];
//                $gs_procent = $row['procent'];
//                $gs_fix = $row['fix'];
//                $gs_used = $row['used'];
//                $gs_valid = $row['valid'];
//
//                if ($gs_valid != '0') {
//
//                    $now = strtotime("now");
//                    $gs_valid_do = strtotime($gs_valid_do);
//                    if ($now < $gs_valid_do) {
//
//                        if ($gs_model == 'fix') {
//                            $price = $price - $gs_fix;
//                            $gsok = 'OK';
//                        } else if ($gs_model == 'procent') {
//                            $price = $price - (($price * $gs_procent) / 100);
//                            $gsok = 'OK';
//                        }
//                    }else{
//                         $gsok = 'EXPIRED';
//                    }
//                }else{
//                         $gsok = 'USED';
//                    }
//             }else{
//                 $gsok = 'ERROR';
//            }
//        }else{
//            $gsok = 'BLANK';
//        }


	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;
	    }

	    $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);
}
?>