$(document).ready(
	
	function(){
		$("#term").autocomplete("quicksearch.php", {
			width: 260,
			max: 25,
			minChars: 5,
			selectFirst: false
	
		});
		$('#term').click(function(){
			if($('#term').val() == "eg Tenerife..."){
				$('#term').val("");
			}
			/*document.getElementById('country').value = '0';
			document.getElementById('destination').value = '0';
			document.getElementById('resort').value = '0';	*/
			$('#country').val('');
			$('#destination').val('');
			$('#resort').val('');
		});
		$('#country').focus(function(){if($('#term').val() != "eg Tenerife..."){$('#term').val('')}});
		$('.cleartext').focus(clearText);
		$('#country').change(getRegions);
		$('#destination').change(getResorts);
		if($('#country').val() == '0'){
			document.getElementById('destination').disabled = true;
		}
		if($('#destination').val() == '0'){
			document.getElementById('resort').disabled = true;
		}
		$('.rthide').hide();
		$('#advancedtoggle').click(function(){$('div.advancedsearch').toggle('fast');return false;});
		$('#occupancytoggle').click(function(){$('div#occupants').show('fast');update_occupancy('occupants', 'extra');return false;});
		
	}
);

function clearText(){
	this.value = '';
}

function getRegions(){
	clearCombo('destination');
	clearCombo('resort');
	if($('#country').val() != '0'){
		document.getElementById('destination').disabled = false;
		document.getElementById('resort').disabled = true;
		$.get('/ajax/reg_callback.php', {countryID: $('#country').val()}, function(data){reloadRegions(data)}, 'xml');
	}
	else{
		document.getElementById('destination').disabled = true;
		document.getElementById('resort').disabled = true;
	}
}
function reloadRegions(data){
	//alert(data.getElementsByTagName('region').length);
	//this is total crap - there is no reason why IE shouldn't work using the jquery version
	if($.browser.msie){
		var regions = data.getElementsByTagName('region');
		for(var i = 0; i < regions.length; i++){
				document.getElementById('destination').options[document.getElementById('destination').length] = new Option(regions[i].childNodes[0].text, regions[i].getAttribute('id'));			
		}
	}
	else{
		$('region', data).each(
			function(){
				document.getElementById('destination').options[document.getElementById('destination').length] = new Option($(this).find('name').text(), $(this).attr('id'));
			}
		);
	}
}

function getResorts(){
	clearCombo('resort');
	if($('#destination').val() != '0'){
		document.getElementById('resort').disabled = false;
		$.get('/ajax/res_callback.php', {regionID: $('#destination').val()}, function(data){reloadResorts(data)}, 'xml');
	}
	else{
		document.getElementById('resort').disabled = true;
	}
}

function reloadResorts(data){
	//alert(data.responseText);
	if($.browser.msie){
		var resorts = data.getElementsByTagName('resort');
		for(var i = 0; i < resorts.length; i++){
				document.getElementById('resort').options[document.getElementById('resort').length] = new Option(resorts[i].childNodes[0].text, resorts[i].getAttribute('id'));			
		}
	}
	else{
		$('resort', data).each(
			function(){
				document.getElementById('resort').options[document.getElementById('resort').length] = new Option($(this).find('name').text(), $(this).attr('id'));
			}
		);
	}
}

function clearCombo(comboName){
	for(var i = document.getElementById(comboName).options.length; i > 0; i--){
		document.getElementById(comboName).remove(i);
	}
}