// smallWin.js  15Apr00:
// Copyright (c) 2000 Sundown Software Systems, Inc. All Rights Reserved

// Open an auxillary window.  A window is assigned to a file.  Jumping into the same file
// will stay in the same window.  Opening a window to another file, opens a new window.
// Usine the same window for the same file is desirable in case the user has move it 
// on the screen.
// This is true for JavaScript only.  Jscipt (MS) will toss a new window every time so the
// old one has to be knocked down.

// Assumes helpDoc variable defined in HTML doc with path to help file.
var auxWin = null;
var helpWin = null;
var results = new Array();		// Multiple result windows are remembered here.
var _summary = null;			// Summary window
var _scenario = null;		// Scenario menu

// Open other window types, same format.
function openWin( url, name ) 
{
   if( helpWin != null )
   {
      if( !helpWin.closed  )
	      helpWin.close();
      helpWin = null;
   }
   if( auxWin != null && !auxWin.closed )
      auxWin.close();
   auxWin = window.open( url, name, 'width=775,height=300,resizable=yes,scrollbars=yes' );
   if( navigator.appName.indexOf( "Microsoft" ) == -1 )
      auxWin.focus();
}
// Help auxillary window
function openHelp( url ) 
{			// Take down larger window to be replaced with help window
   if( auxWin != null )
   {
      if( !auxWin.closed  )
         auxWin.close();
      auxWin = null;
   }
   
   if( helpWin != null && !helpWin.closed && navigator.appName.indexOf( "Microsoft" ) != -1 )
      helpWin.close();
   
   if( helpDoc != null )	// Make sure there is help file defined in HTML doc.
   {  
	helpWin = window.open( helpDoc + '#' + url, 'Help', 
                             'width=675,height=300,resizable=yes,scrollbars=yes' );
      if( navigator.appName.indexOf( "Microsoft" ) == -1 )
         helpWin.focus();
   }
   else
      window.status = 'No parmHelpDoc path to help file assigned';
}

// Called from the <BODY> html statement of all windows calling small windows.
function handleUNLOAD()
{	// If the help window is up, take it down.
   if( auxWin != null && !auxWin.closed )
      auxWin.close();
   if( helpWin != null && !helpWin.closed )
      helpWin.close();
   for( var i = 0; i < results.length; i++ )    // Close all results windows.
      if( results[i] != null && !results[i].closed )
          results[i].close();
   if( _summary != null && !_summary.closed )
      _summary.close();
   if( _scenario != null && !_scenario.closed )
      _scenario.close();
   return true;   
}

// makeWin:  Called from saveORParms in orpFormN.saveORPParms, create a unique
// name of for the results window and open a dummy window for Perl script
// to fill.  Remember the dummy window so that we can take it down when
// orpPform goes down.
function makeWin()
{
   debugLog( 'smallWins.makeWin entered document.domain = ' + document.domain);
   var n = results.length;
 //  var name = "Results_" + n;
   var name = "secondPage";            // Constant required for Monte Carlo

   if( n > 0 && !results[n-1].closed )		// Put earlier results window on top of form
      results[n-1].focus();				// So that user will know they are still around
// Offset the new window based on which window
   var args = 'width=600,height=575,resizable=yes,scrollbars=yes,status=yes,menubar=yes,';
   var screen = window.screen;
   var x = n%2 == 0 ? 0 : screen.availWidth-550;
   var y = n%4 > 1 ? screen.availHeight-575 : 0;
   if( navigator.appName.indexOf( "Microsoft" ) == -1 )		// Microsoft not in browser name
      args += 'screenY=' + y + ',screenX=' + x;		// Netscape
   else
      args += 'top=' + y + ',left=' + x;			// IE
  
// Put up results window with "Orp computing" message.
/*   if( navigator.appName.indexOf( "Microsoft" ) == -1 )
   {                                                         // Netscape
     debugLog( 'smallWins.makeWin:  not MS, before window.open, name = ' + name );
   try {
     results[n] = window.open( '', name, args ); 
     results[n].document.open(); 
     results[n].document.write( '<HTML><HEAD><TITLE>ORP</TITLE>' ); //<script language="JavaScript" >\n' ); 
  //   results[n].document.write( "<!-- \ndocument.domain='i-orp.com'\;\n//  -->\n</script>\n" );
     results[n].document.write( "<!-- window.onError = function( msg, msgUrl, lineNo )" );
     results[n].document.write( "{ alert( msg + '  ' + msgurl + '  ' + lineno ); return false; }\n" );   
     results[n].document.write( "//  -->\n</script>\n</HEAD>\n" );
     results[n].document.write( '<BODY><H1 ALIGN="CENTER"><FONT COLOR="#800000">\n' );
     results[n].document.write( ' ORP is Computing...</FONT></H1></BODY></HTML>\n' );
     results[n].document.close(); 
     results[n].focus();
   } catch( exf ) {
     window.alert( "smallWins.makewin:  " + exf );
   } 
   else		// Internet Explorer:  Sometimes relative indexing gets lost.
 */
   { 
   
       debugLog( 'smallwins.makeWin:  MS browser, name = ' + name );
       try{
           results[n] = window.open( 'orpResults.html', name, args ); 
       } catch( ex ) {
         window.alert( ex );
         debugLog( 'smallWin.makewin exception for MS browser:  ' + ex );
       }

   }
   debugLog( 'smallWins.makeWin exited, name = ' + name  );
   return name;      // Returning name or window for forms target.
}

// debugLog:  23July00:  Open auxillary window and dump javaScript logging into it.
// See JavaScript, O'Reilly, page  266.
var debugConsole = null;
var _logging = false;		// Set false to suppress debugging window.
function debugLog( msg )
{
   if( !_logging )		// Test for logging supperssion
      return;
/*   if( navigator.appName.indexOf( "Microsoft" ) != -1 )
   {             // MS IE won't let your write after opening a window.
      alert( msg );
      return;
   } */
//  Open consol window the first time we are called, or after the existing
//  consol window has been closed
   if( !debugConsole ) // || _console.closed )
   {
      try {
         debugConsole = window.open( '', 'debugConsole', 'width=600,height=300,resizeable=yes,scrollbars=yes' );
      } catch( exk ) {
        window.status( 'debugLog console window open failure:  ' + exk + '  ' + msg + '  URL = ' + document.URL );
        debugConsole = null;
        _logging = false;              // Don't come here more than once.
        return;
      }
      
      if( !debugConsole )
      {
         window.status = 'debugLog:  no console window created, URL = ' + document.URL;
         _logging = false;              // Don't come here more than once
         return;
      }
                     // Open a document in the window to display plain text
      try {
         debugConsole.document.open( "text/plain" );
      } catch( exk ) {
         window.status( 'debugConsole  document open failure:  ' + exk +
            '  ' + msg + '  URL = ' + document.URL );
         debugLog = null;
        _logging = false;              // Don't come here more than once.
         return;
      }
      
      try {
         debugConsole.document.writeln( '<HTML><HEAD><TITLE>ORP Debug Log</TITLE></HEAD>\n<BODY>' );
         debugConsole.document.writeln( '<P>Browser ' + navigator.appName + '/'
              +  navigator.appVersion + '  ' + navigator.appName.indexOf( "Microsoft" ) + '<P>' );
      } catch( exk ) {
         window.status( 'debugConsole Same-orgin policy prevents write access:  ' + exk +
                  '  ' + msg + '  URL = ' + document.URL );
         debugConsole = null;
         _logging = false;              // Don't come here more than once.
         return;
      }

   }   
   window.status = msg;
   debugConsole.document.writeln( msg + "<BR>" );
}             // ### debugLog


// onError( msg, url, lineNo ):  Event handler for an error.
// Put up a dialog box and record error message.  
// Then let JavaScript process the error in its own fashion.
//
window.onerror = function( msg, url, lineNo )
{
   alert( 'onError in ' + url + '(' + lineNo + '):  ' + msg );  
   if( _logging ){

      debugLog( 'onError in ' + url + '(' + lineNo + '):  ' + msg );
   }
   return false;
}                        // ### onError

//       -- Constructor Activities --
window.defaultStatus = window.status = 'Optimal Retirement Planner';
//  ### smallWins -->


