// Driver FUNCTION to CALCULATE a named/composite object SED model SED FLUX
//      DENSITY, color correction, and quoted flux density:

function calc_flux_named(object_name, normfac_named, wavelength_named, z) 
{
   var i, j, dlamb, lamb_mum, lamb_m, r, dnu_Hz;
   var thetop, thebottom, nu, dnu, lamb;
   var c      = 3.0e8, h, k;
   var Nbands = 7;

    // Set response function:

    var R = set_response();

    // Set the average wavelengths (for use with color correction):

    lambda0 = new Array(7);
    lambda0_mum = new Array(7);
        
    lambda0_mum[0] = 3.55;
    lambda0_mum[1] = 4.483;
    lambda0_mum[2] = 5.731;
    lambda0_mum[3] = 7.872;

    lambda0[0] = 3.55 * 1.0e-6;
    lambda0[1] = 4.483 * 1.0e-6;
    lambda0[2] = 5.731 * 1.0e-6;
    lambda0[3] = 7.872 * 1.0e-6;

    lambda0_mum[4] = 23.675;
    lambda0_mum[5] = 71.440;
    lambda0_mum[6] = 155.899;

    lambda0[4] = 23.675 * 1.0e-6;
    lambda0[5] = 71.440 * 1.0e-6;
    lambda0[6] = 155.899 * 1.0e-6;

   // Initialize the SED for whatever the user selected:

   switch (object_name){
	  case "Arp 220": 
	     var the_sed = set_arp220_sed();	
          break;
	  case "M82": 
	     var the_sed = set_m82_sed();	
          break;
	  case "M81": 
	     var the_sed = set_m81_sed();	
          break;
	  case "LINER": 
	     var the_sed = set_liner_sed();	
          break;
	  case "Elliptical": 
	     var the_sed = set_elliptical_sed();	
          break;
	  case "Spiral": 
	     var the_sed = set_spiral_sed();	
          break;
	  case "Seyfert 2": 
	     var the_sed = set_seyfert2_sed();	
          break;	  
	  case "Starburst low reddening": 
	     var the_sed = set_starburst_l_sed();	
          break;
	  case "Starburst high reddening": 
	     var the_sed = set_starburst_h_sed();	
          break;
          default: 
               alert("Unknown SED selection.");
          break;
   }

   // Get intrinsic flux, set color correction = 1 (cludge!):

   K     = new Array(Nbands);
   res   = new Array(Nbands);

   // First handle case if norm wavelength is outside of SED predefined
   // range. If so, set fluxes, K = 0 and warn user. 

   var upperl = the_sed[the_sed.length-1][0]   * (1.0 + 1.0*z);
   var lowerl = the_sed[0][0]   * (1.0 + 1.0*z);
   if ( upperl < wavelength_named || wavelength_named < lowerl) {
      msgWindow=window.open("","Messages", "height=500,width=600,scrollbars=yes,menubar");

      msgWindow.document.writeln("<HEAD><TITLE>Confusion/Saturation/SED warning</TITLE></HEAD>");

      msgWindow.document.writeln("<body bgcolor=\"f52887\"><H4>SED warning!</H4>");
      // msgWindow.document.writeln("<pre>");

      msgWindow.document.writeln("<p><b>Warning:</b> the fiducial normalization wavelength is outside of the region for which the object SED has values tabulated in the PET.</p><p>For " + object_name + ", the SED is defined for wavelengths: &nbsp;&nbsp;&nbsp;" + the_sed[0][0] + " - " + the_sed[the_sed.length-1][0] + " microns (z = 0).</p>");	       
      msgWindow.document.writeln("<P><form><INPUT TYPE='button' VALUE='Close' onClick='self.close()'></form>");  
      // msgWindow.document.writeln("</pre>");
      msgWindow.document.writeln("</body>");

      for (j = 0; j < Nbands; j++)
      {
         intrinsic_flux[j] = 0.0;
	 K[j] = 0.0;
	 res[j] = 0.0;
      }

      return res;
   }

   // Norm wavelength OK, so now calculate K, quoted flux, etc.:

   with(Math) {
      for (j = 0; j < Nbands; j++)
      {
         intrinsic_flux[j] = get_intrinsic_flux( the_sed, lambda0_mum[j], z );     
	 K[j] = 1.0;

	 intrinsic_flux[j] = intrinsic_flux[j] * normfac_named /
	              get_intrinsic_flux( the_sed, wavelength_named, z ); 

	 res[j] = K[j] * intrinsic_flux[j];
      }
   }

   // Intrinsic flux, color corr stored in global vars. Return quoted
   //   flux.

   return res;

}

// This function returns the flux density at a given lambda, z for a
//   named/composite object SED. Called by: calc_flux_named() 

function get_intrinsic_flux( the_sed, lambda , z ) 
{ 
  var MYTOL = 0.0001;
  var res;

  with(Math) {

	   for(var i = 0; i < the_sed.length-1; i++)
           {		  
	      var llo = the_sed[i][0]   * (1.0 + 1.0*z);
	      var lhi = the_sed[i+1][0] * (1.0 + 1.0*z);

	      if( abs(llo - lambda) < MYTOL ) {
	         res    = the_sed[i][1];
		 i      = the_sed.length;
	      }
	      else if( abs(lhi - lambda) < MYTOL ) {
	         res    = the_sed[i+1][1];
		 i      = the_sed.length;
              }
	      else if( llo < lambda && lambda < lhi ) { 
	        // Linear interpolation scheme changed to log-lin:
	        // 10.30.2002
		//
	        //res    = the_sed[i][1] + ( lambda - llo ) * 
		//                ( the_sed[i+1][1] - the_sed[i][1] ) / 
		//		 ( lhi - llo ) ;

	        var lslope = ( Math.log(the_sed[i+1][1]) -
			   Math.log(the_sed[i][1]) ) / 
			   ( Math.log(lhi) - Math.log(llo) ) ; 

	        var linter = ( Math.log(the_sed[i][1]) - lslope *
		               Math.log(llo) ) / Math.LN10;

		var log10lambda = Math.log(lambda) / Math.LN10;

	        res    = Math.pow(10.0, lslope * log10lambda + linter);

		i      = the_sed.length;
              }
	      else {
	         continue;
              }
           } 

  }
   
  return res;

}


// THIS FUNCTION RETURNS the normalized Blackbody SED FLUX DENSITY for
//	an input temperature, wavelength, normalization and wavelength
//      scale for the normalization. Called by: calc_flux_bb()

function fnu_bb(Teff, lambda, fnu0, wavelength_bb, alpha) 
{
   with(Math) 
   {
	var k = 1.38066e-16;
	var h = 6.626e-27;  
	var c = 3.0e8;
	var MPI = 3.141592654;

	var nu = c/lambda;
	var nu0 = c / wavelength_bb;
  
	var arg  = h * nu / (k * Teff);
	var arg0 = h * nu0 / (k * Teff);

	var fnu = fnu0 * pow( nu/ nu0, 3.0) * pow( lambda /
	          wavelength_bb , alpha) * 
	          ( exp( arg0 ) - 1.0 ) / ( exp( arg ) - 1.0 );   
   }
 
   return fnu;
}

// This is the main driver function to determine the intrinsic flux density,
//    color correction and quoted flux density for a BLACKBODY.

function calc_flux_bb(Teff, normfac_bb, wavelength_bb) 
{
   var lamb, dlamb, r, nu0, nu, dnu; 
   var c, h, k;
   var i, j;
   var thetop, thebottom;

   // Set defaults:

   Nbands = 7;

   c      = 3.0e8;
   k      = 1.38066e-16; 
   h      = 6.626e-27; 

   // Set response function:

   var R = set_response(); 

   // Set the average wavelengths (for use with color corr):

   lambda0 = new Array(7);

   // Manually set nominal wavelengths for IRAC:

   lambda0[0] = 3.55;
   lambda0[1] = 4.483;
   lambda0[2] = 5.731;
   lambda0[3] = 7.872;

   // Manually set nominal wavelengths for MIPS:

   lambda0[4] = 23.675;
   lambda0[5] = 71.440;
   lambda0[6] = 155.899;

   with(Math) {
     
      // Get intrinsic flux, calculate color correction:

      nu0   = c / ( wavelength_bb * 1.0e-6 );

      denom = new Array(Nbands);
      numer = new Array(Nbands);
      K     = new Array(Nbands);
      res   = new Array(7);

      // First do IRAC:

      for ( j = 0; j < 4; j++ )
      {

         lambda0[j] = lambda0[j] * 1.0e-6;  // units of lambda0 = m

         // Calculate SED intrinsic flux density:

	 intrinsic_flux[j] = fnu_bb(Teff, lambda0[j],
			     normfac_bb, wavelength_bb*1.0e-6, 0.0);

         // Calculate the color correction:

	denom[j] = 0.0;
	numer[j] = 0.0;
	for ( i = 0; i < R[j].length-1; i++ )
	{       
          lamb = 0.5 * ( R[j][i][0] + R[j][i+1][0] ) * 1.0e-6;
	  r    = 0.5 * ( R[j][i][1] + R[j][i+1][1] );

	  dnu  = ( (c / R[j][i][0]) - (c / R[j][i+1][0]) )/ 1.0e-6;

	  denom[j] += Math.pow( lamb / lambda0[j], 2.0 ) * r * dnu;

	  numer[j] += pow(lambda0[j]/ lamb, 3.0-1.0) *
	    (  exp ( h*c / (k * Teff * lambda0[j]) ) - 1.0 ) /
	    (  exp ( h*c / (k * Teff * lamb)  ) - 1.0 ) * r * dnu;       
	}
    
	K[j] = numer[j] / denom[j];

      // Calculate quoted flux density:

         res[j] = K[j] * intrinsic_flux[j];
      }	

      // Now MIPS:

      for ( j = 4; j < Nbands; j++ )
      {

         lambda0[j] = lambda0[j] * 1.0e-6;  // units  = m

         // Calculate SED intrinsic flux density:

	 intrinsic_flux[j] = fnu_bb(Teff, lambda0[j],
			     normfac_bb, wavelength_bb*1.0e-6, 0.0);

         // Calculate the color correction:

	denom[j] = 0.0;
	numer[j] = 0.0;
	for ( i = 0; i < R[j].length-1; i++ )
	{       
          lamb = 0.5 * ( R[j][i][0] + R[j][i+1][0] ) * 1.0e-6;
	  r    = 0.5 * ( R[j][i][1] + R[j][i+1][1] );

	  dlamb = (R[j][i+1][0] - R[j][i][0]) * 1.0e-6;

	  numer[j] += pow(lambda0[j]/ lamb, 5.0) *
	    (  exp ( h*c / (k * Teff * lambda0[j]) ) - 1.0 ) /
	    (  exp ( h*c / (k * Teff * lamb)  ) - 1.0 ) * r * dlamb; 

	  denom[j] += pow(lambda0[j]/ lamb, 5.0) *
	    (  exp ( h*c / (k * 10000.0 * lambda0[j]) ) - 1.0 ) /
	    (  exp ( h*c / (k * 10000.0 * lamb)  ) - 1.0 ) * r * dlamb;       
	}
    
	K[j] = numer[j] / denom[j];

      // Calculate quoted flux density:

         res[j] = K[j] * intrinsic_flux[j];
      }	

    }

   // Intrinsic flux, color corr stored in global vars. Return quoted
   //   flux:

    return res;
}


// This is the main driver function to determine the intrinsic flux density,
//    color correction and quoted flux density for a MODIFIED BLACKBODY.

function calc_flux_bb_mod(Teff, normfac_bb, wavelength_bb, alpha) 
{
    var lamb, r, nu, nu0, dnu, h, k, i, j;
    var thetop, thebottom;
    var Nbands = 7;

    Teff = 1.0*Teff;
    normfac_bb = 1.0*normfac_bb;
    wavelength_bb = 1.0*wavelength_bb;
    alpha = 1.0*alpha;

    var c   = 3.0e8;

    // Set response function:

    var R = set_response();

    // Set average wavelengths:

    lambda0 = new Array(7);
   
    // Manually set nominal wavelengths for IRAC:

    lambda0[0] = 3.55;
    lambda0[1] = 4.483;
    lambda0[2] = 5.731;
    lambda0[3] = 7.872;

    // Manually set nominal wavelengths for MIPS:

    lambda0[4] = 23.675;
    lambda0[5] = 71.440;
    lambda0[6] = 155.899;

    with(Math) {

      k = 1.38066e-16; 
      h = 6.626e-27;  

      // Get intrinsic flux, calculate color correction:

      nu0   = c / ( wavelength_bb * 1.0e-6 );

      denom = new Array(Nbands);
      numer = new Array(Nbands);
      K     = new Array(Nbands);
      res   = new Array(7);
		   
      for ( j = 0; j < 4; j++ )
      {

         lambda0[j] = lambda0[j] * 1.0e-6;
      
         // Calculate SED intrinsic flux density:

	 intrinsic_flux[j] = fnu_bb(Teff, lambda0[j],
			     normfac_bb, wavelength_bb*1.0e-6, alpha);

         // Calculate the color correction:

	denom[j] = 0.0;
	numer[j] = 0.0;
	for ( i = 0; i < R[j].length-1; i++ )
	{       
          lamb = 0.5 * ( R[j][i][0] + R[j][i+1][0] ) * 1.0e-6;
	  r    = 0.5 * ( R[j][i][1] + R[j][i+1][1] );

	  dnu  = ( (c / R[j][i][0]) - (c / R[j][i+1][0]) )/ 1.0e-6;

	  denom[j] += pow( lamb / lambda0[j], 2.0 ) * r * dnu;

	  numer[j] += pow(lambda0[j] / lamb, 3.0 - 1.0) * pow( lamb /
	    lambda0[j], alpha) *
	    (  exp ( h*c / (k * Teff * lambda0[j]) ) - 1.0 ) /
	    (  exp ( h*c / (k * Teff * lamb)  ) - 1.0 ) * r * dnu;
       
	}
    
	K[j] = numer[j] / denom[j];

      // Calculate quoted flux density:

         res[j] = K[j] * intrinsic_flux[j];
      }

      // Now MIPS:

      for ( j = 4; j < Nbands; j++ )
      {

         lambda0[j] = lambda0[j] * 1.0e-6;  // units  = m

         // Calculate SED intrinsic flux density:

	 intrinsic_flux[j] = fnu_bb(Teff, lambda0[j],
			     normfac_bb, wavelength_bb*1.0e-6, alpha);

         // Calculate the color correction:

	denom[j] = 0.0;
	numer[j] = 0.0;
	for ( i = 0; i < R[j].length-1; i++ )
	{       
          lamb = 0.5 * ( R[j][i][0] + R[j][i+1][0] ) * 1.0e-6;
	  r    = 0.5 * ( R[j][i][1] + R[j][i+1][1] );

	  dlamb = (R[j][i+1][0] - R[j][i][0]) * 1.0e-6;

	  numer[j] += pow(lambda0[j]/ lamb, 5.0) * pow( lamb /
	    lambda0[j], alpha) *
	    (  exp ( h*c / (k * Teff * lambda0[j]) ) - 1.0 ) /
	    (  exp ( h*c / (k * Teff * lamb)  ) - 1.0 ) * r * dlamb; 

	  denom[j] += pow(lambda0[j]/ lamb, 5.0) *
	    (  exp ( h*c / (k * 10000.0 * lambda0[j]) ) - 1.0 ) /
	    (  exp ( h*c / (k * 10000.0 * lamb)  ) - 1.0 ) * r * dlamb;       
	}
    
	K[j] = numer[j] / denom[j];

      // Calculate quoted flux density:

         res[j] = K[j] * intrinsic_flux[j];
      }	
	
    }

   // Intrinsic flux, color corr stored in global vars. Return quoted
   //   flux:

    return res;
}


// DRIVER FUNCTION to CALCULATE SED FLUX DENSITY, COLOR CORR AND QUOTED FLUX
//   FOR A POWER LAW SED. RETURNS A 7 ELEMENT ARRAY WITH QUOTED FLUX.

function calc_flux_powerlaw(alpha, normfac_pl, wavelength_pl) 
{
    var nu0, i, j, lamb, dlamb, r, nu, dnu;
    var thetop, thebottom;
    var Nbands = 7;
    var c = 3.0e8, h, k;

    with(Math)
    {
       alpha = 1.0*alpha;
       normfac_pl = 1.0*normfac_pl;
       wavelength_pl = 1.0*wavelength_pl;
    }

    // Set response function:

    var R = set_response();

    // Set the average wavelengths (for use with color correction):

    lambda0 = new Array(Nbands);

    // Manually set nominal wavelengths for IRAC & MIPS:

    lambda0[0] = 3.55;
    lambda0[1] = 4.483;
    lambda0[2] = 5.731;
    lambda0[3] = 7.872;

    lambda0[4] = 23.675;
    lambda0[5] = 71.440;
    lambda0[6] = 155.899;

    // Get intrinsic flux, calculate color correction:

    res   = new Array(Nbands);
    denom = new Array(Nbands);
    numer = new Array(Nbands);
    K     = new Array(Nbands);

    with(Math) {

       for( j = 0; j < 4; j++ )
       {

          // Calculate SED intrinsic flux density:

          intrinsic_flux[j] = normfac_pl * pow( wavelength_pl/lambda0[j], alpha );

          // Calculate the color correction:

	  denom[j] = 0.0;
	  numer[j] = 0.0;
	  for ( i = 0; i < R[j].length-1; i++ )
	  {       
             lamb = 0.5 * ( R[j][i][0] + R[j][i+1][0] );
	     r    = 0.5 * ( R[j][i][1] + R[j][i+1][1] );

	     dnu  = ( (c / R[j][i][0]) - (c / R[j][i+1][0]) );

	     denom[j] += pow( lamb / lambda0[j], 2.0 ) * r * dnu;

	     numer[j] += pow(lambda0[j] / lamb, alpha - 1.0) * r * dnu;       
	  }
    
	  K[j] = numer[j] / denom[j];

          // Calculate quoted flux density:

          res[j] = K[j] * intrinsic_flux[j];

       }

       for( j = 4; j < Nbands; j++ )
       {

          // Calculate SED intrinsic flux density:

          intrinsic_flux[j] = normfac_pl * pow( wavelength_pl/lambda0[j], alpha );

          // Calculate the color correction:

	  k = 1.38066e-16; 
	  h = 6.626e-27;  

	  denom[j] = 0.0;
	  numer[j] = 0.0;
	  for ( i = 0; i < R[j].length-1; i++ )
	  {       
             lamb = 0.5 * ( R[j][i][0] + R[j][i+1][0] ) * 1.0e-6;
	     r    = 0.5 * ( R[j][i][1] + R[j][i+1][1] );

	     dlamb = (R[j][i+1][0] - R[j][i][0]) * 1.0e-6;

	     denom[j] += pow(lambda0[j]* 1.0e-6/ lamb, 5.0) *
	    (  exp ( h*c / (k * 10000.0 * lambda0[j]* 1.0e-6) ) - 1.0 ) /
	    (  exp ( h*c / (k * 10000.0 * lamb)  ) - 1.0 ) * r * dlamb;  

	     numer[j] += pow(lambda0[j]* 1.0e-6 / lamb, 2.0 + alpha) * r * dlamb;       
	  }

	  K[j] = numer[j] / denom[j];

          // Calculate quoted flux density:

          res[j] = K[j] * intrinsic_flux[j];

       }
	
    }

    return res;

}

// THIS FUNCTION RETURNS A 3-ELEMENT ARRAY OF MIPS SCAN SENSITIVITIES:

function MIPS_scan_sensitivity(scanrate, Nrepeat, background_index) 
{

   // NOTE: need to change indexing of t_exp (throughout the tool).

   // 11.15.2004: this module updated with S11 numbers (GKS).
   //
   // Define sensitivities in MIPS passbands:
   //    Entries are for (low, med, high) backgrounds.
   //    Values for 24 microns come from Bill Latter's 
   //         "MIPS_sens[low,med,hi]_4nov04"
   //         excel spreadsheets; also in the ost cvs repository.
   //

   MIPS_scan_sens_24_slow  = new Array(57.87, 68.94, 112.89);
   MIPS_scan_sens_70_slow  = new Array(3249.0, 3833.0, 6335.0);
   MIPS_scan_sens_160_slow = new Array(42872.0, 51018.0, 88745.0);

   MIPS_scan_sens_24_medium  = new Array(113.21, 130.60, 199.86);
   MIPS_scan_sens_70_medium  = new Array(5137.0, 5908.0, 9298.0);
   MIPS_scan_sens_160_medium = new Array(67786.0, 89478.0, 176244.0);

   MIPS_scan_sens_24_fast  = new Array(211.53, 240.60, 358.22);
   MIPS_scan_sens_70_fast  = new Array(8388.0, 9479.0, 14475.0);
   MIPS_scan_sens_160_fast = new Array(78273.0, 108017.0, 221513.0);

   // Define exposure depth per pixel in MIPS passbands:
   // Source: SOM v4.0, Table 8.7, pg. 249.
 

   MIPS_scan_texp_per_pass_24   = new Array(100.0, 40.0, 15.0);
   MIPS_scan_texp_per_pass_70   = new Array(100.0, 40.0, 15.0);
   MIPS_scan_texp_per_pass_160  = new Array(10.0, 4.0, 3.0);

   mytmp = new Array(3);

   switch (scanrate){
	  case "Slow": 
	       mytmp[0] = MIPS_scan_sens_24_slow[background_index];
	       mytmp[1] = MIPS_scan_sens_70_slow[background_index];
	       mytmp[2] = MIPS_scan_sens_160_slow[background_index];
	       t_exp[4] = MIPS_scan_texp_per_pass_24[0] * Nrepeat;
	       t_exp[5] = MIPS_scan_texp_per_pass_70[0] * Nrepeat;
	       t_exp[6] = MIPS_scan_texp_per_pass_160[0]* Nrepeat;
          break;
	  case "Medium":
	       mytmp[0] = MIPS_scan_sens_24_medium[background_index];
	       mytmp[1] = MIPS_scan_sens_70_medium[background_index];
	       mytmp[2] = MIPS_scan_sens_160_medium[background_index];
	       t_exp[4] = MIPS_scan_texp_per_pass_24[1] * Nrepeat;
	       t_exp[5] = MIPS_scan_texp_per_pass_70[1] * Nrepeat;
	       t_exp[6] = MIPS_scan_texp_per_pass_160[1]* Nrepeat;
	  break;
	  case "Fast":
	       mytmp[0] = MIPS_scan_sens_24_fast[background_index];
	       mytmp[1] = MIPS_scan_sens_70_fast[background_index];
	       mytmp[2] = MIPS_scan_sens_160_fast[background_index];
	       t_exp[4] = MIPS_scan_texp_per_pass_24[2] * Nrepeat;
	       t_exp[5] = MIPS_scan_texp_per_pass_70[2] * Nrepeat;
	       t_exp[6] = MIPS_scan_texp_per_pass_160[2]* Nrepeat;
          break;
	  default: 
	       alert("Scan rate out of range in MIPS Scan Map. The EX-PET is confused! Please reload the webpage, or clear the form.");
          break;
   }

   if( Nrepeat <= 0.0)
   {
      alert("The EX-PET is confused! Please clear the form, and/or reload the webpage.");
   }

   mytmp[0] = mytmp[0] / Math.sqrt( Nrepeat );
   mytmp[1] = mytmp[1] / Math.sqrt( Nrepeat );
   mytmp[2] = mytmp[2] / Math.sqrt( Nrepeat );
   

   return mytmp;

}


// THIS FUNCTION RETURNS A 3-ELEMENT ARRAY OF MIPS PHOTO/SUPER-RES SENSITIVITIES:

function MIPS_photo_sensitivity(mips_24_fieldsize, 
	   mips_24_frametime_index, mips_24_Nrepeat,
	   mips_70_pixelscale, mips_70_fieldsize, mips_70_frametime_index, 
	   mips_70_Nrepeat, mips_160_fieldsize, 
	   mips_160_frametime_index, mips_160_Nrepeat, background_index)
{

   // Define exposure depth per pixel (SOM v5.0, Table 8.11, pg. 304):

   MIPS_photo_24_small_texp = new Array(42.0, 140.0, 420.0);
   MIPS_photo_24_large_texp = new Array(30.0, 100.0, 300.0);

   MIPS_photo_160_small_texp = new Array(6.0, 20.0);
   MIPS_photo_160_large_texp = new Array(3.0, 10.0);

   MIPS_photo_70_large_default_texp = new Array(18.0, 60.0);
   MIPS_photo_70_large_fine_texp = new Array(24.0, 80.0);

   MIPS_photo_70_small_default_texp = new Array(30.0, 100.0);
   MIPS_photo_70_small_fine_texp = new Array(24.0, 80.0);


   // FILL IN 24 MICRON EXPOSURE DEPTH PER PIXEL:

   if (mips_24_fieldsize == "small")
   {
      t_exp[4] = MIPS_photo_24_small_texp[mips_24_frametime_index] * mips_24_Nrepeat;
   }		  
   else if (mips_24_fieldsize == "large")
      {
     t_exp[4] = MIPS_photo_24_large_texp[mips_24_frametime_index] * mips_24_Nrepeat;
   }
  else
     alert("0. Field size index out of range for MIPS P/SR 24 microns");

   // FILL IN 70 MICRON EXPOSURE DEPTH PER PIXEL:

   if (mips_70_fieldsize == "small")
   {
      if( mips_70_pixelscale == "default")      
         t_exp[5] = MIPS_photo_70_small_default_texp[mips_70_frametime_index] * mips_70_Nrepeat;
      else if ( mips_70_pixelscale == "fine")
         t_exp[5] = MIPS_photo_70_small_fine_texp[mips_70_frametime_index] * mips_70_Nrepeat;         else
	 alert("-1. Pixel scale out of range in P/SR. PET is confused! Please clear the form and/or reload the webpage.");      
   }		  
   else if (mips_70_fieldsize == "large")
   {
      if( mips_70_pixelscale == "default")      
         t_exp[5] = MIPS_photo_70_large_default_texp[mips_70_frametime_index] * mips_70_Nrepeat;
      else if ( mips_70_pixelscale == "fine")
         t_exp[5] = MIPS_photo_70_large_fine_texp[mips_70_frametime_index] * mips_70_Nrepeat;         else
	 alert("-1. Pixel scale out of range in P/SR. PET is confused! Please clear the form and/or reload the webpage.");      
   }
  else
     alert("0. Field size index out of range for MIPS P/SR 24 microns");

   // FILL IN 160 MICRON EXPOSURE DEPTH PER PIXEL:

   if (mips_160_fieldsize == "small")
   {
      t_exp[6] = MIPS_photo_160_small_texp[mips_160_frametime_index] * mips_160_Nrepeat;
   }		  
   else if (mips_160_fieldsize == "large")
   {
     t_exp[6] = MIPS_photo_160_large_texp[mips_160_frametime_index] * mips_160_Nrepeat;
   }
   else if (mips_160_fieldsize == "enhanced")
   {
      t_exp[6] = MIPS_photo_160_small_texp[mips_160_frametime_index] * mips_160_Nrepeat;
   }
  else
     alert("0. Field size out of range in P/SR. PET is confused! Please clear the form and/or reload the webpage."); 


   // DEFINE SENSITIVITIES IN 24 MICRON BAND:
   //    Values come from Bill Latter's "MIPS_sens[low,med,hi]_jan04"
   //         excel spreadsheets; also in the ost cvs repository.
   // Notation:
   //    ... ... ...  FOV_BKGD(frametimes)

   MIPS_photo_sens_24_small_lo = new Array(126.41, 48.91, 28.06);
   MIPS_photo_sens_24_large_lo = new Array(149.58, 57.87, 33.20);

   MIPS_photo_sens_24_small_medium = new Array(143.79, 58.26, 34.74);
   MIPS_photo_sens_24_large_medium = new Array(170.13, 68.94, 41.11);

   MIPS_photo_sens_24_small_high = new Array(214.08, 95.41, 71.89995);
   MIPS_photo_sens_24_large_high = new Array(253.30, 112.89, 83.08087);

   // DEFINE SENSITIVITIES IN 70 MICRON BAND:

   MIPS_photo_sens_70_small_default_lo = new Array(5931, 3249);
   MIPS_photo_sens_70_small_fine_lo    = new Array(13818, 7991);
 
   MIPS_photo_sens_70_small_default_medium = new Array(6702.4, 3833.3);
   MIPS_photo_sens_70_small_fine_medium    = new Array(16485.8, 9428.6);

   MIPS_photo_sens_70_small_default_high = new Array(10320.7, 6334.7);
   MIPS_photo_sens_70_small_fine_high    = new Array(25385.5, 15581.1);

   MIPS_photo_sens_70_large_default_lo = new Array(7658, 4194);
   MIPS_photo_sens_70_large_fine_lo    = new Array(13818, 7991);

   MIPS_photo_sens_70_large_default_medium = new Array(8652.8, 4948.8);
   MIPS_photo_sens_70_large_fine_medium    = new Array(16485.8,9428.6);

   MIPS_photo_sens_70_large_default_high = new Array(13324.0, 8178.0);
   MIPS_photo_sens_70_large_fine_high    = new Array(25385.5, 15581.1);


   // DEFINE SENSITIVITIES IN 160 MICRON BAND:
 
   MIPS_photo_sens_160_small_lo = new Array(55347.0, 30315.08);
   MIPS_photo_sens_160_large_lo = new Array(78273.0, 42872.0);

   MIPS_photo_sens_160_small_medium = new Array(76379.0, 36075.5);
   MIPS_photo_sens_160_large_medium = new Array(108017.0, 51018.0);

   MIPS_photo_sens_160_small_high = new Array(156633.3, 62752.2); 
   MIPS_photo_sens_160_large_high = new Array(221513, 88745); 

   MIPS_photo_sens_160_enhanced_lo = new Array(55347.0 * 1.22, 30315.08 * 1.22);
   MIPS_photo_sens_160_enhanced_medium = new Array(55347.0 * 1.22, 30315.08 * 1.22);
   MIPS_photo_sens_160_enhanced_high = new Array(55347.0 * 1.22, 30315.08 * 1.22);
   
   mytmp = new Array(3);	 

   switch (background_index){
	  case 0: 

	     // 24 microns:
	       if (mips_24_fieldsize == "small")
	       {
	          mytmp[0] = MIPS_photo_sens_24_small_lo[mips_24_frametime_index];
	       }		  
	       else if (mips_24_fieldsize == "large")
	          mytmp[0] = MIPS_photo_sens_24_large_lo[mips_24_frametime_index];
	       else
		  alert("1. Field size index out of range for MIPS P/SR 24 microns");

	     // 70 microns:
	       if (mips_70_fieldsize == "small")
	       {
	         if (mips_70_pixelscale == "default")
		    mytmp[1] = MIPS_photo_sens_70_small_default_lo[mips_70_frametime_index];
		 else if (mips_70_pixelscale == "fine")
		    mytmp[1] = MIPS_photo_sens_70_small_fine_lo[mips_70_frametime_index];
		 else
		    alert("1. Pixel scale index out of range for MIPS P/HR 70 microns");
	       }	          
	       else if (mips_70_fieldsize == "large")
	       {
	         if (mips_70_pixelscale == "default")
		    mytmp[1] = MIPS_photo_sens_70_large_default_lo[mips_70_frametime_index];
		 else if (mips_70_pixelscale == "fine")
		    mytmp[1] = MIPS_photo_sens_70_large_fine_lo[mips_70_frametime_index];
		 else
		    alert("2. Pixel scale index out of range for MIPS P/SR 70 microns");
	       }
	       else
		    alert("1. Field size index out of range for MIPS P/SR 70 microns");

	     // 160 microns:
	       if (mips_160_fieldsize == "small")
	          mytmp[2] = MIPS_photo_sens_160_small_lo[mips_160_frametime_index];
	       else if (mips_160_fieldsize == "large")
	          mytmp[2] = MIPS_photo_sens_160_large_lo[mips_160_frametime_index];
	       else if (mips_160_fieldsize == "enhanced")
	          mytmp[2] = MIPS_photo_sens_160_enhanced_lo[mips_160_frametime_index];
	       else
		  alert("1. Field size index out of range for MIPS P/SR 160 microns");

	       break;
	  case 1:

	     // 24 microns:
	       if (mips_24_fieldsize == "small")
	          mytmp[0] = MIPS_photo_sens_24_small_medium[mips_24_frametime_index];
	       else if (mips_24_fieldsize == "large")
	          mytmp[0] = MIPS_photo_sens_24_large_medium[mips_24_frametime_index]; 
	       else
		  alert("2. Field size index out of range for MIPS P/SR 24 microns");

	     // 70 microns:
	       if (mips_70_fieldsize == "small")
	       {
	         if (mips_70_pixelscale == "default")
		    mytmp[1] = MIPS_photo_sens_70_small_default_medium[mips_70_frametime_index];
		 else if (mips_70_pixelscale == "fine")
		    mytmp[1] = MIPS_photo_sens_70_small_fine_medium[mips_70_frametime_index];
		 else
		    alert("3. Pixel scale index out of range for MIPS P/HR 70 microns");
	       }	          
	       else if (mips_70_fieldsize == "large")
	       {
	         if (mips_70_pixelscale == "default")
		    mytmp[1] = MIPS_photo_sens_70_large_default_medium[mips_70_frametime_index];
		 else if (mips_70_pixelscale == "fine")
		    mytmp[1] = MIPS_photo_sens_70_large_fine_medium[mips_70_frametime_index];
		 else
		    alert("4. Pixel scale index out of range for MIPS P/SR 70 microns");
	       }
	       else
		    alert("2. Field size index out of range for MIPS P/SR 70 microns");

	     // 160 microns:
	       if (mips_160_fieldsize == "small")
	          mytmp[2] = MIPS_photo_sens_160_small_medium[mips_160_frametime_index];
	       else if (mips_160_fieldsize == "large")
	          mytmp[2] = MIPS_photo_sens_160_large_medium[mips_160_frametime_index];
	       else if (mips_160_fieldsize == "enhanced")
	          mytmp[2] = MIPS_photo_sens_160_enhanced_medium[mips_160_frametime_index];
	       else
		  alert("2. Field size index out of range for MIPS P/SR 160 microns");

	       break;
	  case 2: 
	     // 24 microns:
	       if (mips_24_fieldsize == "small")
	          mytmp[0] = MIPS_photo_sens_24_small_high[mips_24_frametime_index];
	       else if (mips_24_fieldsize == "large")
	          mytmp[0] = MIPS_photo_sens_24_large_high[mips_24_frametime_index];
	       else
		  alert("3. Field size index out of range for MIPS P/SR 24 microns");	

	     // 70 microns:
	       if (mips_70_fieldsize == "small")
	       {
	         if (mips_70_pixelscale == "default")
		    mytmp[1] = MIPS_photo_sens_70_small_default_high[mips_70_frametime_index];
		 else if (mips_70_pixelscale == "fine")
		    mytmp[1] = MIPS_photo_sens_70_small_fine_high[mips_70_frametime_index];
		 else
		    alert("5. Pixel scale index out of range for MIPS P/HR 70 microns");
	       }	          
	       else if (mips_70_fieldsize == "large")
	       {
	         if (mips_70_pixelscale == "default")
		    mytmp[1] = MIPS_photo_sens_70_large_default_high[mips_70_frametime_index];
		 else if (mips_70_pixelscale == "fine")
		    mytmp[1] = MIPS_photo_sens_70_large_fine_high[mips_70_frametime_index];
		 else
		    alert("6. Pixel scale index out of range for MIPS P/SR 70 microns");
	       }
	       else
		    alert("7. Field size index out of range for MIPS P/SR 70 microns");

	     // 160 microns:
	       if (mips_160_fieldsize == "small")
	          mytmp[2] = MIPS_photo_sens_160_small_high[mips_160_frametime_index];
	       else if (mips_160_fieldsize == "large")
	          mytmp[2] = MIPS_photo_sens_160_large_high[mips_160_frametime_index];
	       else if (mips_160_fieldsize == "enhanced")
	          mytmp[2] = MIPS_photo_sens_160_enhanced_high[mips_160_frametime_index];
	       else
		  alert("3. Field size index out of range for MIPS P/SR 160 microns");

          break;
	  default : 
	       alert("Background index out of range in MIPS P/SR");
   }

  if(mips_24_Nrepeat != 0.0)
     mytmp[0] = mytmp[0] / Math.sqrt(mips_24_Nrepeat);
  if(mips_70_Nrepeat != 0.0)
     mytmp[1] = mytmp[1] / Math.sqrt(mips_70_Nrepeat);
  if(mips_160_Nrepeat != 0.0)
     mytmp[2] = mytmp[2] / Math.sqrt(mips_160_Nrepeat);

 return mytmp;

}

// THIS FUNCTION RETURNS A 4-ELEMENT ARRAY OF IRAC sensitivities:

function IRACsensFull(frameTimeIndex, Nrepeat, bgindex, frametime, mode)
{

 // Source: SOM v7.0, Figures 6.20, 6.21, and 6.22 (or Tables 6.9-11)
 if ( mode == "Full" )
 {
     sensOneLow1 = new Array(360,180,32,3.3,1.4,0.60);
     sensOneLow2 = new Array(430,210,38,4.8,2.4,1.2);
     sensOneLow3 = new Array(1260,630,150,27,16,8.0);
     sensOneLow4 = new Array(450,250,92,29,18,9.8);

     sensOneMed1 = new Array(360,180,32,3.6,1.6,0.73);
     sensOneMed2 = new Array(430,210,38,5.3,2.8,1.4);
     sensOneMed3 = new Array(1260,640,150,31,18,9.3);
     sensOneMed4 = new Array(460,260,110,34,21,12);

     sensOneHigh1 = new Array(360,180,34,4.8,2.5,1.3);
     sensOneHigh2 = new Array(430,220,41,7.1,4.1,2.1);
     sensOneHigh3 = new Array(1280,660,180,44,27,14);
     sensOneHigh4 = new Array(540,330,160,51,32,18);
  }
  else if ( mode == "Subarray" )
  {
     sensOneLow1 = new Array(7300.00, 485.00, 84.00);
     sensOneLow2 = new Array(8600.00, 550.00, 89.00);
     sensOneLow3 = new Array(25000.00, 2010.0, 609.00);
     sensOneLow4 = new Array(8100.00, 690.00, 225.00);

     sensOneMed1 = new Array(7300.00, 490.00, 82.00);
     sensOneMed2 = new Array(8600.00, 550.00, 89.00);
     sensOneMed3 = new Array(25000.00, 2020.0, 610.00);
     sensOneMed4 = new Array(8100.00, 720.00, 250.00);

     sensOneHigh1 = new Array(7300.00, 490.00, 84.00);
     sensOneHigh2 = new Array(8600.00, 560.00, 93.00);
     sensOneHigh3 = new Array(25000.00, 2100.0, 650.00);
     sensOneHigh4 = new Array(8200.00, 860.00, 340.00);

     Nrepeat = 64 * Nrepeat;
  }
  else
  {
     alert("Unknown IRAC readout mode. EX-PET is confused! Please reset the form and/or reload the webpage");
     return -1;
  }

 if (bgindex == 0)
 { 
    sensOne1 = sensOneLow1;
    sensOne2 = sensOneLow2;
    sensOne3 = sensOneLow3;
    sensOne4 = sensOneLow4
 } 
 else if (bgindex == 1)
 {
    sensOne1 = sensOneMed1;
    sensOne2 = sensOneMed2;
    sensOne3 = sensOneMed3;
    sensOne4 = sensOneMed4;
 }
 else if (bgindex == 2)
 {
    sensOne1 = sensOneHigh1;
    sensOne2 = sensOneHigh2;
    sensOne3 = sensOneHigh3;
    sensOne4 = sensOneHigh4;
 }
 else
 {
    alert("Unknown background level for IRAC observations. The PET is confused! Please reset the form and/or reload the PET webpage.");
    return(-1);
 }

 // Fill in the exposure depth info:

 t_exp[0] = Nrepeat * frametime;
 t_exp[1] = Nrepeat * frametime;
 t_exp[2] = Nrepeat * frametime;
 t_exp[3] = Nrepeat * frametime;

 result = new Array(4);

 result[0] = sensOne1[frameTimeIndex] / Math.sqrt(Nrepeat);
 result[1] = sensOne2[frameTimeIndex] / Math.sqrt(Nrepeat);
 result[2] = sensOne3[frameTimeIndex] / Math.sqrt(Nrepeat);
 result[3] = sensOne4[frameTimeIndex] / Math.sqrt(Nrepeat); 

 return result;

}


// FUNCTION TO CHECK for SATURATION

function check_saturation( MIPS_70pixelscale, frameTimeIndex, mode, bgindex )
{

   var N_time_choices;

   // Set saturation limits for full array:
   // Numbering scheme is:
   //
   //        sat<BG level><Channel Number>[frametime index *REVERSED*]
   //
   //        e.g., satLow1[1, or, t = 100s] = 3.66360 mJy
   //
   // The frametimes are 100, 30, 12, 2, 0.6, 0.4 REVERSED just to make
   // things confusing.
   // Source: IRAC webpage http://ssc.spitzer.caltech.edu/irac/sat.html
   //         and SOM v7.0, Table 6.12.
   //
   // NOTE: Defined "satMed[1234], and satHigh[1234] in case sat. is found to
   //       be background dependent.
   if ( mode == "Full" )
   {
      N_time_choices = 5;

      satLow1 = new Array(3.8, 13, 32, 190, 630,950);
      satLow2 = new Array(3.9, 13, 33, 190, 650, 980);
      satLow3 = new Array(27, 92, 230, 1400, 4600, 6950);
      satLow4 = new Array(28, 48, 120, 740, 2500, 3700);
    
      satMed1 = satLow1;
      satMed2 = satLow2;
      satMed3 = satLow3;
      satMed4 = satLow4;

      satHigh1 = satLow1;
      satHigh2 = satLow2;
      satHigh3 = satLow3;
      satHigh4 = satLow4;
   }
   else if ( mode == "Subarray" )
   {
      N_time_choices = 2;

      satLow1 = new Array(1000.00, 4000.0, 20000.0);
      satLow2 = new Array(820.0, 3300.0, 17000.0);
      satLow3 = new Array(3100.0, 13000.0, 63000.0);
      satLow4 = new Array(2300.0, 9000.0, 45000.0);
    
      satMed1 = satLow1;
      satMed2 = satLow2;
      satMed3 = satLow3;
      satMed4 = satLow4;

      satHigh1 = satLow1;
      satHigh2 = satLow2;
      satHigh3 = satLow3;
      satHigh4 = satLow4;
   }
  
   if (bgindex == 0)
   { 
      sat1 = satLow1[N_time_choices - frameTimeIndex];
      sat2 = satLow2[N_time_choices - frameTimeIndex];
      sat3 = satLow3[N_time_choices - frameTimeIndex];
      sat4 = satLow4[N_time_choices - frameTimeIndex];
   } 
   else if (bgindex == 1)
   {
          sat1 = satMed1[N_time_choices - frameTimeIndex];
	  sat2 = satMed2[N_time_choices - frameTimeIndex];
	  sat3 = satMed3[N_time_choices - frameTimeIndex];
	  sat4 = satMed4[N_time_choices - frameTimeIndex];
   }
   else if (bgindex == 2)
   {
          sat1 = satHigh1[N_time_choices - frameTimeIndex];
          sat2 = satHigh2[N_time_choices - frameTimeIndex];
	  sat3 = satHigh3[N_time_choices - frameTimeIndex];
	  sat4 = satHigh4[N_time_choices - frameTimeIndex];
   }
   else
   {
      alert("Unknown background level for IRAC observations. The PET is confused! Please reset the form and/or reload the PET webpage.");
          return(-1);
   }
   

   // Fill in saturation array with four IRAC values from above, and
   // the appropriate MIPS values (webpage, Dec. 7, 2004):

   if ( MIPS_70pixelscale == "default") 
   {
       saturation = new Array( sat1, sat2, sat3, sat4, 4100, 23000, 3000);
   }
   else if ( MIPS_70pixelscale == "fine")
    saturation = new Array( sat1, sat2, sat3, sat4, 4100, 57000, 3000);
   else
     alert("Unknown pixel scale in saturation check. PET is confused! Please reset the form and/or reload the webpage.");

   // Set flag to see if anything is saturated:

   satFlag  = new Array(0,0,0,0,0,0,0);
   var tmp   = 0;

   // Loop over bands and check:

   for (var i = 0; i < 7; i++)
   {  
      if ( flux_quot[i] > saturation[i])
      {
         satFlag[i] = 1;
         tmp       += 1;
      }
   }

 // If saturation flag is set, report message:
 
 if ( tmp > 0) 
 {  
   msgWindow=window.open("","Messages",
                            "height=500,width=600,scrollbars=yes,menubar,toolbar"); 

   msgWindow.document.writeln("<HEAD><TITLE>Confusion/Saturation warning</TITLE></HEAD>");
   msgWindow.document.writeln("<body bgcolor=\"f52887\"><H4>Saturation warning!</H4>");
   msgWindow.document.writeln("<pre>");
   if ( satFlag[0] > 0 )
      {msgWindow.document.writeln("<br>The source may saturate the 3.6 micron band.")};
   if ( satFlag[1] > 0 )
      {msgWindow.document.writeln("<br>The source may saturate the 4.5 micron band.")};
   if ( satFlag[2] > 0)
      {msgWindow.document.writeln("<br>The source may saturate the 5.8 micron band.")};
   if ( satFlag[3] > 0)
      {msgWindow.document.writeln("<br>The source may saturate the 8.0 micron band.")};
   if ( satFlag[4] > 0)
      {msgWindow.document.writeln("<br>The source may saturate the 24 micron band.")};
   if ( satFlag[5] > 0)
      {msgWindow.document.writeln("<br>The source may saturate the 70 micron band.")};
   if ( satFlag[6] > 0)
      {msgWindow.document.writeln("<br>The source may saturate the 160 micron band.")};
   msgWindow.document.writeln("</pre>");

   msgWindow.document.writeln("The saturation limits used in the PET do not include any diffuse emission components. See the  saturation webpages for <a href=\"http://ssc.spitzer.caltech.edu/irac/sat.html\">IRAC</a> and <a href=\"http://ssc.spitzer.caltech.edu/mips/sat.html\">MIPS </a> for more details.");

   msgWindow.document.writeln("<P><form><INPUT TYPE='button' VALUE='Close' onClick='self.close()'></form>");

   msgWindow.document.writeln("</body>");
 }
 
 return satFlag;
}


// FUNCTION TO CHECK CONFUSION LIMIT: 

function check_confusion( the_sensitivity )
{

 // Confusion limits, in micro-Jy:
 // Source: MIPS: http://ssc.spitzer.caltech.edu/mips/sens.html
 //          IRAC: SOM v7.0, Figure 6.20, p105.

 confusion = new Array(.6, .6, 0, 0, 11.2, 640.0, 8000.0); 

 // Flag to see if anything is below confusion limit:

 confFlag  = new Array(0,0,0,0,0,0,0);
 var tmp   = 0;

 // Loop over bands and check:

 for (var i = 0; i < 7; i++)
 {
    if ( the_sensitivity[i] <= confusion[i])
    {
       confFlag[i] = 1;
       tmp        += 1;
    }
 }

 // If confusion flag is set, report message:
 
 if ( tmp > 1) // 160 micron array ALWAYS is below confusion. Do not report.
 {  
   msgWindow=window.open("","Messages", "height=500,width=600,scrollbars=yes,menubar");  
     
   msgWindow.document.writeln("<HEAD><TITLE>Confusion/Saturation warning</TITLE></HEAD>");
   msgWindow.document.writeln("<body bgcolor=\"f52887\"><H4>Confusion warning!</H4>");
   msgWindow.document.writeln("<pre>");
   if ( confFlag[0] > 0 )
      {msgWindow.document.writeln("<br>3.6 micron band may be below the confusion limit.")};
   if ( confFlag[1] > 0 )
      {msgWindow.document.writeln("<br>4.5 micron band may be below the confusion limit.")};
   if ( confFlag[2] > 0)
      {msgWindow.document.writeln("<br>5.8 micron band may be below the confusion limit.")};
   if ( confFlag[3] > 0)
      {msgWindow.document.writeln("<br>8.0 micron band may be below the confusion limit.")};
   if ( confFlag[4] > 0)
      {msgWindow.document.writeln("<br>24 micron band may be below the confusion limit.")};
   if ( confFlag[5] > 0)
      {msgWindow.document.writeln("<br>70 micron band may be below the confusion limit.")};
   if ( confFlag[6] > 0)
      {msgWindow.document.writeln("<br>160 micron band may be below the confusion limit.")};
   msgWindow.document.writeln("<p></p><P><I><FONT COLOR=\"white\">* <B>Note on confusion limits:</FONT></B></I>");

   msgWindow.document.writeln("<pre>The confusion limits are <B>lower limits</B> to the actual");
   msgWindow.document.writeln("position dependent on-sky confusion. The lower limits ");
   msgWindow.document.writeln("shown on the low background sensitivity charts are for ");
   msgWindow.document.writeln("regions of lowest expected confusion at high Galactic ");
   msgWindow.document.writeln("latitudes and on clean sky. The observer should consider ");
   msgWindow.document.writeln("the local confusion caused by background sources when ");
   msgWindow.document.writeln("planning observations.");
   msgWindow.document.writeln("Confusion will likely be more important in higher ");
   msgWindow.document.writeln("background regions, and can limit the sensitivity that ");
   msgWindow.document.writeln("can be achieved. Local sources of confusion, such as ");
   msgWindow.document.writeln("cirrus and the stellar background, are highly variable ");
   msgWindow.document.writeln("and can be very localized.");

   msgWindow.document.writeln("<P><form><INPUT TYPE='button' VALUE='Close' onClick='self.close()'></form>");
   msgWindow.document.writeln("</pre>");
   msgWindow.document.writeln("</body>");
 }
 
 return confFlag;
}

