<?
$pre   = '';
include $pre . 'common.php';
$title = 'Model Entry Page';
$logo  = false;
include HEAD;

$data=$_POST;

if( isset($data['submit']) ){
   $success = false;
   $insert = '';
   foreach( $data as $key=>$value ){
      if( $key == 'var_u' || $key == 'var_v' || $key == 'var_w'
          || $key == 'var_T' || $key == 'var_S' || $key == 'var_rho' ){
         $insert .= ($value == 'on') ? "$key='Y'," : '';
      }elseif( $key != "preview" && $key != "submit" ){
         $value   = ($value == '<--Select One-->') ? '' : $value;
         if( $value != '' ){
            $insert .= "$key='${value}',";
         }
      }else{
         ;
      }
   } // foreach($data as $key=>$value)
   
   $insert = perl_chop($insert);
   if( $_GET['act'] == add ){
      //echo $insert;
      $query = "INSERT INTO models_purg SET " . $insert;
   }else{
      $query = "UPDATE models_purg SET " . $insert . "WHERE id=" . $_GET['ind'];
   }
   //echo $query;
   $result = mysql_query($query);
   if($result){
      if( mysql_affected_rows() == 1 ){
         $success = true;
      }else{
         echo 'ID number ' . $_GET['ind'] . ' does not exist in the Database.';
         exit;
      }
   }else{
      echo "Unknown Error";
      exit;
   }
}

if($success){
   if($act == 'upd'){
      echo 'Update to "' . $app_title . '" has been submitted!';
   }else{
      echo '"' . $app_title . '" has been submitted for approval!';
   }
   echo "\n<br><br>\nClick " . '<a href="model.php">here</a> to submit another model.';
   exit;
}
?>

<form name="who" method="get" style="margin-left: 30px" action="">
   <b>Action: </b>
   <select size="1" name="act" onChange="actionChange(this.options[this.selectedIndex].value);">
      <option value=0>-----Select One-----</option>
      <option value='upd' <? if(isset($act) && $act=='upd') echo 'selected'; ?>>Update Model</option>
      <option value='add' <? if(isset($act) && $act=='add') echo 'selected'; ?>>Add Model</option>
   </select>
</form>
<hr />

<?
if( !isset($_GET['act']) ){
   echo "Please select an action above.";
}else{
?>

<!--form id="form" name="form" method="post" action=""-->
  <table width="100%" border="0">
    <tr>
      <td width="200"><strong>Application</strong></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong> Title:</strong></td>
      <td>
<?
         if($_GET['act']=="upd"){
            $id=(!empty($_GET['ind'])) ? $_GET['ind'] : "";
            echo '<form name="select" method="get" action="model.php' . linker($_GET) . "\">\n";

            $html   = '';
            $output = "app_title,id";
            $tbname = 'models_purg';
            $query  = "SELECT $output FROM $tbname ORDER BY id";
            $result = $db->sql_query($query);
            $html  .= $indent . '<select width="20" name="ind" onChange="nameChange(this.
            options[this.selectedIndex].value);">' . "\n";
            $html  .= $indent . '   <option>-----Select One-----</option>'."\n";
            while($row = $db->sql_fetchrow($result)){
               $title = get_paper_title($row[0]);
               $html .= $indent . '   <option ';
               if(strlen($title) > 50){
                  $title = substr($title,0,44) . " . . .";
               }

               $sel = (isset($_GET['ind']) && $_GET['ind']==$row[1]) ? 'selected ' : '';

               $html .= $sel . 'value="' . $row[1] . '">' . $title . "</option>\n";
            } // while($row=mysql_fetch_row($result))
            $html .= $indent .  "</select>\n";
            echo $html;

            if( isset($_GET['ind'])  && !isset($_POST['preview']) ){
               $query  = "SELECT * FROM $tbname WHERE id=" . $_GET['ind'];
               //echo $query;
               $result = $db->sql_query($query);
               $data   = $db->sql_fetchrow($result);
            }

            echo "</form>OR<br>\n";
            echo '<form name="enter" method="get" action="model.php' . linker($_GET) . "\">\n";
            echo '<input type="hidden" name="act" value="'.$_GET['act'].'" />'."\n";
            echo 'ID: <input type="text" name="ind" value="'.$id.'" size="6" maxlength="5" /> '."\n";
            echo '<input type="submit" value="GO" /></form>'."\n";

            echo '<form name="mainform" method="post" action="model.php' . linker($_GET) . "\">\n";

            //$query  = "SELECT * FROM $tbname WHERE $primary='" . $_GET['ind'] . "'";
            //$result = $db->sql_query($query);
            //$row    = $db->sql_fetchrow($result);
            //echo '<input type="hidden" name="contrib_num" value="' . $row['contrib_num'] . "\" />\n";
            //echo '<textarea name="title" cols="40" rows="3">' . get_paper_title($row['title']) . '</textarea>';
         } // if($_GET['act']=="upd")
         else{
             echo '<form name="mainform" method="post" action="model.php' . linker($_GET) . "\">\n";
          }
?>
        <input name="app_title" type="text" id="app_title" value="<?=stripslashes($data['app_title'])?>" size="60" maxlength="255" /></td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Description:</strong></td>
      <td><textarea name="app_desc" id="app_desc" cols="60" rows="3"><?=stripslashes($data['app_desc'])?></textarea></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200"><strong>Info for  Model Used</strong></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" align="right"><strong>Name / Acronym:</strong></td>
      <td><input name="model_name" type="text" id="model_name" value="<?=stripslashes($data['model_name'])?>" size="60" maxlength="255" /></td>
    </tr>
    <tr>
      <td width="200" align="right"><strong>Reference:</strong></td>
      <td><input name="model_reference" type="text" id="model_reference" value="<?=stripslashes($data['model_reference'])?>" size="60" maxlength="255" /></td>
    </tr>
    <tr>
      <td width="200" align="right"><strong>Website:</strong></td>
      <td><input name="model_url" type="text" id="model_url" value="<?=stripslashes($data['model_url'])?>" size="60" maxlength="255" /></td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Description:</strong></td>
      <td><textarea name="model_desc" cols="60" rows="4" id="model_desc"><?=stripslashes($data['model_desc'])?></textarea></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200"><strong>Grid</strong></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" align="right"><strong>Projection:</strong></td>
      <td><input name="grid_projection" type="text" id="grid_projection" value="<?=stripslashes($data['grid_projection'])?>" size="25" maxlength="255" /></td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Extent:</strong></td>
      <td><strong>
        <input name="grid_extent" type="text" id="grid_extent" value="<?=stripslashes($data['grid_extent'])?>" size="50" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td width="200" align="right"><strong>Vertical Type:</strong></td>
      <td><select name="vgrid_type" id="vgrid_type">
        <option>&lt;--Select One--&gt;</option>
        <option value="Hybrid"<?if($data['vgrid_type']=='Hybrid'){echo ' selected';}?>>Hybrid</option>
        <option value="Isopycnal"<?if($data['vgrid_type']=='Isopycnal'){echo ' selected';}?>>Isopycnal</option>
        <option value="Geopotential"<?if($data['vgrid_type']=='Geopotential'){echo ' selected';}?>>Geopotential</option>
        <option value="Sigma"<?if($data['vgrid_type']=='Sigma'){echo ' selected';}?>>Sigma</option>
      </select>
      <strong> Number of levels: 
      <input name="vgrid_levels" type="text" id="vgrid_levels" value="<?=stripslashes($data['vgrid_levels'])?>" size="5" maxlength="5" />
      </strong></td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Horizontal Type:</strong></td>
      <td><select name="hgrid" id="hgrid">
        <option selected="selected">&lt;--Select One--&gt;</option>
        <option value="Structured"<?if($data['hgrid']=='Structured'){echo ' selected';}?>>Structured</option>
        <option value="Unstructured"<?if($data['hgrid']=='Unstructured'){echo ' selected';}?>>Unstructured</option>
      </select>
      <strong> Grid Type: 
      <select name="hgrid_type" id="hgrid_type">
        <option>&lt;--Select One--&gt;</option>
        <option value="Finite Volume"<?if($data['hgrid_type']=='Finite Volume'){echo ' selected';}?>>Finite Volume</option>
        <option value="Finite Element"<?if($data['hgrid_type']=='Finite Element'){echo ' selected';}?>>Finite Element</option>
        <option value="Arakawa A"<?if($data['hgrid_type']=='Arakawa A'){echo ' selected';}?>>Arakawa A</option>
        <option value="Arakawa B"<?if($data['hgrid_type']=='Arakawa B'){echo ' selected';}?>>Arakawa B</option>
        <option value="Arakawa C"<?if($data['hgrid_type']=='Arakawa C'){echo ' selected';}?>>Arakawa C</option>
        <option value="Other"<?if($data['hgrid_type']=='Other'){echo ' selected';}?>>Other</option>
      </select>
   </strong><br />
      <strong>Min Spacing:
      <input name="hgrid_min" type="text" id="hgrid_min" value="<?=stripslashes($data['hgrid_min'])?>" size="5" maxlength="5" />
      </strong><strong>Max Spacing:
      <input name="hgrid_max" type="text" id="hgrid_max" value="<?=stripslashes($data['hgrid_max'])?>" size="5" maxlength="5" /><br />
      Number of Points:
      <input name="hgrid_num" type="text" id="hgrid_num" value="<?=stripslashes($data['hgrid_num'])?>" size="20" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200"><strong>Bathymetry</strong></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Data Source: </strong></td>
      <td><textarea name="bath_source" cols="60" rows="4" id="bath_source"><?=stripslashes($data['bath_source'])?></textarea></td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Smoothing Applied: </strong></td>
      <td><textarea name="bath_smooth" cols="60" rows="4" id="bath_smooth"><?=stripslashes($data['bath_smooth'])?></textarea></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" height="37" valign="top"><strong>Timestep (type/size):</strong></td>
      <td><p>
        <textarea name="timestep" id="timestep" cols="45" rows="2"><?=stripslashes($data['timestep'])?></textarea>
      </p>        </td>
    </tr>
    <tr>
      <td width="200"><strong>Model Run Duration:</strong></td>
      <td><input name="duration" type="text" id="duration" value="<?=stripslashes($data['duration'])?>" size="50" maxlength="255" /></td>
    </tr>
    <tr>
      <td align="right"><strong>Start:</strong></td>
      <td><strong>
        <input name="time_start" type="text" id="time_start" value="<?=stripslashes($data['time_start'])?>" size="50" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td align="right"><strong>End:</strong></td>
      <td><strong>
        <input name="time_end" type="text" id="time_end" value="<?=stripslashes($data['time_end'])?>" size="50" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200"><strong>Forcing</strong> (i.e. source, frequency, spatial resolution, etc.)</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Tides:</strong></td>
      <td><textarea name="forcing_tides" cols="50" rows="2" id="forcing_tides"><?=stripslashes($data['forcing_tides'])?></textarea></td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Rivers:</strong></td>
      <td><textarea name="forcing_rivers" cols="50" rows="2" id="forcing_rivers"><?=stripslashes($data['forcing_rivers'])?></textarea></td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Atmosphere:</strong></td>
      <td><textarea name="forcing_atmos" cols="50" rows="2" id="forcing_atmos"><?=stripslashes($data['forcing_atmos'])?></textarea></td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Other:</strong></td>
      <td><textarea name="forcing_other" cols="50" rows="2" id="forcing_other"><?=stripslashes($data['forcing_other'])?></textarea></td>
    </tr>
    <tr>
      <td width="200" valign="top"><strong>Initial Conditions:</strong><br />
      (i.e. source, frequency, spatial resolution, etc.)</td>
      <td><textarea name="ini_cond" cols="50" rows="4" id="ini_cond"><?=stripslashes($data['ini_cond'])?></textarea></td>
    </tr>
    <tr>
      <td width="200" valign="top"><strong>Boundary Conditions:</strong><br />
      (i.e. source, frequency, spatial resolution, etc.)</td>
      <td><textarea name="bndry_cond" cols="50" rows="4" id="bndry_cond"><?=stripslashes($data['bndry_cond'])?></textarea></td>
    </tr>
    <tr>
      <td width="200"><strong>Data Assimilation:</strong></td>
      <td><strong>No</strong>
        <input type="radio" name="da_yn" value="N"<?if($data['da_yn']=='N'){echo " checked";}?> />
        <strong>Yes</strong>
        <input type="radio" name="da_yn" value="Y"<?if($data['da_yn']=='Y'){echo " checked";}?> />
        <strong>Info: </strong>      <strong>
        <input name="da_info" type="text" id="da_info" value="<?=stripslashes($data['da_info'])?>" size="50" maxlength="255" />
        </strong></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200"><strong>Equations:</strong></td>
      <td><select name="eq_stat" id="eq_stat">
          <option>&lt;--Select One--&gt;</option>
          <option value="Hydrostatic"<?if($data['eq_stat']=='Hydrostatic'){echo ' selected';}?>>Hydrostatic</option>
          <option value="Nonhydrostatic"<?if($data['eq_stat']=='Nonhydrostatic'){echo ' selected';}?>>Nonhydrostatic</option>
        </select>
          <select name="eq_bous" id="eq_bous">
            <option>&lt;--Select One--&gt;</option>
            <option value="Boussinesq"<?if($data['eq_bous']=='Boussinesq'){echo ' selected';}?>>Boussinesq</option>
            <option value="Non-Boussinesq"<?if($data['eq_bous']=='Non-Boussinesq'){echo ' selected';}?>>Non-Boussinesq</option>
          </select>      </td>
    </tr>
    <tr>
      <td width="200" align="right"><strong>Closures:</strong></td>
      <td><textarea name="eq_closures" cols="50" rows="2" id="eq_closures"><?=stripslashes($data['eq_closures'])?></textarea></td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>Variables:</strong></td>
      <td><p><strong>u</strong>
        <input type="checkbox" name="var_u" id="var_u"<?if($data['var_u']=='on'){echo " checked";}?> />
            <strong>v</strong>
          <input type="checkbox" name="var_v" id="var_v"<?if($data['var_v']=='on'){echo " checked";}?> />
            <strong>w</strong>
          <input type="checkbox" name="var_w" id="var_w"<?if($data['var_w']=='on'){echo " checked";}?> />
            <strong>T</strong>
          <input type="checkbox" name="var_T" id="var_T"<?if($data['var_T']=='on'){echo " checked";}?> />
            <strong>S</strong>
          <input type="checkbox" name="var_S" id="var_S"<?if($data['var_S']=='on'){echo " checked";}?> />
            <strong>rho</strong>
          <input type="checkbox" name="var_rho" id="var_rho"<?if($data['var_rho']=='on'){echo " checked";}?> />
      <br />
      <strong>Biology:</strong> 
      <input name="var_bio" type="text" id="var_bio" value="<?=stripslashes($data['var_bio'])?>" size="24" maxlength="255" /><br />
      <strong>Sediment:</strong> 
      <input name="var_sed" type="text" id="var_sed" value="<?=stripslashes($data['var_sed'])?>" size="24" maxlength="255" /><br />
      <strong>Sea Ice:</strong> 
      <input name="var_ice" type="text" id="var_ice" value="<?=stripslashes($data['var_ice'])?>" size="24" maxlength="255" />
      </p></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200"><strong>Output</strong></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>What/Where/How Often?</strong></td>
      <td><textarea name="output_wwh" cols="50" rows="3" id="output_wwh"><?=stripslashes($data['output_wwh'])?></textarea></td>
    </tr>
    <tr>
      <td width="200" align="right"><strong>Format:</strong></td>
      <td><strong>
        <input name="output_format" type="text" id="output_format" value="<?=stripslashes($data['output_format'])?>" size="24" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200"><strong>Availablilty of Output Data</strong></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>On-line:</strong></td>
      <td><textarea name="avail_url" cols="60" rows="2" id="avail_url"><?=stripslashes($data['avail_url'])?></textarea></td>
    </tr>
    <tr>
      <td width="200" align="right"><strong>Contact Person:</strong></td>
      <td><strong>
        <input name="avail_contact" type="text" id="avail_contact" value="<?=stripslashes($data['avail_contact'])?>" size="50" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td align="right"><strong>Contact Affiliation:</strong></td>
      <td><strong>
        <input name="avail_cont_affil" type="text" id="avail_cont_affil" value="<?=stripslashes($data['avail_cont_affil'])?>" size="50" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td width="200" align="right"><strong>Contact Email:</strong></td>
      <td><strong>
        <input name="avail_cont_email" type="text" id="avail_cont_email" value="<?=stripslashes($data['avail_cont_email'])?>" size="50" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td align="left" valign="top">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" align="left" valign="top"><strong>Reference / Documentation</strong></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="200" align="right" valign="top"><strong>References:</strong></td>
      <td><textarea name="ref_ref" cols="60" rows="2" id="ref_ref"><?=stripslashes($data['ref_ref'])?></textarea></td>
    </tr>
    <tr>
      <td align="right" valign="top"><strong>Who funded this work?</strong></td>
      <td><textarea name="ref_fund" cols="60" rows="2" id="ref_fund"><?=stripslashes($data['ref_fund'])?></textarea></td>
    </tr>
    <tr>
      <td align="right" valign="top"><strong>Award / Contract #:</strong></td>
      <td><strong>
        <input name="ref_award_num" type="text" id="ref_award_num" value="<?=stripslashes($data['ref_award_num'])?>" size="50" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td align="right" valign="top"><strong>Project Title:</strong></td>
      <td><strong>
        <input name="ref_proj_title" type="text" id="ref_proj_title" value="<?=stripslashes($data['ref_proj_title'])?>" size="60" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td align="right" valign="top">&nbsp;</td>
      <td><strong>Start:</strong> <strong>
        <input name="ref_proj_start" type="text" id="ref_proj_start" value="<?=stripslashes($data['ref_proj_start'])?>" size="25" maxlength="255" />
      </strong><strong>End: 
      <input name="ref_proj_end" type="text" id="ref_proj_end" value="<?=stripslashes($data['ref_proj_end'])?>" size="25" maxlength="255" />
      </strong></td>
    </tr>
    <tr>
      <td align="right" valign="top"><strong>Citation</strong><strong>:</strong><br />
      (how to cite data/application)
      </td>
      <td><textarea name="ref_cite" cols="60" rows="2" id="ref_cite"><?=stripslashes($data['ref_cite'])?></textarea></td>
    </tr>
    <tr>
      <td align="right" valign="top"><strong>Notes:</strong></td>
      <td><textarea name="ref_notes" cols="60" rows="5" id="ref_notes"><?=stripslashes($data['ref_notes'])?></textarea></td>
    </tr>
    <tr>
      <td align="right" valign="top">&nbsp;</td>
      <td><input type="submit" name="submit" value="Submit" />
        <input type="submit" name="preview" value="Preview" /></td>
    </tr>
  </table>
</form>
<?
} // if( $_GET['act'] )

include FOOT;
?>
