﻿// ========================================
// document ready

$(function () {
	// dept airport
	PackageDeparturePointLoad();
	$("#pk_packdept").change(PackageDeparturePointChange);

	// dest airport
	$("#pk_packdest").change(PackageDestinationPointChange);

	// Search button click
	$("#engine-search").click(function () {
		$("#engine-search-wait").css('visibility', 'visible');
		$("#engine-search").attr('disabled', 'disabled');
		$.post(
		window._url_searchinit,
		$("#engine-packages").serialize(),
		function (res) {
			if (res.Errors == "") {
				var act = window._url_search;
				$(location).attr('href', act);
			}
			else {
				$("#engine-search").attr('disabled', '');
				$("#engine-search-wait").css('visibility', 'hidden');
				$("#engine-errors").text(res.Errors);
			}
		});
	});

	// Search button click
	$("#Search").click(function () { DoSearch(); });
});

function DoSearch() {
	$("#wait").css('visibility', '');
	$("#Search").attr('disabled', 'disabled');
	var params = $("#engine-packages").serialize();
	$.post(
		window._url_searchinit,
		params,
		function (res) {
			if (res.Errors == "") {
				var act = window._url_search;
				$(location).attr('href', act);
			}
			else {
				$("#wait").css('visibility', 'hidden');
				$("#Search").attr('disabled', '');
				$("#engine-errors").text(res.Errors);
			}
		});
}

// ========================================
// document ready

// ========================================

function PackageDeparturePointLoad() {
	$('#pk_packdept').find('option').remove();
	var url = window._url_packdept; // "&callback=?";
	$.getJSON(url,
			function (data) {
				$.each(data, function (i, result) {
					$('#pk_packdept').find('option').end().append('<option value="' + result.Key + '">' + result.Value + '</option>');
				});
				//set using state or cookie if fresh
				try {
					if (window._dept_point && window._dept_point != "")
						$('#pk_packdept').attr("value", window._dept_point);
					else
						$('#pk_packdept').attr("value", $.cookie('DepartureAirport'));
				} catch (ex) { }
				PackageDeparturePointChange2();
			});
}

function PackageDeparturePointChange() {
	var q1 = $('#pk_packdept').attr("value");
	var q2 = $('#pk_packdept option:selected').text();
	var date = new Date(2049,1,1,1,1,1,1);
	$.cookie('DepartureAirport', q1, { expires: date });
	$.cookie('DepartureAirportName', q2, { expires: date });
	PackageDeparturePointChange2();
}

function PackageDeparturePointChange2() {
	$("#pk_packdest").attr('disabled', 'true');
	$("#pk_packdestloading").css('visibility', 'visible');
	var q = $('#pk_packdept').attr("value");
	$('#pk_packdest').find('option').remove();
	var url = window._url_packdest + "?dept=" + q; // "&callback=?";
	$.getJSON(url,
					function (data) {
						var found = false;
						$.each(data, function (i, result) { found |= result.Key == window._dest_point; });
						$.each(data, function (i, result) {
							$('#pk_packdest').find('option').end().append('<option value="' + result.Key + '">' + result.Value + '</option>');
						});
						$("#pk_packdest").attr('disabled', '');
						$("#pk_packdestloading").css('visibility', 'hidden');
						if (found)
							$('#pk_packdest').val(window._dest_point);
						else
							$("#pk_packdest option:first").attr('selected', 'selected');
						PackageDestinationPointChange();
					});
}

// ========================================

function PackageDestinationPointChange() {
	var q1 = $('#pk_packdept').attr("value");
	var q2 = $('#pk_packdest').attr("value");
	$("#pk_packhotel").attr('disabled', 'true');
	$("#pk_packhotelloading").css('visibility', 'visible');
	$('#pk_packhotel').find('option').remove();
	var url1 = window._url_packhotel + '/?dept=' + q1 + '&dest=' + q2; // "&callback=?";
	$.getJSON(url1,
			function (data) {
				var found = false;
				$.each(data, function (i, result) { found |= result.Key == window._dest_hotel; });
				$.each(data, function (i, result) { $('#pk_packhotel').find('option').end().append('<option value="' + result.Key + '">' + result.Value + '</option>'); });
				$("#pk_packhotel").attr('disabled', '');
				$("#pk_packhotelloading").css('visibility', 'hidden');
				if (found)
					$('#pk_packhotel').val(window._dest_hotel);
				else
					$("#pk_packhotel option:first").attr('selected', 'selected');
		});
	$("#pk_packdur").attr('disabled', 'true');
	$("#pk_packdurloading").css('visibility', 'visible');
	$('#pk_packdur').find('option').remove();
	var url2 = window._url_packdur + '/?dept=' + q1 + '&dest=' + q2; // "&callback=?";
	$.getJSON(url2,
			function (data) {
				var found = false;
				$.each(data, function (i, result) { found |= result.Key == window._dest_dur; });
				$.each(data, function (i, result) { $('#pk_packdur').find('option').end().append('<option value="' + result.Key + '">' + result.Value + '</option>'); });
				$("#pk_packdur").attr('disabled', '');
				$("#pk_packdurloading").css('visibility', 'hidden');
				if (found)
					$('#pk_packdur').val(window._dest_dur);
				else
					$("#pk_packdur option:first").attr('selected', 'selected');
				if (window._do_search) {
					window._do_search = false;
					DoSearch();
				}
	});
}

// ========================================

