var drawRegionsSelect = function (arr) {
	var tmpArrRegion = function (v) {
		var tmpArr = [], tmpSplit;
		for (var a in v) {
			tmpSplit = v[a].split('|');
			if (tmpSplit[1] === tmpSplit[2]) {
				tmpArr.push(tmpSplit[0] + '|' + tmpSplit[2] + '|' + tmpSplit[1]);
			}
		}
		tmpArr.sort();
		return tmpArr;
	}(arr);
	var tmpArrDestination = function (v) {
		var tmpArr = [], tmpSplit;
		for (var a in v) {
			tmpSplit = v[a].split('|');
			if (tmpSplit[1] !== tmpSplit[2]) {
				tmpArr.push(tmpSplit[0] + '|' + tmpSplit[2] + '|' + tmpSplit[1]);
			}
		}
		tmpArr.sort();
		return tmpArr;
	}(arr);
	var selectOpt1 = (document.getElementById('bengine').flugdauer.value !== '-1') ? 'selected="selected"' : '';
	var tmpArr = [];
	tmpArr.push('<select name="ziel_and_region" class="sel_width_310">');
    tmpArr.push('<optgroup label="Najpopularniejsze">');
        tmpArr.push('<option value="10010|10010">Egipt</option>');
        tmpArr.push('<option value="10008|10008">Tunezja</option>');
        tmpArr.push('<option value="10004|10004">Turcja</option>');
        tmpArr.push('<option value="10006|10006">Grecja</option>');
        tmpArr.push('<option value="10005|175">Kreta</option>');
        tmpArr.push('<option value="10005|295">Rodos</option>')
        tmpArr.push('<option value="10001|10001">Wyspy Kanaryjskie</option>');
    tmpArr.push('</optgroup>');
    tmpArr.push('<option value="1|1"' + ((document.getElementById('bengine').flugdauer.value === '1') ? ' selected="selected"' : '') + '>Blisko i ciepło (do 6 godzin lotu)</option>');
    tmpArr.push('<option value="2|2"' + ((document.getElementById('bengine').flugdauer.value === '2') ? ' selected="selected"' : '') + '>Daleko i egzotycznie (powyżej 6 godzin lotu)</option>');
    tmpArr.push('<optgroup label="Wszystkie">');
        tmpArr.push('<option value="-1"' + ((document.getElementById('bengine').flugdauer.value === '-1' && document.getElementById('bengine').zielgebiets_kenner.value === '-1') ? ' selected="selected"' : '') + '>dowolny</option>');
    tmpArr.push('</optgroup>');
	for (var a in tmpArrRegion) {
		var tmpSplit = tmpArrRegion[a].split('|');
		if (
			(document.getElementById('bengine').flugdauer.value !== '-1') &&
			(document.getElementById('bengine').zielgebiets_kenner.value === '-1') &&
			(document.getElementById('bengine').flugdauer.value === tmpSplit[1])
		) {
			tmpArr.push('<option value="' + tmpSplit[1] + '|' + tmpSplit[2] + '" selected="selected">' + tmpSplit[0] + '</option>');
		} else {
			tmpArr.push('<option value="' + tmpSplit[1] + '|' + tmpSplit[2] + '">' + tmpSplit[0] + '</option>');
		}
		for (var b in tmpArrDestination) {
			var tmpSplit2 = tmpArrDestination[b].split('|');
			if (tmpSplit2[1] === tmpSplit[1]) {
				if (
					(document.getElementById('bengine').flugdauer.value !== '-1') &&
					(document.getElementById('bengine').zielgebiets_kenner.value !== '-1') &&
					(document.getElementById('bengine').zielgebiets_kenner.value !== document.getElementById('bengine').flugdauer.value) &&
					(document.getElementById('bengine').zielgebiets_kenner.value === tmpSplit2[2])
				) {
					tmpArr.push('<option value="' + tmpSplit2[1] + '|' + tmpSplit2[2] + '" selected="selected">  - ' + tmpSplit2[0] + '</option>');
				} else {
					tmpArr.push('<option value="' + tmpSplit2[1] + '|' + tmpSplit2[2] + '">  - ' + tmpSplit2[0] + '</option>');
				}
			}
		}
	}
	tmpArr.push('</select>')
	$('#ZielAndRegion').html(tmpArr.join(''));
};
window.onload = function () {
	drawRegionsSelect(TTREGS_654);
	setInterval(
		function () {
			document.getElementById('kalender1').value = document.getElementById('ktag').value+"."+document.getElementById('kmonat').value;
			document.getElementById('kalender2').value = document.getElementById('rueckktag').value+"."+document.getElementById('rueckkmonat').value;
		}, 50
	);
};



