﻿/* チェック */
function InputCheck() {
    //var str = document.iform.AgeText.value;
    var str = $('#txtFreeWord').val();
    if (str.match(/[^0-9ぁ-んァ-ンA-Za-z亜-煕　\s.,-]+/)) {
        return 1;
    }
    return 0;
}

//地図FlashからのPOST用
function JsPostBack(v1, v2, v3) {
    //コンテンツDIVを非表示にする（地図を即座に非表示にする）
    $('#contents').addClass("init");
    $('#select1').val(v1);
    $('#select2').val(v2);
    $('#select3').val(v3);
    document.forms[0].submit();
    return false;
}

//クリアフラグセット
function SetClearFlag(flag) {
    $('#clear').val(flag);
}

//入力文字チェック
function CheckInputString(InputString) {
    if (InputString.match(/<[A-Za-z]/)) {
        return false;
    }
    else {
        return true;
    }
}

function sleep(msec) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while (curDate - date < msec);
}    

//Ready
$(function() {

    //Submit時にフリーワードにタグが入力されている場合は削除
    //    $("form").submit(function() {
    //        var str;
    //        str = $('.Search').children('input:text').val();
    //        if (CheckInputString(str) == false) {
    //            $('.Search').children('input:text').val("");
    //        }
    //    });

    //Rootタイトルにチェックがついている場合、配下すべてにチェックを付ける（for Category-2）
    var boxdiv = $('div.RootTitle input:checkbox:checked').parent().next().children('div.BasicBox');
    boxdiv.children('div.Layer2Title').children('input:checkbox').attr('checked', true);
    boxdiv.children('div.LayerBody').children('div.FloatCellType').children('input:checkbox').attr('checked', true);
    boxdiv.children('div.LayerBody').children('div.Layer3Title').children('input:checkbox').attr('checked', true);
    boxdiv.children('div.LayerBody').children('div.LayerBody').children('div.GovDepFloatCell').children('input:checkbox').attr('checked', true);

    boxdiv = $('div.RootTitle input:checkbox:not(:checked)').parent().next().children('div.BasicBox');
    var boxdivL2 = boxdiv.children('div.Layer2Title').children('input:checkbox:checked').parent().next();
    boxdivL2.children('div.FloatCellType').children('input:checkbox').attr('checked', true);
    boxdivL2.children('div.Layer3Title').children('input:checkbox').attr('checked', true);
    boxdivL2.children('div.LayerBody').children('div.GovDepFloatCell').children('input:checkbox').attr('checked', true);

    boxdivL2 = boxdiv.children('div.Layer2Title').children('input:checkbox:not(:checked)').parent().next();
    var boxdivL3 = boxdivL2.children('div.Layer3Title').children('input:checkbox:checked').parent().next();
    boxdivL3.children('div.GovDepFloatCell').children('input:checkbox').attr('checked', true);

    //チェックがついている場合、配下すべてにチェックを付ける（for Category-3）
    var Cat3box = $('#Category3RootBody').children('div.BasicBox');
    var CheckedBody = Cat3box.children('div.Layer2Title').children('input:checkbox:checked').parent().next();
    CheckedBody.children('div.FloatCellType').children('input:checkbox').attr('checked', true);

    //選択状態があるかチェックする
    var basebox = $('div.BasicBox');
    ///（Cat2:県庁／市町村、Cat3向け）
    var L2TitleNoCheck = basebox.children('div.Layer2Title').children('input:checkbox:not(:checked)').parent().parent().children('div.LayerBody').children('div.FloatCellType');
    if (L2TitleNoCheck.children('input:checkbox:checked').length > 0) {
        L2TitleNoCheck.children('input:checkbox:checked').parent().parent().prev().children('span.selected').text("　選択されているものがあります　");
    }
    ///（Cat2：国・独立行政法人等の下層）
    var L3TitleNoCheck = basebox.children('div.Layer2Title').children('input:checkbox:not(:checked)').parent().parent().children('div.LayerBody').children('div.Layer3Title');
    if (L3TitleNoCheck.children('input:checkbox:checked').length > 0) {
        L3TitleNoCheck.children('input:checkbox:checked').parent().parent().prev().children('span.selected').text("　選択されているものがあります　");
    }
    var L4ItemNoCheck = L3TitleNoCheck.children('input:checkbox:not(:checked)').parent().next().children('div.FloatCellType');
    if (L4ItemNoCheck.children('input:checkbox:checked').length > 0) {
        if (L4ItemNoCheck.children('input:checkbox:not(:checked)').length != 0) {
            L4ItemNoCheck.children('input:checkbox:checked').parent().parent().prev().children('span.selected').text("　選択されているものがあります　");
            L4ItemNoCheck.children('input:checkbox:checked').parent().parent().parent().prev().children('span.selected').text("　選択されているものがあります　");
        }
    }

    //選択されていない地域は非表示にする
    $('div#wrap1[value="no"]').hide(0, false);
    $('div#wrap2[value="no"]').hide(0, false);
    $('div#wrap3[value="no"]').hide(0, false);

    $('#contents').removeClass("init");

    //エラーメッセージを非表示にしておく
    $('.error').hide(0, false);

    //クリアボタン押下処理
    //もっといいセレクタの指定方法があるのでは？
    $('div.Search input:button').click(function() {
        $('#Category3RootTitle input:checkbox').attr('checked', false);
        $('#Category3RootBody').children('Div.BasicBox').children('Div.Layer2Title').children('input:checkbox').attr('checked', false);
        $('#Category3RootBody').children('Div.BasicBox').children('Div.LayerBody').children('Div.FloatCellType').children('input:CheckBox').attr('checked', false);
        $('div.Search input:text').val("");
        SetClearFlag("1");
        //ページへ移動
        JsPostBack("", "", "");
    });

    //検索実行時にチェックボックスとテキストボックスの入力状態をチェック
    $('input:submit').click(function() {
        var checkA;
        var checkB;
        var checkC;
        var errstring;
        //チェックボックスチェック
        if ($('input:checkbox:checked').length <= 0) {
            checkA = false;
            errstring = "検索したい文言を入力するか、チェックボックスを少なくとも1つは選択してください";
        }
        //テキストボックスチェック
        var str = $('.Search').children('input:text').val();
        str = str.replace(/[　\s]/, " ", "g");
        str = jQuery.trim(str);
        if (str.length == 0) {
            checkB = false;
            errstring = "検索したい文言を入力するか、チェックボックスを少なくとも1つは選択してください";
        }

        str = $('.Search').children('input:text').val();
        if (CheckInputString(str) == false) {
            checkC = false;
            errstring = "フリーワードにタグ形式は入力できません";
        }

        if (checkA == false && (checkB == false || checkC == false)) {
            alert(errstring);
            return false;
        }
        else if (checkC == false) {
            alert(errstring);
            return false;
        }
        //$('.wrap').hide(0, false);
        $('#form1').hide(0, false);
        $('div.LockOff').addClass("LockOn");

    });

    //上と下のフリーワード用テキストボックスを同期する
    $('.SearchText').change(function() {
        $('.SearchText').val($(this).val());
    });

    //フリーワード用テキストボックスのFocus時、Blur時の処理
    $('.SearchText')
        .focus(function() {
            //$('.SearchText').addClass('SearchValidateErr');
        })

        .blur(function() {
            //            var str; //PostBack時の入力文字チェック用

            //            str = $('.Search').children('input:text').val();
            //            if (CheckInputString(str) == false) {
            //                $('.error').removeClass("errorshow");
            //                $('.error').text("フリーワードにタグ形式は入力できません");
            //                $('.error').show(0, false);
            //                $('.SearchText').addClass("SearchValidateErr");
            //                $('.Search').children('input:text').focus();
            //                return false;
            //            }
            //            else {
            //                $('.error').addClass("errorshow");
            //                $('.error').text("");
            //                $('.error').hide(0, false);
            //                $('.SearchText').removeClass("SearchValidateErr");
            //            }
        });

    //check all on/off-----------------------------------------

    //○カテゴリーRootタイトルクリック
    $('Div.RootTitle input:Checkbox').click(function() {
        var base;
        base = $(this).parent().next().children('Div.BasicBox');
        //カテゴリー3内部タイトルのチェック制御
        base.children('Div.Layer2Title').children('input:checkbox').attr('checked', $(this).is(':checked'));
        base.children('Div.Layer2Title').children('span.selected').text("");
        //カテゴリー3内部タイトル下のチェックボックス群制御
        base.children('Div.LayerBody').children('Div.FloatCellType').children('input:CheckBox').attr('checked', $(this).is(':checked'));

        //4階層向け（分離するかはあとで検討）
        base.children('Div.LayerBody').children('Div.ShownTitleDesign').children('input:CheckBox').attr('checked', $(this).is(':checked'));
        base.children('Div.LayerBody').children('Div.ShownTitleDesign').children('span.selected').text("");
        base.children('Div.LayerBody').children('Div.LayerBody').children('Div.GovDepFloatCell').children('input:checkbox').attr('checked', $(this).is(':checked'));
    });

    //○2階層メニュー用タイトルクリック時
    $('Div.Layer2Title input:Checkbox').click(function() {
        $(this).parent().children('span.selected').text("");

        //配下のチェックボックスのチェックOn/Off処理
        $(this).parent().next().children('div.FloatCellType').children('input:CheckBox').attr('checked', $(this).is(':checked'));

        //4階層向け（分離するかはあとで検討）
        $(this).parent().parent().children('Div.LayerBody').children('Div.ShownTitleDesign').children('input:CheckBox').attr('checked', $(this).is(':checked'));
        $(this).parent().parent().children('Div.LayerBody').children('Div.ShownTitleDesign').children('span.selected').text("");
        $(this).parent().parent().children('Div.LayerBody').children('Div.LayerBody').children('Div.GovDepFloatCell').children('input:checkbox').attr('checked', $(this).is(':checked'));

        var CheckStatus;
        CheckStatus = $(this).parent().parent().parent().children('div.BasicBox').children('div.ShownTitleDesign').children('input:checkbox:not(:checked)').length == 0 ? true : false;
        //ルートチェックボックス状態を変更
        $(this).parent().parent().parent().prev().children('input:CheckBox').attr('checked', CheckStatus);
    });

    //○3階層目のアイテムクリック時（親のチェック状態変更判断）
    $('Div.FloatCellType input:Checkbox').click(function() {
        //クリックしたチェックボックス群で未チェックがなければTrue、あればFalse
        var checkstatus = $(this).parent().parent().children('div.FloatCellType').children('input:checkbox:not(:checked)').length == 0 ? true : false;
        //クリックしたチェックボックスが属するタイトルチェックボックスのチェック状態を変更
        $(this).parent().parent().prev().children('input:Checkbox').attr('checked', checkstatus);

        //内部チェック状態の表示・非表示処理---開始
        var base = $(this).parent().parent().prev();
        if (checkstatus == false) {
            if (base.next().children('div.FloatCellType').children('input:checkbox:checked').length > 0) {
                base.children('span.selected').text("　選択されているものがあります　");
            }
            else {
                base.children('span.selected').text("");
            }
        }
        else {
            base.children('span.selected').text("");
        }
        //内部チェック状態の表示・非表示処理---終了

        //上階層のタイトルチェックボックス群のチェック状態を確認
        var ff = $(this).parent().parent().parent();
        checkstatus = ff.parent().children('Div.BasicBox').children('Div.ShownTitleDesign').children('input:checkbox:not(:checked)').length == 0 ? true : false;
        //上階層のタイトルチェックボックスの状態に応じてルートチェックボックス状態を変更
        $(this).parent().parent().parent().parent().prev().children('input:CheckBox').attr('checked', checkstatus);
    });

    //3階層目のタイトルクリック時
    $('Div.Layer3Title  input:Checkbox').click(function() {
        $(this).parent().children('span.selected').text("");

        //配下のチェックボックスのチェックOn/Off処理
        $(this).parent().next().children('Div.GovDepFloatCell').children('input:checkbox').attr('checked', $(this).is(':checked'));

        var CheckStatus;
        //同階層のタイトルチェックボックスのチェック状態に合わせて上階層のタイトルチェックボックス状態を変更
        CheckStatus = $(this).parent().parent().children('Div.Layer3Title').children('input:checkbox:not(:checked)').length == 0 ? true : false;
        $(this).parent().parent().prev().children('input:checkbox').attr('checked', CheckStatus);

        //上階層のタイトルに内部チェック状態の表示・非表示処理---開始
        var base = $(this).parent().parent().prev();
        if (CheckStatus == false) {
            if ($(this).parent().parent().children('div.Layer3Title').children('input:checkbox:checked').length > 0) {
                base.children('span.selected').text("　選択されているものがあります　");
            }
            else {
                //3階層目タイトルしたのアイテムにチェックがあるか
                var targetL4Items = $(this).parent().parent().children('div.LayerBody');
                var i;
                for (i = 0; i < targetL4Items.length; i++) {
                    if (targetL4Items.children('div.FloatCellType').children('input:checkbox:checked').length > 0) {
                        //base.children('span.selected').text(" 選択されているものがあります　");
                        break;
                    }
                    else {
                        if (i == targetL4Items.length - 1) {
                            base.children('span.selected').text("");
                        }
                    }
                }
            }
        }
        else {
            base.children('span.selected').text("");
        }
        //上階層のタイトルに内部チェック状態の表示・非表示処理---終了

        //上階層のタイトルチェックボックスのチェック状態に合わせて最上階層のタイトルチェックボックス状態を変更
        var RootBody;
        RootBody = $(this).parent().parent().parent().parent();
        CheckStatus = RootBody.children('Div.BasicBox').children('Div.Layer2Title').children('input:checkbox:not(:checked)').length == 0 ? true : false;
        RootBody.prev().children('input:checkbox').attr('checked', CheckStatus);
    });

    //4階層目のアイテムクリック時
    $('Div.GovDepFloatCell input:checkbox').click(function() {
        //クリックしたアイテムの属する他のチェックボックスの状態を取得し、3階層目のタイトルのチェックボックスの状態を制御する
        var CheckStatus1;
        var CheckStatus2;
        var CheckStatus3;

        CheckStatus1 = $(this).parent().parent().children('Div.GovDepFloatCell').children('input:checkbox:not(:checked)').length == 0 ? true : false;
        $(this).parent().parent().prev().children('input:checkbox').attr('checked', CheckStatus1);

        //3階層目のタイトルのチェックボックス群の状態を取得し、2階層目のチェックボックスの状態を制御する
        var LayerBody;
        LayerBody = $(this).parent().parent().parent();
        CheckStatus2 = LayerBody.children('Div.Layer3Title ').children('input:checkbox:not(:checked)').length == 0 ? true : false;
        LayerBody.prev().children('input:checkbox').attr('checked', CheckStatus2);

        //上階層のタイトルに内部チェック状態の表示・非表示処理---開始
        var base = $(this).parent().parent().prev();
        //        if (CheckStatus1 == false) {
        //            if ($(this).parent().parent().children('div.GovDepFloatCell').children('input:checkbox:checked').length > 0) {
        //                base.children('span.selected').text(" 選択されているものがあります　");
        //            }
        //            else {
        //                base.children('span.selected').text("");
        //            }
        //        }
        //        else {
        //            base.children('span.selected').text("");
        //        }

        if (CheckStatus2 == false) {
            if (LayerBody.children('Div.Layer3Title ').children('input:checkbox:checked').length > 0) {
                LayerBody.prev().children('span.selected').text("　選択されているものがあります　");
            }
            else {
                //if ($(this).parent().parent().children('div.GovDepFloatCell').children('input:checkbox:checked').length > 0) {
                if (LayerBody.children('div.LayerBody').children('div.GovDepFloatCell').children('input:checkbox:checked').length > 0) {
                    base.children('span.selected').text("　選択されているものがあります　");
                    LayerBody.prev().children('span.selected').text("　選択されているものがあります　");
                }
                else {
                    base.children('span.selected').text("");
                    LayerBody.prev().children('span.selected').text("");
                }
            }
        }
        else {
            LayerBody.prev().children('span.selected').text("");
        }

        if (CheckStatus1 == false) {
            if ($(this).parent().parent().children('div.GovDepFloatCell').children('input:checkbox:checked').length > 0) {
                base.children('span.selected').text("　選択されているものがあります　");
            }
            else {
                base.children('span.selected').text("");
            }
        }
        else {
            base.children('span.selected').text("");
        }
        //上階層のタイトルに内部チェック状態の表示・非表示処理---終了

        //2階層目のタイトルのチェックボックス群の状態を取得し、1階層目のチェックボックスの状態を制御する
        var RootBody;
        RootBody = LayerBody.parent().parent();
        CheckStatus3 = RootBody.children('Div.BasicBox').children('Div.ShownTitleDesign').children('input:checkbox:not(:checked)').length == 0 ? true : false;
        RootBody.prev().children('input:checkbox').attr('checked', CheckStatus3);

    });

    //toggle処理-----------------------------------------------

    //MenuClose（Divの初期表示状態）
    //通常用
    $('div.MenuClose').next().hide(0, false);
    //3階層目用（Hideと高さを変更する）
    $('div.MenuCloseForInnerDiv').next().hide(0, false);
    $('div.MenuCloseForInnerDiv').parent().height(30);

    //Span用トグルイベント処理
    $('span.ExpandableMenu').toggle(
            function() {
                $(this).parent().next().show('200', false);
                $(this).removeClass("ui-icon-plus");
                $(this).addClass("ui-icon-minus");
            },
            function() {
                $(this).parent().next().hide('200', false);
                $(this).removeClass("ui-icon-minus");
                $(this).addClass("ui-icon-plus");
            }
            );
    //Span用トグルイベント処理
    $('span.ExpandableMenuType2').toggle(
            function() {
                $(this).parent().next().show('500', false);
                $(this).parent().parent().animate({ height: 320 }, 500);
                $(this).removeClass("ui-icon-plus");
                $(this).addClass("ui-icon-minus");
            },
            function() {
                $(this).parent().next().hide('500', false);
                $(this).parent().parent().animate({ height: 30 }, 500);
                $(this).removeClass("ui-icon-minus");
                $(this).addClass("ui-icon-plus");
            }
            );

});