﻿

//Tạo Cookie
function CreateCookie(name, value, days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

//Đọc Cookie
function ReadCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++)
    {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

///Xóa Cookie
function EraseCookie(name)
{
    CreateCookie(name, "", -1);
}



function Random_Number(len)
{
    chars = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

    charCount = chars.length();

    stringLength = len;
    var outputString;
    i = 0;
    do
    {
        random = Math.floor(Math.random());
        random *= charCount;
        random = chars[random];
        outputString += parseString(random);
        i++;
    }
    while (i < stringLength);

    return outputString;
}

function AskBeforeDelete(message)
{
    return confirm(message);
}

function isNumberKey_Date(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode == 47)//dấu "/"
        return true;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function isNumberKey_int(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function isNumberKey_double(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode == 46)//dấu "."
        return true;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function CheckDate(day, mo, yr)
{
    // handle two-digit year
    if (yr < 100)
    {
        var today = new Date();
        // get current century floor (e.g., 2000)
        var currCent = parseInt(today.getFullYear() / 100) * 100;
        // two digits up to this year + 15 expands to current century
        var threshold = (today.getFullYear() + 15) - currCent;
        if (yr > threshold)
        {
            yr += currCent - 100;
        } else
        {
            yr += currCent;
        }
    }
    var testDate = new Date(yr, mo - 1, day);
    if (testDate.getDate() == day)
    {
        if (testDate.getMonth() + 1 == mo)
        {
            if (testDate.getFullYear() == yr)
            {
                return true;
            } else
            {
                alert("Năm nhập vào là không đúng.");
            }
        } else
        {
            alert("Tháng nhập vào là không đúng.");
        }
    } else
    {
        alert("Ngày nhập vào là không đúng.");
    }

    return false;
}

//Kiểm tra Email
function CheckEmail(mail)
{
    /* text=document.getElementById(txt).value;*/
    mail = trim(mail);
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,4}$/
    if (mail.match(re) == null)
    {
        return false;
    }
    return true;
}

function trim(sString)
{
    while (sString.substring(0, 1) == ' ')
    {
        sString = sString.substring(1, sString.length);

    }
    while (sString.substring(sString.length - 1, sString.length) == ' ')
    {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}

/// <summary>
/// Lấy chiều rộng cho phép của màn hình
/// </summary>
function GetWidth()
{
    //#region
    var w = 0;
    if (self.innerHeight)
    {
        w = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        w = document.documentElement.clientWidth;
    }
    else if (document.body)
    {
        w = document.body.clientWidth;
    }
    return w;
    //#endregion
}

/// <summary>
/// Lấy chiều cao cho phép của màn hình
/// </summary>
function GetHeight()
{
    //#region
    var h = 0;
    if (self.innerHeight)
    {
        h = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        h = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
        h = document.body.clientHeight;
    }
    return h
    //#endregion
}

/// <summary>
/// Lấy tọa độ x của chuột
/// </summary>
function GetMouseX(e)
{
    //#region
    var tempX = 0;

    if (typeBrowser == 0)
    {
        tempX = e.clientX - divBanDo.offsetLeft + document.body.scrollLeft - document.body.clientLeft;
    } else
    {
        tempX = e.pageX - divBanDo.offsetLeft + document.body.scrollLeft - document.body.clientLeft;
    }
    if (tempX < 0) { tempX = 0; }
    return tempX;
    //#endregion
}

/// <summary>
/// Lấy tọa độ y của chuột
/// </summary>
function GetMouseY(e)
{
    //#region
    var tempY = 0;
    if (typeBrowser == 0)
    {
        tempY = e.clientY - divBanDo.offsetTop + document.body.scrollTop - document.body.clientTop;
    } else
    {
        tempY = e.pageY - divBanDo.offsetTop + document.body.scrollTop - document.body.clientTop;
    }

    if (tempY < 0) { tempY = 0; }
    return tempY;
    //#endregion
}

function GetScrollLeft()
{
    var Temp_Left = 0;
    if (self.pageYOffset)
    {
        Temp_Left = self.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {

        Temp_Left = document.documentElement.scrollLeft;

    }
    else if (document.body)
    {
        Temp_Left = document.body.scrollLeft;
    }
    if (!Temp_Left || Temp_Left < 0)
        Temp_Left = 0;

    return Number(Temp_Left);
}
function GetScrollTop()
{
    var Temp_Top;
    if (self.pageYOffset)
    {
        Temp_Top = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        Temp_Top = document.documentElement.scrollTop;
    }
    else if (document.body)
    {
        Temp_Top = document.body.scrollTop;
    }

    if (!Temp_Top || Temp_Top < 0)
        Temp_Top = 0;

    return Number(Temp_Top);
}


function SetScrollTop(iTop)
{
    $('html,body').animate({ scrollTop: iTop }, 0);
}
//Hàm dùng để Khởi tạo đối tượng Ajax (XMLHTML)
function CreateAjax()
{
    //#region
    var XmlHttp;

    //Creating object of XMLHTTP in IE
    try
    {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (oc)
        {
            XmlHttp = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari 
    if (!XmlHttp && typeof XMLHttpRequest != "undefined")
    {
        XmlHttp = new XMLHttpRequest();
    }
    return XmlHttp;

    //#endregion
}

function CreateParameterTime(str_URL)
{
    if (str_URL.indexOf("?", 0) > 0)
    {
        return str_URL + "&time=" + (new Date()).getTime();
    }
    else
    {
        return str_URL + "?time=" + (new Date()).getTime();
    }
}

function OpenNewWindow(str_URL)
{
    if (str_URL && str_URL.length > 0)
        window.open(str_URL);
    return false;
}

function ShowLoading(ctr_This, IsFixed, IsAddClick)
{
    debugger;
    if (!ctr_This)
        return false;
        
    var w = (ctr_This.offsetWidth - 4);
    var h = ctr_This.offsetHeight - 4;
    var ctr_Loading = document.getElementById("divLoading");
    ctr_Loading.style.position = "absolute";

    if (!w || w < 50)
        ctr_Loading.innerHTML = "";
    else
        ctr_Loading.innerHTML = "Đang tải...";
    //        
    //    if (!h || h < 20)
    //        h = 20;

    var top = $(ctr_This).offset().top - (IsFixed ? GetScrollTop() : 0);
    var left = $(ctr_This).offset().left;

    ctr_Loading.style.width = w + "px";
    ctr_Loading.style.height = h + "px";
    ctr_Loading.style.top = top + "px";
    ctr_Loading.style.left = left + "px";

    if (IsFixed)
    {
        ctr_Loading.style.position = "fixed";
    }

    if (IsAddClick)
    {
        //Thêm sự kiện click cho document
        document.onmousedown = DoMouseDownWhenLoading;
    }
}

function DoMouseDownWhenLoading(evt)
{
    if (evt == null) { evt = window.event; }

    if (confirm("Hệ thống đang xử lý, bạn có chắc muốn thực hiện hành động này?"))
    {
        HideLoading();
    }
    else
    {
        return false;
    }
}

//Cho pháp ẩn loading hay không
var AllowHideLoading = true;
function HideLoading()
{
    if (!AllowHideLoading)
    {
        AllowHideLoading = true;
        return false;
    }
    document.onmousedown = null;

    var ctr_Loading = document.getElementById("divLoading");
    ctr_Loading.style.position = "absolute";
    ctr_Loading.style.top = "-1000px";
    ctr_Loading.style.left = "-1000px";
}

