$(document).ready(function(){
	load();
});

/////////--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--/////////

function add_station(){
	$("#add_station").load("module/ground_water_ex/add_station_modal.php");
}

function load(){
	$('#cover-spin').show(0);
    $.ajax({
        url:"module/ground_water_ex/gw_station_datatable.php",
        datatype:'get',
       // data: datastr,
        success:function(data){
			$('#cover-spin').hide();
			$("#data_to_show").html(data);
        }
    });  
	//$("#data_to_show").load("module/morphology/station_datatable.php");
}	

function form_reset(){
	document.getElementById("add_station_modal").reset();
}

//////////----View Form----/////////
function load_station_vw_modal(well_id){
	var datastr = "well_id="+well_id;
	//alert(datastr);
	$("#model_stat_vw_body").load("module/ground_water_ex/gw_station_details_vw.php",datastr);
}
//////////----View Form----/////////

//////////----Edit Form----/////////
function load_station_edit_modal(well_id){
	var datastr = "well_id="+well_id;
	//alert(datastr);
	$("#model_stat_edit_body").load("module/ground_water_ex/gw_station_edit_form.php",datastr);
}
//////////----Edit Form----/////////

//////////----Insert Data----/////////
function add_gw_station(){
	var operation =  "insert_gw_station_data";
	var well_id =  $('#well_id').val();
	var old_id =  $('#new_id').val();
	var upz_code =  $('#upz_code').val();
	var sub_div =  $('#sub_div').val();
	var well_type =  $('#well_type').val();
	var long =  $('#long').val();
	var sink_dt =  $('#sink_dt').val();
	var lat =  $('#lat').val();
	var para_hgt =  $('#para_hgt').val();
	var casing_dia =  $('#casing_dia').val();
	var ri_para =  $('#ri_para').val();
	var opt =  $('#opt').val();
	var depth =  $('#depth').val();
	var strainer =  $('#strainer').val();
	var well_dia =  $('#well_dia').val();
	var village =  $('#village').val();
	var vill_m =  $('#vill_m').val();
	var site =  $('#site').val();
	var note =  encodeURIComponent($('#note').val());
	// var note =  $('#note').val();
	var mob_no =  $('#mob_no').val();
	var cont_per =  $('#cont_per').val();
	var data_type =  $('#data_type').val();
	const note_bytesize = note.length;
	const mob_bytesize = mob_no.length;
	
	if(validateInput(well_id) == false){
		alert('line 83');
		alert('Please Insert The Well ID correctly');
		$('#well_id').val('');
		$('#well_id').focus();
        return false;
	}
	else if(validateInput(old_id) == false){
		alert('Please Insert The New ID correctly');
		$('#new_id').val('');
		$('#new_id').focus();
        return false;
	}
	// else if(validateInput(data_type) == false){
	// 	alert('Please Select The Data Type');
	// 	$('#data_type').val('');
	// 	$('#data_type').focus();
    //     return false;
	// }
	else if(validateInput(sink_dt) == false){
		alert('Please Insert The Sink Dt correctly');
		$('#sink_dt').val('');
		$('#sink_dt').focus();
        return false;
	}
	else if(validateInput(long) == false){
		alert('Please Insert the Longitude correctly');
		$('#long').val('');
		$('#long').focus();
        return false;
	}
	
	else if(validateInput(lat) == false){
		alert('Please Insert The Latitude correctly');
		$('#lat').val('');
		$('#lat').focus();
        return false;
	}
	else if(validateInput(para_hgt) == false){
		alert('Please Insert The Parapet Hgt correctly');
		$('#para_hgt').val('');
		$('#para_hgt').focus();
        return false;
	}
	else if(validateInput(casing_dia) == false){
		alert('Please Insert The Casing Data correctly');
		$('#casing_dia').val('');
		$('#casing_dia').focus();
        return false;
	}
	else if(validateInput(ri_para) == false){
		alert('Please Insert The Rl Paraprt correctly');
		$('#ri_para').val('');
		$('#ri_para').focus();
        return false;
	}
	else if(validateInput(opt) == false){
		alert('Please Insert The Opt Status correctly');
		$('#opt').val('');
		$('#opt').focus();
        return false;
	}
	else if(validateInput(depth) == false){
		alert('Please Insert The Depth correctly');
		$('#depth').val('');
		$('#depth').focus();
        return false;
	}
	else if(validateInput(strainer) == false){
		alert('Please Insert The Strainer correctly');
		$('#strainer').val('');
		$('#strainer').focus();
        return false;
	}
	else if(validateInput(well_dia) == false){
		alert('Please Insert The Well Dia correctly');
		$('#well_dia').val('');
		$('#well_dia').focus();
        return false;
	}
	else if(validateInput(village) == false){
		alert('Please Insert The Village/Address correctly');
		$('#village').val('');
		$('#village').focus();
        return false;
	}
	else if(validateInput(vill_m) == false){
		alert('Please Insert The Vill Mound correctly');
		$('#vill_m').val('');
		$('#vill_m').focus();
        return false;
	}
	else if(validateInput(site) == false){
		alert('Please Insert The Site correctly');
		$('#site').val('');
		$('#site').focus();
        return false;
	}
	else if(validateInput(note) == false){
		alert('Please Insert The Note correctly');
		$('#note').val('');
		$('#note').focus();
        return false;
	}
	else if (note.match(/[$&+,:;=?[\]@#|{}'<>.^*()%!-/]/,"")){
		alert('Please Check The Note Format');
		$('#note').val('');
		$('#note').focus();
        return false;
	}
	else if(note_bytesize > 200){
		alert("Note Length Cannot Be More Than 200 Bytes");
		$('#note').val('');
		$('#note').focus();
        return false;
	}
	else if(validateInput(mob_no) == false){
		alert('Please Insert The Mobile No. correctly');
		$('#mob_no').val('');
		$('#mob_no').focus();
        return false;
	}
	else if(mob_bytesize > 11){
		alert("Mobile Number's Length Cannot Be More Than 11 Bytes");
		$('#mob_no').val('');
		$('#mob_no').focus();
        return false;
	}
	else if(validateInput(cont_per) == false){
		alert('Please Insert The Cotact Person Name correctly');
		$('#cont_per').val('');
		$('#cont_per').focus();
        return false;
	}

	else if(well_id == ''){
		alert('Please Insert The Well ID');
		$('#well_id').focus();
        return false;
	}else if(old_id == ''){
		alert('Please Insert The Old ID');
		$('#new_id').val('');
		$('#new_id').focus();
        return false;
	}
	
	else if(upz_code == ''){
		alert('Please Select The Upzila');
		$('#upz_code').focus();
        return false;
	}
	else if(sub_div == ''){
		alert('Please Select The Sub-Division');
		$('#sub_div').focus();
        return false;
	}
	else if(well_type == ''){
		alert('Please Select The Well Type');
		$('#well_type').focus();
        return false;
	}
	else if(data_type === null){
		alert('Please Select The Data Type');
		$('#data_type').focus();
        return false;
	}
	else if (lat == ''){
		alert('Please Insert The Latitude');
		// $('#lat').val('');
		$('#lat').focus();
        return false;
	}
	else if (long == ''){
		alert('Please Insert the Longitude');
		// $('#long').val('');
		$('#long').focus();
        return false;
	}
	
	else{
		var datastr = "operation="+operation+"&well_id="+well_id+"&old_id="+old_id+
		"&upz_code="+upz_code+"&sub_div="+sub_div+"&well_type="+well_type+	
		"&long="+long+"&sink_dt="+sink_dt+"&lat="+lat+"&para_hgt="+para_hgt+"&casing_dia="+casing_dia+
		"&ri_para="+ri_para+"&opt="+opt+"&depth="+depth+"&strainer="+strainer+	
		"&well_dia="+well_dia+"&village="+village+"&vill_m="+vill_m+"&site="+site+	
		"&note="+note+"&mob_no="+mob_no+"&cont_per="+cont_per+"&data_type="+data_type;
	}

	
	// alert(datastr);
	$('#cover-spin').show(0);
    $.ajax({
        url:"module/ground_water_ex/gw_station_ajax.php",
        datatype:'post',
		data: datastr,
        success:function(data){
			$('#cover-spin').hide();
			// alert(data);
			if(data==1){
				alert('Information has been updated successfully');
				$('#modal_station').modal('hide');
				load();
			}
			else{
				alert('error occurred');
			}
        }
    });
}
//////////----Insert Data----/////////


//////////----Update Data----/////////
function update_gw_station(){
	//alert('update_gw_station');

	var operation =  "update_gw_station_data";
	var old_well_id = $('#e_old_well_id').val();
	var well_id =  $('#e_well_id').val();
	var old_id =  $('#e_new_id').val();
	var upz_code =  $('#e_upz_code').val();
	var sub_div =  $('#e_sub_div').val();
	var well_type =  $('#e_well_type').val();
	var long =  $('#e_long').val();
	var sink_dt =  $('#e_sink_dt').val();
	var lat =  $('#e_lat').val();
	var para_hgt =  $('#e_para_hgt').val();
	var casing_dia =  $('#e_casing_dia').val();
	var ri_para =  $('#e_ri_para').val();
	var opt =  $('#e_opt').val();
	var depth =  $('#e_depth').val();
	var strainer =  $('#e_strainer').val();
	var well_dia =  $('#e_well_dia').val();
	var village =  $('#e_village').val();
	var vill_m =  $('#e_vill_m').val();
	var site =  $('#e_site').val();
	var note =  encodeURIComponent($('#e_note').val());
	var mob_no =  $('#e_mob_no').val();
	var cont_per =  $('#e_cont_per').val();
	var e_data_type =  $('#e_data_type').val();
	const note_bytesize = note.length;
	const mob_bytesize = mob_no.length;
	if(validateInput(well_id) == false){
		alert('Please Insert The Well ID correctly');
		$('#e_well_id').val('');
		$('#e_well_id').focus();
        return false;
	}
	else if(validateInput(old_id) == false){
		alert('Please Insert The Old ID correctly');
		$('#e_new_id').val('');
		$('#e_new_id').focus();
        return false;
	}
	else if(validateInput(long) == false){
		alert('Please Insert the Longitude correctly');
		$('#e_long').val('');
		$('#e_long').focus();
        return false;
	}
	else if(validateInput(sink_dt) == false){
		alert('Please Insert The Sink Dt correctly');
		$('#e_sink_dt').val('');
		$('#e_sink_dt').focus();
        return false;
	}
	else if(validateInput(lat) == false){
		alert('Please Insert The Latitude correctly');
		$('#e_lat').val('');
		$('#e_lat').focus();
        return false;
	}
	else if(validateInput(para_hgt) == false){
		alert('Please Insert The Parapet Hgt correctly');
		$('#e_para_hgt').val('');
		$('#e_para_hgt').focus();
        return false;
	}
	else if(validateInput(casing_dia) == false){
		alert('Please Insert The Casing Data correctly');
		$('#e_casing_dia').val('');
		$('#e_casing_dia').focus();
        return false;
	}
	else if(validateInput(ri_para) == false){
		alert('Please Insert The Rl Paraprt correctly');
		$('#e_ri_para').val('');
		$('#e_ri_para').focus();
        return false;
	}
	else if(validateInput(opt) == false){
		alert('Please Insert The Opt Status correctly');
		$('#e_opt').val('');
		$('#e_opt').focus();
        return false;
	}
	else if(validateInput(depth) == false){
		alert('Please Insert The Depth correctly');
		$('#e_depth').val('');
		$('#e_depth').focus();
        return false;
	}
	else if(validateInput(strainer) == false){
		alert('Please Insert The Strainer correctly');
		$('#e_strainer').val('');
		$('#e_strainer').focus();
        return false;
	}
	else if(validateInput(well_dia) == false){
		alert('Please Insert The Well Dia correctly');
		$('#e_well_dia').val('');
		$('#e_well_dia').focus();
        return false;
	}
	else if(validateInput(village) == false){
		alert('Please Insert The Village/Address correctly');
		$('#e_village').val('');
		$('#e_village').focus();
        return false;
	}
	else if(validateInput(vill_m) == false){
		alert('Please Insert The Vill Mound correctly');
		$('#e_vill_m').val('');
		$('#e_vill_m').focus();
        return false;
	}
	else if(validateInput(site) == false){
		alert('Please Insert The Site correctly');
		$('#e_site').val('');
		$('#e_site').focus();
        return false;
	}
	else if(validateInput(note) == false){
		alert('Please Insert The Note correctly');
		$('#e_note').val('');
		$('#e_note').focus();
        return false;
	}
	else if (note.match(/[$&+,:;=?[\]@#|{}'<>.^*()%!-/]/,"")){
		alert('Please Check The Note Format');
		$('#e_note').val('');
		$('#e_note').focus();
        return false;
	}
	else if(note_bytesize > 200){
		alert("Note Length Cannot Be More Than 200 Bytes");
		$('#e_note').val('');
		$('#e_note').focus();
        return false;
	}
	else if(validateInput(mob_no) == false){
		alert('Please Insert The Mobile No. correctly');
		$('#e_mob_no').val('');
		$('#e_mob_no').focus();
        return false;
	}
	else if(mob_bytesize > 11){
		alert("Mobile Number's Length Cannot Be More Than 11 Bytes");
		$('#e_mob_no').val('');
		$('#e_mob_no').focus();
        return false;
	}
	else if(validateInput(cont_per) == false){
		alert('Please Insert The Cotact Person Name correctly');
		$('#e_cont_per').val('');
		$('#e_cont_per').focus();
        return false;
	}
	// else if(validateInput(e_data_type) == false){
	// 	alert('Please Select The Well Data Type');
	// 	$('#e_data_type').val('');
	// 	$('#e_data_type').focus();
    //     return false;
	// }
	else if(well_id == ''){
		alert('Please Insert The Well ID');
		// $('#e_well_id').val('');
		$('#e_well_id').focus();
        return false;
	}
	else if(old_id == ''){
		alert('Please Insert The Old ID');
		// $('#e_new_id').val('');
		$('#e_new_id').focus();
        return false;
	}
	else if(upz_code == ''){
		alert('Please Select The Upzila');
		$('#e_upz_code').focus();
        return false;
	}
	else if(sub_div == ''){
		alert('Please Select The Sub-Division');
		$('#e_sub_div').focus();
        return false;
	}
	else if(well_type == ''){
		alert('Please Select The Well Type');
		$('#e_well_type').focus();
        return false;
	}
	else if(e_data_type == null){
		alert('Please Select The Well Data Type');
		// $('#e_data_type').val('');
		$('#e_data_type').focus();
        return false;
	}
	else if (lat == ''){
		alert('Please Insert The Latitude');
		// $('#lat').val('');
		$('#e_lat').focus();
        return false;
	}
	else if (long == ''){
		alert('Please Insert the Longitude');
		// $('#long').val('');
		$('#e_long').focus();
        return false;
	}
	else{
		var datastr = "operation="+operation+"&old_well_id="+old_well_id+"&well_id="+well_id+"&old_id="+old_id+
		"&upz_code="+upz_code+"&sub_div="+sub_div+"&well_type="+well_type+	
		"&long="+long+"&sink_dt="+sink_dt+"&lat="+lat+"&para_hgt="+para_hgt+"&casing_dia="+casing_dia+
		"&ri_para="+ri_para+"&opt="+opt+"&depth="+depth+"&strainer="+strainer+	
		"&well_dia="+well_dia+"&village="+village+"&vill_m="+vill_m+"&site="+site+	
		"&note="+note+"&mob_no="+mob_no+"&cont_per="+cont_per+"&e_data_type="+e_data_type;
	
	
	// alert(datastr);
	$('#cover-spin').show(0);
    $.ajax({
        url:"module/ground_water_ex/gw_station_ajax.php",
        datatype:'post',
		data: datastr,
        success:function(data){
			$('#cover-spin').hide();
		//	alert(data);
			if(data==1){
				alert('Information has been updated successfully');
				$('#modal-stat-edit').modal('hide');
				load();
			}
			else{
				alert('error occurred');
			}
        }
    });  
}
}
//////////----Update Data----/////////

