/********************************************************************

    util.js

    Various DHTLM utility functions.

    Created by:     Claudio Felber (claudio.felber@perron2.ch)
    Date created:   2005-01-27
    Modified by:    Claudio Felber (claudio.felber@perron2.ch)
    Date modified:  2005-01-27
    Version:        1.0.0

    Copyright (c) 2005, Perron2 GmbH, All Rights Reserved.

*********************************************************************/

/********************************************************************
    focus_first_field()

    Sets the focus to the first field in a form.
*********************************************************************/

function focus_first_field()
{
    var elems = document.forms[0].elements;

    for (var i = 0; i < elems.length; i++)
    {
        if (elems[i].type != "hidden")
        {
            elems[i].focus();
            elems[i].select();
            return;
        }
    }
}

/********************************************************************
    attachEventHandler(event, func)

    Attaches the event handler "func" to the event of type "event".
*********************************************************************/

function attachEventHandler(event, func)
{
	if (typeof(func) != "function")
	{
		return;
	}

	if (typeof(document.eventHandlers) == "undefined")
	{
		document.eventHandlers = new Array();
	}

	if (typeof(document.eventHandlers[event]) == "undefined")
	{
		document.eventHandlers[event] = new Array();
	}

	if (window.attachEvent)
	{
		window.attachEvent(event, func);
	}
	else
	{
		if (typeof(eval("window." + event)) != "function")
		{
			eval("window." + event + " = function () {" +
                "if ((typeof(document.eventHandlers['" + event + "'])).toLowerCase() != 'undefined') { " +
                    "for (i = 0; i < document.eventHandlers['" + event + "'].length; i++) { " + 
                        "document.eventHandlers['" + event + "'][i]();" +
                    "}}}");
		}

		document.eventHandlers[event][ document.eventHandlers[event].length] = func;
	}
}

/********************************************************************
    setMinWidth(element, minWidth)

    Installs a handler that allows for min-width support in browsers
    that do not support this CSS attribute.
*********************************************************************/

function setMinWidth(element, minWidth)
{
	if (document.getElementById && navigator.appVersion.indexOf("MSIE") > -1 && !window.opera)
	{
		document.mw_element = element;
		document.mw_min_width = minWidth;

		attachEventHandler("onload", controlMinWidth);
		attachEventHandler("onresize", controlMinWidth);
	}
}

/********************************************************************
    controlMinWidth

    Handler called for onload and onresize events when a min-width
    CSS attribute has to be simulated for an element.
*********************************************************************/

function controlMinWidth()
{
    var elem = document.mw_element;

    if (typeof(elem) == "string")
    {
        elem = document.getElementById(elem);
    }

    var offset = parseInt(0 + elem.currentStyle.paddingLeft)
        + parseInt(0 + elem.currentStyle.paddingRight)
        + parseInt(0 + elem.currentStyle.borderLeftWidth)
        + parseInt(0 + elem.currentStyle.borderRightWidth);

	if ((document.body.clientWidth - (elem.offsetLeft * 2)) <= (document.mw_min_width - offset)
        && parseInt(0 + elem.style.width) != document.mw_min_width)
    {
		elem.style.width = document.mw_min_width + "px";
    }
	else if (document.body.clientWidth > (document.mw_min_width + offset + elem.offsetLeft * 2))
    {
		elem.style.width = "auto";
    }
}

/********************************************************************
    getElemPos(elem)

    Returns the position (left, top) of an element.
*********************************************************************/

function getElemPos(elem)
{
    if (typeof(elem) == "string")
    {
        elem = document.getElementById(elem);
    }

    var left = 0;
    var top = 0;

    while (elem)
    {
        left += elem.offsetLeft;
        top += elem.offsetTop;
        elem = elem.offsetParent;
    }

    if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined")
    {
        left += document.body.leftMargin;
        Top += document.body.topMargin;
    }

    return { left: left, top: top };
}

/********************************************************************
    getElemSize(elem)

    Returns the dimensions of an element.
*********************************************************************/

function getElemSize(elem)
{
    if (typeof(elem) == "string")
    {
        elem = document.getElementById(elem);
    }

    var width;
    var height;

    if (elem.offsetWidth)
    {
        width = elem.offsetWidth;
        height = elem.offsetHeight;
    }
    else if (elem.clip && elem.clip.width)
    {
        width = elem.clip.width;
        height = elem.clip.height;
    }
    else if (elem.style && elem.style.pixelWidth)
    {
        width = elem.style.pixelWidth;
        height = elem.style.pixelHeight;
    }

    return { width: width, height: height }
}

/********************************************************************
    getWindowClientSize(elem)

    Returns the dimensions of the window client area.
*********************************************************************/

function getWindowClientSize()
{
    var width;
    var height;

    if (window.innerWidth)
    {
        width = window.innerWidth;
        height = window.innerHeight;
    }
    else if (document.body.parentElement.clientWidth)
    {
        width = document.body.parentElement.clientWidth;
        height = document.body.parentElement.clientHeight;
    }
    else
    {
        width = document.body.clientWidth;
        height = document.body.clientHeight;
    }

    return { width: width, height: height };
}