function GetBrowserWindow() {
   var oWindow = GetRadWindow();
   if (oWindow == null)
      oWindow = window;
   else
      oWindow = oWindow.BrowserWindow;

   return oWindow;
}
function GetRadWindowByName(windowName) {
   var oWnd = null;
   var oWindow = GetRadWindow();
   if (oWindow != null) {
      var oManager = oWindow.get_windowManager();
      if (oManager != null)
         oWnd = oManager.GetWindowByName(windowName);
   }
   return oWnd;
}
function GetRadWindow() {
   var oWindow = null;
   if (window.radWindow)
      oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog   
   else if (window.frameElement != null && window.frameElement.radWindow)
      oWindow = window.frameElement.radWindow; //IE (and Moz az well)   

   return oWindow;
}
function SizeToFit() {
   window.setTimeout(
		function () {
		   var oWindow = GetRadWindow();
		   if (oWindow != null) {
		      var oHidden = document.getElementById('<%= hiddenFieldResizeWindowOccured.ClientID %>');
		      //once the RadWindow's size is changed, it will not be centered on the page anymore
		      //call the Center() method to center it again.
		      if (!oWindow.isClosed() && oHidden.value != "1") {
		         oHidden.value = "1";
		         oWindow.set_width(document.body.scrollWidth + 14);
		         oWindow.set_height(document.body.scrollHeight + 80);
		         oWindow.center();
		      }
		   }
		}, 100);
}



//window.onload = SizeToFit;   


// Leave windowNameToRefresh blank if no window (refreshes top level)
function CloseWindowAndRefreshByWindowName(windowNameToRefresh, refreshArgs, refreshCmd) {
   var oWindow = GetRadWindow();

   if (oWindow != null) {
      oWindow.close();

      var oWinToRefresh = GetRadWindowByName(windowNameToRefresh);

      // if rad window exists, call js function: RefreshData
      if (oWinToRefresh != null) {
         var contentFrame = oWinToRefresh.GetContentFrame();
         if (contentFrame != null) {
            contentFrame.contentWindow.RefreshData(refreshArgs, refreshCmd);
         }
      }
      else {
         // Rad window was not found, so assume parent browswer window (top level) has the RefreshData function
         oWindow.BrowserWindow.RefreshData(refreshArgs, refreshCmd);
      }
   }
}

function OpenWindow(pageToOpen, windowName, width, height) {
   var oWindow = GetBrowserWindow();
   var oRadWin = GetRadWindow();

   if (oWindow != null) {
      // We are in a rad window, so pass current rad window name along the url
      if (oRadWin != null) {
         if (pageToOpen.indexOf("?") > 0)
            pageToOpen = new String(pageToOpen + "&prwname=" + oRadWin.Name)
         else
            pageToOpen = new String(pageToOpen + "?prwname=" + oRadWin.Name)
      }

      var oWnd = oWindow.radopen(pageToOpen, null);
      if (oWnd != null) {
         oWnd.Name = windowName;
         //oWnd.GetStatusbar().parentNode.style.display='none';
         oWnd.setSize(width, height);
         oWnd.center();

         DisableBrowserScrolling();
         oWnd.add_close(OnClientClose);
      }
   }
   return false;
}
function RefreshBrowser() {
   //location.reload(true);
   window.location.href = window.location.href;
}

function CloseWindow() {
   var oWindow = GetRadWindow();
   if (oWindow != null) {
      oWindow.close();
   }
}
var nWindowCnt = 0;

function DisableBrowserScrolling() {
   var oWin = GetBrowserWindow();
   if (oWin != null) {
      if (oWin.document.all) {
         oWin.document.body.scroll = "no";
      }
      else {
         var oTop = oWin.document.body.scrollTop;
         oWin.document.body.style.overflow = "hidden";
         oWin.document.body.scrollTop = oTop;
         oWin.document.body.scroll = "no";
      }

      nWindowCnt = nWindowCnt + 1
   }
}
function EnableBrowserScrolling() {
   var oWin = GetBrowserWindow();
   if (oWin != null) {
      nWindowCnt = nWindowCnt - 1;

      if (nWindowCnt < 0)
         nWindowCnt = 0;

      if (nWindowCnt == 0) {
         document.body.scroll = "";
         document.body.style.overflow = "";
      }
   }
}
function OnClientClose(radWindow) {
   EnableBrowserScrolling();
}

function DetermineWindowHeight(maxHeight) {
   var myWidth = 0, myHeight = 0;
   if (typeof (window.innerWidth) == 'number') {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
   } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
   } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
   }

   // Keep 20 pixels for padding
   myHeight = myHeight - 20;

   if (maxHeight != null && maxHeight != "" && maxHeight > 0 && myHeight > maxHeight) {
      myHeight = maxHeight;
   }

   if (myHeight < 50)
      myHeight = 50;

   return myHeight;
}
function DetermineWindowWidth(maxWidth) {
   var myWidth = 0, myHeight = 0;
   if (typeof (window.innerWidth) == 'number') {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
   } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
   } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
   }

   // Keep 20 pixels for padding
   myWidth = myWidth - 20;

   if (maxWidth != null && maxWidth != "" && maxWidth > 0 && myWidth > maxWidth) {
      myWidth = maxWidth;
   }

   return myWidth;
}
