function load_upazila_by_district(sel_district_id){
  //alert('load_upazila_by_district');
	var datastr = "sel_district_id="+sel_district_id;
  //alert(datastr);
	$('#view_gwq_data').html("");
	//$("#gwq_btn").css({"display":"none"});
	//alert(datastr);
	$("#upz_list_holder").load('module/ground_water_ex/gwq_upz_by_dis.php',datastr);
}

function loadExcelData(){
	// alert('mm');
	// $('#data_list').hide();
	$('#view_new_gwq_data').html("");

}
function load_data_view(){
	// alert('mm');
	// $('#data_list').hide();
	$('#view_new_gwq_data').html("");

}

function show_Quality_info() {
	var sub_div = $("#sub_div").val();
	var from_date = $("#from_date").val();
	var to_date = $("#to_date").val();

	// Simple validation
	if (!sub_div || !from_date || !to_date) {
			alert("Please fill in all fields.");
			return;
	}

		var datastr="sub_div="+sub_div+"&from_date="+from_date+"&to_date="+to_date;
	  //  alert(datastr);
		$('#cover-spin').show(0);

	$.ajax({
			url: 'module/ground_water_ex/gw_quality_datatable_view.php',
			type: 'POST',
			data: datastr,
			cache: false,
			success: function(data) {
					// alert(data);
					$('#cover-spin').hide();
				$("#view_new_gwq_data").html(data);
			}
	});
}


function loadDeForm(){
	// alert('mm');
	// $('#data_list').hide();
	$('#view_new_gwq_data').html("");
}

function load_old_id_by_upz(sel_upazila_id){
  //alert('load_old_id_by_upz');
	var datastr = "sel_upazila_id="+sel_upazila_id;
  //alert(datastr);
	$('#view_gwq_data').html("");
	//$("#gwq_btn").css({"display":"none"});
	//alert(datastr);
	$("#old_id_list_holder").load('module/ground_water_ex/gwq_old_id_by_upz.php',datastr);
}

function load_new_id_by_old_id(sel_old_id) {
	//alert("load_new_id_by_old_id");
    var datastr = "sel_old_id="+sel_old_id;
    //alert(datastr);
	//$("#gwq_btn").show();
	$('#view_gwq_data').html(""); 
	$.ajax({
		url: 'module/ground_water_ex/gwq_new_id_list.php', 
		type: 'get',
		data: datastr,
		cache: false,
		success: function(data) {
			$("#well_id_list_holder").html(data);
			load_new_row();
		}
	});
	//$("#well_id_list_holder").load('module/ground_water_ex/gwq_new_id_list.php', datastr);
}

function load_new_row() {
  // Clear the year input
  $("#year").val('');

  // Reset the date dropdown
  $('#date_d').val('0');
	 // Set this to the value of the default option
	 load_date_list();
  $.ajax({
    type: "post",
    url: "module/ground_water_ex/gwq_new_row.php",
    data: datastr,
    success: function(st) {
      // Clear the previous view
      $('#view_gwq_data').html("");
      $('#view_gwq_data').hide();
      $('#view_new_gwq_data').show();
      $('#view_new_gwq_data').html(st);

      // Focus on the date field if needed
      $('#date').select(); 

      // Reset any other forms or inputs you have
      $('#dis')[0].reset(); // Make sure #dis is a form element
      $('#upz')[0].reset(); // Make sure #upz is a form element
      $('#old_id').val(''); // Reset old_id input
      $('#well_id').val(''); // Reset well_id input
      $("#year").val(''); // Reset year input
      $('#date_d').val('0'); // Ensure this resets the select field

      // If you want to focus on the date dropdown
      $('#date_d').select(); 
    }
  }); 
}


function load_date_list(year) {
	//alert("load_date_list");
	var well_id = $('#well_id').val();
	//alert(well_id); 
    var datastr = "well_id="+well_id+"&year="+year;
  //alert(datastr);
	$('#view_gwq_data').html(""); 
    $("#date_list_holder").load('module/ground_water_ex/gwq_date_list.php', datastr);
}

function flushData() {
	$('#view_gwq_data').html("");
}
$('#year').on("keypress",function(){
	//$("#gwd_sva_btn").css({"display":"none"});
	$('#view_gwq_data').html("");
});
//////////--Load Data--/////////
function load_gwq_show_data(){
	//alert('load_swq_show_data');
	var well_id = $('#well_id').val();
	//alert(well_id);
	var date =  $('#date_d').val();
	//alert(date);
	if (date != 0){
		//alert("date is  not zero");
		var datastr ="well_id="+well_id+"&date="+date;
		//alert(datastr);
		$('#cover-spin').show(0);
		$.ajax({
			url: 'module/ground_water_ex/gw_quality_datatable.php', 
			type: 'get',
			data: datastr,
			cache: false,
			success: function(data) {
			//alert(data);
				$('#cover-spin').hide();
				$('#view_new_gwq_data').hide();
				$('#view_gwq_data').show();
				$("#view_gwq_data").html(data);
				$('#e_tempareture').select(); 
			}
		});
		
	}else{
		//alert("date is zero");
		load_new_row();
	}	
}
//////////--Load Data--///////////

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

//////////-- Save --////////////
function gwq_multiple_save(){
	//alert("gwq_multiple_save");
  var well_id = $('#well_id').val();
  var date = $('#date').val();
	var bic = $('#bicarbonate').val();
	var tur = $('#turbidity').val();;
	var temp = $('#tempareture').val();
	var sul= $('#sulphate').val();
	//var orp = $('#orp').val();
	var ph = $('#ph').val();
	var nit = $('#nitrate').val();
	var sal = $('#salinity').val();
	var eh = $('#eh').val();
	var irn = $('#iron').val();
	var pho = $('#phosphate').val();
	var ec = $('#ec').val();
	var man = $('#manganese').val();
	var bro = $('#bromide').val();
	var tds = $('#tds').val();
	var bor = $('#boron').val();
	var alu = $('#aluminium').val();
	var cal = $('#calcium').val();
	var sil = $('#silicon').val();
	var cop = $('#copper').val();
	var mag = $('#magnisium').val();
	var ars = $('#arsenic').val();
	var zin = $('#zinc').val();
	var sod = $('#sodium').val();
	var flu = $('#fluoried').val();
	var cho = $('#chromium').val();
	var pot = $('#potassium').val();
	var iod = $('#iodide').val();
	var chl = $('#chloride').val();
	var car = $('#carbondioxide').val();
	var cab = $('#carbonate').val();
	var dis = $('#dissolvedoxygen').val();
	var har = $('#hardness').val();
	var sar = $('#sar').val();
	var year = $('#year').val();
	const date_bytesize = date.length;
	var operation = "save";
	//alert(operation);
 
	if(validateInput(date) == false){
		alert('Please Insert The Correct Date');
		$('#date').val('');
		$('#date').focus();
        return false;
	}
	else if(date_bytesize > 11){
		alert("Date Length Cannot Be More Than 11 Bytes");
		$('#date').val('');
		$('#date').focus();
        return false;
	}
	else{
		datastr = "well_id="+well_id+"&date="+date+"&temp="+temp+"&tur="+tur+"&ph="+ph+"&dis="+dis+"&eh="+eh+"&ec="+ec+
		"&tds="+tds+"&sal="+sal+"&har="+har+"&sod="+sod+"&pot="+pot+"&cal="+cal+"&mag="+mag+"&nit="+nit+"&sul="+sul+
		"&pho="+pho+"&car="+car+"&cab="+cab+"&bic="+bic+"&flu="+flu+"&bro="+bro+"&iod="+iod+"&chl="+chl+"&ars="+ars+
		"&irn="+irn+"&man="+man+"&bor="+bor+"&sil="+sil+"&alu="+alu+"&cop="+cop+"&zin="+zin+"&cho="+cho+"&sar="+sar+
		"&operation="+operation;
		//alert(datastr);

		$('#cover-spin').show(0);
			$.ajax({
			url: 'module/ground_water_ex/gw_quality_entry_ajax.php', 
			type: 'post',
			data: datastr,
			cache: false,
			success: function(result) {
				//alert(result);
				$('#cover-spin').hide();
				if(result==1){
					alert('Data has been saved successfully');	
					load_date_list(year);		
					load_new_row();
				}
				else{
					alert(result);
				}
			}
		});
	}
}

function gwq_multiple_update(){
	//alert("gwq_multiple_save");
	var well_id = $('#well_id').val();
	var date = $('#e_date').val();
	var bic = $('#e_bicarbonate').val();
	var tur = $('#e_turbidity').val();;
	var temp = $('#e_tempareture').val();
	var sul= $('#e_sulphate').val();
	//var orp = $('#e_orp').val();
	var ph = $('#e_ph').val();
	var nit = $('#e_nitrate').val();
	var sal = $('#e_salinity').val();
	var eh = $('#e_eh').val();
	var irn = $('#e_iron').val();
	var pho = $('#e_phosphate').val();
	var ec = $('#e_ec').val();
	var man = $('#e_manganese').val();
	var bro = $('#e_bromide').val();
	var tds = $('#e_tds').val();
	var bor = $('#e_boron').val();
	var alu = $('#e_aluminium').val();
	var cal = $('#e_calcium').val();
	var sil = $('#e_silica').val();
	var cop = $('#e_copper').val();
	var mag = $('#e_magnisium').val();
	var ars = $('#e_arsenic').val();
	var zin = $('#e_zinc').val();
	var sod = $('#e_sodium').val();
	var flu = $('#e_fluoried').val();
	var cho = $('#e_chromium').val();
	var pot = $('#e_potassium').val();
	var iod = $('#e_iodide').val();
	var chl = $('#e_chloride').val();
	var car = $('#e_carbondioxide').val();
	var cab = $('#e_carbonate').val();
	var dis = $('#e_dissolvedoxygen').val();
	var har = $('#e_hardness').val();
	var sar = $('#e_sar').val();
	var operation = "save";
	//alert(operation);

	datastr = "well_id="+well_id+"&date="+date+"&temp="+temp+"&tur="+tur+"&ph="+ph+"&dis="+dis+"&eh="+eh+"&ec="+ec+"&tds="+tds+
	"&sal="+sal+"&har="+har+"&sod="+sod+"&pot="+pot+"&cal="+cal+"&mag="+mag+"&nit="+nit+"&sul="+sul+"&pho="+pho+"&car="+car+
	"&cab="+cab+"&bic="+bic+"&flu="+flu+"&bro="+bro+"&iod="+iod+"&chl="+chl+"&ars="+ars+"&irn="+irn+"&man="+man+"&bor="+bor+
	"&sil="+sil+"&alu="+alu+"&cop="+cop+"&zin="+zin+"&cho="+cho+"&sar="+sar+"&operation="+operation;	
	//alert(datastr);

	$('#cover-spin').show(0);
	$.ajax({
		url: 'module/ground_water_ex/gw_quality_entry_ajax.php', 
		type: 'post',
		data: datastr,
		cache: false,
		success: function(result) {
			// alert(result);
			$('#cover-spin').hide();
			if(result==1){
				alert('Data has been saved successfully');			
				load_gwq_show_data();	
			}
			else{
				alert(result);
			}
		}
	});
	
}

//////////-- Save --////////////

///Delete///
function gwq_multiple_delete(delete_id,get_date){
	// alert("delete_id="+delete_id+"&get_date="+get_date);
	var year = $('#year').val();

	var operation = "delete";
	var datastr = "delete_id=" + delete_id + "&get_date=" + get_date + "&operation=" + operation;
	// alert(datastr);
	if (confirm("Are you sure want to delete The selected data?")) {
		$.ajax({
		  type: "POST",
		  url: "module/ground_water_ex/gw_quality_entry_ajax.php",
		  data: datastr,

		  success: function (st) {
			// alert(st);
			alert("Information has been Delete");
			load_date_list(year)
			var date_d = $('#date_d').val('');
			load_new_row();
		  },
		});
	}
}


///Delete///
function gw_quality_multiple_save_excel(){
	// alert("mm");
	var well_id = $(".well_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var date_id = $(".date_id").map(function () {
        return $(this).val();
    }).get().join('#');
	var temperature_id = $(".temperature_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var turbidity_id = $(".turbidity_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var ph_id = $(".ph_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var dissolved_oxygen_do_id = $(".dissolved_oxygen_do_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var eh_id = $(".eh_id").map(function () {
		return $(this).val();
	}).get().join("#");
	/*var orp_id = $(".orp_id").map(function () {
		return $(this).val();
	}).get().join("#");*/
	var ec_id = $(".ec_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var tds_id = $(".tds_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var salinity_id = $(".salinity_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var hardness_caco3_id = $(".hardness_caco3_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var sodium_na_id = $(".sodium_na_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var potassium_k_id = $(".potassium_k_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var calcium_ca_id = $(".calcium_ca_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var magnesium_mg_id = $(".magnesium_mg_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var nitrate_no3_id = $(".nitrate_no3_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var sulphate_so42_id = $(".sulphate_so42_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var carbondioxide_co22_id = $(".carbondioxide_co22_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var phosphate_po42_id = $(".phosphate_po42_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var carbonate_co32_id = $(".carbonate_co32_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var bicarbonate_hco3_id = $(".bicarbonate_hco3_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var fluoride_f_id = $(".fluoride_f_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var bromide_br_id = $(".bromide_br_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var iodide_i2_id = $(".iodide_i2_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var chloride_cl_id = $(".chloride_cl_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var arsenic_as_id = $(".arsenic_as_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var iron_fe_id = $(".iron_fe_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var manganese_mn_id = $(".manganese_mn_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var boron_b_id = $(".boron_b_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var silica_si_id = $(".silica_si_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var aluminium_al_id = $(".aluminium_al_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var copper_cu_id = $(".copper_cu_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var zinc_zn_id = $(".zinc_zn_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var chromium_cr_id = $(".chromium_cr_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var sar_id = $(".sar_id").map(function () {
		return $(this).val();
	}).get().join("#");
	var operation="excel_gw_quality_data_entry"

	var datastr = "well_id="+well_id+"&date_id="+date_id+"&temperature_id="+temperature_id+"&turbidity_id="+turbidity_id+
	"&ph_id="+ph_id+"&dissolved_oxygen_do_id="+dissolved_oxygen_do_id+"&eh_id="+eh_id+"&ec_id="+ec_id+"&tds_id="+tds_id+
	"&salinity_id="+salinity_id+"&hardness_caco3_id="+hardness_caco3_id+"&sodium_na_id="+sodium_na_id+"&potassium_k_id="+
	potassium_k_id+"&calcium_ca_id="+calcium_ca_id+"&magnesium_mg_id="+magnesium_mg_id+"&nitrate_no3_id="+nitrate_no3_id+ 
	"&sulphate_so42_id="+sulphate_so42_id+"&carbondioxide_co22_id="+carbondioxide_co22_id+"&phosphate_po42_id="+
	phosphate_po42_id+"&carbonate_co32_id="+carbonate_co32_id+"&bicarbonate_hco3_id="+bicarbonate_hco3_id+"&fluoride_f_id="+
	fluoride_f_id+"&bromide_br_id="+bromide_br_id+"&iodide_i2_id="+iodide_i2_id+"&chloride_cl_id="+chloride_cl_id+
	"&arsenic_as_id="+arsenic_as_id+"&iron_fe_id="+iron_fe_id+"&manganese_mn_id="+manganese_mn_id+"&boron_b_id="+boron_b_id+ 
	"&silica_si_id="+silica_si_id+"&aluminium_al_id="+aluminium_al_id+"&copper_cu_id="+copper_cu_id+"&zinc_zn_id="+zinc_zn_id+ 
	"&chromium_cr_id="+chromium_cr_id+"&sar_id="+sar_id+"&operation="+operation;
	//alert(datastr);
	$('#cover-spin').show(0);
	$.ajax({
		type: "POST",
		url: "module/ground_water_ex/gw_quality_entry_ajax.php",
		data: datastr,
		success: function (st) {
			// alert(st);
			$('#cover-spin').hide();
			if (st >= 1) {
				alert("Data successfully saved");
			} else {
				alert("Error saving data");
			}
		}
	});
}