//-------------------------------------------------------------------------------------------------
// NewWin: Opens a window
//-------------------------------------------------------------------------------------------------

// -------------------------------------------------------------------------------------------------
// Author       : Hendrik Eppinga
// Latest update: 09.01.2001, Halden
// -------------------------------------------------------------------------------------------------

// Variable for keeping track of those windows that are open, and for making sure that sending the
// focus to the window works properly. 

// In addition, if one opens a window and changes the location of the window by pressing a link 
// then this script/variable makes sure that the latest location of the window is used (unless the 
// window) is closed.

// If nLeft is empty then the window will be placed in the middle (horizontally) or at the left if 
// the window width is too large.
// If nTop is empty then the window will be placed in the middle (vertically ) or at the top if the 
// window height is too large.  

var Windows = new Array()

function NewWin(sUrl, sName, sScrollbars, sToolbar, nWidth, nHeight, nLeft, nTop)
{
// This function opens a window, using the parameters that are sent in.

   nScreenWidth  = screen.width
   nScreenHeight = screen.height

   // If nWidth is empty then the window gets its maximum width
   if ( nWidth == '' && nWidth != '0') nWidth = nScreenWidth - 20

   // If nHeight then the window gets its maximum height
   if ( nHeight == '' && nHeight != '0') nHeight = nScreenHeight - 60

   // If the nLeft value is empty then the window will be placed in the middle
   if ( nLeft == '' && nLeft != '0')
     {
       if ( nWidth > nScreenWidth ) 
         {
          nLeft = 0
         }
       else
         { 
          nLeft = (( nScreenWidth - nWidth ) - (( nScreenWidth - nWidth ) % 2 )) / 2 
         }
     }

   // If nHeight is empty then the window will be placed in the middle. The window will be placed at 
   // the top of the screen if the height is larger then the available screen height.
   // 
   if ( nTop == '' && nTop != '0' ) 
     {
      if ( nHeight > nScreenHeight )
        { 
         nTop = 0 
        }
      else
        {
         nTop = (( nScreenHeight - nHeight ) - (( nScreenHeight - nHeight ) % 2 )) / 2 
        }
     }


   // The properties of the window.
   sWinFeat = 'scrollbars=\''+sScrollbars+'\',toolbar=\''+sToolbar+'\',width='+nWidth+',height='+nHeight+',top='+nTop+',left='+nLeft

   // Checking if the window has been created.
   if (Windows[sName]) 
      {  

        // The window has been created, check if it has been closed.
        if (Windows[sName].closed )
          {
           // The window has been created and has been closed.           
           Windows[sName] = window.open(sUrl,sName,sWinFeat)

          } 
          
        else

          {
           // The window has been created and has not been closed.                     
    
           // If both the new and the old URL-address are the same (not taken in account the 
           // params) then only the location should be changed, this for making sure that the 
           // location gets updated correctly.


           // Skip the params in the new location. 
           sLocNew = ( sUrl.indexOf('?') > -1 ) ? sUrl.substring(0, sUrl.indexOf('?')) : sUrl

        
           // Skip the params in the old location. This one does contain the protocol also. 
           sLocOld = Windows[sName].location + ''
           sLocOld = ( sLocOld.indexOf('?') > -1 ) ? sLocOld.substring(0, sLocOld.indexOf('?')) : sLocOld

           // The new url can contain the protocol and the path, in that case both the addresses can be 
           // compared with each other without any problems.
           if ( sUrl.substring(0,5) == 'http:' || sUrl.substring(0,6) == 'https:' )           
             {

              if ( sLocOld == sLocNew ) 
                 {
                 
                  Windows[sName].location = sUrl
                  
                 }
             }
             
             
           else
             { 
             
              // The new location contains so far only the filename (this the Url that is send in). 
              // A number of character, equalling the number of character in the new Url address, is removed from 
              // the old address (at the end) and the new address is added. 

              sLocNew = sLocOld.substring(0, sLocOld.length-sLocNew.length) + sLocNew

              if ( sLocOld == sLocNew ) 
                {

                 Windows[sName].location = sUrl

                }
             }
             
          }   

      }
          
   else 
   
      {   
  
       // The window has not been created, and can be opened.
       Windows[sName] = window.open(sUrl,sName,sWinFeat)
   
      }
     

   // If the url-address starts with http or https and a IE 4.0 browser is used then there should be no focus.
   if ( sUrl.substring(0,5) == 'http:' || sUrl.substring(0,6) == 'https:' )
      { 
        nIndex = navigator.appVersion.indexOf('MSIE')
        if (navigator.appVersion.substring(nIndex+5,nIndex+8) == '4.0') return
      }     


   // Give the window the focus.
   Windows[sName].focus() 
}






 
