
var sSearchBoxOpen = '';
var sSearchBoxType = '';
var QS = new Querystring();

document.onkeypress = CheckKeyPress
document.onclick = CheckClick

function SearchBox(sListType,sSelect,sCode,iIgnore,sReturn) {
	if (sSearchBoxOpen != sCode && sSearchBoxOpen !='') {
		SBClose(sSearchBoxOpen);
		if (navigator.userAgent.indexOf('MSIE 6') != -1) return;
	}
	if (sSearchBoxOpen == sCode) {
		SBClose(sCode);
		return;
	}
	var SearchBox 						= document.getElementById('SearchBox');
	var SearchBoxScroll 			= document.getElementById('SearchBoxScroll');
	var SearchBoxButtons 			= document.getElementById('SearchBoxButtons');
	var SearchBoxButtonOK			= document.getElementById('SB_B_3');
	var ListBox 							= document.getElementById('ListBox_'+sCode);
	var ListBoxIcon 					= document.getElementById('ListSymbol_'+sCode);
	ListBoxIcon.src 					= '/~global/images/buttons/list_open_down.gif';
	sSearchBoxOpen						= sCode;
	sSearchBoxType						= sListType;
	var item = 1; iScrollBarWidth = 0;
	var ListArray, AJAXReturn = '', ListEntry = ''; ListGraphics = ''; iListBoxWidth = parseInt(GetStyle(ListBox,'width')) - 2;
	if (navigator.userAgent.indexOf('Gecko') != -1) iListBoxWidth = iListBoxWidth + 2;
	AJAXReturn = decodeURI(SendData('/ajax/searchbox.asp','Select=' + sSelect + '&Code=' + sCode + '&Ignore=' + iIgnore + '&Return='+ sReturn, true,'txt'));
	if (AJAXReturn.indexOf('|') == -1) {
		alert('Error in processing: ' + AJAXReturn);
	} else {
		ListArray = AJAXReturn.split('|');
	}
	for (var i=4; i<ListArray.length; i=i+4) {
		ListGraphics = 'B';
		ListEntry += '<div id="SB_' + item + '" class="SearchBoxEntry" ';
			if (ListArray[i+2] == 0) {
				ListEntry += 'style="color:#C0C0C0;" ';
				ListGraphics = 'G';
			}
			ListEntry += 'onmouseout="SBOut(' + item + ')" onmouseover="SBOver(' + item + ')" onclick="SBSelect(' + item + ')">';
			if (ListArray[i+3] == 0) {
				ListEntry += '<img id="SB_I_' + item + '" class="SearchBoxEntry" src="/~global/images/buttons/list_' + sSearchBoxType + '_inactive.gif">';
			} else {
				ListEntry += '<img id="SB_I_' + item + '" class="SearchBoxEntry" src="/~global/images/buttons/list_' + sSearchBoxType + '_active.gif">';
			}
			if (sCode == 'KA' && ListArray[i] != 0) {
				ListEntry += '<span id="SB_T_' + item + '" class="SearchBoxEntry" onclick="SBSelect(' + item + ')">';
				ListEntry += '<img id="SB_T_' + item + '" src="/~global/images/kategorien/' + ListGraphics + ListArray[i+1] + '.gif" onclick="SBSelect(' + item + ')"/>'
			} else {
				ListEntry += '<span id="SB_T_' + item + '" class="SearchBoxEntry">';
				ListEntry += ListArray[i+1];
			}
			ListEntry += '</span>';
			ListEntry += '<SB id="SB_A_' + item + '" count="' + ListArray[i+2] + '" VAL="' + ListArray[i] + '"></SB>';
		ListEntry += '</div>';
		item++;
	}
	i--;
	ListEntry += '<sb id="SB_A_0" type="'+ ListArray[0] + '" count="'+ ListArray[1] + '"></sb>'
	if (parseInt(ListArray[1]) >= 10) iScrollBarWidth = 20;
	if ((parseInt(ListArray[3]) + 20 + iScrollBarWidth) >= iListBoxWidth) {
		SearchBox.style.width = parseInt(ListArray[3]) + 18 + iScrollBarWidth + 'px';
	} else {
		SearchBox.style.width = iListBoxWidth + 'px';
	}
	SearchBox.style.left = GetPosition(ListBox)[0] + 'px';
	SearchBox.style.top = GetPosition(ListBox)[1] + parseInt(GetStyle(ListBox,'height')) + 1 + 'px';
	if ( (document.getElementById('containerMain')) && ( (GetPosition(ListBox)[0] + parseInt(GetStyle(SearchBox,'width'))) >= parseInt(GetStyle(document.getElementById('containerMain'),'width')) ) ) {
		SearchBox.style.left = GetPosition(ListBox)[0] - (parseInt(GetStyle(SearchBox,'width')) - iListBoxWidth) + 'px';
	}
	if (document.getElementById('filter').style.top) {
		SearchBox.style.top = parseInt(SearchBox.style.top) - parseInt(document.getElementById('filter').style.top) + 'px';
	}
	SearchBoxScroll.innerHTML = ListEntry;
	SearchBoxScroll.style.width = parseInt(GetStyle(SearchBox,'width')) + 'px';
	SearchBoxButtons.style.width = parseInt(GetStyle(SearchBox,'width')) + 'px';
	SearchBoxButtonOK.style.width = parseInt(GetStyle(SearchBox,'width')) + 'px';
	if (ListArray[1] <= 10) {
		SearchBoxScroll.style.height = (ListArray[1]*20) + 'px';
	} else {
		SearchBoxScroll.style.height = '180px';
	}			
	SearchBox.style.height = (parseInt(SearchBoxScroll.style.height) + 20) + 'px';
	SearchBoxButtons.style.top = (parseInt(SearchBoxScroll.style.height) + 1) + 'px';
	SearchBox.style.display = 'block';
}

function SBSelect(No) {
	var SBInfo = document.getElementById('SB_A_0');
	var ListItemInfo = document.getElementById('SB_A_' + No);
	var ListItemStatus;
	if (sSearchBoxType == 'RADIO') {
		for (var i=1; i<=parseInt(SBInfo.getAttribute('count')); i++) {
			if ( i!= No) {
				ListItemStatus = document.getElementById('SB_I_' + i);
				ListItemStatus.src = '/~global/images/buttons/list_' + sSearchBoxType + '_inactive.gif';
			}
		}
	}
	ListItemStatus = document.getElementById('SB_I_' + No);
	if (ListItemStatus.src.indexOf('inactive') == -1) {
		ListItemStatus.src = '/~global/images/buttons/list_' + sSearchBoxType + '_inactive.gif';
	} else {
		ListItemStatus.src = '/~global/images/buttons/list_' + sSearchBoxType + '_active.gif';
	}
}

function SBOver(No) {
	var ListItem = document.getElementById('SB_' + No);
	var ListItemInfo = document.getElementById('SB_A_' + No);
	var SearchBoxScroll = document.getElementById('SearchBoxScroll');
	ListItem.style.backgroundColor = '#FF9933'
	if (ListItemInfo.getAttribute('count') == '0') {
		ListItem.style.color = '#FFFFFF';
		var InfoBox = document.getElementById('SearchBoxNoSelectInfo');
		var InfoText = document.getElementById('SearchBoxNoSelectText');
		var SearchBox = document.getElementById('SearchBox');
		var ListText = document.getElementById('SB_T_' + No).innerHTML
		if (GetStyle(InfoBox,'display') == 'none') {
			if (GetPosition(ListItem)[0] - parseInt(GetStyle(InfoBox,'width')) - 5 < 0) {
				InfoBox.style.left = GetPosition(ListItem)[0] + parseInt(GetStyle(SearchBoxScroll,'width')) + 'px';
			} else {
				InfoBox.style.left = GetPosition(ListItem)[0] - parseInt(GetStyle(InfoBox,'width')) - 5 + 'px';
			}
			InfoBox.style.top = GetPosition(ListItem)[1] - SearchBoxScroll.scrollTop + 'px';
			if (document.getElementById('filter').style.top) {
				InfoBox.style.top = parseInt(InfoBox.style.top) - parseInt(document.getElementById('filter').style.top) + 'px'
			}
			InfoText.innerHTML = InfoBoxText(ListText);
			InfoBox.style.display = 'block';
		}
	}
}

function SBOut(No) {
	ListItem = document.getElementById('SB_' + No)
	ListItem.style.backgroundColor = '#FFFFFF'
	var ListItemInfo = document.getElementById('SB_A_' + No);
	if (ListItemInfo.getAttribute('count') == '0') {
		ListItem.style.color = '#C0C0C0';
		var InfoBox = document.getElementById('SearchBoxNoSelectInfo');
		InfoBox.style.display = 'none';
	}
}

function SBClick(iButton) {
	var SBInfo = document.getElementById('SB_A_0');
	var ListItemStatus;
	var ListItemInfo;
	var i;
	switch (iButton) {
		// mark all items (not in user)
		case 1:
			for (i=1; i<=parseInt(SBInfo.getAttribute('count')); i++) {
				ListItemStatus = document.getElementById('SB_I_' + i);
				ListItemInfo = document.getElementById('SB_A_' + i);
				if (ListItemInfo.getAttribute('count') != '0') {
					ListItemStatus.src = '/~global/images/buttons/list_' + sSearchBoxType + '_active.gif';
				}
			}
		break;
		// clear all items (not in use)
		case 2:
			for (i=1; i<=parseInt(SBInfo.getAttribute('count')); i++) {
				ListItemStatus = document.getElementById('SB_I_' + i);
				ListItemStatus.src = '/~global/images/buttons/list_' + sSearchBoxType + '_inactive.gif';
			}
		break;
		// save selection
		case 3:
			var SearchBox = document.getElementById('SearchBox');
			var SelectCriteria = SBInfo.getAttribute('type');
			var SelectString = ''; var OldSelectString = '';
			var bRemoveLast = false;
			for (i=1; i<=parseInt(SBInfo.getAttribute('count')); i++) {
				ListItemStatus = document.getElementById('SB_I_' + i);
				if (ListItemStatus.src.indexOf('inactive') == -1) {
					ListItemInfo = document.getElementById('SB_A_' + i);
					SelectString += SelectCriteria + '_' + ListItemInfo.getAttribute('VAL') + '|'
					if (parseInt(ListItemInfo.getAttribute('count')) == 0) bRemoveLast = true;
				}
			}
			if (bRemoveLast == true) QS.set('select',RemoveSegment(QS.get('Select'),'last'))
			QS.set('select',RemoveSegment(QS.get('Select'),SelectCriteria) + SelectString)
			ShowResults(QS.build());
		break;
	}
}

function SetRadioOption(sCode,sOption) {
	var SelectString = ''; var OldSelectString = '';
	if (sOption != '') SelectString = sCode + '_' + sOption + '|';
	QS.set('select',RemoveSegment(QS.get('Select'),sCode)+SelectString)
	ShowResults(QS.build())
}

function SBClose(sCode) {
	if (sCode == 'act') sCode = sSearchBoxOpen;
	if (sCode != '') {
		var SearchBox 	= document.getElementById('SearchBox');
		var ListBoxIcon = document.getElementById('ListSymbol_'+sCode);
		var ListText = document.getElementById('ListText_' +sCode);
		ListBoxIcon.src = '/~global/images/buttons/list_open_up.gif';
		if (ListText.innerHTML.indexOf('Gehe&nbsp;zu:') != -1) ListText.innerHTML = '';
		SearchBox.style.display = 'none';
		sSearchBoxOpen = '';
	}
}

function CheckKeyPress(E){
	if (E) {
		if (E.charCode == 0 && sSearchBoxOpen != '') SBClose(sSearchBoxOpen);
	} else {
		if (event.keyCode == 27 && sSearchBoxOpen != '') SBClose(sSearchBoxOpen);
	}
}

function CheckClick(E) {
	if (E) {
		if (E.target.id.indexOf('List') == -1 && E.target.id.indexOf('SB') == -1) SBClose(sSearchBoxOpen);
	} else {
		if (event.srcElement.id.indexOf('List') == -1 && event.srcElement.id.indexOf('SB') == -1) SBClose(sSearchBoxOpen);
	}
}

function InfoBoxText(sItemText) {
	var sDenyItem = '', sDenyType = '', sItemType = sSearchBoxOpen;
	var sRemoveTextItem = '', sRemoveText = '';
	if (QS.get('Select')) {
		var aSegments = RemoveSegment(QS.get('Select'),sItemType).split('|');
		sDenyType = aSegments[aSegments.length-2].substr(0,2)
		switch (sDenyType) {
			case 'LA':
				sRemoveTextItem = 'Ihr gewähltes <b>Land</b>';
			break;
			case 'ST':
				sRemoveTextItem = 'Ihre gewählte <b>Stadt</b>';
			break;
			case 'DE':
				sRemoveTextItem = 'Ihr gewählter <b>Abreisetermin</b>';
			break;
			case 'AB':
				sRemoveTextItem = 'Ihr gewählter <b>Abflughafen</b>';
			break;
			case 'VE':
				sRemoveTextItem = 'Ihre gewählte <b>Verpflegung</b>';
			break;
			case 'KA':
				sRemoveTextItem = 'Ihre gewählte <b>Kategorie</b>';
			break;
			case 'HO':
				sRemoveTextItem = 'Ihre gewähltes <b>Hotel</b>';
			break;
			case 'PR':
				sRemoveTextItem = 'Ihr gewählter <b>Preis</b>';
			break;
			case 'KI':
				sRemoveTextItem = 'Ihre <b>Familienauswahl</b>';
			break;
			case 'AU':
				sRemoveTextItem = 'Ihre gewählte <b>Aufenthaltsdauer</b>';
			break;
			case 'EZ':
				sRemoveTextItem = '<b>EZ-Zuschlag GRATIS</b>';
			break;
			case 'HR':
				sRemoveText			= 'Wenn Sie <b>' + sItemText + '</b> jetzt wählen, werden Rouletteangebote wieder angezeigt.'
			break;
		}
		if (sRemoveText.length == 0) {
			sRemoveText = 'Wenn Sie <b>' + sItemText + '</b> jetzt wählen, wird ' + sRemoveTextItem + ' wieder entfernt.'
		}
	}
	return sRemoveText;
}

function RemoveDeparture() {
	QS.set('select',RemoveSegment(QS.get('select'),'DE'));
	ShowResults(QS.build())
}

function ShowResults(NewQS) {
	switch (QS.get('pagetype')) {
		case 'search':
			location.href = '/?' + NewQS;
		break;
		case 'telefon':
			location.href = '/buchen/telefon_select.asp?' + NewQS;
		break;
	}
}

function OpenCalender() {
	var iPosLeft = 100; var iPosTop = 200;
	if (window.screen.width <= 1100) iPosLeft = 50;
	window.open('/dialogs/kalender.asp?' + QS.build(), 'Kalender','toolbar=no,location=0,directories=no,status=no,menubar=0,scrollbars=no,resizable=no,width=460,height=360,left=' + iPosLeft + ',top=' + iPosTop)
}

