// 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)
{
    
   // READ BACKGROUND INDEX:

   var background_index = theform.thebackground.selectedIndex;


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


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

   radio_index_warm = radio_active(theform.IRAC_mode_warm);


   // READ NUMBER OF REPEATS:

   var Nrepeat_warm = theform.Nrepeat_warm[radio_index_warm].value;

   // SET OTHER PARAMS FOR MODE CHOSEN:

    if ( theform.IRAC_mode_warm[radio_index_warm].value == "Full Array" )
    {
       var mode_warm             = "Full"
       var frametime_index_warm  = theform.frameTime_warm.selectedIndex;
       var frametime_warm        = theform.frameTime_warm[theform.frameTime_warm.selectedIndex].value;
    }
    else if ( theform.IRAC_mode[radio_index_warm].value == "Subarray" )
    {
       var mode_warm             = "Subarray"
       var frametime_index_warm  = theform.frameTime_sub_warm.selectedIndex;
       var frametime_warm        = theform.frameTime_sub_warm[theform.frameTime_sub_warm.selectedIndex].value;
    }
    else
    {
       alert("Bad input of Warm IRAC array mode. SET is confused! Please reset the form and/or reload the webpage.");
       return -1;
    }
       
    // CHECK VALIDITY OF INPUT: IS Nrepeat AN INTEGER?
       
    var irac_input_warm = check_IRAC_input( Nrepeat_warm );

    if( !irac_input_warm ) 
       {
          warm_irac_input_error( theform, mode_warm, frametime_warm, Nrepeat_warm );
	  return -1;
       }

    irac_sens_warm = new Array(2);
    irac_sens_warm = IRACsensFull_warm(frametime_index_warm, Nrepeat_warm, background_index, 
			     frametime_warm, mode_warm);

    // UPDATE REPORT MESSAGE WITH WARM IRAC PARAMETERS.

    theform.selected_radio.value = "Warm IRAC: " + theform.IRAC_mode_warm[radio_index_warm].value + " mode ; Frame Time = " + frametime_warm + " sec ; Nrepeat = " + Nrepeat_warm;



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

   // 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 OTHER PARAMS FOR 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. SET 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;
       }

    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 += "\nCryo IRAC: " + theform.IRAC_mode[radio_index].value + " mode ; Frame Time = " + frametime + " sec ; Nrepeat = " + Nrepeat;

// **********

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

   radio_index = radio_active(theform.IRS_mode);

   // READ NUMBER OF REPEATS:

   var Nrepeat_IRS = theform.IRS_Nrepeat[radio_index].value;

   // SET OTHER PARAMS FOR MODE CHOSEN:

    if ( theform.IRS_mode[radio_index].value == "Red Filter" )
    {
       var IRS_mode             = "Red PUI"
       var IRS_frametime_index  = theform.IRS_frameTime_red.selectedIndex;
       var IRS_frametime        = theform.IRS_frameTime_red[theform.IRS_frameTime_red.selectedIndex].value;
    }
    else if ( theform.IRS_mode[radio_index].value == "Blue Filter" )
    {
       var IRS_mode             = "Blue PUI"
       var IRS_frametime_index  = theform.IRS_frameTime_blue.selectedIndex;
       var IRS_frametime        = theform.IRS_frameTime_blue[theform.IRS_frameTime_blue.selectedIndex].value;
    }
    else
    {
       alert("Bad input of IRS PUI mode. SET is confused! Please reset the form and/or reload the webpage.");
       return -1;
    }
       
    // CHECK VALIDITY OF INPUT: IS Nrepeat_IRS AN INTEGER?
       
    var irs_input = check_IRS_input( Nrepeat_IRS );

    if( !irs_input ) 
    {
       irs_input_error( theform, IRS_mode, IRS_frametime, Nrepeat_IRS );
       return -1;
    }

    irs_sens = IRSsensFull(IRS_frametime_index, Nrepeat_IRS, background_index, IRS_frametime, IRS_mode);

    // UPDATE REPORT MESSAGE WITH IRS PARAMETERS.

    theform.selected_radio.value += "\nIRS PUI:  " + theform.IRS_mode[radio_index].value + " mode ; Frame Time = " + IRS_frametime + " sec ; Nrepeat = " + Nrepeat_IRS;

// **************

    // DETERMINE SENSITIVITY in MIPS BANDS:

    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;
       } 

       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(10);

    for( i = 0; i < 4; i ++ )
    {
       full_sens[i] = irac_sens[i];
    }
    full_sens[4] = irs_sens;
    for( i = 5; i < 8; i ++ )
    {
        full_sens[i] = mips_sens[i-5];
    }
    for(i = 8; i < 10; i++ )
    {
       full_sens[i] = irac_sens_warm[i-8];
    }

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

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

    // Round decimals:

    var last_waveband = 9; // set this to 10 for all imaging bands, including IRAC warm in slots 7,8

    if ( IRS_mode == "Blue PUI")
    {
       theform.wavebands.value     = "Warm IRAC:  | 3.6 | 4.5 |\nCryo IRAC:  | 3.6 | 4.5 | 5.8 | 8.0 |\nIRS PUI:    | 16  |\nMIPS:       | 24  | 70  | 160 |";
    }
    else if (IRS_mode == "Red PUI" )
    {
       theform.wavebands.value     = "Warm IRAC:  | 3.6 | 4.5 |\nCryo IRAC:  | 3.6 | 4.5 | 5.8 | 8.0 |\nIRS PUI:    | 22  |\nMIPS:       | 24  | 70  | 160 |";
    }
    else
    {
       alert("Bad input of IRS PUI mode. SENS-PET is confused! Please reset the form and/or reload the webpage.");
       return -1;
    }

    // for( i = 0; i <= last_waveband; i++ )
    for( i = 0; i < 10; i++ )
    {  
       full_sens[i]      = SigFig( full_sens[i], 3 ); 
       extended_source_sens[i] = SigFig( extended_source_sens[i], 3 );
       t_exp[i] =   SigFig( t_exp[i], 3 );   
    }

 
    // FILL-IN FORM OBJECT WITH CALCULATED QUANTITIES:

    // WARM IRAC:

    theform.sensitivity.value = "Warm IRAC:    | ";
    theform.texp.value        = "Warm IRAC:    | ";
    theform.ess.value         = "Warm IRAC:    | ";
    theform.notes.value         = "";

    for( i = 8; i < 10; i++ )
    {
      theform.sensitivity.value += full_sens[i] + " | ";
      theform.texp.value        += t_exp[i] + " | ";
      theform.ess.value         += extended_source_sens[i] + " | ";  
    }

    // IRAC:

    theform.sensitivity.value += "\nCryo IRAC:    | ";
    theform.texp.value        += "\nCryo IRAC:    | ";
    theform.ess.value         += "\nCryo IRAC:    | ";
    theform.notes.value       += "\n";

    for( i = 0; i < 4; i++ )
    {
      theform.sensitivity.value += full_sens[i] + " | ";
      theform.texp.value        += t_exp[i] + " | ";
      theform.ess.value         += extended_source_sens[i] + " | ";
    }

    // IRS PUI: 
    theform.sensitivity.value += "\nIRS PUI:      | " + full_sens[4] + " & see note (ii) |";
    theform.texp.value        += "\nIRS PUI:      | " + t_exp[4] + " |";
    theform.ess.value         += "\nIRS PUI:      | " + extended_source_sens[4] + " |";

    // MIPS:
    theform.sensitivity.value += "\nMIPS:         | ";
    theform.texp.value        += "\nMIPS:         | ";
    theform.ess.value        += "\nMIPS:         | ";

    for( i = 5; i < 8; i++ )
    {
      theform.texp.value        += t_exp[i] + " | ";
    }

    // Special case: 24 micron P/SR, high bkdg, 30s (not recommended):

    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 += "see note: (iii) | " + full_sens[6] + " | " + full_sens[7];
       theform.ess.value         += "see note: (iii) | " + extended_source_sens[6] + " | " + extended_source_sens[7] + " | ";   
  
    }
    else {
	 theform.sensitivity.value += full_sens[5] + " | " + full_sens[6] + " | " + full_sens[7];
	 theform.ess.value         += extended_source_sens[5] + " | " + extended_source_sens[6] + " | " + extended_source_sens[7];
    }

    // Add warning for 160 micron enhanced mode:

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


 // Add note about confusion:

   var cflag;

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

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

      if (conf_bands[8] == 1)
      {
         theform.notes.value += " | Warm IRAC 3.6 microns";           
      }
      if (conf_bands[9] == 1)
      {
         theform.notes.value += " | Warm IRAC 4.5 microns";           
      }
      if (conf_bands[0] == 1)
      {
         theform.notes.value += " | Cryo IRAC 3.6 microns";           
      }
      if (conf_bands[1] == 1)
      {
         theform.notes.value += " | Cryo IRAC 4.5 microns";           
      }
      if (conf_bands[2] == 1)
      {
         theform.notes.value += " | Cray IRAC 5.8 microns";           
      }
      if (conf_bands[3] == 1)
      {
         theform.notes.value += " | Cryo IRAC 8.0 microns";           
      }
      if (conf_bands[4] == 1)
      {
         theform.notes.value += " | PUI";           
      }
      if (conf_bands[5] == 1)
      {
         theform.notes.value += " | 24 microns";           
      }
      if (conf_bands[6] == 1)
      {
         theform.notes.value += " | 70 microns";           
      }
      if (conf_bands[7] == 1)
      {
         theform.notes.value += " | 160 microns";           
      }
      theform.notes.value += " |\n\n";
   }

    // Fill in warning/notes box:

    
    // PUI pt source sensitivity warning:

    theform.notes.value += "(ii) IRS PUI point source sensitivities are provided for very small apertures containing only half the total light (less than 50 square arcseconds). This is a much smaller aperture than used to estimate the MIPS 24 micron sensitivities (approx. 740 square arcseconds). Accounting for apertures, sensitivities for PUI and MIPS 24 micron imaging are similar.\n\n"; 

    // 24 micron, P/SR, high bkdg, 30s (not recommended):

    if (theform.MIPS_mode[radio_index].value == "Photometry" && theform.MIPS_photo_24frameTime[theform.MIPS_photo_24frameTime.selectedIndex].value == 30 && theform.thebackground.selectedIndex == 2)
    {
       theform.notes.value += "(iii) 24 microns: 30s frame time not recommended for high background -- saturation warning.\n\n";
    }


    // MIPS 160 enhanced mode:

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

}
