// デバッグフラグ　true = "Firebug Console に出力"
Debug = false;

function init_dom_free() {

	dom_free = new formControll(document.atourForm);

	dom_free.init = function() {
		
		/***************************************************************************
		// 各プルダウンの読み込み
		***************************************************************************/
		
		this.formView.createPulldown("autoAIRgo", domFreeApo_list.slice(1));	// 出発空港
		this.formView.createPulldown("autoAIRreturn", domFreeApo_list);			// 到着空港
		this.formView.createPulldown("autoDATE_month", month_list);				// 出発月
		this.formView.createPulldown("autoDATE_day", day_list.slice(0, getDaylength()));	// 出発日
		var ninzu_list = persons_list;
		this.formView.createPulldown("autoNINZU", ninzu_list);					// 人数
		this.formView.createPulldown("autoSTAY", stay_list);					// 泊数
		this.formView.createPulldown("autoNUMPFROM", ninzu_list);				// 1部屋利用人数 FROM
		this.formView.createPulldown("autoNUMPTO", ninzu_list);					// 1部屋利用人数 TO
		
		/***************************************************************************
		// 各エレメントの初期値のセット
		***************************************************************************/
		
		this.formView.setPulldown("autoAIRgo", ""); 	    					// 出発空港
		var autoAIR = "";									// 到着空港を指定する場合はこちらをセット
		this.formView.setPulldown("autoAIRreturn" , autoAIR);
		this.formView.enablePulldown("autoREGION", false);					// 宿泊地 地区
		this.formView.enablePulldown("autoAREA", false);					// 宿泊地 地域
		this.setOffsetDay("autoDATE_month", "autoDATE_day", 10);				// 出発日
		var ninzu = 2;
		this.formView.setPulldown("autoNINZU", ninzu);						// 人数
		this.formView.setPulldown("autoSTAY", 1);						// 泊数
		this.formView.setPulldown("autoNUMPFROM", 1);						// 1部屋利用人数 FROM
		this.formView.setPulldown("autoNUMPTO", ninzu);						// 1部屋利用人数 TO
		this.formView.setDynamicParam("autoMAJMAT", ""); 					// 方面
		
		// ページ毎に初期値の再設定
		if (typeof page_id != 'undefined'){
			if (page_id != ''){
				switch (page_id) {
					case 'link_share':
						this.formView.setPulldown("autoAIRgo", arrApo);											// 出発空港
						this.formView.setPulldown("autoAIRreturn" , depApo);								// 到着空港
//						this.formView.setPulldown("autoREGION", depRegion);								// 宿泊地 地区
//						this.formView.setPulldown("autoAREA", depArea);										// 宿泊地 地域
						this.formView.enablePulldown("autoREGION", false);									// 宿泊地 地区
						this.formView.enablePulldown("autoAREA", false);										// 宿泊地 地域
						this.formView.setPulldown("autoDATE_month", arrMonth);							// 出発日
						this.formView.setPulldown("autoDATE_day", arrDay);									// 出発日
						this.formView.setPulldown("autoSTAY", stay);												// 泊数
						this.formView.setPulldown("autoNINZU", man);												// 人数
						this.formView.setPulldown("autoNUMPFROM", minNum);									// 1部屋利用人数 FROM
						this.formView.setPulldown("autoNUMPTO", maxNum);										// 1部屋利用人数 TO
						document.getElementById("refid").value = refid;											// refid
						if (document.getElementById("coopsitebanner") != null) {
							document.getElementById("coopsitebanner").value = coopsitebanner;	// coopsitebanner
						}
						if (document.getElementById("themeid") != null) {
							document.getElementById("themeid").value = themeid;								// themeid
						}
						break;
				}
			}
		}
		
		/***************************************************************************
		// 各エレメントのイベントのセット
		***************************************************************************/
		
		// 二度フラグ
		//this.submited = false;
		
		
		// 到着空港プルダウン変更 => 地区プルダウン生成
		this.autoAIRreturn = function(evt, val, targetElem) {
			
			if(val == "" || val == "NOP") {
				
				if(val == "NOP") this.formView.setPulldown(evt, "");
				this.formView.clearPulldown(targetElem, false);
				this.relationListener(targetElem);
				return false;
			}
			
			// 空港地区紐付けデータを登録
			this.relationList = domRelation_obj[val];
			
			this.formView.createPulldown(targetElem, domRegion_list[this.relationList[0]].slice(1));
			this.formView.setPulldown(targetElem, this.relationList[1]); 
			this.formView.enablePulldown(targetElem, true);
			this.relationListener(targetElem);
		};
		

		// 宿泊地　地区プルダウン変更　=>　地域プルダウン生成
		this.autoREGION = function(evt, val, targetElem) {	
			
			if(val == "") {
				
				this.formView.clearPulldown(targetElem, false);
				return false;
			}
			
			var index = (this.relationList) ? (val == this.relationList[1] ? this.relationList[2]: 0) : 0;
			
			this.formView.createPulldown(targetElem, domArea_list[val]);
			this.formView.setPulldown(targetElem,index); 
			this.formView.enablePulldown(targetElem, true);
		};
		
		
		// 参加人数変更 => 1部屋あたりの利用人数に反映
		this.ninzuListener = function(eventObj) {	
						
			var evt = this.getEventTarget(eventObj);
			this.formView.setPulldown("autoNUMPTO", this.formView.getValue(evt));
		};
		
		
		// フォーム送信イベント
		this.submitTabisaku = function() {
			
			this.setyymmdd("autoDATE_month", "autoDATE_day", "autoDATE");
			if(this.relationList) this.formView.setDynamicParam("autoMAJMAT", this.relationList[0]);
		};

		
		// イベントの追加
		// 到着空港プルダウン変更 => 地区プルダウン生成
		this.addEvent("autoAIRreturn", "change", this.delegate(this, this.relationListener));
		// 宿泊地　地区プルダウン変更　=>　地域プルダウン生成
		this.addEvent("autoREGION", "change", this.delegate(this, this.relationListener));
		// 出発日：月変更 => その月の日数を反映
		this.addEvent("autoDATE_month", "change", this.delegate(this, this.monthListener));		
		// 参加人数変更 => 1部屋あたりの利用人数に反映
		this.addEvent("autoNINZU", "change", this.delegate(this, this.ninzuListener));
		// フォーム送信イベント
		this.addEvent(document.getElementById("BtnDomTourFr"), "click", this.delegate(this, this.submitTabisaku));
		// 宿泊地初期値設定
		if (typeof page_id != 'undefined'){
			if (page_id != ''){
				switch (page_id) {
					case 'link_share':
						this.autoAIRreturn("", depApo, document.atourForm.autoREGION);
						break;
					default:
						this.autoAIRreturn("", autoAIR, document.atourForm.autoREGION);
				}
			}
		}
	}
	
	dom_free.init();
}

// ページ読み込み後　処理を開始する
addLoadEvent(init_dom_free);

