<?php
session_start();
include("../../php_crud/my_class.php");
$obj = new my_class();
extract($_GET);
$month_day = cal_days_in_month(CAL_GREGORIAN,$month,$year);
$curr_date =  date('d-M-Y');
$curr_year = date('Y');
$curr_mon = date('m');

$sel_mon_year = $year."".$month."01";
$sel_mon_year = date("Ym", strtotime($sel_mon_year));

$curr_mon_year = $curr_year."".$curr_mon;
$curr_mon_year = date("Ym", strtotime($curr_mon_year));

if($sel_mon_year > $curr_mon_year){
	echo "<h2 style='color: red; text-align: center;'>You can only see data till running month.</h2>";
}else{



?>
<style>
  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 th, .table td {
  padding: 2px!important;
  line-height: 20px;
  text-align: left;
  vertical-align: top;
  border-top: 1px solid #ddd;
}
</style>
<input type="hidden" style="width: 95%;" id="sel_mon_tot_day" name="sel_mon_tot_day" value="<?php echo $month_day; ?>" />
<input type="hidden" style="width: 95%;" id="year" name="year" value="<?php echo $year; ?>" />
<input type="hidden" style="width: 95%;" id="month" name="month" value="<?php echo $month; ?>" />
<table style = "width: 100%;">
  <tr style = "height: 50px;">
    <!-- <td style = "width: 28%;">&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;</td> -->
    <td style="text-align: right; font-size:15px; margin-right: 2px;">Remarks:&ensp;</td>
    <td style="text-align: left; font-size:13px width: 90%;">
      <?php $rem = $obj->Details_By_Cond_hydro("SWPB_REMARKS","STATION_ID='$station' AND  YEAR = '$year' AND MONTH = '$month'");
      $remarks = $rem['REMARKS'];
      ?>
      <input type="text" style="width: 95%; height:20px !important; font-size: 12px; font-weight: bold;" id="remarks" name="remarks" value="<?php echo $remarks; ?>" />
    </td>
    <!-- <td>&ensp;</td> -->
    <td style="text-align: right; width: 20%;">
    <button id="wtl_sva_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-success " onclick="wl_multiple_save()"><i class="icon-save"></i><b>Save All</b></button>
    <?php
          if($_SESSION['delete'] == 'Y'){
            echo '<button id="wl_delete_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-danger" onclick="wl_delete_all()"><i class="icon-trash"></i><b>Delete All</b></button>';
          }
      ?>
    </td>
  </tr>
</table>
<table id="wli_data_table" class="table table-bordered table-hover">
    <thead>
        <tr style="font-family: 'Open Sans';">
        <td style="text-align: center; font-size:13px;" class="table-header">Date</td>
        <td style="text-align: center; font-size:13px;" class="table-header">6 hr</td>
        <td style="text-align: center; font-size:13px;" class="table-header">9 hr</td>
        <td style="text-align: center; font-size:13px;" class="table-header">12 hr</td>
        <td style="text-align: center; font-size:13px;" class="table-header">15 hr</td>
        <td style="text-align: center; font-size:13px;" class="table-header">18 hr</td>
<?php if($tidal_status=='T'){ ?>
        <td style="text-align: center; font-size:13px;" class="table-header">Time</td>
        <td style="text-align: center; font-size:13px;" class="table-header">Highest</td>
        <td style="text-align: center; font-size:13px;" class="table-header">Time</td>
        <td style="text-align: center; font-size:13px;" class="table-header">Lowest</td>
<?php } ?>
        <td style="text-align: center; font-size:13px;" class="table-header">Action</td>
        </tr>
    </thead>
    <tbody>
        <?php   
            $list=array();
            // $month = 03;
            // $year = 2016;
           
            for($d=1; $d<=$month_day; $d++)
            {
                $time=mktime(12, 0, 0, $month, $d, $year);          
                if (date('m', $time)==$month)       
                { 
                    $show_date = date('d-M-Y', $time);
                    $show_date = strtoupper($show_date);
                    $sl=0;
                    $wl_6hrs = "";
                    $wl_9hrs = "";
                    $wl_12hrs = "";
                    $wl_15hrs = "";
                    $wl_18hrs = "";
                    $wl_cond = "STATION_ID='$station' AND TO_CHAR(SWL_DATE,'dd-mon-rrrr')= '$show_date' AND DATA_TYPE='R'";
                    
					$wl_6hr_cond = "STATION_ID='$station' AND TO_CHAR(SWL_DATE, 'DD-MON-YYYY HH24:MI:SS')='$show_date 06:00:00' AND DATA_TYPE='R'";
                    $wl_6hr_read = $obj->View_column_details_By_Cond_hydro("SWL_DATA","WL",$wl_6hr_cond);

                  
                    
                    $wl_9hr_cond = "STATION_ID='$station' AND TO_CHAR(SWL_DATE, 'DD-MON-YYYY HH24:MI:SS')='$show_date 09:00:00' AND DATA_TYPE='R'";
                    $wl_9hr_read = $obj->View_column_details_By_Cond_hydro("SWL_DATA","WL",$wl_9hr_cond);
                    
                    $wl_12hr_cond = "STATION_ID='$station' AND TO_CHAR(SWL_DATE, 'DD-MON-YYYY HH24:MI:SS')='$show_date 12:00:00' AND DATA_TYPE='R'";
                    $wl_12hr_read = $obj->View_column_details_By_Cond_hydro("SWL_DATA","WL",$wl_12hr_cond);
                    
                    $wl_15hr_cond = "STATION_ID='$station' AND TO_CHAR(SWL_DATE, 'DD-MON-YYYY HH24:MI:SS')='$show_date 15:00:00' AND DATA_TYPE='R'";
                    $wl_15hr_read = $obj->View_column_details_By_Cond_hydro("SWL_DATA","WL",$wl_15hr_cond);
                    
                    $wl_18hr_cond = "STATION_ID='$station' AND TO_CHAR(SWL_DATE, 'DD-MON-YYYY HH24:MI:SS')='$show_date 18:00:00' AND DATA_TYPE='R'";
                    $wl_18hr_read = $obj->View_column_details_By_Cond_hydro("SWL_DATA","WL",$wl_18hr_cond);
 
					if($wl_6hr_read['WL']!=""){
						$show_wl_6hr_val = number_format((float)$wl_6hr_read['WL'], 2, '.', '');
					}
					else{
						$show_wl_6hr_val = $wl_6hr_read['WL'];
					}
					
					if($wl_9hr_read['WL']!=""){
						$show_wl_9hr_val = number_format((float)$wl_9hr_read['WL'], 2, '.', '');
					}
					else{
						$show_wl_9hr_val = $wl_9hr_read['WL'];
					}
					
					if($wl_12hr_read['WL']!=""){
						$show_wl_12hr_val = number_format((float)$wl_12hr_read['WL'], 2, '.', '');
					}
					else{
						$show_wl_12hr_val = $wl_9hr_read['WL'];
					}
					
					if($wl_15hr_read['WL']!=""){
						$show_wl_15hr_val =  number_format((float)$wl_15hr_read['WL'], 2, '.', '');
					}
					else{
						$show_wl_15hr_val = $wl_15hr_read['WL'];
					}
					
					if($wl_18hr_read['WL']!=""){
						$show_wl_18hr_val =  number_format((float)$wl_18hr_read['WL'], 2, '.', '');
					}
					else{
						$show_wl_18hr_val = $wl_18hr_read['WL'];
					}
					
                    $cond_high = "STATION_ID='$station' AND TO_CHAR(SWL_DATE, 'DD-MON-YYYY')= '$show_date' AND DATA_TYPE='H'";
                    $high_wl_info = $obj->View_column_details_By_Cond_hydro("SWL_DATA","WL,TO_CHAR(SWL_DATE, 'HH24:MI:SS') AS HWLHR",$cond_high);
					$high_wl_time_raw = $high_wl_info['HWLHR'];
					$high_wl_time_arr = explode(":",$high_wl_time_raw);
					//print_r($high_wl_time_arr);
					if($high_wl_time_raw!=""){
						$show_high_wl_time = (int)$high_wl_time_arr[0].$high_wl_time_arr[1];
						if(strlen((string)$show_high_wl_time)==1 || strlen((string)$show_high_wl_time)==2){
							$show_high_wl_time = $show_high_wl_time."00";
						}
					}
					else{
						$show_high_wl_time = "";
					}
					
					if($high_wl_info['WL']!=""){
						$show_high_wl_val =  number_format((float)$high_wl_info['WL'], 2, '.', '');
					}
					else{
						$show_high_wl_val = $high_wl_info['WL'];
					}
					
					$cond_low = "STATION_ID='$station' AND TO_CHAR(SWL_DATE, 'DD-MON-YYYY')= '$show_date' AND DATA_TYPE='L'";
					$low_wl_info = $obj->View_column_details_By_Cond_hydro("SWL_DATA","WL,TO_CHAR(SWL_DATE, 'HH24:MI:SS') AS LWLHR",$cond_low);
					$low_wl_time_raw = $low_wl_info['LWLHR'];
					$low_wl_time_arr = explode(":",$low_wl_time_raw);
					//print_r($low_wl_time_arr);
					if($low_wl_time_raw!=""){
						$show_low_wl_time = (int)$low_wl_time_arr[0].$low_wl_time_arr[1];
						if(strlen((string)$show_low_wl_time)==1 || strlen((string)$show_low_wl_time)==2){
							$show_low_wl_time = $show_low_wl_time."00";
						}
					}
					else{
						$show_low_wl_time = "";
					}
					
					if($low_wl_info['WL']!=""){
						$show_low_wl_val =  number_format((float)$low_wl_info['WL'], 2, '.', '');
					}
					else{
						$show_low_wl_val = $low_wl_info['WL'];
					}
					
                   $date_id = "date_$d";
                   $hr6_date_id = "6hr_$d";
                   $hr9_date_id = "9hr_$d";
                   $hr12_date_id = "12hr_$d";
                   $hr15_date_id = "15hr_$d";
                   $hr18_date_id = "18hr_$d";
                   $after_hr18_id = "";
                   $after_lwl_id = "";
                   $next_row_val = $d + 1;
				          $function_for_lwl = "";
				          $function_for_hr6 = "";
            if($tidal_status=='T') {
                   $high_time = "ht_$d";
                   $high_wl = "hwl_$d";
                   $low_time = "lt_$d";
                   $low_wl = "lwl_$d";
                   
                   $after_hr18_id = $high_time;
                   $after_lwl_id = "6hr_$next_row_val";
				           $function_for_hr6 = "call_next_val_loader(event,'$hr18_date_id','$after_hr18_id')";
				           $function_for_lwl = "call_save_function(event,'$low_wl','$after_lwl_id','$d')";
            }
            else{
                $after_hr18_id = "6hr_$next_row_val";
				        $function_for_hr6 = "call_save_function(event,'$hr15_date_id','$after_hr18_id','$d')";
            }       
                   
        ?>  
            <tr>
            <td style="text-align: center;">
                <input type="text" id="<?php echo $date_id;?>" class="input-small" style="text-align: center; font-weight: 700;" value="<?php echo $show_date; ?>" readonly></td>
            <td style="text-align: center;">
                <input type="text" class="input-small" maxlength="6" name="numonly" style="text-align: center; font-weight: bold;" id="<?php echo $hr6_date_id;?>" value="<?php echo $show_wl_6hr_val; ?>" onkeyup="call_next_val_loader(event,'<?php echo $hr6_date_id; ?>','<?php echo $hr9_date_id; ?>')" onkeypress="return isNumericInput(event)"></td>
            <td style="text-align: center;">
                <input type="text" class="input-small" maxlength="6" name="numonly" style="text-align: center; font-weight: bold;" id="<?php echo $hr9_date_id;?>" value="<?php echo $show_wl_9hr_val; ?>" onkeyup="call_next_val_loader(event,'<?php echo $hr9_date_id; ?>','<?php echo $hr12_date_id; ?>')" onkeypress="return isNumericInput(event)"></td>
            <td style="text-align: center;">
                <input type="text" class="input-small" maxlength="6" name="numonly" style="text-align: center; font-weight: bold;" id="<?php echo $hr12_date_id;?>" value="<?php echo $show_wl_12hr_val; ?>" onkeyup="call_next_val_loader(event,'<?php echo $hr12_date_id; ?>','<?php echo $hr15_date_id; ?>')" onkeypress="return isNumericInput(event)"></td>
            <td style="text-align: center;">
                <input type="text" class="input-small" maxlength="6" name="numonly" style="text-align: center; font-weight: bold;" id="<?php echo $hr15_date_id;?>" value="<?php echo $show_wl_15hr_val; ?>" onkeyup="call_next_val_loader(event,'<?php echo $hr15_date_id; ?>','<?php echo $hr18_date_id; ?>')" onkeypress="return isNumericInput(event)"></td>
            <td style="text-align: center;">
                <input type="text" class="input-small" maxlength="6" name="numonly" style="text-align: center; font-weight: bold;" id="<?php echo $hr18_date_id;?>" value="<?php echo $show_wl_18hr_val; ?>" onkeyup="<?php echo $function_for_hr6; ?>" onkeypress="return isNumericInput(event)"></td>
    <?php if($tidal_status=='T') { ?>
            <td style="text-align: center;">
                <input type="text" class="input-small" style="text-align: center; font-weight: bold;" id="<?php echo $high_time;?>"  value="<?php echo $show_high_wl_time; ?>" onkeyup="call_next_val_loader(event,'<?php echo $high_time; ?>','<?php echo  $high_wl; ?>')" onkeypress="return isNumberKey(event)"></td>
            
            <td style="text-align: center;">
                <input type="text" class="input-small" maxlength="6" name="numonly" style="text-align: center; font-weight: bold;" id="<?php echo $high_wl; ?>" value="<?php echo $show_high_wl_val; ?>" 
                onkeyup="call_highest_val(event,'<?php echo $high_wl; ?>','<?php echo $hr6_date_id; ?>',
                '<?php echo $hr9_date_id; ?>','<?php echo $hr12_date_id; ?>','<?php echo $hr15_date_id; ?>','<?php echo $hr18_date_id;?>',
                '<?php echo  $low_time; ?>')" onkeypress="return isNumericInput(event)"></td>


            <td style="text-align: center;">
                <input type="text" class="input-small" style="text-align: center; font-weight: bold;" id="<?php echo $low_time; ?>" value="<?php echo $show_low_wl_time; ?>" onkeyup="call_next_val_loader(event,'<?php echo $low_time; ?>','<?php echo  $low_wl; ?>')" onkeypress="return isNumberKey(event)"></td>
            <td style="text-align: center;">
                <!-- <input type="text" class="input-small" maxlength="6" name="numonly" style="text-align: center; font-weight: bold;" id="<?php echo $low_wl; ?>" value="<?php echo $show_low_wl_val; ?>" onkeyup="<?php echo $function_for_lwl; ?>" onkeypress="return isNumericInput(event)"></td> -->
                <input type="text" class="input-small" maxlength="6" name="numonly" style="text-align: center; font-weight: bold;" id="<?php echo $low_wl; ?>" value="<?php echo $show_low_wl_val; ?>" 
                onkeyup="call_lowest_val(event, '<?php echo $low_wl; ?>',
                '<?php echo $hr6_date_id; ?>','<?php echo $hr9_date_id; ?>','<?php echo $hr12_date_id; ?>',
                '<?php echo $hr15_date_id; ?>','<?php echo $hr18_date_id;?>','<?php echo $after_lwl_id;?>', '<?php echo $d;?>')"
                onkeypress="return isNumericInput(event)"></td>
    <?php } ?>
            <td style="text-align: right;">
                <button class="btn btn-minier btn-purple" type="button" onclick="save_single_water_level_info('<?php echo $d; ?>','btn_click')"><i class="icon-save"></i> <b style="font-size: 12px;">Save</b></button>
                <!-- <?php
                  if($_SESSION['delete'] == 'Y'){
                    echo '&nbsp;<button class="btn btn-minier btn-danger" type="button" onclick = "delete_data('.$d.')">
                    <i class="icon-trash"></i> <b style="font-size: 12px;">Delete</b></button>';
                  }
                ?> -->
            </td>
            </tr>
        <?php }} ?>        
    </tbody>
</table>
<div class="row-fluid" style="margin-bottom:5px;">
	<div class="control-group" style="float:right;">
		<div class="controls">
			<button id="wtl_sva_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-success " onclick="wl_multiple_save()"><i class="icon-save"></i><b>Save All</b></button>
      <?php
          if($_SESSION['delete'] == 'Y'){
            echo '<button id="wl_delete_btn" type="button" style="margin-right: 1px; font-size: 12px;" class="btn btn-small btn-danger" onclick="wl_delete_all()"><i class="icon-trash"></i><b>Delete All</b></button>';
          }
      ?>
		</div>
	</div>
</div> 
<?php
}
?>
<!--New Script (13-05-2023)-->
<script>
  function call_highest_val(e,high_col_id,first,second,third,fourth,fith,next_col_id){

    if(event.keyCode==13 || event.keyCode=='tab' || event.keyCode==9){
      var high_val = $("#"+high_col_id).val();
      //high_val = parseFloat(high_val).toFixed(2);
      //alert(high_val);
    //**** cell value array ****//
    var data = [];
    var data_id = [];
    data.push($("#"+first).val());
    data.push($("#"+second).val());
    data.push($("#"+third).val());
    data.push($("#"+fourth).val());
    data.push($("#"+fith).val());
    //alert (data);
    //**** cell value array ****//

    //**** cell id array ****//
    data_id.push(first);
    data_id.push(second);
    data_id.push(third);
    data_id.push(fourth);
    data_id.push(fith);
    //alert(data_id);
    //**** cell id array ****//

    var len = data.length;
    var max = Math.max(...data);
    //const max_index = data.indexOf(Math.max(...data));

    for(var i =0; i < len; i++){
      if(data[i] == max){
        var max_index = i;
      }
    }
    //alert(max);
    //alert(max_index);
    //alert(data_id[max_index]);
    var curr_high = high_val;
    var data_high = max;
    var data_high_id = data_id[max_index];
    //alert(data_high_id);
    if(data_high > curr_high){
      //alert("true");
      $("#"+high_col_id).val('');
      $("#"+data_high_id).select();
      alert("The Highest Value Can Not Be Less Than The Regular Value");
    }
    else{
      //$("#"+next_col_id).select();
      //alert("call_next_val_loader");
      call_next_val_loader(event,high_col_id,next_col_id)
    }
  }
}
</script>
<!--New Script(13-05-2023)-->
<!--Script for Lowest-->
<script>
  function call_lowest_val(e,low_col_id,first,second,third,fourth,fith,next_col_id,day){

    if(event.keyCode==13 || event.keyCode=='tab' || event.keyCode==9){
      var low_val = $("#"+low_col_id).val();
      //high_val = parseFloat(high_val).toFixed(2);
      //alert(low_val);
    //**** cell value array ****//
    var data = [];
    var data_id = [];
    data.push($("#"+first).val());
    data.push($("#"+second).val());
    data.push($("#"+third).val());
    data.push($("#"+fourth).val());
    data.push($("#"+fith).val());
    //alert (data);
    //**** cell value array ****//

    //**** cell id array ****//
    data_id.push(first);
    data_id.push(second);
    data_id.push(third);
    data_id.push(fourth);
    data_id.push(fith);
    //alert(data_id);
    //**** cell id array ****//

    var len = data.length;
    var min = Math.min(...data);
    //const max_index = data.indexOf(Math.max(...data));

    for(var i =0; i < len; i++){
      if(data[i] == min){
        var min_index = i;
      }
    }
    //alert(min);
    //alert(min_index);
    //alert(data_id[min_index]);
    var curr_low = low_val;
    var data_low = min;
    var data_low_id = data_id[min_index];
    //alert(data_low_id);
    if(data_low < curr_low){
      //alert("true");
      $("#"+low_col_id).val('');
      $("#"+data_low_id).select();
      alert("The Lowest Value Can Not Be Higher Than The Regular Value");
    }
    else{
      //$("#"+next_col_id).select();
      //alert("call_save_function");
      call_save_function(event,low_col_id,next_col_id,day)
    }
  }
}
</script>
<!--Script for Lowest-->
<script>
  //   function call_next_val_loader(e,sel_col_id,next_col_id){
	// 	if(event.keyCode==13 || event.keyCode=='tab' || event.keyCode==9){
  //     //if(event.keyCode==13){
	// 		var col_name_arr = sel_col_id.split("_");
	// 		var col_name = col_name_arr[0];
	// 		var curr_val = $("#"+sel_col_id).val();
      
	// 		if(col_name=='ht' || col_name=='lt'){
	// 			if(curr_val.length==1 || curr_val.length==2){
	// 				curr_val = curr_val+'00';
	// 				$("#"+sel_col_id).val(curr_val);
	// 			}
  //       //four digit changing code 12-05-2023
  //       if (curr_val.length == 3){
  //           //alert(curr_val);
  //           curr_val = '0'+curr_val;
  //           $("#"+sel_col_id).val(curr_val);
  //         }
  //       //four digit changing code 12-05-2023
	// 		}
	// 		else{
	// 			if (curr_val > Math.floor(curr_val)) {
	// 				curr_val = parseFloat(curr_val).toFixed(2);
	// 			}
	// 			else{
	// 				curr_val = (Math.round(curr_val * 100) / 100).toFixed(2);
	// 			}
	// 			$("#"+sel_col_id).val(curr_val);
	// 		}
	// 		//$("#"+sel_col_id).select();
	// 		//alert('moving to the next column');
	// 		//save_water_level_info(sel_col_id,curr_val,next_col_id);
  //     //alert(curr_val);
  //     if(curr_val == 0.00){
  //       //to show 0 in data table below li
  //       // curr_val = "";
  //       $("#"+sel_col_id).val(curr_val);
  //       $("#"+next_col_id).select();
  //     }else
  //     {
  //       $("#"+next_col_id).select();
  //     }
	// 		$("#"+next_col_id).select();
	// 	}
	// }
//   function call_next_val_loader(event, sel_col_id, next_col_id) {
//   if (event.keyCode === 13 || event.keyCode === 9 || event.key === 'Tab') {
//     var col_name_arr = sel_col_id.split("_");
//     var col_name = col_name_arr[0];
//     var curr_val = $("#" + sel_col_id).val();

//     if (col_name === 'ht' || col_name === 'lt') {
//       if (curr_val.length === 1 || curr_val.length === 2) {
//         curr_val = curr_val + '00';
//         $("#" + sel_col_id).val(curr_val);
//       }
//       // Add code to make it four digits if it's three digits
//       if (curr_val.length === 3) {
//         curr_val = '0' + curr_val;
//         $("#" + sel_col_id).val(curr_val);
//       }
//     } else {
//       if (curr_val === '0.00') {
//         $("#" + sel_col_id).val('');
//       } else if (curr_val > Math.floor(curr_val)) {
//         curr_val = parseFloat(curr_val).toFixed(2);
//       } else {
//         curr_val = (Math.round(curr_val * 100) / 100).toFixed(2);
//       }
//       $("#" + sel_col_id).val(curr_val);
//     }

//     if (curr_val === '0.00') {
//       $("#"+next_col_id).select();
//     } else {
//       $("#" + next_col_id).select();
//     }
//   }
// }
function call_next_val_loader(event, sel_col_id, next_col_id) {
  if (event.keyCode === 13 || event.keyCode === 9 || event.key === 'Tab') {
    var col_name_arr = sel_col_id.split("_");
    var col_name = col_name_arr[0];
    var curr_val = $("#" + sel_col_id).val();

    if (curr_val === '0') {
      // If the value is '0', set it as is
      $("#" + sel_col_id).val('0');
    } else if (col_name === 'ht' || col_name === 'lt') {
      if (curr_val.length === 1 || curr_val.length === 2) {
        curr_val = curr_val + '00';
        $("#" + sel_col_id).val(curr_val);
      }
      // Add code to make it four digits if it's three digits
      if (curr_val.length === 3) {
        curr_val = '0' + curr_val;
        $("#" + sel_col_id).val(curr_val);
      }
    } else {
      if (curr_val !== '') {
        if (curr_val > Math.floor(curr_val)) {
          curr_val = parseFloat(curr_val).toFixed(2);
        } else {
          curr_val = (Math.round(curr_val * 100) / 100).toFixed(2);
        }
        $("#" + sel_col_id).val(curr_val);
      }
    }

    $("#" + next_col_id).select();
  }
}


	function call_save_function(e,sel_col_id,next_col_id,sel_day){
		if(event.keyCode==13 || event.keyCode=='tab' || event.keyCode==9){
			var col_name_arr = sel_col_id.split("_");
			var col_name = col_name_arr[0];
			var curr_val = $("#"+sel_col_id).val();
			if(col_name=='ht' || col_name=='lt'){
				if(curr_val.length==1 || curr_val.length==2){
					curr_val = curr_val+'00';
					$("#"+sel_col_id).val(curr_val);
				}
			}
			else{
				if (curr_val > Math.floor(curr_val)) {
					curr_val = parseFloat(curr_val).toFixed(2);
				}
				else{
					curr_val = (Math.round(curr_val * 100) / 100).toFixed(2);
				}
				$("#"+sel_col_id).val(curr_val);
			}
			var press_op = "enter_click";
			save_single_water_level_info(sel_day,press_op);
			$("#"+next_col_id).select();
		}
	}
		
	/*$(function() {
		$("input[name='numonly']").on('input', function(e) {
			
			//$(this).val($(this).val().replace(/[^0-9]/g, ''));
			//$(this).val($(this).val().replace(/^[+-]?\d+(\.\d+)?$/, ''));
			$(this).val($(this).val().replace(/^((-)?(0|([1-9][0-9]*))(\.[0-9]+)?)$/, ''));
		});
	});*/
</script>

 <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 the input would result in a value less than -99.99 or greater than 99.99, disallow it
  const newValue = parseFloat(currentValue + keyChar).toFixed(2);
  if (newValue < -99.99 || newValue > 99.99) {
    event.preventDefault();
    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>

<script>
let inputReset = false;
function isNumberKey(evt) {
    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 a value less than -99.99 or greater than 99.99, disallow it
  const newValue = parseFloat(currentValue + keyChar);
  if (newValue < 0000 || newValue > 9999) {
    event.preventDefault();
    event.target.value = "";
    event.target.value += keyChar;
    return 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 !== "-") {
    inputReset = true;
    event.preventDefault();
    return false;
  }
  
  if (currentValue === "0" && keyChar !== "." && keyChar !== "-") {
    event.preventDefault();
    return false;
  }

  return true;
}
</script>