function load_stat_for_de(sel_subdiv_id){
	var datastr = "sel_subdiv_id="+sel_subdiv_id;
	//$('#station').val('').trigger('chosen:updated');
	$('#view_rfi_data').html("");
	$('#rf_curve_chart').html("");
//	alert(datastr);
	$("#de_stat_list_holder").load('module/ground_water_ex/rainfall_station_list_by_river.php', datastr);
}

function load_stat_for_ex(sel_subdiv_id){
	var datastr = "sel_subdiv_id="+sel_subdiv_id;
	//$('#station').val('').trigger('chosen:updated');
	$('#view_rfi_data').html("");
	$('#rf_curve_chart').html("");
	//alert(datastr);
	$("#ex_stat_list_holder").load('module/ground_water_ex/rainfall_station_list_by_river.php', datastr); 
}

function loadDeForm(){
//	alert('loadDeForm');
	$('#view_rfi_data').html("");
	$('#rf_curve_chart').html("");
	$('#act_tab_name').val('data_entry');
	$('.nav-tabs a[href="#data_list"]').tab('show');
}

function loadExcelData(){
	//alert('loadExcelData');
//	$('#ex_sub_div').val('').trigger('chosen:updated');
	//$('#station').val('').trigger('chosen:updated');
	$('#view_rfi_data').html("");
	$('#rf_curve_chart').html("");
	$('#act_tab_name').val('data_upload');
	$('.nav-tabs a[href="#data_list"]').tab('show');
}

function flushData() {
	$('#view_rfi_data').html("");
	$('#rf_curve_chart').html("");
}

function load_rainfall_info(){
	var subdivision =  $('#de_sub_div').val();
	var station = $("#station").val();
	var year = $('#rf_year').val();
	var month = $('#rf_month').val();

	if(!subdivision){
		alert('Please Select a Sub-division');
	}
	else if(!station){
		alert('Please Select Station');
	}
	else if(!year) {
		alert('Please select Year');
	}
	else if(!month) {
		alert('Please select Month');
	}
	else{
		var station_arr = station.split("#");
		var station_id = station_arr[0];
		// var station_name = station_arr[1];
	
		var datastr = "subdivision="+subdivision+"&station="+station_id+"&year="+year+"&month="+month;
		//alert(datastr);
		$('#cover-spin').show(0);
		$.ajax({
			url: 'module/ground_water_ex/rainfall_information_datatable.php', 
			type: 'get',
			data: datastr,
			cache: false,
			success: function(data) {
					//alert(data);
					$('#cover-spin').hide();
					$("#view_rfi_data").html(data);
					$('#rf_1').select();
				}
		});
	}
}

function save_all_de_rf_data() {
	var station = $("#station").val();
	
	var station_arr = station.split("#");
	var station_id = station_arr[0];
	var station_name = station_arr[1];
	
	var rf_mon_tot_day = $("#rf_mon_tot_day").val();
	if(rf_mon_tot_day==""){
		alert('Please load the data first');
	}else{
		$('#cover-spin').show(0);
		for(var sel_day = 1; sel_day <= rf_mon_tot_day; sel_day++){
			var date = $('#date_'+sel_day).val();
			var rainfall_data = $('#rf_'+sel_day).val(); 
			var operation = "single_row_save";
			var datastr = "sel_date="+date+"&station_id="+station_id+"&rainfall_data="+rainfall_data+"&operation="+operation;
			//alert(datastr);
			$.ajax({
				url: 'module/ground_water_ex/rainfall_information_ajax.php', 
				type: 'post',
				data: datastr,
				cache: false,
				success: function(result) {
					// alert(result);
					if(result==1){
					//	alert('Data List has been saved successfully');
					}
					else{
						 alert(result);
					}
				}
			});
		}
		if(sel_day >= rf_mon_tot_day){
			//alert(sel_day);
			alert('Data has been saved successfully');
			$('#cover-spin').hide();
		}
	}
}

function save_all_ex_rf_data() {
	var station = $("#station").val();
	
	var station_arr = station.split("#");
	var station_id = station_arr[0];
	var station_name = station_arr[1];
	
	var rf_mon_tot_day = $("#rf_mon_tot_day").val();
	if(rf_mon_tot_day==""){
		alert('Please load the data first');
	}else{
		$('#cover-spin').show(0);
		for(var sel_day = 1; sel_day <= rf_mon_tot_day; sel_day++){
			var date = $('#date_'+sel_day).val();
			var rainfall_data = $('#rf_'+sel_day).val(); 
			var operation = "single_row_save";
			var datastr = "sel_date="+date+"&station_id="+station_id+"&rainfall_data="+rainfall_data+"&operation="+operation;
			//alert(datastr);
			$.ajax({
				url: 'module/ground_water_ex/rainfall_information_ajax.php', 
				type: 'post',
				data: datastr,
				cache: false,
				success: function(result) {
					// alert(result);
					if(result==1){
					//	alert('Data List has been saved successfully');
					}
					else{
						 alert(result);
					}
				}
			});
		}
		if(sel_day >= rf_mon_tot_day){
			//alert(sel_day);
			alert('Data has been saved successfully');
			$('#cover-spin').hide();
		}
	}
}

// function save_all_ex_rf_data() {
//     var station = $("#station").val();
//     var station_arr = station.split("#");
//     var station_id = station_arr[0];
//     var station_name = station_arr[1];

//     var rf_mon_tot_day = $("#rf_mon_tot_day").val();
    
//     if (rf_mon_tot_day == "") {
//         alert('Please load the data first');
//     } else {
//         $('#cover-spin').show(0);

//         var dataToSave = [];

//         // Iterate through each row with the 'rowID' class
//         $('.rowID').each(function () {
//             var date = $(this).find('.month_date').val();
//             var rainfall = $(this).find('.rainfall').val();

//             // Create an object for this row and push it into the array
//             dataToSave.push({ date: date, rainfall_data: rainfall });
//         });

//         var operation = "multiple_rows_save";
//         var datastr = "station_id=" + station_id + "&operation=" + operation + "&data=" + JSON.stringify(dataToSave);

//         $.ajax({
//             url: 'module/surface_water/rainfall_information_ajax.php',
//             type: 'post',
//             data: datastr,
//             cache: false,
//             success: function (result) {
//                 $('#cover-spin').hide();
//                 if (result == 1) {
//                     alert('Data has been saved successfully');
//                     // Handle success, e.g., refresh the page or update UI
//                 } else {
//                     // Handle the case when save fails
//                     alert('Failed to save data.');
//                 }
//             }
//         });
//     }
// }

function load_rain_fall_graph(){
	//alert('hello');
	var station = $("#station").val();
	var act_tab_name = $("#act_tab_name").val();
	/*if(act_tab_name=='data_entry'){
		var date = $('#de_mm_yr').val();
	}
	else{
		var date = $('#ex_mm_yr').val();
	}*/
	// alert(station);
	var station_arr = station.split("#");
	var station_id = station_arr[0];
	var station_name = station_arr[1];
	var year = $('#rf_year').val();
	var month = $('#rf_month').val();
	var graph_str = "station="+station_id+"&year="+year+"&month="+month;
	//alert(graph_str);
	if(!station){
		alert('Please Select Station');
	}
	else if(!year) {
		alert('Please select Year');
	}
	else if(!month) {
		alert('Please select Month');
	}
	else{
		$('#cover-spin').show(0);
		$.ajax({
			type:"post",
			url:"module/ground_water_ex/rainfall_graph.php",
			data:graph_str,
			success:function(str){
				if($.trim(str)){
					var my_2d = $.parseJSON(str);
				//	alert(my_2d);
					google.charts.load('current', {packages: ['corechart', 'line']});
					google.charts.setOnLoadCallback(drawChart);
					function drawChart(){
						var data=new google.visualization.DataTable();
						data.addColumn('string','Date');
						data.addColumn('number','Rainfall');
						
						for(i=0;i<my_2d.length;i++)
							data.addRow([my_2d[i][0],parseFloat(my_2d[i][1])]);
						
						var options = {
							title: 'Rainfall Information Station ID: '+station_id+' ('+station_name+')',
							//curveType: 'function',
							pointSize: 5,
							pointShape: 'circle',
							width: 1700,
							height: 500,
							legend: { position: 'bottom' },
							animation: {
							  'startup': true,
							  duration: 5000,
							  easing: 'out',
							},
							hAxis: {
								title: 'Dates'
							},
							vAxis: {
							  title: 'Rainfall (mm)',
							  viewWindow: {
								min: data.getColumnRange(1) , 
								max: data.getColumnRange(1),
							  },
							  format: '0.00',
							  gridlines: {
								count: 20
							  }
							}
						  };
						
						var chart=new
						google.visualization.LineChart(document.getElementById('rf_curve_chart'))
						chart.draw(data,options);
						$('#cover-spin').hide();
					}
				}
			}
		});
	}
}

/*
function rain_fall_info(rf_col_id,next_col_name,date_id) {
	var date = $("#"+date_id).val();
	var station = $("#station").val();
	// alert(station);
	var station_arr = station.split("#");
	var station_id = station_arr[0];
	var station_name = station_arr[1];
	
    var rainfall_data = $("#"+rf_col_id).val(); 
	var operation = "single_row_save";
	datastr = "station_id="+station_id+"&rainfall_data="+rainfall_data+"&sel_date="+date+"&operation="+operation;
	//alert(datastr);
	$('#cover-spin').show(0);
	$.ajax({
		url: 'module/ground_water_ex/rainfall_information_ajax.php', 
		type: 'post',
		data: datastr,
		cache: false,
		success: function(result) {
		//	alert(result);
			var operation = "get_rainfall_sum";
			var year =  $('#year').val();
			var month =  $('#month').val();
			var datastr = "station_id="+station_id+"&year="+year+"&month="+month+"&operation="+operation;
		//	alert(datastr);
			$.ajax({
				url: 'module/ground_water_ex/rainfall_information_ajax.php', 
				type: 'post',
				data: datastr,
				cache: false,
				success: function(result) {
				//	alert(result);
					$("#total-rainfall").val(result);
				}
			});
			$('#cover-spin').hide();
			$('#'+next_col_name).focus();
		}
	});
}

function save_single_rainfall_info(sel_day) {
	// alert(sel_day);
	var station = $("#station").val();
	// alert(station);
	var station_arr = station.split("#");
	var station_id = station_arr[0];
	var station_name = station_arr[1];
	//var station_id = $("#station").val();
	var date = $('#date_'+sel_day).val();
    var rainfall_data = $('#rf_'+sel_day).val(); 
	var operation = "single_row_save";
	datastr = "sel_date="+date+"&station_id="+station_id+"&rainfall_data="+rainfall_data+"&operation="+operation;
	// alert(datastr);

	$.ajax({
		url: 'module/ground_water_ex/rainfall_information_ajax.php', 
		type: 'post',
		data: datastr,
		cache: false,
		success: function(result) {
			
		}
	});
}
*/