﻿	var noValue = '-99'
	var curOption = new Array();
	var isLoaded = new Array();
	var presetCity='';
	var isOnLoad = false ;
	var location_string_value = '' ;

	//----------------------------------------
	// pullDownMenu
	//----------------------------------------

	var countryMenu;
	var cityMenu;
	var stateMenu;
	var areaMenu;
	var locationMenu
	
	//-----------------------------------------
	//	areas[ID].countries
	//-----------------------------------------
	for(var p in countries){
		var ac = countries[p].areacd;
		if(ac==undefined){
			alert(countries[p].jp_name);
		}
		if(areas[ac]['countries'] == undefined){
			areas[ac]['countries'] = [];
		}
		areas[ac]['countries'].push({country_cd:p,jp_name:countries[p].jp_name});
	
	}
	

	//-----------------------------------------
	//	states[ID].cities
	//-----------------------------------------	
	for(var p in points){
		var city = points[p];
		var o = {jp_name:city.city_jpname,citycd:city.citycd};
		if(city.state_cd != ""){
			var sid = city.state_cd;
			if(states[sid]['cities'] == undefined){
				states[sid]['cities'] = [];
			}
			states[sid]['cities'].push(o);
		}
		var cp = city.country_cd;
		if(countries[cp]['cities'] == undefined){
			countries[cp]['cities'] = [];
		}
		countries[cp]['cities'].push(o);
	}
	
	//-----------------------------------------
	//
	
	function checkcity(){
	  var CTval = cityMenu.options[cityMenu.selectedIndex].value;
		if (CTval=='0' || CTval=='' || isNaN(CTval)){
			alert ('都市を選択してください');
			return false;
		}	
		if (document.search_form.inyy.value=='' || document.search_form.indd.value==''){
			alert ('チェックイン日を選択してください');
			return false;
		}			
		if (document.search_form.day_stay.value==''){
			alert ('宿泊日数を選択してください');
			return false;
		}						
		if (document.search_form.ROOM_TYPE.value==''){
			alert ('ルームタイプを選択してください');
			return false;
		}				        		
						return true;
//		document.getElementById("search_form").submit()
	}
	
	function handlePoint(ciid){
		var countrycd = points[ciid].country_cd;
		var areacd = points[ciid].areacd;
		var citycd = points[ciid].citycd;
		
		
		setSelected(areaMenu,areacd);
		setCountry(areacd);
		setSelected(countryMenu,countrycd);
		
		var sid = points[ciid].state_cd;
		
		if(sid == ""){
			emptyList(stateMenu);
			stateMenu.selectedIndex = 0;
			setCity(countrycd);
			setSelected(cityMenu,citycd);
			
		}else{
			setState(countrycd);
			setSelected(stateMenu,sid);
			setCityWithState(sid);
			setSelected(cityMenu,citycd);
		}
		

	}
	
	function setSelected(menu,code){
		var op = menu.options;
		for(var i=0;i<op.length;i++){
			if(op[i].value == code){
				op[i].selected = true;
				break;
			}
		}
	}


	function areaChanged(countrydata,statedata,citydata,country_select,state_select,city_select){
		try{
			var area_cd = areaMenu.options[areaMenu.selectedIndex].value;
			if(area_cd == "" || area_cd == "0"){
				countryMenu.options.length = 1;
				countryMenu.options[0].text = "エリアを選択してください";
			}else{
				// set country
				countryMenu.options[0].text = "国を選択してください";
				setCountry(area_cd, countrydata);
			}
			if(!countrydata && country_select){countryMenu.selectedIndex = country_select;}
			countryChanged(statedata,citydata,state_select,city_select);
			
		}catch(e){
			alert(e);
		}
	}
	
	function countryChanged(statedata,citydata,state_select,city_select){
		try{
			var countrycd = countryMenu.options[countryMenu.selectedIndex].value;
			if(countrycd == "" || countrycd == "0"){
				emptyList(stateMenu);
				cityMenu.options.length = 1;
				if(areaMenu.options[areaMenu.selectedIndex].value == ""){
					cityMenu.options[0].text = "エリアを選択してください";
				}else{
					cityMenu.options[0].text = "国を選択してください";
				}
				stateMenu.options[0].text = "";
			}else{
				cityMenu.options[0].text = "都市を選択してください";
				if(countries[countrycd].states == ""){
					emptyList(stateMenu);
					stateMenu.selectedIndex = 0;
					cityMenu.selectedIndex = 0;
					setCity(countrycd, citydata);
					stateMenu.options[0].text = "";
					if(!citydata && city_select){cityMenu.selectedIndex = city_select;}
				}else{
					stateMenu.selectedIndex = 0;
					cityMenu.selectedIndex = 0;
					stateMenu.options[0].text = "州で絞り込めます";
					setState(countrycd, statedata);
					if(!statedata && state_select){stateMenu.selectedIndex = state_select;}
					stateChanged(citydata,city_select);
				}
			}
		}catch(e){
			alert('#countryChanged: ' + e);
		}

		if(countrycd == noValue){
		  selectOption( countryMenu.name, curOption[countryMenu.name] )
		} else {
		  curOption[countryMenu.name] = countrycd;
		  

			if (isOnLoad != true){
			   emptyList(locationMenu);
			  locationMenu.options[0] = new Option('--ロケーション--', 0);
			}
		  
		  if (isOnLoad==true){
			presetCity='';
			isOnLoad=false ;
		} else {
			presetCity = noValue ;
		}
//		  window.status = 'Loading Options Selections...';
//		  jsrsExecute( '/common/selectcity.asp', cbFillcitylist, 'fetchServerName', countrycd );
		}

	}
	
	function stateChanged(citydata,city_select){
		var sid = stateMenu.options[stateMenu.selectedIndex].value;
			if(sid == ""){
				var countrycd = countryMenu.options[countryMenu.selectedIndex].value;
				setCity(countrycd, citydata);
			}else{
				setCityWithState(sid, citydata);
			}
			if(!citydata && city_select){cityMenu.selectedIndex = city_select;}
	}
	
	function setCity(country_cd, citydata){
		var cities = countries[country_cd].cities;
		emptyList(cityMenu);
		for(var i=0;i<cities.length;i++){
			cityMenu.options[i+1] = new Option(cities[i].jp_name,cities[i].citycd);
			if(citydata){
				if(citydata == cityMenu.options[i+1].value){
					cityMenu.selectedIndex = i + 1;
				}
			}
		}
		
	}	

	function setCityWithState(sid, citydata){
		if (states[sid] == null){
			setCity(countryMenu.options[countryMenu.selectedIndex].value);
		}else{
			emptyList(cityMenu);
			var ci = states[sid].cities;
			for(var i=0;i<ci.length;i++){
				cityMenu.options[i+1] = new Option(ci[i].jp_name,ci[i].citycd);
				if(citydata){
					if(citydata == cityMenu.options[i+1].value){
						cityMenu.selectedIndex = i + 1;
					}
				}
			}
		}
	}
		
	
	function setState(cid){
		emptyList(stateMenu);
		stateMenu.options[0] = new Option('指定なし',0);
		var st = countries[cid].states;
		for(var i=0;i<st.length;i++){
			var sid = st[i];
			stateMenu.options[i+1] = new Option(states[sid].jp_name,sid);
		}
		stateMenu.selectedIndex = 0;
	}
		
	function setCountry(area_cd, countrydata){
		var ci = areas[area_cd].countries;
		clearMenu(countryMenu);
		for(var i=0;i<ci.length;i++){
			countryMenu.options[i+1] = new Option(ci[i].jp_name,ci[i].country_cd);
			if(countrydata){
				if(Number(countrydata) == Number(countryMenu.options[i+1].value)){
					countryMenu.selectedIndex = i + 1;
				}
			}
		}
	}


	
	function clearMenu(menu){
		if(menu.options.length>1){
			for(var i=menu.options.length-1;i>0;i--){
				menu.options[i] = null;
			}
		}
	}
		

	
	function _onLoad(areadata,countrydata,statedata,citydata){
		
		areaMenu = document.getElementById("areaMenu");
		cityMenu = document.getElementById("CT");
		stateMenu = document.getElementById("stateMenu");
		countryMenu = document.getElementById("CO");
		locationMenu = document.getElementById("search_location");

		var area_select = areaMenu.selectedIndex;

		var country_select = countryMenu.selectedIndex;
		var state_select = stateMenu.selectedIndex;
		var city_select  = cityMenu.selectedIndex;
		//
		
		emptyList(areaMenu);
		
		var k = 1;
		try{
			for(var p in areas){
				areaMenu.options[k] = new Option(areas[p].jp_name,p);
				if(areadata){
					if(areadata == areaMenu.options[k].value){
						areaMenu.selectedIndex = k;
					}
				}
				k++;
			}
			if(areadata == 0){
				areaMenu.selectedIndex = area_select;
			}
			areaChanged(countrydata,statedata,citydata,country_select,state_select,city_select);
		}catch(e){
			alert('_onLoad: '+e);
		}

		isOnLoad=true ;

			var val = countryMenu.value + '|' + cityMenu.value ;
		    window.status = 'Loading Options Selections...';
		    jsrsPOST=false;
		    jsrsExecute( 'makelocation.asp', cbFillsearch_location, 'makelocationlist', val );


		
		
	}
	
	

function CT_onchange() {
	
  var COval = countryMenu.options[countryMenu.selectedIndex].value;
  var CTval = cityMenu.options[cityMenu.selectedIndex].value;

  if ((COval == noValue) && (CTval == noValue)) {
    selectOption( countryMenu.name, curOption[countryMenu.name] )
  } else {
		curOption[countryMenu.name] = COval;
		curOption[cityMenu.name] = CTval;
		emptyList(locationMenu);
		
		if (isOnLoad==true){
			presetCity='';
			
			isOnLoad=false ;
			
		} else {
		presetCity = noValue ;
		}
	
	var val = COval + '|' + CTval ;
    window.status = 'Loading Options Selections...';
    jsrsExecute( 'makelocation.asp', cbFillsearch_location, 'makelocationlist', val );
    
  }  

}

function search_location_onchange(){
}


function emptyList( listName ){
//  listName.options.length = 0;
	clearMenu(listName)
//  listName.onchange = null;
  isLoaded[listName] = false;
  curOption[listName] = noValue;
}

function cbFillcitylist ( strModels ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'CT',  strModels );
	selectOption( 'CT', presetCity );
}

function cbFillsearch_location ( strModels ){ 
  // callback for dependent listbox
  window.status = '';
  fillLocationList( 'search_location',  strModels );
	if (isOnLoad==true) {

	selectOption( 'search_location', location_string_value );
	}
}


function fillList( listName, strOptions ){
  // fill any list with options
  
  // always insert selection prompt
  var lst = document.getElementById(listName);
  emptyList( lst );
  lst.disabled = true;
  lst.options[0] = new Option('-- 都市別選択 --', noValue);
  
  // options in form "value~displaytext|value~displaytext|..."
  var aOptionPairs = strOptions.split('|');
  for( var i = 0; i < aOptionPairs.length; i++ ){
    if (aOptionPairs[i].indexOf('~') != -1) {
      var aOptions = aOptionPairs[i].split('~');
      lst.options[i + 1] = new Option(aOptions[1], aOptions[0]);
    }  
  }
  
  // init to no value
  selectOption( listName, noValue );
  lst.onchange = eval( listName + "_onchange" );
  isLoaded[listName] = true;
  lst.disabled = false;
}
function fillLocationList( listName, strOptions ){
  // fill any list with options
  
  // always insert selection prompt
  var lst = document.getElementById(listName);
  emptyList( lst );
  lst.disabled = true;
  lst.options[0] = new Option('全て表示', 0);
  
  // options in form "value~displaytext|value~displaytext|..."
  var aOptionPairs = strOptions.split('|');
  for( var i = 0; i < aOptionPairs.length; i++ ){
    if (aOptionPairs[i].indexOf('~') != -1) {
      var aOptions = aOptionPairs[i].split('~');
      lst.options[i + 1] = new Option(aOptions[1], aOptions[0]);
    }  
  }
  
  // init to no value
  selectOption( listName, noValue );
  lst.onchange = eval( listName + "_onchange" );
  isLoaded[listName] = true;
  lst.disabled = false;
}


function selectOption( listName, optionVal ){
  // set list selection to option based on value
  var lst = document.getElementById(listName);
  for( var i = 0; i< lst.options.length; i++ ){
    if( lst.options[i].value == optionVal ){
      lst.selectedIndex = i;
      curOption[listName] = optionVal;
      return;
    }  
  }
}

function displaychange(){
	var optr=document.getElementById("search-option");
	if (optr.style.display == "none"){
		optr.style.display="block";
	}else{
		optr.style.display="none";
	}
}

function change_stay_date(dCheck) {

var iYY = document.forms['search_form']['inyy'].value ;
var iMM = document.forms['search_form']['inmm'].value ;
var iDD = document.forms['search_form']['indd'].value ;

if (dCheck){
var noDate = new Date()
var neDate = new Date(iYY,iMM-1,iDD)

if (iMM < (noDate.getMonth()+1) && noDate > neDate){
iYY = Number(iYY) + 1;
document.forms['search_form']['inyy'].value=iYY ;
}}

var myDate = calcMD(iYY,iMM,iDD,document.forms['search_form']['day_stay'].value);

var oYY = myDate.getYear();
var oMM = myDate.getMonth();
var oDD = myDate.getDate();

document.forms['search_form']['outyy'].value = oYY ;
document.forms['search_form']['outmm'].value  = oMM+1;
document.forms['search_form']['outdd'].value = oDD;
}

function calcMD(y,m,d,n)
{
	//y = 2004;
	//m = 9;
	//d = 30;
	
	nmsec = n * 1000 * 60 * 60 * 24;	//　１日のミリ秒
	msec  = (new Date(""+y+"/"+m+"/"+d)).getTime();
	dt    = new Date(msec + nmsec );
	month = dt.getMonth() + 1;
	date  = dt.getDate();
	//alert(y+"年"+m+"月"+d+"日から"+n+"日後は"+month+"月"+date+"日です");
	
	return(dt);
}

//--- 2006/12/27 チェックイン日・予算　指定方法変更

function ChangeSelectionRG() {                       
         v = document.search_form.RANGE.selectedIndex;   
         document.search_form.RANGEfrom.value = 0;
         document.search_form.RANGEto.value = 999999;
         if (v > 0) {
         document.search_form.RANGEfrom.value = (v-1) * 5000 ;
         document.search_form.RANGEto.value = (v) * 5000;
         }       
}

function ChangeSelectionYM() {                       
         document.search_form.inyy.value = document.search_form.inYM.value.substr(0,4);
         document.search_form.inmm.value = document.search_form.inYM.value.substr(5,2);
         change_stay_date(false);
}          

function ChangeMORE_search() {
         if (MORE_search.style.display == 'none') {
         // 表示
         MORE_search.style.display = '';
         document.search_form.MORE_search_flg.value = '';
         OFF_IMG.style.display='none';
         ON_IMG.style.display='';
         }else{
         MORE_search.style.display = 'none';
         document.search_form.MORE_search_flg.value = 'none';
         OFF_IMG.style.display='';
         ON_IMG.style.display='none';
          }
}  

function setMORE_search() {
         MORE_search.style.display = document.search_form.MORE_search_flg.value;
         if (document.search_form.MORE_search_flg.value == '') {
         // 表示
         OFF_IMG.style.display='none';
         ON_IMG.style.display='';
         }else{
         OFF_IMG.style.display='';
         ON_IMG.style.display='none';
         }
}  
