/*
 * This is a set of functions to allow manipulation of document objects that
 * are part of the DOM specified by the W3C.
 *
 * Copyright (C) 2003 by John Glorioso, Zitego Solutions, LLC
 * <jglorioso@zitego.com>. All rights reserved.
 *
 * Redistribution and use in source forms, with or without modification, are
 * permitted provided that the following condition is met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @author John Glorioso
 * @version $Id: dhtml.js,v 1.1 2006/10/02 23:55:07 jglorioso Exp $
 */

/*
 * Returns the given object specified by name and given the document to search
 * in. If doc is not specified, then document is assumed.
 *
 * @param name The name of the object to get.
 * @param doc The document to search in.
 */
function getObject(name, doc)
{
    var obj;
    if (!doc) doc = document;

    //First look in the frames. The name would be <obj_name>?<frame_name>
    var qIndex = name.indexOf("?");
    if (qIndex > 0 && parent.frames.length)
    {
        doc = parent.frames[name.substring(qIndex+1)].document;
        name = name.substring(0, qIndex);
    }
    if ( !(obj=doc[name]) && doc.all) obj = doc.all[name];

    //Look in the forms
    if (!obj && doc.forms)
    {
        for (var i=0; i<doc.forms.length; i++)
        {
            obj = doc.forms[i][name];
            if (obj) break;
        }
    }

    //Check out netscape layers objects
    if (!obj)
    {
        for (var i=0; doc.layers && i<doc.layers.length; i++)
        {
            obj = getObject(name, doc.layers[i].document);
            if (obj) break;
        }
    }

    //Finally check getElementById
    if (!obj && document.getElementById) obj = document.getElementById(name);

    return obj;
}

/*
 * Changes the background color of the named object to the specified color.
 *
 * @param objName The name of the object to change the color of.
 * @param color The color to change to.
 */
function swapColor(objName, color)
{
    var obj = getObject(objName);
    if ( obj != void(0) ) obj.style.backgroundColor = color;
}