var suppressPopup = false;
var nameOfValidationGroup = "";

function showLoadingPopupWithTimeout(text, millisecondsToTimeout)
{
   var loadingPopupID = 'loadingPopup';
   showDivWithTimeout(loadingPopupID, text, millisecondsToTimeout);
}

function showDiv(divId)
{
   showDiv(divId, null);
}

function showDivWithTimeout(divId, text, millisecondsToTimeout)
{
   window.setTimeout("showDiv('" + divId + "','" + text + "')", millisecondsToTimeout);
}

function setSuppressPopup(suppress)
{
   suppressPopup = suppress;
}

function pageValidationResult()
{
   //this method was introduced as we do not want to show "loading..." when a validation of the page 
   //failes. The use of this method in showDiv(..,..) will prevent the popup from showing.
   //It will not do anything if the nameOfValidationGroup is not set in the control/page using this.
   if (typeof(Page_ClientValidate) == 'function')
   {
      if (Page_ClientValidate(nameOfValidationGroup) == false) 
      {
         return false;
      }
   }
   return true;
}

function showDiv(divId, text)
{

   if (suppressPopup || !pageValidationResult())
   {
      return;
   }
   
   var div = document.getElementById(divId);
   
   if (div)
   {
      div.style.display = "block";
      div.style.position = "absolute";
      
      var availableHeight = getWindowInnerHeight();      
      var top = (availableHeight - div.offsetHeight) / 2;

      var left = null;
      if (isIE())
      {
         left = (document.body.clientWidth - div.offsetWidth) / 2;
      }
      else
      {
         top = top + "px";
         left = (window.innerWidth - div.offsetWidth) / 2;
         left = left + "px";
      }               
      
      div.style.top = top;
      div.style.left = left;

      div.style.visibility = "visible";
      
      if (text)
      {
         div.innerHTML = text;
      }
      
      HideOverlappingSelects(div);
   }   
}

function moveDivToCursorPosition(divId, currentEvent)
{
   if (suppressPopup)
   {
      return;
   }
   
   var div = document.getElementById(divId);
   
   if (div)
   {
      var currentPosition = getPosition(currentEvent);
      
      var elementDimensions = getElementDimensions(div);
      
      var newTop = currentPosition.y - elementDimensions.height;
      newTop += "px";
      
      var newLeft = currentPosition.x - elementDimensions.width;
      newLeft += "px";
      
      div.style.top = newTop;
      div.style.left = newLeft;      
   }
}

function getPosition(currentEvent) {
    
    var cursor = {x:0, y:0};
    
    if (currentEvent.pageX || currentEvent.pageY) 
    {
        cursor.x = currentEvent.pageX;
        cursor.y = currentEvent.pageY;        
    } 
    else 
    {
        var documentElement = document.documentElement;
        var theBody = document.body;
        cursor.x = currentEvent.clientX + (documentElement.scrollLeft || theBody.scrollLeft) - (documentElement.clientLeft || 0);
        cursor.y = currentEvent.clientY + (documentElement.scrollTop || theBody.scrollTop) - (documentElement.clientTop || 0);
    }
    
    return cursor;
}

function HideOverlappingSelects(div)
{
   if(isIE())
   {
      if(div)
      {  
         var divTop;
         var divBottom;
         var divLeft;
         var divRight;
         
        var readScroll;
         if((window.document.compatMode)&& (window.document.compatMode == 'CSS1Compat'))
         {
            readScroll = window.document.documentElement;
         }
         else
         {
            readScroll = window.document.body;
         }
         
         divTop = div.offsetTop;
         divTop -= (readScroll.scrollTop - readScroll.clientTop);
         divBottom = divTop + div.offsetHeight;
         divLeft = div.offsetLeft;
         divLeft -= (readScroll.scrollLeft - readScroll.clientLeft);
         divRight = divLeft + div.offsetWidth;
         
         var selectBoxes;
         
         selectBoxes = document.getElementsByTagName("select");
         
         for(var i = 0; i < selectBoxes.length; i++) 
         {
            var selectBox; 
            var elementDimensions;
            var selectTop;
            var selectBottom;
            var selectLeft;
            var selectRight;
            
            selectBox = selectBoxes[i];
            
            elementDimensions = getElementDimensions(selectBox);
            
            selectTop = elementDimensions.top;
            selectLeft = elementDimensions.left;
            selectBottom = selectTop + elementDimensions.height;
            selectRight = selectLeft + elementDimensions.width;
            
            //test if something is overlapping:
            if(divTop < selectBottom
                  && divBottom > selectTop
                  && divLeft < selectRight 
                  && divRight > selectLeft)
            {
               selectBox.style.visibility = "hidden";
            }
         }
      }
   }
}
   
function getElementDimensions(control){
    var yOffset = control.offsetTop||0;
    var xOffset = control.offsetLeft||0;
    var height = control.offsetHeight||0;
    var width = control.offsetWidth||0;
    var theParent = control.offsetParent;
    while(theParent){
        yOffset += theParent.offsetTop||0;
        xOffset += theParent.offsetLeft||0;
        theParent = theParent.offsetParent;
    }
    var readScroll;
    if((window.document.compatMode)&&
      (window.document.compatMode == 'CSS1Compat')){
        readScroll = window.document.documentElement;
    }else{
        readScroll = window.document.body;
    }
    xOffset -= (readScroll.scrollLeft - readScroll.clientLeft);
    yOffset -= (readScroll.scrollTop - readScroll.clientTop);
    return {left:xOffset,top:yOffset,width:width,height:height};
} 


function hideLoadingPopup()
{
   var loadingPopupID = 'loadingPopup';
   hideControl(loadingPopupID);
}

function hideControl(controlId)
{
   var ctrl = document.getElementById(controlId);
   if (ctrl && ctrl.style)
   {
      ctrl.style.visibility = "hidden";
   }
}

function setMainTableHeight()
{
   var mainTable = document.getElementById("mainTable");
   
   var availableHeight = getAvailableMainTableHeight();
   
   mainTable.style.height = "100%";
   
   if (mainTable.offsetHeight < availableHeight)
   {
      mainTable.style.height = availableHeight + "px";
   }
}

function getAvailableMainTableHeight()
{
   var mainTable = document.getElementById("mainTable");
   
   var availableHeight = getWindowInnerHeight() - mainTable.offsetTop;
   if (isIE())
   {
      availableHeight -= 8;
   }
   
   return availableHeight;
}

function getWindowInnerHeight()
{
   if (window.innerHeight)
   {
      return window.innerHeight;
   }
   else if (document.documentElement.clientHeight)
   {
      return document.documentElement.clientHeight;
   }
   
   return document.documentElement.clientHeight;
}

function adjustListWidth(listContainerID, preferredWidth)
{   
   var listContainer = document.getElementById(listContainerID);
   
   if (!listContainer)
   {
      return;
   }
   
   var additionalWidth = 0;
   
   if (listContainer.style.height) 
   {      
      if (isFirefox2Up())
      {
         additionalWidth = 18;
      }
      else
      {
         additionalWidth = 20;
      }
   }
   
   listContainer.style.width = preferredWidth + additionalWidth + "px";
}

function adjustListWidth(listContainerID, listID, preferredWidth)
{
   var listContainer = document.getElementById(listContainerID);
   var list = document.getElementById(listID);
   
   if (!listContainer || !list)
   {
      return;
   }
   
   var additionalWidth = 0;
   
   var availableHeight = getAvailableMainTableHeight();
   
//   alert('availableHeight: ' + availableHeight);
//   alert('listContainer.style.height: ' + listContainer.style.height);
//   alert('listContainer.offsetHeight: ' + listContainer.offsetHeight);
//   alert('list.style.height: ' + list.style.height);
//   alert('list.offsetHeight: ' + list.offsetHeight);
//   alert('list.clientHeight: ' + list.clientHeight);
   listContainer.style.height = (availableHeight - 300) + "px";

   if (listContainer.style.height) 
   {      
      if (isFirefox2Up())
      {
         additionalWidth = 18;
      }
      else
      {
         additionalWidth = 20;
      }
   }
   
   listContainer.style.width = preferredWidth + additionalWidth + "px";
   
//   alert('listContainer.style.height: ' + listContainer.style.height);
//   alert('listContainer.offsetHeight: ' + listContainer.offsetHeight);
//   alert('listContainer.clientHeight: ' + listContainer.clientHeight);
//   alert('list.style.height: ' + list.style.height);
//   alert('list.offsetHeight: ' + list.offsetHeight);
//   alert('list.clientHeight: ' + list.clientHeight);
}

function DeselectHeaderCheckBox(headerCheckBoxId)
{
//method is used for pages containing the sortable header control with chekbox. If someone manupulates the data in the 
//list below the header, this method makes sure the header checkbox is unchecked.
   var element;
   element = document.all[headerCheckBoxId];
   if(element)
   {
      element.checked= false;
   }
}

function ConfirmGenerateNewPassword(showMessage)
{
    if(showMessage)
    {
       var message;
       message = 'This will generate a new password for the selected user.';   
       return confirm(message);
    }
}

