
var messageLevel = {};

function checkLimitContent(contentId, funcOk, funcCancel, isNotUnlockScreen) {

    var levelContent = parseInt(messageLevel[contentId].alertMessageLevel);

    if (levelContent == 1) {
        if ($('#limit_level1').length <= 0) {
            var html = '<section class="sectionLimitAccess" id="limit_level1">';
            html += '<h1 class="lang" lang="txtContentWarning">' + I18N.i18nText("txtContentWarning") + '</h1>';
            html += '<p class="message"></p>';
            html += '<p class="deletebtn">';
            html += '<a lang="dspOK" class="ok lang">' + I18N.i18nText("dspOK") + '</a>';
            html += '<a lang="dspCancel" class="cancel lang">' + I18N.i18nText("dspCancel") + '</a>';            
            html += '</p>';
            html += '</section>';
            $('body').append(html);
        }

        $('#limit_level1 .deletebtn .cancel').unbind('click').click(
            function () {
                if (isNotUnlockScreen != 1) {
                    COMMON.unlockLayout();
                }
                $('#limit_level1').hide();
                //キャンセル
                funcCancel();
            }
        );

        COMMON.lockLayout();

        $('#limit_level1 .message').html(messageLevel[contentId].alertMessage);
        $('#limit_level1').show().center();
        $('#limit_level1 .deletebtn .ok').unbind('click').click(
            function () {
                if (isNotUnlockScreen != 1) {
                    COMMON.unlockLayout();
                }
                $('#limit_level1').hide();
                funcOk();
            }
        );
    }
    else if (levelContent == 2) {

        if ($('#limit_level2').length <= 0) {

            var html = '<section class="sectionLimitAccess" id="limit_level2">';
            html += '<h1 class="lang" lang="txtContentPWTitle">' + I18N.i18nText("txtContentPWTitle") + '</h1>';
            html += '<p class="message">';
            html += '<label class="text lang" lang="txtContentPWMsg">' + I18N.i18nText("txtContentPWMsg") + '</label>';
            html += '<input type="password" />';
            html += '<label class="error" id="lblMessageLimitError"></label>';
            html += '</p>';
            html += '<p class="deletebtn">';
            html += '<a lang="dspOK" class="ok lang">' + I18N.i18nText("dspOK") + '</a>';
            html += '<a lang="dspCancel" class="cancel lang">' + I18N.i18nText("dspCancel") + '</a>';            
            html += '</p>';
            html += '</section>';

            $('body').append(html);

            // press enter at input password
            $('#limit_level2 .message input').keydown(
                function (e) {
                    var code = (e.keyCode ? e.keyCode : e.which);
                    if (code == 13) { //Enter keycode
                        $('#limit_level2 .deletebtn .ok').click();
                    }
                }
            );
        }

        $('#limit_level2 .deletebtn .cancel').unbind('click').click(
            function () {
                if (isNotUnlockScreen != 1) {
                    COMMON.unlockLayout();
                }
                $('#limit_level2').hide();
                //キャンセル
                funcCancel();
            }
        );

        // lock layout
        COMMON.lockLayout();

        //reset input password
        $('#limit_level2 .message input').val('');

        // hide error message
        $('#lblMessageLimitError').hide();

        // show dialog
        $('#limit_level2').show().center();
        
        $('#limit_level2 .deletebtn .ok').unbind('click').click(
            function () {
                var password = $('#limit_level2 .message input').val();
                if (!ValidationUtil.CheckRequiredForText(password)) {
                    $('#lblMessageLimitError').html(I18N.i18nText('msgPwdEmpty')).show();
                    return;
                }

                // start login
                var params = {
                    previousSid: ClientData.userInfo_sid(),
                    loginId: ClientData.userInfo_loginId_session(),
                    password: password,
                    urlpath: ClientData.userInfo_accountPath()
                };
                // Get url to login
                var sysSettings = avwSysSetting();
                var apiLoginUrl = sysSettings.apiLoginUrl;

                avwCmsApiSyncWithUrl(apiLoginUrl, null, 'webClientLogin', 'GET', params,
                    function (data) {
                        if (data.result == 'success') {

                            // update sid id
                            ClientData.userInfo_sid(data.sid);

                            if (isNotUnlockScreen != 1) {
                                COMMON.unlockLayout();
                            }
                            $('#limit_level2').hide();
                            // open content
                            funcOk();
                        }
                        else {
                            $('#lblMessageLimitError').html(AVWEB.format(I18N.i18nText('msgLoginErrWrong'), data.errorMessage).toString()).show();
                        }
                    },
                    function (xhr, statusText, errorThrown) {
                        var errorCode = 'E001';
                        if (xhr.responseText && xhr.status != 0) {
                            errorCode = JSON.parse(xhr.responseText).errorMessage;
                        }
                        $('#lblMessageLimitError').html(AVWEB.format(I18N.i18nText('msgLoginErrWrong'), errorCode).toString()).show();
                    }
                );

            }
        );
    }
    else // content level 0 or null
    {
        funcOk();
    }
};