//--------------------------------------
// 画面表示処理

//--------------------------------------
function displayData(responseHttpObj) {
    var text = responseHttpObj.responseText;
    $("THIS_DYNAMIC_AREA_TEXT").innerHTML = text;
}


//--------------------------------------
// Formに対して、Submit処理を行う
//
// [引数]
// id  … 対象のFormオブジェクトID
// url … 遷移先URL
//--------------------------------------
function doSubmit(id, url) {
    var form = $(id);
    form.action = url;
    form.method = "post";
    form.submit();

    return false;
}


//--------------------------------------
// 書誌検索条件画面に遷移する
//--------------------------------------
function transitionBookSearch() {
    // 著者名完全一致フラグ
    $("author_name_complete_agreement_flg").value = "";

    return doSubmit('main_form', './book_search.php');
}


//--------------------------------------
// 書誌一覧画面に遷移する
//
// [引数]
// displayPage … 何ページ目を表示するかの指定

//--------------------------------------
function transitionBookList(displayPage) {
    $("display_page").value = displayPage;
    return doSubmit('main_form', './book_list.php');
}


//--------------------------------------
// 書誌詳細画面に遷移する
//
// [引数]
// id … 詳細画面での表示対象の書誌ID
//--------------------------------------
function transitionBookDetail(id) {
    $("id").value = id;
    return doSubmit('main_form', './book_detail.php');
}


//--------------------------------------
// 親書誌詳細画面（子書誌一覧画面）に遷移する
//--------------------------------------
function transitionBookParentDetail(id) {
    $("id").value = id;
    return doSubmit('main_form', './book_detail_parent.php');
}


//--------------------------------------
// 入力項目の右から入力、左から入力を切り替える
//
// [引数]
// id … 対象のテキストボックスのID
// value …
//   左から右への入力の場合 … "ltr"
//   右から左への入力の場合 … "rtl"
//--------------------------------------
function changeDirection(id, value) {
    var styleName = "direction_" + value;
    $(id).className = styleName;
    $(id).focus();
}


//--------------------------------------
// 特殊文字を対象エレメントの末尾に追加する
//
// [引数]
// id    … 対象のtextareaのID
// value … 追加する特殊文字列
//--------------------------------------
function inputSpecialString(id, value) {
    // 簡易保存中は処理を行えない

    if ($("main_form").disabled == true) return false;

    $(id).value += value;
//    Field.activate(id);
    $(id).focus();
}


//--------------------------------------
//『自動生成機能』実行用関数（共通部分）

//
// [引数]
// index             … 自動生成元のindex
// toFieldId         … 自動生成先のフィールドID
// fromFieldName     … 自動生成元のフィールド名称
// toFieldName       … 自動生成先のフィールド名称
// toIndexDifference … 自動生成先のindexが、自動生成元のindexからいくつ先に存在しているか

//--------------------------------------
function conversionValueCommon(index, toFieldId, fromFieldName, toFieldName, toIndexDifference) {
    var targetValue = $("value" + index).value;

    if ($("field_id" + (index + toIndexDifference)).value == toFieldId) {
        // 確認メッセージボックス
        if (!confirm("『" + fromFieldName + "』 に入力された値から自動生成を行い、\n『" + toFieldName + "』 に値をセットします。\n\nよろしいですか？")) {
            return false;
        }

        var convertValue = conversionValue(targetValue);
        $("value" + (index + toIndexDifference)).value = convertValue;
        $("value" + (index + toIndexDifference)).focus();
        return true;

    } else {
        alert("『" + fromFieldName + "』 に入力された値から自動生成を行おうとしましたが、\n" + toIndexDifference + "つ下のフィールドが 『" + toFieldName + "』 ではありませんでした。\n\n自動生成処理を行うことができません。");
        $("value" + index).focus();
        return false;
    }
}


//--------------------------------------
//『自動生成機能』（引数の文字列を変換して返す）

//--------------------------------------
function conversionValue(targetValue) {
    var returnValue = targetValue;

    // 文頭の文字が "ال" の場合、削除する
    if (returnValue.substring(0, 2) == "ال") {
        returnValue = returnValue.substring(2, returnValue.length);
    }

    // 文頭の文字が "كتاب" の場合、削除する
    if (returnValue.substring(0, 4) == "كتاب") {
        returnValue = returnValue.substring(4, returnValue.length);
    }

    // "ʻ" は削除する
    returnValue = returnValue.replace(/ʻ/g, "");

    // "ʾ" は削除する
    returnValue = returnValue.replace(/ʾ/g, "");

    // "′" は削除する
    returnValue = returnValue.replace(/′/g, "");

    // "á" は "a" に置換する
    returnValue = returnValue.replace(/á/g, "a");

    // "à" は "a" に置換する
    returnValue = returnValue.replace(/à/g, "a");

    // "Ā" は "A" に置換する
    returnValue = returnValue.replace(/Ā/g, "A");

    // "ā" は "a" に置換する
    returnValue = returnValue.replace(/ā/g, "a");

    // "Ç" は "C" に置換する
    returnValue = returnValue.replace(/Ç/g, "C");

    // "ç" は "c" に置換する
    returnValue = returnValue.replace(/ç/g, "c");

    // "Ḍ" は "D" に置換する
    returnValue = returnValue.replace(/Ḍ/g, "D");

    // "ḍ" は "d" に置換する
    returnValue = returnValue.replace(/ḍ/g, "d");

    // "Ē" は "E" に置換する
    returnValue = returnValue.replace(/Ē/g, "E");

    // "ē" は "e" に置換する
    returnValue = returnValue.replace(/ē/g, "e");

    // "ğ" は "g" に置換する
    returnValue = returnValue.replace(/ğ/g, "g");

    // "Ḥ" は "H" に置換する
    returnValue = returnValue.replace(/Ḥ/g, "H");

    // "ḥ" は "h" に置換する
    returnValue = returnValue.replace(/ḥ/g, "h");

    // "ı" は "i" に置換する
    returnValue = returnValue.replace(/ı/g, "i");

    // "İ" は "I" に置換する
    returnValue = returnValue.replace(/İ/g, "I");

    // "Ī" は "I" に置換する
    returnValue = returnValue.replace(/Ī/g, "I");

    // "ī" は "i" に置換する
    returnValue = returnValue.replace(/ī/g, "i");

    // "Ḳ" は "K" に置換する
    returnValue = returnValue.replace(/Ḳ/g, "K");

    // "ḳ" は "k" に置換する
    returnValue = returnValue.replace(/ḳ/g, "k");

    // "Ō" は "O" に置換する
    returnValue = returnValue.replace(/Ō/g, "O");

    // "ō" は "o" に置換する
    returnValue = returnValue.replace(/ō/g, "o");

    // "Ş" は "S" に置換する
    returnValue = returnValue.replace(/Ş/g, "S");

    // "ş" は "s" に置換する
    returnValue = returnValue.replace(/ş/g, "s");

    // "Ṣ" は "S" に置換する
    returnValue = returnValue.replace(/Ṣ/g, "S");

    // "ṣ" は "s" に置換する
    returnValue = returnValue.replace(/ṣ/g, "s");

    // "Ṭ" は "T" に置換する
    returnValue = returnValue.replace(/Ṭ/g, "T");

    // "ṭ" は "t" に置換する
    returnValue = returnValue.replace(/ṭ/g, "t");

    // "Ū" は "U" に置換する
    returnValue = returnValue.replace(/Ū/g, "U");

    // "ū" は "u" に置換する
    returnValue = returnValue.replace(/ū/g, "u");

    // "Ẓ" は "Z" に置換する
    returnValue = returnValue.replace(/Ẓ/g, "Z");

    // "ẓ" は "z" に置換する
    returnValue = returnValue.replace(/ẓ/g, "z");

    // "أ" は "ا" に置換する
    returnValue = returnValue.replace(/أ/g, "ا");

    // "إ" は "ا" に置換する
    returnValue = returnValue.replace(/إ/g, "ا");

    // "ی" は "ي" に置換する
    returnValue = returnValue.replace(/ی/g, "ي");

    // "ک" は "ك" に置換する
    returnValue = returnValue.replace(/ک/g, "ك");

    // "à" は "a" に置換する
    returnValue = returnValue.replace(/à/g, "a");

    // "è" は "e" に置換する
    returnValue = returnValue.replace(/è/g, "e");

    // "È" は "E" に置換する
    returnValue = returnValue.replace(/È/g, "E");

    // "É" は "E" に置換する
    returnValue = returnValue.replace(/É/g, "E");

    // "é" は "e" に置換する
    returnValue = returnValue.replace(/é/g, "e");

    // "ï" は "i" に置換する
    returnValue = returnValue.replace(/ï/g, "i");

    // "ö" は "o" に置換する
    returnValue = returnValue.replace(/ö/g, "o");

    // "Ö" は "O" に置換する
    returnValue = returnValue.replace(/Ö/g, "O");

    // "ü" は "u" に置換する
    returnValue = returnValue.replace(/ü/g, "u");

    // "Ü" は "U" に置換する
    returnValue = returnValue.replace(/Ü/g, "U");

    // "ḳ" は "k" に置換する
    returnValue = returnValue.replace(/ḳ/g, "k");

    // "Ḳ" は "K" に置換する
    returnValue = returnValue.replace(/Ḳ/g, "K");

    return returnValue;
}
