/*
 * constant values
 */
var CMD_ERROR              = "Error";
var CMD_ERROR_PARAM        = "ErrorWithInfo";
var CSS_CLOSE              = "close";
var CSS_CLOSE_SIMPLE       = "closeSimple";
var CSS_CLR_ERR            = " errBack";
var CSS_CLR_INFO           = " infoBack";
var CSS_ERR_BOX            = "errBox";
var CSS_ERR_MSG            = "errMsg";
var CSS_ERR_TITLE          = "errTitle";
var CSS_HIDDEN             = "hidden";
var CSS_INFO               = "infoIcon";
var CSS_INPUT_BLOCK        = "userInputBlocker";
var CSS_OK_BTN             = "okBtn";
var CSS_RBLOCK_BOTTOM      = "rblockbottom";
var CSS_RBLOCK_TOP         = "rblocktop";
var CSS_RND1               = "rnd1";
var CSS_RND2               = "rnd2";
var CSS_RND3               = "rnd3";
var CSS_RND4               = "rnd4";
var CSS_SERVER_MSG         = "srvMessage";
var CSS_SERVER_MSG_TITLE   = "srvMessageTitle";
var CSS_SERVER_MSG_CONTENT = "srvMessageContent";
var CSS_SRV_MSG_BTN        = "srvMsgTitleBtn";
var CSS_SRV_MSG_TEXT       = "srvMsgTitleText";
var CURSOR_DEFAULT         = "default";
var DIV_CONTENT            = "pageContent";
var DIV_DETAIL_STRIP       = "detailStrip";
var ELM_B                  = "b";
var ELM_DIV                = "div";
var ELM_IMG                = "img";
var ELM_SPAN               = "span";
var ERR_PAGE_TIMEOUT       = 5000;
var IMG_CLOSE              = "/images/CloseButton.png";
var IMG_CLOSE_DOWN         = "/images/closeOut.gif";
var IMG_CLOSE_NORM         = "/images/close.gif";
var IMG_INFO               = "/images/InfoIcon.png";
var IMG_OK                 = "/images/Ok Button.png";
var TXT_ERR                = "Error on Page";
var TXT_INFO               = "Important Message";
var TXT_PX                 = "px";

/*
 * script globals
 */
 
var ErrorMessage = null;
var EnableAjaxValidation = true;

/*
 * ErrBox (class):
 *      displays a floating div next to a control that contains a validation error
 *      The div can be closed by the user and contains an error message that is
 *      provided to the class via a function call
 */
function ErrBox()
{
    var Owner    = this;
    this.Control = null;
    this.ErrDiv  = document.createElement(ELM_DIV);
    this.BtnDiv  = document.createElement(ELM_DIV);
    this.MsgDiv  = document.createElement(ELM_DIV);

    this.ErrDiv.appendChild(this.BtnDiv);
    this.ErrDiv.appendChild(this.MsgDiv);

    this.ErrDiv.className = CSS_ERR_BOX;
    this.BtnDiv.className = CSS_ERR_TITLE;
    this.MsgDiv.className = CSS_ERR_MSG;

    var CloseImg = document.createElement(ELM_IMG);
    CloseImg.src       = IMG_CLOSE_NORM;
    CloseImg.className = CSS_CLOSE_SIMPLE;

    CloseImg.onmouseup = function(event) {
        this.src = IMG_CLOSE_NORM;
        Owner.ErrDiv.className = CSS_HIDDEN;
        
        if ( (Owner.Control) && (null != Owner.Control) )
        {
            Owner.Control.focus();
        }
        
        return true;
    }

    CloseImg.onmousedown = function(event) {
        this.src = IMG_CLOSE_DOWN;
        return true;
    }

    CloseImg.onmouseout = function(event) {
        this.src = IMG_CLOSE_NORM;
        return true;
    }

    this.BtnDiv.appendChild(CloseImg);
    
    /*
     * close:
     *      hides/closes the error message
     * Params:
     *      none
     * Returns:
     *      true
     */
    this.close = function()
    {
        this.ErrDiv.className = CSS_HIDDEN;
        return true;
    }
    
    /*
     * getCtlPos:
     *      gets the position of the control on the screen
     * Params:
     *      control - the control to obtain the position for
     * Returns:
     *      the current x and y coordinates of the mouse
     */
    this.getCtlPos = function(control)
    {
        var left = 0;
        var top  = 0;
        
        if (control)
        {
            while (control.offsetParent)
            {
                left += control.offsetLeft;
                top  += control.offsetTop;
                control = control.offsetParent;
            }
            
            left += control.offsetLeft;
            top  += control.offsetTop;
        }

        return {x:left, y:top};
    }
    
    this.showControlError = function(control, message) {
        if ( (control) && (message) ) {
            window.alert(message);
            control.focus();
        }
        
        return true;
    }
    
    /*
     * showControlError:
     *      called to show the error control
     * Params:
     *      control - the control containing the error condition
     *      message - the message to display
     * Returns:
     *      true
     */
    this.showControlError1 = function(control, message)
    {
        if ( (control) && (message) )
        {
            this.Control = control;
            var message = trim(message);
            
            if ( 0 < message.length )
            {
                this.MsgDiv.innerText = message;
                this.MsgDiv.innerHTML = message;
                
                var pos = this.getCtlPos(control);
                
                this.ErrDiv.style.top = pos.y + TXT_PX;
                this.ErrDiv.style.left = (pos.x + control.clientWidth + 5) + TXT_PX;
                
                this.ErrDiv.className = CSS_ERR_BOX;
                
                if ( this.ErrDiv.parentNode )
                {
                    this.ErrDiv.parentNode.removeChild(this.ErrDiv);
                }
                
                if ( control.parentNode )
                {
                    control.parentNode.appendChild(this.ErrDiv);
                }
            }
        }
        
        return true;
    }
    
    return true;
}

/*
 * ServerMessageDlg:
 *      implemented as a Javascript class, this function shows an
 *      error or information message to the user in the event some
 *      server-side processing code needs to notify the user.  An
 *      instance of the object is created right below this function
 *      declaration which ensures the error code check is run so
 *      long as this source file is included on a page.
 * Params:
 *      none
 * Returns:
 *      true
 */
function ServerMessageDlg()
{
    var Owner = this;
    this.msgDlg = document.createElement(TYPE_DIV);
    
    this.msgDlg.className = CSS_HIDDEN;
    this.timeoutId = 0;
    
    //var content_div = document.getElementById(DIV_CONTENT);
    //var content_div = document.getElementById(DIV_DETAIL_STRIP);
    var srv_message = document.getElementById(CTL_SRV_MESSAGE);
    
    this.onHideMsgDlg = function() {
        if ( 0 != Owner.timeoutId )
        {
            clearTimeout(Owner.timeoutId);
            Owner.timeoutId = 0;
        }
        
        Owner.msgDlg.className = CSS_HIDDEN;
        EnableAjaxValidation = true;
        return true;
    }
    
    this.onShowMsgDlg = function(contentDiv, dlgMessage, isError, dlgTitle) {
        if ( (contentDiv) && (dlgMessage) && (0 < trim(dlgMessage).length) ) {
            var child_div;
            var ctl_img;
            var title;
            var title_div;
            
            // we set this flag here as an indicator to other javascript files to
            // not perform any AJAX based validation.  Note that it is the reponsiblity
            // of the other scripts to actually use this flag - by itself, it means
            // nothing
            EnableAjaxValidation = false;
            
            var color = (true == isError) ? CSS_CLR_ERR : CSS_CLR_INFO;
            
            if ( (dlgTitle) && (0 < dlgTitle.length) )
            {
                title = dlgTitle;
            }
            else if ( true == isError )
            {
                title = TXT_ERR;
            }
            else
            {
                title = TXT_INFO;
            }
    
            contentDiv.appendChild(Owner.msgDlg);
            Owner.msgDlg.className = CSS_SERVER_MSG + color;
            
            var block = document.createElement(TYPE_DIV);
            Owner.msgDlg.appendChild(block);
            block.className = CSS_RBLOCK_TOP;
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className =  CSS_RND4 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className =  CSS_RND3 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className =  CSS_RND2 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className =  CSS_RND1 + color;
            
            child_div = document.createElement(TYPE_DIV);
            Owner.msgDlg.appendChild(child_div);
            child_div.className = CSS_SERVER_MSG_TITLE;
            
            ctl_img = document.createElement(ELM_IMG);
            child_div.appendChild(ctl_img);
            ctl_img.src = IMG_INFO;
            ctl_img.className = CSS_INFO;
            
            title_div = document.createElement(TYPE_DIV);
            child_div.appendChild(title_div);
            title_div.innerText = title;
            title_div.innerHTML = title;
            title_div.className = CSS_SRV_MSG_TEXT;
            
            ctl_img = document.createElement(ELM_IMG);
            child_div.appendChild(ctl_img);
            ctl_img.src = IMG_CLOSE;
            ctl_img.className = CSS_CLOSE;
            
            ctl_img.onmouseup = Owner.onHideMsgDlg;
            
            child_div = document.createElement(TYPE_DIV);
            Owner.msgDlg.appendChild(child_div);
            child_div.className = CSS_SERVER_MSG_CONTENT;
            child_div.innerText = dlgMessage;
            child_div.innerHTML = dlgMessage;
            
            block = document.createElement(TYPE_DIV);
            Owner.msgDlg.appendChild(block);
            block.className = CSS_RBLOCK_BOTTOM;
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className = CSS_RND1 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className = CSS_RND2 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className = CSS_RND3 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className = CSS_RND4 + color;
            
            var div_pos = getControlPosition(contentDiv);
            
            Owner.msgDlg.style.width = (contentDiv.clientWidth - 20) + TXT_PX;
            Owner.msgDlg.style.left  = (div_pos.x + 10) + TXT_PX;
            Owner.msgDlg.style.top   = (div_pos.y + 5) + TXT_PX;
            
            // if err_param is true, then the server has determined the user input
            // is invalid (or some other condition has been presented by the user
            // that is otherwise invalid), and there are additional field definitions
            // that accompany the error condition
            if ( true == err_param )
            {
                var err_param;
                var ctl;
                var css_style = null;
                var first_param = true;
                
                // everything past parameter 3 is a field name with an optional CSS
                // class definition
                for (var prm_cnt=3; prm_cnt<parts.length; prm_cnt++)
                {
                    var err_param = parts[prm_cnt];
                    parts = err_param.split(DEL_COLON);
                    
                    // the first parameter is always the name of the field to highlight
                    // or set the form focus to
                    ctl = document.getElementById(parts[0]);
                    
                    // if there is a second parameter, it's the CSS class name to assign
                    // to the field.  Note that if the first field name is assigned a
                    // class name, all subsequent fields will use that class name until
                    // a new class name is encountered
                    if ( 1 < parts.length )
                    {
                        css_style = parts[2];
                    }
                    
                    // make sure we have a reference to the control specified
                    if ( ctl )
                    {
                        // we'll always set focus to the first control only
                        if ( true == first_param )
                        {
                            ctl.focus();
                            first_param = false;
                        }
                        
                        // if a style was specified (regardless of what control it was
                        // specified with - this is an optimization and is by design),
                        // set the control's style to the named style
                        if ( css_style )
                        {
                            ctl.className = css_style;
                        }
                    }
                }
            }
            
            Owner.timeoutId = setTimeout(Owner.onHideMsgDlg, ERR_PAGE_TIMEOUT);
        }
    }
    
    try
    {
        /*
         * TODO: roll up this functionality into this.onShowMsgDlg - the function performs the
         *       same basic functionality with a few minor differences ... no time to do it now
         */
        //if ( (content_div) && (srv_message) && (0 < trim(srv_message.value).length) )
        if ( (srv_message) && (0 < trim(srv_message.value).length) )
        {
            var child_div;
            var ctl_img;
            var title;
            var title_div;
            var parts = srv_message.value.split(DEL_SEMICOLON);
            var is_error = ((3 == parts.length) && (CMD_ERROR == parts[2])) ? true : false;
            var err_param = ((3 <= parts.length) && (CMD_ERROR_PARAM == parts[2])) ? true : false;
            var srv_msg = parts[0];
            
            // we set this flag here as an indicator to other javascript files to
            // not perform any AJAX based validation.  Note that it is the reponsiblity
            // of the other scripts to actually use this flag - by itself, it means
            // nothing
            EnableAjaxValidation = false;
            
            is_error = is_error | err_param;
            
            var color = (true == is_error) ? CSS_CLR_ERR : CSS_CLR_INFO;
            
            if ( 1 < parts.length )
            {
                title = parts[1];
            }
            else if ( true == is_error )
            {
                title = TXT_ERR;
            }
            else
            {
                title = TXT_INFO;
            }
            
            window.alert(srv_msg);
    
            /*
            content_div.appendChild(this.msgDlg);
            this.msgDlg.className = CSS_SERVER_MSG + color;
            
            var block = document.createElement(TYPE_DIV);
            this.msgDlg.appendChild(block);
            block.className = CSS_RBLOCK_TOP;
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className =  CSS_RND4 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className =  CSS_RND3 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className =  CSS_RND2 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className =  CSS_RND1 + color;
            
            child_div = document.createElement(TYPE_DIV);
            this.msgDlg.appendChild(child_div);
            child_div.className = CSS_SERVER_MSG_TITLE;
            
            ctl_img = document.createElement(ELM_IMG);
            child_div.appendChild(ctl_img);
            ctl_img.src = IMG_INFO;
            ctl_img.className = CSS_INFO;
            
            title_div = document.createElement(TYPE_DIV);
            child_div.appendChild(title_div);
            title_div.innerText = title;
            title_div.innerHTML = title_div.innerText;
            title_div.className = CSS_SRV_MSG_TEXT;
            
            ctl_img = document.createElement(ELM_IMG);
            child_div.appendChild(ctl_img);
            ctl_img.src = IMG_CLOSE;
            ctl_img.className = CSS_CLOSE;
            
            ctl_img.onmouseup = this.onHideMsgDlg;
            
            child_div = document.createElement(TYPE_DIV);
            this.msgDlg.appendChild(child_div);
            child_div.className = CSS_SERVER_MSG_CONTENT;
            child_div.innerText = srv_msg;
            child_div.innerHTML = child_div.innerText;
            
            block = document.createElement(TYPE_DIV);
            this.msgDlg.appendChild(block);
            block.className = CSS_RBLOCK_BOTTOM;
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className = CSS_RND1 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className = CSS_RND2 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className = CSS_RND3 + color;
            
            tmp = document.createElement(ELM_B);
            block.appendChild(tmp);
            tmp.className = CSS_RND4 + color;
            
            var div_pos = getControlPosition(content_div);
            
            this.msgDlg.style.width = (content_div.clientWidth - 20) + TXT_PX;
            this.msgDlg.style.left  = (div_pos.x + 10) + TXT_PX;
            this.msgDlg.style.top   = (div_pos.y) + TXT_PX;
            
            // if err_param is true, then the server has determined the user input
            // is invalid (or some other condition has been presented by the user
            // that is otherwise invalid), and there are additional field definitions
            // that accompany the error condition
            if ( true == err_param )
            {
                var err_param;
                var ctl;
                var css_style = null;
                var first_param = true;
                
                // everything past parameter 3 is a field name with an optional CSS
                // class definition
                for (var prm_cnt=3; prm_cnt<parts.length; prm_cnt++)
                {
                    var err_param = parts[prm_cnt];
                    parts = err_param.split(DEL_COLON);
                    
                    // the first parameter is always the name of the field to highlight
                    // or set the form focus to
                    ctl = document.getElementById(parts[0]);
                    
                    // if there is a second parameter, it's the CSS class name to assign
                    // to the field.  Note that if the first field name is assigned a
                    // class name, all subsequent fields will use that class name until
                    // a new class name is encountered
                    if ( 1 < parts.length )
                    {
                        css_style = parts[2];
                    }
                    
                    // make sure we have a reference to the control specified
                    if ( ctl )
                    {
                        // we'll always set focus to the first control only
                        if ( true == first_param )
                        {
                            ctl.focus();
                            first_param = false;
                        }
                        
                        // if a style was specified (regardless of what control it was
                        // specified with - this is an optimization and is by design),
                        // set the control's style to the named style
                        if ( css_style )
                        {
                            ctl.className = css_style;
                        }
                    }
                }
            }
            
            this.timeoutId = setTimeout(this.onHideMsgDlg, ERR_PAGE_TIMEOUT);
            */
        }
    }
    catch (err)
    {
        // we don't expect an error condition during the processing of this
        // function ... doesn't mean there won't be one, we just don't expect
        // it and as such, we don't want scripts for the current page to
        // error out
    }
    
    return true;
}

// always create an instance of this object ... it'll check to see if
// the server has posted back any error messages to us or do nothing if
// no error/information condition message has occurred
var SrvMessage = new ServerMessageDlg();

/*
 * showControlError:
 *      displays the error control box with the specified message
 *      the control is positioned to the left of the control that
 *      generated the error
 * Params:
 *      control - the control the error is tied to
 *      message - the message to display to the user
 * Returns:
 *      true
 */
function showControlError(control, message)
{
    var err_box = new ErrBox();
    err_box.showControlError(control, message);
    return true;
}