﻿
//*****************************************************************
//実行ファイル類を配置してあるサーバー
var SaverPath ="http://www.yuhikaku.co.jp/ltfapp/";


//ボタン名
var ButtonName = "LTRW_ReadButton";

//確認ウィンドウ名
var MessageWindow = SaverPath + "MessageWindow.htm";
//.Netのインストールウィンドウ名
var NETInstallWindow = SaverPath + "NETInstallWindow.htm";
//.Netのアプリケーション
var NetApplication = SaverPath + "netfx30/dotnetfx3setup.exe";

//cookieのキー
//有効期限(日付)
var dayKey = 365;
//ウィンドウの表示の有無
var windowKey = "LTRW_WindowShow";
//インストールを実行するか有無
var isInstallKey = "LTRW_IsInstall";

//呼び出すアプリケーション
var LTReaderApplication = SaverPath + "LTReaderWeb.application";
var UrlParam = "?url=";

//NETのバージョン
var NetVersion = "3.0.0";


//*****************************************************************
//初期化
// <body onload="LTRW_Initialize()">
//*****************************************************************
function LTRW_Initialize()
{
  //対象のボタンリストを取得
  var ele = document.getElementsByName( ButtonName );
  for (var i=0; i<ele.length; i++) {
    //イベントを追加  
    //IEは呼出順が通常と逆
    if( HasRuntimeVersion( NetVersion ) ){
      if (LTRW_IsExplorer()) {
        //IE
        append_event(ele[i], appExecute);
        append_event(ele[i], messagenWindow);
      }else{
        ele[i].style.visibility = "hidden";
//        append_event(ele[i], messagenWindow);
//        append_event(ele[i], appExecute);
      }
    }else{
      if (LTRW_IsExplorer()) {
        append_event(ele[i], netInstallWindow);
      }else{
        ele[i].style.visibility = "hidden";
      }
    }
  } 
  
}

//*****************************************************************
//ウィンドウの表示
//*****************************************************************
function messagenWindow(){

  var ret=0;
  
  //フラグがなかったらウィンドウ表示
  if ( CookieRead( windowKey ) != 1 ){
    // OKボタン:1　キャンセルボタン:0
    ret = window.showModalDialog(
            MessageWindow,
            "",
            "dialogWidth:300px;dialogHeight:250px;resizable:no;status:no;scroll:yes;unadorned:no");
  }else{
    ret = 1;
  }

  // OK/Cancelの状態を保存  
  CookieWrite(isInstallKey,ret);
}

//*****************************************************************
// インストールファイルを実行する
//*****************************************************************
function appExecute()
{
  if ( CookieRead( isInstallKey ) == 1 ){
  //クリックされたボタンの情報を取得
  var id = event.srcElement.id;
  var name = event.srcElement.name;
  //+1は「-」分
    var value = id.substring( name.length+1, id.length );
    
    //クリックされたボタンの情報を取得
    if( LTRW_IsExplorer() ){
      id = event.srcElement.id;
      name = event.srcElement.name;
      //+1は「-」分
      value = id.substring( name.length+1, id.length );
    }else{
      id = this.id;
      name = this.name;
      //+1は「-」分
      value = id.substr( name.length+1, id.length );
    }
    href = LTReaderApplication + UrlParam + value;
    
    // applicationをインストール
    location.href = href;
  }
}

//*****************************************************************
// OKボタン
//*****************************************************************
function LTRW_OK(){
  CookieWrite( windowKey,1);
  
  window.returnValue = 1;
  window.close();
}

//*****************************************************************
// キャンセルボタン
//*****************************************************************
function LTRW_Cancel(){
  window.returnValue = 0;
  window.close();
}

//*****************************************************************
// インストールボタン
//*****************************************************************
function LTRW_Install(){
  window.returnValue = 1;
  window.close();
}

//*****************************************************************
// イベントの追加(IEのみ対応)
//  引数：e=エレメント
//  引数：type=イベント名
//  引数：handler=呼出す関数名
//  返却値：なし
//*****************************************************************
//
function append_event(e, handler) {
  if ( LTRW_IsExplorer() ) { 
    //IE
    e.attachEvent('onclick', handler);
  } else { 
    //Netscape等
    e.addEventListener('click', handler, false);
  }
}

//*****************************************************************
// cookie キーの登録
//  引数：kword=キーワード
//  引数：value=データ
//  返却値：なし
//***************************************************************** 
function CookieWrite( kword ,value)
{
  var setDay = new Date();
  setDay.setTime( setDay.getTime() + (dayKey * 1000 * 60 * 60 * 24) );   
  var expDay = setDay.toGMTString();
　document.cookie = kword + "=" + escape(value) + ";expires=" + expDay;
}

//*****************************************************************
// cookie キーの取得
//  引数：kword=キーワード
//  返却値：データ
//*****************************************************************
function CookieRead( kword )
{
  // キーワードなし
  if( typeof(kword) == "undefined" ){
    // 何もしないで戻る
    return "";        
  }
  kword = kword + "=";
  var data = "";
  // クッキー情報を読み込む
  var cookie = document.cookie + ";";
  // キーワードを検索　
  var start = cookie.indexOf(kword);   　
  if (start != -1){
    // キーワードと一致するものあり

    // 情報の末尾位置を検索
    end = cookie.indexOf(";", start);
    // データ取り出し
    data = unescape(cookie.substring(start + kword.length, end));
  }
  
 return data;
}

//*****************************************************************
// .Net3.0インストール
// 
//*****************************************************************
function  netInstallWindow()
{
  
    var ret = window.showModalDialog(
            NETInstallWindow,
            "",
            "dialogWidth:300px;dialogHeight:200px;resizable:yes;status:no;scroll:yes;unadorned:no");
            
    //インストール        
    if( ret )
    {
        location.href = NetApplication;
    }
}

//*****************************************************************
// .NETのバージョンチェック
//  引数：v=バージョン
//  返却値：true/false
//
//　注意：.NETはインストール後、IEを立上げ直さないとインストールした
//　　　　バージョンが取得できません。
//*****************************************************************
function HasRuntimeVersion(v)
{
  var va = GetVersion(v);
  var i;
  var a = navigator.userAgent.match(/\.NET CLR [0-9.]+/g);
  if (a != null){
    for (i = 0; i < a.length; ++i){
      if (CompareVersions(va, GetVersion(a[i])) <= 0)
        return true;
    }
  }
  return false;
}

//*****************************************************************
// 先頭のバージョンを返す
//  引数：v=バージョン
//  返却値：バージョンの数値
//*****************************************************************
function GetVersion(v)
{
  var a = v.match(/([0-9]+)\.([0-9]+)\.([0-9]+)/i);
  return a.slice(1);
}

//*****************************************************************
// バージョンを比較する
//  引数：v1=比較元
//  引数：v2=比較先
//  返却値：比較元が新しい場合true
//*****************************************************************
function CompareVersions(v1, v2)
{
  for (i = 0; i < v1.length; ++i)
  {
    var n1 = new Number(v1[i]);
    var n2 = new Number(v2[i]);
    if (n1 < n2)
      return -1;
    if (n1 > n2)
      return 1;
    }
  return 0;
}

//*****************************************************************
//  Internet Explorerか
//
//*****************************************************************
function LTRW_IsExplorer()
{
  var uName = navigator.userAgent.toUpperCase();
  if( uName.indexOf("MSIE") != -1) {
    return 1;
  }

  return 0;
}
