#!/usr/bin/perl 

&parse_form;
@fields=keys %Form;

# -------------------------
#default

$cgi	= "http://hea-www.harvard.edu/ChaMPlane/cgi-bin/x-ray4pub/getsrc.pl";
#$rdb	= $Form{"rdb"} 		if defined $Form{"rdb"};
# -------------------------

$rdb = "/proj/ChaMP/ChaMPlane/WWW/data/xray/xmaster.rdb";
open(RDB, $rdb);
@column =("None");
&get_column_name;
$ncolumn = $#column+1 if $ncolumn > $#column+1;
close(RDB);

$sort_option = "";
foreach (@column) {
	if (/dist/) {
		$sort_option .= "<option selected>$_</option>\n";
	} else {
		$sort_option .= "<option>$_</option>\n";
	}
}

print <<EOF;
Content-type: text/html

<HTML>
<HEAD>
</HEAD>
<BODY>
              
<p>

<form action="$cgi" method="get" target="contents">         
<b>Select sources by coordinates</b> <br>
Enter the <a href="guide.html#coord" target="contents">coordinate<a/> and searching radius. <p>

<input type="radio" name="wcs" value="galactic">Galactic<br>
<i>l</i> (&deg;) <br> <input name="gl" type="text" method="get"> <br>
<i>b</i> (&deg;) <br> <input name="gb" type="text" method="get"> <p>

<input type="radio" checked name="wcs" value="j2000">J2000<br>
R.A. (&deg; or h:m:s) <br> <input name="ra" type="text" method="get"> <br>
Dec (&deg; or d:m:s) <br> <input name="dec" type="text" method="get"> <p>

Radius (<input type="radio" checked name="unit" value="arcmin">&prime;
<input type="radio" name="unit" value="arcsec">&Prime;)
<br><input name="radius" type="text" method="get" value="1"> <p>


<a href="guide.html#level" target="contents"> Level</a> <select name="level">
	<option>0</option>
	<option selected>1</option>
	<option>2</option>
	<option>3</option>
</select> <br>
Sort? <select name="sort">$sort_option</select><br>
Reverse? <input type="checkbox" name="reverse" value="1">

<p>
<a href="guide.html#display" target="contents">format?</a><br>
<input type="radio" name="format" value="rdb">rdb 
<input type="radio" checked name="format" value="txt">txt 
<input type="radio" name="format" value="list">list 
<input type="radio" name="format" value="html">html <p>


<p><input type="submit">                
<input type="reset" name="Reset"> <p>

Click <a href="http://hea-www.harvard.edu/ChaMPlane/data_archive.html" target="_parent">here</a>
to go back to the main page of data archive.

</form>
</body>
</html>
EOF
exit;

sub get_column_name{			# check current line, $_
	while(<RDB>){
		next if /^\s*$/ ;	# skip blank lines
    		next if( /^\s*\#/ );	# comment line
    		$lln++ ;  		# logical line nr
		last if( $lln > 1 );
#		while( s/\s*(\S+)// ) {
		while( s/\t*([^\t]+)// ) {
			 $x = $1 ;
    			push( @column, $x ) ;
			#print $x . "\n";
    		}
	}
}

sub parse_form {
	if ($ENV{'REQUEST_METHOD'} eq 'GET') {
	        @pairs = split(/&/, $ENV{'QUERY_STRING'});
	} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        	@pairs = split(/&/, $buffer);
    	}
   	foreach $pair (@pairs) {
      		($name, $value) = split(/=/, $pair);
      		$value =~ tr/+/ /;
      		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      		$Form{$name} = $value;
   	}
}

