// ArwScreenCenterPoupupLib.js
// Version: Arw.Common.Js.Version.1.0.0.0
// Author Sig: ps,rm
//
// Class defines functions that can be used to create JavaScript Windows
// that position themselves in the center of the users screen.
/*
 * Sample Use: *
 - myDialog = new ArwJsDialogWindow(); // create object
 - myDialog.arwShowDialogWindow('/pages/showSysMsg','sysmsg',250,160,'no');	//initialize values
*/
// Function:
// Constructor for ArwJsDialogWindow class.

function ArwJsDialogWindow() {
    this.USER_SCREEN_WIDTH    = screen.width;
    this.USER_SCREEN_HEIGHT   = screen.height;
    this.arwShowDialogWindow = arwShowDialogWindow;
    this.arwCloseDialogWindow = arwCloseDialogWindow;
    this.arwDialogWindow = new Object();
   return;
}

// Function:
// Function to show a dialog to the screen. Function takes URL, name of the
// window, width of the window, height of the window and scrollable property
// as arguments.
// pUrl -> URL to load into the dialog window
// pName -> Use this name to assign the dialog windows name
// pWidth -> Width of the dialog window
// pHeight -> Height of the dialog window
// pCanScroll -> Scrollable property (should be either 'yes' or 'no'
function arwShowDialogWindow( pUrl, pName, pWidth, pHeight, pCanScroll )
{
      // Validating the input from the developer. We are showing an alert
      // here because this should be caught at code development stage
      // and the developer is notified of the error. We could do a little
      // bit more validation like type checking but we will leave the developer
      // to come and see the documentation.

      if( arwShowDialogWindow.arguments.length != 5 )
	  {
         alert("Error reported from [ArwJsDialogWindow] \nWrong number of "+
               "arguments passed to the function! "+
               "\nThe function call needs 5 arguments. Please refer to code "+
               "\ndocumentation for more information");
         return false;
      }

      // We find out the center location depending upon the users
      // resolution.
      var dialogLeftLocation = ( this.USER_SCREEN_WIDTH - pWidth ) / 2;
      var dialogTopLocation = ( this.USER_SCREEN_HEIGHT - pHeight ) / 2;
      var centerX = ( .5 * this.USER_SCREEN_WIDTH ) - ( .5 * pWidth );
      var centerY = ( .5 * this.USER_SCREEN_HEIGHT ) - ( .5 * pHeight );
      
	  // Create the default properties along with the calculated properties
      // for the window object.
	  var dialogProperties = 'toolbar=no, scrolling='+pCanScroll+', status=no, menubar=no, scrollbars='+pCanScroll+', resizeable=no, left='+dialogLeftLocation+',top='+dialogTopLocation+',width='+pWidth+',height='+pHeight+',screenX='+centerX+',top='+centerY+',screenY='+centerY;
      this.arwDialogWindow.win = window.open( pUrl, pName, dialogProperties );
	  
      this.arwDialogWindow.onerror = doNothing;
      if(parseInt(navigator.appVersion) >=4 ) {
          // Attempt to get the focus. 
		  	if(this.arwDialogWindow.win)
		 	{/* if the window has opened, set focus to it. */
		  		this.arwDialogWindow.win.focus();
			}
      }
   return;
}

// Function:
// Function to close the Dialog Window. This function does not take any arguments
// and will attempt to close the previous window object reference.
function arwCloseDialogWindow()
{     
	  // If the object is not null and is not closed we will attempt
      // to close the window.
	  if( this.arwDialogWindow.win && !this.arwDialogWindow.win.closed )
	  {
         this.arwDialogWindow.win.close();
         //this.arwDialogWindow = "";
      }
	  else
	  {
         // this section is not needed but is a workaround
         // for an IE bug.
         this.arwDialogWindow.win.close();
      }
   return;
}

// Function:
// Error handler. Do not report anything to the user.
function doNothing()
{ return true; }