
#######################################################################
#                	Location Variables                            #
#######################################################################

#$url_of_the_form = "Forms/npsf.html";
#$url_of_the_form = "Forms/input_npsf.html";
$location_of_mail_lib = "./Library/mail-lib.pl";
$location_of_setup_file = "final_npsf.setup.cgi";


#section #1 ....... sub which_query {
#section #2 ....... sub min_max {
#section #3 ....... sub obs_name_sub {
#section #4 ....... sub no_input {
#section #5 ....... VALIDATE -- Test a data type validation subroutine
#section #7 ....... sub no_input_input {
#section #8 ....... sub check {
#section #9 ....... sub file_list {
#section #10 .......sub quote_literals {

#######################################################################
#                  Defining your Fields                               #
#######################################################################

#database field names

#used in #section#1
@form_variables_base = ("query_decision","rootname", 
	"prop_id", 
	"camera", 	
	"filter", 	
	"focus", 	
	"date_time_obs", 
	"targname", 
	"spectype",
	"x",
	"y", 
	"pkcount", 
	"totcount", 
	"aperad", 	
	"obsmode", 
	"sampseq", 
	"nsamp", 	
	"exptime", 
	"temperature", 
	"crpix1", 	
	"crpix2", 	
	"crval1", 	
	"crval2", 	
	"ctype1", 	
	"ctype2", 	
	"npfocusp", 
	"npxtiltp", 
	"npytiltp", 
	"fomxpos", 
	"fomypos", 
	"nfxtiltp", 
	"nfytiltp", 
	);


	"min_date_time_obs",
	"max_date_time_obs",

	"x",
	"y",	

#used in #section#4
@incoming_from_form_variables = ("rootname", 
	"prop_id", 	
	"min_date_time_obs", 
	"max_date_time_obs", 
	"nic1_filter",
	"nic2_filter",
	"nic3_filter",
	"x",	
	"y",	
	"x_y_radius",	
	"obsmode", 
	"sampseq", 
	"focus", 	
	"min_npfocusp", 
	"max_npfocusp", 
	"min_temperature", 
	"max_temperature", 
	"targname", 
	"min_exptime", 
	"max_exptime", 
	"min_pkcount", 
	"max_pkcount", 
	"min_totcount", 
	"max_totcount" 
	);
	
# used in #section#1 and #4
@just_parameters_incoming_from_form_variables = ("prop_id", 	
	"min_date_time_obs", 
	"max_date_time_obs", 
	"nic1_filter",
	"nic2_filter",
	"nic3_filter",
	"x",	
	"y",	
	"x_y_radius",	
	"obsmode", 
	"sampseq", 
	"focus", 	
	"min_npfocusp", 
	"max_npfocusp", 
	"min_temperature", 
	"max_temperature", 
	"targname", 
	"min_exptime", 
	"max_exptime", 
	"min_pkcount", 
	"max_pkcount", 
	"min_totcount", 
	"max_totcount" 
	);
	
#used in #section7 & #8
@required_variables = ("rootname", 
	"prop_id", 	
	"camera", 	
	"filter", 	
	"focus", 	
	"date_time_obs", 
	"targname", 
	"spectype", 
	"x",	
	"y",	
	"pkcount", 
	"totcount", 
	"aperad", 	
	"obsmode", 
	"sampseq", 
	"nsamp", 	
	"exptime", 
	"temperature", 
	"crpix1", 	
	"crpix2", 	
	"crval1", 	
	"crval2", 	
	"ctype1", 	
	"ctype2", 	
	"npfocusp", 
	"npxtiltp", 
	"npytiltp", 
	"fomxpos", 
	"fomypos", 
	"nfxtiltp", 
	"nfytiltp" 
	);
	




#used in #section #8
%required_variable_type  = ("rootname"		=> "char",
        "prop_id"                           	=> "int",
        "camera"                           	=> "int",
        "filter" 				=> "char",
        "focus" 				=> "char",
        "date_time_obs" 			=> "date",
	"targname" 				=> "char",
	"spectype" 				=> "char",
	"x" 					=> "real",
	"y" 					=> "real",
	"pkcount" 				=> "real",
	"totcount" 				=> "real",
	"aperad" 				=> "int",
     	"obsmode" 				=> "char",
     	"sampseq" 				=> "char",
     	"nsamp" 				=> "int",
     	"exptime" 				=> "real",
     	"temperature" 				=> "real",
     	"crpix1" 				=> "int",
     	"crpix2" 				=> "int",
     	"crval1" 				=> "real",
     	"crval2" 				=> "real",
     	"ctype1" 				=> "char",
     	"ctype2" 				=> "char",
     	"npfocusp" 				=> "real",
     	"npxtiltp" 				=> "real",
     	"npytiltp" 				=> "real",
     	"fomxpos" 				=> "real",
     	"fomypos" 				=> "real",
    	 "nfxtiltp" 				=> "real",
   	 "nfytiltp" 				=> "real"
    );     



# parse out decision variable and file; load file into array and close file
	$doit = param('db_insert');

	$FILE = param( 'filename' );
		@file_line = <$FILE>;
		close ($FILE);

	$decide = param('query_decision');
	#print "decide = $decide <br>";







######################################################################
#                 check which query       		 	      #
#######################################################################


#print "I'm here in Master<br>";
print "\n";




#section #1
sub which_query {
#print "i'm in which_query<br>";

	foreach $variable (@form_variables_base) {


#check on the query decision to determine the flow
	if ($variable eq "query_decision") {
		

	  print qq~
	  <HTML>
	  <HEAD>
	  <TITLE>Nicmos PSF Library</TITLE>
	  </HEAD>
	  <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
	~;



###CASE 1  (query on obs name)    
   if ($decide eq "Submit") {

		if ($variable = 'rootname') { 
               						
		  $value = param($variable); 
                  $value=~tr/a-z/A-Z/;
 		  $ff=length($value);#print "ff=$ff\n";
		  &obs_name_sub;
		  $cmmd = qq|select * from nicmos_psf where
		           rootname='$value'|;
		}
		
		
###CASE 2   (query on date)   
  } elsif ($decide eq "Query on Date") {


		if ($variable = 'date_time_obs') {
		  $value = param($variable);
    		  $joe = validate ($value);
			if ($joe ne 'date') {
				print "Invalid date format\n"; 
				exit;}

			$cmmd = qq|select * from nicmos_psf 
			where date_time_obs='$value'|;

		}

###CASE 3 (query on everything else)     
   } elsif ($decide eq "Submit Query") {

	$cmmd = qq|select * from nicmos_psf where |;

#print "I'm here in Submit Query<br>";
		undef $where_clause;

		foreach $key (@just_parameters_incoming_from_form_variables) {

		  $value = param($key);
		  $value=~tr/a-z/A-Z/;  # set all to uppercase

   		 	if ($value) {
			$where_clause .= " and " if $where_clause;

			$value2 = $value;
			$value2 = "'$value2'" unless $value2 =~ 
	  		  /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
		
	#set $value with quotes unless tested out to be non-character

#print "key=$key<br>";

	
if ($value2 eq "\'ANY\'") {
	    $where_clause = $where_clause . "1 = 1";
	    
} elsif ($key eq 'min_date_time_obs') {
	$where_clause .= "date_time_obs >= $value2";
} elsif ($key eq 'max_date_time_obs') {	
	$where_clause .= "date_time_obs <= $value2";
} elsif ($key eq 'min_pkcount') {
	$where_clause .= "pkcount >= $value2";
} elsif ($key eq 'max_pkcount') {	
	$where_clause .= "pkcount <= $value2";
} elsif ($key eq 'min_totcount') {
	$where_clause .= "totcount >= $value2";
} elsif ($key eq 'max_totcount') {	
	$where_clause .= "totcount <= $value2";
} elsif ($key eq 'min_exptime') {
	$where_clause .= "exptime >= $value2";
} elsif ($key eq 'max_exptime') {	
	$where_clause .= "exptime <= $value2";
} elsif ($key eq 'min_npfocusp') {
	$where_clause .= "npfocusp >= $value2";
} elsif ($key eq 'max_npfocusp') {	
	$where_clause .= "npfocusp <= $value2";
} elsif ($key eq 'min_temperature') {
	$where_clause .= "temperature >= $value2";
} elsif ($key eq 'max_temperature') {	
	$where_clause .= "temperature <= $value2";
} elsif ($key eq 'nic1_filter') {
	$where_clause .= "camera = 1 and filter=$value2";
} elsif ($key eq 'nic2_filter') {
	$where_clause .= "camera = 2 and filter=$value2";
} elsif ($key eq 'nic3_filter') {
	$where_clause .= "camera = 3 and filter=$value2";		
	
} elsif ($x_y_flag eq 'YES') {

	$where_clause .= "x between $x_delta_minus and $x_delta_plus
	and y between $y_delta_minus and $y_delta_plus";
					
	} else {

	$where_clause .= "$key = $value2";

	}

   			 } # end of if (exists)
		} #end foreach

	
		$where_clause = "1 = 1" unless $where_clause;

		$cmmd = $cmmd . $where_clause;

      } else {exit;}
      
  print qq~

  </BODY>
  </HTML>~;	
	}
}

$cmmd;

}



######################################################################
#                 check min_max inputs       		 	      #
#######################################################################




#section #2
sub min_max {
#print "i'm in min_max<br>";

	#check on 
	$value1 = param('min_date_time_obs');
	$value2 = param('max_date_time_obs');
	$value3 = param('x');
	$value4 = param('y');
	$value5 = param('x_y_radius');
	$value6 = param('min_pkcount');
	$value7 = param('max_pkcount');
	$value8 = param('min_totcount');
	$value9 = param('max_totcount');
	$value10 = param('min_exptime');
	$value11 = param('max_exptime');
	$value12 = param('min_npfocusp');
	$value13 = param('max_npfocusp');
	$value14 = param('min_temperature');
	$value15 = param('max_temperature');
	$value16 = param('nic1_filter');
	$value17 = param('nic2_filter');
	$value18 = param('nic3_filter');

#print "x=$value3<br>";
#print "y=$value4<br>";
#print "radius=$value5<br>";


#date
	if (!$value1 && $value2) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a minimum date"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}

	if ($value1 && !$value2) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a maximum date"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}

#x,y,radius	
		if (!$value3 && $value4 && $value5) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a x value"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}

	if ($value3 && !$value4 && $value5) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a y value"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}

	if ($value3 && $value4 && !$value5) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a radius for x and y"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}
	
	if ($value3 && !$value4 && !$value5) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a radius and y with this x value"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}	
	
	if (!$value3 && $value4 && !$value5) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a radius and x with this y value"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}	
		
	if (!$value3 && !$value4 && $value5) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a y and x with this radius value"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}	

#range of values x,y,and delta-x and delta-y

if ($value3) {

	if ($value3 lt 0.0 || $value3 gt 256.0) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You given an X value that is out of range
		(0.0-256.0)"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}
}

if ($value4) {
	if ($value4 lt 0.0 || $value4 gt 256.0) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You given an Y value that is out of range
		(0.0-256.0)"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}
}

				
#IF ALL THREE VALUES FOR X/Y/RADIUS ARE HERE, CALCULATE THE OFFSETS

  if ($value3 && $value4 && $value5) {

	$x_y_flag = YES;
	$x_delta_plus = $value3 + $value5;
	
	#print "xplus = $x_delta_plus<br>";

	$x_delta_minus = $value3 - $value5;
	
	$y_delta_plus = $value4 + $value5;

	$y_delta_minus = $value4 - $value5;
	

  }	


#boundaries on x,y,including radius

	if ($value3 && $value4 && $value5) {


	if ($x_delta_plus > 256.0) 
	{print "You're x+radius is out of bounds (>256.0)<br>";
	 exit;
	 }
	 
	if ($x_delta_minus < 0.0) 
	{print "You're x-radius is out of bounds (<0.0)<br>";
	 exit;
	 }
	 
	if ($y_delta_plus > 256.0) 
	{print "You're y+radius is is out of bounds (>2048)<br>";
	 exit;
	 }
	 
	if ($y_delta_minus < 0.0) 
	{print "You're y-radius is out of bounds (<0.0)<br>";
	 exit;
	 }
	}
	
	
#min_pkcount,max_pkcount

	if (!$value6 && $value7) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a min_pkcount"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}

	if ($value6 && !$value7) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a max_pkcount"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}
	
#min_totcount,max_totcount

	if (!$value8 && $value9) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a min_totcount"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}

	if ($value8 && !$value9) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a max_totcount"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}

#min_exptime,max_exptime

	if (!$value10 && $value11) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a min_exptime"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}

	if ($value10 && !$value11) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a max_exptime"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}

#min_npfocusp,max_npfocusp

	if (!$value12 && $value13) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a min_npfocusp"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}

	if ($value12 && !$value13) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a max_npfocusp"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}

#min_temperature,max_temperature

	if (!$value14 && $value15) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a min_temperature"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}


	if ($value14 && !$value15) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You must enter a max_temperature"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}

#only allow one camera/filter

	if ($value16 && $value17) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You can have only one camera/filter selection"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}
	
		if ($value16 && $value18) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You can have only one camera/filter selection"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}

		if ($value17 && $value18) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You can have only one camera/filter selection"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}
	
		if ($value16 && $value17 && $value18) {
 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "Error: You can have only one camera/filter selection"; 
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  
	}	



}

######################################################################
#                 observation name subroutine     	 	      #
#######################################################################




#section #3
sub obs_name_sub {

       if ($ff < 9 || $ff >9) { 
               print qq~
               <HTML>
               <HEAD>
               <TITLE>Results in writing to the database</TITLE>
               </HEAD>
               <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
               ~;      
               print "Observation names must be nine characters long\n";
               print qq~

               </BODY>
               </HTML>~;
               exit;					
       }
}


######################################################################
#                 no input subroutine  for query     	 	      #
#######################################################################

####checks for no input, all parameters



#section #4
sub no_input {
#print "i'm in no input, <br>";

	$flag=1;
	#undef $flag;

	foreach $variable (@incoming_from_form_variables) {

   #   	$valuejj=param($variable);
#	print "variable=$variable,valuejj=$valuejj<br>";



 	 next if (param($variable) eq "");
 	 $flag = 0;
   	 last;
 
	#}

	} #end of foreach
	
#print "flag=$flag<br>";

		if ($flag) {
 	 	   &print_no_input_sub;
		}

} #end of sub





####checks for no input, disregarding rootname
sub no_input_just_parameters {

#print "i'm in just parameters,no input, <br>";

	$flag=1;
	#undef $flag;

	foreach $variable (@just_parameters_incoming_from_form_variables) {

      	$valuejj=param($variable);
  #	print "variable=$variable,valuejj=$valuejj<br>";



 	 next if (param($variable) eq "");
 	 $flag = 0;
   	 last;
 
	#}

	} #end of foreach
	
#print "flag=$flag<br>";

		if ($flag) {
 	 	   &print_no_input_sub;
		}

} #end of sub



sub print_no_input_sub {

 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form - Required Fields</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "You didn't supply any input<br>"; 
		#print "print_no_input_sub<br>";
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}


#######################################################################
#                 subroutine validate                    	      #
#######################################################################
# VALIDATE -- Test a data type validation subroutine
#section #5




sub validate {
    my $value = shift;

    # Remove leading and trailing whitespace and quotes from the strings
#print "im in validate<br>";
    $value =~ s/^\s+//;
    $value =~ s/\s+$//;
    $value =~ s/"//g;

    # Start with most restrictive pattern and go to least
    # Numeric patterns from Perl cookbook, 2.1
    # Date allows two digit year

    return 'int' if 
      $value =~ /^[+-]?\d+$/;
    return 'real' 
      if $value =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
    return 'date'
      if $value =~ /^+(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\s+\d\d?\s+\d\d\d?\d?$/i;
    return 'char';
}





#######################################################################
#                 no input subroutine       		 	      #
#######################################################################


#section #7
sub no_input_input {

	if ($doit eq "Send File of Datasets"){
		if (param( 'filename' ) eq ""){
			
			&print_no_file_name_sub;

		} else { return;}


	} elsif ($doit eq "Submit Input"){

		$flag=1;

		foreach $variable (@required_variables) {
   		 	next if (param($variable) eq "");
   		 	$flag = 0;
    			last;
 		}

			if ($flag) {
 
 	 	 		&print_no_input_sub;
	  		#print "no_input_input<br>";

			} #end if (flag)
	
	} #end if ($doit eq

} #end of sub no_input

sub print_no_file_name_sub {

 		 print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Error in Processing Form - Required Fields</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;
                print "You didn't supply any input<br>"; 
		#print "print_no_input_sub<br>";
       		print qq~

 		 </BODY>
  		</HTML>~;
		exit;  

	}
#######################################################################
#                 check input                            	      #
#######################################################################



#section #8
sub check {

#check file input, create hash with required fields and values, and then run thru 
#char,int,real check routines

#print "I'M IN CHECK<br>";

  if ($doit eq "Send File of Datasets"){ 

	foreach $line_blue (@file_line) { 
		chop $line_blue;
		$line_blue=~tr/a-z/A-Z/;
#print "input_line=$line_blue<br>";
		@value_blue = split (/,\s*/, $line_blue); #removes comma and whitespace...pushes it into an array
		@value_blue = map (quote_literals ($_), @value_blue); #sub that quotes characters
#print "value_blue=@value_blue<br>";
		foreach $name_blue (@required_variables) { #populates a hash with name/value
								#foreach2
		$value_blue = shift @value_blue;

		$hash{$name_blue} = $value_blue;
		}

	foreach $name (keys %hash) { 

#print "NNNName=$name,HHHHashname= $hash{$name}<br>";

 		if ($name eq "rootname") {    #check rootname length .... 
 			$ff=length($hash{$name});
			#   print "RRRRRrootname=$hash{$name}<br>";
			  # print "ff=$ff<br>";

#cat'ing the names together to show at the end
# that those names were written to the database....
		if($save_name) {
 			$save_name = $save_name . "," . $hash{$name};
 		} else {
 			$save_name = $hash{$name};
 		}	   
			#print "SSSSSSSSSSSSSSSSsave_name= $save_name<br>";





 			if ($ff < 11 || $ff > 11) { 
 				print "Observation names must be nine characters long\n";
				print "Nothing has been written to the database\n"; 
 			}
 		}

		#print "HHHHHHHHHHHHHHHHHHHHHhashname=$hash{$name}<br>";
	if ($hash{$name} eq 'NULL') {
		#print "BBBBBBBBBB<br><br>";
	 $required_variable_type{$name} = $joe;
	} else {
 	$joe = validate ($hash{$name}); #send to validate and check all values
#print "JJJJoe=$joe,HHHHashname= $hash{$name},NNNName=$name<br>";
	}




#if ($name eq "focus") {

# print "name=$name,datatype=$required_variable_type{$name},value=$hash{$name}<br>";
 
#}

#if ($name eq "date_time_obs") {

#print "name=$name,datatype=$required_variable_type{$name},value=$hash{$name}<br>";
 
#}


		#print "HHHH$required_variable_type{$name},$name,$joe<br>";

 		if ($required_variable_type{$name} ne $joe) { 
		$exit_flag=yes;
		print qq~
 		 <HTML>
 		 <HEAD>
  		 <TITLE>Results in writing to the database</TITLE>
 		 </HEAD>
 		 <BODY BGCOLOR = "FFFFFF" TEXT = "000000">
		~;		
		#print "joe=$joe<br>";
 			print "Bad value for $name=$hash{$name}<br>"; 
			print "Nothing has been written to the database<br>"; 
		     		print qq~

 		 </BODY>
  		</HTML>~;
		
		
			
			} #else {print "everything is okkkkkkk!!!!!!\n";} 
			 
		
	} #end foreach3
  } #end if1
  
			if ($exit_flag eq "yes") {exit;}

#form input, single row entry

     } elsif ($doit eq "Submit Input"){
	foreach $name (@required_variables) {

	    if ($name eq "rootname") {    #check rootname length .... 
 
 		$ff=length(param($name));
  		if ($ff < 9 || $ff >9) { 

		&print_8_char_error_sub;
			 
 		}
 	    }

		$value = param($name);
		print "value=$value\n";
		$joe = validate ($value);
	
		if ($required_variable_type{$name} ne $joe) {
		
			&print_bad_value_sub;
	
		} 
		
	}	
	
  
   } #end elsif ($doit eq "Submit Input")

return;

} #end sub check




#######################################################################
#                 get input from a file and build query    	      #
#######################################################################

#section #9
sub file_list {


	foreach $element_red (@file_line) {
#print "element=$element_red<br>";
		@value_red = split (/,\s*/, $element_red); #
		@value_red = map (quote_literals ($_), @value_red);
		$value_red = join (', ', @value_red);
	#	print "insert into nicmos_psf values ($value_red)\n";
		push (@cmds,"insert into nicmos_psf values ($value_red)\n");

	}

	@input_cmd = @cmds;
	return @input_cmd;

} #end sub file_list.=========================================






#######################################################################
#                 subroutine quote literals             	      #
#######################################################################

#section #10
	sub quote_literals {
	    $_ = shift;
            $_ =~ s/\s+$//;
	    if ($_ eq 'NULL') {return $_;}
	    $_ = "\"$_\""unless /^-?\d+\.?\d*$/;
	    return $_;
	} 







#######################################################################
#                               The End                  	      #
#######################################################################



	





