$(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;
// }

function validateInput(input) {
  if (input == "") {
    return false;
  }
}
/////////--Validation--/////////

function add_lithology_code() {
  $("#add_lithology_code").load(
    "module/ground_water_ex/add_material_code_modal.php"
  );
}

function load() {
  $("#cover-spin").show(0);
  $.ajax({
    url: "module/ground_water_ex/lithology_code_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_material_code_modal").reset();
}

//////////----View Form----/////////
function load_material_code_vw_modal(mat_code) {
  var datastr = "mat_code=" + mat_code;
  //alert(datastr);
  $("#model_litho_code_vw_body").load(
    "module/ground_water_ex/lithology_material_code_details_vw.php",
    datastr
  );
}
//////////----View Form----/////////

//////////----Edit Form----/////////
function load_material_code_edit_modal(mat_code) {
  var datastr = "mat_code=" + mat_code;
  //alert(datastr);
  $("#model_material_code_edit_body").load(
    "module/ground_water_ex/material_code_edit_form.php",
    datastr
  );
}
//////////----Edit Form----/////////

//////////----Insert Data----/////////
function add_lithology_material_code() {
  var operation = "insert_lithology_material_code_data";
  var mat_code = $("#mat_code").val();
  var description = $("#des_cription").val();
  var lower_size = $("#low_size").val();
  var upper_size = $("#up_size").val();
  var yl = $("#yl_id").val();
  var ym = $("#ym_id").val();
  var yh = $("#yh_id").val();
  // var status = "A";

  if (validateInput(mat_code) == false) {
    alert("Please Insert The Material Code correctly");
    $("#mat_code").val("");
    $("#mat_code").focus();
    return false;
  } else if (validateInput(description) == false) {
    alert("Please Insert The Description correctly");
    $("#des_cription").val("");
    $("#des_cription").focus();
    return false;
  } else if (validateInput(lower_size) == false) {
    alert("Please Insert the Lower Size correctly");
    $("#low_size").val("");
    $("#low_size").focus();
    return false;
  } else if (validateInput(upper_size) == false) {
    alert("Please Insert The Upper Size correctly");
    $("#up_size").val("");
    $("#up_size").focus();
    return false;
  } else if (validateInput(yl) == false) {
    alert("Please Insert The YL correctly");
    $("#yl_id").val("");
    $("#yl_id").focus();
    return false;
  } else if (validateInput(ym) == false) {
    alert("Please Insert The YM correctly");
    $("#ym_id").val("");
    $("#ym_id").focus();
    return false;
  } else if (validateInput(yh) == false) {
    alert("Please Insert The YH correctly");
    $("#yh_id").val("");
    $("#yh_id").focus();
    return false;
  } else {
    // var datastr = "operation="+operation+"&mat_code="+mat_code+"&description="+description+
    // "&lower_size="+lower_size+"&upper_size="+upper_size+"&yl="+yl+"&ym="+ym+"&yh="+yh+"&status="+status;

    var datastr =
      "operation=" +
      operation +
      "&mat_code=" +
      mat_code +
      "&description=" +
      description +
      "&lower_size=" +
      lower_size +
      "&upper_size=" +
      upper_size +
      "&yl=" +
      yl +
      "&ym=" +
      ym +
      "&yh=" +
      yh;
  }

  //alert(datastr);
  //	$('#cover-spin').show(0);
  $.ajax({
    url: "module/ground_water_ex/lithology_material_code_ajax.php",
    datatype: "post",
    data: datastr,
    success: function (data) {
      //	$('#cover-spin').hide();
      // alert(data);
      if (data == 1) {
        alert("This material code already exists");
        $("#mat_code").focus();
        // $('#modal_lithology_material_code').modal('hide');
        // load();
      } else if (data == 2) {
        alert("Information has been added successfully");
        $("#modal_lithology_material_code").modal("hide");
        load();
      } else {
        alert("error occurred");
      }
    },
  });
}
//////////----Insert Data----/////////

//////////----Update Data----/////////
function update_lithology_material_code() {
  var operation = "update_lithology_material_code_data";
  var mat_code = $("#mat_code").val();
  var description = $("#des_cription").val();
  var lower_size = $("#low_size").val();
  var upper_size = $("#up_size").val();
  var yl = $("#yl_id").val();
  var ym = $("#ym_id").val();
  var yh = $("#yh_id").val();
  // var status = $('#status').val();

  if (validateInput(mat_code) == false) {
    alert("Please Insert The Material Code correctly");
    $("#mat_code").val("");
    $("#mat_code").focus();
    return false;
  } else if (validateInput(description) == false) {
    alert("Please Insert The Description correctly");
    $("#des_cription").val("");
    $("#des_cription").focus();
    return false;
  } else if (validateInput(lower_size) == false) {
    alert("Please Insert the Lower Size correctly");
    $("#low_size").val("");
    $("#low_size").focus();
    return false;
  } else if (validateInput(upper_size) == false) {
    alert("Please Insert The Upper Size correctly");
    $("#up_size").val("");
    $("#up_size").focus();
    return false;
  } else if (validateInput(yl) == false) {
    alert("Please Insert The YL correctly");
    $("#yl_id").val("");
    $("#yl_id").focus();
    return false;
  } else if (validateInput(ym) == false) {
    alert("Please Insert The YM correctly");
    $("#ym_id").val("");
    $("#ym_id").focus();
    return false;
  } else if (validateInput(yh) == false) {
    alert("Please Insert The YH correctly");
    $("#yh_id").val("");
    $("#yh_id").focus();
    return false;
  } else {
    // var datastr = "operation="+operation+"&mat_code="+mat_code+"&description="+description+
    // "&lower_size="+lower_size+"&upper_size="+upper_size+"&yl="+yl+"&ym="+ym+"&yh="+yh+"&status="+status;

    var datastr =
      "operation=" +
      operation +
      "&mat_code=" +
      mat_code +
      "&description=" +
      description +
      "&lower_size=" +
      lower_size +
      "&upper_size=" +
      upper_size +
      "&yl=" +
      yl +
      "&ym=" +
      ym +
      "&yh=" +
      yh;
  }

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

///// delete data///////
function load_material_code_delete(material_code) {
  // alert(material_code);
  // var row_id = row_id;
  // var well_id = $("#well_id").val();
  // var id = $("#id_" + row_id).val();
  var operation = "delete";
  var datastr = "material_code=" + material_code + "&operation=" + operation;
  // alert(datastr);
  if (confirm("Are you sure you want to delete the selected row?")) {
    $.ajax({
      url: "module/ground_water_ex/lithology_material_code_ajax.php",
      type: "post",
      data: datastr,
      success: function (str) {
        //alert(str);
        if (str == 1) {
          alert("Data has been deleted successfully");
					load();
        } else {
          alert("Fail to delete data");
        }
        
      },
    });
  }
}
////delete////