var mobjLastKeyDownFunction;
var mstrServerWaitClass = "cssServerWaitBackgroundFilter"
var mstrPopupDivWaitClass = "cssPopupDivBackgroundFilter";

function studyB4_MessageBox(strMessage,strTitle,strButtons,strIconType,objCallBackFunction)
{
    var MESSAGE_BOX_DIV_ID                          = "divMessageBox";
    var MESSAGE_BOX_TITLE_DIV_ID                    = "divMessageBoxTitle";
    var divMessageBox                               = document.getElementById(MESSAGE_BOX_DIV_ID + strButtons);
    var objBody                                     = document.getElementsByTagName("body").item(0);
    var tdCell;
    var btnButtonYes;
    var btnButtonNo;
    var btnButtonOK;

    if (!divMessageBox)
    {
        //Create the div
        divMessageBox                               = document.createElement("div");
        
        //Set div properties
        divMessageBox.id                            = MESSAGE_BOX_DIV_ID + strButtons;
        divMessageBox.name                          = MESSAGE_BOX_DIV_ID + strButtons;
        divMessageBox.className                     = "cssMessageBoxDiv";

        //Append the div to the body
        objBody.insertBefore(divMessageBox, objBody.firstChild);
        
        //Create the title div
        var divMessageBoxTitle                      = document.createElement("div");
        divMessageBoxTitle.id                       = MESSAGE_BOX_TITLE_DIV_ID + strButtons;
        divMessageBoxTitle.name                     = MESSAGE_BOX_TITLE_DIV_ID + strButtons;
        divMessageBoxTitle.className                = "cssDivTitle";

        //Title span
        var spnMessageBoxTitle                      = document.createElement("span");
        spnMessageBoxTitle.id                       = "spnMessageBoxTitle" + strButtons;
        spnMessageBoxTitle.innerHTML                = strTitle;
        spnMessageBoxTitle.style.color              = "captiontext";
        spnMessageBoxTitle.style.whiteSpace         = "nowrap";

        //Append the span to the title div
        divMessageBoxTitle.appendChild(spnMessageBoxTitle);
        
        //Message box Table
        var tblMessageBox                           = document.createElement("table");
        tblMessageBox.id                            = "tblMessageBox" + strButtons;
        tblMessageBox.cellPadding                   = "10";
        tblMessageBox.cellSpacing                   = "10";
        tblMessageBox.style.margin                  = "0px auto";

        //Message box - text row
        var trRowText                               = tblMessageBox.insertRow(0);
        trRowText.id                                = "trMessageBoxText" + strButtons;
        
        //Text Cell
        tdCell                                      = trRowText.insertCell(0);
        tdCell.style.whiteSpace                     = "nowrap";

        if (objBody.style.direction == "rtl")
            tdCell.style.textAlign                  = "right";
        else
            tdCell.style.textAlign                  = "left";

        
        //Text span
        var spnMessageBoxText                       = document.createElement("span");
        spnMessageBoxText.id                        = "spnMessageBoxText" + strButtons;
        spnMessageBoxText.innerHTML                 = strMessage;

        //Append the span to the cell
        tdCell.appendChild(spnMessageBoxText);
        
        //Icon Cell
        tdCell                                      = trRowText.insertCell(1);
        
        //Text span
        var imgIcon                                 = document.createElement("img");
        imgIcon.id                                  = "imgIcon" + strButtons;
        imgIcon.src                                 = "";

        //Append the icon to the cell
        tdCell.appendChild(imgIcon);
        
        //Message box - buttons row
        var trRowButtons                            = tblMessageBox.insertRow(1);
        trRowButtons.id                             = "trMessageBoxButtons" + strButtons;
        trRowButtons.style.height                   = "50px";
        
        tdCell                                      = trRowButtons.insertCell(0);
        tdCell.id                                   = "tdMessageBoxNavigation" + strButtons;
        tdCell.colSpan                              = 2;
        tdCell.align                                = "center";
        tdCell.style.textAlign                      = "center";

        switch (strButtons)
        {
            case "YesNo":
            {
                //Yes
                btnButtonYes                                    = document.createElement("input");
                btnButtonYes.id                                 = "btnYes_" + strButtons;
                btnButtonYes.type                               = "button";
                btnButtonYes.value                              = myVars.Yes;
                btnButtonYes.className                          = "cssNavigationButtons";        
                btnButtonYes.onclick                            = function(){studyB4_MessageBoxFinish('Yes',objCallBackFunction);};

                //No
                btnButtonNo                                     = document.createElement("input");
                btnButtonNo.id                                  = "btnNo_" + strButtons;
                btnButtonNo.type                                = "button";
                btnButtonNo.value                               = myVars.No;
                btnButtonNo.className                           = "cssNavigationButtons";        
                btnButtonNo.onclick                             = function(){studyB4_MessageBoxFinish('No',objCallBackFunction);};

                //Append the buttons
                tdCell.appendChild(btnButtonYes);
                tdCell.appendChild(document.createTextNode('\u00A0')); //&nbsp in unicode
                tdCell.appendChild(btnButtonNo);

                break;
            }
            case "OK":
            {
                //OK
                btnButtonOK                                     = document.createElement("input");
                btnButtonOK.id                                  = "btnOK_" + strButtons;
                btnButtonOK.type                                = "button";
                btnButtonOK.value                               = myVars.OK;
                btnButtonOK.className                           = "cssNavigationButtons";        
                btnButtonOK.onclick                             = function(){studyB4_MessageBoxFinish('OK',objCallBackFunction);};

                //Append the span to the title div
                tdCell.appendChild(btnButtonOK);

                break;
            }
        }
        
        if (document.body.dir == "rtl")
            tdCell.align = "left";
        else
            tdCell.align = "right";
        
        //Append the title div to the message box div
        divMessageBox.appendChild(divMessageBoxTitle);

        //Append the table to the message box div
        divMessageBox.appendChild(tblMessageBox);
    }
    
    //Set current icon
    var strIcon;
    switch (strIconType)
    {
        case "Error":
        case "Info":
        case "Warning":
        case "Exclamation":
        case "Question":
        {
            strIcon = strIconType;
            break;
        }
        default:
        {
            strIcon = "Exclamation";
        }
    }
    
    var objIcon = document.getElementById("imgIcon" + strButtons);
    objIcon.src = myVars.BaseUrl + "Images/" + strIcon + ".gif";

    //Set current message
    var objMessageBoxText                       = document.getElementById("spnMessageBoxText" + strButtons);
    objMessageBoxText.innerHTML                 = decodeSpecialCharacters(strMessage);
    
    //Set current title
    var objMessageBoxTitle                      = document.getElementById("spnMessageBoxTitle" + strButtons);
    objMessageBoxTitle.innerHTML                = strTitle;

    //Must display in order to calculate proper width/height
    divMessageBox.style.display                 = "block";

    var divMessageBoxTitle                      = document.getElementById("divMessageBoxTitle" + strButtons);
    var tblMessageBox                           = document.getElementById("tblMessageBox" + strButtons);

    if (tblMessageBox.offsetWidth >= objMessageBoxTitle.offsetWidth)
        divMessageBox.style.width                   = tblMessageBox.offsetWidth;
    else
        divMessageBox.style.width                   = divMessageBoxTitle.offsetWidth;

    var objToFocus;

    //Set callback function for buttons
    switch (strButtons)
    {
        case "YesNo":
        {
            var objYes = document.getElementById("btnYes_" + strButtons)
            objYes.onclick = null;
            objYes.onclick = function(){studyB4_MessageBoxFinish('Yes',objCallBackFunction);};
            objToFocus = objYes;

            var objNo = document.getElementById("btnNo_" + strButtons)
            objNo.onclick = null;
            objNo.onclick = function(){studyB4_MessageBoxFinish('No',objCallBackFunction);};

            studyB4_ShowPopup(divMessageBox,
                                strButtons,
                                function(){studyB4_MessageBoxFinish('Yes',objCallBackFunction);},
                                function(){studyB4_MessageBoxFinish('No',objCallBackFunction);},
                                "CenterDocument",
                                "CenterDocument",
                                null,
                                mstrServerWaitClass);

            break;
        }    

        case "OK":
        {
            var objOK = document.getElementById("btnOK_" + strButtons)
            objOK.onclick = null;
            objOK.onclick = function(){studyB4_MessageBoxFinish('OK',objCallBackFunction);};
            objToFocus = objOK;

            studyB4_ShowPopup(divMessageBox,
                                strButtons,
                                function(){studyB4_MessageBoxFinish('OK',objCallBackFunction);},
                                null,
                                "CenterDocument",
                                "CenterDocument",
                                null,
                                mstrServerWaitClass);
            break;
        }
    }
    
    if (objToFocus)
        objToFocus.focus();
}

function studyB4_MessageBoxFinish(strAction,objCallBackFunction)
{
    var MESSAGE_BOX_DIV_ID  = "divMessageBox";
    var divMessageBox;
    
    switch (strAction)
    {
        case "Yes":
        case "No":
        {
            divMessageBox       = document.getElementById(MESSAGE_BOX_DIV_ID + "YesNo");
            break;
        }
        case "OK":
        {
            divMessageBox       = document.getElementById(MESSAGE_BOX_DIV_ID + "OK");
            break;
        }
    }
    
    if (divMessageBox)
    {
        studyB4_ClosePopup(divMessageBox,mstrServerWaitClass);
        
        if (objCallBackFunction)
        {
            this.myDialogResult = strAction;
            objCallBackFunction();
        }
    }       
}

function studyB4_SetBackgroundFilter(blnMode,objDocument, blnNoWaitIcon, strClassName)
{
    var objCurrentDocument;
    
    if (!objDocument)
        objCurrentDocument = document;
    else
        objCurrentDocument = objDocument;
    
    var strDivClass                                 = "";
    if (!strClassName)
    {
        strDivClass = mstrServerWaitClass;
    }
    else
    {
        strDivClass = strClassName;
    }
    
    var DIV_ID                                      = "divBackgroundFilter_" + strDivClass;
    var TABLE_ID                                    = "tblBackground_" + strDivClass;
    
    var divBackgroundFilter                         = objCurrentDocument.getElementById(DIV_ID);
    var objBody                                     = objCurrentDocument.getElementsByTagName("body").item(0);
    var tblBackground;
    
    if (!divBackgroundFilter)
    {
        //Create the div
        divBackgroundFilter = objCurrentDocument.createElement("div");
        
        //Set div properties
        divBackgroundFilter.id                      = DIV_ID;
        divBackgroundFilter.name                    = DIV_ID;
        divBackgroundFilter.className               = strDivClass;

        var strBackgroundColor;
        
        if (objBody.attributes["myFilterBackgroundColor"] && objBody.attributes["myFilterBackgroundColor"].value)
            strBackgroundColor = objBody.attributes["myFilterBackgroundColor"].value;
        else if (objBody.currentStyle) 
            strBackgroundColor = objBody.currentStyle["backgroundColor"]; 
        else if (window.getComputedStyle) 
            strBackgroundColor = window.getComputedStyle(objBody,"")["backgroundColor"]; 

        divBackgroundFilter.style.backgroundColor   = strBackgroundColor;
        
        tblBackground                               = objCurrentDocument.createElement("table");
        tblBackground.id                            = TABLE_ID;
        tblBackground.style.width                   = "100%";
        tblBackground.style.height                  = "100%";
        
        var trRow                                   = tblBackground.insertRow(0);
        var tdCell                                  = trRow.insertCell(0);
        
        var objAnimatedImage                        = objCurrentDocument.createElement("img");
        objAnimatedImage.src                        = myVars.BaseUrl + "Images/AnimatedWait.gif";
        tdCell.appendChild(objAnimatedImage);

        divBackgroundFilter.appendChild(tblBackground);
        
        objBody.insertBefore(divBackgroundFilter, objBody.firstChild);
    }

    if (!tblBackground)
        tblBackground =  objCurrentDocument.getElementById(TABLE_ID);  

    if (blnMode == true)
    {
        divBackgroundFilter.style.display = "block";
        if (blnNoWaitIcon && blnNoWaitIcon == true)
            //do not show animated gif as requested
            tblBackground.style.visibility = "hidden";
        else
        {
            //default - show animated gif
            tblBackground.style.visibility = "visible";
        }
    }
    else
        divBackgroundFilter.style.display = "none";
}

function studyB4_ShowPopup(divObject,strMode,objOKFunction, objCancelFunction, strLeft, strTop, strFocusControlId, strClassName)
{
    var intLeft;
    var intTop;
    var objBody                     = document.getElementsByTagName("body").item(0);
    var strDivClass                 = "";
    
    if (strClassName)
    {
        strDivClass = strClassName;
    }
    else
    {
        strDivClass = mstrPopupDivWaitClass;
    }
    
    studyB4_SetBackgroundFilter(true,null,true,strDivClass);

    var divFrame = document.getElementById(divObject.id + "_iframe");
    if (!divFrame)
    {
        //Create iFrame that hides combo boxes - first time only
        divFrame                        = document.createElement("iframe");
        divFrame.id                     = divObject.id + "_iframe";
        divFrame.scrolling              = "no";
        divFrame.frameBorder            = "0";

        divFrame.style.display          = "none";
        divFrame.style.position         = "absolute";
        divFrame.style.zIndex           = divObject.style.zIndex;
        objBody.insertBefore(divFrame, objBody.firstChild);
    }

    //Show the div
    divObject.style.display             = "block";
    divFrame.style.display              = "block";

    if (strLeft)
        switch (strLeft)
        {
            case "CenterDocument":
            {
                intLeft                 = (objBody.clientWidth - divObject.offsetWidth)/2;
                divObject.style.left    = intLeft + "px";
                break;
            }
            
            default:
            {
                divObject.style.left    = strLeft;
                break;
            }
        }

    if (strTop)
        switch (strTop)
        {
            case "CenterDocument":
            {
                intTop                  = (objBody.clientHeight - divObject.offsetHeight)/2;
                divObject.style.top     = intTop + "px";
                break;
            }
            
            default:
            {
                divObject.style.top = strTop;
                break;
            }
        }
    divFrame.style.width                = divObject.offsetWidth + "px";
    divFrame.style.height               = divObject.offsetHeight + "px";
    divFrame.style.left                 = divObject.offsetLeft + "px";
    divFrame.style.top                  = divObject.offsetTop + "px";        

    mobjLastKeyDownFunction = document.onkeydown;
    document.onkeydown = 
            function (evt) 
            {  
                var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : window.event.keyCode;
                switch (keyCode)
                {
                    case 13: //Return
                    {
                        if (objOKFunction)
                            objOKFunction();
                        break;
                    }
                    
                    case 27: //Escape
                    {
                        if (objCancelFunction)
                            objCancelFunction();
                        break;
                    }
                }
            }

    //Private property - used for divs that serve both for insert/update
    divObject.myMode            = strMode;
    
    if (strFocusControlId)
        studyB4_Focus(strFocusControlId);
}

function studyB4_ClosePopup(divObject, strClassName)
{
    divObject.style.display         = "none";
    var divFrame                    = document.getElementById(divObject.id + "_iframe");
    
    //Might be null in case of "Delete action" which might not show the div
    if (divFrame)
        divFrame.style.display          = "none";
    
    divObject.myMode                = "";

    document.onkeydown              = mobjLastKeyDownFunction;

    var strDivClass                 = "";
    if (!strClassName)
    {
        strDivClass                 = mstrPopupDivWaitClass;
    }
    else
    {
        strDivClass                 = strClassName;
    }

    studyB4_SetBackgroundFilter(false,null,null,strDivClass);
}

function studyB4_Focus(strControlName, intAsyncTimeout)
{
    if (!intAsyncTimeout)
    {
        var objControl = document.getElementById(strControlName);
        if (objControl)
        {
            try
            {
                objControl.focus();
            }
            catch(e)
            {
            }
        }
    }
    else
        setTimeout("try {document.getElementById('" + strControlName + "').focus();} catch(e){}",intAsyncTimeout);
}

function studyB4_SetTopMenu(objHighlightCell,strHighlightCellId)
{
    var stripeTop = top.document.getElementById("studyB4Master_stripeTop");
    
    if (stripeTop && stripeTop.contentWindow)
    {
        var stripeTopContentWindow = stripeTop.contentWindow;

        if (stripeTopContentWindow.selectTopMenu)
            try
            {
                stripeTopContentWindow.selectTopMenu(objHighlightCell,strHighlightCellId);
            }
            catch(e)
            {
            }
    }    
}

function studyB4_Set3DButtons(strButtonId,strAction,strColor)
{
    var tdLeftButton    = document.getElementById("studyB4Master_bodyContent_" + strButtonId + "Left");
    var tdButton        = document.getElementById("studyB4Master_bodyContent_" + strButtonId);
    var tdRightButton   = document.getElementById("studyB4Master_bodyContent_" + strButtonId + "Right");

    switch (strAction)
    {
        case 'out':
        {
            tdButton.className = "css3DButton" + strColor;

            if (tdLeftButton.className.substring(0,15) == "cssLeft3DButton")
            {
                tdLeftButton.className      = "cssLeft3DButton" + strColor;
                tdRightButton.className     = "cssRight3DButton" + strColor;
            }
            else
            {
                tdLeftButton.className      = "cssRight3DButton" + strColor;
                tdRightButton.className     = "cssLeft3DButton" + strColor;
            }
            break;
        }
        
        case 'hover':
        {
            tdButton.className = "css3DButton" + strColor + "Hover";

            if (tdLeftButton.className.substring(0,15) == "cssLeft3DButton")
            {
                tdLeftButton.className      = "cssLeft3DButton" + strColor + "Hover";
                tdRightButton.className     = "cssRight3DButton" + strColor + "Hover";
            }
            else
            {
                tdLeftButton.className      = "cssRight3DButton" + strColor + "Hover";
                tdRightButton.className     = "cssLeft3DButton" + strColor + "Hover";
            }
            
            break;
        }
    }
}

function studyB4_SetHoverState(objElement,blnHover)
{
    if (blnHover == true)
    {
        objElement.className = objElement.className + "_hover";
    }
    else
    {
        var intIndex = objElement.className.indexOf("_hover");
        if (intIndex >= 0)
            objElement.className = objElement.className.substring(0,intIndex);
    }
}

function studyB4_GetControl(strControlId)
{
    //Ajax may replace id of the combo not to include the prefix
    var objControl = document.getElementById("studyB4Master_bodyContent_" + strControlId);
    if (!objControl)
        objControl = document.getElementById(strControlId);
        
    return objControl;
}
