 // The function below (FormSubmit) is the main, front-end to the form
 // interface. Checks input value syntax, and calls functions to do the
 // calcs, and report the results.

 function FormSubmit(theform)
 {
    
   // FIRST, DETERMINE WHICH OBJECT RADIO BUTTON IS SELECTED, TO DETERMINE
   // OBJECT CLASS:

   var radio_index = radio_active(theform.object_class);

    if ( radio_index >= 0 )
    {
       theform.selected_radio.value =
            theform.object_class[radio_index].value;
    }

    // READ NORMALIZATION AND SCALE:

    var normfac       = theform.normfac[radio_index].value;
    var wavelength    = theform.wavelength[radio_index].value;

    // BLACKBODY SED:

    if( theform.object_class[radio_index].value == "Blackbody" )  
    {
       var Teff          = theform.Teff.value;

       // CHECK VALIDITY OF INPUT PARAMETERS:

       var bb_input = check_bb_input(Teff, normfac, wavelength);

       if( !bb_input ) 
       {
          bb_input_error(theform, radio_index);
	  return -1;
       }

       // FILL IN THE SED MODEL REPORT FIELD:

       theform.selected_radio.value += ":\n   * T_eff = " + Teff + " K; Fnu(" +
	  wavelength + " microns) = " + normfac + " mJy";

       // CALCULATE THE INTRINSIC FLUX, COLOR CORR, AND QUOTED FLUX:

       flux_quot =  calc_flux_bb(Teff, normfac, wavelength); 

    }

    // MODIFIED BLACKBODY SED:

    else if(theform.object_class[radio_index].value == "Modified Blackbody")  
    {

       var Teff_mod          = theform.Teff_mod.value;
       var index_mod         = theform.index_mod.value;

       // CHECK VALIDITY OF INPUT: IS IT A FLOAT?

       var modbb_input = check_modbb_input(Teff_mod, normfac,
	   wavelength, index_mod);

       if( !modbb_input ) 
       {
          modbb_input_error(theform, radio_index);
	  return -1;
       }

       // FILL IN THE SED MODEL REPORT FIELD with sed parameters:

       theform.selected_radio.value += ":\n   * T_eff = " + Teff_mod + " K; index = " + index_mod + "; Fnu(" + wavelength + " microns) = " + normfac + " mJy";

       // CALCULATE THE INTRINSIC FLUX, COLOR CORR, AND QUOTED FLUX:

       flux_quot =  calc_flux_bb_mod(Teff_mod, normfac,
                                wavelength, index_mod); 

    }  

    // POWER LAW SED:
 
    else if( theform.object_class[radio_index].value == "Power law")
    {
       var index       = theform.alpha.value;

       // CHECK VALIDITY OF INPUT: IS IT A FLOAT?

       var pl_input = check_pl_input(index, normfac, wavelength);

       if( !pl_input ) 
       {
          pl_input_error(theform, radio_index);
	  return -1;
       }

       // FILL IN THE SED MODEL REPORT FIELD:

       theform.selected_radio.value += ":\n   * Index = " + index + 
          "; Fnu(" + wavelength + " microns) = " + normfac + " mJy";

       // CALCULATE THE INTRINSIC FLUX, COLOR CORR, AND QUOTED FLUX:

       flux_quot =  calc_flux_powerlaw(index, normfac, wavelength); 
    }

    // NAMED OBJECT SED:

    else if( theform.object_class[radio_index].value == "Named object")
    {
       var redshift   = theform.redshift_named.value;

       // CHECK VALIDITY OF INPUT: IS IT A FLOAT?

       var no_input = check_no_input(normfac, wavelength, redshift);

       if( !no_input ) 
       {
          no_input_error(theform, radio_index);
	  return -1;
       }

       // FILL IN THE SED MODEL REPORT FIELD:

       theform.selected_radio.value += ": " +
          theform.ObjectName[theform.ObjectName.selectedIndex].value +
          "\n   * z = " + redshift + "; Fnu(" + wavelength + 
	  " microns) = " + normfac + " mJy";

       // CALCULATE THE INTRINSIC FLUX, COLOR CORR, AND QUOTED FLUX:

       flux_quot =  calc_flux_named(
                   theform.ObjectName[theform.ObjectName.selectedIndex].value,
                                normfac, wavelength, redshift); 
    }

    // COMPOSITE SED:

    else if( theform.object_class[radio_index].value == "Composite SED")
    {       
       var redshift   = theform.redshift_comp.value;

       // CHECK VALIDITY OF INPUT: IS IT A FLOAT?

       var composite_input = check_no_input(normfac, wavelength, redshift);

       if( !composite_input ) 
       {
          composite_input_error(theform, radio_index);
	  return -1;
       }

       // FILL IN THE SED MODEL REPORT FIELD:

       theform.selected_radio.value += ": " +
          theform.SEDType[theform.SEDType.selectedIndex].value + 
	  "\n   * z = " + redshift + "; Fnu(" + wavelength + " microns) = " 
	  + normfac  + " mJy";
 
      // CALCULATE THE INTRINSIC FLUX, COLOR CORR, AND QUOTED FLUX:
 
       flux_quot =  calc_flux_named(
                   theform.SEDType[theform.SEDType.selectedIndex].value,
                                normfac, wavelength, redshift); 
    }
    else
    {
       alert("Unknown SED selection. PET is confused! Please reset the form and/or reload the webpage.");
       return -1;
    }

    // NOW DONE WITH CALCULATING SOURCE STUFF. ON TO THE SKY + INSTRUMENTS:

    // READ BACKGROUND INDEX:

    var background_index = theform.thebackground.selectedIndex;

    // DETERMINE IRAC MODE (which radio button clicked):

    radio_index = radio_active(theform.IRAC_mode);

    // READ NUMBER OF REPEATS:

    var Nrepeat = theform.Nrepeat[radio_index].value;

    // SET PARAMS (e.g., frametime) FOR IRAC MODE CHOSEN.

    if ( theform.IRAC_mode[radio_index].value == "Full Array" )
    {
       var mode             = "Full"
       var frametime_index  = theform.frameTime.selectedIndex;
       var frametime        = theform.frameTime[theform.frameTime.selectedIndex].value;
    }
    else if ( theform.IRAC_mode[radio_index].value == "Subarray" )
    {
       var mode             = "Subarray"
       var frametime_index  = theform.frameTime_sub.selectedIndex;
       var frametime        = theform.frameTime_sub[theform.frameTime_sub.selectedIndex].value;
    }
    else
    {
       alert("Bad input of IRAC array mode. PET is confused! Please reset the form and/or reload the webpage.");
       return -1;
    }
       
    // CHECK VALIDITY OF INPUT: IS Nrepeat AN INTEGER?
       
    var irac_input = check_IRAC_input( Nrepeat );

    if( !irac_input ) 
       {
          irac_input_error( theform, mode, frametime, Nrepeat );
	  return -1;
       }

    // DETERMINE SENSITIVITY in IRAC BANDS:
    //    Result is in *** micro-Jy ***

    irac_sens = new Array(4);
    irac_sens = IRACsensFull(frametime_index, Nrepeat, background_index, 
			     frametime, mode);

    // UPDATE REPORT MESSAGE WITH IRAC PARAMETERS.

    theform.selected_radio.value += "\nIRAC: " + theform.IRAC_mode[radio_index].value + " mode ; Frame Time = " + frametime + " sec ; Nrepeat = " + Nrepeat;

    // PROCESS MIPS:

    radio_index = radio_active(theform.MIPS_mode);
    mips_sens   = new Array(3);

    if (theform.MIPS_mode[radio_index].value == "Photometry" )
    {

       // CHECK VALIDITY OF INPUT: are all Nrepeats INTEGERS?

       var N24  = theform.MIPS_photo_24Nrepeat.value;
       var N70  = theform.MIPS_photo_70Nrepeat.value;
       var N160 = theform.MIPS_photo_160Nrepeat.value;

       var check_N = check_MIPS_photo_N( N24, N70, N160 );

       if ( !check_N )
       {
          MIPS_photo_input_error(theform, radio_index);
	  return -1;
       }

      mips_sens = MIPS_photo_sensitivity( 
                  theform.MIPS_photo_24fieldsize[theform.MIPS_photo_24fieldsize.selectedIndex].value,
                     theform.MIPS_photo_24frameTime.selectedIndex, N24,
		     theform.MIPS_photo_70pixelscale[theform.MIPS_photo_70pixelscale.selectedIndex].value,
		     theform.MIPS_photo_70fieldsize[theform.MIPS_photo_70fieldsize.selectedIndex].value,
                     theform.MIPS_photo_70frameTime.selectedIndex, N70,
		     theform.MIPS_photo_160fieldsize[theform.MIPS_photo_160fieldsize.selectedIndex].value,
		     theform.MIPS_photo_160frameTime.selectedIndex, N160,
		     theform.thebackground.selectedIndex);

       // UPDATE REPORT FORM WITH MIPS PHOTO PARAMETERS:

       theform.selected_radio.value += "\nMIPS: " + theform.MIPS_mode[radio_index].value + " mode\n  24 microns: Field size = " + theform.MIPS_photo_24fieldsize[theform.MIPS_photo_24fieldsize.selectedIndex].value + " ; Frame time = " + theform.MIPS_photo_24frameTime[theform.MIPS_photo_24frameTime.selectedIndex].value + " sec ; Nrepeat = " + N24;

       theform.selected_radio.value += "\n  70 microns: Field size = " + theform.MIPS_photo_70fieldsize[theform.MIPS_photo_70fieldsize.selectedIndex].value + " ; Frame time = " + theform.MIPS_photo_70frameTime[theform.MIPS_photo_70frameTime.selectedIndex].value + " sec ; Nrepeat = " + N70 + "\n              Pixel scale = " + theform.MIPS_photo_70pixelscale[theform.MIPS_photo_70pixelscale.selectedIndex].value;

       theform.selected_radio.value += "\n 160 microns: Field size = " + theform.MIPS_photo_160fieldsize[theform.MIPS_photo_160fieldsize.selectedIndex].value + " ; Frame time = " + theform.MIPS_photo_160frameTime[theform.MIPS_photo_160frameTime.selectedIndex].value + " sec ; Nrepeat = " + N160 + "\nBackground = " + theform.thebackground[theform.thebackground.selectedIndex].value;

    }
    else if (theform.MIPS_mode[radio_index].value == "Scan")
    {  
       // CHECK VALIDITY OF INPUT: IS Nrepeat an INTEGER?

       var check_Nrepeat_scan = its_integer(theform.MIPS_scan_Nrepeat.value);

       // IF Nrepeat != INTEGER, OR IS NON-PHYSICAL (neg), WARN:

       if ( !check_Nrepeat_scan || theform.MIPS_scan_Nrepeat.value <= 0.0 )
       {
          MIPS_scan_input_error( theform, radio_index);
	  return -1;
       } 

       // CALCULATE INSTRUMENT SENSITIVITY


       mips_sens = MIPS_scan_sensitivity(theform.MIPS_scan_scanrate[theform.MIPS_scan_scanrate.selectedIndex].value, 
                     theform.MIPS_scan_Nrepeat.value,
		     theform.thebackground.selectedIndex);

       // UPDATE REPORT FORM WITH MIPS SCAN MAP PARAMETERS:

       theform.selected_radio.value += "\nMIPS: " + theform.MIPS_mode[radio_index].value + " mode\n  Scan rate = " + theform.MIPS_scan_scanrate[theform.MIPS_scan_scanrate.selectedIndex].value + " ; Number of scan passes = " + theform.MIPS_scan_Nrepeat.value + "\nBackground = " + theform.thebackground[theform.thebackground.selectedIndex].value;

    }

    // WRITE SENSITIVITIES FROM IRAC AND MIPS TO A SINGLE ARRAY:

    full_sens = new Array(7);

    for( i = 0; i < 4; i ++ )
    {
       full_sens[i] = irac_sens[i];
    }
    for( i = 4; i < 7; i ++ )
    {
        full_sens[i] = mips_sens[i-4];
    }

    // Check to see if sensitivities are below confusion limit:

    conf_bands = new Array(7);
    conf_bands = check_confusion( full_sens );

    // DETERMINE SIGNAL-TO-NOISE, etc, and round decimals:

    for( i = 0; i < 7; i++ )
    {  
       SN[i]             = intrinsic_flux[i] / (full_sens[i]/1000.0);     
       intrinsic_flux[i] = SigFig(intrinsic_flux[i], 3);       
       flux_quot[i]      = SigFig(flux_quot[i], 3);
       K[i]              = SigFig(K[i], 3);
       SN[i]		 = SigFig(SN[i], 2);
       full_sens[i]      = SigFig( full_sens[i], 3 );       
       t_exp[i]          = SigFig( t_exp[i], 3 );
    }    


    // CHECK FOR SATURATION
    var sat_bands = new Array(7);

    if (theform.MIPS_mode[radio_index].value == "Photometry" )
    {
        sat_bands = check_saturation(theform.MIPS_photo_70pixelscale[theform.MIPS_photo_70pixelscale.selectedIndex].value, frametime_index, mode, background_index );
    }
    else if (theform.MIPS_mode[radio_index].value == "Scan" )
    {
        sat_bands = check_saturation("default", frametime_index, mode, background_index );
    }
    else
    {
       alert("Unknown MIPS mode in saturation check. PET is confused! Please reset the form and/or reload the webpage.");
    }

    // FILL-IN FORM OBJECT WITH CALCULATED QUANTITIES:

    theform.flux_quot.value   = "IRAC: | ";
    theform.color_corr.value  = "IRAC: | ";
    theform.sed_flux.value    = "IRAC: | ";
    theform.sensitivity.value = "IRAC: | ";
    theform.texp.value        = "IRAC: | ";
    theform.SN.value          = "IRAC: | ";
    for( i = 0; i < 4; i++ )
    {
      theform.flux_quot.value   += flux_quot[i] + " | ";
      theform.color_corr.value  += K[i] + " | ";
      theform.sed_flux.value    += intrinsic_flux[i] + " | ";
      theform.sensitivity.value += full_sens[i] + " | ";
      theform.texp.value        += t_exp[i] + " | ";
      theform.SN.value          += SN[i] + " | ";
    }
    theform.flux_quot.value   += "\nMIPS: | ";
    theform.color_corr.value  += "\nMIPS: | ";
    theform.sed_flux.value    += "\nMIPS: | ";
    theform.sensitivity.value += "\nMIPS: | ";
    theform.texp.value        += "\nMIPS: | ";
    theform.SN.value          += "\nMIPS: | ";
    for( i = 4; i < 7; i++ )
    {
      theform.flux_quot.value   += flux_quot[i] + " | ";
      theform.color_corr.value  += K[i] + " | ";
      theform.sed_flux.value    += intrinsic_flux[i] + " | ";
      theform.sensitivity.value += full_sens[i] + " | ";
      theform.texp.value        += t_exp[i] + " | ";
      theform.SN.value          += SN[i] + " | ";
    }

    if(theform.MIPS_mode[radio_index].value == "Photometry" && theform.MIPS_photo_160fieldsize[theform.MIPS_photo_160fieldsize.selectedIndex].value == "enhanced")
    {
      theform.sensitivity.value += "160um enhanced mode - see note: (iv) |"; 
    }

    theform.wavebands.value     = "IRAC: | 3.55  | 4.49  | 5.73   | 7.87 |\nMIPS: | 23.68 | 71.44 | 155.90 |";

   // CLUDGE TO RE-SET SOME VALUES TO REFLECT REALITIES FOR CP-1

  // If doing P/SR, 24 micron, high bkgd, 30s exposures, reset
  // sensitivity since this is not a recommended mode.
  
  theform.notes.value = "";

  if (theform.MIPS_mode[radio_index].value == "Photometry" && theform.MIPS_photo_24frameTime[theform.MIPS_photo_24frameTime.selectedIndex].value == 30 && theform.thebackground.selectedIndex == 2)
  {

    theform.sensitivity.value = "IRAC: | ";
    theform.SN.value = "IRAC: | ";
    for( i = 0; i <= 3; i++ )
    {     
      theform.sensitivity.value += full_sens[i] + " | ";
      theform.SN.value += SN[i] + " | "
    }
    
    theform.sensitivity.value += "\nMIPS: | see note: (i) | " + full_sens[5] + " | " + full_sens[6] + " | ";

    theform.SN.value += "\nMIPS: | see note: (i) | " + SN[5] + " | " + SN[6] + " | ";
  
    theform.notes.value += "(i) 24 microns: 30s frame time not recommended for high background\n     (saturation warning)\n";
  
  }

 // Add note about confusion:

   var cflag;

   for( i = 0; i < 7; i++ )
    {
       if (conf_bands[i] > 0)
       {
          cflag = 1;
       }
    }


   if ( cflag > 0 )
   {
      theform.notes.value += "\n(ii) Confusion warning for: ";

      if (conf_bands[0] == 1)
      {
         theform.notes.value += " | 3.55 microns";           
      }
      if (conf_bands[1] == 1)
      {
         theform.notes.value += " | 4.49 microns";           
      }
      if (conf_bands[2] == 1)
      {
         theform.notes.value += " | 5.66 microns";           
      }
      if (conf_bands[3] == 1)
      {
         theform.notes.value += " | 7.87 microns";           
      }
      if (conf_bands[4] == 1)
      {
         theform.notes.value += " | 23.68 microns";           
      }
      if (conf_bands[5] == 1)
      {
         theform.notes.value += " | 71.44 microns";           
      }
      if (conf_bands[6] == 1)
      {
         theform.notes.value += " | 155.90 microns";           
      }
      theform.notes.value += " |\n";
   }

 // Add note about saturation:

   var sflag;

   for( i = 0; i < 7; i++ )
    {
       if (sat_bands[i] > 0)
       {
          sflag = 1;
       }
    }


   if ( sflag > 0 )
   {
      theform.notes.value += "\n(iii) Saturation warning for: ";

      if (sat_bands[0] == 1)
      {
         theform.notes.value += " | 3.55 microns";           
      }
      if (sat_bands[1] == 1)
      {
         theform.notes.value += " | 4.49 microns";           
      }
      if (sat_bands[2] == 1)
      {
         theform.notes.value += " | 5.66 microns";           
      }
      if (sat_bands[3] == 1)
      {
         theform.notes.value += " | 7.87 microns";           
      }
      if (sat_bands[4] == 1)
      {
         theform.notes.value += " | 23.68 microns";           
      }
      if (sat_bands[5] == 1)
      {
         theform.notes.value += " | 71.44 microns";           
      }
      if (sat_bands[6] == 1)
      {
         theform.notes.value += " | 155.9 microns";           
      }
      theform.notes.value += " |\n";
   }

    // MIPS 160 enhanced mode warning:

    if(theform.MIPS_mode[radio_index].value == "Photometry" && theform.MIPS_photo_160fieldsize[theform.MIPS_photo_160fieldsize.selectedIndex].value == "enhanced")
    {
      theform.notes.value += "\n(iv) See the EX-PET help page for information on the 160 micron enhanced mode rationale and sensitivity.\n";
    }

     theform.notes.value += "\n(v) See the following note for IRAC S/N when the observations are not background-noise-limited:\n  http://ssc.spitzer.caltech.edu/documents/irac_memo.txt\n";



}
