
addLoadEvent(fixPngs);
addLoadEvent(externalLinks);

function addLoadEvent(func)
{
    var oldonload = window.onload;

    if (typeof window.onload != "function")
    {
        window.onload = func;
    }
    else
    {
        window.onload = function(){oldonload(); func();}
    }
}

function externalLinks()
{
    if (!document.getElementsByTagName) return;

    var anchors = document.getElementsByTagName("a");

    for (var i = 0; i < anchors.length; i++)
    {
        var anchor = anchors[i];

        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
        {
            anchor.target = "_blank";
        }
    }
}

function fixPngs()
{
    var notSupported = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);

    if (!document.getElementsByTagName || !notSupported) return;

    var images = document.getElementsByTagName("img");

    for (var i = 0; i < images.length; i++)
    {
        var img = images[i];

        if (img.className == "transparentPng")
        {
            src     = img.src;
            img.src = "templates/default/img/spacer.png";
            img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='" + src + "')";
        }
    }
}

function _verifyOneCheckMinimum(formName, prefix)
{
    n = "all";

    if (prefix)
    {
        n = prefix + n;
    }

    f = document.getElementById(formName);
    i = 0;
    j = 0;

    while (e = f.elements[i])
    {
        if ((e.type == "checkbox" || e.type == "radio") && e.id != n && e.checked)
        {
            if (!prefix || e.id.indexOf(prefix) != -1)
            {
                j++;
            }
        }

        i++;
    }

    return (j > 0);
}

function verifyOneCheckMinimum(formName, msg, prefix)
{
    result = _verifyOneCheckMinimum(formName, prefix);

    if (!result && msg)
    {
        alert(msg);
    }

    return result;
}

function verifyOneCheckMinimumAndConfirm(formName, msg1, msg2, prefix)
{
    result = _verifyOneCheckMinimum(formName, prefix);

    if (!result && msg1)
    {
        alert(msg1);
        return false;
    }

    if (confirm(msg2))
    {
        return true;
    }

    return false;
}

function toggleAllChecks(formName, prefix)
{
    i = 0;
    n = prefix + "CheckAll";
    e = document.getElementById(n);
    s = e.checked;
    f = document.getElementById(formName);

    while (e = f.elements[i])
    {
        if (e.type == "checkbox" && e.id != n)
        {
            if (!prefix || e.id.indexOf(prefix) != -1)
            {
                e.checked = s;
            }
        }

        i++;
    }
}

function confirmDlg(l, msg)
{
    if (confirm(msg))
    {
        document.location.href = l.href;
    }

    return false;
}

function popUp(l, n, f)
{
    if (!window.focus)
    {
        return true;
    }

    if (typeof(l) == 'string')
    {
        window.open(l, n, f);
    }
    else
    {
        window.open(l.href, n, f);
    }

    return false;
}

function goTo(l)
{
    window.location.href = l;
}

function goToIf(l, msg)
{
    if (confirm(msg))
    {
        if (typeof(l) == 'string')
        {
            window.location.href = l;
        }
        else
        {
            window.location.href = l.href;
        }

        return true;
    }

    return false;
}

function displayLangBlock(e, lang)
{
    d = document.getElementById(lang + "Block");

    if (e.checked)
    {
        d.style.display = "block";
    }
    else
    {
        d.style.display = "none";
    }
}
