/**
 * /Ben/Scripts (tm) - A collection of EMCAScript, JavaScript, ActionScript and VBScript
 *                     for enhacing web application user interfaces.
 *
 * @author    Benjamin DruShell
 * @website   http://www.drushell.net/
 * @copyright Copyright (c) 1998-2003,2005-2009, Benjamin DruShell
 * @license   /Ben/Scripts(tm) Library License
 *****************************************************
 * /Ben/Scripts(tm) Library License
 *
 * COPYRIGHT NOTICE:
 * Copyright (c) 1998-2003,2005-2009, Benjamin DruShell
 *
 * All rights reserved by Benjamin DruShell.
 *
 * DISCLAIMER:
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.  ALL RISK IS WITH YOU.
 *
 * PERMISSION NOTICE:
 * Permission is granted, free of charge, for redistribution and use of this
 * software, with or without modification, provided that the following
 * three (3) conditions are met:
 *
 *    1) This entire license must be included in the source code.
 *       This license consists of (a) the above Copyright Notice,
 *       (b) the above Disclaimer, and (c) this Permission Notice.
 *
 *    2) Condition 1 of this Permission Notice may be removed with the compensation
 *       of $1,000.00 U.S. Dollars paid to Benjamin DruShell, the copyright holder.
 *       Violation of this license is your acceptance to pay the compensation plus
 *       all legal, court, travel, and collection fees in obtaining compensation.
 *
 *    3) Neither the name of /Ben/Scripts(tm) nor the name of Benjamin DruShell
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * END OF LICENSE
 *
 * /Ben/Scripts(tm) is a trademark of Benjamin DruShell
 */

var prevOptionsBox = null;

/**
 * sumParentOffsetLeft - Recursive function to sum up offsetLeft and find an
 *                       absolute X-position of an element.
 * @param Object target
 * @return int
 */
function sumParentOffsetLeft(target) {
    var newOffset = target.offsetParent;
    if( newOffset == null ) {
        return target.offsetLeft;
    }
    return target.offsetLeft + sumParentOffsetLeft(newOffset);
}

/**
 * sumParentOffsetTop - Recursive function to sum up offsetTop and find an
 *                      absolute Y-position of an element.
 * @param Object target
 * @return int
 */
function sumParentOffsetTop(target) {
    var newOffset = target.offsetParent;
    if( newOffset == null ) {
        return target.offsetTop;
    }
    return target.offsetTop + sumParentOffsetTop(newOffset);
}

/**
 * showOptions - Show the options box.  Call this from menu bar or drop link.
 * @global Object prevOptionsBox
 * @param String id
 */
function showOptions(id) {
    optionsBoxId = "options"+id;
    selectBox = document.getElementById(id);
    optionsBox = document.getElementById(optionsBoxId);
    if( prevOptionsBox == optionsBox ) {
        hideOptions();
        return;
    }
    if( prevOptionsBox != null ) {
        hideOptions();
    }
    if( optionsBox ) {
        xPosition = sumParentOffsetLeft(selectBox);
        yPosition = sumParentOffsetTop(selectBox) + selectBox.offsetHeight;
        optionsBox.style.left = xPosition + 'px';
        optionsBox.style.top = yPosition + 'px';
        optionsBox.style.visibility = 'visible';
        prevOptionsBox = optionsBox;
    }
}

/**
 * hideOptions - Hide the options box.
 * @global Object prevOptionsBox
 * @param Event e
 */
function hideOptions(e) {
    if( prevOptionsBox ) {
        prevOptionsBox.style.visibility = 'hidden';
        prevOptionsBox = null;
    }
}

/**
 * selectOption - Call this function from the select fields.
 * @param Object Form
 * @param Object HiddenInput
 * @param String value
 */
function selectOption(form,selectBox,value) {
    selectBox.value = value;
    form.submit();
}
