<?php 
	include("../../php_crud/my_class.php");
	$obj=new my_class();
	extract($_POST);
  // print_r($_POST);

	////--ID--////
   $date_id = "date";
   $bic_id = "bicarbonate";
   $tur_id = "turbidity"; 

   $temp_id = "tempareture";
   $sul_id = "sulphate";
   $orp_id = "orp";

   $ph_id = "ph";
   $nit_id = "nitrate";
   $sal_id = "salinity";

   $eh_id = "eh";
   $irn_id = "iron";
   $pho_id = "phosphate";

   $ec_id = "ec";
   $man_id = "manganese";
   $bro_id = "bromide";

   $tds_id = "tds";
   $bor_id = "boron";
   $alu_id = "aluminium";

   $cal_id = "calcium";
   $sil_id = "silicon";
   $cop_id = "copper";

   $mag_id = "magnisium";
   $ars_id = "arsenic";
   $zin_id = "zinc";

   $sod_id = "sodium";
   $flu_id = "fluoried";
   $cho_id = "chromium";

   $pot_id = "potassium";
   $iod_id = "iodide";

   $chl_id = "chloride";
   $car_id = "carbondioxide";

   $cab_id = "carbonate";
   $dis_id = "dissolvedoxygen";

   $har_id = "hardness";
   $sar_id = "sar";
   ////--ID--//// 
?>

<style type="text/css">
  select, textarea, input[type="text"], input[type="password"], input[type="datetime"], input[type="datetime-local"], input[type="date"], input[type="month"], input[type="time"], input[type="week"], input[type="number"], input[type="email"], input[type="url"], input[type="search"], input[type="tel"], input[type="color"], .uneditable-input {
  display: inline-block;
  height: 1px!important;
  padding: 8px 3px!important;
  margin-bottom: 2px!important;
  font-size: 13px!important;
  line-height: 23px!important;
  color: #555;
  vertical-align: middle;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
}
.table{
   width: 100%;
   margin-letf: 20%;
}
.table th, .table td {
  padding: 2px!important;
  line-height: 20px;
  text-align: left;
  vertical-align: top;
  border: none;
}
.button{
position: relative;
top: -20px;
}
.tbl{
position: relative;
top: -15px;
}
.table tr{
  border: none;
 
}

</style>
<div class="row-fluid" style="margin-bottom:5px;">
	<div class="control-group" style="float:right;">
		<div class="controls">
			<button type="button" style="margin-right: 1px;  font-size: 12px; float:right; " id="btn2" class="btn btn-small btn-primary" onclick="load_new_row()"><i class="icon-plus"></i><b>Add New</b></button>
		</div>
	</div>
</div>
<table id="gwq_data_table" class="table table-hover">
<thead>
    <tr>
    <th style="text-align:center;"></th>
    <th style="text-align:center;"></th>
    <th style="text-align:center;"></th>
    </tr>
</thead>
<tbody id = "gwq_tbl_body">
<!--Row-01-->  
  <tr>
    <td style="width: 33%; font-weight: bold;">
		<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">Date</td>
				<td style="width: 30%;"><input type="text" id="<?php echo $date_id; ?>" class="input-mini month_date gwq_date" 
					style="text-align: center; font-weight: bold; width: 98%;" value="" onkeyup="call_next_val_loader(event,'<?php echo $date_id; ?>','<?php echo $temp_id; ?>')"/>
				</td>
				<td style="width: 20%;"></td>
			</tr>
		</table>
    </td>
	 <td style="text-align: right; width: 33%; font-weight: bold;">Calcium Ca<sup>2+</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $cal_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $cal_id; ?>','<?php echo $mag_id; ?>')">&nbsp;(mg/l)
    </td>
     <td style="text-align: right; width: 33%; font-weight: bold;">Arsenic As
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $ars_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $ars_id; ?>','<?php echo $irn_id; ?>')">&nbsp;(mg/l)
    </td>    
  </tr>
<!--Row-01-->  
<!--Row-02-->  
  <tr>
    <td style="width: 33%; font-weight: bold;">
		<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">Temperature</td>
				<td style="width: 30%;"> <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
					style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $temp_id;?>"
					value="" onkeyup="call_next_val_loader(event,'<?php echo $temp_id; ?>','<?php echo $tur_id; ?>')"> 
				</td>
				<td style="width: 20%;">&nbsp;(<sup>o</sup>C)</td>
			</tr>
		</table>
    </td> 
  <td style="text-align: right; width: 33%; font-weight: bold;">Magnesium Mg<sup>2+</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $mag_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $mag_id; ?>','<?php echo $nit_id; ?>')">&nbsp;(mg/l)
    </td> 
   <td style="text-align: right; width: 33%; font-weight: bold;">Iron Fe<sup>+</sup>
     <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $irn_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $irn_id; ?>','<?php echo $man_id; ?>')">&nbsp;(mg/l)
    </td>   
  </tr>
<!--Row-02-->  
<!--Row-03-->  
<tr>
    <td style="width: 33%; font-weight: bold;">
		<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">Turbidity</td>
				<td style="width: 30%;"> <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
						style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $tur_id;?>"
					value="" onkeyup="call_next_val_loader(event,'<?php echo $tur_id; ?>','<?php echo $ph_id; ?>')">
				</td>
				<td style="width: 20%;">&nbsp;(NTU)</td>
			</tr>
		</table>
    </td>
    <td style="text-align: right; width: 33%; font-weight: bold;">Nitrate NO<sub>3</sub><sup>-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $nit_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $nit_id; ?>','<?php echo $sul_id; ?>')">&nbsp;(mg/l)
    </td>
     <td style="text-align: right; width: 33%; font-weight: bold;">Manganese Mn<sup>2+</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $man_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $man_id; ?>','<?php echo $bor_id; ?>')">&nbsp;(mg/l)
    </td>
  </tr>
<!--Row-03-->  

<!--Row-04-->  
<tr>
    <td style="width: 33%; font-weight: bold;">
		<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">pH</td>
				<td style="width: 30%;"><input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $ph_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $ph_id; ?>','<?php echo $dis_id; ?>')">
				</td>
				<td style="width: 20%;"></td>
			</tr>
		</table>
    </td> 
    <td style="text-align: right; width: 33%; font-weight: bold;">Sulphate SO<sub>4</sub><sup>2-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $sul_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $sul_id; ?>','<?php echo $pho_id; ?>')">&nbsp;(mg/l)
    </td>
    <td style="text-align: right; width: 33%; font-weight: bold;">Boron B<sup>+</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $bor_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $bor_id; ?>','<?php echo $sil_id; ?>')">&nbsp;(mg/l)
    </td>
  </tr>
<!--Row-04--> 

<!--Row-05-->  
<tr>
    <td style="width: 33%; font-weight: bold;">
		<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">Dissolved Oxygen (DO)</td>
				<td style="width: 30%;"><input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $dis_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $dis_id; ?>','<?php echo $eh_id; ?>')"> 
				</td>
				<td style="width: 20%;">&nbsp;(mg/l)</td>
			</tr>
		</table>
    </td>
     <td style="text-align: right; width: 33%; font-weight: bold;">Phosphate PO<sub>4</sub><sup>3-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $pho_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $pho_id; ?>','<?php echo $car_id; ?>')">&nbsp;(mg/l)
    </td>
   <td style="text-align: right; width: 33%; font-weight: bold;">Silica Si<sup>+</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $sil_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $sil_id; ?>','<?php echo $alu_id; ?>')">&nbsp;(mg/l)
    </td> 
  </tr>
<!--Row-05--> 

<!--Row-06-->  
<tr>
    <td style="width: 33%; font-weight: bold;">
		<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">ORP (Eh)</td>
				<td style="width: 30%;"><input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center;  font-weight: bold; width: 98%;" id="<?php echo $eh_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $eh_id; ?>','<?php echo $ec_id; ?>')">
				</td>
				<td style="width: 20%;">&nbsp;(Mv)</td>
			</tr>
		</table>
    </td>
    <td style="text-align: right; width: 33%; font-weight: bold;">Carbondioxide CO<sub>2</sub><sup>2-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $car_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $car_id; ?>','<?php echo $cab_id; ?>')">&nbsp;(mg/l)
    </td>
   <td style="text-align: right; width: 33%; font-weight: bold;">Aluminium Al
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $alu_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $alu_id; ?>','<?php echo $cop_id; ?>')">&nbsp;(mg/l)
    </td>
  </tr>
<!--Row-06--> 

<!--Row-07-->  
<tr>
    <td style="width: 33%; font-weight: bold;">
		<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">Ec</td>
				<td style="width: 30%;"><input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $ec_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $ec_id; ?>','<?php echo $tds_id; ?>')">
				</td>
				<td style="width: 20%;">&nbsp;(µS/cm)</td>
			</tr>
		</table>
    </td>
     <td style="text-align: right; width: 33%; font-weight: bold;">Carbonate CO<sub>3</sub><sup>2-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $cab_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $cab_id; ?>','<?php echo $bic_id; ?>')">&nbsp;(mg/l)
    </td>  
   <td style="text-align: right; width: 33%; font-weight: bold;">Copper Cu
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $cop_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $cop_id; ?>','<?php echo $zin_id; ?>')">&nbsp;(mg/l)
    </td>
</tr>
<!--Row-07-->


<!--Row-08-->  
<tr>
   <td style="width: 33%; font-weight: bold;">
		<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">TDS</td>
				<td style="width: 30%;"><input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $tds_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $tds_id; ?>','<?php echo $sal_id; ?>')"> 
				</td>
				<td style="width: 20%;">&nbsp;(mg/l)</td>
			</tr>
		</table>

    </td> 
    <td style="text-align: right; width: 33%; font-weight: bold;">Bicarbonate HCO<sub>3</sub><sup>-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $bic_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $bic_id; ?>','<?php echo $flu_id; ?>')">&nbsp;(mg/l)
    </td>
      <td style="text-align: right; width: 33%; font-weight: bold;">Zinc Zn
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $zin_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $zin_id; ?>','<?php echo $cho_id; ?>')">&nbsp;(mg/l)
    </td> 
  </tr>
<!--Row-08-->

<!--Row-09-->  
<tr>
  <td style="width: 33%; font-weight: bold;">
	<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">Salinity</td>
				<td style="width: 30%;"><input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $sal_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $sal_id; ?>','<?php echo $har_id; ?>')">
				</td>
				<td style="width: 20%;">&nbsp;(ppt)</td>
			</tr>
	</table>
   
    </td>
      <td style="text-align: right; width: 33%; font-weight: bold;">Fluoride F<sup>-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $flu_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $flu_id; ?>','<?php echo $bro_id; ?>')">&nbsp;(mg/l)
    </td>
   <td style="text-align: right; width: 33%; font-weight: bold;">Chromium Cr
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $cho_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $cho_id; ?>','<?php echo $sar_id; ?>')">&nbsp;(mg/l)
    </td>
  </tr>
<!--Row-09-->

<!--Row-10-->  
<tr>
  <td style="width: 33%; font-weight: bold;">
	<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">Hardness CaCO<sub>3</sub></td>
				<td style="width: 30%; padding-left: 5px;"><input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $har_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $har_id; ?>','<?php echo $sod_id; ?>')">
				</td>
				<td style="width: 20%;">&nbsp;(mg/l)</td>
			</tr>
	</table>
   
    </td>
   <td style="text-align: right; width: 33%; font-weight: bold;">Bromide Br<sup>-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $bro_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $bro_id; ?>','<?php echo $iod_id; ?>')">&nbsp;(mg/l)
    </td>   
	<td style="text-align: right; width: 33%; font-weight: bold;">SAR
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $sar_id;?>"
    value=""onkeyup = "select_save(event)">&nbsp;(mg/l)
    </td>  
  </tr>
<!--Row-10-->

<!--Row-11-->  
<tr>
    <td style="width: 33%; font-weight: bold;">
	<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">Sodium Na<sup>+</sup></td>
				<td style="width: 30%;"><input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $sod_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $sod_id; ?>','<?php echo $pot_id; ?>')">
				</td>
				<td style="width: 20%;">&nbsp;(mg/l)</td>
			</tr>
	</table>
    </td> 
       <td style="text-align: right; width: 33%; font-weight: bold;">Iodide I<sup>2-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_tur"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $iod_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $iod_id; ?>','<?php echo $chl_id; ?>')">&nbsp;(mg/l)
    </td> 
	<td style="text-align: right; width: 33%;"></td>   
  </tr>
<!--Row-11-->

<!--Row-12-->  
<tr>
    <td style="width: 33%; font-weight: bold;">
	<table style="width: 100%;">
			<tr>
				<td style="text-align: right; width: 50%;">Potassium K<sup>+</sup></td>
				<td style="width: 30%;"><input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 98%;" id="<?php echo $pot_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $pot_id; ?>','<?php echo $cal_id; ?>')">
				</td>
				<td style="width: 20%;">&nbsp;(mg/l)</td>
			</tr>
	</table>
    </td>
  <td style="text-align: right; width: 33%; font-weight: bold;">Chloride Cl<sup>-</sup>
    <input onkeypress="return isNumericInput(event)" type="text" class="input-mini gwq_bic"
    style="text-align: center; font-weight: bold; width: 30%;" id="<?php echo $chl_id;?>"
    value="" onkeyup="call_next_val_loader(event,'<?php echo $chl_id; ?>','<?php echo $ars_id; ?>')">&nbsp;(mg/l)
    </td>
	<td style="text-align: right; width: 33%;"></td>    
  </tr>
<!--Row-12-->
</tbody>
</table>
<div class="row-fluid" style="margin-bottom:5px;">
  <div class="control-group" style="float:right;">
    <div class="controls">
      <!-- <button id="" type="button" style="margin-right: 1px;  font-size: 12px;" 
      class="btn btn-small btn-danger " onclick="gwq_multiple_delete()"><i class="icon-save"></i><b>Delete</b></button> -->
      <button id="gwq_sva_btn" type="button" style="margin-right: 1px;  font-size: 12px;" 
      class="btn btn-small btn-success " onclick="gwq_multiple_save()"><i class="icon-save"></i><b>Save</b></button>
    </div>
  </div>
</div>

<script>
function validateInput(input) {
  var regex = /(\b(on\w+)=|javascript:|(<\s*)(\/*)script)/i;
  if (input.length > 100 || regex.test(input)) {
    return false;
  }
  return true;
}
</script>

<script>
function call_next_val_loader(event, sel_col_id,next_col_id){
   if (event.keyCode == 13 || event.keyCode == 'Enter' || event.keyCode == 'Tab') {
      //alert(sel_col_id);
      var curr_val = $("#"+sel_col_id).val();
      var col_name_arr = sel_col_id.split('_');
		  var col_name = col_name_arr[0];
      const byteSize = $("#"+sel_col_id).val().length;

        if(validateInput(curr_val) == false){
            alert("Please Check The Input Fromat");
            curr_val = "";
            $("#"+sel_col_id).val(curr_val);
        }
        else if (byteSize > 11) {
        alert("Date Length Cannot Be More Than 10 Bytes");
            curr_val = "";
            $("#"+sel_col_id).val(curr_val);
        }
        else if(col_name=='date'){
			if(curr_val==""){
				alert("Please enter the date, date field cannot be empty");
			}
			else{
				var operation = "date_checker";
				var datastr = "operation="+operation+"&sel_in_date="+curr_val;
                //alert(datastr);
				$.ajax({
					url: 'module/ground_water_ex/gw_quality_entry_ajax.php', 
					type: 'post',
					data: datastr,
					cache: false,
					success: function(result) {
					  //alert(result);
					  if(result==0){
						alert('You only enter data till today');
						$("#"+sel_col_id).val('');
						$("#"+sel_col_id).select();
						return false;
					  }
					  else{
						$("#"+sel_col_id).val(result);
						$("#"+next_col_id).select();
					  }
					  
					}
				});	
			}
        }
        else{
          if (curr_val > Math.floor(curr_val)) {
            curr_val = parseFloat(curr_val).toFixed(2);
          }
          $("#"+sel_col_id).val(curr_val);
          $("#"+next_col_id).select();
        }
        
    }
}   
function show_alert(){
    alert('You cannot enter for any day after today');
}
</script>
<script>
function select_save(event){
	if(event.keyCode==13 || event.keyCode == 'Enter' || event.keyCode == 'Tab'){
		//alert('call save');
		gwq_multiple_save();
	}
}
</script>   
<!-- Alpha-Numeric Checking-->
<script>
let inputReset = false; // Track whether input should be reset

function isNumericInput(event) {
const key = event.keyCode || event.which;
const keyChar = String.fromCharCode(key);

const regex = /[0-9]|\.|\-/;

// Only allow numeric, decimal, or minus sign input
if (!regex.test(keyChar)) {
  event.preventDefault();
  return false;
}

const input = event.target;
const currentValue = input.value;

// If the input already contains a decimal point or minus sign, disallow another one
if ((keyChar === "." && currentValue.indexOf(".") !== -1) || 
    (keyChar === "-" && currentValue.indexOf("-") !== -1)) {
  event.preventDefault();
  event.target.value = "";
  event.target.value += keyChar;
  return false;
}

// If input should be reset, do so and allow new input
if (inputReset) {
  input.value = "";
  inputReset = false;
}

// If the input would result in more than 2 decimal places, set inputReset flag
const parts = currentValue.split(".");
if (parts.length === 2 && parts[1].length >= 2 && keyChar !== "-") {
  event.preventDefault();
  event.target.value = "";
  event.target.value += keyChar;
  return false;
}

if (currentValue === "0" && keyChar !== "." && keyChar !== "-" && currentValue.indexOf(".") === -1) {
  event.preventDefault();
  return false;
}

return true;
}
</script>
<!-- Alpha-Numeric Checking-->