﻿// Javascript document
/*#####################################################
#   Created by Quyền Đình Dũng
#   Mobile: 0978.399.902 
#   Email: quyendinhdung@gmail.com
#   Date Created: 1:07 AM 02/09/2010
#   Project: QD.Portal v.1.0.1
#####################################################*/

function CallAjax(url, callBack, cacheName, data_type) {
    if (typeof (cacheName) == "undefined") var cacheName = "";
    if (typeof (data_type) == "undefined") data_type = "json";
    var cacheData = null;
    if (cacheName != "") {
        try {
            cacheData = $.jCache.getItem(cacheName);
        }
        catch (e) { }
    }
    if (cacheData == null)
        $.ajax({
            url: url,
            type: "GET",
            dataType: data_type,
            success: function (data) {
                callBack(data);
                if (cacheName != "") $.jCache.setItem(cacheName, data);
            },
            error: function (ex) {
                callBack(ex.responseText);
            }
        });
    else callBack(cacheData);
}

function SendRequestAjax(url, dataPost, callBack, data_type) {
    if (typeof (data_type) == "undefined") data_type = "json";
    $.ajax({
        url: url,
        type: "POST",
        dataType: data_type,
        data: dataPost,
        success: function (data) {
            callBack(data);
        },
        error: function (ex) {
            callBack(ex.responseText);
        }
    });
}

function showMessageBox(msg, title) {
    if (typeof (title) == "undefined") var title = "Thông báo";
    if (!document.getElementById('BlockMessageBox')) {
        $("body").append('<div id="BlockMessageBox" style="display:none;" title="' + title + '"><div class="blog_popup_message"></div></div>');
    }
    else {
        $("#BlockMessageBox>div").removeAttr("title").attr("title", title);
    }
    $("#BlockMessageBox>div").html(msg);
    $("#BlockMessageBox").dialog({
        modal: true,
        resizable: false,
        buttons: {
            'Đồng ý': function () {
                $(this).dialog('close');
            }
        },
        close: function () {
            $(this).dialog("destroy");
        }
    });
    $("#BlockMessageBox").dialog('open');
    return false;
}

function showConfirmBox(msg, callBack, close) {
    if (typeof (close) == "undefined") var close = true;
    if ($("#BlockMessageBox").length <= 0) {
        $("body").append('<div id="BlockMessageBox" style="display:none;" title="Xác nhận"><div class="blog_popup_message"></div></div>');
    } else
        $("#BlockMessageBox").removeAttr("title").attr("title", "Xác nhận");
    $("#BlockMessageBox>div").html(msg);
    $("#BlockMessageBox").dialog({
        modal: true,
        resizable: false,
        buttons: {
            'Bỏ qua': function () {
                $(this).dialog('close');
            },
            'Đồng ý': function () {
                if (close) $(this).dialog('close');
                if (typeof (callBack) != "undefined") callBack();
            }
        },
        close: function () {
            $(this).dialog("destroy");
        }
    });
    $("#BlockMessageBox").dialog('open');
}


function PopupFormDynamic(objContainer, width, height, title, buttons, callEsc) {
    if (typeof (title) == "undefined") var title = "";
    if (typeof (width) == "undefined") var width = 500;
    if (typeof (height) == "undefined") var height = 300;
    if (typeof (objContainer) == "undefined") var objContainer = "#GoMessageBox";
    if ($(objContainer).length <= 0) {
        $("body").append('<div id="' + objContainer.replace('#', '') + '" style="display:none; overflow:hidden;" title="' + title + '"></div>');
    }
    $(objContainer).dialog({
        modal: true,
        width: width,
        height: height,
        resizable: false,
        buttons: buttons,
        close: function () {
            $(this).dialog("destroy");
            if (typeof (callEsc) != "undefined") callEsc();
        }
    });
    $(objContainer).dialog('open');
}

function showPopupBlock(objContainer, width, height, title, callBack) {
    //$("body").css({ "overflow": "hidden" });
    if ($(objContainer).length <= 0) {
        $("body").append('<div id="' + objContainer.replace('#', '') + '" style="display:none; overflow-x:hidden; overflow-y:scroll;" title="' + title + '"></div>');
    }
    $(objContainer).dialog({
        modal: true,
        width: width,
        height: height,
        resizable: false,
        buttons: {
            'Đóng': function () {
                $(this).dialog('close');
            }
        },
        close: function () {
            $(this).dialog("destroy");
            //$("body").css({ "overflow": "auto" });
        }
    });
    $(objContainer).dialog('open');
    if (typeof (callBack) != "undefined")
        callBack();
}

function showPopupForm(objContainer, width, height, title, callBack, callBack2) {
    $("body").css({ "overflow": "hidden" });
    if ($(objContainer).length <= 0) {
        $("body").append('<div id="' + objContainer.replace('#', '') + '" style="display:none; overflow-x:hidden; overflow-y:scroll;" title="' + title + '"></div>');
    }
    $(objContainer).dialog({
        modal: true,
        width: width,
        height: height,
        resizable: false,
        buttons: {
            'Đóng': function () {
                $(this).dialog('close');
            },
            'Đồng ý': function () {
                callBack2();
                $(this).dialog('close');
            }
        },
        close: function () {
            $(this).dialog("destroy");
            $("body").css({ "overflow": "auto" });
        }
    });
    $(objContainer).dialog('open');
    if (typeof (callBack) != "undefined") callBack();
}

function WaitingBox(msg, title, btntitle) {
    if (typeof (title) == "undefined") var title = "Thông báo";
    if ($("#BlockMessageBox").length <= 0) {
        $("body").append('<div id="BlockMessageBox" style="display:none;" title="' + title + '"><div class="blog_popup_message"></div></div>');
    }
    $("#BlockMessageBox>div").html(msg + '<br /><br /><center><img src="/Images/waiting.gif" /></center>');
    $("#BlockMessageBox").dialog({
        modal: true,
        resizable: false,
        buttons: {
            'Đóng': function () {
                $(this).dialog('close');
                return true;
            }
        },
        close: function () {
            $(this).dialog("destroy");
        }
    });
    $("#BlockMessageBox").dialog('open');
}


function InitTinyMCE(objInput, w, h) {
    var editorWidth = typeof (w) == "undefined" ? "700px" : w;
    var editorHeight = typeof (h) == "undefined" ? "120px" : h;
    $(objInput).tinymce({
        // General options
        theme: "advanced",
        language: "en",
        // Theme options
        plugins: "table,fullscreen,media,print",
        theme_advanced_toolbar_location: "top",
        theme_advanced_buttons1: "bold,italic,underline,|,justifyleft, justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,cleanup,code|hr,removeformat,|,sub,sup,|,charmap,|,table,image,media",
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "",
        theme_advanced_resizing: false,
        width: editorWidth,
        height: editorHeight,
        theme_advanced_resizing: true,
        setup: function (ed) {
            ed.onSubmit.add(function (ed, e_) {
                tinyMCE.activeEditor.setContent("");
            });
        }
    });
}

function GetFuncName(vt) {
    var params = location.href;
    params = params.indexOf('#') >= 0 ? params.substring(params.indexOf('#') + 1, params.length) : "";
    var v = params.indexOf('/') >= 0 ? params.split('/') : "";
    if (v.length < 1) return params;
    else return v[vt];
}
// o=object, cl1=old class, cl2= new class
function setFocusMenu(o) {
    $(".mnu_selected").removeClass("mnu_selected").addClass("mnu_unselect");
    $(o).removeClass("mnu_unselect").addClass("mnu_selected");
}
function setFocusTopMenu(o) {
    $(".topmnu_selected").removeClass("topmnu_selected").addClass("topmnu_unselect");
    $(o).removeClass("topmnu_unselect").addClass("topmnu_selected");
}
function changeTab(o) {
    $(".itemac").removeClass("itemac").addClass("item");
    $(o).removeClass("item").addClass("itemac");
}
function changeTab(parentClass) {
    $("." + parentClass + ">.itemac").removeClass("itemac").addClass("item");
    $(o).removeClass("item").addClass("itemac");
}
// For end user
function LogIn() {
}
function LogOut() {
    //showConfirmBox("Bạn có chắc chắc muốn thoát không?", function() {
    CallAjax("/GetRequest.ashx?m=member&ac=logout", function () { location.reload(); });
    //});
}
function InitCookieMember(dataLog) {
    if (dataLog && (!dataLog.error || dataLog.error == 0)) {
        $("#rightHead").html('Chào <span class="username">' + dataLog.Name + '</span> | <a href="/Member.aspx?/profile/user/' + dataLog.Id + '/' + dataLog.Name + '">Trang cá nhân</a> | <a href="/Member.aspx?/setting/user/' + dataLog.Id + '/' + dataLog.Name + '">Thiết lập</a> | <a href="javascript:void(0)" onclick="return LogOut();">Thoát</a>');
    } else {
        $("#rightHead").html('Chào <span class="username">Khách</span> | <a href="/Register.aspx?url=' + escape(location.href) + '">Đăng nhập/Đăng ký</a> | <a href="javascript:void(0)" onclick="Help()">Trợ giúp</a>');
    }
}

var Pagging = function (currentPage, totalRow, objContainer, callBack, objPagging, rowPerPage, pagePerSeg, className, showButton) {
    this.currentPage = typeof (currentPage) == "undefined" ? 1 : currentPage;
    this.totalRow = typeof (totalRow) == "undefined" ? 0 : totalRow;
    this.objContainer = typeof (objContainer) == "undefined" ? "#navigator" : objContainer;
    this.callBack = typeof (callBack) == "undefined" ? function () { } : callBack;
    this.objPagging = typeof (objPagging) == "undefined" ? "objPagging" : objPagging;
    this.rowPerPage = typeof (rowPerPage) == "undefined" ? 20 : rowPerPage;
    this.pagePerSeg = typeof (pagePerSeg) == "undefined" ? 5 : pagePerSeg;
    this.className = typeof (className) == "undefined" ? 'pages_m fr' : className;
    this.showButton = typeof (showButton) == "undefined" ? true : showButton;
    this.handler = "page";
    this.numPage = 1;
    this.Next = function () {
        if (this.currentPage < this.totalRow) {
            this.currentPage++;
            this.callBack();
        }
    };
    this.Prev = function () {
        if (this.currentPage > 0) {
            this.currentPage--;
            this.callBack();
        }
    };
    this.First = function () {
        this.currentPage = 1;
        this.callBack();
    };
    this.Last = function () {
        this.currentPage = this.numPage;
        this.callBack();
    };
    this.GoToPage = function (i) {
        if (i > 0 && i <= this.totalRow) {
            this.currentPage = i;
            this.callBack();
        }
    };
    this.WrapPagging = function () {
        this.numPage = this.totalRow / this.rowPerPage;
        if (this.numPage > Math.floor(this.numPage)) {
            this.numPage = Math.floor(this.numPage) + 1;
        }
        if (this.currentPage > this.numPage) this.currentPage = this.numPage;
        if (this.numPage <= 1) {
            $(this.objContainer).html("");
            return;
        }
        if (this.numPage > 1) {
            var startIndex = 0, endIndex = this.totalRow;
            var constDelta = Math.floor(this.pagePerSeg / 2);
            startIndex = this.currentPage - constDelta;
            if (startIndex <= 0) startIndex = 1;
            endIndex = startIndex + this.pagePerSeg;
            if (endIndex > this.numPage)
                endIndex = this.numPage + 1;
            // Genarate Navigation
            var _handlerString = this.handler != "" ? ' href="#' + this.handler + '{0}" ' : ' href="javascript:;" ';
            var sHTMLNavi = '<div class="' + this.className + '">';
            if (this.currentPage > 1) {
                if (this.showButton) {
                    sHTMLNavi += '<a onclick="' + this.objPagging + '.First()" class="first_page" title="Đầu" ' + _handlerString.format('1') + '>&nbsp;</a>&nbsp;';
                    sHTMLNavi += '<a onclick="' + this.objPagging + '.Prev()" class="first_page_s" title="Trước" ' + _handlerString.format(parseInt(this.currentPage - 1)) + '>&nbsp;</a>';
                }
                else {
                    sHTMLNavi += '<a onclick="' + this.objPagging + '.First()" class="" title="Đầu" ' + _handlerString.format('1') + '>Trang đầu</a>&nbsp;';
                    //sHTMLNavi += '<a onclick="' + this.objPagging + '.Prev()" class="first_page_s" title="Trước" ' + _handlerString.format(parseInt(this.currentPage - 1)) + '>&nbsp;</a>';
                }
                sHTMLNavi += '<span>...</span>';
            }
            for (i = startIndex; i < endIndex; i++) {
                if (i == this.currentPage)
                    sHTMLNavi += '<a class=\"selected\">' + i + '</a>';
                else {
                    sHTMLNavi += '<a onclick="' + this.objPagging + '.GoToPage(' + i + ')" title="Trang ' + i + '" ' + _handlerString.format(i) + '>' + i + '</a>';
                }
            }
            if (this.currentPage < this.numPage) {
                sHTMLNavi += '<span>...</span>';
                if (this.showButton) {
                    sHTMLNavi += '<a onclick="' + this.objPagging + '.Next()" class="end_page_s" title="Sau" ' + _handlerString.format(parseInt(this.currentPage) + 1) + '>&nbsp;</a>&nbsp;';
                    sHTMLNavi += '<a onclick="' + this.objPagging + '.Last()" class="end_page" title="Cuối" ' + _handlerString.format(parseInt(this.numPage)) + ' >&nbsp;</a>';
                }
                else {
                    //sHTMLNavi += '<a onclick="' + this.objPagging + '.Next()" class="end_page_s" title="Sau" ' + _handlerString.format(parseInt(this.currentPage) + 1) + '>&nbsp;</a>&nbsp;';
                    sHTMLNavi += '<a onclick="' + this.objPagging + '.Last()" class="" title="Cuối" ' + _handlerString.format(parseInt(this.numPage)) + ' >Trang cuối</a>';
                }
            }
            sHTMLNavi += '</div>';
            $(this.objContainer).html(sHTMLNavi);
        }
        return this.currentPage;
    }
}

