// -*- Mode: JavaScript; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
//////////////////////////////////////////////////////////////////////////////
// tacticx GmbH After Sales Manager
//
// Deprecated. Old-style interface to dynamic elements.
//
// Copyright(C) 2002-2008, tacticx GmbH.
// All rights reserved.
//
// $Id: common.js 2688 2011-01-05 12:45:02Z flemm $
//

//----------------------------------------------------------------------------
var fInEnv = false;     // Initialisation flag.

var fDHTML = false,     // DHTML-enabled browser.
    fDOM   = false;     // DOM  -enabled browser.

var fEnMS = false,      // MS browser.
    fEnNS = false,      // NS browser.
    fEnOP = false;      // OP browser.

//----------------------------------------------------------------------------
function initEnvironment()
{
    if (fInEnv)
    {
        return;
    }

    if (window.opera)
    {
        fEnOP = true;
    }

    if (typeof (document.getElementById) != 'undefined')
    {
        fDHTML = true;
        fDOM   = true;
    }

    if (document.all
    &&  !fEnOP)
    {
        fDHTML = true;
        fEnMS  = true;
    }

    if (window.netscape
    &&  window.screen
    &&  !fDOM
    &&  !fEnOP)
    {
        fDHTML = true;
        fEnNS  = true;
    }

    fInEnv = true;
}

//----------------------------------------------------------------------------
function setRowStyle (_row, _class)
{
    var aCells = null;
    var cCells;

    initEnvironment();

    if (typeof (document.getElementsByTagName) != 'undefined')
    {
        aCells = _row.getElementsByTagName ('td');
    }
    else
    if (typeof (_row.cells) != 'undefined')
    {
        aCells = _row.cells;
    }
    else
    {
        return (false);
    }

    cCells = aCells.length;

    if (_class)
    {
        var i;

        if (fDOM
        &&  fEnNS)
        {
            for (i = 0; i < cCells; i++)
            {
                if (aCells[i].getAttribute ('class') != '')
                {
                    aCells[i].setAttribute ('class', _class, 0);
                }
            }
        }
        else
        {
            for (i = 0; i < cCells; i++)
            {
                if (aCells[i].className != '')
                {
                    aCells[i].className  = _class;
                }
            }
        }
    }

    return (true);
}

//----------------------------------------------------------------------------
function setCellStyle (_cell, _class)
{
    initEnvironment();

    if (_class)
    {
        if (fDOM && fEnNS)
        {
            if (_cell.getAttribute ('class') != '')
            {
                _cell.setAttribute ('class', _class, 0);
            }
        }
        else
        {
            if (_cell.className != '')
            {
                _cell.className = _class;
            }
        }
    }

    return (true);
}

//----------------------------------------------------------------------------
function setCellStyleMex (_cell, _class, _classLink)
{
    var aLinks = null;
    var cLinks;

    initEnvironment();

    if (typeof (document.getElementsByTagName) != 'undefined')
    {
        aLinks = _cell.getElementsByTagName ('a');
    }
    else
    if (typeof (_cell.cells) != 'undefined')
    {
        aLinks = _cell.cells;
    }
    else
    {
        return (false);
    }

    cLinks = aLinks.length;

    if (_class)
    {
        var i;

        if (fDOM
        &&  fEnNS)
        {
            if (_cell.getAttribute ('class') != '')
            {
                _cell.setAttribute ('class', _class, 0);
            }

            for (i = 0; i < cLinks; i++)
            {
                if (aLinks[i].getAttribute ('class') != '')
                {
                    aLinks[i].setAttribute ('class', _classLink, 0);
                }
            }
        }
        else
        {
            if (_cell.className != '')
            {
                _cell.className  = _class;
            }

            for (i = 0; i < cLinks; i++)
            {
                if (aLinks[i].className != '')
                {
                    aLinks[i].className = _classLink;
                }
            }
        }
    }

    return (true);
}

//----------------------------------------------------------------------------
function setIdStyle (_id, _class)
{
    var oElement = null;

    initEnvironment();

    if (!fDOM)
    {
        return (false);
    }

    oElement = document.getElementById (_id);

    if (oElement
    &&  _class)
    {
        if (fDOM
        &&  fEnNS)
        {
            oElement.setAttribute ('class', _class, 0);
        }
        else
        {
            oElement.className = _class;
        }
    }
}

//----------------------------------------------------------------------------
function toggleIdStyle (_id, _class1, _class2)
{
    var oElement = null;

    initEnvironment();

    if (!fDOM)
    {
        return (false);
    }

    oElement = document.getElementById (_id);

    if (oElement)
    {
        if (fDOM
        &&  fEnNS)
        {
            if (oElement.getAttribute ('class') == _class1)
            {
                oElement.setAttribute ('class', _class2, 0);
            }
            else
            {
                oElement.setAttribute ('class', _class1, 0);
            }
        }
        else
        {
            if (oElement.className == _class1)
            {
                oElement.className =  _class2;
            }
            else
            {
                oElement.className =  _class1;
            }
        }
    }
}

/******/

//----------------------------------------------------------------------------
function switchCellClass (_cell, _class, _flag)
{
    initEnvironment();

    if (fDOM && fEnNS)
    {
        var k;
        var attr =
            _cell.getAttribute ('class').split (' ');

        for (k = 0; k < attr.length && attr[k] != _class; k++);

        if (!_flag && attr.length && attr[k] == _class)
        {   // Remove class identifier.
            attr[k] = null;
        }
        else
        if (_flag && !(attr.length && attr[k] == _class))
        {   // Add class identifier.
            attr.push (_class);
        }

        attr =
            attr.join (' ');

        _cell.setAttribute ('class', attr, 0);
    }
    else
    {
        var k;
        var attr =
            _cell.className.split (' ');

        for (k = 0; k < attr.length && attr[k] != _class; k++);

        if (!_flag && attr.length && attr[k] == _class)
        {   // Remove class identifier.
            attr[k] = null;
        }
        else
        if (_flag && !(attr.length && attr[k] == _class))
        {   // Add class identifier.
            attr.push (_class);
        }

        attr =
            attr.join (' ');

        _cell.className = attr;
    }

    return (true);
}

//----------------------------------------------------------------------------
function switchRowClass (_row, _class, _flag)
{
    var aCells = null;

    initEnvironment();

    if ('undefined' != typeof (document.getElementsByTagName))
    {
        aCells =
            _row.getElementsByTagName ('td');
    }
    else
    if ('undefined' != typeof (_row.cells))
    {
        aCells =
            _row.cells;
    }
    else
    {
        return (false);
    }

    if (aCells.length)
    {
        var i;

        if (fDOM && fEnNS)
        {
            for (i = 0; i < aCells.length; i++)
            {
                var k;
                var attr =
                    aCells[i].getAttribute ('class').split (' ');

                for (k = 0; k < attr.length && attr[k] != _class; k++);

                if (!_flag && attr.length && attr[k] == _class)
                {   // Remove class identifier.
                    attr[k] = null;
                }
                else
                if (_flag && !(attr.length && attr[k] == _class))
                {   // Add class identifier.
                    attr.push (_class);
                }

                attr =
                    attr.join (' ');

                aCells[i].setAttribute ('class', attr, 0);
            }
        }
        else
        {
            for (i = 0; i < aCells.length; i++)
            {
                var k;
                var attr =
                    aCells[i].className.split (' ');

                for (k = 0; k < attr.length && attr[k] != _class; k++);

                if (!_flag && attr.length && attr[k] == _class)
                {   // Remove class identifier.
                    attr[k] = null;
                }
                else
                if (_flag && !(attr.length && attr[k] == _class))
                {   // Add class identifier.
                    attr.push (_class);
                }

                attr =
                    attr.join (' ');

                aCells[i].className = attr;
            }
        }
    }

    return (true);
}






//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
function fireEvent (obj_, event_)
{
    if (document.createEvent)
    {
        var ev =
            document.createEvent ("HTMLEvents");

        ev.initEvent (event_, true, true);

        return (!obj_.dispatchEvent (ev));
    }
    else
    {   // IE-specific.
        var ev =
            document.createEventObject();

        return (obj_.fireEvent ('on' + event, ev));
    }
}
