function changeRooms() {
	num = $('booking_rooms').value;

	for (i = 1; i <= 10; i++) {
		tr = $('room_' + i);
		if (i <= num) {
			tr.style.display = '';
		}
		else {
			tr.style.display = 'none';
		}
	}
}

function searchSelectToday(y, m, d) {
	if ($('search_option_today_true').checked) {
		$('search_option_year').value = y;
		$('search_option_month').value = m;
		$('search_option_day').value = d;
		$('search_option_clear_all_true').checked = false;
	}
}

function searchClearDate() {
	if ($('search_option_clear_all_true').checked) {
		$('search_option_year').value = '';
		$('search_option_month').value = '';
		$('search_option_day').value = '';
		$('search_option_today_true').checked = false;
	}
}

function searchUncheck() {
	$('search_option_today_true').checked = false;
	$('search_option_clear_all_true').checked = false;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function insertAddress(opt) {
	if (!opt) opt = {};

	$('booking_prefecture_select').hide();
	$('booking_city_select').hide();
	$('booking_area_select').hide();
	
	var zip1 = $F('booking_zip1');
	var zip2 = $F('booking_zip2');

	var url = opt.url ? opt.url : '/zipcode';

	if (zip1 && zip2) {
		new Ajax.Request(url, {
			method : 'get',
			onSuccess : _insertAddress,
			parameters : { 'zip1' : zip1, 'zip2' : zip2 },
			evalJSON : true
		});
	}
}

function _insertAddress(res) {
	var json = res.responseJSON;

	if (json.count == 1) {
		r = json.results[0];

		$('booking_prefecture').value = r.prefecture;
		$('booking_city').value = r.city;
		$('booking_area').value = r.area;
	}
	else if (json.count > 1) {
		var prefectures = new Array();
		var cities = new Array();
		var areas = new Array();

		json.results.each(function(r) {
			prefectures.push(r.prefecture);
			cities.push(r.city);
			areas.push(r.area);
		});

		_insertAddresses(prefectures, 'prefecture');
		_insertAddresses(cities, 'city');
		_insertAddresses(areas, 'area');
	}
}

function _insertAddresses(arr, name) {
	arr = arr.uniq();

	if (arr.size() == 1) {
		$('booking_' + name).value = arr.first();
	}
	else if (arr.size() > 1) {
		$('booking_' + name).value = '';

		ul = new Element('ul', {
			id : 'ul_' + name
		});
		ul.setStyle({
			margin: 0,
			padding: '.5em',
			listStyleType: 'none'
		});

		arr.each(function(r, i) {
			li = new Element('li', {
				id : 'li_' + name + '_' + i
			});
			li.setStyle({
				margin: 0,
				padding: 0
			});

			a = new Element('a', {
				href : 'javascript:void(0);'
			});
			a.observe('click', function() {
					$('booking_' + name).value = r;
					$('booking_' + name + '_select').hide();
			});
			a.update(r);

			li.insert(a);

			ul.insert(li);
		});

		$('booking_' + name + '_select').update(ul);
		$('booking_' + name + '_select').show();
	}
}

function hideSelection(name) {
	$('booking_' + name + '_select').hide();
}
