/*****************************************************************************/
/*                           グローバル変数定義                              */
/*****************************************************************************/

g = new Array();
//g['sURLRoot']     = 'http://mati.xrea.jp';
g['sURLRoot']     = 'http://mati.hippopo-eraser.org';
g['sProjectRoot'] = '/lv100';

// 標準は最新作に合わせる（プロフィールからのリンクに使用）
g['sVersionRoot'] = g['sProjectRoot'] + '/x';

/*****************************************************************************/
/*                                                                           */
/*  Name     : buildDateStr                                                  */
/*  Function : 時刻文字列の取得                                              */
/*                                                                           */
/*  Params   : 1234567890000                                                 */
/*  Returns  : '2005/01/04 15:31:00'                                         */
/*                                                                           */
/*****************************************************************************/

function buildDateStr( dTime )
{
  var date , dYear , dMonth , dDay , dHour , dMinute , dSecond , sTime;
  
  if( dTime == 0 )
  {
    date = new Date( document.lastModified );
  }
  else
  {
    date = new Date( dTime );
  }
  
  dYear   = date.getYear();
  dMonth  = date.getMonth() + 1;
  dDay    = date.getDate();
  dHour   = date.getHours();
  dMinute = date.getMinutes();
  dSecond = date.getSeconds();
  if( dYear < 2000 )
  {
    dYear += 1900;
  }

  sYear = dYear + "";
  if( dMonth  < 10 ){ sMonth   = "0" + dMonth;  } else { sMonth  = dMonth  + ""; }
  if( dDay    < 10 ){ sDay     = "0" + dDay;    } else { sDay    = dDay    + ""; }
  if( dHour   < 10 ){ sHour    = "0" + dHour;   } else { sHour   = dHour   + ""; }
  if( dMinute < 10 ){ sMinute  = "0" + dMinute; } else { sMinute = dMinute + ""; }
  if( dSecond < 10 ){ sSecond  = "0" + dSecond; } else { sSecond = dSecond + ""; }

  sTime = sYear + "/" + sMonth  + "/" + sDay    + " "
        + sHour + ":" + sMinute + ":" + sSecond + "";

  return sTime;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : convertUnicode                                                */
/*  Function : IEのescape関数と同じようにunicode変換                         */
/*                                                                           */
/*  Params   : 'あ'                                                          */
/*  Returns  : '%u3042'                                                      */
/*                                                                           */
/*****************************************************************************/

function convertUnicode( sPlain )
{
  var i , dCharCode , sConvert , sUnicode;
  
  sConvert = '';
  for( i = 0 ; i < sPlain.length ; i++ )
  {
    dCharCode = sPlain.charCodeAt( i );
    
    // 非ascii文字
    if( 256 <= dCharCode )
    {
      // 21,32,33で始まる文字のほとんどは機種依存と思われる。（希望的観測）
      sUnicode = dCharCode.toString( 16 );
      if( sUnicode.match( /21[0-9a-f]{2}|32[0-9a-f]{2}|33[0-9a-f]{2}/i ) )
      {
        return sPlain;
      }
      
      if( sUnicode.length == 3 )
      {
        sConvert += "%u0" + sUnicode;
      }
      else
      {
        sConvert += "%u" + sUnicode;
      }
      continue;
    }

    // # : 35
    if( dCharCode == 35 )
    {
      sConvert += "%u0023";
      continue;
    }

    // & : 38
    if( dCharCode == 38 )
    {
      sConvert += "%u0026";
      continue;
    }

    // 半角数字  ：48-57
    // 半角英大字：65-90
    // 半角英大字：97-122
    if( ( 48 <= dCharCode && dCharCode <= 57  )
     || ( 65 <= dCharCode && dCharCode <= 90  )
     || ( 97 <= dCharCode && dCharCode <= 122 ) )
    {
      sConvert += sPlain.charAt( i );
      continue;
    }

    // その他ascii文字
    sConvert += "%";
    if( dCharCode <= 15 )
    {
      sConvert += "0";
    }
    sConvert += dCharCode.toString( 16 );
  }

  return sConvert;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : trimNum                                                       */
/*  Function : 入力数字の補正（全角数字、半角/全角スペース、非数字）         */
/*                                                                           */
/*  Params   : " ６１３ "                                                    */
/*  Returns  : "613"                                                         */
/*                                                                           */
/*****************************************************************************/

function trimNum( sNum )
{
  // 全角・スペースの処理
  sNum = sNum.replace( /０/g , "0" );
  sNum = sNum.replace( /１/g , "1" );
  sNum = sNum.replace( /２/g , "2" );
  sNum = sNum.replace( /３/g , "3" );
  sNum = sNum.replace( /４/g , "4" );
  sNum = sNum.replace( /５/g , "5" );
  sNum = sNum.replace( /６/g , "6" );
  sNum = sNum.replace( /７/g , "7" );
  sNum = sNum.replace( /８/g , "8" );
  sNum = sNum.replace( /９/g , "9" );
  sNum = sNum.replace( /　/g , ""  );
  sNum = sNum.replace( / /g  , ""  );
  sNum = sNum.replace( /[^0-9]/g , ""  );

  return sNum;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : writeProcessTime                                              */
/*  Function : 処理時間を出力                                                */
/*                                                                           */
/*  Params   : '0.123'                                                       */
/*  Returns  :                                                               */
/*                                                                           */
/*****************************************************************************/

function writeProcessTime( sProcess )
{
  document.write( buildProcessTime( sProcess ) );
  return;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : buildProcessTime                                              */
/*  Function : 処理時間を組み立て                                            */
/*                                                                           */
/*  Params   : '0.123'                                                       */
/*  Returns  : '<div align="right">...</div>'                                */
/*                                                                           */
/*****************************************************************************/

function buildProcessTime( sProcess )
{
  return '<div align="right">Process Time : ' + sProcess + '</div>';
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : writeSAPlate                                                  */
/*  Function : S.A.Point出力                                                 */
/*                                                                           */
/*  Params   : '900.00'                                                      */
/*             '2006/06/08 07:58:00'                                         */
/*  Returns  :                                                               */
/*                                                                           */
/*****************************************************************************/

function writeSAPlate( sPoint
                     , sUpdate
                     )
{
  writePlate( sPoint , sUpdate , 'S.A.' );
  return;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : writeSkillPlate                                               */
/*  Function : スキルポイント出力                                            */
/*                                                                           */
/*  Params   : '900.00'                                                      */
/*             '2006/06/08 07:58:00'                                         */
/*  Returns  :                                                               */
/*                                                                           */
/*****************************************************************************/

function writeSkillPlate( sPoint
                        , sUpdate
                        )
{
  writePlate( sPoint , sUpdate , 'Skill' );
  return;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : writePlate                                                    */
/*  Function : 各種プレート出力                                              */
/*                                                                           */
/*  Params   : '900.00'                                                      */
/*             '2006/06/08 07:58:00'                                         */
/*             'Skill'                                                       */
/*  Returns  :                                                               */
/*                                                                           */
/*****************************************************************************/

function writePlate( sPoint
                   , sUpdate
                   , sPointName
                   )
{
  var dFontSize;
  
  dFontSize = 32;
  document.write( '<table bgcolor="#bbbbbb"><tr><td>'
                + '<table align="center" cellspacing="0" cellpadding="5">'
                + '<tr>'
                + '<td width="5"></td>'
                + '<td>'
                + '<font color="#ffffff" style="font-family : Gulim; font-size : ' + dFontSize + 'px;">'
                + sPointName + ' Point'
                + '</font>'
                + '</td>'
                + '<td width="5"></td>'
                + '<td bgcolor="#888888" align="right" width="150">'
                + '<font color="#ffffff" style="font-family : Gulim; font-size : ' + dFontSize + 'px;">' + sPoint + '</font>'
                + '</td>'
                + '<td>'
                + '<font color="#ffffff" style="font-family : Gulim; font-size : ' + dFontSize + 'px;">pt.</font>'
                + '</td>'
                + '</tr>'
                + '<tr>'
                + '<td colspan="5" align="center">'
                + '<font color="#ffffff"><strong>Last Update　' + sUpdate + '</strong></font>'
                + '</td>'
                + '</tr>'
                + '</table>'
                + '</td></tr></table>'
                );
  return;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : writeProfile                                                  */
/*  Function : プロフィール出力                                              */
/*                                                                           */
/*  Params   :                                                               */
/*  Returns  :                                                               */
/*                                                                           */
/*****************************************************************************/

function writeProfile( sName
                     , sPref
                     , sLocation
                     , sDDRBegin
                     , sRival
                     , sMixi
                     , sRSS
                     , sWeb
                     , sMail
                     , sIntroduction
                     )
{
  document.write( buildProfile( sName
                              , sPref
                              , sLocation
                              , sDDRBegin
                              , sRival
                              , sMixi
                              , sRSS
                              , sWeb
                              , sMail
                              , sIntroduction
                              )
                );
  return;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : buildProfile                                                  */
/*  Function : プロフィール組み立て                                          */
/*                                                                           */
/*  Params   :                                                               */
/*  Returns  : '<table>...</table>'                                          */
/*                                                                           */
/*****************************************************************************/

function buildProfile( sName
                     , sPref
                     , sLocation
                     , sDDRBegin
                     , sRival
                     , sMixi
                     , sRSS
                     , sWeb
                     , sMail
                     , sIntroduction
                     )
{
  var sRivalLink , sLocationLink , sMixiLink , sWebLink , sMailLink , sTdSubject;
  var sProfile = '';
//  sMail = sMail.replace( '@' , '(atm@rk)' );

  sRivalLink = '<a href="' + g['sVersionRoot']
             + '/?act=profile&name=' + convertUnicode( sRival ) + '">' + sRival + '</a>';
  sLocationLink = '<a href="http://www.ddr-navi.jp/db/heroskin.cgi?table=ddr&search='
                + URLEncode( sLocation ) + '">' + sLocation  + '</a>';
//  sLocationLink = '<a href="http://ddr-navi.jp/db/heroskin.cgi?table=ddr&skin=skin.html&recpoint=0&mode=search&search='
//                + URLEncode( sLocation ) + '">' + sLocation  + '</a>';
  sMixiLink = '<a href="http://mixi.jp/show_friend.pl?id=' + sMixi + '">' + sMixi  + '</a>';
  sWebLink  = '<a href="'        + sWeb  + '">' + sWeb  + '</a>';
  sMailLink = '<a href="mailto:' + sMail + '">' + sMail + '</a>';

  if( sLocation     === '' ){ sLocationLink = '　'; }
  if( sDDRBegin     === '' ){ sDDRBegin     = '　'; }
  if( sRival        === '' ){ sRivalLink    = '　'; }
  if( sMixi         === '' ){ sMixiLink     = '　'; }
  if( sRSS          === '' ){ sRSS          = '　'; }
  if( sWeb          === '' ){ sWebLink      = '　'; }
  if( sMail         === '' ){ sMailLink     = '　'; }
  if( sIntroduction === '' ){ sIntroduction = '　'; }

  sTdSubject = '<td align="right" bgcolor="#e8e8e8">';

  sProfile = '<table border="1" cellspacing="0" cellpadding="5" bgcolor="#f8f8f8" style="font-size : 13px;">'
           + '<tr>' + sTdSubject + 'DANCER NAME</td><td>'     + sName         + '</td></tr>'
           + '<tr>' + sTdSubject + 'Area</td><td>'            + sPref         + '</td></tr>'
           + '<tr>' + sTdSubject + 'ロケーション</td><td>'    + sLocationLink + '</td></tr>'
           + '<tr>' + sTdSubject + '自己紹介</td><td>'        + sIntroduction + '</td></tr>'
           + '<tr>' + sTdSubject + 'DDRを始めた時期</td><td>' + sDDRBegin     + '</td></tr>'
           + '<tr>' + sTdSubject + 'ライバル登録</td><td>'    + sRivalLink    + '</td></tr>'
           + '<tr>' + sTdSubject + 'mixi ID</td><td>'         + sMixiLink     + '</td></tr>'
           + '<tr>' + sTdSubject + 'Webアドレス</td><td>'     + sWebLink      + '</td></tr>'
           + '<tr>' + sTdSubject + 'RSSアドレス</td><td>'     + sRSS          + '</td></tr>'
           + '<tr>' + sTdSubject + 'メールアドレス</td><td>'  + sMailLink     + '</td></tr>'
           + '</table>'
           ;

  return sProfile;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : getBrowserType                                                */
/*  Function : ブラウザ判定                                                  */
/*                                                                           */
/*  Params   :                                                               */
/*  Returns  : 0  不明                                                       */
/*             1  NN4                                                        */
/*             2  Opera（document.allも有効）                                */
/*             3  NS等                                                       */
/*             4  IE6                                                        */
/*                                                                           */
/*****************************************************************************/

function getBrowserType()
{
  if( window.opera )           { return 2; }
  if( document.all )           { return 4; }
  if( document.getElementById ){ return 3; }
  if( document.layers )        { return 1; }

  return 0;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : rewriteInnerHTML                                              */
/*  Function : innerHTMLの書き換え                                           */
/*                                                                           */
/*  Params   : 4                                                             */
/*             "profile"                                                     */
/*             '<table>...</table>'                                          */
/*  Returns  :                                                               */
/*                                                                           */
/*****************************************************************************/

function rewriteInnerHTML( dBrowserType , sId , sWrite )
{
  if( dBrowserType == 3 )
  {
    document.getElementById( sId ).innerHTML = sWrite;
  }

  if( dBrowserType == 4 )
  {
    document.all[ sId ].innerHTML = sWrite;
  }

  return;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : setDisplay                                                    */
/*  Function : 指定idのdisplay値を書き換え                                   */
/*                                                                           */
/*  Params   : ブラウザ種別                                                  */
/*             id                                                            */
/*             書き換える文字列                                              */
/*  Returns  :                                                               */
/*                                                                           */
/*****************************************************************************/

function setDisplay( dBrowserType , sId , sDisplay )
{
  // NN4とOperaは切り替え対象外
  if( dBrowserType <= 2 )
  {
    return;
  }
  
  if( dBrowserType == 3 )
  {
    document.getElementById( sId ).style.display = sDisplay;
  }

  if( dBrowserType == 4 )
  {
    document.all[ sId ].style.display = sDisplay;
  }

  return;
}

function URLEncode( sPlain )
{
  return EscapeSJIS( sPlain );
/*
  return encodeURI( sPlain );
/**/
/*
  var buf , buffer , i , query;

  buffer = sPlain.replace( /\?/ , '' );
  buffer = buffer.split( /&/ );

  buf = new Array();
  for( i in buffer )
  {
    query = buffer[i].split( /=/ );
    buf[ query[0] ] = query[1];
  }
  
  return buf[ "query" ];
/**/

}

/*****************************************************************************/
/*                                                                           */
/*  Name     : getURLParameter                                               */
/*  Function : URLパラメータの配列を取得                                     */
/*                                                                           */
/*  Params   :                                                               */
/*  Returns  :                                                               */
/*                                                                           */
/*****************************************************************************/

function getURLParameter()
{
  var i , ddsParameter , sQuery , dsQuery , dsKeyValue;

  ddsParameter = new Array();
  ddsParameter[0] = new Array();  // キー
  ddsParameter[1] = new Array();  // 値

  sQuery = location.search;
  if( sQuery == '' )
  {
    return ddsParameter;
  }
  
  dsQuery = sQuery.replace( '?' , '' ).split( '&' );
  for( i = 0 ; i < dsQuery.length ; i++ )
  {
    dsKeyValue = dsQuery[i].split( '=' );
    ddsParameter[0][i] = dsKeyValue[0];
    ddsParameter[1][i] = dsKeyValue[1];
  }

  return ddsParameter;
}

/*****************************************************************************/
/*                                                                           */
/*  Name     : buildPrefRanking                                              */
/*  Function : 県別ランキングの作成                                          */
/*                                                                           */
/*  Params   : '神奈川県'                                                    */
/*             dds[0]=new Array(1,'登録テスト','20324.69',81,82,82,33        */
/*                             ,82,82,82,32,'','',1149631601000,'青森県');   */
/*  Returns  : dds[0]=new Array(1,'登録テスト','20324.69',81,82,82,33        */
/*                             ,82,82,82,32,'','',1149631601000,'青森県');   */
/*                                                                           */
/*****************************************************************************/

function buildPrefRanking( sPref , ddsRankingAll )
{
  var i , dCount , ddsRanking , sPointBefore;
  
  ddsRanking = new Array();
  dCount = 0;
  sPointBefore = '';
  dNoBefore = -1;
  for( i = 0 ; i < ddsRankingAll.length ; i++ )
  {
    if( ddsRankingAll[i][14] == sPref )
    {
      ddsRanking[ dCount ] = ddsRankingAll[i];
      if( ddsRanking[ dCount ][2] != sPointBefore )
      {
//        ddsRanking[ dCount ][0] = dCount + 1;
        dNoBefore = dCount + 1;
        sPointBefore = ddsRanking[ dCount ][2];
      }
      else
      {
//        ddsRanking[ dCount ][0] = dNoBefore;
      }
      dCount++;
    }
  }
  
  return ddsRanking;
}

