﻿function JsCommonFunc()
{
    //define reference to the body object in IE
    this.iebody = (document.compatMode && document.compatMode != 'BackCompat') ? document.documentElement : document.body;
    //
    
    this.getWidth = function(objectId)
    {
        if(document.getElementById && document.getElementById(objectId))
        {
            return document.getElementById(objectId).clientWidth;
        }
        else if (document.all && document.all(objectId))
        {
            return document.all(objectId).clientWidth;
        }
        else if (document.layers && document.layers[objectId])
        {
            return document.layers[objectId];
        }
        return false;
    }

    this.getHeight = function(objectId)
    {
        if(document.getElementById && document.getElementById(objectId))
        {
            return document.getElementById(objectId).clientHeight;
        }
        else if (document.all && document.all(objectId))
        {
            return document.all(objectId).clientHeight;
        }
        else if (document.layers && document.layers[objectId])
        {
            return document.layers[objectId];
        }
        return false;
    }
    
    this.getViewPortWidth = function()
    {
        if(window.innerWidth)
        {
            return window.innerWidth;
        }
        else if (document.all)
        {
            return this.iebody.clientWidth;
        }
        return false;
    }

    this.getViewPortHeight = function()
    {
        if(window.innerHeight)
        {
            return window.innerHeight;
        }
        else if (document.all)
        {
            return this.iebody.clientHeight;
        }
        return false;
    }
    
    this.getScrollCoordinateX = function()
    {
        if(document.all)
        {
            return this.iebody.scrollLeft;
        }
        else if (window.pageXOffset)
        {
            return window.pageXOffset;
        }
        return false;
    }

    this.getScrollCoordinateY = function()
    {
        if(document.all)
        {
            return this.iebody.scrollTop;
        }
        else if (window.pageYOffset)
        {
            return window.pageYOffset;
        }
        return false;
    }
    
    this.getObject = function(objectId)
    {
        if(document.getElementById && document.getElementById(objectId))
        {
            return document.getElementById(objectId);
        }
        else if (document.all && document.all(objectId))
        {
            return document.all(objectId);
        }
        return false;
    }
    
    this.getObjectStyle = function(objectId)
    {
        var obj = this.getObject(objectId);
        return (obj ? obj.style : false);
    }
    
    this.moveObjectToCoordinate = function(objectId, newXCoordinate, newYCoordinate)
    {
        // get a reference to the cross-browser style object and make sure the object exists
        var targetObject = this.getObject(objectId);
        if (targetObject.WCHLayer)
        {
            this.moveObjectToCoordinate(targetObject.WCHLayer.id, newXCoordinate, newYCoordinate);
        }
        //
        var styleObject = this.getObjectStyle(objectId);
        if(styleObject)
        {
            //
            styleObject.left = newXCoordinate+'px';
            styleObject.top = newYCoordinate+'px';
            //
            return true;
        }
        else
        {
            // we couldn't find the object, so we can't very well move it
            return false;
        }
    }
    
    this.moveObjectToViewportCenter = function(objectId)
    {
        //
        var newXCoordinate = this.getScrollCoordinateX() + ((this.getViewPortWidth() - this.getWidth(objectId))/2);
        var newYCoordinate = this.getScrollCoordinateY() + ((this.getViewPortHeight() - this.getHeight(objectId))/2);
        //
        return this.moveObjectToCoordinate(objectId, newXCoordinate, newYCoordinate);
        //
    }
    
    this.changeObjectVisibility = function(objectId, newVisibility)
    {
        // get a reference to the cross-browser style object and make sure the object exists
        var styleObject = this.getObjectStyle(objectId);
        var targetObject = this.getObject(objectId);
        //
        if(newVisibility == 'hidden')
        {
            if (targetObject.WCHLayer)
            {
                this.changeObjectVisibility(targetObject.WCHLayer.id, 'hidden');
            }
        }
        //
        if(styleObject)
        {
            styleObject.visibility = newVisibility;
            //
            if(newVisibility == 'hidden')
            {
                styleObject.zIndex = "-1000";
            }
            else
            {
                styleObject.zIndex = "1000";
            }
            return true;
        }
        else
        {
            // we couldn't find the object, so we can't change its visibility
            return false;
        }
    }
}

function JsMessageBox(divId)
{
    this.InitializeTimer = function()
    {
        // Set the length of the timer, in seconds
        this.secs = 1000;
        this.StopTheClock();
        this.StartTheTimer();
    }
    
    this.StopTheClock = function()
    {
        if(this.timerRunning)
	    {
            clearTimeout();
	    }
        this.timerRunning = false;
    }
    
    this.StopTheTimer = function()
    {
        this.secs = 0;
        this.StopTheClock();
        this.objJsCommonFunc.changeObjectVisibility(this.divId, 'hidden');
        WCH.Discard(this.divId);
    }
    
    this.StartTheTimer = function()
    {
        if (this.secs == 0)
        {
            this.StopTheTimer();
        }
        else
        {
            WCH.Apply(this.divId);
            this.timerRunning = true;
        }
    }

    this.showPopup = function()
    {
        this.objJsCommonFunc.moveObjectToViewportCenter(this.divId);
        // and make it visible
        if(this.objJsCommonFunc.changeObjectVisibility(this.divId, 'visible'))
        {
            // if we successfully showed the popup
            // store its Id on a globally-accessible object
            window.currentlyVisiblePopup = this.divId;
            this.InitializeTimer();
            return true;
        }
        else
        {
            // we couldn't show the popup, boo hoo!
            return false;
        }
    } // this.showPopup
    
    this.secs = 0;
    this.timerID = null;
    this.timerRunning = false;
    this.delay = 10;
    this.divId = divId;
    this.objJsCommonFunc = new JsCommonFunc();
    // initialize
    this.StopTheTimer();
}

