function selectDay(days, years, date) {
	if (date) {
		var pieces = date.split("-");
	} else {
		return;
	}
	
	var d = pieces[2];
	var m = pieces[1];
	var y = pieces[0];
	
	for(i = 0; i < days.length; i++) {
		if(days[i].value == d || days[i].value == "0" + d) {
			days[i].selected = true;
			break;
		}
	}
	for(i = 0; i < years.length; i++) {
		if(years[i].value == y+'-'+m || years[i].value == y+'-0'+m) {
			years[i].selected = true;
			break;
		}
	}
	var nightsId = document.getElementById('nights');
	if (nightsId && d > 0 && y > 0) {
		nightsId.disabled = false;
		nightsId.options[0].text = 'geen voorkeur';
	}	
}

var lastmonth = '';
function updateDays(d, y) {

	// dag op 1e vd maand zetten indien niet gekozen
	if (d < 1) {
		d = 1;
	} 

	var nightsId = document.getElementById('nights');
	if (y.length >= 4) {
		if (lastmonth != y) {
			var url = "/call/daysInMonth.php?y=" + y + "&d=" + d;
			var req = YAHOO.util.Connect.asyncRequest('GET', url , { success: handleDayOptions, failure:null } );
		}
		
		if (d > 0) {
			if (nightsId) {
				nightsId.disabled = false;
				nightsId.options[0].text = 'geen voorkeur';
			}
		} else {
			if (nightsId) {
				nightsId.selectedIndex = 0;
				nightsId.disabled = true;
				nightsId.options[0].text = 'kies eerst een datum';
			}
		}
		lastmonth = y;
	} else {
		if (nightsId) {
			nightsId.selectedIndex = 0;
			nightsId.disabled = true;
			nightsId.options[0].text = 'kies eerst een datum';
		}
	}		
}

handleDayOptions = function (o) {			
	var dayoptions = document.getElementById("day");
	dayoptions.length = 0;
	if (o.responseXML != null) {
		var t = o.responseXML.firstChild;
		for(var i = 0; i < t.childNodes.length; i++){
			dayoptions.length++;
			dayoptions[i].text = t.childNodes[i].firstChild.nodeValue;
			dayoptions[i].label = t.childNodes[i].firstChild.nodeValue;
			dayoptions[i].value = t.childNodes[i].getAttribute('value')
			if (t.childNodes[i].getAttribute('selected') == 'selected') {
				dayoptions[i].selected = true;
			}
		}
	}
}

function focusCountry() {
	var countryId 	= $('#countryId').val();
	var orgtypeId 	= $('#orgtypeId').val();	
	var accotypeId 	= $('#accotypeId').val();
	
	getAreaList(countryId, orgtypeId, accotypeId);
	getCityList(countryId, 0, orgtypeId, accotypeId);
	getAccotypeList();

	if (document.getElementById('orgId')) {
		getOrganisationList();
	}
}
 
function focusArea() {
	var countryId 	= $('#countryId').val();
	var areaId 		= $('#areaId').val();
	var orgtypeId 	= $('#orgtypeId').val();
	var accotypeId 	= $('#accotypeId').val();

	if (areaId > 0 && document.getElementById('orgId')) {
		getOrganisationList();
	}

	getCityList(countryId, areaId, orgtypeId, accotypeId);
	getAccotypeList();
	
	var opt = $('#areaId option:selected').parent().attr('id');
}

function focusAccotype() {
	var countryId 	= $('#countryId').val();
	var orgtypeId 	= $('#orgtypeId').val();	
	var accotypeId 	= $('#accotypeId').val();
	
	getAreaList(countryId, orgtypeId, accotypeId);
	getCityList(countryId, 0, orgtypeId, accotypeId);
}

function getOrganisationList() {
	if ($('#orgId').attr('tagName') == 'SELECT') {
		var countryId 	= $('#countryId').val();
		var areaId 		= $('#areaId').val();
		var cityId 		= $('#cityId').val();
		var accotypeId 	= $('#accotypeId').val();
		
		if (accotypeId == undefined) 	accotypeId = 0;
		if (accotypeId == undefined) 	accotypeId = 0;
		if (accotypeId == undefined) 	accotypeId = 0;
		if (accotypeId == undefined) 	accotypeId = 0;		
		
		var url = "/call/organisationlist.php?countryId=" + countryId + "&areaId=" + areaId + "&cityId=" + cityId + "&accotypeId=" + accotypeId;
		var req = YAHOO.util.Connect.asyncRequest('GET', url , { success: handleOrganisationList, failure:null } );
	}
}

function handleOrganisationList(o) {
	if (o.responseText) {
		$('#orgId').html(o.responseText);
	}
}

function getCountryList() {
	if ($('#countryId').attr('tagName') == 'SELECT') {
		var orgtypeId 	= $('#orgtypeId').val();
		var accotypeId 	= $('#accotypeId').val();
		
		if (orgtypeId == undefined) 	orgtypeId = 0;
		if (accotypeId == undefined) 	accotypeId = 0;
		
		var url = "/call/countries.php?orgtypeId=" + orgtypeId + "&accotypeId=" + accotypeId;
		var req = YAHOO.util.Connect.asyncRequest('GET', url , { success: handleCountryList, failure:null } );
	}
}

function handleCountryList(o) {
	if (o.responseText) {
		$('#countryId').html(o.responseText);
	}
}

function getCityList(countryId, areaId, orgtypeId, accotypeId) {
	if ($('#cityId').attr('tagName') == 'SELECT') {

		if (countryId == undefined) 	countryId = 0;
		if (areaId == undefined) 		areaId = 0;		
		if (orgtypeId == undefined) 	orgtypeId = 0;		
		if (accotypeId == undefined) 	accotypeId = 0;
		
		var cityId = $('#cityId').val();
		
		var url = "/call/cities.php?countryId=" + countryId + "&areaId=" + areaId + "&orgtypeId=" + orgtypeId + "&accotypeId=" + accotypeId + "&cityId=" + cityId;
		var req = YAHOO.util.Connect.asyncRequest('GET', url , { success: handleCityList, failure:null } );
	}
}

function handleCityList(o) {
	if (o.responseText) {
		$('#cityId').html(o.responseText);
	}
}

function getAreaList(countryId, orgtypeId, accotypeId) {
	if ($('#areaId').attr('tagName') == 'SELECT') {
		var areaId 		= $('#areaId').val();
		
		if (countryId == undefined) 	countryId = 0;		
		if (accotypeId == undefined) 	accotypeId = 0;		
		if (orgtypeId == undefined) 	orgtypeId = 0;
		
		var url = "/call/areas.php?countryId=" + countryId + "&orgtypeId=" + orgtypeId + "&accotypeId=" + accotypeId + "&areaId=" + areaId;
		var req = YAHOO.util.Connect.asyncRequest('GET', url , { success: handleAreaList, failure:null } );
	}
}

function handleAreaList(o) {
	if (o.responseText) {
		$('#areaId').html(o.responseText);
	}
}

function getAccotypeList() {
	if ($('#accotypeId').attr('tagName') == 'SELECT') {
		var countryId 	= $('#countryId').val();
		var areaId 		= $('#areaId').val();
		var cityId 		= $('#cityId').val();
		var orgtypeId 	= $('#orgtypeId').val();
		var accotypeId 	= $('#accotypeId').val();
		
		if (countryId == undefined) 	countryId = 0;
		if (areaId == undefined) 		areaId = 0;
		if (cityId == undefined) 		cityId = 0;
		if (orgtypeId == undefined) 	orgtypeId = 0;
		if (accotypeId == undefined) 	accotypeId = 0;

		var url = "/call/accotype.php?countryId=" + countryId + "&areaId=" + areaId + "&cityId=" + cityId + "&orgtypeId=" + orgtypeId + "&accotypeId=" + accotypeId;
		var req = YAHOO.util.Connect.asyncRequest('GET', url , { success: handleAccotypeList, failure:null } );
	}
}

function handleAccotypeList(o) {
	if (o.responseText) {
		$('#accotypeId').html(o.responseText);
	}
}

function focusOrgtype(tab, id) {
	var otSelect = document.getElementById('orgtypeId');
	for(var i = 0; i < otSelect.options.length; i++) {
		if (otSelect.options[i].value == id) {
			otSelect.selectedIndex = i;
			break;
		}
	}
	
	$("#searchtabs li").removeClass('active');
	$("#tab_" + id).addClass('active');
}

function focusSelectOrgtype() {
	var id 			= $('#orgtypeId').val();
	var countryId 	= $('#countryId').val();
	var areaId 		= $('#areaId').val();
	var orgtypeId 	= $('#orgtypeId').val();
	var accotypeId 	= $('#accotypeId').val();

	$("#searchtabs li").removeClass('active');
	$("#tab_" + id).addClass('active');

	getCountryList();
	getAreaList(countryId, orgtypeId, accotypeId);
	getCityList(countryId, areaId, orgtypeId, accotypeId);
	getAccotypeList();

	if (orgtypeId == 1) {
		$('.bungalowfaciliteiten').show();
		$('.hotelfaciliteiten').hide();
		$('.hotelfaciliteiten select').each(function() { $(this).attr('selectedIndex', 0); });
	} else if (orgtypeId == 3) {
		$('.bungalowfaciliteiten').hide();
		$('.bungalowfaciliteiten select').each(function() { $(this).attr('selectedIndex', 0); });
		$('.hotelfaciliteiten').show();
	} else {
		$('.bungalowfaciliteiten').hide();
		$('.hotelfaciliteiten').hide();
		$('.hotelfaciliteiten select').each(function() { $(this).attr('selectedIndex', 0); });
		$('.bungalowfaciliteiten select').each(function() { $(this).attr('selectedIndex', 0); });
	}
}