/**
 *
 * DESCRIPTION
 *
 * This file is expected to be included in almost every page as it contains
 * global variables and javascript expected to be used by lots of different
 * pages
 *
 */
    //  ----------------
    //  Global Variables
    //  ----------------
        var sPrefix             = '';
        var iModalOffsetX       = 2;
        var iModalOffsetY       = 2;
        var iSP2HeightModifier  = 25;
    //  --------------------
    //  Javascript Utilities
    //  --------------------
        function doNothing()
        {
        }
        function foo()
        {
            alert( 'bar' );
        }
        function returnFalse()
        {
            return false;
        }
        function setCookie( sName, sValue )
        {
            document.cookie = sName + '=' + escape(sValue) + '; path=/';
        }
        function getCookie( sName )
        {
            var cookieVar       = document.cookie;
            var cookieName      = sName + '=';
            var cookieLength    = cookieVar.length;
            var cookieBegin     = 0;
            while ( cookieBegin < cookieLength )
            {
                var valueLength = cookieBegin + cookieName.length;
                if ( cookieVar.substring(cookieBegin, valueLength) == cookieName )
                {
                    var valueEnd    = cookieVar.indexOf( ';', valueLength );
                    if ( valueEnd == -1 )
                        valueEnd = cookieLength;
                    return unescape( cookieVar.substring(valueLength, valueEnd) );
                }
                cookieBegin = cookieVar.indexOf( ' ', cookieBegin ) + 1;
                if (cookieBegin == 0)
                    break;
            }
            return null;
        }
        function deleteCookie( sName )
        {
            document.cookie = sName + '=; path=/; expires=Fri, 31 Dec 1999 23:59:59 GMT;';
        }
        function trim( sData )
        {
            while ( sData.substr(0,1) == ' ' )
            {
                sData   = sData.substr( 1 );
            }
            while ( sData.substr(sData.length-1) == ' ' )
            {
                sData   = sData.substring( 0, sData.length-1 );
            }
            return sData;
        }
        function getValueFromParamString( sParamString, sKey )
        {
            // Setup Vars
            var aArguments    = new Array();
            var aValuePairs   = sParamString.split( ',' );
            var sReturnValue  = '';
            var bValueFound   = false;
            // Fill Arguments Array
            for ( var iPair = 0; iPair < aValuePairs.length; iPair++ )
            {
                var sPair           = aValuePairs[iPair]
                var iFirstEquals    = sPair.indexOf( '=' );
                aArguments[aArguments.length]   = sPair.substring( 0, iFirstEquals );
                aArguments[aArguments.length]   = sPair.substr( iFirstEquals+1 );
            }
            // Find Value (if it exists)
            for (var iArg = 0; iArg < aArguments.length; iArg++)
            {
                if ( aArguments[iArg++] == sKey && !bValueFound ) // Only get first instance
                {
                    sReturnValue  = aArguments[iArg];
                    bValueFound   = true;
                }
            }
            return sReturnValue; // Returns '' if not found
        }
        function arrayContains( oArray, oElement )
        {
            var iReturnIndex    = -1;
            for ( iItem = 0; iItem < oArray.length; iItem++ )
            {
                if ( oArray[iItem] == oElement )
                {
                    iReturnIndex    = iItem;
                    break;
                }
            }
            return iReturnIndex;
        }
        function copyObjectInfoToClipboard( oSource, sLabel )
        {
            var sProperties = '';
            for ( sPropertyName in oSource )
            {
                var sPropertyValue  = '' + eval( 'oSource.' + sPropertyName );
                if ( sPropertyValue.indexOf( 'function ' ) == -1 )
                {
                    sProperties += sPropertyName + ' = ' + sPropertyValue + '\n';
                }
            }
            copyToClipboard( sProperties + '\n\n' );
            if ( arguments.length > 1 )
            {
                alert( 'Copied "' + sLabel + '" properties to clipboard' );
            }
        }
        function copyToClipboard( sText )
        {
            window.clipboardData.setData( 'Text', sText );
        }
    //  ---------------
    //  DHTML Utilities
    //  ---------------
        function DoModelessWindow( sUrl, aParams )
        {
            // Set up vars
            var oWindow     = aParams[0];
            var sParams     = aParams[1];
            var sParamString  = 'frameurl=' +sUrl;
            var sArgs     = 'help: no;';
            var sStatusBar    = getValueFromParamString( sParams, 'statusbar' );
            var sNoResize   = getValueFromParamString( sParams, 'noresize' );
            var sBackground   = getValueFromParamString( sParams, 'background' );
            var iHeight     = getValueFromParamString( sParams, 'height' );
            var iWidth      = getValueFromParamString( sParams, 'width' );
            var sScroll     = getValueFromParamString( sParams, 'scroll' );
            //  Determine modeless position (top left of main content cell)
            var oMainContentCell    = document.getElementById( 'maincontentcell' );
            var iDialogTop          = window.screenTop + getTop( oMainContentCell );
            var iDialogLeft         = window.screenLeft + getLeft( oMainContentCell );
            sArgs   += 'dialogTop: ' + ( parseInt(iDialogTop)+parseInt(iModalOffsetY) ) + 'px;';
            sArgs   += 'dialogLeft: ' + ( parseInt(iDialogLeft)+parseInt(iModalOffsetX) ) + 'px;';
            // Set up arguments
            sArgs += (sScroll == 'true') ? 'scroll: yes;' : 'scroll: no;';                                              // scroll=true      [default yes]
            sArgs += (sNoResize == 'true') ? 'resizable: no;' : 'resizable: yes;';                                      // resizable=true   [default yes]
            sArgs += (sStatusBar == 'true') ? 'status: yes;' : 'status: no;';                                           // statusbar=true   [default yes (untrusted) or no (trusted)]
            sArgs += (iHeight > 100) ? 'dialogHeight: ' +(parseInt(iHeight)+parseInt(iSP2HeightModifier))+ 'px;' : '';  // height=x         [no default]
            sArgs += (iWidth > 100) ? 'dialogWidth: ' +iWidth+ 'px;' : '';                                              // width=x          [no default]
            // Set up params
            sParamString  += (sBackground != '') ? ',bgcolor=' +sBackground : sBackground;  // background = (#rrggbb || colorName)
            window.showModelessDialog( 'modelessdialog.htm', [oWindow,sParamString], sArgs );
        }
        function DoModalWindow( sUrl, sParams, sOtherParams )
        {
            //  Set up vars
            var sParamString    = 'frameurl=' +sUrl;
            var sArgs           = 'help: no;';
            var sStatusBar      = getValueFromParamString( sParams, 'statusbar' );
            var sNoResize       = getValueFromParamString( sParams, 'noresize' );
            var sBackground     = getValueFromParamString( sParams, 'background' );
            var iHeight         = getValueFromParamString( sParams, 'height' );
            var iWidth          = getValueFromParamString( sParams, 'width' );
            var sScroll         = getValueFromParamString( sParams, 'scroll' );
            var iDefinedTop     = getValueFromParamString( sParams, 'top' );
            var iDefinedLeft    = getValueFromParamString( sParams, 'left' );
            //  Determine modal position (top left of main content cell)
            if ( iDefinedTop == '' || iDefinedLeft == '' )
            {
                var oMainContentCell    = document.getElementById( 'maincontentcell' );
                var iDialogTop          = window.screenTop + getTop( oMainContentCell );
                var iDialogLeft         = window.screenLeft + getLeft( oMainContentCell );
                sArgs   += 'dialogTop: ' + ( parseInt(iDialogTop)+parseInt(iModalOffsetY) ) + 'px;';
                sArgs   += 'dialogLeft: ' + ( parseInt(iDialogLeft)+parseInt(iModalOffsetX) ) + 'px;';
            }
            else
            {
                sArgs   += 'dialogTop: ' + iDefinedTop + 'px;';
                sArgs   += 'dialogLeft: ' + iDefinedLeft + 'px;';
            }
            // Set up arguments
            sArgs += ( sScroll == 'false' ) ? 'scroll: yes;' : 'scroll: no;';                                               // scroll=false     [defaults to no if not specified]
            sArgs += ( sNoResize == 'false' ) ? 'resizable: yes;' : 'resizable: no;';                                       // noresize=false   [defaults to no if not specified]
            sArgs += ( sStatusBar == 'true' ) ? 'status: yes;' : 'status: no;';                                             // statusbar=true   [defaults to no if not specified]
            sArgs += ( iHeight > 100 ) ? 'dialogHeight: ' +(parseInt(iHeight)+parseInt(iSP2HeightModifier))+ 'px;' : '';    // height=x         [is no default]
            sArgs += ( iWidth > 100 ) ? 'dialogWidth: ' +iWidth+ 'px;' : '';                                                // width=x          [is no default]
            // Set up params
            sParamString  += ( sBackground != '' ) ? ',bgcolor=' +sBackground : sBackground;                                // background=#nnnnnn
            sParamString  += ( sOtherParams != null ) ? ',' +sOtherParams : '';
            // Call modal and return value
            var modalReturnValue  = window.showModalDialog( 'dialog.htm', sParamString, sArgs );
            return modalReturnValue;
        }
        function showHide( sElem, bInline )
        {
            var oElem       = document.getElementById( sElem );
            var bHide       = oElem.style.display != 'none';
            var sVisible    = ( bInline ) ? 'inline' : 'block';
            oElem.style.display = ( bHide ) ? 'none' : sVisible;
        }
        function getLeft( objSourceElement )
        {
            var intReturnCoordinate = 0;
            while ( objSourceElement != null )
            {
                intReturnCoordinate += objSourceElement.offsetLeft;
                objSourceElement    = objSourceElement.offsetParent;
            }
            return intReturnCoordinate;
        }
        function getTop( objSourceElement )
        {
            var intReturnCoordinate = 0;
            while ( objSourceElement != null )
            {
                intReturnCoordinate += objSourceElement.offsetTop;
                objSourceElement    = objSourceElement.offsetParent;
            }
            return intReturnCoordinate;
        }
        function createHelpDiv()
        {
            //  Create Object
            var oNewDiv = document.createElement( 'DIV' );
            //  Set up div
            oNewDiv.style.display       = 'none';
            oNewDiv.style.position      = 'absolute';
            oNewDiv.style.left          = '0px';
            oNewDiv.style.top           = '0px';
            oNewDiv.style.padding       = '4px';
            oNewDiv.style.background    = '#EDF4FC';
            oNewDiv.style.fontSize      = '11px';
            oNewDiv.style.border        = '1px solid #0C5DD2';
            oNewDiv.id                  = 'helpdiv';
            //  Attach title to div
            var oTitle  = document.createElement( 'B' );
            oTitle.appendChild( document.createTextNode("") );
            oNewDiv.appendChild( oTitle );
            oNewDiv.appendChild( document.createElement('BR') );
            //  Attach message to div
            oNewDiv.appendChild( document.createTextNode("") );
            //  Attach div to body
            document.body.appendChild( oNewDiv );
        }
        function showHelp( sTitle, sMessage, iWidth, iOffsetX, iOffsetY, bAlign )
        {
            var oHelpDiv                                    = document.getElementById( 'helpdiv' );
            if ( oHelpDiv != null )
            {
                oHelpDiv.childNodes[0].childNodes[0].nodeValue  = sTitle;
                oHelpDiv.childNodes[2].nodeValue                = sMessage;
                oHelpDiv.style.width                            = iWidth + 'px';
                oHelpDiv.style.display                          = 'block';
                oHelpDiv.style.left                             = ( window.event.clientX + iOffsetX ) + 'px';
                oHelpDiv.style.top                              = ( window.event.clientY + iOffsetY ) + 'px';
                // 29/09/05 MJD
                // New code put in to align the mouseover to the right instead of the left.
                // This will make the right-hand side of the help dialog be below the cursor insted of the left.
                // Do an arguments check first to make sure we don't break any existing calls (e.g. Enviroscreen)
                if ( arguments.length == 6 && bAlign == 'right' )
                {
                    oHelpDiv.style.left = ( window.event.clientX + iOffsetX - iWidth ) + 'px';
                }
            }
        }
        function hideHelp()
        {
            var oHelpDiv            = document.getElementById( 'helpdiv' );
            if ( oHelpDiv != null )
            {
                oHelpDiv.style.display  = 'none';
            }
        }