<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs title="CSCAP Site Management Form" >
<OAuth>
   <Service name="googleAPI">
      <Access url="https://www.google.com/accounts/OAuthGetAccessToken" method="GET" />
      <Request url="https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/" method="GET" />
      <Authorization url="https://www.google.com/accounts/OAuthAuthorizeToken" />
   </Service>
</OAuth>
  <OAuth2>
    <Service name="googleAPI" scope="https://spreadsheets.google.com/feeds">
    </Service>
  </OAuth2>
    <Require feature="dynamic-height"/>  
    <Require feature="auth-refresh"/>
    <Require feature="oauthpopup"/>
  </ModulePrefs>
  <Content type="html">
    <![CDATA[
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css">
<style type="text/css">
.ui-widget{
 font-size: 1em !important;
 font: inherit;
}
.ui-state-active{
  background: #d44716 !important; # blue is 1882f3
}
.optoptions{
  font-style: italic;
}
.optheader{
  font-weight: bold;
  color: #d44716;
}
</style>
<script src="https://www.google.com/jsapi?key=ABQIAAAArXt77YptylBNPFEy0AgJxBQV4szfgV8zGZZkur1B-B3W8b5AThTSlomZliOt8JQHJGH1m63sgDu1rg" type="text/javascript"></script>

<!-- Initially displayed and then hidden once we are running -->
<div id="loading">
 <p><strong>Management form is loading...</strong>
</div>

<!-- Displayed if this gadget needs oauth2 approval -->
<div id="approval" style="display: none">
  This application is a "Google Gadget" written by Daryl Herzmann and needs authorization by you to view and save data to a dedicated spreadsheet for this dataset. <a id="personalize" onclick="javascript:showOneSection('waiting'); return true" target="approve">Click to authorize this gadget</a><p>The authorization step should open a new browser window and request permission to access spreadsheets on your behalf.</div>

<!-- Displayed once the approval process begins... -->
<div id="waiting" style="display: none">Please click <a href="#" onclick="javascript:bootstrap()">I've approved access</a> once you've approved access to your data.</div>

<!-- Our main 'window' -->
<div id="main" style="display: none">
<form name="management">
<input id="rowid" value="0" type="hidden" />
<input id="ipm_rowid" value="0" type="hidden" />
<p><strong>Select Research Site:</strong>
<select name="site" id="siteSelector" onChange="setSite(this.options[this.selectedIndex].value);">
  <option value="invalid">Select Site from List...</option>
</select>
<div id="theform" style="display: none;">
<p><strong>Select Year. Enter information under all 5 tabs. Please click the "SUBMIT" button to save your edits for each tab you enter data for.</strong>
<div id="years">
    <input type="radio" value="2011" id="y2011" name="year"/><label for="y2011">2011 Crop</label>
    <input type="radio" value="2012" id="y2012" name="year"/><label for="y2012">2012 Crop</label>
    <input type="radio" value="2013" id="y2013" name="year"/><label for="y2013">2013 Crop</label>
    <input type="radio" value="2014" id="y2014" name="year"/><label for="y2014">2014 Crop</label>
    <input type="radio" value="2015" id="y2015" name="year" checked="checked" /><label for="y2015">2015 Crop</label>
</div>

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Field Operations</a></li>
    <li><a href="#tabs-2">Management</a></li>
    <li><a href="#tabs-3">Pesticides</a></li>
    <li><a href="#tabs-4">Site Map &amp; GPS</a></li>
    <li><a href="#tabs-5">Notes</a></li>
  </ul>

<div id="tabs-1">
<p><strong>Add to or edit previous Field Operations entries for <span id="foryear"></span>:</strong>
<ul id='previousOperations'>
</ul>
<div id="previousOperationEdit" style="display: none; border: 2px #f00 solid; padding: 5px;">You are now viewing previously entered data, if you wish to make changes please be sure to save this once done.  To cancel 
any changes you make, click the CLEAR FORM button at the bottom.</div>

<p><input type="button" value="Clear Form for New Operation" onClick="javascript: clearData();">

<p><strong>Select Operation:</strong>
<select name="operation" id="operation" class="optoption"  onChange="setOperation(this.options[this.selectedIndex].value);">
  <option value="invalid" class="optheader">Select From List</optidon>
  <option disabled="disabled" value="invalid" class="optheader">... Planting Operations ...</option>
  <option value="plant_corn">Corn Planting</option>
  <option value="plant_soy">Soybean Planting</option>
  <option value="plant_rye">Rye Planting into Corn and Soy Residue</option>
  <option value="plant_rye-corn-res">Rye Planting into Corn Residue</option>
  <option value="plant_rye-soy-res">Rye Planting into Soy Residue</option>
  <option value="plant_wheat">Wheat Planting</option>
  <option value="plant_alfalfa">Alfalfa Planting</option>
  <option value="plant_other">Other Planting</option>
  <option disabled="disabled" value="invalid" class="optheader">... Harvest Operations ...</option>
  <option value="harvest_corn">Corn Harvest</option>
  <option value="harvest_soy">Soybean Harvest</option>
  <option value="termination_rye_corn">Rye Termination to Plant Corn</option>
  <option value="termination_rye_soy">Rye Termination to Plant Soybean</option>
  <option value="harvest_wheat">Wheat Harvest</option>
  <option value="termination_alfalfa_corn">Alfalfa Termination to Plant Corn</option>
  <option value="harvest_other">Other Harvest</option>
  <option disabled="disabled" value="invalid" class="optheader">... Tillage + Residue Management ...</option>
  <option value="tillage_chisel">Chisel Tillage</option>
  <option value="tillage_chopstalks">Chop Stalks</option>   
  <option value="tillage_cultivate">Field Cultivate</option>
  <option value="tillage_rip">Deep Rip Tillage</option>
  <option value="tillage_disk">Disk Tillage</option>
  <option value="tillage_plow">Plow</option>
  <option value="tillage_rotaryhoe">Rotary Hoe</option>
  <option value="tillage_rowcultivate">Row Cultivate</option> 
  <option value="tillage_strip">Strip Tillage</option>
  <option value="tillage_surface">Surface Tillage</option>  
  <option value="tillage_vertical">Vertical Tillage</option>
  <option disabled="disabled" value="invalid" class="optheader">... Soil Treatments ...</option>
  <option value="soiladmend_lime">Lime Application</option>  
  <option value="manure_manure">Manure Application</option>
  <option value="fertilizer_synthetic">Synthetic Fertilizer</option>
  <option disabled="disabled" value="invalid" class="optheader">... Sampling Date ...</option>
  <option value="sample_canopy">Canopy N-Sensing</option>  
  <option value="sample_covercrop">Cover Crop Sampling</option>  
  <option value="sample_soilnitrate">Soil Nitrate</option>  
</select>

 <p><div id="operation_label" style="font-weight: bold;">Operation Date:</div>
<input type="text" id="datepicker"></p>

<div id="fertilizer_options" style="display: none;">
<p><strong>Form of Fertilizer:</strong>
<select id="fertilizer_form">
 <option value="invalid">Select from List</option>
 <option value="liquid">Liquid</option>
 <option value="solid">Solid (granular)</option>
 <option value="gas">Gas (anhydrous)</option>
</select>

<p><strong>Which crop was this applied to:</strong>
<select id="fertilizer_crop">
 <option value="invalid">Select from list</option>
 <option value="corn">Corn</option>
 <option value="soybean">Soybean</option>
 <option value="wheat">Wheat</option>
 <option value="oats">Oats</option>
 <option value="alfalfa">Alfalfa</option>
 <option value="multiple">multiple crops (explain in Comments)</option>
 <option value="other">other (explain in Comments)</option>
</select>

<p><strong>Application Type:</strong>
<select id="fertilizer_apptype" onChange="showFertilizerInjection(this.options[this.selectedIndex].value);">
 <option value="invalid">Select from List</option>
 <option value="broadcast">(1) Broadcast - surface applied</option>
 <option value="broadcast in every row">(2) Placed in the row - surface applied</option>
 <option value="injected in every row">(3) Placed in the row - injected</option>
 <option value="broadcast between row">(4) Placed between rows - surface applied</option>
 <option value="injected between rows">(5) Placed between rows - injected</option>
 <option value="broadcast between every other row">(6) Placed between every other row - surface applied</option>
 <option value="injected between every other row">(7) Placed between every other row - injected</option>
 <option value="injected diagonally">(8) Diagonally injected across field</option>
 <option value="other">(9) Other (explain in Comments)</option>
</select>

<div id="fertilizer_injected_options" style="display: none;">
<table><tr>
  <th>What was the injection depth?</th>
  <td><input type="text" id="fertilizer_injection_depth" size="6" /></td>
  <td><div id="fertilizer_injection_depth_units">
    <input type="radio" value="inch" id="fertilizer_injection_depth_units_inch" 
name="fertad" checked="checked"/><label for="fertilizer_injection_depth_units_inch">inch</label>
    <input type="radio" value="cm" id="fertilizer_injection_depth_units_cm" 
name="fertad"/><label for="fertilizer_injection_depth_units_cm">cm</label>
</div>
</td></tr>
</table>
</div><!-- End of fertilizer_injected_options -->

<p><strong>What is the chemical formulation of the fertilizer applied? 
(example: urea, superphosphate, etc.)</strong>
<br /><input type="text" id="fertilizer_formulation" size="60" />

<p><strong>Was a stabilizer (e.g. inhibitor or slow release) used?</strong>
<br /><select id="fertilizer_stabilizer" onChange="showFertilizerStabilizer(this.options[this.selectedIndex].value);">
  <option value="invalid">Select from List</option>
  <option value="yes">Yes</option>
  <option value="no">No</option>
</select>

<div id="fertilizer_stabilizer_options" style="display: none;">
<p><strong>What type of stabilizer was used?</strong>
<br /><select id="fertilizer_stabilizer_used">
  <option value="invalid">Select from List</option>
  <option value="urease">urease inhibitor</option>
  <option value="nitrification">nitrification inhibitor</option>
  <option value="slow">slow release</option>
  <option value="other">Other</option>
</select>

<p><strong>Select the stabilizer product name:</strong>
<br /><select id="fertilizer_stabilizer_name">
  <option value="invalid">Select from List</option>
  <option value="n-serve">N-Serve</option>
  <option value="instinct">Instinct</option>
  <option value="agrotain">Agrotain</option>
  <option value="agrotain-plus">Agrotain Plus</option>
  <option value="esn">ESN</option>
  <option value="super u">Super U</option>
  <option value="other">Other</option>
</select>

</div>

<p>If you apply a fertilizer blend (multiple sources), create a different entry for each product.

<p>
<table>
<tr><th width="50%">Option A</th><th width="50%">Option B</th></tr>
<tr><td>
<strong>Product Rate (lbs of product per acre):</strong>
<br /><input type="text" id="product_rate" size="5">
</td><td>
Use this side if you know the quantity applied per nutrient. Do not 
 combine multiple applications though as we need to know the dates 
 associated with each.
</td></tr>
<tr><td>
<strong>Fertilizer Formulation (report in percent):</strong>
<br /><i>Example: 100 lbs/acre of 18-46-0 was applied. 100 should be entered as
 the product rate (above) with 18 in the %N box and 46 in the %P<sub>2</sub>O<sub>5</sub> box
 below. The elemental form of each will be calculated in the database
 using these values.</i>

<br /><input type="text" id="nitrogen_rate" size="5" value="0">
 <label for="nitrogen_rate">% N (Nitrogen)</label>

<br /><input type="text" id="phosphate_rate" size="5" value="0">
 <label for="phosphate_rate">% P<sub>2</sub>O<sub>5</sub> (Phosphate)</label>

<br /><input type="text" id="phosphorus_rate" size="5" value="0">
 <label for="phosphorus_rate">% P (Phosphorus)</label>

<br /><input type="text" id="potash_rate" size="5" value="0">
 <label for="potash_rate">% K<sub>2</sub>O (Potash)</label>

<br /><input type="text" id="potassium_rate" size="5" value="0">
 <label for="potassium_rate">% K (Potassium)</label>

<br /><input type="text" id="sulfur_rate" size="5" value="0">
 <label for="sulphur_rate">% S (Sulfur)</label>

<br /><input type="text" id="zinc_rate" size="5" value="0">
 <label for="zinc_rate">% Zn (Zinc)</label>

<br /><input type="text" id="magnesium_rate" size="5" value="0">
 <label for="magnesium_rate">% Mg (Magnesium)</label>

<br /><input type="text" id="calcium_rate" size="5" value="0">
 <label for="calcium_rate">% Ca (Calcium)</label>

<br /><input type="text" id="iron_rate" size="5" value="0">
 <label for="iron_rate">% Fe (Iron)</label>

</td><td valign="top">

<strong>Fertilizer Formulation (report in pounds per acre):</strong>

<br /><input type="text" id="nitrogen_rate_lbs" size="5" value="0">
 <label for="nitrogen_rate_lbs">lbs N (Nitrogen) per acre</label>

<br /><input type="text" id="phosphate_rate_lbs" size="5" value="0">
 <label for="phosphate_rate_lbs">lbs P<sub>2</sub>O<sub>5</sub> per acre (Phosphate)</label>

<br /><input type="text" id="phosphorus_rate_lbs" size="5" value="0">
 <label for="phosphorus_rate_lbs">lbs P (Phosphorus) per acre</label>

<br /><input type="text" id="potash_rate_lbs" size="5" value="0">
 <label for="potash_rate_lbs">lbs K<sub>2</sub>O (Potash) per acre</label>

<br /><input type="text" id="potassium_rate_lbs" size="5" value="0">
 <label for="potassium_rate_lbs">lbs K (Potassium) per acre</label>

<br /><input type="text" id="sulfur_rate_lbs" size="5" value="0">
 <label for="sulphur_rate_lbs">lbs S (Sulfur) per acre</label>

<br /><input type="text" id="zinc_rate_lbs" size="5" value="0">
 <label for="zinc_rate_lbs">lbs Zn (Zinc) per acre</label>

<br /><input type="text" id="magnesium_rate_lbs" size="5" value="0">
 <label for="magnesium_rate_lbs">lbs Mg (Magnesium) per acre</label>

<br /><input type="text" id="calcium_rate_lbs" size="5" value="0">
 <label for="calcium_rate_lbs">lbs Ca (Calcium) per acre</label>

<br /><input type="text" id="iron_rate_lbs" size="5" value="0">
 <label for="iron_rate_lbs">lbs Fe (Iron) per acre</label>

</td></tr></table>
</div>

<div id="soiladmend_options" style="display: none;">
<p><strong>Current soil pH of the site:</strong>
<input type="text" id="soiladmend_current_ph" width="10" />

<p><strong>Target soil pH of the site:</strong>
<input type="text" id="soiladmend_target_ph" width="10" />

<p>
<div id="soiladmend_rate_units" style="float:right;">
    <input type="radio" value="hectare" id="soiladmend_rate_units_hectare" name="soiladmend_rate_units" checked="checked"/><label for="soiladmend_rate_units_hectare">Mg per hectare</label>
    <input type="radio" value="acre" id="soiladmend_rate_units_acre" name="soiladmend_rate_units"/><label for="soiladmend_rate_units_acre">tons per acre</label>
</div>
<strong>Application Rate:</strong>
<input type="text" id="lime_rate">
<br clear="all" />

<p><strong>What is the Neutralizing Index value of the material?</strong>
<input type="text" id="soiladmend_neutral_index" width="10" maxlength="3" />

</div>

<div id="manure_options" style="display: none;">
<strong>Source of Manure e.g. hog, etc.:</strong>
<input type="text" id="manure_source" />

<p><strong>What is the composition of this manure?</strong>
<br /><select id="manure_composition">
 <option value="invalid">Select from List</option><option value="farmyard">Farmyard manure</option><option value="green">Green manure</option><option value="straw">Straw</option><option value="slurry">Slurry animal waste</option><option value="compost">Compost</option><option value="beancake">Bean cake</option><option value="human">Human waste</option><option value="poultry">Poultry waste</option><option value="sewage">Sewage sludge</option><option value="meat+blood">Meat or blood meal</option><option value="other">Other</option>
</select>

<p><strong>What was the application method?</strong>
<br /><select id="manure_method" onChange="showManureMethodOptions(this.options[this.selectedIndex].value);">
 <option value="invalid">Select from List</option>
 <option value="broadcast without cultivation">Broadcast without cultivation</option>
 <option value="broadcast with cultivation">Broadcast with cultivation</option>
 <option value="injected">Injected (e.g. knifed)</option>
 <option value="other">Other</option>
</select>

<div id="manure_method_options" style="display: none;">
<table><tr>
  <th>What was the injection depth?</th>
  <td><input type="text" id="manure_injection_depth" size="6" /></td>
  <td><div id="manure_injection_depth_units">
    <input type="radio" value="inch" id="manure_injection_depth_units_inch" name="mad" checked="checked"/><label for="manure_injection_depth_units_inch">inch</label>
    <input type="radio" value="cm" id="manure_injection_depth_units_cm" name="mad"/><label for="manure_injection_depth_units_cm">cm</label>
</div>
</td></tr>
</table>
</div><!-- End of manure_method_options -->

<p><strong>Was a stabilizer (e.g. inhibitor or slow release) used?</strong>
<br /><select id="manure_stabilizer"  onChange="showManureStabilizer(this.options[this.selectedIndex].value);">
  <option value="invalid">Select from List</option>
  <option value="yes">Yes</option>
  <option value="no">No</option>
</select>

<div id="manure_stabilizer_options" style="display: none;">
<p><strong>What type of stabilizer was used?</strong>
<br /><select id="manure_stabilizer_used">
  <option value="invalid">Select from List</option>
  <option value="urease">urease inhibitor</option>
  <option value="nitrification">nitrification inhibitor</option>
  <option value="slow">slow release</option>
  <option value="other">Other</option>
</select>

<p><strong>Select the stabilizer product name:</strong>
<br /><select id="manure_stabilizer_name">
  <option value="invalid">Select from List</option>
  <option value="n-serve">N-Serve</option>
  <option value="instinct">Instinct</option>
  <option value="agrotain">Agrotain</option>
  <option value="agrotain-plus">Agrotain Plus</option>
  <option value="esn">ESN</option>
  <option value="super u">Super U</option>
  <option value="other">Other</option>
</select>

</div><!-- End of manure_stabilizer -->

<p>
<table>
<tr><th>Application Rate:</th>
<td><input type="text" id="manure_rate" /></td>
<td>
<div id="manure_rate_units">
    <input type="radio" value="gpa" id="manure_rate_units_gpa" name="manure_rate_units" checked="checked"/><label for="manure_rate_units_gpa">gallons per acre</label>
    <input type="radio" value="tpa" id="manure_rate_units_tpa" name="manure_rate_units"/><label for="manure_rate_units_tpa">tons per acre</label>
    <input type="radio" value="lph" id="manure_rate_units_lph" name="manure_rate_units"/><label for="manure_rate_units_lph">L per hectare</label>
    <input type="radio" value="mgph" id="manure_rate_units_mgph" name="manure_rate_units"/><label for="manure_rate_units_mgph">Mg per hectare</label>
</div></td></tr></table>
<br clear="all"/>
<p><strong>Nutrient analysis of manure:</strong>

<br /><input type="text" id="manure_nitrogen_rate" size="5" value="0">
 <label for="manure_nitrogen_rate">% N (Total Nitrogen (TKN))</label>
<br /><input type="text" id="manure_phosphate_rate" size="5" value="0">
 <label for="manure_phosphate_rate">% P<sub>2</sub>0<sub>5</sub> (Phosphate)</label>
<br /><input type="text" id="manure_potash_rate" size="5" value="0">
 <label for="manure_potash_rate">% K<sub>2</sub>0 (Potash)</label>
<br /><input type="text" id="manure_sulfur_rate" size="5" value="0">
 <label for="manure_sulfur_rate">% S (Sulfur)</label>
<br /><input type="text" id="manure_calcium_rate" size="5" value="0">
 <label for="manure_calcium_rate">% Ca (Calcium)</label>
<br /><input type="text" id="manure_magnesium_rate" size="5" value="0">
 <label for="manure_magnesium_rate">% Mg (Magnesium)</label>



</div>
<div id="sample_options" style="display: none;">
<p>Please include additional metadata about the sampling event where
appropriate.  You can enter free-form comments to further describe the
sampling that was done.  The actual data for these sampled are stored
elsewhere.</p>
<br /><input type="text" id="sample_growth_stage" size="5" value="-">
 <label for="sample_growth_stage">Sample Growth Stage</label>
<br /><input type="text" id="sample_height" size="5" value="-">
 <label for="sample_height">Canopy Height (meters)</label>
</div>

<div id="plant_options" style="display: none;">
<div id="plant_rye_options" style="display: none;">
<p><strong>How was the rye seeded?</strong>
<br /><select id="plantryemethod">
 <option value="broadcast">Broadcast</option>
 <option value="drilled">Drilled</option>
 <option value="other">Other (specify in comments)</option>
</select>
</p>
</div><!-- end of plant_rye_options -->

<table>
<tr><td><strong>Hybrid or Variety Planted:</strong></td>
<td><input type="text" id="plant_hybrid"></td><td>(ex: Pioneer 1234)</td></tr>

<tr><td><strong>Maturity of Hybrid or Variety:</strong></td>
<td><input type="text" id="plant_maturity"></td>
<td>For corn, the relative maturity (85-125).  For soybean, a value between 1.0 and 4.0</td></tr>

<tr><td><strong>Seeding Rate:</strong></td>
<td><input type="text" id="plant_rate"></td>
<td><div id="plant_rate_units">
    <input type="radio" value="hectare" id="plant_rate_units_hectare" name="plant_rate_units" checked="checked"/><label for="plant_rate_units_hectare">seeds per hectare</label>
    <input type="radio" value="acre" id="plant_rate_units_acre" name="plant_rate_units"/><label for="plant_rate_units_acre">seeds per acre</label>
    <input type="radio" value="lbs" id="plant_rate_units_lbs" name="plant_rate_units"/><label for="plant_rate_units_lbs">lbs per acre</label>
</div></td></tr>
<tr><td colspan="3">Note: To convert bu/acre to lbs/acre use: rye (56 lbs/bu), 
alfalfa (60 lbs/bu), and wheat (60 lbs/bu).</td></tr>
</table>

</div><!-- End of plant_options -->

<div id="harvest_options" style="display: none;">

</div><!-- End of harvest_options -->

<div id="termination_options" style="display: none;">

<p><strong>How was the crop terminated?</strong>
<select id="terminate_method">
 <option value="invalid">Select from List</option>
 <option value="herbicide">Herbicide
 <option value="tillage">Tillage
 <option value="herbicide+tillage">Herbicide and Tillage
 <option value="crimping">Crimping (rye only)
 <option value="other">Other
</select>

</div><!-- End of termination_options -->

<div id="termination2_options" style="display: none;">

<p><strong>If biomass samples were collected, please enter the date of sampling. If
sampled more than once, enter each date.</strong>
<br />Date 1: <input type="text" id="tdatepicker1">
Date 2: <input type="text" id="tdatepicker2">

</div><!-- End of termination2_options -->

<div id="tillage_options" style="display: none;">
<div style="float: left;"><strong>Tillage Depth (if applicable):</strong>
<input type="text" id="tillage_depth" size="5"></div>
<div id="tillage_depth_units" style="float:right;">
    <input type="radio" value="cm" id="tillage_depth_units_cm" name="tillage_depth_units" checked="checked"/><label for="tillage_depth_units_cm">centimeters</label>
    <input type="radio" value="in" id="tillage_depth_units_in" name="tillage_depth_units"/><label for="tillage_depth_units_in">inches</label>
</div>
</div>

<br clear="all"/>
<p><strong>Comments:</strong>
<br />
<textarea name="comments" id="comments" cols="60" rows="6"></textarea>
  
<p>
<input id="submitbutton" type="button" value="SUBMIT" onClick="javascript: saveOperationData();">
<input type="button" onClick="javascript: clearData();" value="CLEAR FORM" />
</div>
<div id="tabs-2">
<div id="managementTabMessage" style="display:none; padding: 5px; border: 2px solid #f00;">Management information not found for this year.</div>
<p><strong>Was this crop irrigated?</strong>
<select id="irrigation" onChange="irrigation_show(this.options[this.selectedIndex].value);">
 <option value="invalid">Select from List</option>
 <option value="yes">Yes</option>
 <option value="no">No</option>
</select>

<div id="irrigation-sub" style="display:none;">

<p><table
<tr><td><strong>Enter the total amount applied during the growing season:</strong></td><td>
<input type="text" size="5" id="irrigation_amount" /></td><td>
<div id="irrigation_units" style="float:right;">
    <input type="radio" value="cm" id="irrigation_units_cm" name="irrigation_units" checked="checked"/><label for="irrigation_units_cm">centimeters</label>
    <input type="radio" value="in" id="irrigation_units_in" name="irrigation_units"/><label for="irrigation_units_in">inches</label>
</div></td></tr></table>

<p><strong>What method was used for irrigation?</strong>
<select id="irrigation_method">
 <option value="invalid">Select from List</option>
 <option value="overhead">overhead (pivot/linear system)
 <option value="sprinkler">sprinkler
 <option value="subsurface">subsurface drip
 <option value="flood">flood (furrow/gravity)
</select>

</div>

<p><strong>Was crop residue (biomass) removed at or after grain harvest?</strong>
<select id="residue_removal" onChange="residue_show(this.options[this.selectedIndex].value);">
 <option value="invalid">Select from List</option>
 <option value="yes">Yes</option>
 <option value="no">No</option>
 <option value="varies">Yes and No (Varies by Treatment)</option>
</select>

<div id="residue-sub-no" style="display:none;">
</div>

<div id="residue-sub-yes" style="display:none;">
<p><strong>If answered "Yes" or "Yes and No"; how was the crop residue removed from the applicable plots?</strong>
<br /><select id="residue_how">
 <option value="invalid">Select from List</option>
 <option value="burning">Burning</option>
 <option value="mechanical">Mechanical</option>
 <option value="mechanical_cobs">Mechanical - Cobs Only</option>
 <option value="mowed">Mowed</option>
 <option value="n/a">Not Applicable</option>
 <option value="other">Other</option>
</select>

<p><strong>What amount of residue was removed? Enter the:</strong>
<br />Biomass weight <input type="text" id="residue_biomass_weight" size="10" />
<div id="residue_biomass_weight_units">
    <input type="radio" value="lbs" id="residue_biomass_weight_units_lbs" name="residue_biomass_weight_units" checked="checked"/><label for="residue_biomass_weight_units_lbs">lbs per acre</label>
    <input type="radio" value="kg" id="residue_biomass_weight_units_kg" name="residue_biomass_weight_units" /><label for="residue_biomass_weight_units_kg">kg per acre</label>
 </div>
<br />Moisture content <input type="text" id="residue_biomass_moisture" size="4" /> (%)

</div><!-- End of residue-sub-yes -->

<p><strong>What is the estimated percent of residue cover on the surface at planting? Take into account tillage and residue removal.</strong>
<br />
<select id="residue_planting">
 <option value="invalid">Select from List</option>
 <option value="varies">Varies by Treatment</option>
 <option value="30+">More than 30% (Conservation Tillage)</option>
 <option value="15-30">Between 15-30% (Reduced Tillage)</option>
 <option value="0-15">Less than 15% (Conventional Tillage)</option>
</select>
<br />If residue varies by treatment, you will enter this information in a separate spreadsheet.

<p><strong>What type of residue existed on the surface at planting?</strong>
<select id="residue_type">
 <option value="invalid">Select from List</option>
 <option value="corn">Corn</option>
 <option value="soybean">Soybean</option>
 <option value="wheat">Wheat</option>
 <option value="oats">Oats</option>
 <option value="alfalfa">Alfalfa</option>
 <option value="rye and corn">Rye and Corn</option>
 <option value="rye and soybean">Rye and Soybean</option>
 <option value="red clover">Red Clover</option>
 <option value="none">None (biomass removed)</option>
 <option value="varies">Varies by Treatment</option>
 <option value="n/a">Not Applicable</option>
</select>


<!--
<p><strong>Was there a cover crop present at planting?</strong>
<select id="cover_crop">
 <option value="invalid">Select from List</option>
 <option value="yes">Yes</option>
 <option value="no">No</option>
</select>

<p><strong>Was manure applied to the field?</strong>
<select id="manure">
 <option value="invalid">Select from List</option>
 <option value="yes">Yes</option>
 <option value="no">No</option>
</select>
-->
<p><strong>Was lime applied to this field prior to 2011? If within the past 5 years, enter the year here</strong>
<input type="text" id="lime_year" size="10" maxlength="4" />

<p><strong>Were any inorganic or organic amendments applied to the site that are not entered under the Operations tab?</strong>
<select id="organic_amendments">
 <option value="invalid">Select from List</option>
 <option value="yes">Yes</option>
 <option value="no">No</option>
</select>

<p><strong>If yes, enter the type of amendment here.</strong>
<textarea id="organic_amendments_text" cols="60" rows="1"></textarea>

<br clear="all"/>
<p><strong>Comments:</strong>
<br />
<textarea name="management_comments" id="management_comments" cols="60" rows="6"></textarea>

<p>
<input id="submitbutton2" type="button" value="SUBMIT" onClick="javascript: saveManagementData();">
<input type="button" value="CLEAR FORM" onClick="javascript: clearManagementData();">

</div><!-- End of tabs-2 -->
<div id="tabs-3">
<p><strong>Add to or edit previous Pesticide entries for <span id="ipm_foryear"></span>:</strong>
<ul id='previousIpmOperations'>
</ul>
<div id="previousIpmOperationEdit" style="display: none; border: 2px #f00 solid; padding: 5px;">You are now editing a previously saved operation, please be sure to save this once done.  To cancel 
this edit, click the CLEAR FORM button at the bottom.</div>

<p><strong>Select Operation:</strong>
<select name="ipm_operation" id="ipm_operation" class="optoption"  onChange="setIpmOperation(this.options[this.selectedIndex].value);">
  <option value="invalid" class="optheader">Select From List</optidon>
  <option value="herbicide">Herbicide</option>
  <option value="insecticide">Insecticide</option>
  <option value="fungicide">Fungicide</option>
  <option value="seed">Seed Treatment</option>
</select>

<div id="ipm_date" style="display: block;">
<p><strong>Application Date:</strong>
<input type="text" id="ipm_datepicker">
</div>

<div id="seed_options" style="display: none;">

<p>Note: Report all seed treatments. This includes standard seed treatments 
done by the company or dealer; look on the hybrid bag for the product names. 
Do not report the product rate.

<p><strong>Timing:</strong>
<select id="seed_timing">
  <option value="seed_applied">Seed Applied</option>
</select>

<p><strong>Application made to or for:</strong>
<select id="seed_crop"><option value="invalid">Select from List</option><option value="corn">Corn</option><option value="soybean">Soybean</option><option value="rye">Rye</option><option value="wheat">Wheat</option><option value="alfalfa">Alfalfa</option><option value="other">Other</option>
</select>

<p><strong>Product Name Applied (example: Poncho)</strong>
<br />1. <input type="text" id="seed_product1" />
<br />2. <input type="text" id="seed_product2" />
<br />3. <input type="text" id="seed_product3" />
<br />4. <input type="text" id="seed_product4" />

<p><strong>Justification for action.</strong> Note: This may include any measurements of pest abundance or relevant economic thresholds.
<textarea id="seed_justify" rows="4" cols="60"></textarea>

<p><strong>Reference.</strong> Note: This may include a discussion with an 
 IPM consultant (e.g., county or university extension specialists) or
 published guideline (e.g., online IPM newsletter) used in the decision
 making process that led to the use of a pest-management strategy (e.g.,
 application of herbicide, insecticide, or fungicide).
<br /><textarea id="seed_reference" rows="6" cols="60"></textarea>

<p><strong>Any additional comments?</strong>
<textarea id="seed_comments" rows="6" cols="60"></textarea>

</div>

<div id="herbicide_options" style="display: none;">

<p>Note: If you <strong>tank-mixed</strong> several herbicides together enter each product individually under Product Applied and the respective rate for each. 

<p>Note: You do not need to separate products like Bicep II Magnum into active ingredients unless you 
 think it is unknown or confusing. If this is the case, insert active
 ingredients under Comments.

<p>Note: Report all seed treatments. This includes standard seed treatments 
done by the company or dealer; look on the hybrid bag for the product names. 
Do not report the product rate.

<p><strong>Timing:</strong>
<select name="herbicide_timing" id="herbicide_timing">
  <option value="invalid">Select from List</option>
  <option value="fall_application">Fall Application</option>
  <option value="burndown_only">Burndown Only</option>
  <option value="seed_applied">Seed Applied</option>
  <option value="preplant_only">Preplant Only</option>
  <option value="burndown+preplant">Burndown and Preplant</option>
  <option value="pre_emerge">Pre Emerge</option>
  <option value="post_emerge">Post Emerge</option>
  <option value="harvest_aid">Harvest Aid</option>
</select>

<p><strong>Application made to or for:</strong>
<select id="herbicide_crop">
  <option value="invalid">Select from List</option>
  <option value="corn">Corn</option>
  <option value="soybean">Soybean</option>
  <option value="rye">Rye</option>
  <option value="wheat">Wheat</option>
  <option value="alfalfa">Alfalfa</option>
  <option value="other">Other</option>
</select>

<p>
<table>
<tr><th>Total liquid application rate: (optional)</th>
<td><input type="text" id="herbicide_total_rate" size="10" /></td>
<td>
<div id="herbicide_total_rate_units">
    <input type="radio" value="gpa" id="herbicide_total_rate_units_gpa" name="herbicide_total_rate_units" checked="checked"/><label for="herbicide_total_rate_units_gpa">gallons per acre</label>
    <input type="radio" value="lha" id="herbicide_total_rate_units_lha" name="herbicide_total_rate_units"/><label for="herbicide_total_rate_units_lha">L per hectare</label>
</div>
</td></tr>
<tr><th>Application Pressure: (optional)</th>
<td><input type="text" id="herbicide_pressure" size="10" /></td>
<td>
<div id="herbicide_pressure_units">
    <input type="radio" value="psi" id="herbicide_pressure_units_psi" name="herbicide_pressure_units" checked="checked"/><label for="herbicide_pressure_units_psi">psi</label>
    <input type="radio" value="kpa" id="herbicide_pressure_units_kpa" name="herbicide_pressure_units"/><label for="herbicide_pressure_units_kpa">kPa</label>
</div>
</td></tr>
</table>

<p><strong>Method of application:</strong>
<select id="herbicide_method">
  <option value="invalid">Select from List</option>
  <option value="ground">Ground</option>
  <option value="aerial">Aerial</option>
</select>

<p><table>
<tr>
 <th>Product Name Applied (example: Bicep II Magnum)</th>
 <th>Product Rate Applied</th>
 <th>Rate Units</th>
 <th></th>
</tr>

<tr><td>1. 
<input type="text" name="herbicide_product1" id="herbicide_product1" />
</td><td>
<input type="text" name="herbicide_rate1" id="herbicide_rate1" size="5"/>
</td>
<td>
<div id="herbicide_rate_units1a">
    <input type="radio" value="dry" id="herbicide_rate_units1a_dry" name="herbicide_rate_units1a"/><label for="herbicide_rate_units1a_dry">oz (dry)</label>
    <input type="radio" value="wet" id="herbicide_rate_units1a_wet" name="herbicide_rate_units1a" checked="checked"/><label for="herbicide_rate_units1a_wet">oz (liquid)</label>
</div>
</td>
<td>
<div id="herbicide_rate_units1b">
    <input type="radio" value="hectare" id="herbicide_rate_units1b_hectare" name="herbicide_rate_units1b"/><label for="herbicide_rate_units1b_hectare">per hectare</label>
    <input type="radio" value="acre" id="herbicide_rate_units1b_acre" name="herbicide_rate_units1b" checked="checked"/><label for="herbicide_rate_units1b_acre">per acre</label>
</div>
</td></tr>

<tr><td>2. 
<input type="text"  id="herbicide_product2" />
</td><td>
<input type="text" name="herbicide_rate2" id="herbicide_rate2" size="5"/>
</td><td>
<div id="herbicide_rate_units2a">
    <input type="radio" value="dry" id="herbicide_rate_units2a_dry" name="herbicide_rate_units2a"/><label for="herbicide_rate_units2a_dry">oz (dry)</label>
    <input type="radio" value="wet" id="herbicide_rate_units2a_wet" name="herbicide_rate_units2a" checked="checked"/><label for="herbicide_rate_units2a_wet">oz (liquid)</label>
</div>
</td>
<td>
<div id="herbicide_rate_units2b">
    <input type="radio" value="hectare" id="herbicide_rate_units2b_hectare" name="herbicide_rate_units2b"/><label for="herbicide_rate_units2b_hectare">per hectare</label>
    <input type="radio" value="acre" id="herbicide_rate_units2b_acre" name="herbicide_rate_units2b" checked="checked"/><label for="herbicide_rate_units2b_acre">per acre</label>
</div>
</td></tr>

<tr><td>3. 
<input type="text" name="herbicide_product3" id="herbicide_product3" />
</td><td>
<input type="text" name="herbicide_rate3" id="herbicide_rate3" size="5"/>
</td><td>
<div id="herbicide_rate_units3a">
    <input type="radio" value="dry" id="herbicide_rate_units3a_dry" name="herbicide_rate_units3a"/><label for="herbicide_rate_units3a_dry">oz (dry)</label>
    <input type="radio" value="wet" id="herbicide_rate_units3a_wet" name="herbicide_rate_units3a" checked="checked"/><label for="herbicide_rate_units3a_wet">oz (liquid)</label>
</div>
</td>
<td>
<div id="herbicide_rate_units3b">
    <input type="radio" value="hectare" id="herbicide_rate_units3b_hectare" name="herbicide_rate_units3b"/><label for="herbicide_rate_units3b_hectare">per hectare</label>
    <input type="radio" value="acre" id="herbicide_rate_units3b_acre" name="herbicide_rate_units3b" checked="checked"/><label for="herbicide_rate_units3b_acre">per acre</label>
</div>
</td></tr>

<tr><td>4. 
<input type="text" name="herbicide_product4" id="herbicide_product4" />
</td><td>
<input type="text" name="herbicide_rate4" id="herbicide_rate4" size="5"/>
</td><td>
<div id="herbicide_rate_units4a">
    <input type="radio" value="dry" id="herbicide_rate_units4a_dry" name="herbicide_rate_units4a"/><label for="herbicide_rate_units4a_dry">oz (dry)</label>
    <input type="radio" value="wet" id="herbicide_rate_units4a_wet" name="herbicide_rate_units4a" checked="checked"/><label for="herbicide_rate_units4a_wet">oz (liquid)</label>
</div>
</td>
<td>
<div id="herbicide_rate_units4b">
    <input type="radio" value="hectare" id="herbicide_rate_units4b_hectare" name="herbicide_rate_units4b"/><label for="herbicide_rate_units4b_hectare">per hectare</label>
    <input type="radio" value="acre" id="herbicide_rate_units4b_acre" name="herbicide_rate_units4b" checked="checked"/><label for="herbicide_rate_units4b_acre">per acre</label>
</div>
</td></tr>
</table>

<br clear="all" />
<p><strong>If an adjuvant was applied, select the appropriate type:</strong>
<select id="herbicide_adjuvant">
 <option value="invalid">Select from List</option>
 <option value="na">Not Applicable
 <option value="nis">Non-Ionic Surfactant (NIS)
 <option value="cropoil">Crop Oil
 <option value="other">Other
</select>
<p><strong>Target weeds</strong> (enter up to 10)
<table>
<tr><td>1.</td><td><input type="text" name="herbicide_target1" id="herbicide_target1" /></td>
<td>2.</td><td><input type="text" name="herbicide_target2" id="herbicide_target2" /></td></tr>
<tr><td>3.</td><td><input type="text" name="herbicide_target3" id="herbicide_target3" /></td>
<td>4.</td><td><input type="text" name="herbicide_target4" id="herbicide_target4" /></td></tr>
<tr><td>5.</td><td><input type="text" name="herbicide_target5" id="herbicide_target5" /></td>
<td>6.</td><td><input type="text" name="herbicide_target6" id="herbicide_target6" /></td></tr>
<tr><td>7.</td><td><input type="text" name="herbicide_target7" id="herbicide_target7" /></td>
<td>8.</td><td><input type="text" name="herbicide_target8" id="herbicide_target8" /></td></tr>
<tr><td>9.</td><td><input type="text" name="herbicide_target9" id="herbicide_target9" /></td>
<td>10.</td><td><input type="text" name="herbicide_target10" id="herbicide_target10" /></td></tr>
</table>

<p><strong>Developmental stage of crop e.g. V3</strong>
<br /><input type="text" name="herbicide_stage" id="herbicide_stage" maxlength="4" size="4" />

<p><strong>Justification for action.</strong> Note: This may include any measurements of pest abundance or relevant economic thresholds.
<textarea id="herbicide_justify" rows="4" cols="60"></textarea>

<p><strong>Reference.</strong> Note: This may include a discussion with an 
 IPM consultant (e.g., county or university extension specialists) or
 published guideline (e.g., online IPM newsletter) used in the decision
 making process that led to the use of a pest-management strategy (e.g.,
 application of herbicide, insecticide, or fungicide).
<br /><textarea id="herbicide_reference" rows="6" cols="60"></textarea>

<p><strong>Any additional comments?</strong>
<textarea id="herbicide_comments" rows="6" cols="60"></textarea>
</div><!-- End herbicide_options -->

<div id="insecticide_options" style="display: none;">

<p>Note: If you <strong>tank-mixed</strong> an insecticide with a herbicide or fungicide, enter each separately under their respective categories.

<p><strong>Timing:</strong>
<select name="insecticide_timing" id="insecticide_timing">
  <option value="invalid">Select from List</option>
  <option value="seed_applied">Seed Applied</option>
  <option value="in-furrow">In-furrow</option>
  <option value="post_emerge">Post Emerge</option>
</select>

<p><strong>Method of application:</strong>
<select id="insecticide_method">
  <option value="invalid">Select from List</option>
  <option value="ground">Ground</option>
  <option value="aerial">Aerial</option>
</select>

<p><strong>Application made to or for:</strong>
<select id="insecticide_crop">
  <option value="invalid">Select from List</option>
  <option value="corn">Corn</option>
  <option value="soybean">Soybean</option>
  <option value="rye">Rye</option>
  <option value="wheat">Wheat</option>
  <option value="alfalfa">Alfalfa</option>
  <option value="other">Other</option>
</select>

<p>
<table>
<tr><th>Total liquid application rate: (optional)</th>
<td><input type="text" id="insecticide_total_rate" size="10" /></td>
<td>
<div id="insecticide_total_rate_units">
    <input type="radio" value="gpa" id="insecticide_total_rate_units_gpa" name="insecticide_total_rate_units" checked="checked"/><label for="insecticide_total_rate_units_gpa">gallons per acre</label>
    <input type="radio" value="lha" id="insecticide_total_rate_units_lha" name="insecticide_total_rate_units"/><label for="insecticide_total_rate_units_lha">L per hectare</label>
</div>
</td></tr>
<tr><th>Application Pressure: (optional)</th>
<td><input type="text" id="insecticide_pressure" size="10" /></td>
<td>
<div id="insecticide_pressure_units">
    <input type="radio" value="psi" id="insecticide_pressure_units_psi" name="insecticide_pressure_units" checked="checked"/><label for="insecticide_pressure_units_psi">psi</label>
    <input type="radio" value="kpa" id="insecticide_pressure_units_kpa" name="insecticide_pressure_units"/><label for="insecticide_pressure_units_kpa">kPa</label>
</div>
</td></tr>
</table>

<p>
<table>
<tr>
 <th>Product Name Applied (example: Warrior)</th>
 <th>Product Rate Applied</th>
 <th>Rate Units</th>
 <th></th>
</tr>

<tr><td><input type="text" name="insecticide_product1" id="insecticide_product1" />
</td><td>
<input type="text" name="insecticide_rate1" id="insecticide_rate1" size="5"/>
</td>
<td>
<div id="insecticide_rate_units1a">
    <input type="radio" value="dry" id="insecticide_rate_units1a_dry" name="insecticide_rate_units1a"/><label for="insecticide_rate_units1a_dry">oz (dry)</label>
    <input type="radio" value="wet" id="insecticide_rate_units1a_wet" name="insecticide_rate_units1a" checked="checked"/><label for="insecticide_rate_units1a_wet">oz (liquid)</label>
</div>
</td>
<td>
<div id="insecticide_rate_units1b">
    <input type="radio" value="hectare" id="insecticide_rate_units1b_hectare" name="insecticide_rate_units1b"/><label for="insecticide_rate_units1b_hectare">per hectare</label>
    <input type="radio" value="acre" id="insecticide_rate_units1b_acre" name="insecticide_rate_units1b" checked="checked"/><label for="insecticide_rate_units1b_acre">per acre</label>
</div>
</td></tr>
</table>

<br clear="all" />
<p><strong>If an adjuvant was applied, select the appropriate type:</strong>
<select id="insecticide_adjuvant">
 <option value="invalid">Select from List</option>
 <option value="na">Not Applicable
 <option value="nis">Non-Ionic Surfactant (NIS)
 <option value="cropoil">Crop Oil
 <option value="other">Other
</select>

<p><strong>Target insects</strong> (enter up to 10)
<table>
<tr><td>1.</td><td><input type="text" name="insecticide_target1" id="insecticide_target1" /></td>
<td>2.</td><td><input type="text" name="insecticide_target2" id="insecticide_target2" /></td></tr>
<tr><td>3.</td><td><input type="text" name="insecticide_target3" id="insecticide_target3" /></td>
<td>4.</td><td><input type="text" name="insecticide_target4" id="insecticide_target4" /></td></tr>
<tr><td>5.</td><td><input type="text" name="insecticide_target5" id="insecticide_target5" /></td>
<td>6.</td><td><input type="text" name="insecticide_target6" id="insecticide_target6" /></td></tr>
<tr><td>7.</td><td><input type="text" name="insecticide_target7" id="insecticide_target7" /></td>
<td>8.</td><td><input type="text" name="insecticide_target8" id="insecticide_target8" /></td></tr>
<tr><td>9.</td><td><input type="text" name="insecticide_target9" id="insecticide_target9" /></td>
<td>10.</td><td><input type="text" name="insecticide_target10" id="insecticide_target10" /></td></tr>
</table>

<p><strong>Developmental stage of crop e.g. V3</strong>
<br /><input type="text" name="insecticide_stage" id="insecticide_stage" maxlength="4" size="4" />

<p><strong>Justification for action.</strong> Note: This may include any measurements of pest abundance or relevant economic thresholds.
<textarea id="insecticide_justify" rows="4" cols="60"></textarea>

<p><strong>Reference.</strong> Note: This may include a discussion with an 
 IPM consultant (e.g., county or university extension specialists) or
 published guideline (e.g., online IPM newsletter) used in the decision
 making process that led to the use of a pest-management strategy (e.g.,
 application of herbicide, insecticide, or fungicide).
<br /><textarea id="insecticide_reference" rows="6" cols="60"></textarea>

<p><strong>Any additional comments?</strong>
<textarea id="insecticide_comments" rows="6" cols="60"></textarea>
</div><!-- End insecticide_options -->

<div id="fungicide_options" style="display: none;">

<p>Note: If you <strong>tank-mixed</strong> a fungicide with a herbicide or insecticide, enter each separately under their respective categories.

<p><strong>Timing:</strong>
<select name="fungicide_timing" id="fungicide_timing">
  <option value="invalid">Select from List</option>
  <option value="seed_applied">Seed Applied</option>
  <option value="vegetative_application">Vegetative Application</option>
  <option value="reproductive_application">Reproductive Application</option>
</select>

<p><strong>Method of application:</strong>
<select id="fungicide_method">
  <option value="invalid">Select from List</option>
  <option value="ground">Ground</option>
  <option value="aerial">Aerial</option>
</select>

<p><strong>Application made to or for:</strong>
<select id="fungicide_crop">
  <option value="invalid">Select from List</option>
  <option value="corn">Corn</option>
  <option value="soybean">Soybean</option>
  <option value="rye">Rye</option>
  <option value="wheat">Wheat</option>
  <option value="alfalfa">Alfalfa</option>
  <option value="other">Other</option>
</select>

<p>
<table>
<tr><th>Total liquid application rate: (optional)</th>
<td><input type="text" id="fungicide_total_rate" size="10" /></td>
<td>
<div id="fungicide_total_rate_units">
    <input type="radio" value="gpa" id="fungicide_total_rate_units_gpa" name="fungicide_total_rate_units" checked="checked" /><label for="fungicide_total_rate_units_gpa">gallons per acre</label>
    <input type="radio" value="lha" id="fungicide_total_rate_units_lha" name="fungicide_total_rate_units"/><label for="fungicide_total_rate_units_lha">L per hectare</label>
</div>
</td></tr>
<tr><th>Application Pressure: (optional)</th>
<td><input type="text" id="fungicide_pressure" size="10" /></td>
<td>
<div id="fungicide_pressure_units">
    <input type="radio" value="psi" id="fungicide_pressure_units_psi" name="fungicide_pressure_units" checked="checked"/><label for="fungicide_pressure_units_psi">psi</label>
    <input type="radio" value="kpa" id="fungicide_pressure_units_kpa" name="fungicide_pressure_units"/><label for="fungicide_pressure_units_kpa">kPa</label>
</div>
</td></tr></table>

<p><table>
<tr>
 <th>Product Name Applied (example: Quilt)</th>
 <th>Product Rate Applied</th>
 <th>Rate Units</th>
 <th></th>
</tr>

<tr><td><input type="text" name="fungicide_product1" id="fungicide_product1" />
</td><td>
<input type="text" name="fungicide_rate1" id="fungicide_rate1" size="5"/>
</td>
<td>
<div id="fungicide_rate_units1a">
    <input type="radio" value="dry" id="fungicide_rate_units1a_dry" name="fungicide_rate_units1a"/><label for="fungicide_rate_units1a_dry">oz (dry)</label>
    <input type="radio" value="wet" id="fungicide_rate_units1a_wet" name="fungicide_rate_units1a" checked="checked"/><label for="fungicide_rate_units1a_wet">oz (liquid)</label>
</div>
</td>
<td>
<div id="fungicide_rate_units1b">
    <input type="radio" value="hectare" id="fungicide_rate_units1b_hectare" name="fungicide_rate_units1b"/><label for="fungicide_rate_units1b_hectare">per hectare</label>
    <input type="radio" value="acre" id="fungicide_rate_units1b_acre" name="fungicide_rate_units1b" checked="checked"/><label for="fungicide_rate_units1b_acre">per acre</label>
</div>
</td></tr>
</table>

<br clear="all" />
<p><strong>If an adjuvant was applied, select the appropriate type:</strong>
<select id="fungicide_adjuvant">
 <option value="invalid">Select from List</option>
 <option value="na">Not Applicable
 <option value="nis">Non-Ionic Surfactant (NIS)
 <option value="cropoil">Crop Oil
 <option value="other">Other
</select>

<p><strong>Target pathogens</strong> (enter up to 10)
<table>
<tr><td>1.</td><td><input type="text" name="fungicide_target1" id="fungicide_target1" /></td>
<td>2.</td><td><input type="text" name="fungicide_target2" id="fungicide_target2" /></td></tr>
<tr><td>3.</td><td><input type="text" name="fungicide_target3" id="fungicide_target3" /></td>
<td>4.</td><td><input type="text" name="fungicide_target4" id="fungicide_target4" /></td></tr>
<tr><td>5.</td><td><input type="text" name="fungicide_target5" id="fungicide_target5" /></td>
<td>6.</td><td><input type="text" name="fungicide_target6" id="fungicide_target6" /></td></tr>
<tr><td>7.</td><td><input type="text" name="fungicide_target7" id="fungicide_target7" /></td>
<td>8.</td><td><input type="text" name="fungicide_target8" id="fungicide_target8" /></td></tr>
<tr><td>9.</td><td><input type="text" name="fungicide_target9" id="fungicide_target9" /></td>
<td>10.</td><td><input type="text" name="fungicide_target10" id="fungicide_target10" /></td></tr>
</table>

<p><strong>Developmental stage of crop e.g. R1</strong>
<br /><input type="text" name="fungicide_stage" id="fungicide_stage" maxlength="4" size="4" />

<p><strong>Justification for action.</strong> Note: This may include any measurements of pest abundance or relevant economic thresholds.
<textarea id="fungicide_justify" rows="4" cols="60"></textarea>

<p><strong>Reference.</strong> Note: This may include a discussion with an 
 IPM consultant (e.g., county or university extension specialists) or
 published guideline (e.g., online IPM newsletter) used in the decision
 making process that led to the use of a pest-management strategy (e.g.,
 application of herbicide, insecticide, or fungicide).
<br /><textarea id="fungicide_reference" rows="6" cols="60"></textarea>

<p><strong>Any additional comments?</strong>
<textarea id="fungicide_comments" rows="6" cols="60"></textarea>
</div><!-- End fungicide_options -->

<p>
<input id="ipm_submitbutton" type="button" value="SUBMIT" onClick="javascript: saveIpmData();">
<input type="button" value="CLEAR FORM" onClick="javascript: clearIpmData();">

</div><!-- End tabs-3 -->
<div id="tabs-4">
<p><strong>Plot Map Upload</strong>

<p>Upload site map with plot designations noted. Maps will be used as needed to
clarify design and layout of plots and treatments. You can upload your site map 
depiction to this <a href="https://drive.google.com/#folders/0B6ZGw0coobCxaWtob2t5Tm1fSVE"
target="_new">Google Drive Folder</a>.  Please be sure to clearly note the field 
name within the filename.

<p><strong>GPS Coordinates</strong>

<p>Enter GPS coordinates for the 4 corners of your research plot.  You may
enter the values in either Degrees Minutes Seconds or Decimal format.

<table>
<tr><th>Corner</th><th>Longitude</th><th>Latitude</th></tr>
<tr><th>Northwest</th>
 <td><input type="text" id="nw_lon" size="15"></td>
 <td><input type="text" id="nw_lat" size="15"></td></tr>
<tr><th>Northeast</th>
 <td><input type="text" id="ne_lon" size="15"></td>
 <td><input type="text" id="ne_lat" size="15"></td></tr>
<tr><th>Southeast</th>
 <td><input type="text" id="se_lon" size="15"></td>
 <td><input type="text" id="se_lat" size="15"></td></tr>
<tr><th>Southwest</th>
 <td><input type="text" id="sw_lon" size="15"></td>
 <td><input type="text" id="sw_lat" size="15"></td></tr>
</table>

<p>If this site does not have 4 straight edges, upload the coordinates for the
perimeter into the box below.

<br /><textarea id="raw_lonlat" cols="60" rows="4"></textarea>

<p><input type="button" onClick="javascript: saveLonLat();" value="SAVE COORDINATES"/>

</div><!-- End tabs-4 -->
<div id="tabs-5">

<p>Enter additional information here that does not fit within
other tabs. Examples of this include installation dates for field equipment,
a weather station that went down for a month, etc. These notes are associated
with the crop year selected above.

<p>Please also include notes regarding missing data here. Specifically,
 if you have more than 1 plot per site missing due to the same issue,
 include the details here. If it is only 1 plot that had a problem,
 then include a comment specifically within that spreadsheet for that
 data variable.

<br /><textarea id="notes" cols="60" rows="4"></textarea>

<p><input type="button" onClick="javascript: saveNotes();" value="SAVE NOTES"/>


</div><!-- End tabs-5 -->
</div>
</div>
</form>
</div>

   <div id="error" style="display: none;background-color:yellow;font-size:xx-small;" title="An error occured processing your request">
       <div id="error_code"><u>code:</u></div>
       <div id="error_uri"><u>uri:</u></div>
       <div id="error_description"><u>description:</u></div>
       <div id="error_explanation"><u>explanation:</u></div>
       <div id="error_trace"><u>trace:</u></div>
    </div>

<script type="text/javascript">
/* TODO
 * - If it varies by experimental plot, enter the estimated cover on a plot 
> basis (We will need people to enter % values on a per plot basis
> somewhere... where does this make the most sense? On the spreadsheet
> where they have to enter plot numbers to the treatments??)

 * Make fonts appear the same for all portions of the form.
 */
  var operationsFeed = null;
  var service;
  var sites = [];
  var sitekeys = [];
  var operationsEntry;
  var managementEntry;
  var ipmEntry;
  var myYear = 2011;
  var man_sskey = '0AqZGw0coobCxdE9wN2J4aVE1bUthdWFsWjNrYURHWGc';
  var site_sskey = '0AugT6NSY_M5HdGd2cFV2WXBCQlJZNDRoSjZ0Qml4bnc';
  var SITES_FEED;
  var FERTELEM = [ 'nitrogen', 'phosphorus', 'phosphate', 'potassium', 
    'potash', 'sulfur', 'calcium', 
           'magnesium', 'zinc','iron'];
  var MANURE_ELEM = [ 'nitrogen', 'phosphate', 
    'potash', 'sulfur', 'calcium', 'magnesium'];

function dei(id) { return document.getElementById(id); }
function loadNotes(){
  var selectedSite = dei("siteSelector").value;
  var siteEntry = 0;
  for (var i=0, n=SITES_FEED.entry.length; i < n; i++) {
        var entry = SITES_FEED.entry[i];
        if (entry.gsx$uniqueid.$t == selectedSite){
           siteEntry = entry;
        }
  }
  if (siteEntry == 0){
    dei("notes").value = "";
    return;
  }

  dei("notes").value = siteEntry['gsx$notes'+myYear].$t;
}

function loadLonLat(){
  var keys = ['nw_lon','nw_lat','ne_lon','ne_lat','se_lon','se_lat','sw_lon','sw_lat', 'raw_lonlat'];

  var selectedSite = dei("siteSelector").value;
  var siteEntry = 0;
  for (var i=0, n=SITES_FEED.entry.length; i < n; i++) {
        var entry = SITES_FEED.entry[i];
        if (entry.gsx$uniqueid.$t == selectedSite){
           siteEntry = entry;
        }
  }
  if (siteEntry == 0){
    return;
  }

  for (var i=0;i<keys.length;i++){
    var key = keys[i];
    dei(keys[i]).value = siteEntry['gsx$'+ key.replace("_","")].$t;
  }
}

/* Save the Notes Information Entered */
function saveNotes(){
  var selectedSite = dei("siteSelector").value;
  var siteEntry = 0;
  for (var i=0, n=SITES_FEED.entry.length; i < n; i++) {
        var entry = SITES_FEED.entry[i];
        if (entry.gsx$uniqueid.$t == selectedSite){
           siteEntry = entry;
        }
  }
  if (siteEntry == 0){
    alert("Could not find site for saveNotes");
    return;
  }

  var val = dei("notes").value;
  siteEntry['gsx$notes'+myYear] = {$t: val};
  function good(entry){
        alert('Notes Entry was saved!');
  }
  service.setHeaders({"If-Match": "*"});
  service.updateEntry(siteEntry.id.$t, siteEntry, good, getFeed_eb);
  //service.setHeaders({"If-Match": null});
}

/* Save the Lat/Lon Information Entered */
function saveLonLat(){
  var keys = ['nw_lon','nw_lat','ne_lon','ne_lat','se_lon','se_lat','sw_lon','sw_lat', 'raw_lonlat'];

  var selectedSite = dei("siteSelector").value;
  var siteEntry = 0;
  for (var i=0, n=SITES_FEED.entry.length; i < n; i++) {
        var entry = SITES_FEED.entry[i];
        if (entry.gsx$uniqueid.$t == selectedSite){
           siteEntry = entry;
        }
  }
  if (siteEntry == 0){
    alert("Could not find site for saveLonLat");
    return;
  }

  for (var i=0;i<keys.length;i++){
    var key = keys[i];
    var val = dei(keys[i]).value;
    //if (val.trim() == "" && key != 'raw_lonlat'){
    //  alert("Coordinate "+ keys[i] +" invalid, try again");
    //  return;
    //}
    siteEntry['gsx$'+key.replace("_","")] = {$t: val};
  }
    function good(entry){
        alert('LonLat Entry was saved!');
    }
    service.setHeaders({"If-Match": "*"});
    service.updateEntry(siteEntry.id.$t, siteEntry, good, getFeed_eb);
    //service.setHeaders({"If-Match": null});
}
function showFertilizerInjection(val){
  var el = dei("fertilizer_injected_options");
  if (val.search("injected") == 0){
    el.style.display = 'block';
  } else {
    el.style.display = 'none';
  }
}
function showManureMethodOptions(val){
  var el = dei("manure_method_options");
  if (val == "injected"){
    el.style.display = 'block';
  } else {
    el.style.display = 'none';
  }
}
function showManureStabilizer(val){
  var el = dei("manure_stabilizer_options");
  if (val == "yes"){
    el.style.display = 'block';
  } else {
    el.style.display = 'none';
  }
}
function showFertilizerStabilizer(val){
  var el = dei("fertilizer_stabilizer_options");
  if (val == "yes"){
    el.style.display = 'block';
  } else {
    el.style.display = 'none';
  }
}

function irrigation_show(val){
  var el = dei("irrigation-sub");
  if (val == "yes"){
    el.style.display = 'block';
  } else {
    el.style.display = 'none';
  }
}

function residue_show(val){
  var elyes = dei("residue-sub-yes");
  var elno = dei("residue-sub-no");
  if (val == "yes" || val == "varies"){
    elyes.style.display = 'block';
    elno.style.display = 'none';
  } else {
    elyes.style.display = 'none';
    elno.style.display = 'block';
  }
}

/* Sets the current operation */
function setOperation(op_code){
    if (op_code == "invalid"){ return; }
    var tokens = op_code.split("_");
    showOneOperation( tokens[0], tokens[1] );
}

/* Sets the current IPM operation */
function setIpmOperation(op_code){
    if (op_code == "invalid"){ return; }
    showOneIpmOperation( op_code );
}

/* Sets the current site */
function setSite(site_code){
    dei("theform").style.display = 'block';
    downloadOperations();
    loadPreviousManagement();
    loadPreviousIpm();
    loadLonLat();
    loadNotes();
}

/* Sets the current year, simple enough */
function setYear(year){
    myYear = year;
    dei("foryear").innerHTML = year;
    dei("ipm_foryear").innerHTML = year;
    downloadOperations();
    loadPreviousManagement();
    loadPreviousIpm();
    loadLonLat();
    loadNotes();
}

function resetNitrogenRate(){
    for (var i=0; i < FERTELEM.length; i++){
        var elem = FERTELEM[i];
        dei(elem + "_rate").value = 0;
        dei(elem + "_rate_lbs").value = 0;
    }
}

/* Reset the app */
function clearData(){
    dei("previousOperationEdit").style.display = 'none';
    dei("rowid").value = "0";
    dei("plant_hybrid").value = "";
    dei("plant_maturity").value = "";
    dei("plant_rate").value = "";
    dei("comments").value = "";
    resetNitrogenRate();
    dei("submitbutton").value = "SUBMIT";
}

function clearIpmData(){
    dei("previousIpmOperationEdit").style.display = 'none';
    dei("ipm_rowid").value = "0";
    var ops = ['herbicide','insecticide','pesticide'];
    var fields = ['comments','justify', 'stage', 'reference'];
    for (var o=0; o < ops.length; o++){
       for (var f=0; f < fields.length; f++){
          var el = dei(ops[o]+"_"+fields[f]);
          if (el){ el.value = ""; }
       }
    }
    dei("ipm_submitbutton").value = "SUBMIT";
}

function clearManagementData(){
    dei("previousOperationEdit").style.display = 'none';
    dei("rowid").value = "0";
    dei("plant_hybrid").value = "";
    dei("plant_maturity").value = "";
    dei("plant_rate").value = "";
    dei("organic_amendments_text").value = "";
    dei("submitbutton2").value = "SUBMIT";
}

/* Return an operation entry for the given id */
function getIpmEntryByRowId(myrowid){
    for (var i=0, n=ipmEntry.length; i < n; i++) {
        var entry = ipmEntry[i];
        var tokens = entry.id.$t.split("/");
        var rowid = tokens[ tokens.length - 1];
        if (myrowid == rowid){ return entry; }
    }
    return null;
}

/* Return an operation entry for the given id */
function getOperationEntryByRowId(myrowid){
    for (var i=0, n=operationsEntry.length; i < n; i++) {
        var entry = operationsEntry[i];
        var tokens = entry.id.$t.split("/");
        var rowid = tokens[ tokens.length - 1];
        if (myrowid == rowid){ return entry; }
    }
    return null;
}

/* Delete a row from the database table */
function deleteRow(myrowid){
    var y = window.confirm("Are you sure you wish to remove this entry?");
    if (!y){
      return;
    }
    var entry = getOperationEntryByRowId(myrowid);
    var url = entry.id.$t;
    function good(){ 
      operationsFeed = null;
      downloadOperations();
      alert("Row Deleted!"); 
    }
    service.setHeaders({"If-Match": "*"});
    service.deleteEntry(url, good, getFeed_eb);
    //service.setHeaders({"If-Match": null});
}

/* Delete a row from the database table */
function deleteIpmRow(myrowid){
    var y = window.confirm("Are you sure you wish to remove this entry?");
    if (!y){
      return;
    }
    var entry = getIpmEntryByRowId(myrowid);
    var url = entry.id.$t;
    function good(){ 
        loadPreviousIpm();
        alert("Row Deleted!"); 
    }
    service.setHeaders({"If-Match": "*"});
    service.deleteEntry(url, good, getFeed_eb);
    //service.setHeaders({"If-Match": null});
}

/*
 * Edit a pre-existing IPM Entry!
 */
function editIpmRow(myrowid){
    document.getElementById("ipm_rowid").value = myrowid;
    document.getElementById("previousIpmOperationEdit").style.display = 'block';
    var entry = getIpmEntryByRowId(myrowid);
    var op = entry['gsx$operation'].$t;
    setIpmOperation(op);
    dei("ipm_operation").value = op;
    $("#ipm_datepicker").datepicker("setDate", entry.gsx$date.$t);
    
    document.getElementById(op+"_crop").value = entry['gsx$crop'].$t;
    if (op != "seed"){
document.getElementById(op+"_timing").value = entry['gsx$timing'].$t;
    document.getElementById(op+"_method").value = entry['gsx$method'].$t;
    var totalrate = parseFloat(entry['gsx$totalrate'].$t);
    if (totalrate > 0){
      /* Convert LPH to GPA */
      document.getElementById(op+"_total_rate").value = totalrate / 9.3539;
      $("#"+op+"_total_rate_units_gpa").click();
    } else {
      document.getElementById(op+"_total_rate").value = '';
    }
    var pressure = parseFloat(entry['gsx$pressure'].$t);
    if (pressure > 0){
      /* convert kPa to PSI */
      document.getElementById(op+"_pressure").value = pressure / 6.89475;
      $("#"+op+"_pressure_units_psi").click();
    } else {
      document.getElementById(op+"_pressure").value = '';
    }
    document.getElementById(op+"_adjuvant").value = entry['gsx$adjuvant'].$t;
    document.getElementById(op+"_stage").value = entry['gsx$stage'].$t;
    }

    for(var i=1;i<5;i++){
        if (i > 1 && op != 'herbicide'  && op != 'seed'){ continue; }
        document.getElementById(op+"_product"+i).value = entry['gsx$product'+i].$t;
        if (op != 'seed'){
        document.getElementById(op+"_rate"+i).value = entry['gsx$rate'+i].$t;
        var ra = entry['gsx$rateunit'+i].$t;
        if (ra != null && ra != ""){
            $("#"+op+"_rate_units"+i+"a_"+ra).click();
            $("#"+op+"_rate_units"+i+"b_acre").click();
            /* Hectre to Acre */
            if (parseFloat(entry['gsx$rate'+i].$t) > 0){
                document.getElementById(op+"_rate"+i).value = parseFloat(entry['gsx$rate'+i].$t) / 2.47105;
            }
        }
   
}
    }

if (op != "seed"){
    for(var i=1;i<11;i++){
       document.getElementById(op+"_target"+i).value = entry['gsx$target'+i].$t;
    }
}
    document.getElementById(op+"_justify").value = entry['gsx$justify'].$t;
    document.getElementById(op+"_reference").value = entry['gsx$reference'].$t;
    document.getElementById(op+"_comments").value = entry['gsx$comments'].$t;
    document.getElementById("ipm_submitbutton").value = "Save Edit of Previous Operation";
}

function modify_plant_rate(entry){
    /* Set the units for planting rate */
    var pru = entry.gsx$plantrateunits.$t;
    var pr = entry.gsx$plantrate.$t;
    if (pr != null && pr != ""){
      if (pru == 'seeds'){
        $("#plant_rate_units_acre").click();
        document.getElementById("plant_rate").value = parseFloat(pr) / 2.47105;
      } else if (pru == 'lbs'){
        $("#plant_rate_units_lbs").click();
        document.getElementById("plant_rate").value = parseFloat(pr);
      } else if (pru == 'kg'){
        $("#plant_rate_units_lbs").click();
        document.getElementById("plant_rate").value = parseFloat(pr) / 2.47105;
      }
    } else {
        $("#plant_rate_units_acre").click();
        document.getElementById("plant_rate").value = "";
   }
}
function modify_tillage_depth(entry){
  var td = entry.gsx$depth.$t;
  if (td != null && td != ""){
    document.getElementById("tillage_depth").value = parseFloat(td) / 2.54;
    $("#tillage_depth_units_in").click();
  }
}
function modify_manure(entry){
    /* 
       1 gallons/acre = 9.353956 L/ha 
       1 L/ha = 0.1069066 gallons/acre
       1 T/acre =  2.2417400 Mg/ha
       1 Mg/ha = 0.4460821 T/acre
     */
    document.getElementById("manure_rate").value = entry.gsx$manurerate.$t;
    var mru = entry.gsx$manurerateunits.$t;
    if (mru != null && mru != ""){
       $('#manure_rate_units_'+mru).click();
    }
    document.getElementById("manure_source").value = entry.gsx$manuresource.$t;
    document.getElementById("manure_composition").value = entry.gsx$manurecomposition.$t;
    document.getElementById("manure_method").value = entry.gsx$manuremethod.$t;
    showManureMethodOptions(entry.gsx$manuremethod.$t);    
    if (entry.gsx$manuremethod.$t == 'injected'){
      var val = parseFloat(entry.gsx$depth.$t ) / 2.54;
      $('#manure_injection_depth_units_inch').click();
      document.getElementById("manure_injection_depth").value = val;
    } else {
      document.getElementById("manure_injection_depth").value = "";
    }
   
    for (var i=0; i < MANURE_ELEM.length; ++i) {
        var elem = MANURE_ELEM[i];
        document.getElementById("manure_"+elem+"_rate").value = entry['gsx$'+ elem].$t;
    }
}

/* Load up the data interface with a pre-existing row! */
function editOperationRow(myrowid){
    
    document.getElementById("previousOperationEdit").style.display = 'block';
    var entry = getOperationEntryByRowId(myrowid);
    /* Load up form based on this entry! */
    document.getElementById("comments").value = entry.gsx$comments.$t;
    document.getElementById("rowid").value = myrowid;
    dei("operation").value = entry.gsx$operation.$t;
    dei("plant_rye_options").value = entry.gsx$plantryemethod.$t;
    document.getElementById("plant_hybrid").value = entry.gsx$planthybrid.$t;
    document.getElementById("plant_maturity").value = entry.gsx$plantmaturity.$t;

    modify_plant_rate(entry);
    modify_tillage_depth(entry);
    modify_manure(entry);

    document.getElementById("lime_rate").value = entry.gsx$limerate.$t;
document.getElementById("soiladmend_current_ph").value = entry.gsx$currentph.$t;
document.getElementById("soiladmend_target_ph").value = entry.gsx$targetph.$t;
document.getElementById("soiladmend_neutral_index").value = entry.gsx$neutralindex.$t;
    

    document.getElementById("terminate_method").value = entry.gsx$terminatemethod.$t;
    setOperation(entry.gsx$operation.$t);
    $("#datepicker").datepicker("setDate", entry.gsx$date.$t);
    $("#tdatepicker1").datepicker("setDate", entry.gsx$biomassdate1.$t);
    $("#tdatepicker2").datepicker("setDate", entry.gsx$biomassdate2.$t);
    dei("foryear").innerHTML = entry.gsx$cropyear.$t;
    dei("submitbutton").value = "Save Edit of Previous Operation";

    dei("fertilizer_form").value = entry.gsx$fertilizerform.$t;
    dei("fertilizer_crop").value = entry.gsx$fertilizercrop.$t;
    dei("fertilizer_apptype").value = entry.gsx$fertilizerapptype.$t;
    showFertilizerInjection(entry.gsx$fertilizerapptype.$t);
    dei("fertilizer_formulation").value = entry.gsx$fertilizerformulation.$t;
    dei("fertilizer_stabilizer_used").value = entry.gsx$stabilizerused.$t;
    dei("fertilizer_stabilizer_name").value = entry.gsx$stabilizername.$t;
    dei("fertilizer_stabilizer").value = entry.gsx$stabilizer.$t;
    dei("sample_growth_stage").value = entry.gsx$growthstage.$t;
    dei("sample_height").value = entry.gsx$canopyheight.$t;
    var fertilizer_depth = parseFloat(entry.gsx$depth.$t);
    if (fertilizer_depth > 0){
      dei("fertilizer_injection_depth").value = fertilizer_depth / 2.54;
    }  else {
      document.getElementById("fertilizer_injection_depth").value = "";
    }
    $('#fertilizer_injection_depth_units_inch').click();

    document.getElementById("manure_stabilizer_used").value = entry.gsx$stabilizerused.$t;
    showManureStabilizer(entry.gsx$stabilizerused.$t);
    document.getElementById("manure_stabilizer_name").value = entry.gsx$stabilizername.$t;
    document.getElementById("manure_stabilizer").value = entry.gsx$stabilizer.$t;
    document.getElementById("product_rate").value = entry.gsx$productrate.$t;
    resetNitrogenRate();  
    var pr = parseFloat(entry.gsx$productrate.$t);
    if (pr > 0){
      for (var i=0; i < FERTELEM.length; i++){
        var elem = FERTELEM[i];
        document.getElementById(elem + "_rate").value = entry['gsx$'+elem].$t;
      }
      document.getElementById("product_rate").value = pr / 1.12;
    }
    if (pr == -1){
      for (var i=0; i < FERTELEM.length; i++){
        var elem = FERTELEM[i];
        document.getElementById(elem + "_rate_lbs").value = entry['gsx$'+elem].$t;
      }
    }
}

/*
 * Load entries from the IPM worksheet on the managemnet spreadsheet
 */
function loadPreviousIpm(){
  url = 'https://spreadsheets.google.com/feeds/list/'+ man_sskey +'/3/private/full';
  var callback = function(response) {
      if (response.oauthApprovalUrl) {
      } else if (response.feed) {
          var sheetFeed = response.feed;
          ipmEntry = sheetFeed.entry;
          var prevIpmOpsUL = document.getElementById("previousIpmOperations");
          while(prevIpmOpsUL.hasChildNodes()){
              prevIpmOpsUL.removeChild(prevIpmOpsUL.childNodes[0]);
          }
          if (ipmEntry){
            for(var i=0, n=ipmEntry.length; i < n; i++) {
              var entry = ipmEntry[i];
              if (entry.gsx$cropyear.$t == myYear  && entry.gsx$uniqueid.$t == document.getElementById("siteSelector").value){
 var tokens = entry.id.$t.split("/");
  var rowid = tokens[ tokens.length - 1];
  var ipmdate = new Date(entry.gsx$date.$t);
   txt = entry.gsx$date.$t +" "+ entry.gsx$operation.$t +" <a href='#' onClick='javascript: editIpmRow(\""+ rowid +"\");'>Edit</a> &nbsp; <a href='#' onClick='javascript: deleteIpmRow(\""+ rowid +"\");'>Delete</a> &nbsp; <a href='#' onClick='javascript: editIpmRow(\""+ rowid +"\");'>View</a>";
                  var li = document.createElement("li");
                  li.setAttribute("opdate", entry.gsx$date.$t);
                  li.innerHTML = txt;
                  if (prevIpmOpsUL.children.length == 0){
                     prevIpmOpsUL.appendChild(li);
                  } else {
                    var placed = false;
                    for (var c=0; c < prevIpmOpsUL.children.length; c++){
                      var ldate = new Date(prevIpmOpsUL.children[c].getAttribute("opdate"));
                      if (ipmdate < ldate){
                         prevIpmOpsUL.insertBefore(li, prevIpmOpsUL.children[c]);
                         placed = true;
                         break;
                      }
                    }
                    if (! placed){
                      prevIpmOpsUL.appendChild(li);
                    }
                 }
              }
            }
          }
          if (prevIpmOpsUL.children.length == 0){
              var li = document.createElement("li");
              li.innerHTML = 'No Pesticide operations found...';
              prevIpmOpsUL.appendChild(li);
          }
      } else {
            var main = document.getElementById('main');
            var err = document.createTextNode('1. Something went wrong');
            main.appendChild(err);
            showOneSection('main');
      }
  };
  service.getFeed(url, callback, getFeed_eb);
}

function getFeed_eb(errorObj){
  //if (errorObj.cause.getStatus() == 304) 
  msg = errorObj.cause ? errorObj.cause.statusText : errorObj.message;
  var main = document.getElementById('main');
  var err = document.createTextNode('API ERROR: '+ msg);
  main.appendChild(err);
  main.appendChild(document.createElement('br'));
  showOneSection('main');
}


function loadPreviousManagement(){
  url = 'https://spreadsheets.google.com/feeds/list/'+ man_sskey +'/2/private/full';
  var callback = function(response) {
      if (response.oauthApprovalUrl) {
      } else if (response.feed) {
          var sheetFeed = response.feed;
          managementEntry = sheetFeed.entry;
          foundData = false;
          if (managementEntry){ for(var i=0, n=managementEntry.length; i < n; i++) {
              var entry = managementEntry[i];
              if (entry.gsx$cropyear.$t != myYear || entry.gsx$uniqueid.$t != document.getElementById("siteSelector").value){ continue; }
/* Assign form values based on this entry */
foundData = true;
dei("lime_year").value = entry.gsx$limeyear.$t;
dei("residue_how").value = entry.gsx$residuehow.$t;
residue_show(entry.gsx$residuehow.$t);
dei("residue_removal").value = entry.gsx$residueremoval.$t;
dei("residue_type").value = entry.gsx$residuetype.$t;
dei("residue_planting").value = entry.gsx$residueplantingpercentage.$t;
var rbw = entry.gsx$residuebiomassweight.$t;
if (rbw != null && rbw != ""){
  rbw = parseFloat(rbw) / 1.1208;
  dei("residue_biomass_weight").value = rbw; 
}
dei("residue_biomass_moisture").value = entry.gsx$residuebiomassmoisture.$t;
dei("organic_amendments").value = entry.gsx$organicamendments.$t;
dei("organic_amendments_text").value = entry.gsx$organicamendmentstext.$t;
dei("irrigation").value = entry.gsx$irrigation.$t;
var ia = entry.gsx$irrigationamount.$t;
if (ia != null && ia != "" && ia != "n/a"){
  dei("irrigation_amount").value = parseFloat(ia) / 2.54;
  $("#irrigation_units_in").click();
}
dei("irrigation_method").value = entry.gsx$irrigationmethod.$t;
dei("management_comments").value = entry.gsx$comments.$t;
              
          }}
          if (!foundData){
dei("managementTabMessage").style.display = 'block';
dei("lime_year").value = '';
dei("residue_how").value = 'invalid';
dei("residue_removal").value = 'invalid';
dei("residue_type").value = 'invalid';
dei("residue_planting").value = 'invalid';
dei("residue_biomass_weight").value = 'invalid'; 
dei("residue_biomass_moisture").value = '';
dei("organic_amendments").value = 'invalid';
dei("organic_amendments_text").value = '';
dei("irrigation").value = '';
dei("irrigation_amount").value = '0';
$("#irrigation_units_in").click();
dei("irrigation_method").value = '';
dei("management_comments").value = '';
          } else {
dei("managementTabMessage").style.display = 'none';
          }
      } else {
            var main = document.getElementById('main');
            var err = document.createTextNode('4. Something went wrong');
            main.appendChild(err);
            showOneSection('main');
      }
  };
  service.getFeed(url, callback, getFeed_eb);
}

function downloadOperations(){
  /* Download and save to a var the remote spreadsheet */
  if (operationsFeed != null){
    parseOperations();
    return;
  }
  loadingPreviousOperations();
  url = 'https://spreadsheets.google.com/feeds/list/'+ man_sskey +'/1/private/full';
  var callback = function(response) {
    if (response.oauthApprovalUrl) {
    } else if (response.feed) {
      operationsFeed = response.feed;
      parseOperations();
    } else {
       var main = document.getElementById('main');
       var err = document.createTextNode('3. Something went wrong');
       main.appendChild(err);
    }
  }; // End of callback()

  service.getFeed(url, callback, getFeed_eb);
}

function loadingPreviousOperations(){
  zeroPreviousOperations();  
  var prevOpsUL = document.getElementById("previousOperations");
  var li = document.createElement("li");
  li.innerHTML = 'Standby, processing...';
  prevOpsUL.appendChild(li);
}


function zeroPreviousOperations(){
  var prevOpsUL = document.getElementById("previousOperations");
  while(prevOpsUL.hasChildNodes()){
    prevOpsUL.removeChild(prevOpsUL.childNodes[0]);
  }
}

function parseOperations(){
  if (operationsFeed == null || operationsFeed.entry == null){
    return;
  }
  /* Parse through the operations and update the page as necessary */
  operationsEntry = operationsFeed.entry;
    

  /* Clear old entries */
  var prevOpsUL = document.getElementById("previousOperations");
  zeroPreviousOperations();
            
  for(var i=0, n=operationsEntry.length; i < n; i++) {
    var entry = operationsEntry[i];
    if (entry.gsx$cropyear.$t != myYear || entry.gsx$uniqueid.$t != document.getElementById("siteSelector").value){
      continue;
    }
    var tokens = entry.id.$t.split("/");
    var opdate = new Date(entry.gsx$date.$t);
    var rowid = tokens[ tokens.length - 1];
    txt = entry.gsx$date.$t +" "+ entry.gsx$operation.$t +" <a href='#' onClick='javascript: editOperationRow(\""+ rowid +"\");'>Edit</a> &nbsp; <a href='#' onClick='javascript: deleteRow(\""+ rowid +"\");'>Delete</a> &nbsp; <a href='#' onClick='javascript: editOperationRow(\""+ rowid +"\");'>View</a>";
    var li = document.createElement("li");
    li.setAttribute("opdate", entry.gsx$date.$t);
    li.innerHTML = txt;
    if (prevOpsUL.children.length == 0){
      prevOpsUL.appendChild(li);
    } else {
      var placed = false;
      for (var c=0; c < prevOpsUL.children.length; c++){
        var ldate = new Date(prevOpsUL.children[c].getAttribute("opdate"));
        if (opdate < ldate){
          prevOpsUL.insertBefore(li, prevOpsUL.children[c]);
          placed = true;
          break;
        }
      }
      if (! placed){
        prevOpsUL.appendChild(li);
      }
    }  
  } // End of for
  if (prevOpsUL.children.length == 0){
    var li = document.createElement("li");
    li.innerHTML = 'No management operations found...';
    prevOpsUL.appendChild(li);
  }
}; // end of parseOperations



function showOneIpmOperation(toshow) {
    var sections = [ 'herbicide', 'insecticide', 'fungicide', 'seed' ];
    for (var i=0; i < sections.length; ++i) {
        var s = sections[i];
        var el = document.getElementById(s+"_options");
        if (s === toshow) {
            el.style.display = "block";
        } else {
            el.style.display = "none";
        }
    }
    if (toshow == "seed"){
        document.getElementById("ipm_date").style.display = 'none';
    } else {
        document.getElementById("ipm_date").style.display = 'block';
    }
}

function showOneOperation(toshow, subtype) {
    var sections = [ 'plant', 'harvest', 'tillage', 
        'fertilizer', 'soiladmend', 'manure','termination', 
        'termination2', 'sample'];
    var sectionLabels = [ 'Planting Date:', 'Harvest Date:', 'Tillage Date:', 
        'Fertilizer Date:', 'Lime Application Date:', 
        'Manure Application Date:', 'Termination Date', '', 'Sampling Date'];
    for (var i=0; i < sections.length; ++i) {
        var s = sections[i];
        var el = document.getElementById(s+"_options");
        if (s === toshow) {
            dei("operation_label").innerHTML = sectionLabels[i];
            
            el.style.display = "block";
        } else if (s === 'termination2' && (toshow === 'harvest' || toshow === 'termination')){
            el.style.display = "block";
        } else {
            el.style.display = "none";
        }
    }
    // Hack for plant_rye_options
    if (toshow == 'plant' && subtype.substring(0,3) == 'rye'){
        dei("plant_rye_options").style.display = 'block';
    } else {
        dei("plant_rye_options").style.display = 'none';
    }
}

      function showOneSection(toshow) {
        var sections = [ 'loading', 'main', 'approval', 'waiting' ];
        for (var i=0; i < sections.length; ++i) {
          var s = sections[i];
          var el = document.getElementById(s);
          if (s === toshow) {
            el.style.display = "block";
          } else {
            el.style.display = "none";
          }
        }
      }

/*
 * Called from clicking on the save data button
 */
function saveIpmData(){
    var operation = document.getElementById("ipm_operation").value;
    if (operation == 'invalid'){
       alert("Please select a valid IPM Operation");
       return;
    }

    if (document.getElementById("ipm_rowid").value != "0"){ 
        var myrowid = document.getElementById("ipm_rowid").value;
        entry = getIpmEntryByRowId(myrowid);
        url = entry.id.$t;
     } else {
      url = 'https://spreadsheets.google.com/feeds/list/'+ man_sskey +'/3/private/full';
      entry = {xmlns: "http://www.w3.org/2005/Atom", 
         xmlns$gsx: "http://schemas.google.com/spreadsheets/2006/extended"};
      }
    entry['gsx$uniqueid'] = {$t:document.getElementById("siteSelector").value};
    entry['gsx$cropyear'] = {$t:myYear};
    entry['gsx$operation'] = {$t: operation};
    for(var i=1;i<5;i++){  
        if (i > 1 && operation != 'herbicide' && operation != 'seed'){ continue; }  
        entry['gsx$product'+i] = {$t: document.getElementById(operation+"_product"+i).value};
        if (operation != "seed"){
          var rate = parseFloat(document.getElementById(operation+"_rate"+i).value);
        
          var units = $('#'+operation+'_rate_units'+i+'a :radio:checked')[0].value;
          entry['gsx$rateunit'+i] = {$t: units};
          var units2 = $('#'+operation+'_rate_units'+i+'b :radio:checked')[0].value;
          if (units2 == 'acre'){ rate = rate * 2.47105; }
          entry['gsx$rate'+i] = {$t: rate};
        }
    }
    entry['gsx$crop'] = {$t: document.getElementById(operation+"_crop").value};

    if (operation != "seed"){
      var units = $('#'+operation+'_total_rate_units :radio:checked')[0].value;
      var rate = parseFloat(document.getElementById(operation+"_total_rate").value);
      if (units == 'gpa'){ rate = rate * 9.3539; }
      entry['gsx$totalrate'] = {$t: rate};

      var units = $('#'+operation+'_pressure_units :radio:checked')[0].value;
      var rate = parseFloat(document.getElementById(operation+"_pressure").value);
      if (units == 'psi'){ rate = rate * 6.89475; }
      entry['gsx$pressure'] = {$t: rate};

      entry['gsx$adjuvant'] = {$t: document.getElementById(operation+"_adjuvant").value};
      entry['gsx$method'] = {$t: document.getElementById(operation+"_method").value};
      entry['gsx$stage'] = {$t: document.getElementById(operation+"_stage").value};
      for(var i=1;i<11;i++){
         entry["gsx$target"+i] = {$t: document.getElementById(operation+"_target"+i).value};
      }
    }
    entry['gsx$timing'] = {$t: document.getElementById(operation+"_timing").value};
    entry['gsx$justify'] = {$t: document.getElementById(operation+"_justify").value};
    entry['gsx$reference'] = {$t: document.getElementById(operation+"_reference").value}; 
    entry['gsx$comments'] = {$t: document.getElementById(operation+"_comments").value};
    if (operation != 'seed'){
        entry['gsx$date'] = {$t:
  $.datepicker.formatDate("yy/mm/dd", $("#ipm_datepicker").datepicker("getDate"))};
    }
    entry['gsx$updated'] = {$t: new Date()};
    function good(entry){
        alert('IPM Entry was saved!');
        loadPreviousIpm();
    }
      if (document.getElementById("ipm_rowid").value != "0"){
          service.setHeaders({"If-Match": "*"});
          service.updateEntry(entry.id.$t, entry, good,getFeed_eb);
          //service.setHeaders({"If-Match": null});
    } else {
          service.insertEntry(url, entry, good, getFeed_eb);
    }
}

function saveManagementData(){
    var foundEntry = null;
    if (managementEntry){
    for(var i=0, n=managementEntry.length; i < n; i++) {
        var entry = managementEntry[i];
        if (entry.gsx$cropyear.$t == myYear  && entry.gsx$uniqueid.$t == document.getElementById("siteSelector").value){
           foundEntry = entry;
        }
    }}
    var url = 'https://spreadsheets.google.com/feeds/list/'+ man_sskey +'/2/private/full';
    if (foundEntry){
      entry = foundEntry;
    } else {
      entry = {xmlns: "http://www.w3.org/2005/Atom", 
         xmlns$gsx: "http://schemas.google.com/spreadsheets/2006/extended"};
      entry['gsx$uniqueid'] = {$t:document.getElementById("siteSelector").value};
      entry['gsx$cropyear'] = {$t:myYear};
    }
    entry['gsx$residueremoval'] = {$t:document.getElementById("residue_removal").value};
    entry['gsx$residuehow'] = {$t:document.getElementById("residue_how").value};
    entry['gsx$residuetype'] = {$t:document.getElementById("residue_type").value};
    var units = $('#residue_biomass_weight_units :radio:checked')[0].value;
    var rbw = document.getElementById("residue_biomass_weight").value;
    if (rbw != null && rbw != ""){
      rbw = parseFloat(rbw);
      if (units == "lbs"){
        rbw = rbw * 1.1208;
      }
    }
    entry['gsx$residuebiomassweight'] = {$t:rbw};
    entry['gsx$residuebiomassmoisture'] = {$t:document.getElementById("residue_biomass_moisture").value};
    //entry['gsx$residuevary'] = {$t:document.getElementById("residue_vary").value};
    entry['gsx$comments'] = {$t:document.getElementById("management_comments").value};
    entry['gsx$limeyear'] = {$t:document.getElementById("lime_year").value};
    entry['gsx$residueplantingpercentage'] = {$t:document.getElementById("residue_planting").value};
    
    var amendval = document.getElementById("organic_amendments").value;
    var amendtext = document.getElementById("organic_amendments_text").value;
    if (amendval == "yes" && amendtext == ""){
       alert("Please fill out the Amendment Text Box");
       return;
    }
    entry['gsx$organicamendments'] = {$t: amendval};
    entry['gsx$organicamendmentstext'] = {$t:amendtext};

    var irval = document.getElementById("irrigation").value;
    entry['gsx$irrigation'] = {$t:irval}; 
    if (irval == "yes"){
        var depth = parseInt(document.getElementById("irrigation_amount").value);
        if (depth > 0){
            var units = $('#irrigation_units :radio:checked')[0].value;
            if (units == 'in'){ depth = depth * 2.54; }
            entry['gsx$irrigationamount'] = {$t:depth};
        }
        entry['gsx$irrigationmethod'] = {$t:document.getElementById("irrigation_method").value};
    } else {
        entry['gsx$irrigationmethod'] = {$t:"n/a"};
        entry['gsx$irrigationamount'] = {$t:"n/a"};
    }
    entry['gsx$updated'] = {$t: new Date()};
    function good(entry){
        alert('Management Entry was saved!');
        loadPreviousManagement();
    }
    if (foundEntry != null){
          service.setHeaders({"If-Match": "*"});
          service.updateEntry(entry.id.$t, entry, good, getFeed_eb);
          //service.setHeaders({"If-Match": null});
      } else {
          service.insertEntry(url, entry, good, getFeed_eb);
      }
}

function saveOperationData(){
    if (document.getElementById("operation").value == "invalid"){
        alert("Invalid Operation Selected");
        return;
    }
  
    document.getElementById("previousOperationEdit").style.display = 'none';
    var entry;
    var url;
    if (document.getElementById("rowid").value != "0"){ 
        var myrowid = document.getElementById("rowid").value;
        entry = getOperationEntryByRowId(myrowid);
        url = entry.id.$t;
     } else {
      url = 'https://spreadsheets.google.com/feeds/list/'+ man_sskey +'/1/private/full';
      entry = {xmlns: "http://www.w3.org/2005/Atom", 
         xmlns$gsx: "http://schemas.google.com/spreadsheets/2006/extended"};
      }
      var operation = document.getElementById("operation").value;
      if (operation == 'invalid'){
          alert("Invalid Operation Selected");
          return;
      }
      var optokens = operation.split("_");
      if (optokens[0] == "plant"){ 
          var prate = document.getElementById("plant_rate").value;
          prate = parseInt( prate.replace(',','') );
          var units = $('#plant_rate_units :radio:checked')[0].value;
          if (units == 'acre' || units == 'lbs'){ 
             prate = prate * 2.47105; 
          }
          if (units == 'lbs'){
             entry['gsx$plantrateunits'] = {$t: 'kg'};
          } else {
             entry['gsx$plantrateunits'] = {$t: 'seeds'};
          }
          entry['gsx$plantrate'] = {$t: prate};

          entry['gsx$planthybrid'] = {$t: document.getElementById("plant_hybrid").value};
          if (optokens[1].substring(0,3) == 'rye'){
              entry['gsx$plantryemethod'] = {$t: dei("plantryemethod").value};
          }
          entry['gsx$plantmaturity'] = {$t: document.getElementById("plant_maturity").value};
          
      }
      if (optokens[0] == "harvest"){ 
          
      }
       
      entry['gsx$terminatemethod'] = {$t: document.getElementById("terminate_method").value};
      
      if (optokens[0] == "tillage"){
          var depth = parseInt(document.getElementById("tillage_depth").value);
          if (depth > 0){
            var units = $('#tillage_depth_units :radio:checked')[0].value;
            if (units == 'in'){ depth = depth * 2.54; }
            entry['gsx$depth'] = {$t:depth};
          }
      }
      if (optokens[0] == "soiladmend"){
          var fertrate = parseFloat(document.getElementById("lime_rate").value);
          if (fertrate > 0){
            var units = $('#soiladmend_rate_units :radio:checked')[0].value;
            // Mg per hectre or tons per acre
            if (units == 'acre'){ fertrate = fertrate / 0.446089; }
            entry['gsx$limerate'] = {$t:fertrate};
          }
          entry['gsx$currentph'] = document.getElementById("soiladmend_current_ph").value;
          entry['gsx$targetph'] = document.getElementById("soiladmend_target_ph").value;
          entry['gsx$neutralindex'] = document.getElementById("soiladmend_neutral_index").value;

      }
      if (optokens[0] == "manure"){
 entry['gsx$stabilizer'] = {$t:document.getElementById("manure_stabilizer").value}; 
 entry['gsx$stabilizername'] = {$t:document.getElementById("manure_stabilizer_name").value}; 
 entry['gsx$stabilizerused'] = {$t:document.getElementById("manure_stabilizer_used").value};
          var manure_rate = parseFloat(document.getElementById("manure_rate").value);
            entry['gsx$manuresource'] = {$t:document.getElementById("manure_source").value};
          entry['gsx$manurecomposition'] = {$t:document.getElementById("manure_composition").value};
          var manure_method = document.getElementById("manure_method").value;
          var manure_injection_depth = parseFloat(document.getElementById("manure_injection_depth").value);
          if (manure_method == 'injected'){
              if (manure_injection_depth > 0){
                  var units = $('#manure_injection_depth_units :radio:checked')[0].value;
                  if (units == 'inch'){ manure_injection_depth = manure_injection_depth * 2.54;}
              } else {
                  alert("Injection Depth needs to be specified");
                  return;            
              }        
          } else {
                manure_injection_depth = "";
          }
          entry['gsx$depth'] = {$t: manure_injection_depth};
          entry['gsx$manuremethod'] = {$t: manure_method};
          if (manure_rate > 0){
            var units = $('#manure_rate_units :radio:checked')[0].value;
            // TODO
            if (units == 'gpa'){ manure_rate = manure_rate * 1; }
            if (units == 'tpa'){ manure_rate = manure_rate * 1; }
            if (units == 'lph'){ manure_rate = manure_rate * 1; }
            if (units == 'mgph'){ manure_rate = manure_rate * 1; }
            entry['gsx$manurerate'] = {$t:manure_rate};
            entry['gsx$manurerateunits'] = {$t:units};
            for (var i=0; i < MANURE_ELEM.length; ++i) {
               var elem = MANURE_ELEM[i];
               var rate = parseFloat(document.getElementById("manure_"+elem+"_rate").value);
               if (rate >= 0){
                 entry['gsx$'+ elem] = {$t:rate};
               }
             }
          }
      }
      if (optokens[0] == "fertilizer"){
         var fertilizer_form = document.getElementById("fertilizer_form").value;
         var fertilizer_crop = dei("fertilizer_crop").value;
         var fertilizer_apptype = document.getElementById("fertilizer_apptype").value;
          var fertilizer_formulation = document.getElementById("fertilizer_formulation").value;
         entry['gsx$fertilizerform'] = {$t:fertilizer_form}; 
         entry['gsx$fertilizercrop'] = {$t:fertilizer_crop}; 
         entry['gsx$fertilizerapptype'] = {$t:fertilizer_apptype};
         var fertilizer_injection_depth = parseFloat(document.getElementById("fertilizer_injection_depth").value);
          if (fertilizer_apptype.search('injected') == 0){
              if (fertilizer_injection_depth > 0){
                  var units = $('#fertilizer_injection_depth_units :radio:checked')[0].value;
                  if (units == "inch"){ fertilizer_injection_depth = fertilizer_injection_depth * 2.54;}
              } else {
                  alert("Injection Depth needs to be specified");
                  return;            
              }        
          } else {
                fertilizer_injection_depth = "";
          }
          entry['gsx$depth'] = {$t: fertilizer_injection_depth};
         entry['gsx$fertilizerformulation'] = {$t:fertilizer_formulation}; 
 entry['gsx$stabilizer'] = {$t:document.getElementById("fertilizer_stabilizer").value}; 
 entry['gsx$stabilizername'] = {$t:document.getElementById("fertilizer_stabilizer_name").value}; 
 entry['gsx$stabilizerused'] = {$t:document.getElementById("fertilizer_stabilizer_used").value};


         /* Two options here, need to be clever... */
         var prate_text = document.getElementById("product_rate").value;
         var prodrate = parseFloat(document.getElementById("product_rate").value) * 1.12; 
         var fieldappend = '';       
         if (prate_text != ""){
           if (prodrate < -1 || prodrate > 1000){
             alert("Error, product rate outside of bounds 1-1000");
             return;
           }
         } else {
           prodrate = -1;
           fieldappend = '_lbs';
         }
         entry['gsx$productrate'] = {$t:prodrate};

         for (var i=0; i < FERTELEM.length; ++i) {
           var elem = FERTELEM[i];
           var rate = parseFloat(document.getElementById(elem+"_rate"+fieldappend).value);
           if (rate >= 0){
             entry['gsx$'+ elem] = {$t:rate};
           }
         }
      }
      entry['gsx$uniqueid'] = {$t:document.getElementById("siteSelector").value};
entry['gsx$operation'] = {$t:operation};
entry['gsx$cropyear'] = {$t:myYear};
      entry['gsx$comments'] = {$t: document.getElementById("comments").value};
      entry['gsx$date'] = {$t: $.datepicker.formatDate("yy/mm/dd", $("#datepicker").datepicker("getDate"))};
      entry['gsx$biomassdate1'] = {$t: $.datepicker.formatDate("yy/mm/dd", $("#tdatepicker1").datepicker("getDate"))};
      entry['gsx$biomassdate2'] = {$t: $.datepicker.formatDate("yy/mm/dd", $("#tdatepicker2").datepicker("getDate"))};  
      entry['gsx$updated'] = {$t: new Date()};
      entry['gsx$growthstage'] = {$t:document.getElementById("sample_growth_stage").value}; 
      entry['gsx$canopyheight'] = {$t:document.getElementById("sample_height").value}; 
      function good(entry){
        alert('Entry was saved!');
        operationsFeed = null;
        downloadOperations();
      }
      if (document.getElementById("rowid").value != "0"){
service.setHeaders({"If-Match": "*"});
 service.updateEntry(url,entry, good, getFeed_eb);
  //service.setHeaders({"If-Match": null});
      } else {
          service.insertEntry(url,entry, good, getFeed_eb);
      }
  }

  function adjustSiteSelector(){
     var el = document.getElementById('siteSelector');

     for (i=0;i<sites.length;i++){
var opt = document.createElement("option");
 opt.text = sites[i];
opt.value = sitekeys[i];
el.options.add(opt);
     }
  }
  
/*
 * Called with the results of the Site Metadata & History Form
 */
function showResults(result){
    console.log(results);
    var sheetFeed = result.feed;
    var list = sheetFeed.entry;
    for(var i=0, n=list.length; i < n; i++) {
       var entry = list[i];
       sites.push(entry.gsx$uniqueid.$t +": "+ entry.gsx$leadpi.$t );
       sitekeys.push(entry.gsx$uniqueid.$t);
    }
    adjustSiteSelector();
    var today = new Date();
    setYear( today.getFullYear() ); 
    //setOperation("plant_corn");
    setIpmOperation("herbicide");
  }

/* Called once google has all our apis loaded! */
function bootstrap(){
    $( "#tabs" ).tabs();
   $( "#amount" ).val( "$" + $( "#slider" ).slider( "value" ) );
    var today = new Date();
    $( "#datepicker" ).datepicker();
    $( "#tdatepicker1" ).datepicker();
    $( "#tdatepicker2" ).datepicker();
    $( "#datepicker" ).datepicker('setDate', today);
    $( "#ipm_datepicker" ).datepicker();
    $( "#ipm_datepicker" ).datepicker('setDate', today);
    $("#years").buttonset();
    $('#years input[type=radio]').change(function() {
       setYear(this.value);  
    });
    $("#tillage_depth_units").buttonset();
    $("#plant_rate_units").buttonset();
    $("#irrigation_units").buttonset();
    $("#herbicide_total_rate_units").buttonset();
    $("#insecticide_total_rate_units").buttonset();
    $("#fungicide_total_rate_units").buttonset();
    $("#herbicide_pressure_units").buttonset();
    $("#insecticide_pressure_units").buttonset();
    $("#fungicide_pressure_units").buttonset();
    $("#herbicide_rate_units1a").buttonset();
    $("#herbicide_rate_units2a").buttonset();
    $("#herbicide_rate_units3a").buttonset();
    $("#herbicide_rate_units4a").buttonset();
    $("#herbicide_rate_units1b").buttonset(); 
    $("#herbicide_rate_units2b").buttonset();
    $("#herbicide_rate_units3b").buttonset();
    $("#herbicide_rate_units4b").buttonset();
    //$("#pesticide_rate_units1").buttonset();
    $("#insecticide_rate_units1a").buttonset();
    $("#insecticide_rate_units1b").buttonset();
    $("#fungicide_rate_units1a").buttonset();
    $("#fungicide_rate_units1b").buttonset();
    $("#residue_biomass_weight_units").buttonset();

    $("#soiladmend_rate_units").buttonset();
    $("#manure_rate_units").buttonset();
    $("#manure_injection_depth_units").buttonset();
    $("#fertilizer_injection_depth_units").buttonset();
    url = 'https://spreadsheets.google.com/feeds/list/'+site_sskey+'/1/private/full';

    var params = {};
    params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.FEED;
    params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
    params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "googleAPI";
    params[gadgets.io.RequestParameters.REFRESH_INTERVAL] = "0";

   console.log(url);
   console.log(params);
   gadgets.io.makeRequest(url, function (response) {
       console.log(response);
       if (response.oauthApprovalUrl) {
            var onOpen = function() {
              showOneSection('aproval');
            };
            var onClose = function() {
              fetchData();
            };
            var popup = new gadgets.oauth.Popup(response.oauthApprovalUrl,
                null, onOpen, onClose);
            //dei('personalize').onclick = popup.createOpenerOnClick();
            //dei('approvaldone').onclick = popup.createApprovedOnClick();
            showOneSection('approval');
       } else if (response.data) {
            SITES_FEED = response.data;
            showResults(response);
            dei('main').appendChild(document.createTextNode(response.data));
            showOneSection('main');
       } else {
             dei('error_code').appendChild(document.createTextNode(response.oauthError));
             dei('error_uri').appendChild(document.createTextNode(response.oauthErrorUri));
             dei('error_description').appendChild(document.createTextNode(response.oauthErrorText));
             dei('error_explanation').appendChild(document.createTextNode(response.oauthErrorExplanation));
             dei('error_trace').appendChild(document.createTextNode(response.oauthErrorTrace));
            showOneSection('error');
       }
    }, params);

  }
/* called after gadgets are ready, now we need more apis */
function myinit(){
    //google.load("gdata", "2.x");
    google.load("jquery", "1.6.1");
    google.load("jqueryui", "1.8.16");
    google.setOnLoadCallback(bootstrap);
}
/* Wait until google gadgets is fired up */
gadgets.util.registerOnLoadHandler(myinit);
</script>
    ]]>
  </Content>
</Module>
