﻿var URLWebService;


var departureSelected = 0;
var arrivalSelected = 0;
var dateSelected = '';

var departureResortSelected = '';
var arrivalResortSelected = '';
var dateResortSelected = '';


function LoadingDestineFX(elem, li, hide) {
	var pos = li.lastIndexOf('_');
	var parts = [];
	parts[0] = li.substring(0, pos + 1);
	parts[1] = li.substr(pos + 1, li.length - 1);
	if (Number(parts[1]) == 1452)
		li = parts[0] + "0";

	if (hide) {
		$('#' + li).addClass('combo_loading');
		$('#' + elem).attr('style', 'visibility:hidden');
	}
	else {
		$('#' + li).removeClass('combo_loading');
		$('#' + elem).attr('style', 'visibility:visible');
	}
}

$(function () {
	URLWebService = GetBaseUrlWS() + '/WSPackage.asmx';
	//alert(URLWebService);

	var searchType = $('#divSearchCategory').html();
	if ($("#" + $("#hdnIsPostBack").html()).val() == 1) {
		LoadSelectedSearch(searchType);

		ClearDeparture(searchType);

	}
	else {

		if ($("#" + $("#rbtNational_" + searchType).html()).attr("checked"))
			rbtNational_click(searchType);

		if ($("#" + $("#rbtInternational_" + searchType).html()).attr("checked"))
			rbtInternational_click(searchType);

		startLoadNew(searchType);
	}

	ClearDestination(searchType);
	ClearDate(searchType);

	//Controle de idade das crianças.
	$('[id$=ddlKidPassengerPck]').change(function () {
		var kids = parseInt($(".ddlKidPassengerPck").val());
		$(".ddlKidPassengerPck").siblings('span').text(kids.toString());

		if (kids > 0) {
			$(".liKidAgePassengerPck").show();
		}

		switch (kids) {
			case 0:
				$(".criancas").hide();
				$("#childenPackage1").hide();
				$("#childenPackage2").hide();
				$("#childenPackage3").hide();
				$("#childenPackage4").hide();
				break;
			case 1:
				$(".criancas").show();
				$("#childenPackage1").css('display', 'inline-block');
				$("#childenPackage2").hide();
				$("#childenPackage3").hide();
				$("#childenPackage4").hide();
				break;
			case 2:
				$(".criancas").show();
				$("#childenPackage1").css('display', 'inline-block');
				$("#childenPackage2").css('display', 'inline-block');
				$("#childenPackage3").hide();
				$("#childenPackage4").hide();
				break;
			case 3:
				$(".criancas").show();
				$("#childenPackage1").css('display', 'inline-block');
				$("#childenPackage2").css('display', 'inline-block');
				$("#childenPackage3").css('display', 'inline-block');
				$("#childenPackage4").hide();
				break;
			case 4:
				$(".criancas").show();
				$("#childenPackage1").css('display', 'inline-block');
				$("#childenPackage2").css('display', 'inline-block');
				$("#childenPackage3").css('display', 'inline-block');
				$("#childenPackage4").css('display', 'inline-block');
				break;
			default:
		}
	});
});

function SetData(searchType) {
	$("#" + $("#divSelected_" + searchType).html()).val(
        $("#" + $("#ddlDeparture_" + searchType).html()).val() + ":" +
        $("#" + $("#ddlDeparture_" + searchType).html() + " option:selected").text() + ":" +
        $("#" + $("#ddlDestination_" + searchType).html()).val() + ":" +
        $("#" + $("#ddlDestination_" + searchType).html() + " option:selected").text() + ":" +
        $("#" + $("#ddlDate_" + searchType).html()).val() + ":" +
        $("#" + $("#ddlDate_" + searchType).html() + " option:selected").text()
        );
}

function ClearDeparture(searchType) {
	$("#" + $("#ddlDeparture_" + searchType).html()).find('option').remove().end();
	$("#" + $("#ddlDeparture_" + searchType).html()).find('optgroup').remove().end();
	//$("#" + $("#ddlDeparture_" + searchType).html()).append($("#" + $("#hdnddlDeparture_" + searchType).html()).text());//resources
}


function ClearDestination(searchType) {
	$("#" + $("#ddlDestination_" + searchType).html()).find('option').remove().end();
	$("#" + $("#ddlDestination_" + searchType).html()).find('optgroup').remove().end();
	$("#" + $("#ddlDestination_" + searchType).html()).append('<option value="">' + $("#labelSelectDeparture").html() + '</option>');
}

function ClearDate(searchType) {
	$("#" + $("#ddlDate_" + searchType).html()).find('option').remove().end();
	$("#" + $("#ddlDate_" + searchType).html()).append('<option value="">' + $("#labelSelectDestination").html() + '</option>');
}

function SetDeparture(searchType) {

	if (departureSelected != '') {
		$("#" + $("#ddlDeparture_" + searchType).html()).val(departureSelected);
		departureSelected = '';
		LoadDestination(searchType, $("#" + $("#ddlDeparture_" + searchType).html()).val().split('|')[0]);
	}
}

function updateSelect() {
	$.uniform.update("select");
	
}

function startLoadNew(searchType) {
	$("#" + $("#ddlDeparture_" + searchType).html()).unbind('change');
	$("#" + $("#ddlDeparture_" + searchType).html()).change(function () {
		$("#" + $("#ddlDestination_" + searchType).html()).focus();
		ClearDestination(searchType);
		ClearDate(searchType);
		LoadDestination(searchType, $(this).val().split('|')[0]);
		$(".ddlDeparture").siblings('span').text($("#" + $("#ddlDeparture_" + searchType).html() + " option:selected").text());
	});

	$("#" + $("#ddlDestination_" + searchType).html()).unbind('change');
	$("#" + $("#ddlDestination_" + searchType).html()).change(function () {
		$("#" + $("#ddlDate_" + searchType).html()).focus();

		ClearDate(searchType);
		LoadDate(searchType, $("#" + $("#ddlDeparture_" + searchType).html()).val().split('|')[0], $(this).val().split('|')[0]);
		$(".ddlDestination").siblings('span').text($("#" + $("#ddlDestination_" + searchType).html() + " option:selected").text());
	});

	$("#" + $("#ddlDate_" + searchType).html()).unbind('change');
	$("#" + $("#ddlDate_" + searchType).html()).change(function () {
		SetData(searchType);
		$(".ddlDate").siblings('span').text($("#" + $("#ddlDate_" + searchType).html() + " option:selected").text());
	});

	$("#" + $("#rbtNational_" + searchType).html()).unbind('click');
	$("#" + $("#rbtNational_" + searchType).html()).click(function () {
		rbtNational_click(searchType);
	});

	$("#" + $("#rbtInternational_" + searchType).html()).unbind('click');
	$("#" + $("#rbtInternational_" + searchType).html()).click(function () {
		rbtInternational_click(searchType);
	});
}

function rbtNational_click(searchType) {
	//$("#" + $("#ddlDeparture_" + searchType).html()).focus();

	ClearDeparture(searchType);
	ClearDestination(searchType);
	ClearDate(searchType);
	$(".ddlDeparture").siblings('span').text($("#" + $("#ddlDeparture_" + searchType).html() + " option[value='']").text());
	$(".ddlDestination").siblings('span').text($("#" + $("#ddlDestination_" + searchType).html() + " option[value='']").text());
	$(".ddlDate").siblings('span').text($("#" + $("#ddlDate_" + searchType).html() + " option[value='']").text());
	
	LoadDeparture(searchType);
}

function rbtInternational_click(searchType) {

	ClearDeparture(searchType);
	ClearDestination(searchType);
	ClearDate(searchType);
	$(".ddlDeparture").siblings('span').text($("#" + $("#ddlDeparture_" + searchType).html() + " option[value='']").text());
	$(".ddlDestination").siblings('span').text($("#" + $("#ddlDestination_" + searchType).html() + " option[value='']").text());
	$(".ddlDate").siblings('span').text($("#" + $("#ddlDate_" + searchType).html() + " option[value='']").text());

	LoadDeparture(searchType);
}


function bindSelectLocation(response, select) {
	var isFirst = true;
	var selectId = $(select).attr('id');
	$(select).find('option').remove().end();
	$(select).find('optgroup').remove().end();
	

	var groupId = -1;
	$(response.d).each(function (index) {
		if (response.d[index].IsNewGroup) {
			groupId++;
			$(select).append($('<optgroup />')
											.attr('id', selectId.replace('#', '') + "_gr_" + groupId)
											.attr('label', response.d[index].Country));
		}
		else {
			var option = $('<option />')
										.val(response.d[index].CityId)
										.html(response.d[index].CityName);
			if (response.d[index].Country != '') {
				option.attr('OptionGroup', response.d[index].Country);
				$('#' + selectId.replace('#', '') + "_gr_" + (groupId)).append(option);
			}
			else $(select).append(option);
		}
	});
}


function LoadDeparture(searchType) {
	var elemCombo = $("#ddlDeparture_" + searchType).html();
	LoadingDestineFX(elemCombo, 'liLoadingDeparture_' + searchType, true);
	$.ajax({
		type: "POST",
		url: URLWebService + "/LoadDeparture",
		data: "{isNational:" + $("#" + $("#rbtNational_" + searchType).html()).attr("checked") + ",idCategoria:" + searchType + ",distributionGroup:'" + distributionGroup + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function (response) {
			var selectId = "#" + $("#ddlDeparture_" + searchType).html();
			var select = $(selectId);

			bindSelectLocation(response, select)
			
			
			if (departureSelected != '') {
				$(select).val(departureSelected);
				departureSelected = '';
				LoadDestination(searchType, $(select).val().split('|')[0]);
			}
			
			startLoadNew(searchType);
			LoadingDestineFX(elemCombo, 'liLoadingDeparture_' + searchType, false);
			$(".ddlDeparture").siblings('span').text($("#" + $("#ddlDeparture_" + searchType).html() + " option:selected").text());
			updateSelect();
		},
		failure: function (msg) {
			//alert(msg)
			LoadingDestineFX(elemCombo, 'liLoadingDeparture_' + searchType, false);
		},
		error: function (msg) {
			//alert(msg)
			LoadingDestineFX(elemCombo, 'liLoadingDeparture_' + searchType, false);
		}
	});
}

function LoadDestination(searchType, departure) {
	var elemCombo = $("#ddlDestination_" + searchType).html();
	LoadingDestineFX(elemCombo, 'liLoadingDestination_' + searchType, true);
	$.ajax({
	    type: "POST",
	    url: URLWebService + "/LoadDestination",
	    data: "{isNational:" + $("#" + $("#rbtNational_" + searchType).html()).attr("checked") + ",departure:" + departure + ",idCategoria:" + searchType + ",distributionGroup:'" + distributionGroup + "'}",
	    contentType: "application/json; charset=utf-8",
	    dataType: "json",
	    success: function (response) {
			var selectId = "#" + $("#ddlDestination_" + searchType).html();
	    	var select = $(selectId);

	    	bindSelectLocation(response, select)
	     
	        if (arrivalSelected != '') {
	            $(select).val(arrivalSelected);
	            arrivalSelected = '';

	            LoadDate(searchType, $("#" + $("#ddlDeparture_" + searchType).html()).val().split('|')[0], $(select).val().split('|')[0]);
	        }

	        LoadingDestineFX(elemCombo, 'liLoadingDestination_' + searchType, false);
	        $(".ddlDestination").siblings('span').text($(selectId + " option:selected").text());
	        updateSelect();
	    },
	    failure: function (msg) {
	        //alert(msg.responseText);
	        LoadingDestineFX(elemCombo, 'liLoadingDestination_' + searchType, false);
	    },
	    error: function (msg) {
	        //alert(msg.responseText);
	        LoadingDestineFX(elemCombo, 'liLoadingDestination_' + searchType, false);
	    }
	});
}

function LoadDate(searchType, departure, destination) {
	if (typeof destination == 'undefined' || destination == '')
		return;

	var selectId = "#" + $("#ddlDate_" + searchType).html();
	var select = $(selectId);
	var elemCombo = $("#ddlDate_" + searchType).html();

	LoadingDestineFX(elemCombo, 'liLoadingData_' + searchType, true);

	$.ajax({
		type: "POST",
		url: URLWebService + "/LoadDate",
		data: "{departure:" + departure + ",destination:" + destination + ",idCategoria:" + searchType + ",distributionGroup:'" + distributionGroup + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function (response) {
			$("#" + $("#ddlDate_" + searchType).html()).find('option').remove().end();
			var html = '';
			$(response.d).each(function (index) {
				var dt = response.d[index].value.split("/");
				var dtstr = "";
				if (response.d[index].value != "") {
					var dtstr = dt[2] + '-' + dt[1] + '-' + dt[0];
				}
				html += '<option value="' + dtstr + '">' + response.d[index].text + '</option>';
			});

			$("#" + $("#ddlDate_" + searchType).html()).append(html);

				if (dateSelected != '') {
					$("#" + $("#ddlDate_" + searchType).html()).val(dateSelected);
					dateSelected = '';
					SetData(searchType);
				}

            LoadingDestineFX(elemCombo, 'liLoadingData_' + searchType, false);
            $(".ddlDate").siblings('span').text($("#" + $("#ddlDate_" + searchType).html() + " option:selected").text());
            updateSelect();
		},
		failure: function (msg) {
			LoadingDestineFX(elemCombo, 'liLoadingData_' + searchType, false);

		},
		error: function (msg) {
			LoadingDestineFX(elemCombo, 'liLoadingData_' + searchType, false);
		}
	});
}


function LoadSelectedSearch(searchType) {
	$.ajax({
		type: "POST",
		url: URLWebService + "/LoadSelectedSearch",
		data: "{searchType:" + searchType + "}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function (response) {
			if (response.d != null) {

				if (searchType == 1452) {
					departureResortSelected = response.d.Departure + "|" + response.d.DepartureCountry;
					arrivalResortSelected = response.d.Destination + "|" + response.d.DestinationCountry;
					if (response.d.SelectedCheckIn != null)
						dateResortSelected = response.d.SelectedCheckIn;
				}
				else {
					departureSelected = response.d.Departure + "|" + response.d.DepartureCountry;
					arrivalSelected = response.d.Destination + "|" + response.d.DestinationCountry;
					if (response.d.SelectedCheckIn != null)
						dateSelected = response.d.SelectedCheckIn;
				}
				$("#" + $("#rbtNational_" + searchType).html()).attr("checked", !response.d.IsInternational);
				$("#" + $("#rbtInternational_" + searchType).html()).attr("checked", response.d.IsInternational);
				if (!response.d.IsInternational) {
					rbtNational_click(searchType);
				} else {
					rbtInternational_click(searchType);
				}

				//$("#" + $("#ddlDestination_" + searchType).html()).change();

				//LoadDestination(searchType, response.d.Departure);
				//LoadDate(searchType, response.d.Departure);
				//$("#" + $("#ddlDestination_" + searchType).html()).val(response.d.Destination + "|" + response.d.DestinationCountry)
			}
			
		},
		failure: function (msg) {
			//alert(msg.responseText);
		},
		error: function (msg) {
			// alert(msg.responseText);
		}
	});
}
function ShowKidsAgePck(ddl) {
	var kids = $(".ddlKidPassengerPck").val();
	  
	if (kids > 0) {
		$(".liKidAgePassengerPck").show();
	}
	else {
		$(".liKidAgePassengerPck").hide();
	}
	for (i = 1; i < 7; i++) {
		if (i <= kids) {
			$(".ddlKidAge" + i).show();
		}
		else {
			$(".ddlKidAge" + i).hide();
		}
	}
	return false;
}

