writeGameLogin("EC","エターナルカオスNEO","JP","600051J4");

var contents_id            = 20;
var downloadPage           = '/start/download.aspx';
var MaintenanceStatusPage  = '/inc/parts/gamestart/MaintenanceStatus.aspx';
var ActiveXPage            = 'http://www.gamania.co.jp/login/activex.aspx?id=20';
// ☆☆☆☆☆ 2011/11/14 UpdateStart ☆☆☆☆☆
//var LoginPage              = 'https://gash.gamania.co.jp/login/index.aspx';
var LoginPage = '/partnerAct/login_count.aspx';
// ☆☆☆☆☆ 2011/11/14 UpdateEnd   ☆☆☆☆☆
var LogoutPage             = 'http://www.gamania.co.jp/error/logout.aspx';
var cookieDomain           = ';domain=.gamania.co.jp;';
var ContentsName           = "エターナルカオスNEO";

var GameMaintenancePage    = '/error/GameMaintenance.aspx';
var exec                   = '';

// ゲームスタート
function OnGameStart() {

  // 連打制御用
  if(exec == true){ return false; };
  exec = true;

  // 60秒後に有効化
  setTimeout('execEffective()',60*1000);

  if ( getMaintenanceInfo() ) {
    location.href = GameMaintenancePage ;
    return false;
  }

  OnGameStart2();
  return false;
}

// メンテナンス情報
function getMaintenanceInfo () {

  try {
    var http = new JKL.ParseXML.Text( MaintenanceStatusPage );
    var data = http.parse();
    if ( data.toString() != '1' ) {
      return true;
    }

  } catch ( e ) {
  }
  return false;
}

function OnGameStart2() {

  //1. ログイン確認（cookieの確認）
  if(!CookieReadChild("Gamania.MyGASH", "MainID"))
  {
    deleteCookie();
    // 戻りURL
    document.cookie = "Gamania.OldPosition=" + document.URL + cookieDomain;
    // ログインページに遷移
    location.href = LoginPage;
    return false;
  }
  else
  {
    GashAccount = CookieReadChild("Gamania.MyGASH", "MainID")
  }

  // ☆☆☆☆☆ 2011/11/14 AddStart ☆☆☆☆☆
  $.get("/partneract/start_count.aspx");
  // ☆☆☆☆☆ 2011/11/14 AddEnd   ☆☆☆☆☆


  //2. ActiveX存在確認
  try {
    // アクティブXのバージョン取得 （存在しない場合例外発生）
    var ActivexVersion = lcjggame.GetVersion();

    // バージョンチェック (2.0.1.6) add 2010/08/30 higashi
    if(ActivexVersion == "undefined" || ActivexVersion != 0x140A080A)
    {
      alert("ActiveXが古い可能性があります。");
      // ActiveXインストールページに遷移
      location.href = ActiveXPage;
      return false;
    }
  } catch ( e ) {
    // ActiveXインストールページに遷移
    location.href = ActiveXPage;
    return false;
  }

  //3. GameIDの存在確認
  serviceCode = "service" + contents_id

  if(!CookieReadChild("GameStart", serviceCode))
  {
    // クッキーに値がない
    location.href="/index.aspx";
    return false;
  }
  startActiveX();
  return false;
}


// クッキーの値取得
function CookieRead(kword)
{
  if(typeof(kword) == "undefined")      // キーワードなし
    return "";                          // 何もしないで戻る
  kword = kword + "=";
  kdata = "";
  scookie = document.cookie + ";";  // クッキー情報を読み込む
  start = scookie.indexOf(kword);   // キーワードを検索
  if (start != -1){    // キーワードと一致するものあり
    end = scookie.indexOf(";", start);    // 情報の末尾位置を検索
    kdata = unescape(scookie.substring(start + kword.length, end));  // データ取り出し
  }
  return kdata;
}

function CookieReadChild(kword1, kword2)
{
  // キーワードなし
  if(typeof(kword1) == "undefined" || typeof(kword2) == "undefined")
    return "";                          // 何もしないで戻る

    value = CookieRead(kword1);
    start = value.indexOf(kword2);   // キーワードを検索
    kword2 = kword2 + "=";

    if (start != -1){    // キーワードと一致するものあり
      end = value.indexOf("&", start);    // 情報の末尾位置を検索
      if(end == -1){
        end = value.length;
      }
      startset = start + kword2.length;
      kdata = unescape(value.substring(startset, end));  // データ取り出し
    }
    else
    {
      return "";
    }

  return kdata;
}

// ゲームスタート
function startActiveX () {
    //4. クライアント存在確認
    // GameAccountとKeyCodeを渡す、200が帰ってきたら存在なし
    // 存在なし：コンテンツダウンロードへ遷移する

    try {

      // AddStart
      var startcode  = CookieReadChild("GameStart", serviceCode);
      // AddEnd

      var ret = lcjggame.StartClick(startcode, startcode)

      // add 2009/10/29 higashi
      if(ret == "undefined")
      {
        alert("ActiveXが古い可能性があります。");
        // ActiveXインストールページに遷移
        location.href = ActiveXPage + "?id=" + contents_id;
        return false;
      }

      if(ret == '200')
      {
        // コンテンツダウンロードへ遷移
        alert( "『" + ContentsName + "がインストールされていません。』" );
        location.href = downloadPage;
        return false;
      }
//      // 2010/09/23 Add
//      else if(ret == '004')
//      { 
//
//        deleteCookie();
//        location.reload();
//      }
      else if(ret != '999')
      {
        deleteCookie();
        // エラーページ
        // 0パディング
        ret = ("00" + ret).slice(-3)
        document.cookie = "Gamania.OldPosition=" + document.URL + cookieDomain;
        location.href   = LogoutPage + "?id=X" + ret + "&cid=" + contents_id;
      }
    } catch(e) {
      // 例外発生
      // エラーページ
      document.cookie = "Gamania.OldPosition=" + document.URL + cookieDomain;
      location.href   = LogoutPage + "?id=GS99";
      return false;
    }
}

// クッキーの削除
function deleteCookie() {
  cName = "GameStart="; // 削除するクッキー名
  dTime = new Date();
  dTime.setYear(dTime.getYear() - 1);
  document.cookie = cName + cookieDomain + "expires=" + dTime.toGMTString();
}

function execEffective() {
  exec = false;
}

