<?
$pre   = '';
include "common.php";
$title = "US GLOBEC Personnel";
$logo  = true;
include HEAD;
?>
      <form method="get" action="<?=$_SERVER['PHP_SELF']?>">
      <table align="center">
      <tr>
         <td>&nbsp;</td>
         <td align="left" colspan="3">
            <input type="text" name="s" value="<?=stripslashes($_GET[s])?>" size="30" />
            <input type="submit" name="q" value="Search" />
         </td>
      </tr>
      <tr>
         <td>
            Limit seach to: 
         </td>
         <td>
            <input type="checkbox" name="nep" <?if($_GET['nep']=="on")echo "CHECKED";?> /> NEP<br />
            <input type="checkbox" name="so" <?if($_GET['so']=="on")echo "CHECKED";?> /> Southern Ocean<br />
            <input type="checkbox" name="gb" <?if($_GET['gb']=="on")echo "CHECKED";?> /> Georges Bank<br />
            <input type="checkbox" name="ssc" <?if($_GET['ssc']=="on")echo "CHECKED";?> /> SSC<br />
         </td>
         <td>
            <input type="checkbox" name="exec_committee" <?if($_GET['exec_committee']=="on")echo "CHECKED";?> /> Executive Committee<br />
            <input type="checkbox" name="ex_officio" <?if($_GET['ex_officio']=="on")echo "CHECKED";?> /> Ex-Officio<br />
            <input type="checkbox" name="staff" <?if($_GET['staff']=="on")echo "CHECKED";?> /> Staff<br />
            <input type="checkbox" name="agency_contact" <?if($_GET['agency_contact']=="on")echo "CHECKED";?> /> Agency Contacts<br />
         </td>
      </tr>
      </table>
      </form>
<?
$cat = array( 'nep'=>'on','so'=>'on','gb'=>'on','ssc'=>'on','exec_committee'=>'on',
              'ex_officio'=>'on','staff'=>'on','agency_contact'=>'on');
$tbname = TABLE_PERSONNEL;

if(isset($_GET['s'])){
   $data = $_GET;
   if($data['s'] == "*" || $data['s'] == ""){
      $data['s'] = "%";
   }
   $suf1 = '';
   $suf2 = '';
   $terms=preg_split ("/(\s+|\+)/", $data['s']);
   $num=count($terms);
   $tmp = array_intersect_assoc($cat,$data); // Search limiting
   for($i=0;$i<$num;$i++){
      $term = addslashes($terms[$i]);
      $suf1.="last_name LIKE '%$term%' OR first_name LIKE '%$term%' OR institute LIKE '%$term%' OR ";
   }

   // compile search limiting terms
   foreach($tmp as $key => $value){
      $suf2 .= $key . "='Y' OR ";
   }

   $suf1=substr($suf1,0,-4); // chop last ' OR ' off
   $suf2=substr($suf2,0,-4); // chop last ' OR ' off

   if(empty($suf2)) // No-limit seacrch
      $query1="SELECT * FROM $tbname WHERE (" . $suf1 . ") ORDER BY last_name";
   else // Limited Search
      $query1="SELECT * FROM $tbname WHERE (" . $suf1 . ") AND (" . $suf2 . ") ORDER BY last_name";
} // if(!empty($_GET[s])){

if(!empty($query1)){
   echo '<br /><a href="' . MEMBERSHIP . '"><b>View All</b></a>' . "\n";
   $result=$db->sql_query($query1);
   if(!$db->sql_numrows($result)){
      echo '<br /><h2>Sorry, there are no results for your query.</h2><br />' . "\n";
   }
   else{
      echo "<br /><br />\n";
   
      // Get all the First letters of last names
      for($i=0;$row=$db->sql_fetchrow($result);$i++){
         $names[$i] = strtoupper(substr($row[last_name],0,1));
         $info[$i]  = $row;
      }
   
      echo display_index($names);

      $anchor="";
      $info = array_reverse($info);
      while( $row=array_pop($info) ){
         if( ($a=strtoupper(substr($row[last_name],0,1))) != $anchor){
            $anchor=$a;
            echo "\n" . '<a name="' . $anchor . '"></a>' . "\n";
            //echo $i;
         }
         echo '<p style="text-indent: -20px; padding-left: 20px">' . "\n";
         if(!empty($row[position])){
            echo '<b>' . $row['last_name'] . ', ' . $row['first_name'] . '</b> (' . $row['position'] . ")<br />\n";
         }
         else{
            echo '<b>' . $row['last_name'] . ', ' . $row['first_name'] . "</b><br />\n";
         }
         echo $row['institute'] . "<br />\n";
         echo $row['phone'] . "<br />\n";
         if(!empty($row[fax])){
            echo $row[fax] . " (fax)<br />\n";
         }
         echo '<a href="mailto:' . $row['email'] . '">' . $row['email'] . "</a>\n";
         echo "</p>\n";
      }
   } // else
} // if(!empty($query1))

/*
$i = '<br />
      <a href="#a">A</a> | <a href="#b">B</a> | <a href="#c">C</a> | <a href="#d">D</a> | <a href="#e">E</a> | 
      <a href="#f">F</a> | <a href="#g">G</a> | <a href="#h">H</a> | <a href="#i">I</a> | <a href="#j">J</a> | 
      <a href="#k">K</a> | <a href="#l">L</a> | <a href="#m">M</a> | <a href="#n">N</a> | <a href="#o">O</a> | 
      <a href="#p">P</a> | <a href="#q">Q</a> | <a href="#r">R</a> | <a href="#s">S</a> | <a href="#t">T</a> | 
      <a href="#u">U</a> | <a href="#v">V</a> | <a href="#w">W</a> | <a href="#x">X</a> | <a href="#y">Y</a> | 
      <a href="#z">Z</a><br />';

echo $i;
*/
else{
   $query="SELECT last_name,first_name,position,institute,phone,fax,email FROM $tbname ORDER BY last_name";
   $result=$db->sql_query($query);
   echo "<br />\n";

   // Get all the First letters of last names
   for($i=0;$row=$db->sql_fetchrow($result);$i++){
      $names[$i] = strtoupper(substr($row[last_name],0,1));
      $info[$i]  = $row;
   }

   echo display_index($names);

   $anchor="";
   $info = array_reverse($info);
   while( $row=array_pop($info) ){
      if( ($a=strtoupper(substr($row[last_name],0,1))) != $anchor){
         $anchor=$a;
         echo "\n" . '<a name="' . $anchor . '"></a>' . "\n";
         //echo $i;
      }
      echo '<p style="text-indent: -20px; padding-left: 20px">' . "\n";
      if(!empty($row[position])){
         echo '<b>' . $row['last_name'] . ', ' . $row['first_name'] . '</b> (' . $row['position'] . ")<br />\n";
      }
      else{
         echo '<b>' . $row['last_name'] . ', ' . $row['first_name'] . "</b><br />\n";
      }
      echo $row['institute'] . "<br />\n";
      echo $row['phone'] . "<br />\n";
      if(!empty($row[fax])){
         echo $row[fax] . " (fax)<br />\n";
      }
      echo '<a href="mailto:' . $row['email'] . '">' . $row['email'] . "</a>\n";
      echo "</p>\n";
   }
} // else

include FOOT;
?>
