header.js 36.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/// <reference path="../common/js/avweb.js" />
/// <reference path="../common/js/common.js" />
/// <reference path="../common/js/i18n.js" />
/// <reference path="../common/js/jquery-1.8.1.min.js" />
/// <reference path="../common/js/jquery-ui-1.8.23.custom.min.js" />
/// <reference path="../common/js/jquery.toastmessage.js" />


var timeWaitCloseNewInfoPushMessage = 5000; // time wait close info new push message 5 seconds
var currentPagePushMessage = 1;
var isHoverOn = false;

$(document).ready(function () {

    if (!avwCheckLogin(ScreenIds.Login)) return;

    // Set event to prevent leave
    //avwSetLogoutNortice();
    if (ClientData.requirePasswordChange() != 1) {
        ToogleLogoutNortice();
    }

    //Toggle Searchbox
    $('input#searchbox-key').click(toggleSearchPanel);

    $("#searchbox-key").attr('placeholder', i18nText('msgPlaceHolder'));

    //Go to Search Page
    $('#searchbox-search').click(searchHeaderButtonFunction);

    //Change Language JP
    $('#language-jp').click(changeLanguageJa);

    //Change Language KR
    $('#language-kr').click(changeLanguageKo);

    //Change Language EN
    $('#language-en').click(changeLanguageEn);

    //Go To Bookmark Page
    $('#dspShiori').click(bookmarkFunction);

    //Go To update configuration
    $('#dspSetting').click(updateConfigFunction);

    // hide logout button with anonymous user
    if (isAnonymousLogin()) {
        $('#dspLogout').hide();
        $('#dspSetting').hide();
    }
    else {
        //Go To Login Page
        $('#dspLogout').click(logoutFunction);
        $('#dspLogout').show();
        $('#dspSetting').show();
    }

    $('#dspViewHistory').click(historyClickFunction);

    $('#dspHome').click(homeClickFunction);

    //Hide search panel until click on text field
    $('div#header-searchbox').css('display', 'none');

    if (isAnonymousLogin()) {
        $('#li-login-username').hide();
    }
    else {
        //li-login-username
        $('#li-login-username').show();
        //Display user name
        $('#login-username').text(ClientData.userInfo_userName());
Masaru Abe committed
73
        $('#login-username').attr("title", ClientData.userInfo_userName());
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    }

    $('#dlgConfirmBackup-backup').click(confirmWithBackupFunction);

    $('#dlgConfirmBackup-withoutbackup').click(confirmWithoutBackupFunction);

    $('#dlgConfirmBackup1').hide();

    $('#searchbox-key').keydown(headerSearchKeyDownEventFunction);

    $('#searchbox-content-header').click(headerSearchContentClickFunction);

    $('#searchbox-tag-header').click(headerSearchTagClickFunction);

    $('#searchbox-body-header').click(headerSearchBodyClickFunction);

    //init push message
    initPushMessage();

    //$('*').click(handleHeaderSearchBoxEvent);

    if (isTouchDevice() == false) {
        $('#searchbox-key').hover(searchBoxHoverFunction, searchBoxHoverOffFunction);

        $('#header-searchbox').hover(searchBoxHoverFunction, searchBoxHoverOffFunction);
    }

    if (isTouchDevice() == true) {
        var bodyTag = document.getElementsByTagName('body')[0];
        bodyTag.addEventListener('touchstart', bodyClickFunction, false);
    }
    else {
        $('body').click(bodyClickFunction);
    }

});

function searchBoxHoverFunction(){
	isHoverOn = true;
};

function searchBoxHoverOffFunction() {
	isHoverOn = false;
};

// check disabled button
function checkDisabledButton(selector, buttonid) {
    $(selector).click(
            function () {
                setDisabledButton(selector, buttonid);
            });
    setDisabledButton(selector, buttonid);
};

function setDisabledButton(selector, buttonid) {
    var isDisabled = $(selector + ':checked').length == 0;
    if (isDisabled) {
        $(buttonid).addClass('disabled');
    }
    else {
        $(buttonid).removeClass('disabled');
    }
};


function bodyClickFunction(event) {
    if (isTouchDevice()) {
        
        // Check mouse is in rectangle of searching panel
        if ($('#header-searchbox').is(":visible"))  //if ($('#header-searchbox').css('display') != "none") 
        {

            var currPosX, currPosY;
            var avwUserEnvObj = new UserEnvironment();

            if (avwUserEnvObj.os == 'android') {
                //$("#searchbox-key").val(event.targetTouches[0].pageX + "_" + $('#header-searchbox').position().left + ":" + ($('#header-searchbox').position().left + $('#header-searchbox').width()));
                currPosX = event.targetTouches[0].pageX;
                currPosY = event.targetTouches[0].pageY;
            }
            else {
                currPosX = event.targetTouches[0].clientX;
                currPosY = event.targetTouches[0].clientY;
            }

            var leftsearch = $('#header-searchbox').offset().left;
            var topsearch = $('#header-searchbox').offset().top;
            var rightsearch = $('#header-searchbox').width() + leftsearch;
            var bottomsearch = $('#header-searchbox').height() + topsearch;

            // check mouse position in search region
            if (currPosX >= leftsearch && currPosX <= rightsearch && currPosY >= topsearch && currPosY <= bottomsearch) {
                isHoverOn = true;
            }
            else {
                isHoverOn = false;
                $('#header-searchbox').hide();
            }


//            if (currPosX >= $('#header-searchbox').position().left
//                && currPosX <= ($('#header-searchbox').position().left + $('#header-searchbox').width())
//                && currPosY >= $('#header-searchbox').position().top
//                && currPosY <= ($('#header-searchbox').position().top + $('#header-searchbox').height())) {

//                isHoverOn = true;
//            }
//            else {
//                isHoverOn = false;
//            }
        }
    }
    else {
        if (!isHoverOn) {
            $('#header-searchbox').hide();
        }
    }
};

function headerSearchBodyClickFunction() {
    
	$('#searchbox-body').attr('checked','checked');
	$('#searchbox-tag').removeAttr('checked');
	$('#searchbox-content').removeAttr('checked');
	isHoverOn = true;
};

function headerSearchTagClickFunction() {
    
	$('#searchbox-tag').attr('checked','checked');
	$('#searchbox-body').removeAttr('checked');
	$('#searchbox-content').removeAttr('checked');
	isHoverOn = true;
};

function headerSearchContentClickFunction() {
    
	$('#searchbox-content').attr('checked','checked');
	$('#searchbox-tag').removeAttr('checked');
	$('#searchbox-body').removeAttr('checked');
	isHoverOn = true;
};


//function header search box key down function
function headerSearchKeyDownEventFunction(e){
 	var code = (e.keyCode ? e.keyCode : e.which);
 	if(code == 13) { //Enter keycode
   		$('#searchbox-search').click();
 }
 isHoverOn = true;
};

//Toggle Search Panel Click function
function toggleSearchPanel(){
    if ($("div#header-searchbox").is(":hidden")) {

        // show radio options
        $('div#header-searchbox').slideDown('slow');

        // set default option search
        if ($('#header-searchbox input:checked').length == 0) {
            // set option default is searchbox content
            $('#searchbox-content').attr('checked', 'checked');
        }

	} else {
		$('div#header-searchbox').hide();
	}	
};

//Button Search Event function
function searchHeaderButtonFunction(){
	var content = $('#searchbox-content').attr('checked');
	var tag = $('#searchbox-tag').attr('checked');
	var body = $('#searchbox-body').attr('checked');
	
	var searchDivision;
	var searchText = $('#searchbox-key').val();
	if(content == 'checked')
	{
		searchDivision = $('#searchbox-content').val();
	}
	if(tag == 'checked')
	{
		searchDivision = $('#searchbox-tag').val();
	}
	if(body == 'checked')
	{
		searchDivision = $('#searchbox-body').val();
	}			

	ClientData.searchCond_searchText(searchText);
	ClientData.searchCond_searchDivision(searchDivision);
	//window.location = ScreenIds.ContentSearch;
	avwScreenMove(ScreenIds.ContentSearch);
};

function homeClickFunction(){
    //window.location = ScreenIds.Home;
    avwScreenMove(ScreenIds.Home);
};

//Change Language Japanese function
function changeLanguageJa(){
	changeLanguage(Consts.ConstLanguage_Ja);	
	//ClientData.userInfo_language(Consts.ConstLanguage_Ja);
	//$('#control-sort-titlekana').css('display','inline-block');
	//$('#separate').css('display','inline-block');
	//formatDisplayMoreRecord();
	if(window.changeLanguageCallBackFunction){
		changeLanguageCallBackFunction();
	}
    $("#searchbox-key").attr('placeholder', i18nText('msgPlaceHolder'));
};

//Change Language English functions
function changeLanguageEn(){
	changeLanguage(Consts.ConstLanguage_En);	
	//ClientData.userInfo_language(Consts.ConstLanguage_En);
	//$('#control-sort-titlekana').css('display','none');
	//$('#separate').css('display','none');
	//formatDisplayMoreRecord();
	if(window.changeLanguageCallBackFunction){
		changeLanguageCallBackFunction();
	}
    $("#searchbox-key").attr('placeholder', i18nText('msgPlaceHolder'));
};

//Change Language English function
function changeLanguageKo(){
	changeLanguage(Consts.ConstLanguage_Ko);	
	//ClientData.userInfo_language(Consts.ConstLanguage_Ko);
	//$('#control-sort-titlekana').css('display','none');
	//$('#separate').css('display','none');
	//formatDisplayMoreRecord();
	if(window.changeLanguageCallBackFunction){
		changeLanguageCallBackFunction();
    }
    $("#searchbox-key").attr('placeholder', i18nText('msgPlaceHolder'));
};

//Shiori function
function bookmarkFunction(){
    //window.location = ScreenIds.BookmarkList;
    avwScreenMove(ScreenIds.BookmarkList);
};

//Update Config function
function updateConfigFunction(){
    //window.location = ScreenIds.Setting;
    avwScreenMove(ScreenIds.Setting);
};

//Logout function
function logoutFunction() {

    // check content is changed, update status option backup

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

    if (ClientData.isChangedMemo()) {

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

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


    if (ClientData.isChangedBookmark() == true || ClientData.isChangedMarkingData() == true || ClientData.isChangedMemo() == true) {
        // In case: user_data_backup = "Y" -> backup
        if (ClientData.serviceOpt_user_data_backup() == "Y") {

            if (ClientData.userOpt_bkConfirmFlg() == 1) {  // Show confirming dialog                
                //$('#dlgConfirmBackup1').dialog({ width: 600, height: 200, modal: true });
                lockLayout();
                $('#dlgConfirmBackup1').show();
                $('#dlgConfirmBackup1').center();

                // check disabled button backup
                checkDisabledButton('#dlgConfirmBackup1 .option_backup input', '#dlgConfirmBackup-backup');
            }
            else {  // Do not show confirming dialog
                if (ClientData.userOpt_logoutMode() == null || ClientData.userOpt_logoutMode() == undefined) {
                    //$('#dlgConfirmBackup1').dialog({ width: 600, height: 200, modal: true });
                    lockLayout();
                    $('#dlgConfirmBackup1').show();
                    $('#dlgConfirmBackup1').center();
                }
                else {
                    if (ClientData.userOpt_logoutMode() == 0) {  // Logout with backup                        
                        
                        var isBackupMarking=ClientData.userOpt_bkMakingFlag() == 1;
                        var isBackupMemo=ClientData.userOpt_bkMemoFlag() == 1;
                        var isBackupBookmark = ClientData.userOpt_bkShioriFlag() == 1;

                        DoBackup(isBackupMarking, isBackupMemo, isBackupBookmark, true);
                    }
                    else if (ClientData.userOpt_logoutMode() == 1) {  // Logout without backup
                        // Do nothing
                        //Logout
Masaru Abe committed
406
                    	COMMON.webLogoutEvent();
407 408 409 410 411 412
                    }
                }
            }
        }
        // In case: user_data_backup != "Y" -> No backup, logout
        else { 
Masaru Abe committed
413
        	COMMON.webLogoutEvent();
414 415 416
        }
	}
	else{
Masaru Abe committed
417
		COMMON.webLogoutEvent();
418 419 420 421 422 423 424 425
	}
};

function historyClickFunction(){
    //window.location = ScreenIds.History;
    avwScreenMove(ScreenIds.History);
};

Masaru Abe committed
426
/*
427 428 429
//Web Logout Event
function webLogoutEvent(){

430 431
    var isExisted = false;

432 433 434 435 436 437
    var params = {
        sid: ClientData.userInfo_sid()
    };

    avwCmsApiSync(ClientData.userInfo_accountPath(), "webLogout", "GET", params,
                    function (data) {
438
                        isExisted = true;
439 440 441 442 443 444
                        SessionStorageUtils.clear();
                        avwUserSetting().remove(Keys.userInfo_sid);
                        // Move to login screen
                        //window.location = ScreenIds.Login;
                        avwScreenMove(ScreenIds.Login);
                    },
445 446 447 448 449 450 451 452 453 454 455
                    function (xmlHttpRequest, txtStatus, errorThrown) {
                        if (xmlHttpRequest.status == 403) {
                            isExisted = false;
                        }
                        else {
                            // Show system error
                            isExisted = true;
                        }
                    });
                    
    return isExisted;
456
};
Masaru Abe committed
457
*/
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

//Logout Without Backup function
function confirmWithoutBackupFunction(e) {
    e.preventDefault();
	var remember = $('#chkRememberBackup').attr('checked');
	
	if(remember == 'checked'){
	    ClientData.userOpt_bkConfirmFlg(0); // Do not show dialog in next time
	}
	else{
	    ClientData.userOpt_bkConfirmFlg(1); // Show dialog in next time
	}

	ClientData.userOpt_logoutMode(1); // In next time, if choose: [do not show dialog], will not backup and logout
	//window.location = ScreenIds.Login;
Masaru Abe committed
473
	COMMON.webLogoutEvent();
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
};

//Logout With Backup function
function confirmWithBackupFunction(e) {
    e.preventDefault();

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

    // update status flag update options No.17

    var isBackupMarking=$("#chkBkAllMarking").attr('checked') == 'checked';
    var isBackupMemo = $("#chkBkAllMemo").attr('checked') == 'checked';
    var isBackupBookmark = $("#chkBkAllShiori").attr('checked') == 'checked';
    
	var remember = $('#chkRememberBackup').attr('checked');
	unlockLayout();
	$('#dlgConfirmBackup1').css('z-index', '99');
	lockLayout();

	if (remember == 'checked') {

	    ClientData.userOpt_bkConfirmFlg(0);  // Do not show dialog in next time

	    // update status backup in setting
	    ClientData.userOpt_bkMakingFlag(isBackupMarking);
	    ClientData.userOpt_bkMemoFlag(isBackupMemo);
	    ClientData.userOpt_bkShioriFlag(isBackupBookmark);

        DoBackup(isBackupMarking, isBackupMemo, isBackupBookmark, true);
	}
	else{
		ClientData.userOpt_bkConfirmFlg(1);  // Show dialog in next time
		DoBackup(isBackupMarking, isBackupMemo, isBackupBookmark, true);
	}
	
	ClientData.userOpt_logoutMode(0);  // In next time, if choose: [do not show dialog], will backup and logout
	//webLogoutEvent();
};
//Confirm Back Up Ok
function DoBackup(isBackupMarking, isBackupMemo, isBackupBookmark,isLogout,funcCallback) {
    // ----------------------------
    // Process backup here
    // ----------------------------

     //Bakup memo/marking/bookmark

//    var params = [
//        { name: 'sid', content: ClientData.userInfo_sid() },
//        { name: 'deviceType', content: '4' },
//        { name: 'formFile', content: JSON.stringify(buildBackupData()), fileName: 'webBackupData.json', contentType: 'text-plain' }
//    ];
//        avwUploadBackupFile(ClientData.userInfo_accountPath(), params, false,
//                        function (data) {
//                            if (JSON.parse(data).result == "success") {
//                                ClientData.isChangedBookmark(false);
//                                ClientData.isChangedMarkingData(false);
//                                ClientData.isChangedMemo(false);
//                                //alert(i18nText('msgBackupSuccess'));
//															   							  
//                               // Show message: msgBackupSuccess
//                               $().toastmessage({ position: 'middle-center' });
//                               $().toastmessage('showToast', {
//                                   type: 'success',
//                                   sticky: true,
//                                   text: i18nText('msgBackupSuccess'),                                   									
//                               });
//                               $('.toast-position-middle-center').css('width', '500px');
//                               $('.toast-position-middle-center').css('margin-left', '-250px');
//                               $('.toast-item-close').live('click', webLogoutEvent);                               
//                            }
//                            else {
//                                //alert(i18nText('msgBackupFailed'));
//                               // Show error message: msgBackupFailed
//                               $().toastmessage({ position: 'middle-center' });
//                               $().toastmessage('showToast', {
//                                   type: 'error',
//                                   sticky: true,
//                                   text: i18nText('msgBackupFailed')
//                               });
//                               $('.toast-position-middle-center').css('width', '500px');
//                               $('.toast-position-middle-center').css('margin-left', '-250px');
//                               $('.toast-item-close').live('click', webLogoutEvent);
//                            }
//                        },
//                        function (a, b, c) {
//                            //alert(i18nText('msgBackupFailed'));
//                           // Show error message: msgBackupFailed
//                           $().toastmessage({ position: 'middle-center' });
//                           $().toastmessage('showToast', {
//                               type: 'error',
//                               sticky: true,
//                               text: i18nText('msgBackupFailed')
//                           });
//                           $('.toast-position-middle-center').css('width', '500px');
//                           $('.toast-position-middle-center').css('margin-left', '-250px');
//                           $('.toast-item-close').live('click', webLogoutEvent);
//                            });

    // Backup for No.17    

//    if (!isBackupMarking && !isBackupMemo && !isBackupBookmark)
//        return;

    // check if data is changed and has option backup
    if ((ClientData.isChangedBookmark() == true && isBackupBookmark) || (ClientData.isChangedMarkingData() == true && isBackupMarking) || (ClientData.isChangedMemo() == true && isBackupMemo)) {

        if (isLogout) {
            lockLayout();
        }

        $().toastmessage({ position: 'middle-center' });
        $().toastmessage('showToast', {
            type: '',
            sticky: true,
            text: '',
            customMessages: 'divResultMessage'
        });

        // show item loading
        $('#divResultMessage').append("<div class='toast-item-loading'></div>");

        setTimeout(function () {

            // backup Marking
            var isBackupMarkingOK = true;
            var isBackupMemoOK = true;
            var isBackupBookmarkOK = true;

            if (isBackupMarking && ClientData.isChangedMarkingData() == true) {
                isBackupMarkingOK = sendSignalBackupStart(2); // start backup type marking
                if (isBackupMarkingOK) {
                    isBackupMarkingOK = backupFile(JSON.stringify({ "type": 2, "data": ClientData.MarkingData() }), 'Marking.json', 2);
                    if (isBackupMarkingOK) {
                        ClientData.isChangedMarkingData(false);
                        $('#divResultMessage').append("<div class='toast-item-image-success toast-item-message'>" + i18nText('txtBkMarking') + " " + i18nText('msgBackupSuccess') + "</div>");
                    }
                    // finish backup marking if start backup marking success
                    sendSignalBackupFinish(2);
                }
                if (!isBackupMarkingOK) {
                    $('#divResultMessage').append("<div class='toast-item-image-error toast-item-message'>" + i18nText('txtBkMarking') + " " + i18nText('msgBackupFailed') + "</div>");
                }
            }

            // Backup bookmark
            if (isBackupBookmark && ClientData.isChangedBookmark() == true) {
                isBackupBookmarkOK = sendSignalBackupStart(4); // start backup type bookmark
                if (isBackupBookmarkOK) {
                    isBackupBookmarkOK = backupFile(JSON.stringify({ "type": 3, "data": ClientData.BookMarkData() }), 'Bookmark.json', 4);
                    if (isBackupBookmarkOK) {
                        ClientData.isChangedBookmark(false);

                        $('#divResultMessage').append("<div class='toast-item-image-success toast-item-message'>" + i18nText('txtBkShiori') + " " + i18nText('msgBackupSuccess') + "</div>");
                    }
                    // finish backup bookmark if start backup bookmark ok
                    sendSignalBackupFinish(4);
                }
                if (!isBackupBookmarkOK) {
                    $('#divResultMessage').append("<div class='toast-item-image-error toast-item-message'>" + i18nText('txtBkShiori') + " " + i18nText('msgBackupFailed') + "</div>");
                }
            }

            // Backup Memo

            if (isBackupMemo && ClientData.isChangedMemo() == true) {
                isBackupMemoOK = sendSignalBackupStart(1); // start backup type memo
                if (isBackupMemoOK) {
                    isBackupMemoOK = backupFile(JSON.stringify({ "type": 1, "data": ClientData.MemoData() }), 'ContentMemo.json', 1);
                    if (isBackupMemoOK) {
                        ClientData.isChangedMemo(false);
                        $('#divResultMessage').append("<div class='toast-item-image-success toast-item-message'>" + i18nText('txtBkMemo') + " " + i18nText('msgBackupSuccess') + "</div>");
                    }
                    // finish backup memo if start backup memo ok
                    sendSignalBackupFinish(1);
                }
                if (!isBackupMemoOK) {
                    $('#divResultMessage').append("<div class='toast-item-image-error toast-item-message'>" + i18nText('txtBkMemo') + " " + i18nText('msgBackupFailed') + "</div>");
                }
            }

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

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

            if (isLogout) {
                $('.toast-position-middle-center').css('width', '500px');
                $('.toast-position-middle-center').css('margin-left', '-250px');
                $('.toast-item-close').live('click', webLogoutEvent);
            }

            // check call back function
            if (funcCallback) {
                funcCallback();
            }

        }, 1000);
    }
    else 
    {
        if (isLogout) {
Masaru Abe committed
678
        	COMMON.webLogoutEvent();
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
        }
    }
};

function backupFile(data, file,type) {
    var result = false;
    var params = [
        { name: 'sid', content: ClientData.userInfo_sid() },
    //{ name: 'deviceType', content: '4' },
        //{name: 'fileType', content: type },
        { name: 'formFile', content: data, fileName: file, contentType: 'text-plain' }
    ];
    avwUploadBackupFile(ClientData.userInfo_accountPath(), params, false,
                function (data)
                {
                    if (JSON.parse(data).result == "success") 
                    {
                        result = true;
                    }
                }, function (a, b, c) { });
    return result;
};

// send signal backup start
function sendSignalBackupStart(typeBackup)
{
    var result = false;
    var params = { "sid": ClientData.userInfo_sid(), "fileType": typeBackup };
    avwCmsApiSync(ClientData.userInfo_accountPath(), "notifyBackupStart", "post", params,
                    function (data) {                        
                        if (data.result == "success") {
                            result = true;
                        }
                    },
                    function (xhr, b, c) { });
    return result;
};

// send signal backup finish
function sendSignalBackupFinish(typeBackup)
{
    var result = false;
    var params = { "sid": ClientData.userInfo_sid(), "fileType": typeBackup };
    avwCmsApiSync(ClientData.userInfo_accountPath(), "notifyBackupFinish", "post", params,
                    function (data) {
                        if (data.result == "success") {
                            result = true;
                        }
                    },
                    function (xhr, b, c) { });
    return result;
};

/* ------ */

function checkForceChangePassword(){
	if(ClientData.BookmarkScreen() != ScreenIds.Setting){
	   if(ClientData.requirePasswordChange() == 1){
	   		//alert(i18nText('msgPWDNeedChange'));
			showErrorScreenForceChangePassword();
	   }
	}
};

function showErrorScreenForceChangePassword(){
	var tags = '<div id="avw-auth-error">' +
				   '<div style="display:table; width:100%; height:100%;">' + 
				   '<div style="display:table-cell; text-align:center; vertical-align:middle;">' +
				   '<p>'+i18nText('msgPWDNeedChange')+'</p>' + 
				   '<div><button id="avw-unauth-ok">OK</button></div>' +
				   '</div></div></div>';
		$('body').prepend(tags);
Vo Duc Thang committed
751
		$('#avw-auth-error').css( {			
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
			'opacity': 1,
			'position': 'fixed',
			'top': '0',
			'left': '0',
			'width': $(window).width(),
			'height': $(window).height(),
			'zIndex': '10000'
		});
		// resize error page	
		$(window).resize(function() {
			$('#avw-auth-error').css( {
				'width': $(window).width(),
				'height': $(window).height()
			});			
		});
		
		$('#avw-unauth-ok').click(function() {
			ClientData.BookmarkScreen(ScreenIds.Setting);
			avwScreenMove(ScreenIds.Setting);		
		});	
};

/* region for Push message */

// init for push message
function initPushMessage() 
{
    $('#liPushMessage').click(
        function () {

            if ($(this).hasClass('show')) {
                $(this).removeClass('show').addClass('hide');
                $('#accordion').slideUp();
            }
            else {

                $('.notification-pushmessage').hide(); // hide notification
                $(this).removeClass('hide').addClass('show');

                currentPagePushMessage = 1;
                getPushMessageList();
            }
        }
    );

    // set default hide list message
    $('#liPushMessage').removeClass('show').addClass('hide');

        $('#prev-page-message').click(function (e) {
            previousPushMessageClick();
            e.stopPropagation();
            return false;
        });
        $('#next-page-message').click(function (e) {
            nextPushMessageClick();
            e.stopPropagation();
            return false;
        });

//        $('#list-push-message').click(
//            function (e) {

//                $('#liPushMessage').removeClass('show').addClass('hide');
//                $('#list-push-message').slideUp();

//                e.stopPropagation();
//                return false;
//            }
//        );

            $('.notification-pushmessage').click(
            function () {
                $(this).slideUp();
            }
        );

        // check new push message
Masaru Abe committed
829
        if (isAnonymousLogin() == false ) {
830
            getPushMessageNew();
Masaru Abe committed
831
        }
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    };

// get time wait check new push message
function getTimeWaitCheckNewPushMessage() 
{    
    return avwSysSetting().pushTimePeriod * 1000;// time unit is seconds
}

// get message new
function getPushMessageNew() 
{
    //$('.notification-pushmessage').hide();
    var params = { "sid": ClientData.userInfo_sid()};
    avwCmsApi(ClientData.userInfo_accountPath(), "webPushMessageNew", "post", params,
        callbackGetPushMessageNewSuccess,
847 848 849
        function (xhr, b, c) {
        }
    );
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
};

// callback get number new message success
function callbackGetPushMessageNewSuccess(data) {
    if (data) {        
        // get current number message in session
        var currentMessage = parseInt(ClientData.pushInfo_newMsgNumber());
        if (isNaN(currentMessage)) {
            currentMessage = 0;
        }
        var totalMessage = currentMessage + data.count;

        if ($('#liPushMessage').hasClass('hide')) {
            // update new number message to session
            ClientData.pushInfo_newMsgNumber(totalMessage);

            // only show number new message when total messages greater than 0
            if (totalMessage > 0) {
                // show current number message
                $('#numbermessage').html(totalMessage);
            }
            else $('#numbermessage').html('');
        }
        // show notification for new message
        if (data.count > 0)
        {
            $('.notification-pushmessage').html(i18nText("msgPushAlert")).slideDown();

            // auto off notification push message after timeWaitCloseNewInfoPushMessage
            setTimeout(function () {
                $('.notification-pushmessage').slideUp();

            }, timeWaitCloseNewInfoPushMessage);
        }
    }

    // continue check new push message
    setTimeout(getPushMessageNew, getTimeWaitCheckNewPushMessage());

};

// get message
function getPushMessageList() {

    var sysSettings = avwSysSetting();
    var pushPageCount=sysSettings.pushPageCount;

    var from = (currentPagePushMessage - 1) * pushPageCount + 1;
    var to = currentPagePushMessage * pushPageCount;

    var params = { "sid": ClientData.userInfo_sid(), "recordFrom": from, "recordTo": to };
    avwCmsApiSync(ClientData.userInfo_accountPath(), "webPushMessageList", "post", params,
                    function (data) {

                        // reset number message
                        ClientData.pushInfo_newMsgNumber(0);

                        // hide number new message
                        $('#numbermessage').html('');

                        showListPushMessage(data);
                    },
                    function (xhr, b, c) {
                        showSystemError();
                    });
};

// get string from date crate pushmessage

function getDateCreatePushMessage(data) {
    return (data.year + 1900) + "/" + (data.month + 1) + "/" + data.date + " " + data.hours + ":" + data.minutes;
}
// show push message list
function showListPushMessage(data)
{
    $('#show-push-message').html('');
    for (var i = 0; i < data.messageList.length && i <= (data.recordTo - data.recordFrom); i++) 
    {
        var titleMessage = truncate(data.messageList[i].messageDetail, 30).replace(/</g, '&lt;').replace(/>/g, '&gt;');
        var detailMessage = data.messageList[i].messageDetail.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br/>');
        var message = '<div class="newmsg">';
        message += '<h5 class="postItem"><a href="#">' + titleMessage + '</a></h5>';
        message += '<p>' + detailMessage + '<span class="date">' + getDateCreatePushMessage(data.messageList[i].messageSendDate) + '</span></p></div>';
        $('#show-push-message').append(message);
    }

    // hide all detail message
    $('#show-push-message .newmsg p').hide();

    // show list title message
    $('#accordion').slideDown();

    if (currentPagePushMessage > 1 || data.recordTo < data.totalRecord) {
        $('#accordion .pagechange').show();
    }
    else {
        $('#accordion .pagechange').hide();
    }

    // check show next button
    if (data.recordTo < data.totalRecord) {
        $('#next-page-message').show();
    }
    else {
        $('#next-page-message').hide();
    }

    // check show previous button 
    if (currentPagePushMessage > 1) {        
        $('#prev-page-message').css({ "visibility": "visible" });
    }
    else {
        $('#prev-page-message').css({ "visibility":"hidden"});
    }


    // show detail message when click at title
    $('#show-push-message .newmsg h5').click(
        function () {
            var isshow = !$(this).parent().find('p').is(':visible');
            $('#show-push-message .newmsg p').slideUp();
            if (isshow) {
                $(this).parent().find('p').slideDown();
            }
        }
    );
};

// load next page message
function nextPushMessageClick() {
    currentPagePushMessage++;
    getPushMessageList();
};

// load previous page message
function previousPushMessageClick() {
    if (currentPagePushMessage > 1)
        currentPagePushMessage--;
    else currentPagePushMessage = 1;
    getPushMessageList();
};

function truncate(strInput, length){
    if (strInput.length <= length)
    {
        return strInput;
    }
    else
    {
        return strInput.substring(0, length) + "...";
    }
};

function setStatusSort(currentid, isAsc) {
    $('#menu_sort li a').removeClass('descending_sort').removeClass('ascending_sort');
    
    if($('#menu_sort li a#off-default').size()){
        $('#menu_sort li a#off-default').addClass('descending_sort');
    }   
    
    $('#menu_sort li').removeClass('current');
    $(currentid).addClass(isAsc ? 'ascending_sort' : 'descending_sort').parent().addClass("current");
};

// get icon of content type
function getIconTypeContent(contentType) {

    var src = '';
    switch (contentType) {
        case ContentTypeKeys.Type_PDF:
            {
                src = 'img/bookshelf/icon_01.jpg';
                break;
            }
        case ContentTypeKeys.Type_Enquete:
            {
                src = 'img/bookshelf/icon_05.jpg';
                break;
            }
        case ContentTypeKeys.Type_Html:
            {
                src = 'img/bookshelf/icon_05.jpg';
                break;
            }
        case ContentTypeKeys.Type_Image:
            {
                src = 'img/bookshelf/icon_02.jpg';
                break;
            }
        case ContentTypeKeys.Type_Music:
            {
                src = 'img/bookshelf/icon_06.jpg';
                break;
            }
        case ContentTypeKeys.Type_NoFile:
            {
                src = 'img/bookshelf/icon_07.png';
                break;
            }
        case ContentTypeKeys.Type_Others:
            {
                src = 'img/bookshelf/icon_03.jpg';
                break;
            }
        case ContentTypeKeys.Type_Video:
            {
                src = 'img/bookshelf/icon_04.jpg';
                break;
            }
        default: break
    }
    return src;
};

// download resouce content id
function downloadResourceById(contentId){
    var params = {
        sid: ClientData.userInfo_sid(),
        contentId: contentId,
        getType: '2'
    };
    avwCmsApiSync(ClientData.userInfo_accountPath(), "webGetContent", "get", params,
            function (data) {
                //Get resourceurl
                var resourceUrl = getResourceByIdFromAPI(data.contentData.content.resourceId);

                // open url to download file
                if (isSafariNotOnIpad()) {
                    window.onbeforeunload = null;
                    window.open(resourceUrl, "_self"); // open url to download file on safari not for ipad
                    var toogleTime = setTimeout(function () { ToogleLogoutNortice() }, 200);
                }
                else {
                    window.open(resourceUrl); //open url to download file on orther browser
                }
            },
        function (xhr, b, c) { });
};

//Download resource
function getResourceByIdFromAPI(resourceId){
    return getURL("webResourceDownload") + "/?sid=" + ClientData.userInfo_sid() + "&resourceId=" + resourceId + "&isDownload=true";
};

// check is browser safari on Mac and Window devide ( not Ipad )
function isSafariNotOnIpad() {
    if (!window.chrome) {
        var ua = navigator.userAgent.toLowerCase();
        if (!/ipad/.test(ua) && /safari/.test(ua)) {
            return true;
        }
    }
    return false;
};