// ****************************************************************************//
// COPYRIGHT © 2002 Foxfire Printing & Packaging, Inc. All Rights Reserved.
//
// NAME:        f_Common.js
//
// AUTHOR:      Zuber Jafri
//
// ****************************************************************************//

var g_fltBrowserVersion = 0.0;

/* ---
PURPOSE : Function detects and returns browser's major version.
--- */
function getBrowserMajorVersion()
{
	var l_intBrowserMajorVersion ;
	var l_strBrowserUserAgent = window.navigator.userAgent ;
	var msie = l_strBrowserUserAgent.indexOf("MSIE ") ;
	if (msie > 0)
		l_intBrowserMajorVersion = parseInt(l_strBrowserUserAgent.substring(msie+5,
		        l_strBrowserUserAgent.indexOf(".",msie))) ;
	else
		l_intBrowserMajorVersion = 0 ;

	return l_intBrowserMajorVersion ;
}

/* ---
PURPOSE : To stop processing in development systems. Meant to alert user to
            exceptional conditions that should never occur.
REMARKS : Use assert during development to catch programming errors. This is not
            meant as a substitute for error handling. If you break into the
            debugger at this line, check the call stack to find the source of
            the assert. This will be a no-op in production code.
--- */
function assert()
{
	debugger;
}

/* ---
PURPOSE : Function changes top css5 image and mouse cursor to show busy state.
INPUTS  : pi_strMessage: String.
          pi_blnMouseStatus: Boolean.
--- */
function setWindowStatus(pi_strMessage, pi_blnMouseStatus)
{
	// Note, discontinued using animated .gif. Now top doc shows a wait message.
	top.frames("sei_top_main").showWaitMessage(pi_blnMouseStatus);

    // Do content frame.
    if (elementExists(top.m_frmMiddle)
     && typeof top.m_frmMiddle.showWaitCursor == "function")
    {
        top.m_frmMiddle.showWaitCursor(pi_blnMouseStatus);

	    // Prod the system to paint the cursor. Otherwise it dawdles.
	    top.m_frmMiddle.document.body.setCapture(true);
	    top.m_frmMiddle.document.releaseCapture();
	}

    // Do left nav bar.
    if (elementExists(top.m_frmLeft))
        top.m_frmLeft.showWaitCursor(pi_blnMouseStatus);

    // Do mini search frame.
    if (elementExists(top.m_frmMiniSearch))
        top.m_frmMiniSearch.showWaitCursor(pi_blnMouseStatus);

    // Do top nav bar.
    if (elementExists(top.m_frmTop))
        top.m_frmTop.showWaitCursor(pi_blnMouseStatus);

    top.setWaitMode(pi_blnMouseStatus);

    // Set status bar.
	window.status = pi_strMessage ;
}

/* ---
PURPOSE : To trim leading and trailing spaces from a string.
INPUTS  : str: The string to trim.
OUTPUTS : string: The trimmed string.
REMARKS : Equivalent to the VB Trim function.
--- */
function Trim(str)
{
    // Using regular expressions for more efficient trim.
	var re = /(^\s+|\s+$)/ig;
	return (str.toString()).replace(re, "");
}

/* ---
PURPOSE : To trim leading zeroes from a string.
INPUTS  : str: The string to trim.
OUTPUTS : string: The trimmed string.
REMARKS : If a plus sign precedes the number, it is removed. If a minus sign
            precedes the number, it is preserved unless the value is 0.
--- */
function trimLeadingZeroes(str)
{
    // Make sure it's a string object.
    var l_str = str.toString();

    // Get rid of leading sign, but remember if neg.
    var l_blnNeg = (l_str.charAt(0) == "-");
    var re = /(^[+-])/;
	l_str = l_str.replace(re, "");

	// Remove leading zeroes
	re = /(^0+)/;
	l_str = l_str.replace(re, "");

	// Must at least have leading zero if decimal pt.
	re = /(^\.)/;
	l_str = l_str.replace(re,"0.");

	// Return at least 0; put back minus
	if (l_str == "")
	    l_str = "0";
	else if (l_blnNeg && l_str.valueOf() != 0)
	    l_str = "-" + l_str;

	return l_str;
}

/* ---
PURPOSE:    To insert a string into another string.
INPUTS:     pi_strInput: The string to insert into.
            pi_intPos: The zero-based position at which to insert.
            pi_str: The character or string to insert.
OUTPUT:     The new string.
--- */
function insertAt(pi_strInput, pi_intPos, pi_str)
{
	return pi_strInput.substr(0, pi_intPos) + pi_str
	     + pi_strInput.substr(pi_intPos);
}

/* ---
PURPOSE:    To display the error page.
INPUTS:     pi_strMessage: The error text to display.
            pi_strEIID: The ID of the marker in the server's event log matching
                this error.
--- */
function showErrorPage(pi_strMessage, pi_strEIID)
{
    var l_strEIID = (pi_strEIID == null) ? "" : pi_strEIID;

	if (isDialogWindow())
	    setDialogWaitIndicator(false);
	else
		setWindowStatus(CONST_STATUS_DONE, false) ;

	showModalDialog("mdErrorPage.html", new Array(pi_strMessage, l_strEIID),
	    "resizable:yes; dialogWidth:400px; dialogHeight:235px; status:no; scroll:yes; help:no");
}

/* ---
PURPOSE:    To display the wait indicator in a modal dialog.
INPUTS:     pi_blnShow: true to show the indicator; false to hide it.
--- */
function setDialogWaitIndicator(pi_blnShow)
{
    var l_divWaitMsg = document.all("WaitLayer");
    if (elementExists(l_divWaitMsg))
        showElement(l_divWaitMsg, pi_blnShow);

    var l_divWaitCursor = document.all("WaitCursor");
    if (elementExists(l_divWaitCursor))
    {
        // Show hourglass by enabling layer with wait cursor. Note, set- and
        // releaseCapture force cursor to change if necessary.
        if (pi_blnShow)
        {
            l_divWaitCursor.style.top = document.body.scrollTop + "px";
            l_divWaitCursor.style.left = document.body.scrollLeft + "px";
            l_divWaitCursor.style.width = document.body.clientWidth + "px";
            l_divWaitCursor.style.height = document.body.clientHeight + "px";
        }
        else
        {
            l_divWaitCursor.style.width = "0px";
            l_divWaitCursor.style.height = "0px";
        }
        document.body.setCapture(true);
        document.releaseCapture();
    }
}

/* ---
PURPOSE:	To determine if an element exists.
INPUTS:     pi_element: The element to test.
OUTPUTS:	Boolean: true if the element exists; false if not.
--- */
function elementExists(pi_element)
{
    return (typeof pi_element == "object" && pi_element != null);
}

/* ---
PURPOSE:    To set the global browser version variable to the current browser
            version.
REMARKS:    The version is a float value. Examples are 5.0, 5.01, 5.5, etc. If
            the browser is not supported, the browser version will be 0.0.

            This function should not be called from the page. Call
            getBrowserVersion() or isBrowserSupported() instead.
--- */
function setBrowserVersion()
{
    g_fltBrowserVersion = 0.0;  // browser not supported

    if (navigator.appName != "Microsoft Internet Explorer")
        return;                 // must be IE
    else
    {
        var l_strVersion = navigator.appVersion;

        // Find start of IE version number.
        var l_intVerLocStart = l_strVersion.indexOf("MSIE");
        if (l_intVerLocStart == -1)
            return;
        l_intVerLocStart += 5;

        // Find length of IE version number.
        var l_intVerLocEnd = l_strVersion.indexOf(";", l_intVerLocStart);
        if (l_intVerLocEnd == -1) assert();
        var l_intVerLen = l_intVerLocEnd - l_intVerLocStart;

        // Convert the IE version number to float.
        var l_strIEVersion = l_strVersion.substr(l_intVerLocStart, l_intVerLen);
        var l_fltBrowserVersion = parseFloat(l_strIEVersion);
        if (l_fltBrowserVersion >= 5.0)
            g_fltBrowserVersion = l_fltBrowserVersion;
    }
}

/* ---
PURPOSE:    To get the current browser version.
OUTPUTS:    float. The browser version or 0.0 if the browser is not supported.
--- */
function getBrowserVersion()
{
    if (g_fltBrowserVersion == 0.0)     // not set yet?
        setBrowserVersion();
    return g_fltBrowserVersion;
}

/* ---
PURPOSE:    To determine if the current browser is supported for Customer
            StrataStation 5.0.
OUTPUTS:    boolean. true, if the current browser is supported, false, if not.
--- */
function isBrowserSupported()
{
    return (getBrowserVersion() != 0.0);
}
