// アップロードするファイルをチェック ///////////////////////////////////////////
function check_upfile ( src_id, str_ext ) {
 var str_fn = document.getElementById( src_id ).value;
 ary_fn = str_fn.split('\\');
 ary_fn.reverse();
 str_fn = ary_fn[0];
 // 拡張子チェック
 up_chk_flg = check_ext ( str_fn, str_ext );
 if ( up_chk_flg == 1 ) {
  alert ( 'アップロードできないファイル形式です。\n選択しなおしてください。');
  return;
 }
 // 文字置換
 str_new_fn = toUnderscore ( str_fn );
// 2007/03/27 一時コメントアウト
// document.getElementById( dst_id ).value = str_new_fn;
 return;
}


// アップロードするファイルの basename を取り出す ///////////////////////////////
function make_link_anker ( src_id, dst_id ) {
 var str_fn = document.getElementById( src_id ).value;
 ary_fn = str_fn.split('\\');
 ary_fn.reverse();
 str_fn = ary_fn[0];
 // 拡張子チェック
 up_chk_flg = check_ext ( str_fn, '111001' );
 if ( up_chk_flg == 1 ) {
  alert ( 'アップロードできないファイル形式です。\n選択しなおしてください。');
  return;
 }
 // 文字置換
 str_new_fn = toUnderscore ( str_fn );
// 2007/03/27 一時コメントアウト
// document.getElementById( dst_id ).value = str_new_fn;
 return;
}


// プレビューウィンドウオープン /////////////////////////////////////////////////
function page_preview  ( targetURL ) {
 window_name = 'subwindow';
 option      = 'location=0,menubar=0,scrollbars=1,status=0,width=820,height=600 resizable=1';
 option      = '';
 if ( targetURL.indexOf( '?', 0 ) < 0 ) {
  str_joint = '?';
 } else {
  str_joint = '&';
 }
 subwin  = window.open( targetURL + str_joint + 'mode=preview', window_name, option );
 subwin.focus();
}


// プレビューページのリロード ///////////////////////////////////////////////////
function page_reload ( ) {
 var str_href = location.href;
 var length   = str_href.length;
 var flg      = str_href.substr( length-1, 1 )
// alert( str_href + '\n' + length + '\n' + flg );
 if ( flg != 1 ) {
  location.href = location.href + '&reload=1';
 } else {
// 遅延措置をする場合
  for ( i=0; i<1001; i++ ) {
   str_i = Math.floor( i/10 );
   str_percent = 'now loading ... ' + str_i + '%';
   document.getElementById('header_contents').innerHTML = str_percent;
  }
  str_html = '<table class="preview"><tr><td class="td_preview">プレビュー画面です</td><td>';
  str_html = str_html + '<button onclick="window.close();">閉じる</button>';
  str_html = str_html + '</td></tr></table>';
  document.getElementById('header_contents').innerHTML = str_html;
 }
}


// データアップロードのフィールドチェック ///////////////////////////////////////
function check_up_data () {
 str_fn = document.getElementById('up_data').value;
 // 拡張子による判定
 up_chk_flg = check_ext ( str_fn, '111111' );
 if ( up_chk_flg == 1 ) return;
 // 文字置換
 str_new_fn = toUnderscore ( str_fn );
}


// 拡張子検査 ///////////////////////////////////////////////////////////////////
function check_ext ( str_fn, str_ok_ext ) {
 // str_ok_ext に対応する拡張子配列を用意
 var ary_ext = new Array ( 'jpg', 'gif', 'zip', 'lzh', 'pdf', 'ai' );
 
 // 拡張子のみ取り出し小文字に変換
 str_ext = str_fn.replace ( /.*\./, '' );
 str_ext = str_ext.toLowerCase();
 chkflg = 1;
 for ( i=0; i<ary_ext.length; i++ ) {
  if ( str_ok_ext.charAt(i) == '1' ) {
   if ( str_ext == ary_ext[i] ) {
    chkflg = 0;
    document.getElementById('alert').innerHTML = '';
   }
  }
 }
 if ( chkflg == 1 ) {
  str_alert = 'アップロードできないファイル形式です';
  document.getElementById('alert').innerHTML = str_alert;
 } else  {
  chkflg = 0;
  document.getElementById('alert').innerHTML = '';
 }
 return chkflg;
}


// 全角文字を半角_に置き換え /////////////////////////////////////////////////////
function toUnderscore ( chkstr ) {
// アップロード対象ファイルをパスとファイル名に切り分ける
 var sp_path   = chkstr.split ( '\\' );
 var str_path = '';
 for ( i=0; i<sp_path.length-1; i++ ) {
  str_path = str_path + sp_path[i] + '\\';
 }
 var str_fn = sp_path[i];
 // 全角文字列中の数字・ハイフンなどを半角に
 ZenByte = "あ".length;
 zenkaku = "０１２３４５６７８９．，＿－ー～＠ａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ";
 hankaku = "0123456789.,_---@abcdefghijklmnopqrstuvwxyz";
 for ( cnt = 0; cnt <= hankaku.length -1; cnt ++ ) {
  chktarget = zenkaku.substring( cnt * ZenByte, cnt * ZenByte + ZenByte );
  while ( str_fn.indexOf ( chktarget ) >= 0 ) {
   str_fn = str_fn.substring ( 0, str_fn.indexOf ( chktarget ) )
          + hankaku.charAt ( cnt )
          + str_fn.substring ( str_fn.indexOf ( chktarget ) + ZenByte, str_fn.length );
  }
 }
 // ファイル名中のダメな文字はすべて '_' に変換
 str_new_fn = str_fn.replace ( /[^0-9,^a-z,^A-Z,^\.,]/g, '_' );
 str_new_fn = str_new_fn.replace ( /_+/g, '_' );
 if ( str_fn == str_new_fn ) {
  chkflg = 0;
 } else {
  chkflg = 1;
  str_alert = 'サーバ上で使えない文字があったのでファイル名を ' + str_new_fn + ' に変更してアップロードします';
  document.getElementById('alert').innerHTML = str_alert;
 }
 document.getElementById('up_fn').value = str_new_fn;
 return str_new_fn;
}


// 必須条件を満たしているか ////////////////////////////////////////////////////
function check_fill () {
 if ( document.getElementById('up_data').value  == '' ) {
  alert('アップロードするファイルを選択してください');
 } else if ( document.getElementById('target_dir').value == '' ) {
  alert('アップロード先を選択してください');
 } else {
  document.getElementById('upload_file').submit();
 }
}

