/*********************************************************************
	都市選択リスト（Ajax版）

**********************************************************************/
$(function(){
	//*****DetailCondition Open
	$("#DetailConditionTitle").click(function(){
		$("#DetailCondition").toggleClass("close");
	});

	//*****arrival select change
	$(".ajax_area,.ajax_country").change(function(){
		ar = this.id.split("@");
		if (ar.length == 1) {
			row_pos = this.id;
		} else if (ar.length == 2) {
			row_pos = ar[1];
		} else {
			return false;
		}

		//arrival ajax
		if ($(this).filter(".ajax_area").length > 0) {
			path = "countries";
			target_class = ".ajax_country";
			target_arrivals_class = ".ajax_arrival";
		} else {
			path = "arrivals";
			target_class = ".ajax_arrival";
		}
		var target_obj = $(target_class+"[id$='"+row_pos+"']")

		var target_arrival_obj = $(target_arrivals_class+"[id$='"+row_pos+"']")

		if ($(this).val() != "") {
			//Ajax通信
			$.ajax({
				type: "GET",
				url: base_url+"/"+path+"/selectlist/"+$(this).val(),
				dataType: "json",
				cache: false,
				timeout: 5000,
				async: false,
				success: function(data, dataType) {
					var recs = eval(data);
					target_obj.empty().append('<option value="">－ご選択ください－</option>');
					for (code in recs) {
						target_obj.append('<option value="'+code+'">'+recs[code]+'</option>');
					}
					
					if (path == "countries") {
						target_arrival_obj.empty().append('<option value="">－ご選択ください－</option>');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					if (textStatus == "timeout") {
						mess = "AJAX通信タイムアウトエラーが発生しました";
					} else {
						mess = "AJAX通信エラーが発生しました(Error Code："+textStatus+")";
					}
					alert(mess);
				}
			});
		} else {
			target_obj.empty();
			target_obj.append('<option value="">－ご選択ください－</option>');
			if (path == "countries") {
				target_arrival_obj.empty().append('<option value="">－ご選択ください－</option>');
			}
		}
	});
	
	//***** Submit Event
	$("#TourIndexForm").submit(function(){
		//required check
		if ($("#TourDepartureDepartureCode").val() == "") {
			alert("出発地を選択してください。");
			return false;
		}
		if($("select[id^='ArrivalAreaCode'] option:selected[value!='']").length == 0) {
			alert("目的地を選択してください。");
			return false;
		}
		//cnt = $("select[id^='TourCalenderDepartureDate'] option:selected[value!='']").length;
		//if(cnt == 0) {
		//	if ($("#TourCalenderDepartureDateYear").val() == "" && $("#TourCalenderDepartureDateMonth").val() == "" && $("#TourCalenderDepartureDateDay").val() == "") {
		//		alert("出発日は必ず選択してください。");
		//		return false;
		//	}
		//} else if (cnt == 1) {
		//	if ($("#TourCalenderDepartureDateYear").val() == "") {
		//		alert("出発日の指定方法が違います。「年」「年月」の指定は可能です。");
		//		return false;
		//	}
		//} else if (cnt == 2) {
		//	if ($("#TourCalenderDepartureDateDay").val() != "") {
		//		alert("出発日の指定方法が違います。「年」「年月」の指定は可能です。");
		//		return false;
		//	}
		//}
	});

	
	//***** Loading Event
	$(".ajax_area").each(function(){
		$(this).change();
	});

	$(".ajax_country").each(function(){
		$(this).val(country[this.id]);
		$(this).change();
	});

	$(".ajax_arrival").each(function(){
		$(this).val(arrival[this.id]);
	});
});

