// 部屋数変更処理
function changeLodgers(ev) {
	// 既存の部屋要素配列を取得
	val = jQuery(this).val();
	lodgers = jQuery('[class="lodgers"]').map(function(obj) {
		return jQuery(this);
	});
	num = lodgers.length;

	if ((num - val) <= 0) {
		// 増
		for (i = 0; i < val; i++) {
			if (lodgers[i] == null) {
				ele = ev.data[0].clone(true);
				jQuery('[class="lodgers"]:last').after(ele);
			} else {
				ele = lodgers[i];
			}
			ele.find('[class="num_adults"]').attr('name', 'num_adults' + (i + 1));
			ele.find('[class="num_children"]').attr('name', 'num_children' + (i + 1));
			ele.find('[class="label"]').text('部屋' + (i+1));
		}
	} else {
		// 減
		for (i = num-1; i >= val; i--) {
				lodgers[i].remove();
		}
	}
}



// 子供人数変更処理
function changeChildren(ev) {
	val = jQuery(this).val();
	ages = jQuery(this).parent().find('[class="ages"]').map(function(obj) {
		return jQuery(this);
	});

	num = ages.length;

	if ((num - val) <= 0) {
		for (i = 0; i < val; i++) {
			if (ages[i] == null) {
				ele = ages[0].clone(true);
				jQuery(this).parent().find('[class="age"]:last').after(ele);
			} else {
				ele = ages[i]
			}
			name = jQuery(this).parent().find('[class="num_children"]').attr('name');
			age = ele.find('[class="age"]');
			age.attr('name', 'age' + /*name.charAt(13) +*/ (i + 1));
			ele.find('label').text('Age' + (i+1));
			ele.css('display', 'block');
		}
	} else {
		for (i = num-1; i >= val; i--) {
			if (i > 0 ) {
				ages[i].remove();
			} else {
				ages[i].css('display', 'none');
				age = ages[i].find('[class="age"]').removeAttr('name');
			}
		}
	}
}



// 入力値チェック処理（簡易）
function validation1(ev) {
	error = validation();
	ret = true;

	// エラー表示
	if (error != '') {
		alert(error);
		ret = false;
	}

	return ret;
}



// 入力値チェック処理（詳細）
function validation2(ev) {
	combo_max = 9;	// 最大部屋人数
	total_max = 9;	// 最大合計人数
	error = validation();
	ret = true;

	total = 0;
	jQuery('[class="lodgers"]').each(function(idx) {
		combo = parseInt(jQuery(this).find('[name^="num_adults"]').val());
		combo += parseInt(jQuery(this).find('[name^="num_children"]').val());
		if (combo > combo_max) {
			error += '人数が' + combo_max + '人を超えています。\n';
		}
		total += combo;

		jQuery(this).find('[name^="age"]').each(function(idx2) {
			if (jQuery(this).val() == '') {
				error += 'Age' + (idx2+1) + 'を選択してください。\n';
				return;
			}
		});
	});

/*	if (total > total_max) {
		error += '合計人数が' + total_max + '人を超えています。\n';
	}
*/
	// エラー表示
	if (error != '') {
		if ($('wall')) hide_wait(); //add by ktc
		alert(error);
		ret = false;
	}

	return ret;
}



// 入力値チェック処理
function validation(ev) {
	error = '';

	// 連動メニュー優先
	if (jQuery('#d_city').val() != '') {
		jQuery('#destination_result').val(jQuery('#d_city').val());
	}

	if (jQuery('#destination_result').val() == '') {
		error += '都市を入力または選択してください。\n';
	}

	if ((jQuery('[name="num_rooms"]').val() > 1) && (parseInt(jQuery('[name="num_children"]').val(), 10))) {
		error += '子供連れの場合は部屋数を1室としてください。\n';
	}

	if ($('departure')) { //add by ktc
	val = jQuery('#departure').val();
	if (val == '') {
		error += 'チェックイン日を入力してください。\n';
	} else {
		reg = new RegExp('^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})$', 'gi');
		res = val.match(reg);
		if (!res) {
			error += 'チェックイン日はyyyy/mm/ddの形式で入力してください。\n';
		} else {
			// 到着日を設定
			date = new Date(val);
			date.setTime(date.getTime() + (parseInt(jQuery('[name="num_nights"]').val()) * 24 * 60 * 60 * 1000));
			d	= date.getDate();
			m	= date.getMonth() + 1;
			if (d < 10) { d = "0" + d };
			if (m < 10) { m = "0" + m };
//			jQuery('[name="checkout_date"]').val(d + '/' + m + '/' + date.getFullYear());
			jQuery('[name="checkout_date"]').val(date.getFullYear() + '/' + m + '/' + d);

/*			// 日付形式をdd/mm/yyyyに変換してチェックイン日を設定
			str = val.split('/');
			jQuery('[name="checkin_date"]').val(str[2] + '/' + str[1] + '/' + str[0]);
*/
			jQuery('[name="checkin_date"]').val(val);
		}
	}
	} //add by ktc

	return error;
}



