function sessionTimeout(timeout, url) {
    var timeoutFunc = "alert('Your Session Has Timed Out');document.location.replace('" + url + "');";
    setTimeout(timeoutFunc, timeout);
}

function inActiveUser(url) {
    alert('Your Account Has Been Deactivated');
    document.location.replace(url);
}

function confirmDelete(displayName) {
    return confirm('Are you sure you want to delete ' + displayName + '?');
}

function popup_news(stockCode, itemNumber) {
    var url = 'http://data.iguana2.com/dnb/news-item?Number=' + itemNumber + '&Code=' + stockCode;
    window.open(url, 'news', 'resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no,width=800,height=550');
    return false;
}

function popup_external(url) {
    window.open(url, 'external', 'resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,width=800,height=550');
    return false;
}

function getElementsByClassName(node, tagName, className) {
    if (!tagName)
        tagName = "*";
    if (!node)
        node = document.getElementsByTagName("body")[0];

    var returnedElements = [];
    var classNameSearchRegExp = new RegExp('\\b' + className + '\\b');
    var elements = node.getElementsByTagName(tagName);

    for (var i = 0, j = elements.length; i < j; i++)
        if (classNameSearchRegExp.test(elements[i].className))
        returnedElements.push(elements[i]);

    return returnedElements;
}

// This applies the styles to fix for Quirks mode under IE .. MUST be a UL.ASPNet-Menu control.  Can append manually.
startList = function() {
    if (document.all && document.getElementById) {
        menuElements = getElementsByClassName(null, "UL", "AspNet-Menu");
        for (var j = 0; j < menuElements.length; j++) {
            for (var i = 0; i < menuElements[j].childNodes.length; i++) {
                if (menuElements[j].childNodes[i].nodeName == "LI") {
                    menuElements[j].childNodes[i].onmouseover = function() {
                        this.className += " over";
                    }
                    menuElements[j].childNodes[i].onmouseout = function() {
                        this.className = this.className.replace(" over", "");
                    }
                }
            }
        }
    }
}

function addWindowOnLoad(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addWindowOnLoad(startList);

function ShowWebDialog(controlId, parameter) {
    var webButton = ISGetObject(controlId);
    var webDialogBox = ISGetObject(parameter);

    webDialogBox.ShowDialog();
    return true;
}

function SetDisplayAttribute(clientId, displayAttribute) {
    if (!clientId)
        return;
    if (document.layers &&
	    document.layers[clientId])
        document.layers[clientId].display = displayAttribute;
    else if (document.all &&
	         document.all[clientId] &&
	         document.all[clientId].style)
        document.all[clientId].style.display = displayAttribute;
    else if (document.getElementById &&
	         document.getElementById(clientId) &&
	         document.getElementById(clientId).style)
        document.getElementById(clientId).style.display = displayAttribute;
}

function GetDisplayAttribute(clientId) {
    if (!clientId)
        return;
    if (document.layers &&
	    document.layers[clientId])
        return document.layers[clientId].display;
    else if (document.all &&
	         document.all[clientId] &&
	         document.all[clientId].style)
        return document.all[clientId].style.display;
    else if (document.getElementById &&
	         document.getElementById(clientId) &&
	         document.getElementById(clientId).style)
        return document.getElementById(clientId).style.display;
}

function SetVisibilityAttribute(clientId, visibilityAttribute) {
    if (!clientId)
        return;
    if (document.layers &&
	    document.layers[clientId])
    // Note old Netscape 4 version which uses hide/show instead of hidden / visible.  Parameter though uses consistent (current) attribute
        document.layers[clientId].visibility = (visibilityAttribute == 'hidden' ? 'hide' : 'show');
    else if (document.all &&
	         document.all[clientId] &&
	         document.all[clientId].style)
        document.all[clientId].style.visibility = visibilityAttribute;
    else if (document.getElementById &&
	         document.getElementById(clientId) &&
	         document.getElementById(clientId).style)
        document.getElementById(clientId).style.visibility = visibilityAttribute;
}

function GetVisibilityAttribute(clientId) {
    if (!clientId)
        return;
    if (document.layers &&
	    document.layers[clientId])
    // Note old Netscape 4 version which uses hide/show instead of hidden / visible.  Return consistent (current) attribute
        return (document.layers[clientId].visibility == 'hide' ? 'hidden' : 'visible');
    else if (document.all &&
	         document.all[clientId] &&
	         document.all[clientId].style)
        return document.all[clientId].style.visibility;
    else if (document.getElementById &&
	         document.getElementById(clientId) &&
	         document.getElementById(clientId).style)
        return document.getElementById(clientId).style.visibility;
}

// Visible attribute still leaves the space in the page, we want it to collapse.  Don't really want to change the extender itself,
// so am changing the display attribute to be inlin with the visibiltiy attribute.  This will cause the page to expand / collapse appropriately
function SyncDisplayAttributeToVisibility(validationElementIds) {
    if (validationElementIds &&
        validationElementIds.length) {
        for (var i = 0; i < validationElementIds.length; i++) {
            SetDisplayAttribute(validationElementIds[i], GetVisibilityAttribute(validationElementIds[i]) == 'hidden' ? "none" : "block");
        }
    }
}

function SetCssClass(clientId, className) {
    if (!clientId)
        return;
    if (document.layers &&
	    document.layers[clientId])
    // Note old Netscape 4 version which uses hide/show instead of hidden / visible.  Parameter though uses consistent (current) attribute
        document.layers[clientId].className = className;
    else if (document.all &&
	         document.all[clientId] &&
	         document.all[clientId].style)
        document.all[clientId].className = className;
    else if (document.getElementById &&
	         document.getElementById(clientId) &&
	         document.getElementById(clientId).style)
        document.getElementById(clientId).className = className;
}

// Select all or none check box items
function SelectAllCheckbox(cb, checked, f) {
    var cbl = document.getElementById(cb);

    if (cbl != null) {
        $(cbl).find("input").each(function(e) {
            this.checked = checked;
        });
    }

    // call function if provided
    if (f != null && f.length > 0) {
        eval(f);
    }
}

// Is a checkbox item selected
function IsCheckboxItemSelected(cb) {
    var cbl = document.getElementById(cb);
    var hasChecked = false;

    if (cbl != null) {
        var checkBoxArray = cbl.getElementsByTagName('input');
        for (var i = 0; i < checkBoxArray.length; i++) {
            if (checkBoxArray[i].checked == true) {
                hasChecked = true;
                break;
            }
        }
    }
    return hasChecked;
}

function CheckCreditPoints(l, lc, cl, ch, ac, gb) {
    var li = $get(l).getElementsByTagName('input');
    var cost = $get(cl);

    if (IsCheckboxItemSelected(l) == false) {
        cost.innerHTML = 0;
        EnableButton($get(gb).id, 'highlight', false);
        return;
    }

    var fc = 0;
    var p = $get(lc);

    for (var i = 0; i < li.length; i++) {
        if (li[i].checked == true) {
            fc += parseInt(li[i].getAttribute('creditCost'));
        }
    }

    cost.innerHTML = addCommas(fc);
    $get(ch).value = fc;

    if (fc > ac) {
        p.style.display = 'block';
        EnableButton($get(gb).id, 'highlight', false);
    }
    else {
        p.style.display = 'none';
        EnableButton($get(gb).id, 'highlight', true);
    }
}

// Enable or disbale generate report button
function AllowReportGeneration(cb, gb, checked) {
    if (checked == null) {
        checked = IsCheckboxItemSelected(cb);
    }

    EnableButton($get(gb).id, 'highlight', checked);
    return true;
}


function UpdateNodesOnClient(t, sv) {
    ignoreCheck = true;
    var nodeList = t.GetArrayNodes();
    for (var i = nodeList.length - 1; i >= 0; i--) {
        var currNode = nodeList[i];
        if (sv.value.indexOf(',' + currNode.Value + ',') > 0) {
            if (currNode.Checked == false)
                currNode.Check();
        }
        else {
            if (currNode.Checked == true)
                currNode.UnCheck();
        }
    }
    ignoreCheck = false;
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
