﻿function InviteEmail(fullName, listEmail) {
    $("#div_Region_01").hide();
    $("#div_Region_02").show();
    $("#div_Region_02").html("<img src='images/loadingAnimation.gif'/>");
    $.ajax({
        type: "POST",
        url: "Popup_Email2Friend.aspx/InviteEmail",
        data: "{'fullName':'" + fullName + "', 'listEmail':'" + listEmail + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                $("#div_Region_01").hide();
                $("#div_Region_02").show();
                $("#div_Region_02").html("Đã gửi email thành công!");
                
            }
            else {
            }
        },
        error: function(msg) {
        }
    });
}
function UserChildAction(iuType, childID, userName, childName, birthDay) {
    $.ajax({
        type: "POST",
        url: "Popup_Birthday.aspx/UserChildAction",
        data: "{'iuType':'" + iuType + "', 'childID':'" + childID + "', 'userName':'" + userName + "', 'childName':'" + childName + "', 'birthDay':'" + birthDay + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                LoadData("default", "FetchAllChild", "Child", "-1", "userName:" + userName);
                Cancel();
            }
            else {
            }
        },
        error: function(msg) {
        }
    });
}
function LoadFun(file) {
    $("#img_Fun").attr("src", "cms/uploads/fun/" + file);
}
function ReloadImage(imgFile, typeDimension, targetSize, oldFunWidth, oldFunHeight) {
    $.ajax({
        type: "POST",
        url: "Center.asmx/ReLoadImage",
        data: "{'imgFile':'" + imgFile + "', 'typeDimension':'" + typeDimension + "', 'targetSize':'" + targetSize + "', 'oldFunWidth':'" + oldFunWidth + "', 'oldFunHeight':'" + oldFunHeight + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            LoadFun(msg);
        },
        error: function(msg) {
        }
    });
}
function DoPostMomment(mommentID, comment) {
    $.ajax({
        type: "POST",
        url: "Popup_Momment.aspx/DoPostMomment",
        data: "{'mommentID':'" + mommentID + "', 'comment':'" + comment + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                Cancel();
            }
            else {
            }
        },
        error: function(msg) {
        }
    });
}
function DoVote(userName, id, isMoment) {
    //    $.ajax({
    //        type: "POST",
    //        url: "Popup_Momment.aspx/DoVote",
    //        data: "{'userName':'" + userName + "', 'id':'" + id + "', 'isMomment':'" + isMoment + "'}",
    //        contentType: "application/json; charset=utf-8",
    //        dataType: "json",
    //        success: function(msg) {
    //            if (msg != "notok") {
    //                LoadData("Popup_Momment.aspx", "FetchMommentInfo", "div_CurrentMomment", "-1", "id:" + id);
    //            }
    //            else {
    //                alert("Bạn chỉ có thể bình chọn cho mỗi tấm ảnh 1 lần");
    //            }
    //        },
    //        error: function(msg) {
    //        }
    //    });
    alert('Thời gian bình chọn đã kết thúc!');
}
function UserAction(option) {
    showLayer();
    $.ajax({
        type: "POST",
        url: "Register.aspx/UserAction",
        data: "{" + option + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            hideLayer();
            if (msg == "ok") {
                alert("Vui lòng kiểm tra email để kích hoạt tài khoản!");
                window.location.href = './';
            }
            else {
                alert(msg);
            }
        },
        error: function(msg) {
            hideLayer();
        }
    });
}
function DoLogin(userName, password) {
    var oldPathname = window.location;
    $.ajax({
        type: "POST",
        url: "Center.asmx/Login",
        data: "{'userName':'" + userName + "', 'password':'" + password + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                window.location.href = oldPathname;
            }
            else {
                alert("Tài khoản hoặc mật khẩu không chính xác");
            }
        },
        error: function(msg) {
        }
    });
}
function DoLogout() {
    $.ajax({
        type: "POST",
        url: "Center.asmx/DoLogout_Client",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                window.location.href = './';
            }
        },
        error: function(msg) {
        }
    });
}
function CheckLogout() {
    $.ajax({
        type: "POST",
        url: "Center.asmx/CheckLogout_Client",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                $("#div_Region_01").hide();
                $("#div_Region_02").show();
            }
            else {
                $("#div_Region_02").hide();
                $("#div_Region_01").show();
            }
        },
        error: function(msg) {
        }
    });
}
function CheckLogin() {
    $.ajax({
        type: "POST",
        url: "Center.asmx/CheckLogin_Client",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg != "notok") {
                $("#divLogin_01").hide();
                var result = "<span style='color:#7ed6ee'>" + msg + "</span><span style='color:#3f2a1d'></span>";
                $("#divLogin_02").html(result);
                $("#divLogin_02").show();
                $("#divLogin_03").show();
            }
            else {
            }
        },
        error: function(msg) {
        }
    });
}
function PostComment(content) {
    $.ajax({
        type: "POST",
        url: "DiaryDetail.aspx/PostComment",
        data: "{'content':'" + content + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                LoadData("DiaryDetail.aspx", "FetchComment", "Comment_AllComments", "-1", "cateType:1");
            }
            else {
            }
        },
        error: function(msg) {
        }
    });
}
function LoadData(target, method, container, page, extension) {
    if (target == "default") {
        target = "Center.asmx";
    }
    $.ajax({
        type: "POST",
        url: target + "/" + method,
        data: "{'page':'" + page + "', 'extension':'" + extension + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var temp = msg;
            $("#" + container).html(temp);
        },
        error: function(msg) {
        }
    });
}
function ArticleAction(method, option) {
    $.ajax({
        type: "POST",
        url: "DiaryPost.aspx/" + method,
        data: "{" + option + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                window.location.href = 'Diary.aspx';
            }
            else {
                alert(msg);
            }
        },
        error: function(msg) {
        }
    });
}
function DoBlast(blast) {
    $.ajax({
        type: "POST",
        url: "Popup_EditBlast.aspx/DoBlast",
        data: "{'blast':'" + blast + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                var userName = $("#userName").val();
                LoadData("default", "FetchBlast", "Blast", "-1", "userName:" + userName);
                jQuery(document).trigger('close.facebox');
            }
            else {
            }
        },
        error: function(msg) {
        }
    });
}
function DoInfo(info) {
    $.ajax({
        type: "POST",
        url: "Popup_EditInfo.aspx/DoInfo",
        data: "{'info':'" + info + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                var userName = $("#userName").val();
                LoadData("default", "FetchBlogInfo", "Info", "-1", "userName:" + userName);
                jQuery(document).trigger('close.facebox');
            }
            else {
            }
        },
        error: function(msg) {
        }
    });
}
function DoManual(title, content, cateType) {
    $.ajax({
        type: "POST",
        url: "Manual.aspx/DoManual",
        data: "{'title':'" + title + "','content':'" + content + "', 'cateType':'" + cateType + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg == "ok") {
                LoadData("default", "FetchAllManual", "div_AllManual", "-1", "cateType:7");
                $("#div_ManualPost").hide();
                $("#div_ManualDetail").show();
                LoadData("default", "FetchManualDetail_Newest", "div_ManualDetail", "-1", "");
            }
            else {
                alert("Bạn hãy đăng nhập để có thể cùng chia sẻ những cẩm nang của mình!");
            }
        },
        error: function(msg) {
        }
    });
}
function Cancel() {
    jQuery(document).trigger('close.facebox');
}
function ReinitialSession(key, value) {
    $.ajax({
        type: "POST",
        url: "Center.asmx/SetSession",
        data: "{'key':'" + key + "', 'value':'" + value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            LoadData("default", "FetchBlogInfo", "Info", "-1", "userName:" + value);
        },
        error: function(msg) {
        }
    });
}
function SetSession(key, value) {
    $.ajax({
        type: "POST",
        url: "Center.asmx/SetSession",
        data: "{'key':'" + key + "', 'value':'" + value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
        },
        error: function(msg) {
        }
    });
}
function DoRecover(email) {
    showLayer();
    $.ajax({
        type: "POST",
        url: "Popup_Password.aspx/DoRecover",
        data: "{'email':'" + email + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg != 'ok') {
            }
            else {
                alert("Thông tin tài khoản của bạn đã được gửi đến [" + email + "]");
                jQuery(document).trigger('close.facebox');
            }
            hideLayer();
        },
        error: function(msg) {
        }
    });
}
function DeleteBlog(id) {
    $.ajax({
        type: "POST",
        url: "Diary.aspx/DeleteBlog",
        data: "{'id':'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg != "ok") {
            }
            else {
                LoadData("Diary.aspx", "FetchBlog", "BlogContent", "-1", "cateType:2");
            }
            jQuery(document).trigger('close.facebox');
        },
        error: function(msg) {
            //alert(msg);
        }
    });
}
function LoadManualDetail(id) {
    $("#div_ManualPost").hide();
    $("#div_ManualDetail").show();
    LoadData("Manual.aspx", "FetchManualDetail", "div_ManualDetail", "-1", "id:" + id);
}
function encodeJSON(str) {
    str = str.replace(new RegExp("'", "g"), "%qt%"); 
    str = str.replace(/\\/g, "%sl%"); 
    return str;
}
function echeck(str) {

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false
    }
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false
    }
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false
    }
    if (str.indexOf(at, (lat + 1)) != -1) {
        return false
    }
    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false
    }
    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false
    }
    if (str.indexOf(" ") != -1) {
        return false
    }
    if (ldot == lstr - 1) {
        return false;
    }
    return true
}
function getBrowserHeight() {
    var intH = 0;
    var intW = 0;

    if (typeof window.innerWidth == 'number') {
        intH = window.innerHeight;
        intW = window.innerWidth;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        intH = document.documentElement.clientHeight;
        intW = document.documentElement.clientWidth;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        intH = document.body.clientHeight;
        intW = document.body.clientWidth;
    }

    return { width: parseInt(intW), height: parseInt(intH) };
}

function setLayerPosition() {
    var shadow = document.getElementById("shadow");
    var loading = document.getElementById("loading");

    var bws = getBrowserHeight();
    shadow.style.width = bws.width + "px";
    shadow.style.height = bws.height + "px";

    loading.style.left = parseInt((bws.width - 174) / 2) + "px";
    loading.style.top = parseInt((bws.height - 14) / 2) + "px";

    shadow = null;
    loading = null;
}

function showLayer() {
    setLayerPosition();

    var shadow = document.getElementById("shadow");
    var loading = document.getElementById("loading");

    shadow.style.display = "block";
    loading.style.display = "block";
    shadow = null;
    loading = null;
}

function hideLayer() {
    var shadow = document.getElementById("shadow");
    var loading = document.getElementById("loading");

    shadow.style.display = "none";
    loading.style.display = "none";

    shadow = null;
    loading = null;
}
window.onresize = setLayerPosition;