//******************************************************************************
// モジュール名：jigmy_meishi.js
// 概要：名刺用のJavaScript
// 作成者：Shinya Iwasaki
// 作成日：2008/10/27
// バージョン：1.0
// 修正履歴：
//
//******************************************************************************

//******************************************************************************
// 一時トレイ関連処理
//******************************************************************************
//******************************************************************************
// 処理概要：
//      Ajaxの処理正常終了ハンドラーです。
//      指定されたIDに処理結果を反映します。
// 引数：
//      request リクエスト
//      json JSON
// 返却値：
//      n/a
//******************************************************************************
function tempTrayCompleteHandler(request, json) {
    $('temporary_tray').innerHTML = request.responseText;
    if (json.JSESSIONID != null) {
        document.cookie = "JSESSIONID=" + json.JSESSIONID + "; path=/";
    }
}
//******************************************************************************
// 処理概要：
//      Ajaxの処理以上終了ハンドラーです。
// 引数：
//      request リクエスト
// 返却値：
//      n/a
//******************************************************************************
function tempTrayFailureHandler(request) {
    //alert("Ajax Error");
}
//******************************************************************************
// 処理概要：
//      一時トレイ追加処理を行います。
//      カタログコードが未指定の場合、一時トレイのロード処理のみを行います。
// 引数：
//      categoryCd カテゴリーコード
//      catalogCd カタログコード
//      faceTypeIdx 印刷面タイプインデックス
//      os 注文セッションID
// 返却値：
//      n/a
//******************************************************************************
function addToTray(categoryCd, catalogCd, faceTypeIdx, os) {

    var url = "/meishi/addToTray";

    var myAjax = new Ajax.Request(
        url,
        {
                method: 'post',
                parameters: 'category=' + encodeURIComponent(categoryCd) + '&cat=' + encodeURIComponent(catalogCd) + '&faceTypeIdx=' + encodeURIComponent(faceTypeIdx) + '&os=' + encodeURIComponent(os),
                onComplete: tempTrayCompleteHandler
        }
    );

}
//******************************************************************************
// 処理概要：
//      一時トレイ削除処理を行います。
//
// 引数：
//      index 削除対象インデックス
//      faceTypeIdx 印刷面タイプインデックス
//      os 注文セッションID
// 返却値：
//      n/a
//******************************************************************************
function deleteFromTray(index, faceTypeIdx, os) {

    var url = "/meishi/deleteFromTray";

    var myAjax = new Ajax.Request(
        url,
        {
            method: 'post',
            parameters: 'index=' + encodeURIComponent(index) + '&faceTypeIdx=' + encodeURIComponent(faceTypeIdx) + '&os=' + encodeURIComponent(os),
            onComplete: tempTrayCompleteHandler,
            onFailure: tempTrayFailureHandler
        }
    );
}
//******************************************************************************
// 処理概要：
//      一時トレイ全削除処理を行います。
//
// 引数：
//      faceTypeIdx 印刷面タイプインデックス
//      os 注文セッションID
// 返却値：
//      n/a
//******************************************************************************
function deleteAllFromTray(faceTypeIdx, os) {

    var url = "/meishi/deleteAllFromTray";

    var myAjax = new Ajax.Request(
        url,
        {
                method: 'post',
                parameters: 'faceTypeIdx=' + encodeURIComponent(faceTypeIdx) + '&os=' + encodeURIComponent(os),
                onComplete: tempTrayCompleteHandler,
                onFailure: tempTrayFailureHandler
        }
    );
}

//******************************************************************************
// 住所検索関連処理
//******************************************************************************
//******************************************************************************
// グローバル変数
//******************************************************************************
var address1;   // 検索結果を表示する住所

//******************************************************************************
// 処理概要：
//      住所検索処理成功時のハンドラーです。
// 引数：
//      request HTTPリクエスト
// 返却値：
//      n/a
//******************************************************************************
function searchAddressCompleteHandler(request) {
    if (200 == request.status) {
        address1.value = request.responseText;
    } else {
        alert("サーバと通信できません。");
    }
    address1 = null;
}
//******************************************************************************
// 処理概要：
//      住所検索処理失敗時のハンドラーです。
// 引数：
//      request HTTPリクエスト
// 返却値：
//      n/a
//******************************************************************************
function searchAddressFailureHandler(request) {
    address1.value = "";
    address1 = null;
}

//******************************************************************************
// 特殊文字入力関連処理
//******************************************************************************
//******************************************************************************
// グローバル変数
//******************************************************************************
var gaijiScreen;    // 特殊文字入力画面
var focusedField;   // 特殊文字入力箇所の名称

//******************************************************************************
// 処理概要：
//      特殊文字入力項目名を保存します。
// 引数：
//      item 特殊文字入力項目
// 返却値：
//      n/a
//******************************************************************************
function catchFocus(item) {
    if (null == item) {
        focusedField = null;
    } else {
        focusedField = item.name;
    }
}
//******************************************************************************
// 処理概要：
//      特殊文字入力画面を表示します。
// 引数：
//      actionPath 特殊文字入力画面を開くアクションパス
// 返却値：
//      n/a
//******************************************************************************
function openGaijiScreen(actionPath) {
    if (null == focusedField) {
        alert("特殊文字を入力する入力欄をクリックしてから\nこのボタンを押してください");
    } else {
        var wx = 660;
        var wy = 700;
        var x = (screen.width - wx) / 2;
        var y = (screen.height - wy) / 2;
        gaijiScreen = window.open(actionPath,'gaiji',"resizable=yes,scrollbars=yes,left="+x+",top="+y+",width="+wx+",height="+wy);
        gaijiScreen.focus();
    }
}
//******************************************************************************
// 処理概要：
//      特殊文字入力項目にコード値を設定して、特殊文字入力画面を閉じます。
// 引数：
//      code 特殊文字コード値
// 返却値：
//      n/a
//******************************************************************************
function setCode(code) {
    var target = window.opener.focusedField;
    if (null == target) {
        alert("特殊文字を入力する入力欄をクリックしてから\n特殊文字を選択してください");
    } else {
        if (null != window.opener && !window.opener.closed) {
            var code = "{" + code + "}";
            window.opener.document.forms[0][target].value += code;
        }
        window.close();
    }
}
//******************************************************************************
// 処理概要：
//      特殊文字入力画面を閉じます。
// 引数：
//      n/a
// 返却値：
//      n/a
//******************************************************************************
function closeGaijiScreen() {
    if (null != gaijiScreen && !gaijiScreen.closed) {
        gaijiScreen.close();
    }
}

//******************************************************************************
// Flashアプリケーション関連処理
//******************************************************************************
//******************************************************************************
// グローバル変数
//******************************************************************************
var flashAppScreen; // レイアウト自由編集画面
var flashAppOpnnerForm; // Flashアプリケーション起動元フォーム
var flashAppFaceTypeIdx; // 印刷面タイプインデックス

//******************************************************************************
// 処理概要：
//      レイアウト自由編集画面を開きます。
// 引数：
//      faceTypeIdx 印刷面タイプインデックス
//      system システム識別子
//      service サービス識別子
//      version Flashアプリケーションバージョン
//      uss ユーザセッション文字列
//      form Flashアプリケーション起動元フォーム
//
// 返却値：
//      n/a
//******************************************************************************
function openFlashAppScreen(faceTypeIdx, system, service, version, uss, form) {
    if (null == flashAppScreen || flashAppScreen.closed) {
        var wx = 1100;
        var wy = 800;
        var x = (screen.width - wx) / 2;
        var y = (screen.height - wy) / 2;
        var autoCompId2;
        if("0" == faceTypeIdx) {
            autoCompId2 = form.fdAutoCompId2.value;
        } else {
            autoCompId2 = form.bdAutoCompId2.value;
        }
        flashAppScreen = window.open("bc170view?system="+system+"&service="+service+"&version="+version+"&svg="+autoCompId2,"fix","resizable=yes,scrollbars=yes,left="+x+",top="+y+",width="+wx+",height="+wy);
        flashAppOpnnerForm = form;
        flashAppFaceTypeIdx = faceTypeIdx;
    } else {
        if (flashAppFaceTypeIdx != faceTypeIdx) {
            alert("すでに別のレイアウト自由編集画面が開かれています。\n編集中の画面を完了させてからこのボタンを押してください。");
        }
    }
    flashAppScreen.focus();
}

//******************************************************************************
// 処理概要：
//      レイアウト自由編集結果反映処理を行います。
// 引数：
//      autoCompId 編集後の組版管理ID
//
// 返却値：
//      n/a
//******************************************************************************
function updateImage(autoCompId){
    if("0" == flashAppFaceTypeIdx) {
        flashAppOpnnerForm.fdAutoCompId2.value = autoCompId;
    } else {
        flashAppOpnnerForm.bdAutoCompId2.value = autoCompId;
    }
    flashAppOpnnerForm.action = "/meishi/sv/bc130view";
    flashAppOpnnerForm.submit();
}
//******************************************************************************
// 処理概要：
//      レイアウト自由編集画面を閉じます。
// 引数：
//      n/a
// 返却値：
//      n/a
//******************************************************************************
function closeFlashAppScreen() {
    if (null != flashAppScreen && !flashAppScreen.closed) {
        flashAppScreen.close();
        flashAppScreen = null;
        flashAppOpnnerForm = null;
        flashAppFaceTypeIdx = null;
    }
}
//******************************************************************************
// 処理概要：
//      レイアウト自由編集画面を閉じる際の確認処理を行います。
// 引数：
//      n/a
// 返却値：
//      true:画面を閉じた場合（既に閉じている場合）／false:確認でキャンセルされた場合
//******************************************************************************
function confirmFlashAppScreenClosed() {
    if (null != flashAppScreen && !flashAppScreen.closed) {
        var ret = confirm("レイアウトの自由編集を取り消して、他の画面に切り換えてもよろしいですか。\n（レイアウト編集ウィンドウでの編集結果は失われます）");
        if (ret) {
            flashAppScreen.close();
            return true;
        }else{
            return false;
        }
    } else {
        return true;
    }
}

//******************************************************************************
// 画面UI部品関連処理
//******************************************************************************
//******************************************************************************
// 処理概要：
//      郵便番号の入力妥当性検査を行います。
// 引数：
//      zipCode 検査対象の郵便番号
// 返却値：
//      true:妥当な場合／false:妥当でない場合
//******************************************************************************
function validateZipcode(zipcode) {
    return zipcode.match(/^\d{3}[-‐ー－―]\d{4}$/);
}
//******************************************************************************
// 処理概要：
//      レイアウト拡大表示画面（商品詳細）を開きます。
// 引数：
//      categoryCd カタログコード
//      catalogCd カタログコード
//      os 注文セッションID
//      step 注文ステップ番号
// 返却値：
//      n/a
//******************************************************************************
function openEnlargedDesign(categoryCd, catalogCd, os, step) {
    var wx = 850;
    var wy = screen.availHeight * 0.9;
    var x = (screen.width - wx) / 2;
    var y = (screen.height - wy) / 2;
    var url = "bc160view" + "?category=" + categoryCd + "&cat=" + catalogCd + "&os=" + os + "&step=" + step;
    var bc160 = window.open(url,'BC160',"resizable=yes,scrollbars=yes,left="+x+",top="+y+",width="+wx+",height="+wy);
    bc160.focus();
}

//******************************************************************************
// 処理概要：
//      レイアウト拡大表示画面（プレビュー）を開きます。
// 引数：
//      os 注文セッションID
//      faceTypeIdx 印刷面タイプインデックス
// 返却値：
//      n/a
//******************************************************************************
function openEnlargedPreview(os, faceTypeIdx) {
    wx = 700;
    wy = 750;
    x = (screen.width - wx) / 2;
    y = (screen.height - wy) / 2;
    var url = "bc161view" + "?os=" + os + "&faceTypeIdx=" + faceTypeIdx;
    var bc161 = window.open(url,'BC161',"resizable=yes,scrollbars=yes,left="+x+",top="+y+",width="+wx+",height="+wy);
    bc161.focus();
}
