﻿function listGetAt(list, position){
	var arrayOfList = list.split(",");
	var listItem = "";
	if (position < arrayOfList.length){
			listItem = arrayOfList[position];
	}
	return listItem;
}
function indexOfAirportInArray(array, cityCode, airportCode){
	var i = null, indexOfValue = null;
	for (i in array){
		if (array[i][2] == cityCode && array[i][0] == airportCode){
			indexOfValue = i;
			break;
		}
	}
	return indexOfValue;
}
function regionTypeOfAirport(selectedValue){
	var regionType = "";
	if (selectedValue.length){
		regionType = airports[indexOfAirportInArray(airports, listGetAt(selectedValue, 1), listGetAt(selectedValue, 0))][3];
	}
	return regionType;
}
function flightIsDomestic(fromValue, toValue){
	var regionTypeFrom = regionTypeOfAirport(fromValue);
	var regionTypeTo = regionTypeOfAirport(toValue);
	return regionTypeFrom == 'dom' && regionTypeTo == 'dom';
}
function fillTheAirportsToList(airports, listToFill, addCityCodeToTheValue, addAirportCodeToTheValue, selectedAirportCode, addTheAllItem, excludeAirportCodes, language) {
	
	function getIndex(arr, val) {
		for (var i=0; i<arr.length; ++i) {
			if (arr[i]==val) return i;
		}
		return null;
	}

	var valueToAdd = "";
	var textToAdd = "";
	var messageToAdd = "";
	var itemInOrder = 0;

	//liste boşsa. yani daha önce doldurulmamışsa (varsayılan "." item'ı göz ardı edilir)
	if (listToFill.options.length <= 1){
		switch(language){
			case "tr":
				messageToAdd = "Seçiniz";
			break;
			//"en" and other posibilities
			default:
				messageToAdd = "Please select";
			break;
		}
		//listenin boş olduğundan emin ol
		listToFill.options.length--;
		//mesaj item'ı
		listToFill.options[(++listToFill.options.length)-1] = new Option(messageToAdd, "");

		//listeyi doldurmak için dönmeye başla
		for (var i=0; i < airports.length; ++i) {
			valueToAdd = "";
			textToAdd = "";
			if (getIndex(excludeAirportCodes, airports[i])!=null) {
				continue;
			}
			if (addTheAllItem == true || airports[i][0] != "ALL") {
				textToAdd = airports[i][1];
				if (addCityCodeToTheValue){
					if (addAirportCodeToTheValue){
						valueToAdd = airports[i][0] + ',' + airports[i][2];
					}else{
						valueToAdd = airports[i][2];
					}
				}else if (addAirportCodeToTheValue){
					valueToAdd = airports[i][0];
				}
				listToFill.options[(++listToFill.options.length)-1] = new Option(textToAdd, valueToAdd);
			}
		}
	}
}

