/**
 * VMS共通JavaScript
 */

/**
 * 現在の日付を(yy-mm-dd)形式で得る
 * @param void
 * @return string 今日の日付(yy-mm-dd)
 */
function getToday()
{
  n = new Date();
  yy = n.getFullYear();
  m = n.getMonth()+1+"";
  mm = ('0'+m).slice((-1+m.length),(1+m.length));
  d = n.getDate()+"";
  dd = ('0'+d).slice((-1+d.length),(1+d.length));
  return yy+"-"+mm+"-"+dd;
}

/**
 * 税を入力する
 * @param void
 * @return string 今日の日付(yy-mm-dd)
 */
function setTax( priceObj, taxObj, taxRate )
{
	taxObj.value = Math.floor( priceObj.value * taxRate );
}

/**
 * フォームエレメントが送信されるか確認する
 * checkbox・hidden
 * @param string フォームエレメント名
 * @return bool 結果
 */
function checkSendObject( objName )
{
	var flag = false;
	for (i=0 ; i<document.getElementsByName(objName).length ; i++)
	{
		switch( document.getElementsByName(objName)[i].type )
		{
			// チェックボックス
			case "checkbox":
			if( document.getElementsByName(objName)[i].checked )
			{
				flag = true;
			}
			break;
			// ヒドゥン
			case "hidden":
			if( document.getElementsByName(objName)[i].value )
			{
				flag = true;
			}
			break;
		}
	}
	return flag;
}

/**
 * チェックボックスのチェック数を得る
 * @param object フォームオブジェクト
 * @return int チェック数
 */
function countChecked( obj )
{
	var c = 0;

	for ( i=0; i<obj.elements.length; i++ )
	{
		if ( obj.elements[i].checked )
		{
			c = c + 1;
		}
	}
	return c;
}

/**
 * ポストで新しいウインドウを開く
 * @param string オープンさせるURL
 * @param int ウインドウ幅
 * @param int ウインドウ高さ
 * @return void
 */
function postNewWindow(objForm, actionUrl, targetWindowName, fileName, w, h)
{
	// ファイル名縦横サイズ指定でwindow.open
	if (fileName&&w&&h)
	{
		win=window.open(fileName, targetWindowName,'width='+w+',height='+h);
		win.focus();
	}
	// パラメータ保存
	targetBackup = objForm.target;
	actionBackup = objForm.action;
	// パラメータ変更
	objForm.target=targetWindowName;
	objForm.action=actionUrl;
	objForm.submit();

	// 保存しておいたパラメータを戻す
	objForm.target = targetBackup;
	objForm.action = actionBackup;
}

 /*
function postNewWindow ( win, url )
{
	//win = window.open("blank.htm","newWindow","width="+w+",height="+h);
	win.document.open('text/html');
	win.document.write("<html>");
	win.document.write("<head></head>");
	win.document.write("<body>");
	win.document.write("<form method='post' action='" + url + "'>\n");
	for( i=0; i < win.window.opener.document.forms[0].elements.length; i++ )
	{
	//win.document.write(win.window.opener.document.forms[0].elements[i].type);
	switch( win.window.opener.document.forms[0].elements[i].type )
	{
		// チェックのついているチェックボックスのみ表示
		case "checkbox":
		if(win.window.opener.document.forms[0].elements[i].checked)
		{
			name  = win.window.opener.document.forms[0].elements[i].name;
			value = win.window.opener.document.forms[0].elements[i].value;
			win.document.write("<input type='hidden' name='" + name + "' value='" + value + "'>\n");
		}
		break;

		// サブミットボタンは非表示
		case "submit":
		break;

		// サブミットボタンは非表示
		default:
		name  = win.window.opener.document.forms[0].elements[i].name;
		value = win.window.opener.document.forms[0].elements[i].value;
		win.document.write("<input type='hidden' name='" + name + "' value='" + value + "'>\n");
		break;
		}
	}
	win.document.write("</form>");
	win.document.write("</body></html>");
	win.document.close();
	win.document.forms[0].submit();
	win.focus();
}*/

/**
 * フォーム内の全てのチェックボックスをトグルする
 * @param object formオブジェクト
 * @return bool check状態
 */
var checkFlag = false;
function checkAllToggle( obj )
{
	if ( ! obj ) return;
	checkFlag = !checkFlag;
	for( var i=0; i<obj.elements.length; i++ ) obj.elements[i].checked = checkFlag;
	return checkFlag;
}

/**
 * チェックボックスをトグルする
 * @param string オブジェクト名
 * @return void
 */
function toggle(objName)
{
	document.getElementById(objName).checked = (document.getElementById(objName).checked ? false : true);
}

/**
 * チェックボックスの状態で背景色を変更する
 * @param string 対象オブジェクト名
 * @param string チェックボックスオブジェクト名
 * @param string チェック色
 * @return void
 */
function changeRowColor(rowID, checkBoxID, color)
{
	//alert(rowName);
	// チェック状態による色の変更
	if (document.getElementById(checkBoxID).checked)
	{
		document.getElementById(rowID).style.backgroundColor = color;
	} else {
		document.getElementById(rowID).style.backgroundColor = "";
	}
}

/**
 * フォーム内のチェックボックスの状態で背景色を変更する
 * （フォーム内に他のチェックボックスが無い場合に限る）
 * @param string 対象フォーム
 * @param string 対象オブジェクト接頭名
 * @param string チェック色
 * @return void
 */
function changeRowColorAll(rowID, formID, color)
{
	formObj = document.getElementById(formID);
	for( var i=0; i<formObj.elements.length; i++ )
	{
		rowObj = document.getElementById(rowID+formObj.elements[i].name);
		if (rowObj)
		{
			if (formObj.elements[i].checked)
			{
				rowObj.style.backgroundColor = color;
			} else {
				rowObj.style.backgroundColor ="";
			}
		}
	}
}

/**
 * 動的セレクトボックス作成
 *
 * @param object 対象エレメント
 * @param string 選択値
 * @param string 親の選択値
 * @return void
 */
function changeSelect(target, myValue, parentValue)
{
  if (typeof(target)=="undefined") {return;}
  if (typeof(selectOptions)=="undefined") {return;} 
  
  // 選択値の設定
  var targetValue = 0;
  if (target.options.length) targetValue = target.options[target.selectedIndex].value;
  if (!targetValue) targetValue = myValue
  //alert(targetValue);

  // SelectBoxの初期化
  target.length = 1;
  target.options[0].value = "0";
  target.options[0].text  = selectOptions[0][0];

  // SelectBoxを作成
  // selectOptions配列に要素を設定しておく事
  indexCount=1;
  for (i=1; i<selectOptions[parentValue].length; i++) {
    if (selectOptions[parentValue][i]) {
      target.length++;
	  target.options[indexCount].value = i;
      target.options[indexCount].text  = selectOptions[parentValue][i];
      // 選択値の設定
      if (parseInt(targetValue)==i) {
        target.selectedIndex = indexCount;
      }
      indexCount++;
    }
  }
}
