jQuery(function($){

	$('#location_city').val('');
	$('#form_location').find('input[name=completion]').first().attr('checked', 'checked');
	$('#form_location').find('input[name=genre]').first().attr('checked', 'checked');
	$('#form_location').find('label.only_land_biz').hide();

	$('#nearby_station').val('');
	$('#form_station').find('input[name=completion]').first().attr('checked', 'checked');
	$('#form_station').find('input[name=genre]').first().attr('checked', 'checked');
	$('#form_station').find('label.only_land_biz').hide();

	$('#location_root').change(function(){
		$('#location_city').find('option').remove();
		if ($(this).val() != "") {
			var city = '#city_' + $(this).val();
			$(city).find('option').clone().appendTo('#location_city');
		} else {
			$('#city_default').find('option').clone().appendTo('#location_city');
		}
		$('#location_city').val('');
	});
	$('#route_root').change(function(){
		$('#nearby_station').find('option').remove();
		if ($(this).val() != "") {
			var station_ = '#station_' + $(this).val();
			$(station_).find('option').clone().appendTo('#nearby_station');
		} else {
			$('#station_default').find('option').clone().appendTo('#nearby_station');
		}
		$('#nearby_station').val('');
	});
	$('#form_location').find('input[name=genre]').click(function(){
		var completion = $('#form_location').find('input[name=completion]:checked').val();
		if ($(this).val() != 'house' && $(this).val() != 'cond') {
			$('#form_location').find('label.only_house_cond').hide();
			$('#form_location').find('label.only_land_biz').show();
			if (completion == 'new' || completion == 'used') {
				$('#form_location').find('input[name=completion]:checked').attr('checked', '');
				$('#form_location').find('input[name=completion][value=sale]').attr('checked', 'checked');
			}
		} else {
			$('#form_location').find('label.only_land_biz').hide();
			$('#form_location').find('label.only_house_cond').show();
			if (completion == 'sale') {
				$('#form_location').find('input[name=completion]:checked').attr('checked', '');
				$('#form_location').find('input[name=completion][value=new]').attr('checked', 'checked');
			}
		}
	});
	$('#form_station').find('input[name=genre]').click(function(){
		var completion = $('#form_station').find('input[name=completion]:checked').val();
		if ($(this).val() != 'house' && $(this).val() != 'cond') {
			$('#form_station').find('label.only_house_cond').hide();
			$('#form_station').find('label.only_land_biz').show();
			if (completion == 'new' || completion == 'used') {
				$('#form_station').find('input[name=completion]:checked').attr('checked', '');
				$('#form_station').find('input[name=completion][value=sale]').attr('checked', 'checked');
			}
		} else {
			$('#form_station').find('label.only_land_biz').hide();
			$('#form_station').find('label.only_house_cond').show();
			if (completion == 'sale') {
				$('#form_station').find('input[name=completion]:checked').attr('checked', '');
				$('#form_station').find('input[name=completion][value=new]').attr('checked', 'checked');
			}
		}
	});
	$('#form_location').submit(function(){
		var location_root = $('#location_root').val();
		var location_city = $('#location_city').val();
		var genre         = $('#form_location').find('input[name=genre]:checked').val();
		var completion    = $('#form_location').find('input[name=completion]:checked').val();
		if (location_root != "" && location_city == "") {
			alert('エリアを選択してください');
			return false;
		}
		if (genre == "") {
			alert('物件の種類を選択してください');
			return false;
		}
		if (completion == "") {
			alert('売買・賃貸を選択してください');
			return false;
		}

		var path = '';
		if (completion == 'rent') {
			path = 'for_rent.php?genre=' + genre;
		} else {
			path = 'for_sale.php?genre=' + genre + '&completion=' + completion;
		}
		path += (location_city != '' ? '&location_city=' + location_city : '');
		$(this).attr('action', path);
		return true;
	});
	$('#form_station').submit(function(){
		var route_root     = $('#route_root').val();
		var nearby_station = $('#nearby_station').val();
		var genre          = $('#form_station').find('input[name=genre]:checked').val();
		var completion     = $('#form_station').find('input[name=completion]:checked').val();
		if (route_root != "" && nearby_station == "") {
			alert('路線・最寄駅を選択してください');
			return false;
		}
		if (genre == "") {
			alert('物件の種類を選択してください');
			return false;
		}
		if (completion == "") {
			alert('売買・賃貸を選択してください');
			return false;
		}

		var path = '';
		if (completion == 'rent') {
			path = 'for_rent.php?genre=' + genre;
		} else {
			path = 'for_sale.php?genre=' + genre + '&completion=' + completion;
		}
		path += (nearby_station != '' ? '&nearby_station=' + nearby_station : '');
		$(this).attr('action', path);
		return true;
	});
});


