﻿/*
Javascript message control
*/

var WebMessage = Class.create();

WebMessage.prototype =
{
    // Constructor
    initialize : function(piTitle, piText)
    {
        try
        {
            this.EnableAlertImage = false;
            this.EnableErrorImage = false;
            this.EnableNotification = false;
            
            // Declare variable holding the message box title
            if(!piTitle.blank())
            {
                this._title = piTitle;
            }
            else
            {
                this._title = "Information";
            }
            // Declare variable holding the button text
            if(!piText.blank())
            {
                this._text = piText;
            }
            else
            {
                throw 'A message text must be provided!';
            }
            // Declare variable holding the message box
            this._msgBox;
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'initialize', arguments, 'WEBMESSAGE', null, e, false)
        }
    },
    
    Build : function()
    {
        try
        {
            // Build div layer to hold the message box elements
            this._msgBox = new WebWindow(this._title);
            this._msgBox.Width = 300;
            this._msgBox.zIndex = 2000;
            this._msgBox.CenterScreen = true;
            this._msgBox.Class = 'WebMessage';
            
            // Build paragraph node for the message text
            var msgText = Builder.node('div', { style: 'padding:5px;' });
                        
            if(this.EnableAlertImage == true)
            {
                var alertImg = Builder.node('img', { src: 'Template/SYSTEM/Image/WebMessage/invenit_image_alert.jpg', style: 'float:left' });
                msgText.appendChild(alertImg);
            }
            
            if(this.EnableErrorImage == true)
            {
                var errorImg = Builder.node('img', { src: 'Template/SYSTEM/Image/WebMessage/invenit_image_error.jpg', style: 'float:left' });
                msgText.appendChild(errorImg);
            }
            
            if(this.EnableNotificationImage == true)
            {
                var notificationImg = Builder.node('img', { src: 'Template/SYSTEM/Image/WebMessage/invenit_image_notification.jpg', style: 'float:left' });
                msgText.appendChild(notificationImg);
            }
            
            // Append message text value to the new paragraph node
            msgText.innerHTML += this._text;
            // Append paragraph node to the message holder
            this._msgBox.appendChild(msgText);
            
            // Build content holder for the close button
            var msgBoxButtonArea = Builder.node('div', { style : 'width:290px;padding:5px 5px 2px 5px' });
            // Create a new button object
            var button = new WebButton(Lingo.GetLingoItem('CLOSE'));
            // Declare click event handler for the button
            var onClickFunc = this.Close.bindAsEventListener(this);
            button.OnClick = onClickFunc;
            // Append the button html markup to the button holder
            var buttonNode = button.GetHtml();
            buttonNode.style.marginLeft = '110px';
            msgBoxButtonArea.appendChild(buttonNode);
            this._msgBox.appendChild(msgBoxButtonArea);
            
            // Create clear elem
            var clearElem = Builder.node('div', { style: 'clear:both;height:1px' });
            this._msgBox.appendChild(clearElem);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Build', arguments, 'WEBMESSAGE', null, e, false)
        }
    },
    
    Show : function()
    {
        try
        {
            // Call Build function to build the message box html
            this.Build();
            // Append message box to the document body
            document.body.appendChild(this._msgBox.GetHtml());
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Show', arguments, 'WEBMESSAGE', null, e, false)
        }
    },
    
    Close : function()
    {
        try
        {
            this._msgBox.Close();
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Close', arguments, 'WEBMESSAGE', null, e, false)
        }
    }
}