﻿//
// Dynamic Function
// Author: ishikawa.ryuta@jp.fujitsu.com
//


function selectListFromValue(selObj,txtVal)
{
	for(i=0; i<selObj.length; i++)
	{
		if(selObj.options[i].value == txtVal)
		{
			selObj.selectedIndex = i;
			return true;
		}
	}
	
	selObj.selectedIndex = 0;
	return false;
}


///
/// 年のselectをセットする
/// param : idYear  年のselectのID
///         bOffset 年の開始オフセット （例：２年前からの場合、-2 を指定） 
///         aOffset 年の終了オフセット （例：２年先までの場合、2 を指定） 
///
function setYearList(idYear,bOffset,aOffset)
{
	//selectオブジェクトを取得
	var selYear = document.getElementById(idYear);

	//selectオブジェクトをクリア
	selYear.length = 0;

	//開始年と終了年を作成
	now = new Date();
	sYear = now.getFullYear() + bOffset;
	eYear = now.getFullYear() + aOffset;

	//最初の行の生成
	var opt = document.createElement("option");
	opt.text = "年";
	opt.value = "-1"
	opt.className = "default";
	try{
		selYear.add(opt,null);
	}catch(e){
		selYear.add(opt);
	}

	//年を生成
	for(y=sYear; y<=eYear; y++)
	{
		opt = document.createElement("option");
		opt.text = y + "年";
		opt.value = y;
		try{
			selYear.add(opt,null);
		}catch(e){
			selYear.add(opt);
		}
	}

}

///
/// 月のselectをセットする。
/// param : idMonth 月のselectのid
///
function setMonthList(idMonth)
{
	//selectオブジェクトを取得
	var selMonth = document.getElementById(idMonth);

	//selectオブジェクトをクリア
	selMonth.length = 0;

	//最初の行を設定
	var opt = document.createElement("option");
	opt.text = "月";
	opt.value = "-1";
	opt.className = "default";
	try{
		selMonth.add(opt,null);
	}catch(e){
		selMonth.add(opt);
	}

	//月を設定
	for(m=1; m<=12; m++)
	{
		opt = document.createElement("option");
		opt.text = m + "月";
		var mmm = new String("0"+m);
		opt.value = mmm.substr(mmm.length-2,2);
		try{
			selMonth.add(opt,null);
		}catch(e){
			selMonth.add(opt);
		}
	}
}

///
/// 各月の日数
///
var DaysOfMonth = new Array(12);
DaysOfMonth[0] = 31;
DaysOfMonth[1] = 28;
DaysOfMonth[2] = 31;
DaysOfMonth[3] = 30;
DaysOfMonth[4] = 31;
DaysOfMonth[5] = 30;
DaysOfMonth[6] = 31;
DaysOfMonth[7] = 31;
DaysOfMonth[8] = 30;
DaysOfMonth[9] = 31;
DaysOfMonth[10] = 30;
DaysOfMonth[11] = 31;

///
/// 日付のselectをセットする
/// param idYear  年のselectのID
///       idMonth 月のselectのID
///       idDay   日のselectのID
///
function setDayList(idYear,idMonth, idDay, idText)
{
	var selYear = document.getElementById(idYear);
	var selMonth = document.getElementById(idMonth);
	var selDay = document.getElementById(idDay);
	var txtBirthDay = document.getElementById(idText);
	//選択位置の退避
	//dayPosition = selDay.selectedIndex;
	
	//日付一覧を削除
	//selDay.length = 0;
    
    //sapnの中身全て削除
    while (selDay.firstChild) {
	    selDay.removeChild(selDay.firstChild);
    }
    
    //select作成
    select = document.createElement('select');
    select.className = "default";
    select.onchange = function (){changeDay(idYear, idMonth, idDay, idText, this); chkNum(this);}//firefox用にchkNumが必要
    select.name = "selDay";
    
    //spanに、作成したselectを追加
    selDay.appendChild(select);
    
	//最初の行を追加
	var opt = document.createElement("option");
	opt.text = "日";
	opt.style.color = "#999";//ie6用
	opt.value = "-1";
	
	try{
		select.add(opt,null);
	}catch(e){
		select.add(opt);
	}

	//年と月が設定されている場合に、日付を設定
	if(selYear.selectedIndex > 0 && selMonth.selectedIndex > 0)
	{
		//最終日付を取得
		eDay = DaysOfMonth[selMonth.selectedIndex - 1];

		//うるう年検査
		if(selMonth.selectedIndex == 2)
		{
			year = selYear.options[selYear.selectedIndex].value;
			if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )
			{
				eDay++;
			}
		}

		//日付一覧を作成する
		for(d=1; d<=eDay; d++)
		{
			opt = document.createElement("option");
			opt.text = d + "日";
			var ddd = new String("0"+d);
			opt.value = ddd.substr(ddd.length-2,2);
			try{
				select.add(opt,null);
			}catch(e){
				select.add(opt);
			}
		}

		//元の選択位置を戻す
		//if(dayPosition < select.length)
		//{
			//select.selectedIndex = dayPosition;
		//}
		
	}
	
	return select;
}

///
/// 年月日が選択された際に、textに値をセットする
/// param idYear  年のselectのID
///       idMonth 月のselectのID
///       idDay   日のselectのID
///       idText  セットするinput type=textのID 
///
function changeDate(idYear,idMonth, idDay, idText, select)
{
	//selectオブジェクトを取得
	var selYear  = document.getElementById(idYear);
	var selMonth = document.getElementById(idMonth);
	
	//書き込み先オブジェクトを取得
	var txtDate = document.getElementById(idText);
	
	//年月日情報を取得
	yy = selYear.options[selYear.selectedIndex].value;
	mm = selMonth.options[selMonth.selectedIndex].value;
	dd = select.selectedIndex;
	
	if (0 < dd && dd < 10) //日付が一桁なら0を加える(0はValidationチェックのため、つけない)
	{
	    dd = "0" + dd;
	}
	
   //日付をセット
    if (yy == -1 && mm == -1 && dd == 0)
    {
        txtDate.value = "";
    }
    else
    {
    	txtDate.value = yy + "/" + mm + "/" + dd;
    }
}

///
/// textの値から各selectを設定する
/// param idYear  年のselectのID
///       idMonth 月のselectのID
///       idDay   日のselectのID
///       idText  input type=textのID 
///
function setDateFromText(idYear,idMonth,idDay,idText)
{
	//オブジェクトを取得
	var selYear  = document.getElementById(idYear);
	var selMonth = document.getElementById(idMonth);
	var selDay   = document.getElementById(idDay);
	var txtDate  = document.getElementById(idText);

	var strDate  = txtDate.value;
	var strYear;
	var strMonth;
	var strDay;

	if(strDate.length == 10)
	{
		strYear = strDate.substr(0,4);
		strMonth = strDate.substr(5,2);
		strDay = strDate.substr(8,2);
		selectListFromValue(selYear,strYear);
		selectListFromValue(selMonth,strMonth);
		select = setDayList(idYear,idMonth,idDay,idText);
		selectListFromValue(select, strDay);
	}
	else
	{
		selYear.selectedIndex = 0;
		selMonth.selectedIndex = 0;
		select = setDayList(idYear,idMonth,idDay,idText);
		select.selectedIndex = 0;
	}
}

///
/// 年のselectが変更された場合の処理
///
function changeYear(idYear,idMonth,idDay,idText)
{
	var select = setDayList(idYear,idMonth,idDay,idText);
	changeDate(idYear,idMonth,idDay,idText,select);
}

///
/// 月のselectが変更された場合の処理
///
function changeMonth(idYear,idMonth,idDay,idText)
{
	var select = setDayList(idYear,idMonth,idDay,idText);
	changeDate(idYear,idMonth,idDay,idText, select);
}

///
/// 日のselectが変更された場合の処理
///
function changeDay(idYear,idMonth,idDay,idText,select)
{
    changeDate(idYear,idMonth,idDay,idText, select);
}


///
/// 初期化
/// param idYear  年のselectのID
///       idMonth 月のselectのID
///       idDay   日のselectのID
///       idText  input type=textのID 
///        bOffset 年の開始オフセット （例：２年前からの場合、-2 を指定） 
///        aOffset 年の終了オフセット （例：２年先までの場合、2 を指定） 
///
function initDateList(idYear,idMonth,idDay,idText,bOffset,aOffset)
{
	setYearList(idYear,bOffset,aOffset);
	setMonthList(idMonth);
	
	setDateFromText(idYear,idMonth,idDay,idText);
}




///
/// ポップアップDIV
/// 
function openDivWindow(event,id,num)
{
	if (!event) var event=window.event;
	if (!event.pageX) event.pageX = event.clientX + document.body.scrollLeft;
	if (!event.pageY) event.pageY = event.clientY + document.body.scrollTop;

	document.getElementById(id).style.display = "block";
	document.getElementById(id).style.top = event.pageY + "px";
	document.getElementById(id).style.left = event.pageX + "px";

	if (navigator.userAgent.indexOf('MSIE 7')!=-1){
		document.getElementById(id).style.top = event.pageY + document.documentElement.scrollTop + "px";
		document.getElementById(id).style.left = event.pageX + "px";
	}

	if(num!=undefined) {
		alert("opened " + num);
	}
}
function closeDivWindow(id)
{
	document.getElementById(id).style.display = "none";
	document.getElementById(id).style.top = "0px";
	document.getElementById(id).style.left = "-9999px";
}


var AreaDropDown
var CountryDropDown
var CityDropDown

var area_hidden 
var country_hidden
var city_hidden
 
///
/// 地域セレクトで選択されたオプションから国セレクトを反映する処理///

function GetCountryOption(area_dd, country_dd, city_dd, area_hide, country_hide, city_hide)
{
    AreaDropDown    = $get(area_dd);
    CountryDropDown = $get(country_dd);
    CityDropDown    = $get(city_dd);
   
    area_hidden     = $get(area_hide);
    country_hidden  = $get(country_hide);
    city_hidden     = $get(city_hide);
   
    //地域セレクト、国セレクトで選択されたオプションのインデックスを取得
    var area_index    = AreaDropDown.selectedIndex;

    //インデックスからエリアコード、都市コードを取得
    var area_cd    = AreaDropDown.options[area_index].value;
    
    area_hidden.value    = area_cd;
    country_hidden.value = "";
    city_hidden.value    = "";
    
    //webサービスで地域コードから国コード一覧を取得する
    
    SearchControlService.GetCountryCodeByAreaCode(area_cd, OnGetCountryOptionSuccess, OnGetCountryOptionFailure);
    
}

///
/// SearchControlServiceが成功した場合の処理
///
function OnGetCountryOptionSuccess(result)
{
    var area_index = AreaDropDown.selectedIndex;
    AreaDropDown.options[area_index].selected = true;
    
    //表示されている国option、都市optionを一度クリアする
    //国option
    var counry_length = CountryDropDown.options.length;
    for (var i = 0; i < counry_length; i++)
    {
        CountryDropDown.options[0] = null;
    }
    CountryDropDown.options.length = 0;
    CountryDropDown.options[0] = new Option("-", "");
    CountryDropDown.options[0].selected = true;
    
    //都市option
    var city_length = CityDropDown.options.length;
    
    
    for (var i = 0; i < city_length; i++)
    {
        CityDropDown.options[0] = null;
    }
    
    
    CityDropDown.options.length = 0;
    CityDropDown.options[0] = new Option("-", "");
    
    CityDropDown.options[0].selected = true;
     
    //該当する国一覧を表示する
    //該当する国がなければ終了
    if (result == null || result.length == 0)
    {
        return;
    }
   
    for (var i = 0; i < result.length; i++)
    {
        CountryDropDown.options[i + 1] = new Option(result[i].name, result[i].code);
    }
}


///
/// SearchControlServiceが失敗した場合の処理
///
function OnGetCountryOptionFailure(ex)
{
    alert(ex.get_message());
}

///
/// 国セレクトで選択されたオプションから都市セレクトを反映する処理///
function GetCityOption(area_dd, country_dd, city_dd, area_hide, country_hide, city_hide)
{  
    AreaDropDown    = $get(area_dd);
    CountryDropDown = $get(country_dd);
    CityDropDown    = $get(city_dd);
    
    area_hidden     = $get(area_hide);
    country_hidden  = $get(country_hide);
    city_hidden     = $get(city_hide);
    
    //地域セレクト、国セレクトで選択されたオプションのインデックスを取得
    var area_index    = AreaDropDown.selectedIndex;
    var country_index = CountryDropDown.selectedIndex;

    //インデックスからエリアコード、都市コードを取得
    var area_cd    = AreaDropDown.options[area_index].value;
    var country_cd = CountryDropDown.options[country_index].value;
    
    area_hidden.value    = area_cd;
    country_hidden.value = country_cd;
    city_hidden.value    = "";
    
    //webサービスで地域コード、国コードから都市コード一覧を取得する
    SearchControlService.GetCityCodeByCountryCode(area_cd, country_cd, OnGetCityOptionSuccess, OnGetCityOptionFailure);
}

///
/// SearchControlServiceが成功した場合の処理
///
function OnGetCityOptionSuccess(result)
{
    var area_index    = AreaDropDown.selectedIndex;
    var country_index = CountryDropDown.selectedIndex;
    
    AreaDropDown.options[area_index].selected = true;
    CountryDropDown.options[country_index].selected = true;
    
    
    //表示されている都市optionを一度クリアする。
    var length = CityDropDown.options.length;
    for (var i = 0; i < length; i++)
    {
        CityDropDown.options[0] = null;
    }
    CityDropDown.options.length = 0;
    CityDropDown.options[0] = new Option("-", "");
    CityDropDown.options[0].selected = true;
    
    //該当する都市一覧を表示する
    //該当する都市がなければ終了
    if (result == null || result.length == 0)
    {
        return;
    }
   
    for (var i = 0; i < result.length; i++)
    {
        CityDropDown.options[i + 1] = new Option(result[i].name, result[i].code);
    }
}

///
/// SearchControlServiceが失敗した場合の処理
///
function OnGetCityOptionFailure(ex)
{
    alert(ex.get_message());
}

function SetCityOption(area_dd, country_dd, city_dd, area_hide, country_hide, city_hide)
{
    AreaDropDown    = $get(area_dd);
    CountryDropDown = $get(country_dd);
    CityDropDown    = $get(city_dd);
    
    area_hidden     = $get(area_hide);
    country_hidden  = $get(country_hide);
    city_hidden     = $get(city_hide);
    
    //地域セレクト、国セレクトで選択されたオプションのインデックスを取得
    var area_index    = AreaDropDown.selectedIndex;
    var country_index = CountryDropDown.selectedIndex;
    var city_index    = CityDropDown.selectedIndex;
    
    //インデックスからエリアコード、都市コードを取得
    var area_cd    = AreaDropDown.options[area_index].value;
    var country_cd = CountryDropDown.options[country_index].value;
    var city_cd    = CityDropDown.options[city_index].value;

    area_hidden.value = area_cd;
    country_hidden.value = country_cd;
    city_hidden.value = city_cd;
}



function IsReadyState()
{
	if(window.document.readyState != null&&window.document.readyState != 'complete')
	{
		return false;		
	}else{
		return true;		
	}
}
