﻿function blurDiv(divId) {
    var div = document.getElementById(divId);
    if (div) {
        div.style.color = "#999999";
    }
}

var sessionTimeoutTimer;
function setSessionTimeoutTimer(ms, timeouturl) {
    clearTimeout(sessionTimeoutTimer);
    sessionTimeoutTimer = setTimeout("window.location.replace(\'" + timeouturl + "\');", ms);
}

function getPageExtension() {
    if (window.location.search != null && window.location.search != "") {
        var path = window.location.toString().substring(0, window.location.toString().lastIndexOf(window.location.search))
        alert(path.substring(path.lastIndexOf('.')));
    } else {
        alert(window.location.toString().substring(window.location.toString().lastIndexOf('.')));
    }
}

function OnLoadWorkUnitClick(hl,txtWorkUnitId) {
    var txt = document.getElementById(txtWorkUnitId);
    if (hl && txt) {
        if (hl.href.indexOf("&srch=") > -1) {
            hl.href = hl.href.substring(0, hl.href.indexOf("&srch="));
        }

        hl.href = hl.href + "&srch=" + txt.value;
    }
}

function CCMenuSwapCls(itm, cls) {
    if (itm) itm.className = cls;
}

function BlurListbox(id) {
    var listbox = document.getElementById(id);
    if (listbox != null) {
        listbox.disabled = true;
    }
}

function DeleteSpecieItems(e, btnId) {
    var key = window.event ? e.keyCode : e.which;
    if (key == 46) // del
        Click(btnId);
}

function Click(updLnkId) {
    var updLnkClientId = (updLnkId ? updLnkId.replace(/\$/g, "_") : updLnkId);
    var lnkObj = document.getElementById(updLnkClientId);
    if (lnkObj && lnkObj.tagName == "A") {
        if (lnkObj.dispatchEvent && !lnkObj.click) {
            // Firefox link click simulate to
            var evt = document.createEvent("MouseEvents");
            evt.initMouseEvent("click", true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
            if (lnkObj.dispatchEvent(evt) && lnkObj.href.indexOf("__doPostBack") > -1) {
                var theForm = document.forms["aspnetForm"];
                if (!theForm) {
                    theForm = document.aspnetForm;
                }
                if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
                    theForm.__EVENTTARGET.value = updLnkId;
                    theForm.__EVENTARGUMENT.value = "";
                    theForm.submit();
                }
            }
        } else {
            // IE and others link click simulate
            lnkObj.click();
        }
    } else if (lnkObj.click) {
        lnkObj.click();
    } else {
        lnkObj.fireEvent("click");
    }
}

function ToggleLevel(level,pnlId, img, chkid) {
    var pnl = document.getElementById(pnlId);
    var chk = document.getElementById(chkid);
    if (pnl != null && chk != null) {
        chk.checked = !chk.checked;
        
        for (var i = 0; i < pnl.childNodes.length; i++) {
            var subPnl = pnl.childNodes[i];
            if (subPnl.attributes["level"] != null) {
                var subLevel = subPnl.attributes["level"].value;
                if (chk.checked) {
                    if (subLevel.indexOf(level) == 0 && subLevel.split("|").length - 1 == level.split("|").length)
                        subPnl.style.display = "";
                } else {
                    if (subLevel.indexOf(level) == 0 && subLevel.split("|").length > level.split("|").length) 
                        subPnl.style.display = "none";
                }
            }
        }

        if (img != null) {
            if (chk.checked && img.attributes["urlopen"]) {
                img.src = img.attributes["urlopen"].value;
            } else if (!chk.checked && img.attributes["urlclosed"]) {
                img.src = img.attributes["urlclosed"].value;
            }
        }
    }
}

function ToggleImporterPanelVertical(pnlId,img,chkid) {
    var pnl = document.getElementById(pnlId);
    if (pnl != null) {
        var bVisible;
        var h = 0;

        if (pnl.initialized === undefined) {
            if (pnl.style.display == "none") {
                var myVerticalSlide = new Fx.Slide(pnlId, { mode: 'vertical' });
                myVerticalSlide.hide();
                pnl.style.display = "";
            }

            pnl.initialized = true;
        }
        
        for (i = 0; i < pnl.childNodes.length; i++) {
            if(pnl.childNodes[i].clientHeight)
                h += pnl.childNodes[i].clientHeight
            
            if(pnl.childNodes[i].style) {
                 h += (pnl.childNodes[i].style.marginTop != "" ? parseInt(pnl.childNodes[i].style.marginTop) : 0)
                    + (pnl.childNodes[i].style && pnl.childNodes[i].style.marginBottom != "" ? parseInt(pnl.childNodes[i].style.marginBottom) : 0)
                    + (pnl.childNodes[i].style && pnl.childNodes[i].style.borderTop != "" ? parseInt(pnl.childNodes[i].style.borderTop) : 0)
                    + (pnl.childNodes[i].style && pnl.childNodes[i].style.borderBottom != "" ? parseInt(pnl.childNodes[i].style.borderBottom) : 0);
             }
        }
        pnl.style.height = (h + 4) + "px";
        pnl.style.width = pnl.offsetWidth + "px";
        
        myVerticalSlide = new Fx.Slide(pnlId, { mode: 'vertical' });
        $(pnlId).getParent().setStyle('width', (pnl.offsetWidth + 2) + "px");   
        
        if (img.src.indexOf(img.attributes["urlclosed"].value) > -1) { //(pnl.style.display == "none") {
            myVerticalSlide.slideIn();
            bVisible = true;
        } else {
            myVerticalSlide.slideOut();
            bVisible = false;
        }

        var chk = document.getElementById(chkid);
        if (chk) chk.checked = bVisible

        myVerticalSlide.addEvent('complete', function() {
            if (img != null) {
                if (bVisible && img.attributes["urlopen"]) {
                    img.src = img.attributes["urlopen"].value;
                } else if (!bVisible && img.attributes["urlclosed"]) {
                    img.src = img.attributes["urlclosed"].value;
                }
            }
        });
    }
}

function SetIframeSrc(ifrId, url) {
    var iframe = document.getElementById(ifrId);
    if (iframe != null) {
        if (document.addEventListener) {
            document.addEventListener("DOMContentLoaded", init(iframe, url), false);
        } else if (document.onreadystatechange == null) {
            document.onreadystatechange = function() {
                if (document.readyState == 'complete') {
                    init(iframe, url);
                }
            };
        } else {
            window.onload = init(iframe, url);
        }
    }
}

function init(iframe, url) { iframe.src = url; }
