 // button 用イベントリスナー
function set_submit_event () {
 $('#submit').click (
  function () {
   if ( $('#c_regist:parent').length != 0 ) {
    ary_check = [
     'c_div[]'
    ];
    ary_text = [
     'c_type',
     'c_type_pos',
     'c_name',
     'c_kana',
     'c_zc',
     'c_pref',
     'c_addr',
     'c_url',
     'c_r_post',
     'c_r_name',
     'c_r_kana',
     'c_r_tel',
     'c_r_fax',
     'c_r_mail',
     'c_s_post',
     'c_s_name',
     'c_s_kana',
     'c_ctgr_c02',
     'c_intro'
    ];
   } else if ( $('#p_regist:parent').length != 0 ) {
    ary_check = [
     'p_ctgr_c01[]',
     'p_ctgr_c02[]',
     'p_ctgr_c03[]',
     'p_ctgr_c04[]'
    ];
    ary_text = [
     'p_name',
     'p_kana',
     'p_ctgr_c00',
     'p_attr00',
     'p_attr01',
     'p_price',
     'p_feat',
     'p_description',
     'p_pic00'
    ];
   } else {
    ary_check = [];
    ary_text = [];
   }

   // アラート用文字列
   str = '';

   // checkbox 判定
   if ( 0 < ary_check.length ) {
    for ( n in ary_check ) {
     flg = false;
     loop_max = $('input[name="' + ary_check[n] + '"]').length;
     for ( i=0; i< loop_max; i++ ) {
// debug code
//alert($('input[name="' + ary_check[n] + '"]')[i].checked);
      if ( $('input[name="' + ary_check[n] + '"]')[i].checked ) {
       flg = true;
      }
     }
     if ( !flg ) {
      field = $('input[name="' + ary_check[n] + '"]').parent().parent().prev().text();
      str += '[' + field + '] は必須項目です\n';
     }
    }
   }

   // .val() を持っているもの（チェックボックス以外）
   if ( 0 < ary_text.length ) {
    for ( n in ary_text ) {
     if ( $('#' + ary_text[n] ).val() == '' ) {
      flg = false;
      // 写真のみ別扱い
      if ( ary_text[n].substr(1,4) == '_pic') {
       name = ary_text[n] + '_org';
// debug code
//alert(name + ', ' + $('input[name=' + name + ']').length );
       if ( $('input[name=' + name + ']').length == 0 ) {
        flg = true;
       }
      } else {
       flg = true;
      }

      if (flg) {
       field = $('#' + ary_text[n] ).parent().prev().text();
       str += '[' + field + '] は必須項目です\n';
      }
     }
    }
   }

   // 文字列が '' でなければ確認画面に進めない
   if ( str != '' ) {
    alert(str);
    $('#submit').attr('disabled','disabled');
   }
  }  // function end
 );  // .click end

}


 // クリッカブルエリア用イベントリスナー
function set_clickable_event () {
 $('.clickable').click (
  function () {
   id_num  = this.id;
   show_id = id_num +'_list';
   status = $('div#'+ show_id).css('display');
   $('div#'+ show_id).toggle();

   $ary_str = $('#'+ id_num ).text().split ('　');
   if ( $('div#'+ show_id).css('display') == 'block' ) {
    $str = $ary_str[0]
     + '<span style="font-weight:normal">　[項目を非表示]</span>'
   } else {
    $str = $ary_str[0]
     + '<span style="font-weight:normal">　[項目を表示]</span>'
   }
   $('#'+ id_num ).html ( $str );
  }  // function end
 );  // .click end

}

// [company] 企業区分用イベントリスナー
function set_maker_shop_check_event () {
 $('input:checkbox').click (
  function () {
   $class0 = 'tr.c_ctgr_c00';
   $class1 = 'tr.c_ctgr_c01';
   if ( $(this).attr ('name') == 'c_div[]' && $(this).val() == '3' ) {
     $( $class0 ).toggle ();
     $( $class1 ).toggle ();
   }
  }  // function end
 );  // .click end

}


 // [dealing] select 用イベントリスナー
function set_dealing_select_event () {
 $('select.category').change (
  function () {
   id_num   = this.id.substr ( 0, 3 );
   val_num  = this.value;
   show_id  = id_num +'_'+ val_num;
   // 変更された division の属性チェックボックスを非表示
   $('.list label:has(input[name="'+id_num+'[]"])').css('display','none');

   // 次の選択肢表示
   $('.list label:has(.attribute_'+ show_id +')').css('display','inline');

  }  // function end
 );  // .change end

}


// [dealing] clear button 用イベントリスナー
function set_dealing_reset_check_event () {
 $('div.reset_check').click (
  function () {
   id_num   = this.id.substr ( 0, 3 );
   div_id   = id_num + '_list';
   $('div#'+ div_id + ' input:checkbox').attr('checked','');
  }  // function end
 );  // .click end
}


// [company] 企業区分初期処理
function init_c_ctgr () {
 if ( $('tr.c_div input:last').attr('checked') ) {
  $('tr.c_ctgr_c00').css('display','block');
  $('tr.c_ctgr_c01').css('display','block');
 } else {
  $('tr.c_ctgr_c00').css('display','none');
  $('tr.c_ctgr_c01').css('display','none');
 }
}


// [regist] upload file 拡張子監視イベントリスナー
function set_uplfile_check_event () {
 $('input:file').change (
  function () {
   str_ext = this.value.replace ( /.*\./, '' );
   str_ext = str_ext.toLowerCase();
   switch ( str_ext ) {
    case 'gif' :
    case 'jpg' :
    case 'jpeg' :
    case 'png' :
     // アップロード可
     $('#submit').attr('disabled','');
     break;
    default :
     // アップロード不可
     // JS の仕様により type=file の変更はできないので submit を disabled
     $('#submit').attr('disabled','disabled');
     str = 'アップロードできる画像は拡張子が\n「gif」「jpg」「jpeg」「png」\nのものだけです';
     alert ( str );
   }
  }  // function end
 );  // .change end
}


// [regist] uri 監視イベントリスナー
function set_uri_check_event () {
 $('#c_url').change (
  function () {
   ary_uri = this.value.split('//');
   protcol = ary_uri[0];
   switch ( protcol ) {
    case 'http:' :
    case 'https:' :
     // アップロード可
     $('#submit').attr('disabled','');
     break;
    default :
     // アップロード不可
     $('#submit').attr('disabled','disabled');
     str = '企業URLは http:// か https:// で始めてください';
     alert ( str );
   }
  }  // function end
 );  // .change end
}


// [regist] 使用不可文字置換イベントリスナー
function set_disablechar_replace_event () {
 $('input:text, textarea').change (
  function () {
   this.value = this.value.replace("`", "‘");
   this.value = this.value.replace('<', '＜');
   this.value = this.value.replace('>', '＞');
   this.value = this.value.replace("'","’");
   this.value = this.value.replace('\t','　');

   // submit ボタン復帰
   $('#submit').attr('disabled','');
  }  // function end
 );  // .change end
 $('input:text').change (
  function () {
   this.value = this.value.replace('\n','');
  }  // function end
 );  // .change end

}


// [regist] 高コントラスト時フィールド幅調整イベントリスナー
function set_textarea_resize_event () {
//alert($('body').css('font-size'));
//alert($('body').css('background-color'));
 if ($('body').css('font-size') == '16px'){
  $('textarea').attr('cols','36');
  $('#c_s_post').parent().prev().width('60px');
  $('#c_s_name').parent().prev().width('60px');
  $('#c_s_kana').parent().prev().width('60px');
  $('#c_s_post').parent().prev().css('white-space', 'normal');
  $('#c_s_name').parent().prev().css('white-space', 'normal');
  $('#c_s_kana').parent().prev().css('white-space', 'normal');
 }
}


// 初期化処理 ==============================================
function initialize() {

 // 読み込みチェック（通常はコメントアウト）
 // alert('now loading p_regist.js');

 // 属性チェックボックスは非表示
 $('div.list').css('display','none');

 // 店舗用属性チェックボックス もし「ショップ」なら表示
 init_c_ctgr ();

 // クリッカブルエリア設定
 $('.clickable').css('cursor','pointer');
 for ( $i=0; $i<=7; $i++ ) {
  for ( $j=1; $j<=12; $j++ ) {
   $str = $('#p0'+$i+'_'+$j +'.clickable').text();
   if ( $str != '' ) {
    if ( 0 < $('#p0'+$i+'_'+$j+'_list :checked').val() ) {
     $str = $str
      + '<span style="font-weight:normal">　[クリックして項目を非表示]</span>';
     $('#p0'+$i+'_'+$j).html ( $str );
    } else {
     $str = $str
      + '<span style="font-weight:normal">　[クリックして項目を表示]</span>';
     $('#p0'+$i+'_'+$j).html ( $str );
    } // if ( 0 < $('#p0'+$i+'_'+$j+'_list :checked').val() )
   } else {
    break;
   } // if ( $str != '' )
  }
 }
 for ( $i=1; $i<=19; $i++ ) {
  $str = $('#c_ctgr_c02_'+ $i +'.clickable').text();
  if ( $str != '' ) {
   if ( 0 < $('#c_ctgr_c02_'+$i+'_list :checked').val() ) {
    $str = $str
     + '<span style="font-weight:normal">　[クリックして項目を非表示]</span>';
    $('#c_ctgr_c02_'+ $i).html ( $str );
   } else {
    $str = $str
     + '<span style="font-weight:normal">　[クリックして項目を表示]</span>';
    $('#c_ctgr_c02_'+ $i).html ( $str );
   } // if ( 0 < $('#p0'+$i+'_'+$j+'_list :checked').val() )
  } else {
   break;
  } // if ( $str != '' )
 }


 // 選択されていた項目があれば表示
 $('div.list:has(:checked)').css('display','block');
 $('tr.c_ctgr_c00:has(:checked)').css('display','block');
 $('tr.c_ctgr_c01:has(:checked)').css('display','block');
 $('tr.c_ctgr_c02:has(:checked)').css('display','block');

 for (i=1; i<=7; i++ ) {
  $('div#p0'+ i +'_selected label').css('display','none');
  $('div#p0'+ i +'_list label').css('display','none');
  $('div#p0'+ i +'_list').css('display','block');
 }

 // イベントリスナー設定
 set_clickable_event ();
 set_submit_event ();
 set_dealing_select_event ();
 set_dealing_reset_check_event ();
 set_maker_shop_check_event ();
 // 2009-09-24 追加
 set_uplfile_check_event ();
 set_uri_check_event ();
 set_disablechar_replace_event ();
 // 2009-10-13 追加
 set_textarea_resize_event ();
}

// for event --------------------------------------------------------------------
$( initialize );




