// Defines a class that contains utility methods used by the coral application.
function CoralUtils()
{	
}

CoralUtils.addBookmark = "<<uninitialized>>";
CoralUtils.bookMarkMsg1 = "<<uninitialized>>";
CoralUtils.bookMarkMsg2 = "<<uninitialized>>";
CoralUtils.bookMarkMsg3 = "<<uninitialized>>";
CoralUtils.bookMarkMsg4 = "<<uninitialized>>";
CoralUtils.bookMarkMsg5 = "<<uninitialized>>";

/**
 * Sets the href of the window
 * only used in simpleError.jsp
 *
 * @param location
 */
CoralUtils.setWindowLocation = function(location)
{
    window.location.href = location;
    return false;
};

/**
 * Displays the help in a separate window
 * Only used in searchButtons.jsp
 *
 * @param helpUrl
 */
CoralUtils.openHelp = function(helpUrl)
{
    var helpWindow = window.open(helpUrl, "RAHelpWindow", "width=850,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes", false);
    helpWindow.focus();
    return false;
};

/**
 * Adds a bookmark, or prompts the user to add the bookmark
 *
 * @param url the url to add the book mark for
 * @param title  the name to give the link
 */

CoralUtils.addBookMark = function(url, title)
{

    var ua  = navigator.userAgent.toLowerCase();
    var isMac = (ua.indexOf('mac')!=-1);
    var buttonStr = isMac?'Command/Cmd':'CTRL';

    if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4))
    {
        window.external.AddFavorite(url,title);
    }
    else if (navigator.appName == "Netscape")
    {
        if(isMac)
        {
            MessageBox.info(CoralUtils.addBookmark, "Press Command/Cmd-D to bookmark");
        }
        else
        {
            window.sidebar.addPanel(title,url,"");
        }
    }
    else
    {
        MessageBox.info(CoralUtils.addBookmark, "Press CTRL-D to bookmark");
    }
};

/**
 * Gets the previous non-text node for the give html dom node.
 *
 * @param node the node to get the sibling for
 * @return the previous sibling, or null, if the node is the first sibling.
 */
CoralUtils.getPreviousSibling = function(node)
{
    var ret = node.previousSibling;
    while(ret !== null && ret.nodeName == "#text")
    {
        ret = ret.previousSibling;
    }

    return ret;
};

/**
 * Gets the next non-text node for the give html dom node.
 *
 * @param node the node to get the sibling for
 * @return the next sibling, or null, if the node is the only sibling.
 */
CoralUtils.getNextSibling = function(node)
{
    var ret = node.nextSibling;
    while(ret !== null && ret.nodeName == "#text")
    {
        ret = ret.nextSibling;
    }

    return ret;
};

/**
* Formats the integer by using the NumberFormat object.
*
* @param intVal the integer value to format
* @return the formatted value of the integer of the form x,xxx,xxx....
*/
CoralUtils.formatInteger = function(intVal)
{
    var num = new NumberFormat();
    num.setInputDecimal('.');
    num.setNumber(intVal);
    num.setPlaces('0', false);
    num.setCurrencyValue('$');
    num.setCurrency(false);
    num.setCurrencyPosition(num.LEFT_OUTSIDE);
    num.setNegativeFormat(num.LEFT_DASH);
    num.setNegativeRed(false);
    num.setSeparators(true, ',', ',');

    return num.toFormatted();
};

/**
 * Initializes an event object by making sure that it exists and then setting
 * the target to the source element if the target does not exist (in Internet Explorer).
 * @param e the event object to initialize
 */
CoralUtils.initEvent = function(e)
{
    e = e ? e : window.event;
    if (YAHOO.lang.isUndefined(e.target))
    {
        e.target = e.srcElement;
    }

    return e;
};


