/// 設定変更画面

//名前空間用のオブジェクトを用意する
var SETTINGS = {};

// Init function of page
$(document).ready(function () {

    if (!AVWEB.avwCheckLogin(COMMON.ScreenIds.Login)) return;
    COMMON.ToogleLogoutNortice();
    COMMON.LockScreen();

    document.title = I18N.i18nText('dspSetting') + ' | ' + I18N.i18nText('sysAppTitle');

    // Set bookmark screen
    ClientData.BookmarkScreen(COMMON.ScreenIds.Setting);

    SETTINGS.initScreen();

    $("#dspSave").click(SETTINGS.dspSave_Click);

    $("#dspPwdUpd").click(SETTINGS.dspPwdUpd_Click);

    $("#dspOptReset").click(SETTINGS.dspOptReset_Click);

    $("#dspOptBk").click(SETTINGS.dspOptBk_Click);

    $("#dspOptRes").click(SETTINGS.dspOptRes_Click);

    $("#dspPwdUpd1").click(SETTINGS.dspPwdUpd1_Click);
    $("#dspSkip").click(SETTINGS.dspSkip_Click);
    $("#dspCancel").click(SETTINGS.dspCancel_Click);

    $("#dspOptRes_OK").click(SETTINGS.dspOptRes_OK_Click);
    $("#dspOptRes_Cancel").click(SETTINGS.dspOptRes_Cancel_Click);

    $("#dspOptBk_OK").click(SETTINGS.dspOptBk_OK_Click);
    $("#dspOptBk_Cancel").click(SETTINGS.dspOptBk_Cancel_Click);

    // Check to hide/show backup button
    if (ClientData.isChangedBookmark() == true
        || ClientData.isChangedMarkingData() == true
        || ClientData.isChangedMemo() == true) {
        $("#dspOptBk").show();

        // check disabled button backup
        HEADER.checkDisabledButton('#dlgConfirmBackup .option_backup input', '#dspOptBk_OK');
    }
    else {
        $("#dspOptBk").hide();
    }


    // Get flag to determine must change password
    AVWEB.avwCmsApi(ClientData.userInfo_accountPath(), "requirePasswordChange", 'GET', { sid: ClientData.userInfo_sid() },
    		SETTINGS.avwCmsApi_requirePasswordChange_success,
    		null
    );

    // In case: user_data_backup = "Y" -> backup
    if (ClientData.serviceOpt_user_data_backup() != "Y") {

        $("#dspOptBk").css('visibility', 'hidden');
        $("#dspOptRes").css('visibility', 'hidden');

        $("#chkOptBkCfm").css('visibility', 'hidden');
        $("#txtOptBkCfm").css('visibility', 'hidden');
        $("#txtBkResCap").css('visibility', 'hidden');
        $('#regionOptionBackup').css('visibility', 'hidden');
    }
    else {

        // check show restore button No.17

        var isExistMarking = SETTINGS.IsExistBackupFile('Marking.json', 2);
        var isExistContentMemo = SETTINGS.IsExistBackupFile('ContentMemo.json', 1);
        var isExistBookmark = SETTINGS.IsExistBackupFile('Bookmark.json', 4);

        if (isExistMarking || isExistContentMemo || isExistBookmark) {
            $("#dspOptRes").css('visibility', '');

            if (!isExistMarking) {
                $('#chkopResMarking').attr('disabled', 'disabled').removeAttr('checked');
            }
            else {
                $('#chkopResMarking').removeAttr('disabled');
            }

            if (!isExistContentMemo) {
                $('#chkopResMemo').attr('disabled', 'disabled').removeAttr('checked');
            }
            else {
                $('#chkopResMemo').removeAttr('disabled');
            }

            if (!isExistBookmark) {
                $('#chkopResShiori').attr('disabled', 'disabled').removeAttr('checked');
            }
            else {
                $('#chkopResShiori').removeAttr('disabled');
            }

            // check disabled button restore
            HEADER.checkDisabledButton('#dlgConfirmRestore .option_backup input', '#dspOptRes_OK');

        }
        else {
            $("#dspOptRes").css('visibility', 'hidden');
        }
    }
});

/*
  event of changing language
*/
function changeLanguageCallBackFunction() {        
    document.title = I18N.i18nText('dspSetting') + ' | ' + I18N.i18nText('sysAppTitle');
};


/*
Check backup file exists or not for No.17
*/

SETTINGS.IsExistBackupFile = function(file,type) {
    var isExisted = false;
    var params = { "sid": ClientData.userInfo_sid(), "filename": file, fileType: type }; //, deviceType: '4' 
    // Get list of files
    AVWEB.avwCmsApiSync(ClientData.userInfo_accountPath(), "getBackupFile", "post", params,
                    function (data) {
                        if (data) {
                            isExisted = true;
                        }
                    },
                    function (xhr, b, c) {
                        if (xhr.status != 0) {
                            isExisted = false;
                        }
                        else {
                        	AVWEB.showSystemError();
                        }
                    });
    return isExisted;
};

// Event success
SETTINGS.avwCmsApi_requirePasswordChange_success = function(data) {

    ClientData.requirePasswordChange(0);
    if (data.requirePasswordChange == 1) {
        if (ClientData.serviceOpt_force_pw_change_on_login() == 0) { // No need to change password
            // Skip this case
        }
        else if (ClientData.serviceOpt_force_pw_change_on_login() == 1) {  // Recommend to change password
            var pwdSkipDt = ClientData.userInfo_pwdSkipDt();
            
            if (pwdSkipDt) {
                // Check 30 days
                var currDate = new Date();
                var skipDate = new Date(pwdSkipDt);
                var numDay = currDate.subtractByDays(skipDate);
                
                if (numDay <= 30) {
                    // Do not show dialog to change password
                }
                else if (numDay > 30) {
                    // Show dialog to change password
                	SETTINGS.openChangePassword();

                    $("#dspSkip").show();
                    $("#dspCancel").hide();
                }
            }
            else {
            	
                //alert('pwdSkipDt=null');
            	SETTINGS.openChangePassword();

                $("#dspSkip").show();
                $("#dspCancel").hide();
            }
        }
        else if (ClientData.serviceOpt_force_pw_change_on_login() == 2) {  // Force to change password
        	ClientData.requirePasswordChange(1);
        	SETTINGS.openChangePassword();
			
            $("#dspSkip").hide();
            $("#dspCancel").hide();
            //$("#dspPwdUpd1").css('margin', $("#dspCancel").css('margin'));
            //$("#dspPwdUpd1").css('margin', '-27px 97px 0 0');
            $("#txtChangePassComment").css('visibility', 'hidden');

        }
        
    }
    else if (data.requirePasswordChange == 2) {

        if (ClientData.serviceOpt_force_pw_change_periodically() == 0) { // No need to change password
            // Skip this case
        }
        else if (ClientData.serviceOpt_force_pw_change_periodically() == 1) {  // Recommend to change password
            var pwdSkipDt = ClientData.userInfo_pwdSkipDt();

            if (pwdSkipDt) {
                // Check 30 days
                var currDate = new Date();
                var skipDate = new Date(pwdSkipDt);
                var numDay = currDate.subtractByDays(skipDate);
                
                if (numDay <= 30) {
                    // Do not show dialog to change password
                }
                else if (numDay > 30) {
                    // Show dialog to change password
                	SETTINGS.openChangePassword();

                    $("#dspSkip").show();
                    $("#dspCancel").hide();
                }
            }
            else {
                //alert('pwdSkipDt=null');
                
            	SETTINGS.openChangePassword();

                $("#dspSkip").show();
                $("#dspCancel").hide();
            }


        }
        else if (ClientData.serviceOpt_force_pw_change_periodically() == 2) {  // Force to change password
        	ClientData.requirePasswordChange(1);
        	SETTINGS.openChangePassword();

            $("#dspSkip").hide();
            $("#dspCancel").hide();
            //$("#dspPwdUpd1").css('margin', '-27px 97px 0 0');
            $("#txtChangePassComment").css('visibility', 'hidden');
        }
    }
    else if (data.requirePasswordChange == 0) {
        // Skip this case
    }
};

/*
----------------------------------------------------------------------------
Event groups [start]
----------------------------------------------------------------------------
*/

// OK for backup
SETTINGS.dspOptBk_OK_Click = function(e) {
    e.preventDefault();

    // check button is disabled
    if ($(this).hasClass('disabled'))
        return;

    // ----------------------------
    // Process backup here
    // ----------------------------   
    // Bakup memo/marking/bookmark
    // backup data for No.17

    var isBackupMarking = $('#chkopBkMarking').attr('checked') == 'checked';
    var isBackupMemo = $('#chkopBkMemo').attr('checked') == 'checked';
    var isBackupBookmark = $('#chkopBkShiori').attr('checked') == 'checked';

    if (!isBackupMarking && !isBackupMemo && !isBackupBookmark)
    {
    	SETTINGS.closeBackup();
        return;
    }

    // call backup file at header
    HEADER.DoBackup(isBackupMarking, isBackupMemo, isBackupBookmark, false,
    function () {

        // Check to hide/show backup button
    	SETTINGS.setStatusButtonBackup();

        // update check box restore
        if (isBackupMarking && !ClientData.isChangedMarkingData()) {
            $('#chkopResMarking').removeAttr('disabled');
        }
        if (isBackupMemo && !ClientData.isChangedMemo()) {
            $('#chkopResMemo').removeAttr('disabled');
        }
        if (isBackupBookmark && !ClientData.isChangedBookmark()) {
            $('#chkopResShiori').removeAttr('disabled');
        }

        SETTINGS.closeBackup();

    });  
};

// set status button backup and checkbox option
SETTINGS.setStatusButtonBackup = function() {
    if (ClientData.isChangedBookmark() == true || ClientData.isChangedMarkingData() == true || ClientData.isChangedMemo() == true) {

        if (ClientData.isChangedBookmark() != true) {
            $('#chkopBkShiori').attr('disabled', 'disabled').removeAttr('checked');
        }
        if (ClientData.isChangedMarkingData() != true) {
            $('#chkopBkMarking').attr('disabled', 'disabled').removeAttr('checked');
        }
        if (ClientData.isChangedMemo() != true) {
            $('#chkopBkMemo').attr('disabled', 'disabled').removeAttr('checked');
        }
        $("#dspOptBk").show();
    }
    else {
        
        $("#dspOptBk").hide();
    }
};

// Cancel for backup
SETTINGS.dspOptBk_Cancel_Click = function(e) {
    e.preventDefault();
    SETTINGS.closeBackup(true);
};

// OK for restore
SETTINGS.dspOptRes_OK_Click = function(e) {
    e.preventDefault();
    // ----------------------------
    // Process restore
    // ----------------------------

    // check button is disabled
    if ($(this).hasClass('disabled')){
        return;
    }

    // Restore data for No.17

    var isRestoreMarking = $('#chkopResMarking').attr('checked') == 'checked';
    var isRestoreMemo = $('#chkopResMemo').attr('checked') == 'checked';
    var isRestoreBookmark = $('#chkopResShiori').attr('checked') == 'checked';
    if (!isRestoreMarking && !isRestoreMemo && !isRestoreBookmark) {
    	SETTINGS.closeRestore();
        return;
    }

        $().toastmessage({ position: 'middle-center' });
        $().toastmessage('showToast', {
            type: '',
            sticky: true,
            text: '',
            customMessages: 'divResultMessage'
        });
        $('#divResultMessage').append("<div class='toast-item-loading'></div>"); // show item loading

        setTimeout(function () {

            if (isRestoreMarking) {

                // restore Marking Data
                var res = SETTINGS.restoreMarkingData();
                if (!res) {
                    $('#divResultMessage').append("<div class='toast-item-image-error toast-item-message'>" + I18N.i18nText('txtBkMarking') + " " + I18N.i18nText('msgRestoreFailed') + "</div>");
                }
                else {
                    $('#divResultMessage').append("<div class='toast-item-image-success toast-item-message'>" + I18N.i18nText('txtBkMarking') + " " + I18N.i18nText('msgRestoreSuccess') + "</div>");
                }
            }
            if (isRestoreMemo) {

                // restore Memo data
                var res = SETTINGS.restoreMemoData();
                if (!res) {
                    $('#divResultMessage').append("<div class='toast-item-image-error toast-item-message'>" + I18N.i18nText('txtBkMemo') + " " + I18N.i18nText('msgRestoreFailed') + "</div>");
                }
                else {
                    $('#divResultMessage').append("<div class='toast-item-image-success toast-item-message'>" + I18N.i18nText('txtBkMemo') + " " + I18N.i18nText('msgRestoreSuccess') + "</div>");
                }
            }
            if (isRestoreBookmark) {

                // restore Bookmark data
                var res = SETTINGS.restoreBookmarkData();
                if (!res) {
                    $('#divResultMessage').append("<div class='toast-item-image-error toast-item-message'>" + I18N.i18nText('txtBkShiori') + " " + I18N.i18nText('msgRestoreFailed') + "</div>");
                }
                else {
                    $('#divResultMessage').append("<div class='toast-item-image-success toast-item-message'>" + I18N.i18nText('txtBkShiori') + " " + I18N.i18nText('msgRestoreSuccess') + "</div>");
                }
            }

            // set status button backup
            //SETTINGS.setStatusButtonBackup();

            // show message result restore

            // hide item loading
            $('#divResultMessage .toast-item-loading').hide();

            // active close toast button
            $('.toast-item-close').click(function () { $().toastmessage('removeToast', $('#divResultMessage'), null) });
        }, 1000);

        SETTINGS.closeRestore();
};

/*
 * Call api to restore bookmark data
 */
SETTINGS.restoreBookmarkData = function()
{
    var result = false;
    AVWEB.avwCmsApiSync(ClientData.userInfo_accountPath(), "getBackupFile", "post",
                { sid: ClientData.userInfo_sid(), filename: "Bookmark.json" }, // deviceType: '4',
                    function (data) {
                        if (data) {                            
                            COMMON.getDataBookmark(data);
                            ClientData.isChangedBookmark(false);
                            result = true;
                        }
                    }
                ,
                function (xhr, b, c) {
                }
    );
    return result;
};

/*
 * Call api to restore memo data
 */
SETTINGS.restoreMemoData = function() {
    var result = false;
    AVWEB.avwCmsApiSync(ClientData.userInfo_accountPath(), "getBackupFile", "post",
                { sid: ClientData.userInfo_sid(), filename: "ContentMemo.json" }, //deviceType: '4', 
                    function (data) {
                        if (data) {
                            COMMON.getDataMemo(data);
                            //ClientData.isChangedMemo(true);
                            result = true;
                        }
                    }
                ,
                function (xhr, b, c) {
                }
    );
    return result;
};

/*
 * Call api to restore marking data
 */
SETTINGS.restoreMarkingData = function() 
{
    var result = false;
    AVWEB.avwCmsApiSync(ClientData.userInfo_accountPath(), "getBackupFile", "post",
                { sid: ClientData.userInfo_sid(), filename: "Marking.json" }, // deviceType: '4', 
                    function (data) {
                        if (data) {
                            COMMON.getDataMarking(data);
                            ClientData.isChangedMarkingData(false);
                            result = true;
                        }
                    }
                ,
                function (xhr, b, c) {
                }
    );
    return result;
};


// Cancel for restore
SETTINGS.dspOptRes_Cancel_Click = function(e) {
    e.preventDefault();
    SETTINGS.closeRestore(true);
};

// Cancel to change password
SETTINGS.dspCancel_Click = function(e) {
    e.preventDefault();
    var msgError = $('#dialog-error-message');
    msgError.html('');
    SETTINGS.closeChangePassword(true);
};

// Save setting
SETTINGS.dspSave_Click = function(e) {
    e.preventDefault();

    // 最初の画面を選択
    if ($("#rdoOpt001").attr('checked') == 'checked') {
        ClientData.sortOpt_viewMode(COMMON.Consts.ConstDisplayMode_BookShelf); // Bookshelf
    }
    else {
        ClientData.sortOpt_viewMode(COMMON.Consts.ConstDisplayMode_List); // List
    }
    // 動画、音楽繰り返し
    if ($("#chkOpt002").attr('checked') == 'checked') {
        ClientData.userOpt_musicMode(1);
        ClientData.userOpt_videoMode(1);
    }
    else {
        ClientData.userOpt_musicMode(0);
        ClientData.userOpt_videoMode(0);
    }
    // マーキング（コンテンツを開いた時に表示する）
    if ($("#chkOpt003").attr('checked') == 'checked') {
        ClientData.userOpt_makingDsp(1);        
    }
    else {
        ClientData.userOpt_makingDsp(0);
    }
    // Show/not show alert when press F5.close tab.broswer.
    if ($("#chkOpt005").attr('checked') == 'checked') {
        ClientData.userOpt_closeOrRefreshAlert(1);
        COMMON.ToogleLogoutNortice();
    }
    else {
        ClientData.userOpt_closeOrRefreshAlert(0);
        COMMON.ToogleLogoutNortice();
    }
    // 毎回ログアウトの時、バックアップするかどうかは必ず確認する
    if ($("#chkOptBkCfm").attr('checked') == 'checked') {
        ClientData.userOpt_bkConfirmFlg(1);
    }
    else {
        ClientData.userOpt_bkConfirmFlg(0);
    }


    // save options backup No.17
    if ($("#chkBkMarking").attr('checked') == 'checked') {
        ClientData.userOpt_bkMakingFlag(1);
    }
    else {
        ClientData.userOpt_bkMakingFlag(0);
    }

    if ($("#chkBkMemo").attr('checked') == 'checked') {
        ClientData.userOpt_bkMemoFlag(1);
    }
    else {
        ClientData.userOpt_bkMemoFlag(0);
    }

    if ($("#chkBkShiori").attr('checked') == 'checked') {
        ClientData.userOpt_bkShioriFlag(1);
    }
    else {
        ClientData.userOpt_bkShioriFlag(0);
    }

    // save config page transition No.4

    ClientData.userOpt_pageTransition($('#cboAnimation').val());

    var timeAnimation = $('#txtValueAnimation').val();

    if (isNaN(timeAnimation) || timeAnimation < 0.1 || timeAnimation.length == 2 || timeAnimation.length > 3 || timeAnimation.indexOf('.')==0) {
        timeAnimation = 0.1;
    }
    else if (timeAnimation > 9.9) {
        timeAnimation = 9.9;
    }

    ClientData.userOpt_pageTransitionPeriod(timeAnimation);
    $('#txtValueAnimation').val(timeAnimation);


    /* show messages */
    $().toastmessage({ position: 'middle-center' });
    $().toastmessage('showToast', {
        type: 'success',
        sticky: true,
        text: I18N.i18nText('msgSaveOk')
    });	

};

// Skip to change password
SETTINGS.dspSkip_Click = function(e) {
    e.preventDefault();
    var msgError = $('#dialog-error-message');
    msgError.html('');
    // Update パスワードスキップ日時
    ClientData.userInfo_pwdSkipDt(new Date());
    SETTINGS.closeChangePassword();
};

SETTINGS.openChangePassword = function() {
    //$("#dlgChangePassword").dialog("open");
    //$(".ui-dialog-titlebar").hide();
    
    // Clear all input values
    $("#txtPwdCur").val('');
    $("#txtPwdNew").val('');
    $("#txtPwdNewRe").val('');

    COMMON.lockLayout();
    $("#dlgChangePassword").show();
    $("#dlgChangePassword").center();

};

SETTINGS.closeChangePassword = function(skip) {
    //$("#dlgChangePassword").dialog("close");
    $("#dlgChangePassword").hide();
    COMMON.unlockLayout();
};

// Want to change password
SETTINGS.dspPwdUpd_Click = function(e) {
    e.preventDefault();
    $("#dspCancel").show();
    $("#dspSkip").hide();
    $("#txtChangePassComment").css('visibility', 'hidden');
    //$("#dspPwdUpd1").css('margin', '-27px 97px 0 0');
         
    // Show dialog
    SETTINGS.openChangePassword();
};

// Reset setting
SETTINGS.dspOptReset_Click = function(e) {
    e.preventDefault();

    // 最初の画面を選択
    $("#rdoOpt001").attr('checked', 'checked');
    // 動画、音楽繰り返し
	if( AVWEB.avwSysSetting().optMusicMode != undefined){
		if(AVWEB.avwSysSetting().optMusicMode == 1){
			$("#chkOpt002").attr('checked', 'checked');
		} else {
			$("#chkOpt002").removeAttr('checked');
		}
	} else {
		$("#chkOpt002").attr('checked', 'checked');
	}
    // マーキング（コンテンツを開いた時に表示する）
    $("#chkOpt003").attr('checked', 'checked');
    // Show alert when press F5.close tab.broswer
    $("#chkOpt005").attr('checked', 'checked');
    // 毎回ログアウトの時、バックアップするかどうかは必ず確認する
    $("#chkOptBkCfm").attr('checked', 'checked');

    // set default backup marking
    $('#chkBkMarking').attr('checked', 'checked');
    // set default backup memo
    $('#chkBkMemo').attr('checked', 'checked');
    // set default backup bookmark
    $('#chkBkShiori').attr('checked', 'checked');

    // reset page transition no.4
    $('#txtValueAnimation').val(1);
    $('#cboAnimation').val(0);
    $('#slidebar-animation').slider('value', 1);

};

// Backup
SETTINGS.dspOptBk_Click = function(e) {
    e.preventDefault();

    // set options backup No.17

    if (ClientData.userOpt_bkMakingFlag() == 0) 
    {
        $("#chkopBkMarking").removeAttr('checked');
    }
    else 
    {
        $("#chkopBkMarking").attr('checked', 'checked');
    }
    if (ClientData.userOpt_bkMemoFlag() == 0) {
        $("#chkopBkMemo").removeAttr('checked');
    }
    else {
        $("#chkopBkMemo").attr('checked', 'checked');
    }

    if (ClientData.userOpt_bkShioriFlag() == 0) {
        $("#chkopBkShiori").removeAttr('checked');
    }
    else {
        $("#chkopBkShiori").attr('checked', 'checked');
    }

    // check content is changed

    if (!ClientData.isChangedBookmark()) 
    {
        $('#chkopBkShiori').attr('disabled', 'disabled').removeAttr('checked');        
    }
    else {
        $('#chkopBkShiori').removeAttr('disabled');
    }

    if (!ClientData.isChangedMemo()) {
        $('#chkopBkMemo').attr('disabled', 'disabled').removeAttr('checked');
    }
    else {
        $('#chkopBkMemo').removeAttr('disabled');
    }    
    if (!ClientData.isChangedMarkingData()) {
        $('#chkopBkMarking').attr('disabled', 'disabled').removeAttr('checked');
    }
    else {
        $('#chkopBkMarking').removeAttr('disabled');
    }

    HEADER.setDisabledButton('#dlgConfirmBackup .option_backup input', '#dspOptBk_OK');

    SETTINGS.openBackup();
};

// Restore
SETTINGS.dspOptRes_Click = function(e) {
    e.preventDefault();
    SETTINGS.openRestore();
};

// Process changing password
SETTINGS.dspPwdUpd1_Click = function(e) {
    e.preventDefault();
    var isOK = true;
	var msgError = $('#dialog-error-message');
    // Check validation
    if (!ValidationUtil.CheckRequiredForText(SETTINGS.getCurrentPassword())) {
        isOK = false;
	    msgError.html(I18N.i18nText('msgPwdEmpty'));
    	msgError.show();
        
    }
    else {
        if (!ValidationUtil.CheckRequiredForText(SETTINGS.getNewPassword())) {
            isOK = false;
    	    msgError.html(I18N.i18nText('msgPwdEmpty'));
	    	msgError.show();
        }
        else {
            if (SETTINGS.getNewPassword() != SETTINGS.getNewPasswordRe()) {
                isOK = false;
                msgError.html(I18N.i18nText('msgPwdNotMatch'));
    			msgError.show();
            }

        }
    }

    if (isOK) {

        // Check max length
        if (!ValidationUtil.CheckMaxLengthForByte(SETTINGS.getCurrentPassword(), 16)) {
            isOK = false;
        }
        if (!ValidationUtil.CheckMaxLengthForByte(SETTINGS.getNewPassword(), 16)) {
            isOK = false;
        }
        if (!ValidationUtil.CheckMaxLengthForByte(SETTINGS.getNewPasswordRe(), 16)) {
            isOK = false;
        }
        // Data type
        if (!ValidationUtil.IsAlphabetOrNumberOrSymbol(SETTINGS.getCurrentPassword())) {
            isOK = false;
        }
        if (!ValidationUtil.IsAlphabetOrNumberOrSymbol(SETTINGS.getNewPassword())) {
            isOK = false;
        }
        if (!ValidationUtil.IsAlphabetOrNumberOrSymbol(SETTINGS.getNewPasswordRe())) {
            isOK = false;
        }
        var str = SETTINGS.getCurrentPassword() + "";

    }

    // Do changing password
    var params = {
        sid: ClientData.userInfo_sid(),
        loginId: ClientData.userInfo_loginId_session(),
        password: SETTINGS.getCurrentPassword(),
        newPassword: SETTINGS.getNewPassword(),
        appId: 4
    };
    
    if (isOK) {

    	AVWEB.avwCmsApi(ClientData.userInfo_accountPath(), "passwordChange", "GET", params,
    			SETTINGS.avwCmsApi_passwordChange_success,
    			SETTINGS.avwCmsApi_passwordChange_fail);
    }
    else {
        //alert('error');
    }
};

SETTINGS.avwCmsApi_passwordChange_success = function(data) {
    // OK
	var msgError = $('#dialog-error-message');
    if (data.result != undefined && data.result != null) {

        if (data.result != COMMON.Consts.ConstAPI_SUCCESS) {
       	    msgError.html(I18N.i18nText('msgPwdChangeNG'));
			msgError.show();
        }
        else {
        	ClientData.requirePasswordChange(0);
        	msgError.html('');
        	SETTINGS.closeChangePassword();
            /* show messages */
            $().toastmessage({ position: 'middle-center' });
            $().toastmessage('showToast', {
                type: 'success',
                sticky: true,
                text: I18N.i18nText('msgPwdChangeOK')
            });
        }
    }
};
SETTINGS.avwCmsApi_passwordChange_fail = function(xhr, b, c) {
    if (xhr.responseText && xhr.status != 0) {
        /* show error messages */
        var msgError = $('#dialog-error-message');

        //msgError.html(I18N.i18nText('msgPwdChangeNG'));
        msgError.html(JSON.parse(xhr.responseText).errorMessage);
        msgError.show();
    }
    else {
    	AVWEB.showSystemError();
    }
};


/*
----------------------------------------------------------------------------
Event groups [ end ]
----------------------------------------------------------------------------
*/

// Setting dialog
$(function () {
    $('#dlgChangePassword').center();
    $('#dlgConfirmBackup').center();
    $('#dlgConfirmRestore').center();

    $('#dlgChangePassword').hide();
    $('#dlgConfirmBackup').hide();
    $('#dlgConfirmRestore').hide();
});

SETTINGS.openBackup = function() {

    COMMON.lockLayout();
    $("#dlgConfirmBackup").show();
    $("#dlgConfirmBackup").center();

};

SETTINGS.closeBackup = function(cancel) {
    if (cancel != undefined || cancel == true) {
        //alert('you cancelled');
    }
    //$("#dlgConfirmBackup").dialog("close");
    $("#dlgConfirmBackup").hide();
    COMMON.unlockLayout();
};

SETTINGS.openRestore = function() {
    // check avaliable restore no.17
    COMMON.lockLayout();
    $("#dlgConfirmRestore").show();
    $("#dlgConfirmRestore").center();
};

SETTINGS.closeRestore = function(cancel) {
    if (cancel != undefined || cancel == true) {
        //alert('you cancelled');
    }
    //$("#dlgConfirmRestore").dialog("close");
    $("#dlgConfirmRestore").hide();
    COMMON.unlockLayout();
};

// Get input current password
SETTINGS.getCurrentPassword = function() {
    return $("#txtPwdCur").val();
};
// Get input new password
SETTINGS.getNewPassword = function() {
    return $("#txtPwdNew").val();
};
// Get input new password 
SETTINGS.getNewPasswordRe = function() {
    return $("#txtPwdNewRe").val();
};

// Initalize screen
SETTINGS.initScreen = function() {

    // ログインID
    $("#txtLoginId").text(ClientData.userInfo_loginId_session());
    // アカウントパス
    $("#txtLoginPath").text(ClientData.userInfo_accountPath_session());

    // 最終ログイン時間
    $("#txtLastLoginTime").text(ClientData.userInfo_lastLoginTime());
    //alert(getLastLoginDate());


    // 最初の画面を選択
    if (ClientData.sortOpt_viewMode() == COMMON.Consts.ConstDisplayMode_List) {
        $("#rdoOpt0011").attr('checked', 'checked');
    }
    else {
        $("#rdoOpt001").attr('checked', 'checked');
    }

    // 動画、音楽繰り返し
    if (ClientData.userOpt_musicMode() == 0) {
        $("#chkOpt002").removeAttr('checked');
    }
    else {
        $("#chkOpt002").attr('checked', 'checked');
    }

    // マーキング（コンテンツを開いた時に表示する）
    if (ClientData.userOpt_makingDsp() == 0) {
        $("#chkOpt003").removeAttr('checked');
    }
    else {
        
        $("#chkOpt003").attr('checked', 'checked');
    }
	// Show alert when press F5.close tab.broswer	
	if (ClientData.userOpt_closeOrRefreshAlert() == 0) {		
        $("#chkOpt005").removeAttr('checked');
    }
    else {        
        $("#chkOpt005").attr('checked', 'checked');
    }
    // 毎回ログアウトの時、バックアップするかどうかは必ず確認する
    if (ClientData.userOpt_bkConfirmFlg() == 0) {
        $("#chkOptBkCfm").removeAttr('checked');
    }
    else {
        $("#chkOptBkCfm").attr('checked', 'checked');
    }

    // options backup No.17

    if (ClientData.userOpt_bkMakingFlag() == 0) {
        $("#chkBkMarking").removeAttr('checked');
    }
    else {
        $("#chkBkMarking").attr('checked', 'checked');
    }

    if (ClientData.userOpt_bkMemoFlag() == 0) {
        $("#chkBkMemo").removeAttr('checked');
    }
    else {
        $("#chkBkMemo").attr('checked', 'checked');
    }

    if (ClientData.userOpt_bkShioriFlag() == 0) {
        $("#chkBkShiori").removeAttr('checked');
    }
    else {
        $("#chkBkShiori").attr('checked', 'checked');
    }

    // load config page transition & page transition period  No.4

    $('#cboAnimation').val(ClientData.userOpt_pageTransition());

    $('#txtValueAnimation').val(ClientData.userOpt_pageTransitionPeriod());

    $('#txtValueAnimation').blur(function () {

        var value = $(this).val();
        if (isNaN(value) || value < 0.1 || value.length == 2 || value.length > 3 || value.indexOf('.') == 0) {
            value = 0.1;
        }
        else if (value > 9.9) {
            value = 9.9;
        }

        $(this).val(value);
        $('#slidebar-animation').slider('value', value);
    });

    $('#slidebar-animation').slider(
        {
            min: 0.1,
            max: 9.9,
            step: 0.1,
            value: $('#txtValueAnimation').val(),
            slide: SETTINGS.doChangeAnimationSlidebar
        }
    );

};

SETTINGS.doChangeAnimationSlidebar = function(e,obj) {
    $('#txtValueAnimation').val(obj.value);
};
