﻿var SelectFlightTicketsArray = new Array();
var InputFlightTicketsArray = new Array();
var SelectedDropDownId = null;
var SelectedDropDownValue = null;

function InitializeDropDownValues() {
	SelectedDropDownValue = new Array();
	for (var i = 0; i < SelectFlightTicketsArray.length; i++) {
		SelectedDropDownValue[i] = null;
	}
}

function RememberSelection(selectId) {
	if (SelectedDropDownValue == null) {
		InitializeDropDownValues();
	}
	var lContainer = document.getElementById(selectId);
	if (lContainer != null) {
		SelectedDropDownId = selectId;
		var index = GetIndexOfSelect(selectId);
		if (SelectedDropDownValue[index] == null || SelectedDropDownValue[index] == '') {
			SelectedDropDownValue[index] = lContainer.options[1].value;
		}
	}
}

function SetRememberSelections() {
	var index = GetIndexOfSelect(SelectedDropDownId);
	if (SelectedDropDownId != null && SelectedDropDownValue[index] != null) {
		var lSelectElement = document.getElementById(SelectedDropDownId);
		if (lSelectElement.selectedIndex == 0) {
			lSelectElement.value = SelectedDropDownValue[index];
		}
	}
}

function GetIndexOfSelect(selectId) {
	for (var i = 0; i < SelectFlightTicketsArray.length; i++) {
		if (SelectFlightTicketsArray[i] == selectId) {
			return i;
		}
	}
}

function VerifyDestAirport(selectElem, txtBoxId, errId, popUpContainer, closeText, popUpUrl, selectId) {
	var lTxtAirOther = document.getElementById(txtBoxId);
	if (selectElem.selectedIndex == 0) {
		CreateFlightTicketsOverlayPopUpWindow(popUpContainer, closeText, popUpUrl, selectId);
	}
	else {
		if (SelectedDropDownValue == null) {
			InitializeDropDownValues();
		}
		if (selectElem.value != '') {
			SelectedDropDownValue[GetIndexOfSelect(selectId)] = selectElem.value;
		}
	}
	if (selectElem.options[selectElem.selectedIndex].value == '') {
		lTxtAirOther.style.display = 'block';
	}
	else {
		lTxtAirOther.style.display = 'none';
		document.getElementById(errId).style.display = 'none';
	}
}

var CountryId;
var PopulateSelectId;
var PopulateSelectIdSupp;
var UniqueKey;
var UniqueKeySupp;
var AllGroup = new Array();
var OptionsObject = new Array();
var DirectFlights = new Array();
var SelectedValue = null;
var Options = null;

function AddDirectFlights(selectElem, countryId, populateSelectId, uniqueKey) {
	CountryId = countryId;
	
	SelectedValue = document.getElementById(populateSelectId).value;
	if (uniqueKey.indexOf("_supp") > 0) {
		UniqueKeySupp = uniqueKey;
		PopulateSelectIdSupp = populateSelectId;
	}
	else {
		UniqueKey = uniqueKey;
		PopulateSelectId = populateSelectId;
	}
	if (typeof (AllGroup[uniqueKey]) == "undefined" || AllGroup[uniqueKey] == null) {
		AllGroup[uniqueKey] = $("#allOptionGroup_" + uniqueKey).clone();
		$("#allOptionGroup_" + uniqueKey).remove();
	}

	if (Options == null) {
		Options = $("#" + populateSelectId + " option").clone();
	}
	
	if (typeof(OptionsObject[uniqueKey]) == "undefined" || OptionsObject[uniqueKey] == null)	{
		OptionsObject[uniqueKey] = $("#" + populateSelectId + " option:not(optgroup > *):not([value='map']):not([value=''])").clone();
		$("#" + populateSelectId + " option:not(optgroup > *):not([value='map']):not([value=''])").remove();
	}
	
	if (typeof(DirectFlights[uniqueKey]) == "undefined" || DirectFlights[uniqueKey] == null)	{
		DirectFlights[uniqueKey] = $("#directOpt_" + uniqueKey).clone();
		$("#directOpt_" + uniqueKey).remove();
	}

	if (uniqueKey.indexOf("_supp") > 0) {
		NetMatch.Zoover.Web.Services.Suppliers.FlightTicketsService.GetDirectFlights(selectElem.value, countryId, OnDirectFlightsAvailableSupp, null, null);
	}
	else {
		NetMatch.Zoover.Web.Services.Suppliers.FlightTicketsService.GetDirectFlights(selectElem.value, countryId, OnDirectFlightsAvailable, null, null);
	}
}

function OnDirectFlightsAvailableSupp(result) {
	HandleResults(result, UniqueKeySupp, PopulateSelectIdSupp);
}

function OnDirectFlightsAvailable(result) {
	HandleResults(result, UniqueKey, PopulateSelectId);
}

function HandleResults(result, uniqueKey, populateSelectId) {
	if (result != null && result.length > 0) {
		var lOptGroup = "";
		for (var i = 0; i < result.length; i++) {
			var lAirportName = GetValueForId(Options, result[i]);
			if (typeof (lAirportName) != "undefined") {
				lOptGroup += "<option value=\"" + result[i] + "\" airportname=\"" + lAirportName.attributes["airportname"].value + "\">"
					+ lAirportName.text + "</option>";
			}
		}

		if (lOptGroup != "") {
			$("#" + populateSelectId + " option[value='map']").after($(DirectFlights[uniqueKey]));
			$("#directOpt_" + uniqueKey).html($(lOptGroup));
			HandleGrouping(true, uniqueKey, populateSelectId);
		}
		else {
			$("#directOpt_" + uniqueKey).remove();
			HandleGrouping(false, uniqueKey, populateSelectId);
		}
	}
	else {
		$("#directOpt_" + uniqueKey).remove();
		HandleGrouping(false, uniqueKey, populateSelectId);
	}
	$("#" + populateSelectId).val(SelectedValue);
}

function HandleGrouping(status, uniqueKey, populateSelectId) {
	if (status) {
		$("#directOpt_" + uniqueKey).after($(AllGroup[uniqueKey]));
	}
	else {
		var lToRemove = $("#allOptionGroup_" + uniqueKey);
		if (lToRemove.length != 0) {
			lToRemove.remove();
		}
	}

	if (status) {
		var lToRemove = $("#" + populateSelectId + " option:not(optgroup > *):not([value='map']):not([value=''])");
		if (lToRemove.length != 0) {
			lToRemove.remove();
		}
	}
	else {
		$("#" + populateSelectId + " option[value='map']").after($(OptionsObject[uniqueKey]));
		
	}
}

function GetValueForId(options, value) {
	for (var i = 0; i < options.length; i++) {
		if (options[i].value == value) {
			return options[i];
		}
	}
}

function SelectAirportFromIframe(selectId, value, valueName) {
	var lSelectElement = document.getElementById(selectId);
	var lExistsOption = false;
	for (var i=0; i<lSelectElement.options.length;i++)
	{
		if (lSelectElement.options[i].value == value){
			lExistsOption = true;
			break;
		}
	}
	var lCount = -1;
	for (var j = 0; j < SelectFlightTicketsArray.length; j++) {
		if (SelectFlightTicketsArray[j] == selectId) {
			lCount = j;
			break;
		}
	}
	
	if (lExistsOption) {
		lSelectElement.value = value;
		SelectedDropDownValue[GetIndexOfSelect(selectId)] = value;
		if (lCount >= 0) {
			var lInputText = document.getElementById(InputFlightTicketsArray[lCount]);
			if (lInputText != null) {
				lInputText.style.display = "none";
			}
		}
	}
	else {
		
		if (lCount >= 0) {
			var lInputText = document.getElementById(InputFlightTicketsArray[lCount]);
			if (lInputText != null) {
				lSelectElement.value = "";
				lInputText.style.display = "block";
				lInputText.value = valueName;
			}
		}
	}
}

function ChangeDestinationSelect(uniqueKeyword) {
	var lDepAirport = document.getElementById(("selCarsFrom_" + uniqueKeyword)).value;
	if (lDepAirport != '' && lDepAirport != 'map') {
		var lDestinationAirport = document.getElementById(("selCarsTo_" + uniqueKeyword));
		lDestinationAirport.value = lDepAirport;
	}
}

function ValidateFlightTicketAndCarData(startIdFrom, startIdTo, uniqueKeyword, select1, select2) {
	var lErrorMessageFrom = document.getElementById(("errorFrom_" + uniqueKeyword));
	var lErrorMessageTo = document.getElementById(("errorTo_" + uniqueKeyword));
	lErrorMessageFrom.style.display = "none";
	lErrorMessageTo.style.display = "none"
	var lReturn = true;
	
	//Verify that the value of the airport departure is not the map option
	if (select1 != null && select1.value == 'map') {
		lErrorMessageFrom.style.display = "block";
		lReturn = false;
	}

	var lInputText = document.getElementById((startIdFrom + uniqueKeyword));
	if (lInputText.style.display == "block") {
		if (lInputText.value == '') {
			lErrorMessageFrom.style.display = "block";
			lReturn = false;
		}
	}

	//Verify that the value of the airport destination is not the map option
	if (select2 != null && select2.value == 'map') {
		lErrorMessageTo.style.display = "block";
		lReturn = false;
	}

	var lInputTextTo = document.getElementById((startIdTo + uniqueKeyword));
	if (lInputTextTo.style.display == "block") {
		if (lInputTextTo.value == '') {
			lErrorMessageTo.style.display = "block";
			lReturn = false;
		}
	}
	var lDate = ValidateDateFlight(uniqueKeyword);
	lReturn = lReturn && lDate;

	return lReturn;
}

function ValidateDateFlight(uniqueKeyword) {
	var lDepDay = document.getElementById(("depDayFlight_" + uniqueKeyword));
	var lDepMonth = document.getElementById(("depMonthFlight_" +uniqueKeyword));
	var lRetDay = document.getElementById(("retDayFlight_" + uniqueKeyword));
	var lRetMonth = document.getElementById(("retMonthFlight_" + uniqueKeyword));
	var lDepYear = lDepMonth.value.substr(0, 4);
	var lDepMonthValue = lDepMonth.value.substr(4, 2);
	var lRetYear = lRetMonth.value.substr(0, 4);
	var lRetMonthValue = lRetMonth.value.substr(4, 2);
	
	var lDepDate = new Date();
	lDepDate.setFullYear(lDepYear, trimDateFlight(lDepMonthValue) - 1, lDepDay.value);
	
	var lRetDate = new Date();
	lRetDate.setFullYear(lRetYear, trimDateFlight(lRetMonthValue) - 1, lRetDay.value);
	var lErrorMessage = document.getElementById(("dateError_" + uniqueKeyword));
	lErrorMessage.style.display = "none";
	if (lRetDate < lDepDate) {
		lErrorMessage.style.display = "block";
		return false;
	}
	return true;
}

function trimDateFlight(month) {
	if (month[0] == '0') {
		return month[1];
	}
	return month;
}

var lFlightWin;
var lCarWin;
function flightRedirect(uniqueKeyword) {
	if (!ValidateFlightTicketAndCarData("txtFlightFrom_", "txtFlightTo_", uniqueKeyword,
		document.getElementById(("selFlightsFrom_" + uniqueKeyword)), document.getElementById(("selFlightsTo_" + uniqueKeyword)))) {
		return;
	}
	var lChildren = document.getElementById(("selbOfChildren_" + uniqueKeyword)).value;
	var lAdults = document.getElementById(("selNbOfAdults_" + uniqueKeyword)).value;
	var lAirport;
	var lDepatureAirport;
	var lInputText = document.getElementById(("txtFlightTo_" + uniqueKeyword));
	var lDepatureAirportInput = document.getElementById(("txtFlightFrom_" + uniqueKeyword));
	var lDepartureAirportName;
	var lDestinationAirportName;
	if (lDepatureAirportInput.style.display == 'block') {
		lDepatureAirport = lDepatureAirportInput.value;
		lDepartureAirportName = lDepatureAirportInput.value;
	}
	else {
		var lCurrentSelect = document.getElementById(("selFlightsFrom_" + uniqueKeyword));
		lDepatureAirport = lCurrentSelect.value;
		lDepartureAirportName = lCurrentSelect.options[lCurrentSelect.selectedIndex].attributes["airportname"].value;
	}
	if (lInputText.style.display == 'block') {
		lAirport = lInputText.value;
		lDestinationAirportName = lInputText.value;
	}
	else {
		var lCurrentSelectTo = document.getElementById(("selFlightsTo_" + uniqueKeyword));
		lAirport = lCurrentSelectTo.value;
		lDestinationAirportName = lCurrentSelectTo.options[lCurrentSelectTo.selectedIndex].attributes["airportname"].value;
	}
	var lDepartureDay = document.getElementById(("depDayFlight_" + uniqueKeyword)).value;
	var lDepartureYearMonth = document.getElementById(("depMonthFlight_" + uniqueKeyword)).value;
	var lReturnDay = document.getElementById(("retDayFlight_" + uniqueKeyword)).value;
	var lReturnYearMonth = document.getElementById(("retMonthFlight_" + uniqueKeyword)).value;
	var lLink;
	
	NetMatch.Zoover.Web.Services.Suppliers.FlightTicketsService.GetFlightTicketsUrl(lChildren, lDepatureAirport, lAirport, lDepartureDay,
		lDepartureYearMonth, lReturnDay, lReturnYearMonth, lAdults, EscapeSequence(lDepartureAirportName), EscapeSequence(lDestinationAirportName),
		GetDateForQueryString(lDepartureDay, lDepartureYearMonth), GetDateForQueryString(lReturnDay, lReturnYearMonth), OnFlightLinkAvailable,
		OnTimeOut, null);
	lFlightWin = window.open("");
	lFlightWin.opener = this;
	lFlightWin.focus();
}

function OnFlightLinkAvailable(result) {
	lFlightWin.location.href = result;
}

function OnCarLinkAvailable(result) {
	lCarWin.location.href = result;
}

function OnTimeOut() {

}

function EscapeSequence(value) {
	return encodeURIComponent(value);//.replace(' ', '%2B');
}

function GetDateForQueryString(day, yearMonth) {
	return day + EscapeSequence(' ') + monthNames[trimDateFlight(yearMonth.substr(4, 2)) - 1] + EscapeSequence(' ') + yearMonth.substr(0, 4);
}

function carRedirect(uniqueKeyword) {
	if (!ValidateFlightTicketAndCarData("txtCarFrom_", "txtCarTo_", uniqueKeyword, document.getElementById(("selCarsFrom_" + uniqueKeyword)),
		document.getElementById(("selCarsTo_" + uniqueKeyword)))) {
		return;
	}
	var lAirport;
	var lDepatureAirport;
	var lDepartureAirportName;
	var lDestinationAirportName;
	var lInputText = document.getElementById(("txtCarTo_" + uniqueKeyword));
	var lInputFrom = document.getElementById(("txtCarFrom_" + uniqueKeyword));
	if (lInputFrom.style.display == "block") {
		lDepatureAirport = lInputFrom.value;
		lDepartureAirportName = lInputFrom.value;
	}
	else {
		var lCurrentSelect = document.getElementById(("selCarsFrom_" + uniqueKeyword));
		lDepatureAirport = lCurrentSelect.value;
		lDepartureAirportName = lCurrentSelect.options[lCurrentSelect.selectedIndex].attributes["airportname"].value;
	}
	
	if (lInputText.style.display == 'block') {
		lAirport = lInputText.value;
		lDestinationAirportName = lInputText.value;
	}
	else {
		var lCurrentSelectTo = document.getElementById(("selCarsTo_" + uniqueKeyword));
		lAirport = lCurrentSelectTo.value;
		lDestinationAirportName = lCurrentSelectTo.options[lCurrentSelectTo.selectedIndex].attributes["airportname"].value;
	}
	var lDepartureDay = document.getElementById(("depDayFlight_" + uniqueKeyword)).value;
	var lDepartureYearMonth = document.getElementById(("depMonthFlight_" + uniqueKeyword)).value;
	var lReturnDay = document.getElementById(("retDayFlight_" + uniqueKeyword)).value;
	var lReturnYearMonth = document.getElementById(("retMonthFlight_" + uniqueKeyword)).value;
	var lLink;
	var lPickUpTime = document.getElementById(("depTimeFlight_" + uniqueKeyword)).value;
	var lDropOffTime = document.getElementById(("retTimeFlight_" + uniqueKeyword)).value;
	NetMatch.Zoover.Web.Services.Suppliers.FlightTicketsService.GetCarRentalUrl(lDepatureAirport, lDepartureDay, lDepartureYearMonth, EscapeSequence(lPickUpTime),
		lAirport, lReturnDay, lReturnYearMonth, EscapeSequence(lDropOffTime), EscapeSequence(lDepartureAirportName), EscapeSequence(lDestinationAirportName), 
		EscapeSequence(GetDateForQueryString(lDepartureDay, lDepartureYearMonth)), EscapeSequence(GetDateForQueryString(lReturnDay, lReturnYearMonth)), OnCarLinkAvailable, OnTimeOut, null);
	lCarWin = window.open("");
	lCarWin.opener = this;
	lCarWin.focus();
}
