﻿// Copyright 2008-2011: Thomson Reuters Global Resources. All Rights Reserved.
// Proprietary and Confidential information of TRGR. Disclosure, Use or
// Reproduction without the written authorization of TRGR is prohibited.

// ***************************************************************************************************
// Summary: Encapsulates common error handler routines.
// ***************************************************************************************************
var errorHandler = {

    showErrorMessage: function (error) {
        if (error) {
            showBasicMessageBoxAlert(error.description);
        }
    }
}

// global variables
var objName = null, // the global variable that holds object that the message should appear under (if the message is not centered)
             // passed up thru javascript
    tfsDiv = "thom_fullscreen_div";
var forceIframe = false;
    

function showBasicMessageBox(detailText, underObject)
{ 
    generalMessageBoxFormatting('basicMessage','Error_icon_only.gif', 
            detailText, 'bold', null, null, 'bold', underObject, 0, null, null, null, null, null, null, null);
}

function showBasicMessageBoxAlert(detailText)
{ 
    generalMessageBoxFormatting('basicMessage','Error_icon_only.gif', 
            detailText, 'bold', null, null, 'bold', null, 0, null, null, null, null, null, null, null);
}

function showBasicMessageBoxConfirm(detailText, OkCancel, okFunctionToPerform, param1, param2, param3, param4, param5 )
{
    generalMessageBoxFormatting('basicMessage','Error_icon_only.gif', 
            detailText, 'bold', OkCancel, null, 'bold', null, 0, null, okFunctionToPerform, param1, param2, param3, param4, param5);                
}

function showBasicMessageBoxConfirmInformational(detailText, OkCancel, okFunctionToPerform, param1, param2, param3, param4, param5, isIframe)
{
    generalMessageBoxFormatting('informationalMessage','icon_Lightbulb.png',
            detailText, 'bold', OkCancel, null, 'bold', null, 0, null, okFunctionToPerform, param1, param2, param3, param4, param5, isIframe);                
}

function showBasicMessageBoxConfirmWithExtender(detailText, underObject, OkCancel, popupID, okFunctionToPerform, param1, param2, param3, param4, param5)
{
    generalMessageBoxFormatting('basicMessage','Error_icon_only.gif', 
            detailText, 'bold', OkCancel, null, 'bold', underObject, 1, popupID, okFunctionToPerform, param1, param2, param3, param4, param5);
}

function showBasicMessageBoxScreenCenteredWithExtender(headerText, detailText, underObject, popupID)
{
    generalMessageBoxFormatting('basicMessage','Error_icon_only.gif', 
            detailText, 'bold', null, headerText, 'bold', underObject, 1, popupID, null, null, null, null, null, null );         
}

function showBasicMessageBoxScreenCentered(headerText, detailText, underObject, isIframe)
{
    generalMessageBoxFormatting('basicMessage','Error_icon_only.gif',
            detailText, 'bold', null, headerText, 'bold', underObject, 1, null, null, null, null, null, null, null, isIframe);         
}

function showFatalMessageBox(detailText)
{
    generalMessageBoxFormatting('fatalMessage','Error_icon_only.gif',              
            detailText, 'bold', null, null, 'bold', 0, null, null, null, null, null, null, null);
}

function showInformationalMessageBox(headerText, detailText, isIframe)
{
    generalMessageBoxFormatting('informationalMessage','icon_Lightbulb.png',
            detailText, 'bold', null, headerText, 'bold', null, 0, null, null, null, null, null, null, null, isIframe);
}

function showSelectTextMessageBox(headerText, detailText)
{
    generalMessageBoxFormatting('basicMessage','Error_icon_only.gif', 
            detailText, 'bold', null, headerText, 'bold', null, 0, null, null, null, null, null, null, null);         
}

//***************************************************************************************************
// Generic function to show a pm2 style hover box.  The message is displayed
// in a div tag defined on the master page.
//
// Arguments are: 
//  1) the headerText to be displayed (if any).  This text will be bolded.
//  2) the detailText to be displayed (if any).  This text will be in normal font weight.
//  3) the event from the mouseover - this determines the positioning of the hover box.
//  4) the required width of the div (optional).
//
// The box will be displayed to the bottom right of the control which received the mouseover event.
// The box will be displayed until hideMessageBox is called.
//**************************************************************************************************
function showHoverMessageBox(headerText, detailText, event, divWidth) 
{


    if (event == null) {
        alert("event is null");
    }
    
    
    init();
    var isIE = (document.all?true:false);

    var mouseXPosition = 0, mouseYPosition = 0, divTagXPosition = 0, divTagYPosition = 0, divTagWidth = 0, windowWidth = 0;
    
    // Get the position of the mouse in the screen.
    if(isIE)
    {
        mouseXPosition = event.clientX +
                    (document.documentElement.scrollLeft ?
                        document.documentElement.scrollLeft :
                        document.body.scrollLeft);
        mouseYPosition = event.clientY +
                    (document.documentElement.scrollTop ?
                        document.documentElement.scrollTop :
                        document.body.scrollTop);
    }
    else 
    {
        if (event != null) {
            mouseXPosition = event.pageX;
            mouseYPosition = event.pageY;
        }
        else 
        {
            mouseXPosition = "100px";
            mouseXPosition = "200px";       
        }

    }  

    // Find the floating div
    var divTag = document.getElementById('hoverMessage');
    if (divTag == null)
    {
        createDivTagForHover("hoverMessage");
    }
    if (divTag == null) divTag = document.getElementById('hoverMessage');

    
     
    if (divTag != null)
    {
        var divTagHTML = "<TABLE cellPadding='8'><tr>";
        divTagHTML += "<td rowspan='1' style='vertical-align:text-middle;margin:15px;text-align:left;'>";
        if (headerText)
        {
            // display the header text in bold
            divTagHTML += "<b>" + unescape(headerText) + "</b><br><br>";
        }
        
        if (detailText)
        {
            divTagHTML += unescape(detailText);
        }
        
        divTagHTML += "</td></tr></TABLE>";
     
        divTag.innerHTML = divTagHTML;    

        if (divWidth)
        {
            divTag.style.width = divWidth;
        }
        
        // Figure out how wide the div is and calculate where it should
        // be displayed based on the width and the position of the mouse
        divTagWidth = divTag.offsetWidth;
        windowWidth = getWindowWidth();
        
        // will this go off the RHS of the screen ?
        if( divTagWidth + mouseXPosition >= windowWidth + 20 )
        {
            // skoosh this over a bit
            var moveOverLeftAmount = (divTagWidth + mouseXPosition) - windowWidth;
            divTagXPosition = mouseXPosition - moveOverLeftAmount;
            if (divTagXPosition < 0) 
            {
                divTagXPosition = 10;
            }
        }
        else
        {
            divTagXPosition = mouseXPosition + 15;
        }
        
        divTagYPosition = mouseYPosition;
        divTagXPosition = divTagXPosition + "px";
        divTagYPosition = divTagYPosition + "px";
    
        divTag.style.left = divTagXPosition;
        divTag.style.top = divTagYPosition;
        divTag.style.visibility = 'visible';                
     }       
}
       
//*******************************************
// Generic function to hide a div. 
// Can be used to hide all message box types
//*******************************************
function hideMessageBox(divTagId)
{ 
    var divTag = getElementIDFromDocumentOrParent(divTagId);   
    if (divTag)
    {
        divTag.parentNode.removeChild(divTag);
    }  
}

//*******************************************************************************************
// General function to show a pm2 style Message Box of type passed in
//
// Arguments are: 
//  1) divTagId:         id of the div in which to display the box (this is defined on the master page).
//  2) icon:             filename of icon if one is to be used (optional)
//  3) detailText:       the detailText to be displayed (if any).  
//  4) detailTextWeight: the detailText font weight (optional, default is regular).  
//  5) messageBoxButtons:   denotes to which buttons to show within the div tag.  Possiblel values are: Default or null means OK only, ContinueAndCancel, OkAndCancel
//  6) headerText:       the headerText to be displayed (if any).  This text will be bolded.
//  7) headerTextWeight: the headerText font weight for the diplayed text (optional, default is regular)
//  8) underObject       if this is true, use the button object that is passed in
//
// generalMessageBoxFormatting('messagetype','icon file', 'detailText', 'detailTextWeight', 
//                              'messageBoxButtons', 'headerText','headerTextWeight', underOjbect);
//
//*******************************************************************************************
function generalMessageBoxFormatting(divTagId, icon,  detailText, detailTextWeight, 
                                        messageBoxButtons, headerText, headerTextWeight, underObject, offset, popupID, okFunctionToPerform, param1, param2, param3, param4, param5, isIframe)
{
    init();

    var divTagXPosition = 0, divTagYPosition = 0, divTagWidth = 0, divTagHeight = 0, windowWidth = 0;  windowHeight = 0;

    if (isIframe != null) {
        forceIframe = isIframe;
    }
    else {
        forceIframe = false;
    }
     
    windowWidth = getWindowWidth();
    windowHeight = getWindowHeight();   
    
     // Find the floating full screen div
    var divTagFullScreen = document.getElementById(tfsDiv);
    if (divTagFullScreen == null)
    {
        // create the div tag
        createPageSizeDivTag(windowWidth, windowHeight);
        
        // set divTag variable
        divTagFullScreen = getElementIDFromDocumentOrParent(tfsDiv);    
    }
    
    // Find the floating error box div
    var divTag = document.getElementById(divTagId);     
    if (divTag == null)
    {
        createDivTag(divTagId);
               
        // set divTag variable
        divTag = getElementIDFromDocumentOrParent(divTagId);      
    }                   
    //if (divTag == null) divTag = document.getElementById(divTagId);
    
    if (divTag != null)
    {  
        //divTagFullScreen.appendChild(divTag);
          
        // HTML to create a spaced table, containing an image if needed
        var divTagHTML = "<TABLE cellPadding='5'><tr ><td rowspan='3' style='vertical-align:top;margin:15px;'>";
        if (icon)
        {
            divTagHTML += "<IMG src='/images/" + icon + "'>&nbsp;";
        }
        divTagHTML += "</td><td rowspan='2' style='vertical-align:text-middle;font-size:11px;margin:15px;text-align:left;position:static;z-index:10000;'>";

        if (headerText)
        {
            if (headerTextWeight == 'bold')
            {
                // display the header text in bold
                divTagHTML += "<b>" + unescape(headerText) + "</b><br><br>";
            }
            else                
            {
                // display the header text not in bold
                divTagHTML += unescape(headerText) + "<br><br>";
            }
        }
        
        if (detailText)
        {
            if (detailTextWeight == 'bold')
            {
                divTagHTML += '<b>' + unescape(detailText) + '</b><br>';
            }
            else
            {
                divTagHTML += unescape(detailText) + "<br><br>";    
            }
        }
        
        divTagHTML += "</td></tr>";   
              
        isOnlyOKButton = true;
        
        // If messageBoxButtons is anything but null, display both the Ok and Cancel button within the div tag
        if ( messageBoxButtons != null) 
        {   
            var imagePath = '/images/ok_btn.gif';
            if (messageBoxButtons == "ContinueAndCancel")
            {
                imagePath = '/images/continue_btn.gif'
            }
            
            // In the case of Patent classifications, there are 3 treeviews and we need to keep track of which one was selected.
            var hrefLocation = "#";
            if( document.location.href.indexOf('#classType', 0) > 0){
                hrefLocation = document.location.href;
            }
            
            divTagHTML += "<tr><td></td></tr><tr><td align='center'><a href='" + hrefLocation + "' id='okButton'><img border='0' src='" + imagePath + "' style='cursor: pointer;' /></a>&nbsp;&nbsp;"
            divTagHTML += "<a href='" + hrefLocation + "' id='cancelButton' onclick='cancelButtonClicked(\""+divTagId+"\", \""+tfsDiv+"\", \""+popupID+"\",\""+underObject+"\",\""+detailText+"\")'><img border='0' src='/images/cancel_btn.png' style='cursor: pointer;' /></a></td></tr></TABLE>"   
        
            isOnlyOKButton = false;
        }
        else { 
            divTagHTML += "<tr><td></td></tr><tr><td align='center'><a href='#' id='okOnlyButton'><img border='0' src='/images/ok_btn.gif'style='cursor: pointer;' /></a></td></tr></TABLE>"   
        }
               
        divTag.innerHTML = divTagHTML;  
              
        if ( messageBoxButtons != null) {
            // Load up the Ok button for Div tag that has an OK and a Cancel
            // We can't use array parameters in the divTagHTML setup so we need to use this approach to activate the OK button upon being clicked       
            var okButton = getElementIDFromDocumentOrParent('okButton');
            var functionToCall = function() { okButtonClicked( divTagId, tfsDiv,popupID, underObject, detailText, okFunctionToPerform, param1, param2, param3, param4, param5); };
            if (okButton != null) {
             okButton.onclick = functionToCall;
            }  
        }    
        else {          
            // Load up the Ok button for Div tag that has only the OK 
            // We can't use array parameters in the divTagHTML setup so we need to use this approach to activate the OK button upon being clicked       
            var okOnlyButton = getElementIDFromDocumentOrParent('okOnlyButton');
            var okOnlyFunctionToCall = function() { okButtonClicked( divTagId, tfsDiv,popupID, underObject, detailText, okFunctionToPerform, param1, param2, param3, param4, param5); };
            if (okOnlyButton != null) {
                okOnlyButton.onclick = okOnlyFunctionToCall;  
            }    
        }          
        
        // is this message displayed under an object or centered        
        // boolean passed in to indicate that we use the object in the 
        // global variable
        //if (underObject & isIE)
        var scrollXY = getScrollXY();
        var rtnXandYValues = new Array(2);
        if (underObject)
        {
            var buttonObj = getElementIDFromDocumentOrParent(underObject);
            if (buttonObj)
            {
            
                var location = findPos(buttonObj);
                
                if (offset){
                    divTagXPosition = (windowWidth / 2) - (divTag.offsetWidth / 2);
                    divTagYPosition = location[1] + 36
                }
                else {   
                    divTagXPosition = location[0];
                    divTagYPosition = location[1] + 30;                    
                }            

                // make sure it doesn't go off RHS of screen
                var divTagOver = divTagXPosition + divTag.offsetWidth;
                if (divTagOver > windowWidth)
                {
                    // move message over
                    divTagXPosition = (windowWidth - divTag.offsetWidth);
                    if (divTagXPosition < 0)
                    {   
                        // don't let the x position go negative
                        divTagXPosition = 10;
                    }
                }     

                // make sure it doesn't go off bottom of screen, accounting for any scrolling
                var divTagBottom = divTagYPosition + scrollXY[1] + divTag.offsetHeight;
                if (divTagBottom > (windowHeight + scrollXY[1]))
                {
                    divTagYPosition = (windowHeight + scrollXY[1] - divTag.offsetHeight - 15 );
                }                      
           }
           else
           {
                rtnXandYValues = centerDivTag(divTag,windowWidth,scrollXY);
                divTagXPosition = rtnXandYValues[1];
                divTagYPosition = rtnXandYValues[2] + 150;
           }
        }
        
        else
        {
            rtnXandYValues = centerDivTag(divTag,windowWidth,scrollXY);
            divTagXPosition = rtnXandYValues[1];
            divTagYPosition = rtnXandYValues[2];

        }

        divTag.style.left = divTagXPosition  + "px";;
        divTag.style.top = divTagYPosition  + "px";;
        divTag.style.visibility = 'visible';
        divTagFullScreen.style.visibility = 'visible';     
        
        // For FireFox disable the scrollbar until the error box has been clicked on
        if (clientIsFF()) {
            window.parent.document.body.style.overflow = "hidden";
            
            // When dealing with an IFrame we ALSO have to turn off/hide the scroll bar for Iframe also
            if (forceIframe) {
                document.body.style.overflow = "hidden";
            }          
        }
        else {
            // For IE and others disable the scrollbar until the error box has been clicked on
            window.parent.document.body.scroll = "no";
            
            // When dealing with an IFrame we ALSO have to turn off/hide the scroll bar for the Iframe also
            if (forceIframe) {
                document.body.scroll = "no";
            }
        }        
        
        // disable the calling popup Extender (if one exists) temporarily until user clicks OK or Cancel
        if ( popupID != null) {
                  
            // disable the Object the user clicked on (i.e., Save button)
            if ( underObject != null ){
                document.getElementById(underObject).disabled = true;
                document.getElementById(underObject).style.cursor = "none";
                if (document.getElementById(underObject).src.match("/images/Save_btn.gif")) {
                    document.getElementById(underObject).src = "/images/Save_Disabled_btn.gif";
                }       
            }
            
            // Disable the text elements in the popupExtender
            var arrElements = $find(popupID)._popupElement.getElementsByTagName("input");
            if (arrElements != null) {
                for (var i = 0; i < arrElements.length; i++) {
                    arrElements[i].disabled = true;
                }
            }
            
            // Set the Z-index to be a smaller number than the error_message pop-up so 
            // the error message never gets caught behind the popupExtender if the user scrolls
            $find(popupID)._popupElement.style.zIndex = 10;
            // Disbale the Close button (and X)
            if ($find(popupID)._CancelControlID) {
                document.getElementById($find(popupID)._CancelControlID).disabled = true;
            }
        }
                                
     }
     // set the bogus IFrame to the size of the error message so that it remains on top, 
     // since IE6.0 puts dropdowns on top of all objects
            divTag.style.display = 'block';
            var iframe = document.getElementById('iframetop');
            iframe.style.display = 'block';
            iframe.style.width = divTag.offsetWidth;
            iframe.style.height = divTag.offsetHeight;
            iframe.style.left = divTag.offsetLeft;
            iframe.style.top = divTag.offsetTop;
            
         
     // set focus to OK button; this is needed for when user hits <ENTER>. 
     // <Enter> will default to OK click action if focus is still active on OK button.
     okButton = null;
     if(isOnlyOKButton)
     {
        okButton = getElementIDFromDocumentOrParent('okOnlyButton');
     }
     else
     {
        okButton = getElementIDFromDocumentOrParent('okButton');
     }     
     if(okButton)
     {
         okButton.focus();
         // this is to prevent the OK button from losing focus; OK button has a default focus but loses it when user clicks somewhere else thus causes the <return> click to not function; 
         // we will force constant focus on OK button when error message is visible.
         // Req: 52.1.10.1
         okButton.onblur = function() 
         {
             // timeout is used to accommodate all browser types
             // if button loses focus we will refocus back
             setTimeout("keepFocusOnOKButton('" + okButton.id + "')", 10);
         }
     }           
}

// This function is to keep focus on OK button at all times.
// It is used in conjunction with <Enter> click default action when error is displayed.
// Cancel button works as designed provided that user clicks on button. Tabbing will not work.
function keepFocusOnOKButton(OKButtonControlId) 
{
    var OKButton = $get(OKButtonControlId);
    if (OKButton) {
        OKButton.focus();
    }
}

function getScreenCenterY() 
{
    var y = 0;
    y = getScrollOffset()+(getInnerHeight()/2);

    return(y);
}

function getInnerHeight() 
{
    var y;
    if (self.innerHeight) // all except Explorer
    {
        y = self.innerHeight;
    }
    else if (document.documentElement &&
        document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
    {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
        y = document.body.clientHeight;
    }
    return(y);
}

function getScrollOffset() 
{
    var y;
    if (self.pageYOffset) // all except Explorer
    {
        y = self.pageYOffset;
    }
    else if (document.documentElement &&
        document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
      y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
       y = document.body.scrollTop;
    }
    return(y);
}

// Create dynamic div tag 
function createDivTagForHover(divTagId)
{
    // the ID and the Class name need to be the same for this to work
    ldiv = document.createElement("div");
    ldiv.id = divTagId;
    ldiv.style.visibility = 'hidden';
    ldiv.style.position = 'absolute';
    ldiv.className = divTagId;
    page_body.appendChild(ldiv);

}

// Create dynamic div tag 
function createDivTag(divTagId)
{      
    var outter_document = getDocumentOrParent();
          
    // the ID and the Class name need to be the same for this to work
    ldiv = outter_document.createElement("div");
    ldiv.id = divTagId;
    ldiv.style.visibility = 'hidden';
    ldiv.style.position = 'absolute';   
    ldiv.className = divTagId;
    outter_document.body.appendChild(ldiv);
}

// Create dynamic div tag for the Page Size div
function createPageSizeDivTag(windowWidth, windowHeight)
{
    var outter_document = getDocumentOrParent();   

    // the ID and the Class name need to be the same for this to work
    lpagediv = outter_document.createElement("div");
    lpagediv.id = tfsDiv;   
    lpagediv.style.visibility = 'hidden';  
    if (clientIsFF()) {  
        lpagediv.style.width = "100%";
        //lpagediv.style.height = outter_document.documentElement.scrollHeight.toString() + 'px';
        lpagediv.style.height = getFrameHeight(outter_document).toString() + 'px';    
    }
    else {
        lpagediv.style.width = "100%";
        //lpagediv.style.height = outter_document.parentWindow.screen.height; 
        lpagediv.style.height = outter_document.documentElement.scrollHeight > outter_document.documentElement.clientHeight ? outter_document.documentElement.scrollHeight : outter_document.documentElement.clientHeight;
    }
    lpagediv.className = tfsDiv;
    outter_document.body.appendChild(lpagediv);   
}

//*******************************************
// Function to get the height of the window
// for various browser types.
//*******************************************
function getWindowHeight()
{
    var myHeight = 0;
    var outter_document = getDocumentOrParent();
    
    if( typeof( window.innerHeight ) == 'number' )
    {
        //Non-IE
        myHeight = window.innerHeight;
    }
    else if( outter_document.documentElement && outter_document.documentElement.clientHeight )
    {
        //IE 6+ in 'standards compliant mode'
        myHeight = outter_document.documentElement.clientHeight;
    }
    else if( document.body && document.body.clientHeight )
    {
        //IE 4 compatible
        myHeight = outter_document.body.clientHeight;
    }
    return myHeight;
}

function getScrollXY() 
{
  var scrOfX = 0, scrOfY = 0;
  var outter_document = getDocumentOrParent();
  
  if( typeof( window.pageYOffset ) == 'number' ) 
  {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  }
  else if( outter_document.body && ( outter_document.body.scrollLeft || outter_document.body.scrollTop ) ) 
  {
    //DOM compliant
    scrOfY = outter_document.body.scrollTop;
    scrOfX = outter_document.body.scrollLeft;
  } 
  else if( outter_document.documentElement && ( outter_document.documentElement.scrollLeft || outter_document.documentElement.scrollTop ) ) 
  {
    //IE6 standards compliant mode
    scrOfY = outter_document.documentElement.scrollTop;
    scrOfX = outter_document.documentElement.scrollLeft;
  }
  
  return [ scrOfX, scrOfY ];
}
//*******************************************
// Function to get the width of the Window
// for various browser types.
//*******************************************
function getWindowWidth()
{
    var myWidth = 0;
    var outter_document = getDocumentOrParent();

    if( typeof( window.innerWidth ) == 'number' )
    {
        //Non-IE
        myWidth = window.innerWidth;
    }
    else if( document.documentElement && document.documentElement.clientWidth )
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = outter_document.documentElement.clientWidth;
    }
    else if( document.body && document.body.clientWidth )
    {
        //IE 4 compatible
        myWidth = outter_document.body.clientWidth;
    }
    return myWidth;
}

function okButtonClicked(divTagId, tfsDiv, popupID, underObject, detailText, okFunctionToPerform, param1, param2, param3, param4, param5)
{
    hideMessageBox(divTagId);
    hideMessageBox(tfsDiv);
    
    // For FireFox enable the scrollbar until the error box has been clicked on
    if (clientIsFF()) {
        window.parent.document.body.style.overflow = "visible";
    }
    else {
        // For IE and others enable the scrollbar until the error box has been clicked on
        window.parent.document.body.scroll = "";  
    }        
    
    if ( popupID != null &&  popupID != "null" ) {
        
        // Enable the Object the user clicked on (i.e., Save button)
        if ( underObject != null && underObject != "null"){
            document.getElementById(underObject).disabled = false;
            document.getElementById(underObject).style.cursor = "hand";
            if (document.getElementById(underObject).src.match("/images/Save_Disabled_btn.gif")) {
                document.getElementById(underObject).src = "/images/Save_btn.gif";
            }
        }
            
        // Enable the text elements in the popExtender
        var arrElements = $find(popupID)._popupElement.getElementsByTagName("input");
        if (arrElements != null) {
            for (var i = 0; i < arrElements.length; i++) {
                arrElements[i].disabled = false;
            }
        }

        // Enable the Close button (and X)
        if ($find(popupID)._CancelControlID) {
            document.getElementById($find(popupID)._CancelControlID).disabled = false;
        }
        // Set the Z-index to be it's original setting 
        $find(popupID)._popupElement.style.zIndex = 10001;  
    }
    
    // call the function that was passed in (if one was passed in)
    if ( okFunctionToPerform != null) {
        if ( param5 != null) {
            okFunctionToPerform(param1,param2,param3,param4,param5);  
        }  
        else if ( param4 != null) {
            okFunctionToPerform(param1,param2,param3,param4);           
        } 
        else if ( param3 != null) {
            okFunctionToPerform(param1,param2,param3);           
        }
        else if (param2 != null) {
            okFunctionToPerform(param1,param2);
        }
        else if (param1 != null) {
            okFunctionToPerform(param1);
        }
        else if (param1 == null) {
            okFunctionToPerform();
        }        
    }   
    
    setFromCancelInUrlToFalse();   
    // remove iframe that is behind the message
    var iframe = document.getElementById('iframetop');
    iframe.style.display = 'block';
    iframe.style.width = 0;
    iframe.style.height = 0;
    iframe.style.left = 0;
    iframe.style.top = 0;

}

function cancelButtonClicked(divTagId, tfsDiv, popupID, underObject)
{
    hideMessageBox(divTagId);
    hideMessageBox(tfsDiv);
    
     // For FireFox enable the scrollbar until the error box has been clicked on
    if (clientIsFF()) {
        window.parent.document.body.style.overflow = "visible";
    }
    else {
        // For IE and others enable the scrollbar until the error box has been clicked on
        window.parent.document.body.scroll = "";  
    }           
        
    if ( popupID != "null" && popupID != null) {
    
        // Enable the Object the user clicked on (i.e., Save button)
        if ( underObject != null && underObject != "null"){
            document.getElementById(underObject).disabled = false;
            document.getElementById(underObject).style.cursor = "hand";
            if (document.getElementById(underObject).src.match("/images/Save_Disabled_btn.gif")) {
                document.getElementById(underObject).src = "/images/Save_btn.gif";
            }         
        }
            
        // Enable the text elements in the popExtender 
        $find(popupID)._popupElement.disabled = false;
         // enable the text elements in the popupExtender
        var arrElements = $find(popupID)._popupElement.getElementsByTagName("input");
        if (arrElements != null)
        {
            for (var i = 0; i < arrElements.length; i++)
            {
                arrElements[i].disabled = false;
            }
        }
        
        // Enable the Close button (and X)
        document.getElementById($find(popupID)._CancelControlID).disabled = false;
        // Set the Z-index to be it's original setting 
        $find(popupID)._popupElement.style.zIndex = 10001;
    }
    // remove iframe that is behind the message
    var iframe = document.getElementById('iframetop');
    iframe.style.display = 'block';
    iframe.style.width = 0;
    iframe.style.height = 0;
    iframe.style.left = 0;
    iframe.style.top = 0;

}

function getDocumentOrParent()
{
    if (window.parent.document && !forceIframe) {
       return window.parent.document;
   }
   else {
      return document;
   }
}

function getElementIDFromDocumentOrParent(divTagId) {

    try {
        var divTag = document.getElementById(divTagId);

        if (divTag == null) {
            divTag = window.parent.document.getElementById(divTagId);
        }
        return divTag;
    }
    catch (e) {
        //do nothing. this is used when accessing via a Share Point environment.
    }
}   

function getFrameHeight (frame)
{
   var document = frame;
   var doc_height = document.height ? document.height : 0; // Safari uses document.height

   if (document.documentElement && document.documentElement.scrollHeight) /* Strict mode */
      return Math.max (document.documentElement.scrollHeight, doc_height);
   else /* quirks mode */
      return Math.max (document.body.scrollHeight, doc_height);
}

function setFromCancelInUrlToFalse(){ 
    var origUrl = document.location.href;
    var newUrl;
    if (origUrl.indexOf('&fromcancel', 0) > 0) 
    {
        //remove the fromcancel parameter from the URL
        //Fix for tracker 2272
        var startPos = origUrl.indexOf('&fromcancel');
        newUrl = origUrl.substr(0, startPos);
        
        document.location.href = newUrl;  
    }    
}

function centerDivTag(divTag, windowWidth, scrollXY)
{
    // this function returns the centered x/y positions on the window
    
    var returnValues = new Array(2);
    var windowHeight = getWindowHeight();   

    // Find the size of the div, and the center of the screen
    // center the message
    divTagWidth = divTag.offsetWidth;
    divTagHeight = divTag.offsetHeight;

    var windowCenterX = windowWidth / 2;
    var windowCenterY = scrollXY[1] + ((windowHeight - scrollXY[1]) / 2);

    // if the div is going to extend off the RHS, move it back a little
    var divTagCenter = divTagWidth / 2;
    if (divTagCenter + windowCenterX >= windowWidth + 20)
    {
       divTagXPosition = windowCenterX - divTagCenter - 15;
    }
    else
    {
        divTagXPosition = windowCenterX - divTagCenter;
    }
    divTagYPosition = windowCenterY - (divTagHeight / 2);
    
    returnValues[1] = divTagXPosition;
    returnValues[2] = divTagYPosition;
    
    return (returnValues);
}
