Index: /trunk/php-testing/conequery-basic.php
===================================================================
--- /trunk/php-testing/conequery-basic.php	(revision 79)
+++ /trunk/php-testing/conequery-basic.php	(revision 79)
@@ -0,0 +1,587 @@
+<?php	session_start(); ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
+<?php
+	// conequery-basic.php
+	// Matthew Whiting June 18 2007
+	// 
+	// A test of a conesearch query for new data.
+	// Using Albert Teoh's query.php as a template
+?>
+<head>
+<link rel="stylesheet" type="text/css" href="../../styles/style.css" />
+<link rel="stylesheet" type="text/css" href="../../styles/results.css" />
+<link rel="stylesheet" type="text/css" href="../../styles/query.css" />
+
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8759-1" />
+<meta name="DC.Creator" lang="en" content="personalName=Teoh,Albert" />
+<meta name="ATNF.Maintainer" content="contactName=Webmaster,the" />
+<meta name="DC.Titlepri" lang="en" content="The ATNF Pulsar Archive Searcher" />
+<meta name="DC.Title.alt" lang="en" content="" />
+<meta name="DC.Subject" lang="en" content="pulsar" />
+<meta name="DC.Description" lang="en" content="Search the observation data archives at the ATNF" />
+<meta name="DC.Date.created" scheme="ISO8601" content="2002-12-12" />
+<meta name="ATNF.Site" content="Marsfield" />
+<meta name="ATNF.AccessPermission" content="Public" />
+<meta name="ATNF.MetaVersion" content="1.0" />
+
+<script type="text/javascript" src="../../scripts/nodomws.js"></script>
+<script type="text/javascript" src="../../scripts/atnf-css-utils.js"></script>
+<script type="text/javascript" src="../../scripts/atnf-dom-utils.js"></script>
+<script type="text/javascript" src="../../scripts/results.js"></script>
+<script type="text/javascript">
+function submit_form()
+{
+  document.frmResults.submit();
+}
+</script>
+<?php include_once("../../header.html"); ?>
+</head>
+<title>ATNF Pulsar Data Archive Query Results</title>
+
+<body>
+<!-- Start Content -->
+
+<?php
+	include_once("../../database.php");
+	include_once("../../utils.php");
+
+	// List of request parameters to display
+	$params           = get_param('params', $HTTP_GET_VARS);	
+	// The current state. E.g cart or results
+	// Need to maintain this because other actions could be performed without changing state 
+	$state            = get_param('state', $HTTP_GET_VARS);
+	// The selected action to perform. E.g. add observations to cart, or view profile
+	$actions          = get_param('actions', $HTTP_GET_VARS);
+	// List of checked observations. Note: Not necessarily observations in the cart.
+	// Cart observations are stored in $cart_items variable
+	$selected_obs              = get_param('selected_obs', $HTTP_GET_VARS);
+	// cart_items is an array of numbers that index to the list of 
+	// results in all_results
+	$cart_items          = $_SESSION['cart_items'];
+
+	$raj_degs         = trim(get_param('raj_degs', $HTTP_GET_VARS));
+	$decj_degs        = trim(get_param('decj_degs', $HTTP_GET_VARS));
+	$radius           = trim(get_param('radius', $HTTP_GET_VARS));
+
+///////////////////////////////////////////////////////////////////////////
+
+		$link = db_connect_admin();
+
+$createTable = "CREATE TEMPORARY TABLE s70 (file_id SMALLINT UNSIGNED AUTO_INCREMENT,filename VARCHAR(30),fileloc VARCHAR(100),hdrver VARCHAR(10),src_name VARCHAR(20),raj VARCHAR(15),decj VARCHAR(15),rajd FLOAT,decjd FLOAT,bmaj FLOAT,bmin FLOAT,bpa FLOAT,date_obs VARCHAR(30),obsfreq FLOAT,chanbw FLOAT,nchan FLOAT,scanlen FLOAT,tbin FLOAT,PRIMARY KEY (file_id))";
+
+$result = mysql_query($createTable) or die('Query failed #1: ' . mysql_error());
+
+$loadTable = "LOAD DATA LOCAL INFILE \"/DATA/SITAR_1/whi550/VO/PulsarVO/s70v_summary.csv\" INTO TABLE s70 FIELDS TERMINATED BY \",\" OPTIONALLY ENCLOSED BY \"\\\"\" IGNORE 1 LINES (filename,fileloc,hdrver,src_name,raj,decj,rajd,decjd,bmaj,bmin,bpa,date_obs,obsfreq,chanbw,nchan,scanlen,tbin)";
+
+$result = mysql_query($loadTable) or die('Query failed #2: ' . mysql_error());
+
+///////////////////////////////////////////////////////////////////////////
+
+	if (!isset($cart_items)) {
+		$cart_items = array();
+	}
+	//////////////////////////////////////////
+	// Initialise the output heading format
+	
+	$heading = "<th>&nbsp;</th><th>#</th>";
+	$raj_exists = false;
+	$decj_exists = false;
+	$raj_heading = "";
+	$decj_heading = "";
+	$keyheading = "";
+	
+	if (!isset($params) && !isset($_SESSION['results'])) {
+		echo "No parameters were selected. Please select at least one.<br>\n";
+		exit;
+	}
+	
+	// Check if RA and Dec were queried
+	$flipped_parameters = array_flip($params);
+	
+	if (array_key_exists('raj', $flipped_parameters)) {
+		$raj_heading = "<th>RAJ</th>";
+		unset($flipped_parameters['raj_hour, raj_minute, raj_second']);
+		$raj_exists = true;
+	}
+	if (array_key_exists('decj', $flipped_parameters)) {
+		$decj_heading = "<th>DecJ</th>";
+		unset($flipped_parameters['decj_degree, decj_minute, decj_second']);
+		$decj_exists = true;
+	}
+	
+	foreach (array_keys($flipped_parameters) as $key) {
+		if (eregi("raj_", $key)) {
+			$raj_heading = "<th>RAJ</th>";
+			$raj_exists = true;
+		}
+		
+		else if (eregi("decj_", $key)) {
+			$decj_heading = "<th>DecJ</th>";
+			$decj_exists = true;
+		}
+		else {
+//			$heading .= "<th>$key</th>";
+			$keyheading .= "<th>$key</th>";
+		}
+	}
+	
+//	$heading .= $raj_heading.$decj_heading;
+//	$heading .= "<th>Distance</th>";
+//	$heading .= $raj_heading.$decj_heading."<th>Distance</th>".$keyheading;
+	$heading .= $keyheading."<th>Distance</th>";
+
+		$whereString = "";
+
+		////////////////////////////
+		// Constraint on boundary
+
+	        $angsep_str = "";
+		if ($radius) {
+
+	               $angsep_str = "degrees( acos( cos(radians(rajd-$raj_degs))*cos(radians(decjd))*cos(radians($decj_degs))+sin(radians(decjd))*sin(radians($decj_degs))) )";
+
+	               $whereString .= "($angsep_str - 0.25*(bmaj+bmin)) < $radius";
+
+		}
+
+
+		// Remove any leading or trailing AND and OR
+		$whereString = trim($whereString, " AND OR");
+
+		$query = "SELECT *,$angsep_str as angsep FROM s70 WHERE $whereString ORDER by angsep";
+
+		//echo "query = $query<br>\n";
+
+		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
+
+		$all_results = array();
+
+		while ($line = mysql_fetch_assoc($result)) {
+			array_push($all_results, $line );
+		}
+		
+		$_SESSION['results'] = $all_results;
+	
+		mysql_free_result($result);
+
+	
+		mysql_close($link);
+
+	// Make a copy of it to pass to another function
+	$params_cpy = $params;
+
+	// Get the number of rows and report
+	$num_rows = count($all_results);
+	
+	// Calculate the total size of observations cart
+	$total_cart_size_bytes = 0;
+	
+	foreach ($cart_items as $row) {
+		$total_cart_size_bytes += $all_results[$row]['file_size_bytes'];		
+	}
+	
+	if ($state == 'cart') {
+		$num_display_rows = count($cart_items);
+	}
+	else {
+		$num_display_rows = $num_rows;
+	}
+	
+	if (!(isset($startRange) && isset($endRange))) {
+		$startRange = 1;
+		
+		
+		if ($endRange < MAX_DISPLAY_ROWS) {
+			$endRange = $num_display_rows;
+		}
+		else {
+			$endRange = MAX_DISPLAY_ROWS;
+		}
+	}
+	?>
+
+<form action="<?php echo $PHP_SELF; ?>" method="GET" id="frmResults">
+<input type="hidden" name="startRange" value="<?php echo $startRange;?>">
+<input type="hidden" name="endRange" value="<?php echo $endRange;?>">
+
+<!--
+<br>
+<br>Params = <?php echo $params?><br>
+<br>Query = <?php echo $query ?><br>
+<br>Number of results = <?php echo $num_rows ?> <br>
+<br>
+-->
+
+<table class="top_menu" border="0" id="tabQueryResults">
+	<?php if ($state == 'cart') { ?>
+		<tr class="top_menu"><td class="top_menu_heading" colspan="4"><h2>Observations Cart</h2></td></tr>
+	<?php } else if ($state == 'results') { ?>
+		<tr class="top_menu"><td class="top_menu_heading" colspan="4"><h2>Query Results</h2></td></tr>
+	<?php } ?>
+
+		<tr class="top_menu"><td class="top_menu"  align="left" width="30%"><a class="top_menu" href="index.html?command=logout">Logout</a> | 
+		<a class="top_menu" href="change_password.html?prev_page=<?php echo $PHP_SELF;?>">Change Password</a> |
+		<a class="top_menu" href="index.html?actions=new_query">New Query</a>
+</td>
+		<td class="top_menu" width="50%" align="center">
+			Actions: <select name="actions">
+		<?php if ($state == 'cart') { ?>
+
+			<?php 
+				if ($total_cart_size_bytes/1000000 < MAX_DOWNLOAD_MB) {
+					echo "<option>".DOWNLOAD_ALL_OBS_CALS."</option>\n";
+					echo "<option>".DOWNLOAD_ALL_OBS."</option>\n";
+					echo "<option>".DOWNLOAD_ALL_CALS."</option>\n";
+				}
+				else {
+					echo "<option disabled>Download limit ".MAX_DOWNLOAD_MB."MB exceeded</option>\n";
+				}
+			?>
+			</option>
+			<option><?php echo REMOVE_FROM_CART; ?></option>
+			</select>
+			<input type="submit" value="Perform action">
+			</td>
+			<td class="top_menu" align="right"><b><?php echo count($cart_items); ?></b>
+			Item/s (<?php printf("%.1f MB", $total_cart_size_bytes/1000000);?>)</td>
+			<td class="top_menu" align="right" width="1%"><a href="<?php echo $PHP_SELF;?>?state=results&startRange=1&endRange=<?php echo MAX_DISPLAY_ROWS; ?>">
+			<img alt="Revert to query results" border="0" title="Revert to query results" src="images/table_30px_white.png"></img></a>
+			</td>
+		<?php } else { ?>
+			<option><?php echo ADD_TO_CART; ?></option>
+			<option>----- Profile Viewing Options -----</option>
+			<option><?php echo VIEW_PROFILES; ?></option>
+			<?php 
+			   echo "cals = $cals<br>\n";
+			   if (isset($cals) && $cals != "") {
+				echo "<option>".VIEW_PROFILES_CALS."</option>\n";
+			   }
+			?>
+			</select>
+			<input type="submit" value="Perform action">
+			</td>
+			<td class="top_menu" align="right" width="20%"><b><?php echo count($cart_items); ?></b>
+			<a href="<?php echo $PHP_SELF;?>?state=cart&startRange=1&endRange=<?php echo count($cart_items); ?>">Item/s</a> (<?php printf("%.1f MB", $total_cart_size_bytes/1000000);?>)</td>
+			<td class="top_menu" align="right" width="1%"><a href="<?php echo $PHP_SELF;?>?state=cart&startRange=1&endRange=<?php echo count($cart_items); ?>">
+			<img alt="View observation cart" border="0" title="View observation cart" src="images/cart_30px_white.png"></img></a>
+			</td>
+		<?php } ?>
+		</tr></table>
+<?php	
+
+	if ($num_display_rows == 0) {
+		echo "<center>Sorry, no results matched your search</center><br>\n";
+		echo "<center><a href=\"index.html\">Search again</a></center><br>\n";
+		exit();
+	}
+	
+	echo "<center>\n";
+	printNavigate($startRange, $num_display_rows);
+	echo "</center>\n";				
+	
+	
+	if ($num_display_rows < $endRange) {
+		echo "Showing <b>$startRange - $num_display_rows</b> of <b>$num_display_rows</b> rows\n";
+	}
+	else {
+		echo "Showing <b>$startRange - $endRange</b> of <b>$num_display_rows</b> rows\n";
+	}
+	
+	showTable($startRange-1, $endRange-1, $heading, $all_results, $params_cpy, $cals, $cals_within_mins);
+	
+
+?>
+</form>
+</body>
+</html>
+
+
+<?php
+
+///////////////////////////////////////////////////////////////////////////////////////////////
+//
+// Function definitions
+// --------------------
+//
+///////////////////////////////////////////////////////////////////////////////////////////////
+function printNavigate($startRange, $num_rows) {
+
+	// Collect the form values
+	global $params, $pulsar_name,	$data_type,
+	$raj,	$decj,$radius, $from_day,	$from_month, $from_year,
+	$to_day, $to_month, $to_year, $state, $cals;
+	
+	if (isset($params)) {
+		$params_str = array2urlstring("params", $params);
+	}
+		
+	$num_pages = ceil($num_rows/MAX_DISPLAY_ROWS);
+	
+	$startPage = ($startRange-1)/MAX_DISPLAY_ROWS;
+	
+	$startPageRange = floor($startPage / MAX_DISPLAY_PAGES) * MAX_DISPLAY_PAGES;
+	
+	// Print the previous button
+	if ($startPageRange > 0) {
+		$prevStartRange = ($startPageRange - 1)*MAX_DISPLAY_ROWS + 1;
+		$prevEndRange = $prevStartRange + MAX_DISPLAY_ROWS - 1;
+		echo "<a href=\"$PHP_SELF?startRange=$prevStartRange&endRange=$prevEndRange&$params_str&cals=$cals&pulsar_name=$pulsar_name&data_type=$data_type&raj=$raj&decj=$decj&radius=$radius&from_day=$from_day&from_month=$from_month&from_year=$from_year&to_day=$to_day&to_month=$to_month&to_year=$to_year&state=$state\"><< Prev 10</a> &nbsp;\n";
+	}
+	
+	for ($i=$startPageRange; $i < $num_pages && $i - $startPageRange <= MAX_DISPLAY_PAGES; $i++) {
+		$start = $i*MAX_DISPLAY_ROWS + 1;
+		$end = $start + MAX_DISPLAY_ROWS - 1;
+		
+		if ($end > $num_rows) {
+			$end = $num_rows;
+		}
+		
+		if ($startRange == $start) {
+			echo "<b>".($i+1)."</b> &nbsp;\n";
+		}
+		// When the page index has reached the maximum range, print the next button
+		else if ($i - $startPageRange == MAX_DISPLAY_PAGES ) {
+			echo " <a href=\"$PHP_SELF?startRange=$start&endRange=$end&$params_str&cals=$cals&pulsar_name=$pulsar_name&data_type=$data_type&raj=$raj&decj=$decj&radius=$radius&from_day=$from_day&from_month=$from_month&from_year=$from_year&to_day=$to_day&to_month=$to_month&to_year=$to_year&state=$state\">Next 10 >></a> \n";
+		
+		}
+		else {
+			echo " <a href=\"$PHP_SELF?startRange=$start&endRange=$end&$params_str&cals=$cals&pulsar_name=$pulsar_name&data_type=$data_type&raj=$raj&decj=$decj&radius=$radius&from_day=$from_day&from_month=$from_month&from_year=$from_year&to_day=$to_day&to_month=$to_month&to_year=$to_year&state=$state\">".($i+1)."</a> &nbsp;\n";
+		}
+	}
+	
+}
+
+function showTable($startRange, $endRange, $heading, $all_results, $params, $cals, $cals_within_mins) {
+	global $state, $cart_items, $obs_cals;
+	
+	//echo "showTable: state = $state, startRange = $startRange, endRange = $endRange<br>\n";
+	
+	$link = db_connect();
+
+	// Printing results in HTML
+	echo "<table cellpadding=\"5\">\n";
+
+	echo "<thead class=\"file-headings\">
+					<tr>
+						$heading
+					</tr>
+				</thead>\n";
+	
+	if (!isset($params)) {
+		echo "No params were selected. Please select at least one.<br>\n";
+	}
+	
+	$index = $startRange;
+	
+	// For each row in the observations results, print it out as a table
+	// and append any matching cals if requested
+	// 
+	// obs_cals is a hash table with key = the index of the observation
+	// and value = an array of cal results
+	for ($i=$startRange; ($i < count($all_results) && $index <= $endRange); $i++) {
+
+		$row_html_unchecked = "\t<td class=\"result_row\">
+					<input id=\"chkSelect-$i\"  type=\"checkbox\" name=\"selected_obs[]\" value=\"$i\" />
+					<span class=\"glyph\" id=\"spaToggle-$i\"></span>
+					</td>\n";
+	
+		$row_html_checked = "\t<td class=\"result_row\">
+					<input id=\"chkSelect-$i\" type=\"checkbox\" name=\"selected_obs[]\" value=\"$i\" checked=\"true\" />
+					<span class=\"glyph\" id=\"spaToggle-$i\"></span>
+					</td>\n";
+
+		$line = $all_results[$i];
+		
+		// Print the checkbox column
+		if ($state == 'cart') {
+			if (in_array($i, $cart_items)) {
+				if ($index % 2 == 0) {
+					echo "<tbody class=\"even\">\n";
+				}
+				else {
+					echo "<tbody class=\"odd\">\n";
+				}
+	  			echo "\t<tr>\n";
+				echo $row_html_checked;
+
+				// Makes the values of array the keys (index)
+				// Guaranteed that values are unique so it's OK
+				//$flipped_cart = array_flip($cart_items);
+				
+				//$cart_index = $flipped_cart[$i];
+			}
+			else {
+				continue;
+			}
+		}
+		else {			
+			if ($index % 2 == 0) {
+				echo "<tbody class=\"even\">\n";
+			}
+			else {
+				echo "<tbody class=\"odd\">\n";
+			}
+	  	
+			echo "\t<tr>\n";
+
+			if (in_array($i, $cart_items)) {
+				echo $row_html_checked;
+			}
+			
+			else {
+				echo $row_html_unchecked;
+			}
+		}
+		
+		// Query for cals
+		$all_cals = array();
+		
+		if (isset($cals) && $cals != "") {
+			$num_params = count($params);
+			
+			//echo "\t<tr class=\"extra-detail\">\n";
+			
+			$all_cals = query_cals($line['pulsar_name'], $line['MJD'], $line['data_type'], $cals_within_mins);
+			
+			// Set the obs_cals array
+			if (count($obs_cals) == 0) {
+				$obs_cals = array($index => $all_cals);
+			}
+			else {
+				array_push($obs_cals, $all_cals);
+			}
+			
+			//echo "\t</tr>\n";
+		}
+		
+		
+		///////////////////
+		// Display the row
+		echo "\t<td class=\"result_row\" align=\"right\">".($index+1)."</td>\n";	
+		
+  		foreach ($params as $param) {
+			if (eregi("raj_", $param) || eregi("decj_", $param)) {
+				continue;
+			}
+
+			print_param($line, $param);
+
+     			echo "</td>\n";
+  		}
+		//print_r($params);
+		
+//		print_ra_dec($line, $params);
+		
+		print_angsep($line, $params);
+			
+//		echo "\t</tr>\n";
+		
+//		print_cals_row($all_cals, $params);
+			
+		// End the row
+		
+		echo "\t</tbody>\n";
+
+		//echo "Printing obs_cals<br><pre>\n";
+		//print_r($obs_cals);
+		//echo "</pre>\n";
+
+		$index++;
+	}
+	echo "</table>\n";
+	
+	// By now all cals for each observation have been determined, so set the obs_cals session variable
+	$_SESSION['obs_cals'] = $obs_cals;
+	
+	mysql_close($link);
+}
+
+function print_ra_dec_angsep ($result_row, $params) {
+
+	if (in_array('rajd', $params)) {
+	    $ra = $result_row['rajd'];
+            echo "\t\t<td class=\"result_row\">$ra</td>\n";
+        } 
+	if (in_array('decjd', $params)) {
+	    $dec = $result_row['decjd'];
+            echo "\t\t<td class=\"result_row\">$dec</td>\n";
+        } 
+
+        $dist = $result_row['angsep'];
+        echo "\t\t<td class=\"result_row\">$dist</td>\n";
+
+
+}
+
+function print_ra_dec ($result_row, $params) {
+
+	if (in_array('rajd', $params)) {
+	        $raj_hours  = $result_row['rajd'] / 15.;
+	        $raj_hour   = floor($raj_hours);	
+	        $raj_minute = floor(($raj_hours-$raj_hour)*60.);
+	        $raj_second = ($raj_hours-$raj_hour-$raj_minute/60.)*3600.;
+ 
+		$raj_str = sprintf("%02d",$raj_hour).":".sprintf("%02d",$raj_minute).":". sprintf("%02.3f",$raj_second);
+		echo "\t\t<td class=\"result_row\">$raj_str</td>\n";
+	}
+
+	if (in_array('decjd', $params)) {
+                $decj_degs = abs($result_row['decjd']);
+	        $decj_dd   = intval($decj_degs);	
+	        $decj_mm   = intval(($decj_degs-$decj_dd)*60.);
+	        $decj_ss   = ($decj_degs-$decj_dd-$decj_mm/60.)*3600.;
+		$decj_str  = sprintf("%02d",$decj_dd).":".sprintf("%02d",$decj_mm).":". sprintf("%02.3f",$decj_ss);
+	        if($result_row['decjd'] < 0.) $decj_str = "-".$decj_str;
+		echo "\t\t<td class=\"result_row\">$decj_str</td>\n";
+	}
+
+}
+
+function print_angsep ($result_row, $params) {
+
+        $dist = $result_row['angsep'];
+	$dist_str = sprintf("%03.4f",$dist);
+        echo "\t\t<td class=\"result_row\">$dist_str</td>\n";
+
+}
+
+function print_param($result_row, $param) {
+	/////// Perform formatting specific to the data ///////
+
+	if ($result_row[$param] == "" ) {
+		echo "\t\t<td class=\"result_row\">";
+		echo "&nbsp;";
+	}
+	else {
+		switch ($param) {
+		case OBSFREQ:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.3f", $result_row[$param]);
+			break;
+		case MJD:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.6f", $result_row[$param]);
+			break;
+		case CHANBW:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.3f", $result_row[$param]);
+			break;
+		case SCANLEN:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.3f", $result_row[$param]);
+			break;
+		case NCHAN:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%d", $result_row[$param]);
+			break;
+		case TBIN:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.6f", $result_row[$param]);
+			break;
+		default:
+			echo "\t\t<td class=\"result_row\">";
+			echo $result_row[$param];
+		}
+	}
+}
+?>
Index: /trunk/php-testing/conequery.php
===================================================================
--- /trunk/php-testing/conequery.php	(revision 79)
+++ /trunk/php-testing/conequery.php	(revision 79)
@@ -0,0 +1,876 @@
+<?php	session_start(); ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
+<?php
+	// conequery.php
+	// Matthew Whiting June 18 2007
+	// 
+	// A test of a conesearch query for new data.
+	// Using Albert Teoh's query.php as a template
+?>
+<head>
+<link rel="stylesheet" type="text/css" href="styles/style.css" />
+<link rel="stylesheet" type="text/css" href="styles/results.css" />
+<link rel="stylesheet" type="text/css" href="styles/query.css" />
+
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8759-1" />
+<meta name="DC.Creator" lang="en" content="personalName=Teoh,Albert" />
+<meta name="ATNF.Maintainer" content="contactName=Webmaster,the" />
+<meta name="DC.Titlepri" lang="en" content="The ATNF Pulsar Archive Searcher" />
+<meta name="DC.Title.alt" lang="en" content="" />
+<meta name="DC.Subject" lang="en" content="pulsar" />
+<meta name="DC.Description" lang="en" content="Search the observation data archives at the ATNF" />
+<meta name="DC.Date.created" scheme="ISO8601" content="2002-12-12" />
+<meta name="ATNF.Site" content="Marsfield" />
+<meta name="ATNF.AccessPermission" content="Public" />
+<meta name="ATNF.MetaVersion" content="1.0" />
+
+<script type="text/javascript" src="../../scripts/nodomws.js"></script>
+<script type="text/javascript" src="../../scripts/atnf-css-utils.js"></script>
+<script type="text/javascript" src="../../scripts/atnf-dom-utils.js"></script>
+<script type="text/javascript" src="../../scripts/results.js"></script>
+<script type="text/javascript">
+function submit_form()
+{
+  document.frmResults.submit();
+}
+</script>
+<!-- <?php include_once("../../header.html"); ?> -->
+</head>
+<title>ATNF Pulsar Data Archive Query Results</title>
+
+<body>
+<!-- Start Content -->
+
+<?php
+	
+	include_once("../../constants.php");
+	include_once("../../database.php");
+	include_once("../../utils.php");
+	include_once("../../date.php");
+	include_once("../../security.php");
+		
+	// Processes the form and queries the database on the given parameters
+	//echo "username = " . $_SESSION['username'] . " authcode = " . $_SESSION['auth_code'] . " <br>\n";
+	
+	
+//	if ( !( (isset($_SESSION['username']) && isset($_SESSION['auth_code'])) &&
+//	         $_SESSION['auth_code'] == OK)) {
+//		
+//		echo "<center>Your session has expired due to inactivity. Please <a href=\"index.html\">log in</a>.</center>\n";
+//		exit();
+//	}
+	
+	// Clean out any old variables
+	init();
+	
+	///////////////////////////
+	// Collect the form values
+	
+	// List of request parameters to display
+	$params           = get_param('params', $HTTP_GET_VARS);	
+	
+	// The current state. E.g cart or results
+	// Need to maintain this because other actions could be performed without changing state 
+	$state            = get_param('state', $HTTP_GET_VARS);
+	
+	// The selected action to perform. E.g. add observations to cart, or view profile
+	$actions          = get_param('actions', $HTTP_GET_VARS);
+	
+	// List of checked observations. Note: Not necessarily observations in the cart.
+	// Cart observations are stored in $cart_items variable
+	$selected_obs              = get_param('selected_obs', $HTTP_GET_VARS);
+	
+	// A boolean value to indicate whether if the user wants to view cals as well
+	$cals             = get_param('cals', $HTTP_GET_VARS);
+	
+	// an array mapping observation number (corresponding to the index in all_results) 
+	// to an array of matching cals
+	$obs_cals = array();
+		
+	// Display cals within this many minutes of the observation
+	$cals_within_mins = get_param('cals_within_mins', $HTTP_GET_VARS);
+	
+	// cart_items is an array of numbers that index to the list of 
+	// results in all_results
+	$cart_items          = $_SESSION['cart_items'];
+	
+	
+	// this holds all the query results of observations as an array of
+	// associative arrays. E.g.
+	// array ( 0 => ( pulsar_name => 'J0437-4715',
+	//                DM          => 10,
+	//                period      => 1
+	//              ),
+	//
+	//         1 => (pulsar_name => 'J0437-4715',
+	//                DM          => 27,
+	//                period      => 1.112
+	//              )
+	//       )
+	//
+	if (isset($_SESSION['results'])) {
+		$all_results = $_SESSION['results'];
+	}
+	
+	
+	// Other form parameters. Pretty self explanatory?
+	$pulsar_name      = trim(get_param('pulsar_name', $HTTP_GET_VARS));
+	$data_type        = trim(get_param('data_type', $HTTP_GET_VARS));
+	$raj              = trim(get_param('raj', $HTTP_GET_VARS));
+	$decj             = trim(get_param('decj', $HTTP_GET_VARS));
+	$radius           = trim(get_param('radius', $HTTP_GET_VARS));
+	$from_day         = get_param('from_day', $HTTP_GET_VARS);
+	$from_month       = get_param('from_month', $HTTP_GET_VARS);
+	$from_year        = get_param('from_year', $HTTP_GET_VARS);
+	$to_day           = get_param('to_day', $HTTP_GET_VARS);	
+	$to_month         = get_param('to_month', $HTTP_GET_VARS);
+	$to_year          = get_param('to_year', $HTTP_GET_VARS);
+	
+	$startRange  = $HTTP_GET_VARS['startRange'];
+	$endRange = $HTTP_GET_VARS['endRange'];	
+	
+	$tar_file = "";
+	
+	///////////////////////
+	// Perform actions
+	//
+	// Precondition: The query has already been made, so the list of cals foreach observation
+	// has already been determined ($_SESSION['obs_cals']).
+	
+	if (isset($actions) && $actions != "") {
+		switch ($actions) {
+
+			case DOWNLOAD_ALL_OBS_CALS:
+
+				if (isset($cart_items) && isset($all_results)) {
+					//echo "Download all<br>\n";
+					$tar_file = tarObsCalFiles($cart_items, $all_results, $cals_within_mins);
+					echo " <meta http-equiv=\"REFRESH\" content=\"0; URL=".WEB_DOWNLOADS_DIR."/$tar_file\">\n";
+				}
+
+				break;
+				
+			case DOWNLOAD_ALL_OBS:
+
+				if (isset($cart_items) && isset($all_results)) {
+					//echo "Download all<br>\n";
+					$tar_file = tarObsFiles($cart_items, $all_results);
+					echo " <meta http-equiv=\"REFRESH\" content=\"0; URL=".WEB_DOWNLOADS_DIR."/$tar_file\">\n";
+				}
+
+				break;
+			
+			case DOWNLOAD_ALL_CALS:
+
+				if (isset($cart_items) && isset($all_results)) {
+					//echo "Download all<br>\n";
+					$tar_file = tarCalFiles($cart_items, $all_results, $cals_within_mins);
+					echo " <meta http-equiv=\"REFRESH\" content=\"0; URL=".WEB_DOWNLOADS_DIR."/$tar_file\">\n";
+				}
+
+				break;
+			
+			case VIEW_PROFILES:
+				
+				if (isset($selected_obs) && isset($all_results)) {
+					
+					// redirect to the view_profile page and pass through the selected_obs and obs_cals arrays
+					$_SESSION['selected_obs'] = $selected_obs;
+					$_SESSION['display_cals'] = false;
+					
+					include_once("view_profiles.php");
+					exit;
+				}
+
+				break;
+			
+			case VIEW_PROFILES_CALS:
+				
+				if (isset($selected_obs) && isset($all_results)) {
+					
+					// redirect to the view_profile page and pass through the selected_obs and obs_cals arrays
+					$_SESSION['selected_obs'] = $selected_obs;
+					$_SESSION['display_cals'] = true;
+					
+					include_once("view_profiles.php");
+					exit;
+				}
+
+				break;
+			
+			case REMOVE_FROM_CART:
+				if (isset($selected_obs)) {
+					if ($actions == REMOVE_FROM_CART) {
+						$cart_items = array_diff($cart_items, $selected_obs);
+					}
+				}
+				break;
+
+			case ADD_TO_CART:
+				if (isset($selected_obs)) {
+					foreach ($selected_obs as $ob) {
+						if (!in_array($ob, $cart_items)) {
+							array_push($cart_items, $ob);
+						}
+					}
+				}
+				break;
+		}
+	}
+	
+	if (!(isset($startRange) && isset($endRange))) {
+		
+		if ($HTTP_GET_VARS['submit_button'] == 'submit' || !(isset($_SESSION['start_range']) || isset($_SESSION['end_range']))) {
+			$startRange = 1;
+			$endRange = MAX_DISPLAY_ROWS;
+		}
+	
+		else {
+			$startRange = $_SESSION['start_range'];
+			$endRange = $_SESSION['end_range'];
+		}
+		
+	}
+	
+	if (!isset($cart_items)) {
+		$cart_items = array();
+	}
+	
+	
+	
+	$_SESSION['cart_items'] = $cart_items;
+	$_SESSION['start_range'] = $startRange;
+	$_SESSION['end_range'] = $endRange;
+	
+	//print_r($params);
+	//print_r ($selected_obs);
+	//print_r($_SESSION['cart_items']);
+	//echo "Number of elements in result array = " . count($_SESSION['results']);
+	//echo "<br>pulsar_name = $pulsar_name, data_type = $data_type
+	//      raj = $raj, decj = $decj, from: day = $from_day, month = $from_month
+	//	year = $from_year, to: day = $to_day, month = $to_month, year = $to_year<br>\n";
+	
+	//////////////////////////////////////////
+	// Initialise the output heading format
+	
+	$heading = "<th>&nbsp;</th><th>#</th>";
+	$raj_exists = false;
+	$decj_exists = false;
+	$raj_heading = "";
+	$decj_heading = "";
+	
+//	if (!isset($params) && !isset($_SESSION['results'])) {
+//		echo "No parameters were selected. Please select at least one.<br>\n";
+//		exit;
+//	}
+	
+	// Check if RA and Dec were queried
+	$flipped_parameters = array_flip($params);
+	
+	if (array_key_exists('raj_hour, raj_minute, raj_second', $flipped_parameters)) {
+		$raj_heading = "<th>RAJ</th>";
+		unset($flipped_parameters['raj_hour, raj_minute, raj_second']);
+		$raj_exists = true;
+	}
+	if (array_key_exists('decj_degree, decj_minute, decj_second', $flipped_parameters)) {
+		$decj_heading = "<th>DecJ</th>";
+		unset($flipped_parameters['decj_degree, decj_minute, decj_second']);
+		$decj_exists = true;
+	}
+	
+	foreach (array_keys($flipped_parameters) as $key) {
+		if (eregi("raj_", $key)) {
+			$raj_heading = "<th>RAJ</th>";
+			$raj_exists = true;
+		}
+		
+		else if (eregi("decj_", $key)) {
+			$decj_heading = "<th>DecJ</th>";
+			$decj_exists = true;
+		}
+		else {
+			$heading .= "<th>$key</th>";
+		}
+	}
+	
+	$heading .= $raj_heading.$decj_heading;
+
+	if (!isset($_SESSION['results']) || $HTTP_GET_VARS['submit_button'] == 'submit') {
+		
+$createTable = "CREATE TEMPORARY TABLE s70 (file_id SMALLINT UNSIGNED AUTO_INCREMENT,filename VARCHAR(30),fileloc VARCHAR(100),hdrver VARCHAR(10),src_name VARCHAR(20),raj VARCHAR(15),decj VARCHAR(15),rajd FLOAT,decjd FLOAT,bmaj FLOAT,bmin FLOAT,bpa FLOAT,date_obs VARCHAR(30),obsfreq FLOAT,chanbw FLOAT,nchan FLOAT,scanlen FLOAT,tbin FLOAT,PRIMARY KEY (file_id));"
+
+//echo "hello world.<br>";
+//echo "query = $createTable<br>\n";
+
+$result = mysql_query($createTable) or die('Query failed: ' . mysql_error());
+
+$loadTable = "LOAD DATA LOCAL INFILE \"/DATA/SITAR_1/whi550/VO/PulsarVO/s70v_summary.csv\" INTO TABLE s70 FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY \"\\\"\" IGNORE 1 LINES (filename,fileloc,hdrver,src_name,raj,decj,rajd,decjd,bmaj,bmin,bpa,date_obs,obsfreq,chanbw,nchan,scanlen,tbin);"
+
+$result = mysql_query($loadTable) or die('Query failed: ' . mysql_error());
+
+	
+
+		/////////////////////////////////////////////////////
+		// Form the parameter string in the SQL SELECT clause
+		$parameters = $params[0];
+
+		for ($i=1; $i < count($params); $i++) {
+			$parameters .= ", " . $params[$i];
+		}
+
+		// remove any leading and trailing commas
+		$parameters = trim($parameters, ",");
+
+
+		///////////////////////////////////////
+		// Convert gregorian day to julian day
+		//
+
+		$from_gregorian = strtotime("$from_month/$from_day/$from_year");
+		$to_gregorian = strtotime("$to_month/$to_day/$to_year");
+
+		$from_mjd = cal_to_jd (CAL_GREGORIAN, $from_month,  $from_day, $from_year ) - 2400000.5;//cal_to_jd ( $from_day, $from_month, $from_year ) - 2400000.5;
+		$to_mjd = cal_to_jd ( CAL_GREGORIAN, $to_month, $to_day, $to_year ) - 2400000.5;	//cal_to_jd ( $to_day, $to_month, $to_year ) - 2400000.5;	
+		
+
+		/////////////////////////////////////////
+		// Check that the user hasn't queried for data younger than 18 months
+		//
+		
+		if (!atnf_staff($_SESSION['username'])) {
+			echo "from_mjd = $from_mjd and to_mjd = $to_mjd<br>\n";
+
+			$today_mjd = cal_to_jd ( CAL_GREGORIAN, date("n"), date("j"), date("Y") ) - 2400000.5;
+		
+			if ($today_mjd - $from_mjd < 730) { // 2 * 365 days = 730 days
+		
+				echo "You're asking for data which has not aged past the 18 month proprietary period<br>\n";
+				echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=index.html?actions=error\">\n";
+				exit;
+			}
+		
+			// Set the end mjd date to the 18 months prior to current date
+			if ( $to_mjd > ($today_mjd - 730)) {
+				$to_mjd = $today_mjd - 730;
+			}
+		}
+		
+		//////////////////////////
+		// Convert the coordinates
+		//
+//		$raj_degs = hms2degs($raj);
+//		$dec_degs = dms2degs($decj);
+
+		///////////////////////////////
+		// Set up the query string
+		//
+
+		$whereString = "";
+
+		////////////////////////////
+		// Constraint on boundary
+
+	        $angsep_str = "";
+		if ($radius) {
+
+	               $angsep_str = "degrees( acos( cos(radians(rajd-$raj_degs))*cos(radians(decjd))*cos(radians($decj_degs))+sin(radians(decjd))*sin(radians($raj_degs))) )";
+
+	               $whereString .= "($angsep_str - 0.25*(bmaj+bmin)) < $radius";
+
+		}
+
+		////////////////////////////
+		// Constraint on data type
+
+		if ($data_type) {
+
+			$whereString .= " AND data_type = '$data_type'";
+		}
+
+		///////////////////////////
+		// Constraint on pulsar name
+		if ($pulsar_name) {
+
+			$whereString .= " AND pulsar_name LIKE '%$pulsar_name%'";
+		}
+
+		//////////////////////
+		// Constraint on MJD
+
+		$whereString .= " AND (MJD < $to_mjd AND MJD > $from_mjd)";
+
+		///////////////////////////////
+		// Performing SQL query
+		//
+
+		$link = db_connect();
+
+		// Remove any leading or trailing AND and OR
+		$whereString = trim($whereString, " AND OR");
+
+//		$query = "SELECT * FROM ".OBSERVATIONS_TABLE." WHERE $whereString";
+		$query = "SELECT *,$angsep_str as angsep FROM s70 WHERE $whereString ORDER by angsep";
+
+		//echo "query = $query<br>\n";
+
+		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
+
+		$all_results = array();
+
+		while ($line = mysql_fetch_assoc($result)) {
+			array_push($all_results, $line );
+		}
+		
+		$_SESSION['results'] = $all_results;
+	
+		mysql_free_result($result);
+
+	
+		mysql_close($link);
+	
+	}
+	
+	// Make a copy of it to pass to another function
+	$params_cpy = $params;
+
+	// Get the number of rows and report
+	$num_rows = count($all_results);
+	
+	// Calculate the total size of observations cart
+	$total_cart_size_bytes = 0;
+	
+	foreach ($cart_items as $row) {
+		$total_cart_size_bytes += $all_results[$row]['file_size_bytes'];		
+	}
+	
+	if ($state == 'cart') {
+		$num_display_rows = count($cart_items);
+	}
+	else {
+		$num_display_rows = $num_rows;
+	}
+	
+	if (!(isset($startRange) && isset($endRange))) {
+		$startRange = 1;
+		
+		
+		if ($endRange < MAX_DISPLAY_ROWS) {
+			$endRange = $num_display_rows;
+		}
+		else {
+			$endRange = MAX_DISPLAY_ROWS;
+		}
+	}
+	?>
+<form action="<?php echo $PHP_SELF; ?>" method="GET" id="frmResults">
+<input type="hidden" name="startRange" value="<?php echo $startRange;?>">
+<input type="hidden" name="endRange" value="<?php echo $endRange;?>">
+
+
+<table class="top_menu" border="0" id="tabQueryResults">
+	<?php if ($state == 'cart') { ?>
+		<tr class="top_menu"><td class="top_menu_heading" colspan="4"><h2>Observations Cart</h2></td></tr>
+	<?php } else if ($state == 'results') { ?>
+		<tr class="top_menu"><td class="top_menu_heading" colspan="4"><h2>Query Results</h2></td></tr>
+	<?php } ?>
+
+		<tr class="top_menu"><td class="top_menu"  align="left" width="30%"><a class="top_menu" href="index.html?command=logout">Logout</a> | 
+		<a class="top_menu" href="change_password.html?prev_page=<?php echo $PHP_SELF;?>">Change Password</a> |
+		<a class="top_menu" href="index.html?actions=new_query">New Query</a>
+</td>
+		<td class="top_menu" width="50%" align="center">
+			Actions: <select name="actions">
+		<?php if ($state == 'cart') { ?>
+
+			<?php 
+				if ($total_cart_size_bytes/1000000 < MAX_DOWNLOAD_MB) {
+					echo "<option>".DOWNLOAD_ALL_OBS_CALS."</option>\n";
+					echo "<option>".DOWNLOAD_ALL_OBS."</option>\n";
+					echo "<option>".DOWNLOAD_ALL_CALS."</option>\n";
+				}
+				else {
+					echo "<option disabled>Download limit ".MAX_DOWNLOAD_MB."MB exceeded</option>\n";
+				}
+			?>
+			</option>
+			<option><?php echo REMOVE_FROM_CART; ?></option>
+			</select>
+			<input type="submit" value="Perform action">
+			</td>
+			<td class="top_menu" align="right"><b><?php echo count($cart_items); ?></b>
+			Item/s (<?php printf("%.1f MB", $total_cart_size_bytes/1000000);?>)</td>
+			<td class="top_menu" align="right" width="1%"><a href="<?php echo $PHP_SELF;?>?state=results&startRange=1&endRange=<?php echo MAX_DISPLAY_ROWS; ?>">
+			<img alt="Revert to query results" border="0" title="Revert to query results" src="images/table_30px_white.png"></img></a>
+			</td>
+		<?php } else { ?>
+			<option><?php echo ADD_TO_CART; ?></option>
+			<option>----- Profile Viewing Options -----</option>
+			<option><?php echo VIEW_PROFILES; ?></option>
+			<?php 
+			   echo "cals = $cals<br>\n";
+			   if (isset($cals) && $cals != "") {
+				echo "<option>".VIEW_PROFILES_CALS."</option>\n";
+			   }
+			?>
+			</select>
+			<input type="submit" value="Perform action">
+			</td>
+			<td class="top_menu" align="right" width="20%"><b><?php echo count($cart_items); ?></b>
+			<a href="<?php echo $PHP_SELF;?>?state=cart&startRange=1&endRange=<?php echo count($cart_items); ?>">Item/s</a> (<?php printf("%.1f MB", $total_cart_size_bytes/1000000);?>)</td>
+			<td class="top_menu" align="right" width="1%"><a href="<?php echo $PHP_SELF;?>?state=cart&startRange=1&endRange=<?php echo count($cart_items); ?>">
+			<img alt="View observation cart" border="0" title="View observation cart" src="images/cart_30px_white.png"></img></a>
+			</td>
+		<?php } ?>
+		</tr></table>
+<?php	
+
+	if ($num_display_rows == 0) {
+		echo "<center>Sorry, no results matched your search</center><br>\n";
+		echo "<center><a href=\"index.html\">Search again</a></center><br>\n";
+		exit();
+	}
+	
+	echo "<center>\n";
+	printNavigate($startRange, $num_display_rows);
+	echo "</center>\n";				
+	
+	
+	if ($num_display_rows < $endRange) {
+		echo "Showing <b>$startRange - $num_display_rows</b> of <b>$num_display_rows</b> rows\n";
+	}
+	else {
+		echo "Showing <b>$startRange - $endRange</b> of <b>$num_display_rows</b> rows\n";
+	}
+	
+	
+	// TODO: Is this a better way to do it?
+	//if (mysql_data_seek($result, $startRange);
+
+	// Show first few
+	//print_r($all_results);
+	showTable($startRange-1, $endRange-1, $heading, $all_results, $params_cpy, $cals, $cals_within_mins);
+	
+
+	echo "<center>\n";
+	printNavigate($startRange, $num_display_rows);
+	echo "</center>\n";
+	
+	//echo "count all results = " . count($all_results);
+
+
+?>
+</form>
+</body>
+</html>
+
+<?php
+
+///////////////////////////////////////////////////////////////////////////////////////////////
+//
+// Function definitions
+// --------------------
+//
+///////////////////////////////////////////////////////////////////////////////////////////////
+
+// The page navigator
+function printNavigate($startRange, $num_rows) {
+
+	// Collect the form values
+	global $params, $pulsar_name,	$data_type,
+	$raj,	$decj,$radius, $from_day,	$from_month, $from_year,
+	$to_day, $to_month, $to_year, $state, $cals;
+	
+	if (isset($params)) {
+		$params_str = array2urlstring("params", $params);
+	}
+		
+	$num_pages = ceil($num_rows/MAX_DISPLAY_ROWS);
+	
+	$startPage = ($startRange-1)/MAX_DISPLAY_ROWS;
+	
+	$startPageRange = floor($startPage / MAX_DISPLAY_PAGES) * MAX_DISPLAY_PAGES;
+	
+	// Print the previous button
+	if ($startPageRange > 0) {
+		$prevStartRange = ($startPageRange - 1)*MAX_DISPLAY_ROWS + 1;
+		$prevEndRange = $prevStartRange + MAX_DISPLAY_ROWS - 1;
+		echo "<a href=\"$PHP_SELF?startRange=$prevStartRange&endRange=$prevEndRange&$params_str&cals=$cals&pulsar_name=$pulsar_name&data_type=$data_type&raj=$raj&decj=$decj&radius=$radius&from_day=$from_day&from_month=$from_month&from_year=$from_year&to_day=$to_day&to_month=$to_month&to_year=$to_year&state=$state\"><< Prev 10</a> &nbsp;\n";
+	}
+	
+	for ($i=$startPageRange; $i < $num_pages && $i - $startPageRange <= MAX_DISPLAY_PAGES; $i++) {
+		$start = $i*MAX_DISPLAY_ROWS + 1;
+		$end = $start + MAX_DISPLAY_ROWS - 1;
+		
+		if ($end > $num_rows) {
+			$end = $num_rows;
+		}
+		
+		if ($startRange == $start) {
+			echo "<b>".($i+1)."</b> &nbsp;\n";
+		}
+		// When the page index has reached the maximum range, print the next button
+		else if ($i - $startPageRange == MAX_DISPLAY_PAGES ) {
+			echo " <a href=\"$PHP_SELF?startRange=$start&endRange=$end&$params_str&cals=$cals&pulsar_name=$pulsar_name&data_type=$data_type&raj=$raj&decj=$decj&radius=$radius&from_day=$from_day&from_month=$from_month&from_year=$from_year&to_day=$to_day&to_month=$to_month&to_year=$to_year&state=$state\">Next 10 >></a> \n";
+		
+		}
+		else {
+			echo " <a href=\"$PHP_SELF?startRange=$start&endRange=$end&$params_str&cals=$cals&pulsar_name=$pulsar_name&data_type=$data_type&raj=$raj&decj=$decj&radius=$radius&from_day=$from_day&from_month=$from_month&from_year=$from_year&to_day=$to_day&to_month=$to_month&to_year=$to_year&state=$state\">".($i+1)."</a> &nbsp;\n";
+		}
+	}
+	
+}
+
+function showTable($startRange, $endRange, $heading, $all_results, $params, $cals, $cals_within_mins) {
+	global $state, $cart_items, $obs_cals;
+	
+	//echo "showTable: state = $state, startRange = $startRange, endRange = $endRange<br>\n";
+	
+	$link = db_connect();
+
+	// Printing results in HTML
+	echo "<table cellpadding=\"5\">\n";
+
+	echo "<thead class=\"file-headings\">
+					<tr>
+						$heading
+					</tr>
+				</thead>\n";
+	
+	if (!isset($params)) {
+		echo "No params were selected. Please select at least one.<br>\n";
+	}
+	
+	$index = $startRange;
+	
+	// For each row in the observations results, print it out as a table
+	// and append any matching cals if requested
+	// 
+	// obs_cals is a hash table with key = the index of the observation
+	// and value = an array of cal results
+	for ($i=$startRange; ($i < count($all_results) && $index <= $endRange); $i++) {
+
+		$row_html_unchecked = "\t<td class=\"result_row\">
+					<input id=\"chkSelect-$i\"  type=\"checkbox\" name=\"selected_obs[]\" value=\"$i\" />
+					<span class=\"glyph\" id=\"spaToggle-$i\"></span>
+					</td>\n";
+	
+		$row_html_checked = "\t<td class=\"result_row\">
+					<input id=\"chkSelect-$i\" type=\"checkbox\" name=\"selected_obs[]\" value=\"$i\" checked=\"true\" />
+					<span class=\"glyph\" id=\"spaToggle-$i\"></span>
+					</td>\n";
+
+		$line = $all_results[$i];
+		
+		// Print the checkbox column
+		if ($state == 'cart') {
+			if (in_array($i, $cart_items)) {
+				if ($index % 2 == 0) {
+					echo "<tbody class=\"even\">\n";
+				}
+				else {
+					echo "<tbody class=\"odd\">\n";
+				}
+	  			echo "\t<tr>\n";
+				echo $row_html_checked;
+
+				// Makes the values of array the keys (index)
+				// Guaranteed that values are unique so it's OK
+				//$flipped_cart = array_flip($cart_items);
+				
+				//$cart_index = $flipped_cart[$i];
+			}
+			else {
+				continue;
+			}
+		}
+		else {			
+			if ($index % 2 == 0) {
+				echo "<tbody class=\"even\">\n";
+			}
+			else {
+				echo "<tbody class=\"odd\">\n";
+			}
+	  	
+			echo "\t<tr>\n";
+
+			if (in_array($i, $cart_items)) {
+				echo $row_html_checked;
+			}
+			
+			else {
+				echo $row_html_unchecked;
+			}
+		}
+		
+		// Query for cals
+		$all_cals = array();
+		
+		if (isset($cals) && $cals != "") {
+			$num_params = count($params);
+			
+			//echo "\t<tr class=\"extra-detail\">\n";
+			
+			$all_cals = query_cals($line['pulsar_name'], $line['MJD'], $line['data_type'], $cals_within_mins);
+			
+			// Set the obs_cals array
+			if (count($obs_cals) == 0) {
+				$obs_cals = array($index => $all_cals);
+			}
+			else {
+				array_push($obs_cals, $all_cals);
+			}
+			
+			//echo "\t</tr>\n";
+		}
+		
+		
+		///////////////////
+		// Display the row
+		echo "\t<td class=\"result_row\" align=\"right\">".($index+1)."</td>\n";	
+		
+				
+  		foreach ($params as $param) {
+			if (eregi("raj_", $param) || eregi("decj_", $param)) {
+				continue;
+			}
+
+			print_param($line, $param);
+
+     			echo "</td>\n";
+  		}
+		//print_r($params);
+		
+		print_ra_dec($line, $params);
+				
+		echo "\t</tr>\n";
+		
+		print_cals_row($all_cals, $params);
+			
+		// End the row
+		
+		echo "\t</tbody>\n";
+
+		//echo "Printing obs_cals<br><pre>\n";
+		//print_r($obs_cals);
+		//echo "</pre>\n";
+
+		$index++;
+	}
+	echo "</table>\n";
+	
+	// By now all cals for each observation have been determined, so set the obs_cals session variable
+	$_SESSION['obs_cals'] = $obs_cals;
+	
+	mysql_close($link);
+}
+
+function print_cals_row($all_cals, $params) {
+
+	for ($i=0; $i < count($all_cals); $i++) {
+		
+		$line = $all_cals[$i];
+		
+		echo "\t\t<tr class=\"result_row\">\n";
+		echo "\t\t<td class=\"result_row\" colspan=\"2\">&nbsp;</td>\n";
+
+  		foreach ($params as $param) {
+			if (eregi("raj_", $param) || eregi("decj_", $param)) {
+				continue;
+			}
+
+ 			print_param($line,$param);
+  		}
+		print_ra_dec($line, $params);
+					
+		echo "\t\t</tr>\n";	
+	}
+}
+
+function print_ra_dec ($result_row, $params) {
+
+	if (in_array('raj_hour,raj_minute,raj_second', $params)) {
+		$raj_str = sprintf("%02d",$result_row['raj_hour']).":".sprintf("%02d",$result_row['raj_minute']).":". sprintf("%02.3f",$result_row['raj_second']);
+		echo "\t\t<td class=\"result_row\">$raj_str</td>\n";
+	}
+
+	if (in_array('decj_degree,decj_minute,decj_second', $params)) {
+		$decj_str = sprintf("%02d",$result_row['decj_degree']).":".sprintf("%02d",$result_row['decj_minute']).":". sprintf("%02.3f",$result_row['decj_second']);
+		echo "\t\t<td class=\"result_row\">$decj_str</td>\n";
+	}		
+
+}
+
+function print_param($result_row, $param) {
+	/////// Perform formatting specific to the data ///////
+
+	if ($result_row[$param] == "" ) {
+		echo "\t\t<td class=\"result_row\">";
+		echo "&nbsp;";
+	}
+	else {
+		switch ($param) {
+		case PERIOD:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.16f", $result_row[$param]);
+			break;
+
+		case DM:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.5f", $result_row[$param]);
+			break;
+
+		case FREQUENCY:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.3f", $result_row[$param]);
+			break;
+		case MJD:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.6f", $result_row[$param]);
+			break;
+		case BW:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.3f", $result_row[$param]);
+			break;
+		case LENGTH:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.3f", $result_row[$param]);
+			break;
+		case NSUB:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%d", $result_row[$param]);
+			break;
+		case NBIN:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%d", $result_row[$param]);
+			break;
+		case FILESIZE:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%d", $result_row[$param]);
+			break;
+		case TSAMP:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%.6f", $result_row[$param]);
+			break;
+		case NBEAM:
+			echo "\t\t<td class=\"result_row\" align=\"right\">";
+			printf("%d", $result_row[$param]);
+			break;
+		default:
+			echo "\t\t<td class=\"result_row\">";
+			echo $result_row[$param];
+		}
+	}
+}
+
+// Initialise the input form variables
+function init() {
+	global $actions, $selected_obs;
+	
+	unset($actions);
+	unset($selected_obs);
+
+	unset($_SESSION['selected_obs']);
+	unset($_SESSION['actions']);
+}
+
+?>
Index: /trunk/php-testing/index.html
===================================================================
--- /trunk/php-testing/index.html	(revision 79)
+++ /trunk/php-testing/index.html	(revision 79)
@@ -0,0 +1,208 @@
+<?php	session_start(); ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
+<?php
+	// index.html
+	// Matthew Whiting June 18 2007
+	// 
+	// A test of a conesearch webpage for new data.
+	// Using Albert Teoh's index.html as a template
+/*
+<!-- BEGIN Javascript for expansion blocks -->
+<!-- Thanks to: -->
+<!-- http://assn9.blogspot.com/ -->
+<!-- Douglas Bowman
+     URL:      www.stopdesign.com
+     Used in: http://www.bloggerforum.com/modules/newbb/viewtopic.php?topic_id=8725&forum=6
+     Date:     26 Feb 2004
+-->
+*/
+?>
+<head>
+<link rel="stylesheet" type="text/css" href="../../styles/style.css" />
+<link rel="stylesheet" type="text/css" href="../../styles/query.css" />
+<link rel="stylesheet" type="text/css" href="../../styles/results.css" />
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8759-1" />
+<meta name="DC.Creator" lang="en" content="personalName=Teoh,Albert" />
+<meta name="ATNF.Maintainer" content="contactName=Webmaster,the" />
+<meta name="DC.Title.alt" lang="en" content="" />
+<meta name="DC.Subject" lang="en" content="pulsar" />
+<meta name="DC.Description" lang="en" content="Searches the data archives at the ATNF" />
+<meta name="DC.Date.created" scheme="ISO8601" content="2002-12-12" />
+<meta name="ATNF.Site" content="Marsfield" />
+<meta name="ATNF.AccessPermission" content="Public" />
+<meta name="ATNF.MetaVersion" content="1.0" />
+</head>
+<script type="text/javascript" language="javascript1.2"><!--
+function togglecomments (id1,id2) {
+
+		var obj1 = document.getElementById(id1);
+		var obj2 = document.getElementById(id2);
+		(obj1.className=="commentshown") ? obj1.className="commenthidden" : obj1.className="commentshown"; 
+		(obj1.className=="commentshown") ? obj2.innerHTML="<< Hide" : obj2.innerHTML="Show all >>"; 
+
+}
+
+function checkAll(objName, FormName, FieldName ) {
+
+	var obj = document.getElementsByName(objName);
+	var CheckValue = obj[0].checked;
+
+	if(!document.forms[FormName])
+		return;
+	var objCheckBoxes = document.forms[FormName].elements[FieldName];
+	if(!objCheckBoxes)
+		return;
+	var countCheckBoxes = objCheckBoxes.length;
+	if(!countCheckBoxes)
+		objCheckBoxes.checked = CheckValue;
+	else
+		// set the check value for all check boxes
+		for(var i = 0; i < countCheckBoxes; i++)
+			objCheckBoxes[i].checked = CheckValue;
+}
+
+function updateSelectAll(objId, allId) {
+	var obj = document.getElementById(objId);
+	var allCheckbox = document.getElementById(allId);	
+	
+	if (!objId.checked && allCheckbox.checked) {
+		allCheckbox.checked = false;
+	}
+		
+		
+}
+//--></script>
+
+<title>ATNF Pulsar Data Archive Testing</title>
+
+<!-- Start Content -->
+<?php
+	
+include_once("../../constants.php");
+include_once("../../security.php");
+include_once("../../database.php");
+include_once("../../date.php");
+
+$now_day = date("j");
+$now_month = date("n");
+$now_year = date("Y");
+
+$all_params_id = "all_params";
+$all_others_id = "all_others";
+
+//$link = db_connect();
+
+///////////////////////////////////////////////////////////////////////////
+
+		$link = db_connect_admin();
+
+$createTable = "CREATE TEMPORARY TABLE s70 (file_id SMALLINT UNSIGNED AUTO_INCREMENT,filename VARCHAR(30),fileloc VARCHAR(100),hdrver VARCHAR(10),src_name VARCHAR(20),raj VARCHAR(15),decj VARCHAR(15),rajd FLOAT,decjd FLOAT,bmaj FLOAT,bmin FLOAT,bpa FLOAT,date_obs VARCHAR(30),obsfreq FLOAT,chanbw FLOAT,nchan FLOAT,scanlen FLOAT,tbin FLOAT,PRIMARY KEY (file_id))";
+
+$result = mysql_query($createTable) or die('Query failed #1: ' . mysql_error());
+
+$loadTable = "LOAD DATA LOCAL INFILE \"/DATA/SITAR_1/whi550/VO/PulsarVO/s70v_summary.csv\" INTO TABLE s70 FIELDS TERMINATED BY \",\" OPTIONALLY ENCLOSED BY \"\\\"\" IGNORE 1 LINES (filename,fileloc,hdrver,src_name,raj,decj,rajd,decjd,bmaj,bmin,bpa,date_obs,obsfreq,chanbw,nchan,scanlen,tbin)";
+//"
+$result = mysql_query($loadTable) or die('Query failed #2: ' . mysql_error());
+
+//////////////////////////////////////////////
+// Get the field names to fill form checkboxes
+
+$query = "SHOW COLUMNS FROM s70";
+$result = mysql_query($query) or die ('Query failed: ' . mysql_error());
+
+$fields = array();
+if (mysql_num_rows($result)) {
+	while($row = mysql_fetch_assoc($result)) {
+		array_push($fields, $row);
+	}
+}
+
+///////////////////////////////////////////////////////////////////////////
+
+?>
+
+<body class="atoa">
+
+<form action="conequery-basic.php" method="GET" name="data_archive_form" id="data_archive_form">
+<input type="hidden" name="state" value="results">
+
+<table class="noborder">
+
+	<tr>
+	
+	<td valign="top" class="form_field" rowspan="1">
+	
+	<fieldset>
+		<legend>Display Parameters</legend>
+		<input type="checkbox" name="<?php echo $all_params_id; ?>" id="<?php echo $all_params_id; ?>" onClick="checkAll('<?php echo $all_params_id; ?>', 'data_archive_form', 'params[]')" checked="true">Select all<br>
+			<font size="-1"><a href="javascript:togglecomments('params', 'toggler1')" id="toggler1">Show all >></a></font><br>
+			<span class="commenthidden" id="params">
+			<div id="list">
+				<?php
+				// Special case for ra and dec
+				$raj_count = 0;
+				$decj_count = 0;
+				$raj_field = "";
+				$decj_field = "";
+
+				foreach ($fields as $field) {
+					if (ereg("raj_.+", $field['Field'] )) {
+						$raj_field .= "," . $field['Field'];
+						$raj_count++;
+						if ($raj_count == 3) {
+							$raj_field = trim($raj_field, " ,");
+							echo "<input onClick=\"updateSelectAll('ra',
+'$all_params_id')\" type=\"checkbox\" name=\"params[]\" value=\"$raj_field\" id=\"RA\" checked=\"true\">RA<br>\n";
+						}
+					}
+					else if (ereg("decj_.+", $field['Field'] ) ){
+						$decj_field .= "," . $field['Field'];
+						$decj_count++;
+						if ($decj_count == 3) {
+							$decj_field = trim($decj_field, " ,");
+							echo "<input onClick=\"updateSelectAll('dec', '$all_params_id')\" type=\"checkbox\" name=\"params[]\" value=\"$decj_field\" id=\"dec\" checked=\"true\">DecJ<br>\n";
+						}
+					}
+					else {
+						echo "<input
+onClick=\"updateSelectAll('".$field['Field']."', '$all_params_id')\" type=\"checkbox\" name=\"params[]\" value=\"".$field['Field']."\" id=\"".$field['Field']."\" checked=\"true\">".$field['Field']."<br>\n";
+					}
+				}
+				?>
+			</div>
+			</span>
+	</fieldset>
+	</td>
+
+	<td class="form_field" height="10%">
+	<!-- Filter Options -->
+	<fieldset id="flsPosition">
+				
+		<legend>Filter Options</legend>
+
+
+		<!-- Coordinates -->
+		<fieldset>
+			<legend>Coordinates</legend>
+
+			RAJ [deg]: <input type="text" name="raj_degs" size="10">
+			DecJ [deg]: <input type="text" name="decj_degs" size="10">
+			Search radius [deg]: <input type="text" name="radius" size="10">
+
+
+		</fieldset>	
+
+	</fieldset>
+	<br>
+	<input type="submit" value="submit" name="submit_button">
+	</td>
+	</tr>	
+</table>
+
+
+</form>
+
+
+</body>
+
+</html>
Index: /trunk/php-testing/loadS70.php
===================================================================
--- /trunk/php-testing/loadS70.php	(revision 79)
+++ /trunk/php-testing/loadS70.php	(revision 79)
@@ -0,0 +1,17 @@
+<?php	
+
+session_start();
+
+$createTable = "CREATE TEMPORARY TABLE s70 (file_id SMALLINT UNSIGNED AUTO_INCREMENT,filename VARCHAR(30),fileloc VARCHAR(100),hdrver VARCHAR(10),src_name VARCHAR(20),raj VARCHAR(15),decj VARCHAR(15),rajd FLOAT,decjd FLOAT,bmaj FLOAT,bmin FLOAT,bpa FLOAT,date_obs VARCHAR(30),obsfreq FLOAT,chanbw FLOAT,nchan FLOAT,scanlen FLOAT,tbin FLOAT,PRIMARY KEY (file_id));"
+
+echo "hello world.<br>";
+echo "query = $createTable<br>\n";
+
+$result = mysql_query($createTable) or die('Query failed: ' . mysql_error());
+
+$loadTable = "LOAD DATA LOCAL INFILE \"/DATA/SITAR_1/whi550/VO/PulsarVO/s70v_summary.csv\" INTO TABLE s70 FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY \"\\\"\" IGNORE 1 LINES (filename,fileloc,hdrver,src_name,raj,decj,rajd,decjd,bmaj,bmin,bpa,date_obs,obsfreq,chanbw,nchan,scanlen,tbin);"
+
+$result = mysql_query($loadTable) or die('Query failed: ' . mysql_error());
+
+?>
+
