/*
	■階層セレクトボックスの制御

	前提:
	・デフォルトエレメントが存在すること。
	・階層セレクトボックスは、次のidを設定しなければならない。

		２階層の場合
			１階層のセレクトボックス    id_comb_select_first:識別名
			２階層のセレクトボックス    id_comb_select_second:識別名

		３階層の場合
			１階層のセレクトボックス    id_tri_select_first:識別名
			２階層のセレクトボックス    id_tri_select_second:識別名
			３階層のセレクトボックス    id_tri_select_third:識別名

			識別名は、ユニークであること。同じグループのセレクトボックスで同一とする。

	注意:
	・combSelectinit(tripleSelectinit)をonLosdイベントで呼び出すこと。
	・配列データの名前は、シングルクォートやダブルクォートで囲まないこと。
	・階層データは、GUI_CombSelectDataで生成する。
	・２階層(３階層)のセレクトボックスは、GUI_CombSelectで生成する。

	※詳細は、「職種グループ－職種セレクトのデータ設定.txt」を参照下さい。
*/


// ●２階層セレクトボックスを初期化する
// data_name : ２階層データ名
function combSelectinit(data_name)
{
	//全てのセレクトボックスを取得
	var collection = document.getElementsByTagName('select');
	var total = collection.length;

	for (i=0 ; i<total ; i++)
	{
		var id = collection[i].getAttribute('id');
        if(id != null) {
    		var id_element = id.split(':');

    		//idが２階層セレクトボックスの場合、初期化する
    		if (id_element[0] == 'id_comb_select_first')
    		{
    			changeCombSelect(data_name , id_element[1] , 'init');
    		}
        }
	}
}


// ●２階層セレクトボックスを制御する
// data_name : ２階層データ名
// no : セレクトボックス番号
// mode : 制御モード  init : 初期化モード
function changeCombSelect(data_name , no , mode, all)
{
  //１階層のセレクトボックスのid
  var id_first = 'id_comb_select_first:' + no;

  //２階層のセレクトボックスのid
  var id_second = 'id_comb_select_second:' + no;
  var id_second_val = 'id_comb_select_second_val:' + no;

  var index = document.getElementById(id_first).selectedIndex;
  if (index == 9999 || index == 0)
  {
    //１階層のセレクトボックスがデフォルトの場合、２階層のセレクトボックスを空にする
    if (all) {
      var len = data_name.length;
      var total = 0;
      for (var i = 0; i < len; ++i) {
        total += data_name[i].length;
      }
      document.getElementById(id_second).length = 1;
      document.getElementById(id_second).length = total + 1;

      var idx = 1;
      for (i = 0; i < len; ++i) {
        total = data_name[i].length;
        for (k = 0; k < total; ++k, ++idx) {
          var option_data = data_name[i][k].split('->');

          document.getElementById(id_second).options[idx].value = option_data[0];
          document.getElementById(id_second).options[idx].text = option_data[1];
        }
      }
    } else {
      document.getElementById(id_second).length = 1;
      document.getElementById(id_second).selectedIndex = 0;
    }
  }
  else
  {
    //１階層のセレクトボックスが指したエレメントに紐付く２階層のセレクトボックスを設定

    var total = data_name[index].length;
    document.getElementById(id_second).length = 1;
    document.getElementById(id_second).length = total + 1;

    for (k=0 ; k<total ; k++)
    {
      //keyとvalueを分解
      var option_data = data_name[index][k].split('->');

      document.getElementById(id_second).options[k + 1].value = option_data[0];
      document.getElementById(id_second).options[k + 1].text = option_data[1];
    }
  }

  //初期化の操作でなければデフォルトを選択
  if (mode != 'init')
  {
    document.getElementById(id_second).selectedIndex = 0;
  } else {
    var second_val = document.getElementById(id_second_val).value;
    var sec = document.getElementById(id_second);
    if (second_val != '') {
      for (var i = 0; i < sec.length; ++i) {
        if (sec.options[i].value == second_val) {
          sec.selectedIndex = i;
          break;
        }
      }
    }
  }
}

// ●２階層セレクトボックスを制御する
// data_name : ２階層データ名
// no : セレクトボックス番号
// mode : 制御モード  init : 初期化モード
function changeCombSelect_nonEmpty(data_name , no , mode)
{
	//１階層のセレクトボックスのid
	var id_first = 'id_comb_select_first:' + no;

	//２階層のセレクトボックスのid
	var id_second = 'id_comb_select_second:' + no;


	var index = document.getElementById(id_first).selectedIndex;
	//１階層のセレクトボックスが指したエレメントに紐付く２階層のセレクトボックスを設定

	var total = data_name[index].length;
	document.getElementById(id_second).length = total + 1;

	for (k=0 ; k<total ; k++)
	{
		//keyとvalueを分解
		var option_data = data_name[index][k].split('->');

		document.getElementById(id_second).options[k].value = option_data[0];
		document.getElementById(id_second).options[k].text = option_data[1];
	}

	//初期化の操作でなければデフォルトを選択
	if (mode != 'init')
	{
		document.getElementById(id_second).selectedIndex = 0;
	}
}


// ●２階層セレクトボックスを制御する(担当者専用)
// data_name : ２階層データ名
// no : セレクトボックス番号
// mode : 制御モード  init : 初期化モード
function changeCombSelectAgt(data_name , no , mode) {
	//１階層のセレクトボックスのid
	var id_first = 'id_comb_select_agt_first:' + no;

	//２階層のセレクトボックスのid
	var id_second = 'id_comb_select_agt_second:' + no;

	var index = document.getElementById(id_first).selectedIndex;
//	if (index == 0) {
//		//１階層のセレクトボックスがデフォルトの場合、２階層のセレクトボックスを空にする
//		document.getElementById(id_second).length = 1;
//		document.getElementById(id_second).selectedIndex = 0;
//	}
	//１階層のセレクトボックスが指したエレメントに紐付く２階層のセレクトボックスを設定
	var total = data_name[index].length;
	document.getElementById(id_second).length = total + 1;

	for (k=0 ; k<total ; k++) {
		//keyとvalueを分解
		var option_data = data_name[index][k].split('->');
		document.getElementById(id_second).options[k + 1].value = option_data[0];
		document.getElementById(id_second).options[k + 1].text = option_data[1];
	}

	//初期化の操作でなければデフォルトを選択
	if (mode != 'init') {
		document.getElementById(id_second).selectedIndex = 0;
	}
}


// ●３階層セレクトボックスを初期化する
// comb_data_name : ２階層データ名
// tri_data_name : ３階層データ名
function tripleSelectinit(comb_data_name , tri_data_name)
{
	//全てのセレクトボックスを取得
	var collection = document.getElementsByTagName('select');
	var total = collection.length;

	for (i=0 ; i<total ; i++)
	{
		var id = collection[i].getAttribute('id');
		if (id != null) {
			var id_element = id.split(':');
		}

		//idが３階層セレクトボックスの場合、初期化する
		if (id_element[0] == 'id_tri_select_first')
		{
			//１階層のセレクトボックスのid
			var id_first = 'id_tri_select_first:' + id_element[1];

			//２階層のセレクトボックスのid
			var id_second = 'id_tri_select_second:' + id_element[1];

			//３階層のセレクトボックスのid
			var id_third = 'id_tri_select_third:' + id_element[1];

			var index_first = document.getElementById(id_first).selectedIndex;
			if (index_first == 0)
			{
				//１階層のセレクトボックスがデフォルトの場合、２-３階層のセレクトボックスを空にする

				document.getElementById(id_second).length = 1;
				document.getElementById(id_second).selectedIndex = 0;

				document.getElementById(id_third).length = 1;
				document.getElementById(id_third).selectedIndex = 0;
			}
			else
			{
				//１階層のセレクトボックスが指したエレメントに紐付く２階層のセレクトボックスを設定

				var total_first = comb_data_name[index_first].length;
				document.getElementById(id_second).length = total_first + 1;

				for (k=0 ; k<total_first ; k++)
				{
					//keyとvalueを分解
					var option_data = comb_data_name[index_first][k].split('->');

					document.getElementById(id_second).options[k + 1].value = option_data[0];
					document.getElementById(id_second).options[k + 1].text = option_data[1];
				}


				var index_second = document.getElementById(id_second).selectedIndex;

				if (index_second == 0)
				{
					//２階層のセレクトボックスがデフォルトの場合、３階層のセレクトボックスを空にする

					document.getElementById(id_third).length = 1;
					document.getElementById(id_third).selectedIndex = 0;
				}
				else
				{
					//２階層のセレクトボックスが指したエレメントに紐付く３階層のセレクトボックスを設定

					var total_second = tri_data_name[index_first][index_second].length
					document.getElementById(id_third).length = total_second + 1;

					for (k=0 ; k<total_second ; k++)
					{
						//keyとvalueを分解
						var option_data = tri_data_name[index_first][index_second][k].split('->');

						document.getElementById(id_third).options[k + 1].value = option_data[0];
						document.getElementById(id_third).options[k + 1].text = option_data[1];
					}

					//３階層のセレクトボックスが無効の場合は、デフォルトを選択
					var index_third = document.getElementById(id_third).selectedIndex;
					if (index_third < 0)
					{
						document.getElementById(id_third).selectedIndex = 0;
					}
				}
			}
		}
	}
}


// ●３階層セレクトボックスを制御する
// data_name : 階層データ名(階層に適合したデータを設定すること)
// ref : 参照名    first : １階層を参照    second : ２階層を参照 
// no : セレクトボックス番号
function changeTripleSelect(data_name , ref , no)
{
	//１階層のセレクトボックスのid
	var id_first = 'id_tri_select_first:' + no;

	//２階層のセレクトボックスのid
	var id_second = 'id_tri_select_second:' + no;

	//３階層のセレクトボックスのid
	var id_third = 'id_tri_select_third:' + no;


	var index_first = document.getElementById(id_first).selectedIndex;
	if (ref == 'first')
	{
		//１-２階層のセレクトボックスの制御

		if (index_first == 0)
		{
			//１階層のセレクトボックスがデフォルトの場合、２階層のセレクトボックスを空にする

			document.getElementById(id_second).length = 1;
			document.getElementById(id_second).selectedIndex = 0;
		}
		else
		{
			//１階層のセレクトボックスが指したエレメントに紐付く２階層のセレクトボックスを設定

			var total_first = data_name[index_first].length;
			document.getElementById(id_second).length = total_first + 1;

			for (k=0 ; k<total_first ; k++)
			{
				//keyとvalueを分解
				var option_data = data_name[index_first][k].split('->');

				document.getElementById(id_second).options[k + 1].value = option_data[0];
				document.getElementById(id_second).options[k + 1].text = option_data[1];
			}

			//２階層のセレクトボックスはデフォルトを選択
			document.getElementById(id_second).selectedIndex = 0;
		}

		//３階層のセレクトボックスは初期化
		document.getElementById(id_third).length = 1;
		document.getElementById(id_third).selectedIndex = 0;
	}
	else if (ref == 'second')
	{
		//２-３階層のセレクトボックスの制御

		var index_second = document.getElementById(id_second).selectedIndex;

		if (index_second == 0)
		{
			//２階層のセレクトボックスがデフォルトの場合、３階層のセレクトボックスを空にする

			document.getElementById(id_third).length = 1;
			document.getElementById(id_third).selectedIndex = 0;
		}
		else
		{
			//２階層のセレクトボックスが指したエレメントに紐付く３階層のセレクトボックスを設定

			var total_second = data_name[index_first][index_second].length
			document.getElementById(id_third).length = total_second + 1;

			for (k=0 ; k<total_second ; k++)
			{
				//keyとvalueを分解
				var option_data = data_name[index_first][index_second][k].split('->');

				document.getElementById(id_third).options[k + 1].value = option_data[0];
				document.getElementById(id_third).options[k + 1].text = option_data[1];
			}

			//３階層のセレクトボックスはデフォルトを選択
			document.getElementById(id_third).selectedIndex = 0;
		}
	}
}

