/**
* A common library of functions used in various javascript libraries that I've
* written.
*
* @author Toby Miller <tmiller@tobymiller.com>
* @copyright Copyright (C) 2005, Toby Miller
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/

/*
* DO NOT EDIT BELOW THIS LINE
*/

/**
* add an event to an element
*
* @param object dom element to add event to
* @param string w3c-standard event name being handled
* @param object w3c-standard event object or simulated function
* @param boolean [optional] whether to truly process the onload event or not (default = false)
* @return void
*/
function addEvent(element, eventName, eventObject)
{
    if (element.addEventListener)
    {
        // this is a w3c-compliant browser, just wrap existing functions
        element.addEventListener(eventName, eventObject, false);
    }
    else if (element.attachEvent)
    {
        // collect optional parameters
        var processOnload = (arguments.length == 4) ? arguments[3] : false;

        if ((element == window) && (eventName == 'load') && (!processOnload))
        {
            // store this event, we'll do it in order with the other onloads
            addEvent._onloads.push(eventObject);
        }
        else
        {
            // add the new event
            element.attachEvent('on' + eventName, eventObject);
        }
    }
    return(true);
}
addEvent._onloads = [];
addEvent(window, 'load', function(){for(var i = 0; i < addEvent._onloads.length; i++){addEvent._onloads[i]();}}, true);

/**
* gets information pertinent to the page dimensions
*   screenheight    viewable screen height
*   screenwidth     viewable screen width
*   pageheight      full page height
*   pagewidth       full page width
*
* @param event
* @return void
*/
var screenheight    = 0;
var screenwidth     = 0;
var pageheight      = 0;
var pagewidth       = 0;
var centerx         = 0;
var centery         = 0;
function pageinfo()
{
    var dombody = document.body;

    if (document.documentElement)
    {
        if ((document.body.clientHeight == document.body.offsetHeight) && (document.body.offsetHeight == document.body.scrollHeight))
        {
            dombody = document.documentElement;
        }
        if ((document.body.clientHeight == 0) && (document.documentElement.clientHeight > 0))
        {
            dombody = document.documentElement;
        }
    }

    screenheight    = dombody.clientHeight;
    screenwidth     = dombody.clientWidth;
    pageheight      = dombody.scrollHeight;
    pagewidth       = dombody.scrollWidth;
    centerx         = (screenwidth / 2);
    centery         = (screenheight / 2);
}
addEvent(window, 'load', pageinfo);
addEvent(window, 'resize', pageinfo);

/**
* gets information pertinent to the mouse position on screen and keeps it set
* dynamically through the following variables:
*   screenx     x mouse position in relation to the viewable screen area
*   screeny     y mouse position in relation to the viewable screen area
*   pagex       x mouse position in relation to the entire page
*   pagey       x mouse position in relation to the entire page
*   scrollx     x screen scrolled position in relation to the actual page
*   scrolly     y screen scrolled position in relation to the actual page
*   centerx     x position of viewable screen area in relation to the page
*   centery     y position of viewable screen area in relation to the page
*
* @param event
* @return void
*/
var screenx = 0;
var screeny = 0;
var pagex   = 0;
var pagey   = 0;
var scrollx = 0;
var scrolly = 0;
var centerx = 0;
var centery = 0;
function mouseinfo(e)
{
    if (!e) var e = window.event;

    var dombody = document.body;

    if (document.documentElement)
    {
        if ((document.documentElement.scrollTop > 0) || (document.documentElement.scrollLeft > 0))
        {
            dombody = document.documentElement;
        }
    }

    var adjustleft = dombody.scrollLeft;
    var adjusttop = dombody.scrollTop;

    if (e.pageX || e.pageY)
    {
        pagex = e.pageX;
        pagey = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
        pagex = e.clientX + adjustleft;
        pagey = e.clientY + adjusttop;
    }

    scrollx = adjustleft;
    scrolly = adjusttop;
    screenx = pagex - scrollx;
    screeny = pagey - scrolly;
    centerx = (screenwidth / 2) + scrollx;
    centery = (screenheight / 2) + scrolly;
}
addEvent((document.all) ? document : window, 'mousemove', mouseinfo);

/**
* set the value of a form field
*
* @param string form name
* @param string field name
* @param string hex color
* @return void
*/
function setField(formname, fieldname, value)
{
    if (document.forms[formname])
    {
        if (document.forms[formname].elements[fieldname])
        {
            document.forms[formname].elements[fieldname].value = value;
        }
    }
}

/**
* set the index of a select field
*
* @param string form name
* @param string field name
* @param string hex color
* @return void
*/
function setFieldIndex(formname, fieldname, index)
{
    if (document.forms[formname])
    {
        if (document.forms[formname].elements[fieldname])
        {
            if (document.forms[formname].elements[fieldname].type == 'select-one')
            {
                document.forms[formname].elements[fieldname].selectedIndex = index;
            }
        }
    }
}

/**
* submit a form
*
* @param string form name
* @return void
*/
function submitForm(formname)
{
    document.forms[formname].submit();
}

/**
* change a form fields background color
*
* @param string form name
* @param string field name
* @param string hex color
* @return void
*/
function changeFieldColor(formname, fieldname, color)
{
    if (document.forms[formname])
    {
        document.forms[formname].elements[fieldname].style.backgroundColor = color;
    }
}

/**
* convert hexadecimal to rgb
*
* @param string hex color
* @return array array(r, g, b), null on failure
*/
function hex2rgb(hex)
{
    var regexp = new RegExp('^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$');
    var match = hex.toLowerCase().match(regexp);

    if (match)
    {
        var rgb = new Array(parseInt(match[1], 16), parseInt(match[2], 16), parseInt(match[3], 16));
        return(rgb);
    }
    else
    {
        return(null);
    }
}

/**
* convert rgb to hexadecimal
*
* @param integer red
* @param integer green
* @param integer blue
* @return string hex color
*/
function rgb2hex(red, green, blue)
{
    var hex = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
    var rgb = new Array();
    var k = 0;

    for (var i = 0; i < 16; i++) for (var j = 0; j < 16; j++) { rgb[k] = hex[i] + hex[j]; k++ }

    return('#' + rgb[red] + rgb[green] + rgb[blue]);
}

/**
* human friendly numeric sort
*
* @param array array to sort
* @return array sorted array
*/
function sortarray(arr)
{
    return arr.sort(
        function(a, b)
        {
            return (a.match(/\d+/) - b.match(/\d+/));
        }
    );
}

