function enableControls() {
	enable('ibeType', 'countryList', 'regionList', 'destinationList', 'dateIntervalList', 'submit-button');
}

function disableControls() {
	disable('ibeType', 'countryList', 'regionList', 'destinationList', 'dateIntervalList', 'submit-button');
}

function dateListChanged(select) {
	if (select.options[select.selectedIndex].value == "EXACT") {
		disableControls();
		var countryId = dwr.util.getValue("country") == "ALL" ? null : dwr.util.getValue("country");
		var regionId = dwr.util.getValue("region") == "ALL" ? null : dwr.util.getValue("region");
		var ibeConfigId = dwr.util.getValue("ibeType");
		AjaxSearchUtil.getStartDates(countryId, regionId, ibeConfigId, getSearchDates);
	} else {
		jQuery('#calendarRow').hide();
		jQuery('#searchDateDiv').hide();
	}
}

function LZ(x) { return (x < 0 || x > 9 ? "" : "0") + x; }

function parseDate(ds) {
	ds = ds.replace(/[^0-9]/g, '');
	var d = new Date(ds.substr(0, 4), parseInt(ds.substr(4, 2), 10) - 1, ds.substr(6, 2));
	return d; 
}

function showCalendar() {
	jQuery('#calendarRow:hidden').slideDown('slow');
}

function dateChanged(date) {
	dateString = date;
	dwr.util.setValue('searchDate', date);
	jQuery('#calendarRow').slideUp('slow');
}

var selectableDates;
function getSearchDates(dates) {
	selectableDates = dates;
	if (jQuery('#calendarRow').datepicker) {
		jQuery('#calendarRow').datepicker("destroy");
	}
	
	var max;
	if (selectableDates.length == 0) {
		selectableDates[0] = '' + new Date().getFullYear() + LZ(new Date().getMonth() + 1) + LZ(new Date().getDate());
		max='2y';
	} else {
		max = parseDate(selectableDates[selectableDates.length-1]);
	} 
	
	var min = parseDate(selectableDates[0]);
	
	jQuery('#calendarRow').datepicker({ 
			firstDay: 1, 
			changeFirstDay: false,
			minDate: min, 
			maxDate: max,
			hideIfNoPrevNext: true,
			beforeShowDay: checkSelectableDates,
			mandatory: true,
			dateFormat: 'yy.mm.dd',
			defaultDate: dateString == '' ? min : parseDate(dateString),
			onSelect: dateChanged
		});
	jQuery('#calendarRow').datepicker(jQuery.extend({}, jQuery.datepicker.regional['hu']));

	if (dateString != '') {
		var ds = dateString.replace(/[^0-9]/g, '');
		for (i = 0; i < selectableDates.length; i++) { 
			if (ds == selectableDates[i]) {
				jQuery('#searchDateDiv').show();
				enableControls();
				return;
			} 
		}
		dateString = selectableDates[0].substr(0, 4) + "."
				+ selectableDates[0].substr(4, 2) + "." + selectableDates[0].substr(6, 2);
		dwr.util.setValue('searchDate', dateString);
		jQuery('#calendarRow').datepicker("setDate", min);
	}

	jQuery('#searchDateDiv').show();
	
	enableControls();
}

function checkSelectableDates(date) {
	for (i = 0; i < selectableDates.length; i++) { 
		if ('' + date.getFullYear() + LZ(date.getMonth() + 1) + LZ(date.getDate()) == selectableDates[i]) { 
			return [true, '']; 
		} 
	} 
	return [false, '']; 
}

// TODO refactor the multiple AJAX calls into a single one 
function ibeTypeChanged(radioButton) {
	disableControls();
	var selectedCountry = resetList("countryList", countryListDefaultOptions);
	
	var language = dwr.util.getValue("language");
	var ibeConfigId = radioButton.value;

	//TODO:remove-me???
	AjaxSearchUtil.getAgentHomeUrl(ibeConfigId, friendlyUrl, function(homeUrl) {
		if (homeUrl != null && homeUrl != '') {
			dwr.util.byId('searchForm').action = homeUrl;
		}
	});
	
	//TODO:1
	AjaxSearchUtil.getIbeCountryList(language, ibeConfigId, function(countryList) {
		dwr.util.addOptions("countryList", countryList, "countryId", "name");
		dwr.util.setValue('countryList', selectedCountry);
		countryListChanged();
	});
	
	//TODO:1
	AjaxSearchUtil.getIbeSearchAttributes(agentId, ibeConfigId, friendlyUrl, function(attributes) {
		dwr.util.removeAllRows('searchAttributesTable',
			{ filter: function(tr) { return tr.id != 'attributeRow'; }});
		if (attributes.length > 0) {
			for (var i = 0; i < attributes.length; i++) { 
				var id = attributes[i].searchKey;
				var clone = dwr.util.cloneNode('attributeRow', { idSuffix:id });
				clone.style.display = '';
	
				dwr.util.setValue('attibuteKey' + id, id);
				dwr.util.setValue('attributeName' + id, attributes[i].name);
			}
			show('searchAttributes');
		} else {
			hide('searchAttributes');
		}
	});
	
	//TODO: 1 : Affiliate miatt? egyesíthető?
}

function countryListChanged() {
	var selectedRegion = resetList("regionList", regionListDefaultOptions);

	dateString = '';
	dwr.util.setValue('searchDate', '');
	
	var countryId = dwr.util.getValue("country");
	if (countryId != "ALL") {
		disableControls();
		var language = dwr.util.getValue("language");
		var ibeConfigId = dwr.util.getValue("ibeType");
	
		AjaxSearchUtil.getRegionsForCountry(countryId, language, ibeConfigId, function(regionList) {
			dwr.util.addOptions("regionList", regionList, "regionId", "name");
			dwr.util.setValue('regionList', selectedRegion);
			// deferred invocation
			regionListChanged();
		});
	} else {
		// immediate invocation
		regionListChanged();
	}
}

function regionListChanged() {
	var currentDateInterval = resetList("dateIntervalList", dateIntervalListDefaultOptions);
	var selectedDestination = resetList("destinationList", destinationListDefaultOptions);
	disableControls();
	
	var countryId = dwr.util.getValue("country") == "ALL" ? null : dwr.util.getValue("country");
	var regionId = dwr.util.getValue("region") == "ALL" ? null : dwr.util.getValue("region");
	var language = dwr.util.getValue("language");
	var ibeConfigId = dwr.util.getValue("ibeType");

	if (destinationComboEnabled && regionId != null) {
		AjaxSearchUtil.getDestinationsForRegion(regionId, language, ibeConfigId,
			function(destinationList) {
				dwr.util.addOptions("destinationList", destinationList, "destinationId", "name");
				dwr.util.setValue("destinationList", selectedDestination);
			}
		);
	}
	
	AjaxSearchUtil.getIntervalsForRegion(countryId, regionId, language, ibeConfigId,
		function(intervalList) {
			dwr.util.addOptions("dateIntervalList", intervalList, "key", "value");
			dwr.util.setValue("dateIntervalList", currentDateInterval);
			dateListChanged(dwr.util.byId('dateIntervalList'));
			enableControls();
		}
	);
}

function validateForm(form) {
	if (dwr.util.getValue('dateIntervalList') == 'EXACT' && dwr.util.getValue('searchDate') == '') {
		alert(msgSelectDate);
		return false;
	}
	
	if (s && s.channel) {
		dwr.util.setValue('channel', s.channel);
	}
	
	// forget the filters set by the user for a new search
	form.categoryFilter.value = 'ALL';
	form.boardFilter.value = 'ALL';

	return true;
}

function resetInputHelperAttributes() {
	var div = document.getElementById('inputHelpersAccordion');
	var inputs = div.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++) {
		var name = inputs.item(i).getAttribute('name');
		dwr.util.setValue(name, null);
	}
	var selects = div.getElementsByTagName('select');
	for (var i = 0; i < selects.length; i++) {
		var name = selects.item(i).getAttribute('name');
		dwr.util.setValue(name, '-');
	}
	hide('inputHelperText');
}
