// アプリケーション特有のJavaScriptの関数とクラスをここに置いてください。
// このファイルは javascript_include_tag :defaults と書けば、自動的に読み込まれます。

// datetime 型フィールドのための一群の select タグをまとめて、enable/disable する。
function enable_datetime_select_tags(object_name, method, checkbox) {
  cb = $(object_name + '_' + checkbox);
  if (!cb) return;
  ary = [1, 2, 3, 4, 5, 6]
  ary.each( function(i) {
    element = $(object_name + '_' + method + '_' + i + 'i');
    if (element) element.disabled = !cb.checked;
  });
}

// datetime 型フィールドのための一群の select タグの値を、
// 別のフィールドの対応する select タグからコピーする。
function copy_datetime_value(object_name, from, to) {
  ary = [1, 2, 3, 4, 5, 6]
  ary.each( function(i) {
    element1 = $(object_name + '_' + from + '_' + i + 'i');
    element2 = $(object_name + '_' + to + '_'  + i + 'i');
    if(element1 && element2)
      element2.value = element1.value;
  });
}

function addCommas(nStr) {
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
          x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}
