﻿
//Khai báo biến
var Arr_CheckBoxOnGrid = ""; //Chuỗi các CheckBox trên 1 Grid nào đó, dùng vào mục đích Select All


//Chọn (bỏ chọn) tất cả các checkbox trên GridView
function SelectCheckBox_All(CheckBox_All)
{
    if (CheckBox_All)
    {
        if (Arr_CheckBoxOnGrid.length > 1)
        {

            var Arr = Arr_CheckBoxOnGrid.split(",");
            for (var i = 0; i < Arr.length; i++)
            {
                var ctr_Check = document.getElementById(Arr[i]);
                if (ctr_Check)
                {
                    ctr_Check.checked = CheckBox_All.checked;
                }
            }
        }
    }
}


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;
}



///Khai báo cho face box
var div_FaceBox_OutID;
var div_FaceBox_InID;
var div_FaceBoxID;
var div_FaceBoxContentID;
var div_FaceBoxImageLoadingID;
var div_FaceBoxContentChildID;
var chk_IsShowFaceBoxID;
var IsReloadPage = false; //Để cho phép khi nhấn nút close trên Facebox có được reload lại trang hay không
var chk_IsDeleteContentID;

//Control div
var div_FaceBox_Out;
var div_FaceBox_In;
var div_FaceBox;
var div_FaceBoxContent;
var div_FaceBoxImageLoading;
var div_FaceBoxContentChild;
var chk_IsShowFaceBox;
var chk_IsDeleteContent;
var typeBrowser = 0;
var HideScrollIframe = false;
/// <summary>
/// Hàm dùng để lấy trình duyệt đang sử dụng
/// </summary>
function GetBrowser()
{
    //#region
    var sBrowser = navigator.userAgent.toLowerCase();
    if (sBrowser.indexOf('msie') > -1)
        return 0;
    else if (sBrowser.indexOf('firefox') > -1)
        return 1;
    else if (sBrowser.indexOf('chrome') > -1)
        return 2;
    else if (sBrowser.indexOf('safari') > -1)
        return 3;
    else if (sBrowser.indexOf('opera') > -1)
        return 4;
    else if (sBrowser.indexOf('netscape') > -1)
        return 5;
    else return -1;
    //#endregion
}


//Khởi tạo đầu tiên cho facebox
function InitFaceBox()
{

    typeBrowser = GetBrowser();
    div_FaceBox_Out = document.getElementById(div_FaceBox_OutID);
    div_FaceBox_In = document.getElementById(div_FaceBox_InID);

    div_FaceBox = document.getElementById(div_FaceBoxID);
    div_FaceBoxContent = document.getElementById(div_FaceBoxContentID);
    div_FaceBoxImageLoading = document.getElementById(div_FaceBoxImageLoadingID);
    div_FaceBoxContentChild = document.getElementById(div_FaceBoxContentChildID);
    chk_IsShowFaceBox = document.getElementById(chk_IsShowFaceBoxID);
    chk_IsDeleteContent = document.getElementById(chk_IsDeleteContentID);

    div_FaceBox_Out.style.display = 'none';
    div_FaceBoxImageLoading.style.display = 'inline';
    div_FaceBoxContentChild.style.display = 'none';
}

function HideScroll(isHide)
{
   
    if (isHide)
    {
        switch (typeBrowser)
        {
            case 0: //IE
                document.body.scrollbars = "no";
                document.documentElement.style.overflowX = 'hidden';
                document.documentElement.style.overflowY = 'hidden';
                break;
            case 1: //firefox
                document.documentElement.style.overflow = 'hidden';
                break;
            case 2: //chrome
                document.documentElement.style.overflow = 'hidden';
                break;
            case 3: //safari
                document.documentElement.style.overflow = 'hidden';
                break;
            case 4: //opera
                document.documentElement.style.overflow = 'hidden';
                break;
            case 5: //netscape
                document.documentElement.style.overflow = 'hidden';
                break;
        }
    }
    else
    {
        switch (typeBrowser)
        {
            case 0: //IE
                document.body.scrollbars = "auto";
                document.documentElement.style.overflowX = 'auto';
                document.documentElement.style.overflowY = 'auto';
                break;
            case 1: //firefox
                document.documentElement.style.overflow = 'auto';
                break;
            case 2: //chrome
                document.documentElement.style.overflow = 'auto';
                break;
            case 3: //safari
                document.documentElement.style.overflow = 'auto';
                break;
            case 4: //opera
                document.documentElement.style.overflow = 'auto';
                break;
            case 5: //netscape
                document.documentElement.style.overflow = 'auto';
                break;
        }
    }
}
//Hiển thị facebox
function Show_FaceBox(isShow)
{
    HideScroll(isShow);
    div_FaceBox_Out.style.height = 4000 + "px";
    
    var x = 0, y = 0;

    var centerX, centerY;
    if (self.innerHeight)
    {
        centerX = self.innerWidth;
        centerY = self.innerHeight;

        div_FaceBox_Out.style.width = parseInt(self.innerWidth) + "px";
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        centerX = document.documentElement.clientWidth;
        centerY = document.documentElement.clientHeight;

        div_FaceBox_Out.style.width = parseInt(document.documentElement.clientWidth) + "px";
    }
    else if (document.body)
    {
        centerX = document.body.clientWidth;
        centerY = document.body.clientHeight;

        div_FaceBox_Out.style.width = parseInt(document.body.clientWidth) + "px";
    }

    var scrolledX, scrolledY;
    if (self.pageYOffset)
    {
        scrolledX = self.pageXOffset;
        scrolledY = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {

        scrolledX = document.documentElement.scrollLeft;
        scrolledY = document.documentElement.scrollTop;

    }
    else if (document.body)
    {
        scrolledX = document.body.scrollLeft;
        scrolledY = document.body.scrollTop;

    }

    x = centerX / 2 - div_FaceBox.offsetWidth / 2;
    y = centerY / 2 - div_FaceBox.offsetHeight / 2 + scrolledY;

    x = x < 0 ? 0 : x;
    y = y < 0 ? 0 : y;

    div_FaceBox.style.left = x + 'px';
    div_FaceBox.style.top = y + 'px';


    if (isShow == true)
    {
        div_FaceBox_Out.style.display = 'inline';
        chk_IsShowFaceBox.checked = true;
        div_FaceBox.focus();
    }
    else
    {

        div_FaceBoxImageLoading.style.display = 'inline';
        div_FaceBoxContentChild.style.display = 'none';

        div_FaceBox_Out.style.display = 'none';

        div_FaceBoxContent.style.height = '';

        chk_IsShowFaceBox.checked = false;
    }
    return false;
}

//Cho phép công thêm width, height cho iframe
var AllowAddWidth = true;
function IframeLoadComplete(frameId)
{
    //Làm cho Iframe có thể auto height, width frame.document.body.scrollHeight
    var frame = document.getElementById(frameId);
    var innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
    var objToResize = (frame.style) ? frame.style : frame;
    if (!AllowAddWidth)
    {
        objToResize.height = (parseInt(innerDoc.body.style.height)) + 'px';
        objToResize.width = (parseInt(innerDoc.body.style.width)) + 'px';
        AllowAddWidth = true;
    }
    else
    {
        objToResize.height = (parseInt(innerDoc.body.style.height) + 10) + 'px';
        objToResize.width = (parseInt(innerDoc.body.style.width) + 22) + 'px';
    }
    div_FaceBoxImageLoading.style.display = 'none';
    div_FaceBoxContentChild.style.display = 'inline';
    ResetPosition();
    if (typeBrowser == 0)
        ResetPosition();
}

function SetShow_ChildContent(IsShow)
{
    if (IsShow)
    {
        div_FaceBoxImageLoading.style.display = 'none';
        div_FaceBoxContentChild.style.display = 'inline';
    }
    else
    {
        div_FaceBoxImageLoading.style.display = 'inline';
        div_FaceBoxContentChild.style.display = 'none';
    }
}

//hiển thị face box không có hiển thị image ajax loading
function Show_Facebox_Content()
{
    div_FaceBoxImageLoading.style.display = 'none';
    div_FaceBoxContentChild.style.display = 'inline';
    Show_FaceBox(true);
    FaceBoxLoadComplete();  
    return false;
}

//Khi load facebox xong
function FaceBoxLoadComplete()
{
    
    div_FaceBoxImageLoading.style.display = 'none';
    div_FaceBoxContentChild.style.display = 'inline';   
    ResetPosition();
    
    if (typeBrowser == 0)
        ResetPosition();    
}

//Đóng facebox
function CloseFaceBox()
{
    Show_FaceBox(false);
    //Nếu mà cho phép reload lại trang
    if (IsReloadPage)
    {
        IsReloadPage = false;
        location.reload(true);
    }
    //Nếu cho phép xóa nội dụng trong facebox
    if (chk_IsDeleteContent.checked)
    {
        div_FaceBoxContentChild.innerHTML = "";
    }
}

function Set_IsDeleteContent(b_Value)
{
    chk_IsDeleteContent.checked = b_Value;
}

//Hiển thị facebox khi có liên quan tới ajax
function Show_FaceBoxAjax(URL_Get)
{
    Show_FaceBox(true);
    AjaxFunction(URL_Get);
    return false;
}
function Show_FaceBoxIframe_HideScroll(URL)
{
    HideScrollIframe = true;
    Show_FaceBoxIframe(URL);
    return false;
}
//Hiển thị FaceBox với nội dung là 1 iframe
function Show_FaceBoxIframe(URL)
{
    Show_FaceBox(true);
    if (HideScrollIframe)
    {
        div_FaceBoxContentChild.innerHTML = ' <iframe src="' + URL + '" id="ifr_FaceBox" frameborder="0" scrolling="no" onload="IframeLoadComplete(\'ifr_FaceBox\');"></iframe>';
        HideScrollIframe = false;
    } else
    {
        div_FaceBoxContentChild.innerHTML = ' <iframe src="' + URL + '" id="ifr_FaceBox" frameborder="0" scrolling="auto" onload="IframeLoadComplete(\'ifr_FaceBox\');"></iframe>';
    }
    return false;
}
//Hiển thị FaceBox với nội dung là 1 iframe
function Show_FaceBoxIframe_NotAddWidth(URL)
{
    AllowAddWidth = false;
    Show_FaceBox(true);

    if (HideScrollIframe)
    {
        div_FaceBoxContentChild.innerHTML = ' <iframe src="' + URL + '" id="ifr_FaceBox" frameborder="0" scrolling="no" onload="IframeLoadComplete(\'ifr_FaceBox\');"></iframe>';
        HideScrollIframe = false;
    }
    else
    {
        div_FaceBoxContentChild.innerHTML = ' <iframe src="' + URL + '" id="ifr_FaceBox" frameborder="0" scrolling="auto" onload="IframeLoadComplete(\'ifr_FaceBox\');"></iframe>';
    }
    return false;
}
function Show_FaceBoxContent(str_HTML)
{
    div_FaceBoxContentChild.innerHTML = str_HTML;
    Show_FaceBox(true);
    FaceBoxLoadComplete();  
    return false;
}
function Show_FaceBoxImage(str_ImageURL)
{
    div_FaceBoxContentChild.innerHTML = '<img src="' + str_ImageURL + '" onload="FaceBoxLoadComplete();" />';
    Show_FaceBox(true);
    return false;
}
//Reset lại vị trí của facebox để facebox nằm giữa  màn hình
function ResetPosition()
{
    var x = 0, y = 0;

    var centerX, centerY;
    
    if (self.innerHeight)
    {
        div_FaceBoxContent.style.height = div_FaceBoxContent.offsetHeight >= self.innerHeight - 50 ? (self.innerHeight - 50) + 'px' : div_FaceBoxContent.style.height;

        centerX = self.innerWidth;
        centerY = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        div_FaceBoxContent.style.height = div_FaceBoxContent.offsetHeight >= document.documentElement.clientHeight - 50 ? (document.documentElement.clientHeight - 50) + 'px' : div_FaceBoxContent.style.height;

        centerX = document.documentElement.clientWidth;
        centerY = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
        div_FaceBoxContent.style.height = div_FaceBoxContent.offsetHeight >= document.body.clientHeight - 50 ? (document.body.clientHeight - 50) + 'px' : div_FaceBoxContent.style.height;

        centerX = document.body.clientWidth;
        centerY = document.body.clientHeight;
    }

    var scrolledX, scrolledY;
    if (self.pageYOffset)
    {
        scrolledX = self.pageXOffset;
        scrolledY = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        scrolledX = document.documentElement.scrollLeft;
        scrolledY = document.documentElement.scrollTop;
    }
    else if (document.body)
    {
        scrolledX = document.body.scrollLeft;
        scrolledY = document.body.scrollTop;
    }
    
    x = centerX / 2 - div_FaceBox.offsetWidth / 2;
    y = centerY / 2 - div_FaceBox.offsetHeight / 2 + scrolledY;

    x = x < 0 ? 0 : x;
    y = y < 0 ? 0 : y;

    div_FaceBox.style.left = x + 'px';
    div_FaceBox.style.top = y + 'px';
}

//Hàm ajax
function AjaxFunction(URL_Get)
{
    var xmlHttp;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4)
        {
            div_FaceBoxContentChild.innerHTML = xmlHttp.responseText;
            FaceBoxLoadComplete();
        }
    }
    xmlHttp.open("GET", URL_Get, true);
    xmlHttp.send(null);
}

function UploadFile(FileUploadID, TypeUpload, TextBoxID, ImageID, MessageID, URL)
{
    var ctr_File = document.getElementById(FileUploadID);
    var tbx_URLUploaded = document.getElementById(TextBoxID);
    var str_FileName = ctr_File.value.replace(" ", "`");
    var str_OldFileName = tbx_URLUploaded.value.replace(" ", "`");

    var str_URL = URL + "?OldFileName=" + str_OldFileName + "&FileName=" + str_FileName + "&UploadType=" + TypeUpload;
    AjaxUpload(str_URL, TextBoxID, ImageID, MessageID);
    return false;
}

function AjaxUpload(URL_Get, TextBoxID, ImageID, MessageID)
{
    var tbx_URLUploaded = document.getElementById(TextBoxID);
    var lbl_Message = document.getElementById(MessageID);
    var img_Uploaded = document.getElementById(ImageID);
    var xmlHttp;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function()
    {

        if (xmlHttp.readyState == 4)
        {
            var array = xmlHttp.responseText.split("`");
            if (array[0] == "0")
            {
                lbl_Message.innerHTML = array[1] ? array[1] : "";
            }
            else
            {
                tbx_URLUploaded.value = img_Uploaded.src = array[1] ? "../" + array[1] : "";
            }

        }
        else
        {
            //var s = xmlHttp.responseText?xmlHttp.responseText:"";
        }

    }
    xmlHttp.open("GET", URL_Get, true);
    xmlHttp.send(null);
}



function AjaxCombobox(URL_Get, sel_ParentID, sel_ChildID, Type)
{
    var sel_Parent = document.getElementById(sel_ParentID);
    var sel_Child = document.getElementById(sel_ChildID);

    URL_Get = URL_Get + "?Ma=" + sel_Parent.value + "&Type=" + Type;
    var xmlHttp;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function()
    {

        if (xmlHttp.readyState == 4)
        {

            var xmlDoc = xmlHttp.responseXML.documentElement;
            var arrTable = xmlDoc.getElementsByTagName("Child");
            sel_Child.innerHTML = "";
            var strHTML = "";

            var sBrowser = navigator.userAgent;
            if (sBrowser.toLowerCase().indexOf('msie') > -1)
            {
                var newOpt = document.createElement("option");
                newOpt.value = "0";
                newOpt.innerText = "- - Không chọn - -";
                sel_Child.appendChild(newOpt);
            }

            for (var i = 0; i < arrTable.length; i++)
            {
                var opt = document.createElement("option");
                if (arrTable[i].getElementsByTagName("Ma")[0].text)
                {
                    opt.value = arrTable[i].getElementsByTagName("Ma")[0].text;
                    opt.innerText = arrTable[i].getElementsByTagName("Ten")[0].text;
                    sel_Child.appendChild(opt);
                }
                else
                {
                    opt.value = arrTable[i].getElementsByTagName("Ma")[0].textContent;
                    opt.text = arrTable[i].getElementsByTagName("Ten")[0].textContent;
                    strHTML += "<option value='" + opt.value + "'>" + opt.text + "</option>"
                }

                opt = null;
            }
            if (sBrowser.toLowerCase().indexOf('msie') < 0)
            {
                sel_Child.innerHTML = "<option value='0'>- - Không chọn - -</option>" + strHTML;
            }
        }
        else
        {

        }

    }
    xmlHttp.open("GET", URL_Get, true);
    xmlHttp.send(null);
}
//lấy thông tin Ajax
function AjaxGetInfo(URL_Get, div_ID)
{
    var xmlHttp;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function()
    {

        if (xmlHttp.readyState == 4)
        {
            document.getElementById(div_ID).innerHTML = xmlHttp.responseText;
        }

    }
    xmlHttp.open("GET", URL_Get, true);
    xmlHttp.send(null);

}





















//đổi ảnh khi trỏ chuột
function ChangeImage(mID, img_Path)
{
    var ctr_Image = document.getElementById(mID);
    ctr_Image.src = img_Path;
}

//Hàm kiểm tra định dạnh của ngày tháng nhập vào
function Check_FormatDate(Id_Text, Value_Text)
{
    var marker = "/";
    var submitDate = document.getElementById(Id_Text).value;
    var dateComp = submitDate.split(marker);
    var now = new Date();
    var yearNow = now.getFullYear();
    dayInmonth = new Array(12);
    dayInmonth[0] = 31;
    dayInmonth[1] = 29;
    dayInmonth[2] = 31;
    dayInmonth[3] = 30;
    dayInmonth[4] = 31;
    dayInmonth[5] = 30;
    dayInmonth[6] = 31;
    dayInmonth[7] = 30;
    dayInmonth[8] = 31;
    dayInmonth[9] = 31;
    dayInmonth[10] = 30;
    dayInmonth[11] = 31;

    //Kiểm tra theo định dạng dd/MM/yyyy
    if (dateComp.length != 3)
    {
        alert("Dữ liệu nhập vào " + Value_Text + " là không đúng định dạng (Ngày/Tháng/Năm)!");
        document.getElementById(Id_Text).focus();
        return false;
    }
    if (dateComp[2] % 4 == 0)
    {
        dayInmonth[1] = 29;
    }
    else
    {
        dayInmonth[1] = 28;
    }
    //Kiểm tra ngày
    if (dateComp[0] > dayInmonth[dateComp[1] - 1] || dateComp[0] < 1)
    {
        alert("Ngày nhập vào " + Value_Text + " phải từ (1 tới " + dayInmonth[dateComp[1] - 1] + ")!");
        document.getElementById(Id_Text).focus();
        return false;
    }
    //Kiểm tra tháng	
    if (dateComp[1] > 12 || dateComp[1] < 1)
    {
        alert("Tháng nhập vào " + Value_Text + " phải từ (1 tới 12)!");
        document.getElementById(Id_Text).focus();
        return false;
    }

    if (dateComp[2] < 1900)
    {
        alert("Năm nhập vào " + Value_Text + " phải từ 1900 trở lên!");
        document.getElementById(Id_Text).focus();
        return false;
    }
    return true;
}


//Kiểm tra ngày bắt đầu có lơn hơn ngày kết thúc không
function Compare_Date(Start_ID, Start_Text, Finish_ID, Finish_Text)
{
    var arrStartDate = document.getElementById(Start_ID).value.split("/");
    var arrFinishDate = document.getElementById(Finish_ID).value.split("/");
    var Start_Date = new Date(arrStartDate[2], arrStartDate[1] - 1, arrStartDate[0]);
    var Finish_Date = new Date(arrFinishDate[2], arrFinishDate[1] - 1, arrFinishDate[0]);
    if (Finish_Date <= Start_Date)
    {
        alert("(" + Finish_Text + ") phải lớn hơn (" + Start_Text + ")!");
        document.getElementById(Finish_ID).focus();
        return false;
    }
    return true;
}

//Kiểm tra ngày bắt đầu có lớn hơn hay nhỏ hơn ngày hiện tại
function Check_StartDate(Start_ID, Start_Text)
{
    var now = new Date();
    var CurrentDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    var strStartDay = document.getElementById(Start_ID).value
    var array = strStartDay.split("/"); //Ngay/Thang/Nam (00/00/0000)
    var Start_Date = new Date(array[2], array[1] - 1, array[0]);
    var strToday = now.getDate() + "/" + (now.getMonth() + Number(1)) + "/" + now.getFullYear();

    if (Start_Date <= CurrentDate)
    {
        alert("(" + Start_Text + ") phải lớn hơn Ngày Hiện Tại: " + strToday + "!");
        document.getElementById(Start_ID).focus();
        return false;
    }
    return true;
}

function OpenNewWindow(url)
{
    window.open(url);
    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;
}
function FocusTextBoxSearch(tbx_Search, IsFocus)
{

    if (tbx_Search)
    {
        if (IsFocus)
        {
            if (tbx_Search.value == "Nhập từ cần tìm")
                tbx_Search.value = "";
        }
        else
        {
            if (tbx_Search.value == "")
                tbx_Search.value = "Nhập từ cần tìm";
        }
    }
    return false;
}

function OpenNewWinDow(url)
{
    window.open(url);
    return false;
}





function ChangeCurentGia(ctr_Select, lbl_ID, DVT_Truoc, Gia_Truoc)
{
    var lbl_Gia = document.getElementById(lbl_ID);
    if (!ctr_Select || !lbl_Gia)
        return;
    var DVT_Sau = ctr_Select.options[ctr_Select.selectedIndex].value;
    //Chuyển kiểu số định dạng việt nam --> định dạng kiểu mỹ

    Gia_Truoc = Number(Gia_Truoc.replace(",", "."))
    var Gia_Sau = ChangePrice(DVT_Truoc, DVT_Sau, Gia_Truoc);
    lbl_Gia.innerHTML = NummberVN(Gia_Sau);

}
//Hàm làm tròn
function RoundNummer(num_So, num_DoDaiThapPhan)
{
    num_So = Number(num_So);
    var num_Temp = 1;
    for (var i = 0; i < num_DoDaiThapPhan; i++)
    {
        num_Temp *= 10;
    }
    return Math.round(num_So * num_Temp) / num_Temp;
}
//Chuyển kiểu sang định dạng số của việtnam VD:1,2000,333.00
function NummberVN(num_So)
{
    var num_SoVN = "";
    //s = num_So;
    //Làm tròn số
    num_So = RoundNummer(num_So, 2);
    //Lấy phần nguyên
    var so_Nguyen = Math.floor(num_So);
    //Lấy phần dư (Phần thập phân)
    var So_Du = num_So - so_Nguyen;
    //Làm tròn số dư
    So_Du = RoundNummer(So_Du, 2);
    So_Du = So_Du * 100;

    //Tạo mảng các chữ số thuộc phần nguyên
    var arr_s = (so_Nguyen + "").split("");

    var j = 1;
    for (var i = arr_s.length - 1; i >= 0; i--)
    {

        if (j == 4)
        {
            num_SoVN = arr_s[i] + "." + num_SoVN;
            j = 1;
        }
        else
        {
            num_SoVN = arr_s[i] + num_SoVN;
        }
        j++;
    }
    if (So_Du != 0)
    {
        num_SoVN = num_SoVN + "," + So_Du;
    }
    else
    {
        num_SoVN = num_SoVN + ",00";
    }
    return num_SoVN;
}
function ChangePrice(DVT_Truoc, DVT_Sau, Gia_Truoc)
{
    if (!vGoldSjcSell || !vForexs || !vCosts)
        return Gia_Truoc;

    var GiaSJC = Number(vGoldSjcSell.replace(",", "")) * 1000;
    var GiaUSD = 0;
    var GiaVND_Truoc = 0;
    var Gia_Sau = 0;
    for (var i = 0; i < vForexs.length; i++)
    {
        if (vForexs[i].toUpperCase() == "USD")
        {
            GiaUSD = Number(vCosts[i]) * 1000;
            break;
        }
    }
    //Chuyển tiền trước về dạng VND
    switch (DVT_Truoc.toUpperCase())
    {
        case "USD":
            GiaVND_Truoc = Gia_Truoc * GiaUSD;
            break;
        case "VND":
            GiaVND_Truoc = Gia_Truoc;
            break;
        case "SJC":
            GiaVND_Truoc = Gia_Truoc * GiaSJC;
            break;
        default:
            GiaVND_Truoc = Gia_Truoc;
    }

    switch (DVT_Sau.toUpperCase())
    {
        case "USD":
            Gia_Sau = GiaVND_Truoc / GiaUSD;
            break;
        case "VND":
            Gia_Sau = GiaVND_Truoc;
            break;
        case "SJC":
            Gia_Sau = GiaVND_Truoc / GiaSJC;
            break;
    }

    return RoundNummer(Gia_Sau, 2);
}
