﻿// JScript File
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function ShowPopup(popUp, widthpopUp, heightpopUp)
{
    window.open(popUp, "newWin", "width="+widthpopUp+", height="+heightpopUp+" ,scrollbars=yes");
}
function Logout() {
    var today = new Date();
    var cookie_date = new Date();
    cookie_date.setTime(today.getTime() + (-30 * 3600000 * 24));
    document.cookie = "userinfo= ;expires = " + cookie_date.toGMTString() + ";path=/";
    document.cookie = "AuthUser= ;expires = " + cookie_date.toGMTString() + ";path=/";
  
    if (window.location.href.indexOf("BackOfficeLite") == -1)
        window.location.href = "Default.aspx";
    else window.location.href = "../Default.aspx";
}
function ClearUploadCookies() {
    var today = new Date();
    var cookie_date = new Date();
    document.cookie = "videoinfo=;expires=" + cookie_date.toGMTString() + ";path=/";
}

// added by Liem
function swapImageSrc(img, src1, src2) {
    if(img.src.indexOf(src1) != -1)
        img.src =  src2
    else img.src = src1;
}

function swapCollapseExpandImage(img, collapseSrc, expandSrc) {
    if (img.src.indexOf("collapse") != -1)
        img.src = expandSrc;
    else img.src = collapseSrc;
}

function swapCollapseExpandOverImage(img, collapseSrc, expandSrc) {
    if (img.src.indexOf("collapse") != -1)
        img.src = collapseSrc;
    else img.src = expandSrc;
}

function onTextBoxKeyPress(txt, maxLen) {
    
    try {
        //if (txt.value.length > (maxLen - 1))
        //            return false;
        
        txt.value = txt.value.substring(0, maxLen);
    }catch(e){  
    }  
}
/*
$Tên hàm    : trimAll
$Mục đích   :   Hàm này dùng để cắt các khoảng trắng ở 2 đầu một chuỗi
$Tham số    :
- sString   :   chuỗi cần cắt các khoảng trắng 2 đầu
$Return     : 
string
*/

function trimAll(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 Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 ;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}


/*
$Tên hàm    : trimAll
$Mục đích   :   Hàm này dùng để cắt các khoảng trắng ở 2 đầu một chuỗi
$Tham số    :
- sString   :   chuỗi cần cắt các khoảng trắng 2 đầu
$Return     : 
string
*/

function trimAll(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 init() {
    var ellipsisSeq = 0;
    var ellipsisEls = document.getElementsByTagName('span');
    for (var i = 0; i < ellipsisEls.length; i++) {
        var el = ellipsisEls[i];
        if (el.className.match("ellipsis")) 
        {
            var maxLength = 24;
            var longText = el.innerHTML;
            if (!maxLength) maxLength = 10;
            var shortText = trim(stripTags(longText));
            if (shortText.length < maxLength) continue;
            shortText = shortText.substring(0, maxLength);
            var s1, s2;
            if (shortText.length > 15) {
                s1 = shortText.substring(0, 16);
                s2 = shortText.substring(16, shortText.length);
                            
               
            }           
            el.id = "ellipsis" + ellipsisSeq++;
            el.innerHTML = s1 + s2 + '...';
        }
    }
}

function stripTags(t) {
    while (t.match(/<.*>/)) {
        t = t.replace(/<[^>]*>/, "");
    }
    return t;
}

function trim(t) {
    return t.replace(/^[ \r\n\t]*/, "").replace(/[ \r\n\t]*$/, "");
}

function ellipsisShort(id) {
    document.getElementById(id + "short").style.display = "inline";
    document.getElementById(id + "long").style.display = "none";
}

function ellipsisLong(id) {
    document.getElementById(id + "short").style.display = "none";
    document.getElementById(id + "long").style.display = "inline";
}
function ShowLinkCreateGuide(id) {
    //safe function to show an element with a specified id

    if (document.getElementById) {
        if (document.getElementById(id).style.display == 'none') {
            document.getElementById(id).style.display = 'block';
        }
        else {
            document.getElementById(id).style.display = 'none';
        }

    }


}
function SetCreateGuideLinkText(link) {

    if (document.getElementById(link).innerHTML == "[ Click here to create new guide! ]") {
        document.getElementById(link).innerHTML = "[ Cancel! ]";
    }
    else {
        document.getElementById(link).innerHTML = "[ Click here to create new guide! ]";
    }
}

function toggle(div_id) {
    var el = document.getElementById(div_id);
    
    if (el.style.display == 'none') 
    {
        el.style.display = 'block';
        hideSelects('hidden');
        
    }
    else 
    {
        el.style.display = 'none';
        hideSelects('visible');
    }
}
function blanket_size(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('blanket');
    blanket.style.height = blanket_height + 'px';
    var popUpDiv = document.getElementById(popUpDivVar);
    popUpDiv_height = blanket_height / 10; //150 is half popup's height
    popUpDiv.style.top = popUpDiv_height + 'px';
    
}
function window_pos(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById(popUpDivVar);
    window_width = window_width / 4; //25% screen width
    popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
    blanket_size(windowname);
    window_pos(windowname);
    toggle('blanket');
    toggle(windowname);
}
 // In IE, select elements hover on top of the lightbox
function hideSelects(visibility) {
        selects = document.getElementsByTagName('select');
        for (i = 0; i < selects.length; i++) {
            selects[i].style.visibility = visibility;
        }

}
function close_all_popups() {
    var popups = document.getElementsByTagName('DIV');
    
        for (var i = 0; i < popups.length; i++) {
            if (popups[i].className == "popUpDiv") {
                popups[i].style.display = "none";
            }
        }
        var blanket = document.getElementById('blanket');
        blanket.style.display = "none";
        hideSelects('visible');
    }
function detectBrowser() {
    var val = navigator.userAgent.toLowerCase();
    if (val.indexOf("firefox") > -1) {
        return "firefox";
    }
    else if (val.indexOf("opera") > -1) {
        return "opera";
    }
    else if (val.indexOf("msie") > -1) {
        return "msie";
    }
    else if (val.indexOf("safari") > -1) {
        return "safari";
    }
}
function GetExtensionFile(FilePath) {
    if (FilePath == "") return;
    while (FilePath.indexOf("\\") != -1)
        FilePath = FilePath.slice(FilePath.indexOf("\\") + 1);
    Extension = FilePath.slice(FilePath.indexOf(".") + 1).toLowerCase();
    return Extension;
}
function getURLParam(strParamName) {
    var strReturn = "";
    var strHref = window.location.href;
    if (strHref.indexOf("?") > -1) {
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for (var iParam = 0; iParam < aQueryString.length; iParam++) {
            if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return unescape(strReturn);
}

function SelectAllCheckboxes(spanChk) {
    // Added as ASPX uses SPAN for checkbox
    var oItem = spanChk.children;
    var theBox = (spanChk.type == "checkbox") ? spanChk : spanChk.children.item[0];
    xState = theBox.checked;
    elm = theBox.form.elements;

    for (i = 0; i < elm.length; i++)
        if (elm[i].type == "checkbox" && elm[i].id != theBox.id) {
        if (elm[i].checked != xState)
            elm[i].click();
    }
}