var oUtil = (function() {

    var _bBrowserCanSetCursor = null;
    var _aOrigCursor = null;
    var _sOrigBodyCursor = null;

        function logBtn(sBtnName) {
        (new Image()).src =
                        'logBtn.php?title=.' + sBtnName + '.&random=' + Math.random();
    }

        function canSetCursor() {
        if (_bBrowserCanSetCursor == null) {
                        _bBrowserCanSetCursor =
                                (                 document.body.currentStyle
                 ||
                                  window.getComputedStyle)
                &&
                                (document.body &&
                 document.body.style &&
                 (typeof(document.body.style.cursor) != 'undefined'));
        }
        return _bBrowserCanSetCursor;
    }

        function setDocWaitCursor() {
        _setPageCursor(true);
        if (typeof(oBox) != 'undefined') {
            oBox.showImgWait();
        }
    }

        function restoreCursor() {
        _setPageCursor(false);
        if (typeof(oBox) != 'undefined') {
            oBox.hideImgWait();
        }
    }

        function _setPageCursor(bSetWait) {
        var bCanSet = (canSetCursor()
                       ? ((bSetWait &&
                           (_aOrigCursor != null))
                          ?                             false
                          : true)
                       : false);
        if (bCanSet) {
            var sCursor = '';
            if (bSetWait) {
                                _aOrigCursor = [];
            }

                        for (var i=0; i<document.links.length; i++) {
                _setObjCursor(document.links[i], bSetWait);
            }

            for (var i=0; i<document.images.length; i++) {
                _setObjCursor(document.images[i], bSetWait);
            }

            var aInput = document.getElementsByTagName('input');
            for (var i=0; i<aInput.length; i++) {
                _setObjCursor(aInput[i], bSetWait);
            }

            _setObjCursor(document.body, bSetWait);

            if (!bSetWait) {
                                _aOrigCursor = null;
            }
        }
    }

        function _setObjCursor(oObj, bSetWait) {
        if (            (oObj.tagName == 'BODY')
            ||
                        /^\S+$/.test(oObj.id)) {
            var sCursor = '';
            if (bSetWait) {
                                var sOrigCursor = (oObj.currentStyle
                                   ?                                      oObj.currentStyle['cursor']
                                   :                                      document.defaultView
                                     .getComputedStyle(oObj, null)
                                     .getPropertyValue('cursor'));
                if (sOrigCursor == 'wait') {
                    sOrigCursor = 'pointer';
                }
                if (oObj.tagName == 'BODY') {
                    _sOrigBodyCursor = sOrigCursor;
                } else {
                    _aOrigCursor[oObj.id] = sOrigCursor;
                }
                sCursor = 'wait';
            } else {
                                sCursor = ((oObj.tagName == 'BODY')
                           ? _sOrigBodyCursor
                           : _aOrigCursor[oObj.id]);
            }
            oObj.style.cursor = sCursor;
        }
    }

        function setFormJsAct(sJsAct) {
        document.getElementById('queryForm').action = sJsAct;
    }

        function initLoadCalc() {
        if (document.getElementById('ltd')) {
                        _setRequestTime();
        }
    }

        function updStat() {
        var aStat = [];
                aStat[aStat.length] = 'u=' +
                              document
                              .getElementById('urlId')
                              .value;

                var aLoadTime = _calcLoad();
        if (aLoadTime != null) {
            aStat[aStat.length] = 'c=' + aLoadTime['fCompleteLoadSec'];
            aStat[aStat.length] = 'i=' + aLoadTime['fInitialLoadSec'];
        }

                if (aStat.length > 1) {
                        (new Image()).src =  'updStat.php?' + aStat.join('&');
        }
    }

        function _calcLoad() {
        var aLoadTime = null;
        var oLoadTimeDsp =
            document.getElementById('ltd');
        if (oLoadTimeDsp) {
                        var fCurTime = new Date().getTime();
            var sSubmitTime = _getRequestTime();

                        _delRequestTime();

            var fLastTime = ((sSubmitTime == null)
                             ?                                aGlobal['fPageStartTime']
                             :                                parseFloat(sSubmitTime));

                        var fCompleteLoadSec = (fCurTime - fLastTime)/1000;

                        var fInitialLoadSec = (fCurTime - aGlobal['fPageStartTime'])/1000;

                        oLoadTimeDsp.value = fCompleteLoadSec + '(' + fInitialLoadSec + ')';

            aLoadTime = {
                'fCompleteLoadSec': fCompleteLoadSec,
                'fInitialLoadSec': fInitialLoadSec
            };

        }
        return aLoadTime;
    }

        function _getQd() {
        return oCookie.getSubCookie('qd');
    }

        function _setRequestTime() {
        oCookie.setSubCookie('rt', (new Date()).getTime());
    }

        function _delRequestTime() {
        oCookie.delSubCookie('rt');
    }

        function _getRequestTime() {
        return oCookie.getSubCookie('rt');
    }

        function getLoadStat() {
        var oLoadTimeDsp =
            document.getElementById('ltd');
        return (oLoadTimeDsp
                ? oLoadTimeDsp.value
                : null);
    }

        function toggleNextRowDisplay(oCurTag, sShowLabel, sHideLabel) {
                var oTr = oCurTag;
        while (oTr.tagName != 'TR') {
            oTr = oTr.parentNode;
        }

                var oTbl = oTr;
        while (oTbl.tagName != 'TABLE') {
            oTbl = oTbl.parentNode;
        }

                var aNextRowCell = oTbl.rows[(oTr.rowIndex + 1)].cells;
        var bCurIsHide = true;
        for (var i=0; i<aNextRowCell.length; i++) {
            var oCell = aNextRowCell[i];
            bCurIsHide = !(oCell.style.display == 'none');
            oCell.style.display = ((oCell.style.display == 'none')
                                   ?                                      ((document.all && !window.opera)
                                      ?                                         'block'
                                      :                                         'table-cell')
                                   :                                      'none');
        }
        if (arguments.length == 3) {
                        oCurTag.childNodes[0].nodeValue = (bCurIsHide
                                               ? sShowLabel
                                               : sHideLabel);
        }

        return bCurIsHide;
    }
        function crSubmitForm(oForm, oEvt) {
        var bRet = true;
        var iKeyCode = (window.event
                        ? window.event.keyCode
                        : (oEvt
                           ? oEvt.which
                           : null));
        if (iKeyCode == 13) {
            oForm.submit();
            bRet = false;
        }
        return bRet;
    }
        
        return {
        'logBtn': logBtn,
        'canSetCursor': canSetCursor,
        'setDocWaitCursor': setDocWaitCursor,
        'restoreCursor': restoreCursor,
        'setFormJsAct': setFormJsAct,
        'initLoadCalc': initLoadCalc,
        'updStat': updStat,
        'getLoadStat': getLoadStat,
        'toggleNextRowDisplay': toggleNextRowDisplay,
        'crSubmitForm': crSubmitForm
    }
})();

