$(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) {
	// var regex = /(\b(on\w+)=|javascript:|(<\s*)(\/*)script)/i;
	if (input=='') {
	  return false;
	}
}
/////////--Validation--/////////

function add_lithology_color(){
	$("#add_lithology_color").load("module/ground_water_ex/add_lithology_color_modal.php");
}

function load(){
	$('#cover-spin').show(0);
    $.ajax({
        url:"module/ground_water_ex/litho_color_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_litho_color_vw_modal(col_code){
	var datastr = "col_code="+col_code;
	//alert(datastr);
	$("#model_litho_color_vw_body").load("module/ground_water_ex/lithology_color_details_vw.php",datastr);
}
//////////----View Form----/////////

//////////----Edit Form----/////////
function load_litho_color_edit_modal(col_code){
	var datastr = "col_code="+col_code;
	//alert(datastr);
	$("#model_lithology_color_edit_body").load("module/ground_water_ex/lithology_color_edit_form.php",datastr);
}
//////////----Edit Form----/////////

//////////----Insert Data----/////////
function add_lithology_color_data(){
	var operation =  "insert_lithology_color_data";
	var color_code =  $('#color_code').val();
	var color =  $('#color_id').val();
	// var status = "A";

	if(validateInput(color_code) == false){
		alert('Please Insert The Color Code correctly');
		$('#color_code').val('');
		$('#color_code').focus();
        return false;
	}
	else if(validateInput(color) == false){
		alert('Please Insert The Color correctly');
		$('#color_id').val('');
		$('#color_id').focus();
        return false;
	}
	else{
		// var datastr = "operation="+operation+"&color_code="+color_code+"&color="+color+"&status="+status;
		var datastr = "operation="+operation+"&color_code="+color_code+"&color="+color;
	}

	// alert(datastr);
//	$('#cover-spin').show(0);
    $.ajax({
        url:"module/ground_water_ex/lithology_color_ajax.php",
        datatype:'post',
		data: datastr,
        success:function(data){
		//	$('#cover-spin').hide();
			// alert(data);
			if(data==1){
				alert('This color code already exists');
				$('#color_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_color_data(){
	var operation =  "update_lithology_color_data_ajax";
	var color_code =  $('#col_code').val();
	var color =  $('#col_id').val();
	// var status = $('#status').val();

	if(validateInput(color_code) == false){
		alert('Please Insert The Color Code correctly');
		$('#col_code').val('');
		$('#col_code').focus();
        return false;
	}
	if(validateInput(color) == false){
		alert('Please Insert The Color correctly');
		$('#col_id').val('');
		$('#col_id').focus();
        return false;
	}
	else{
		// var datastr = "operation="+operation+"&color_code="+color_code+"&color="+color+"&status="+status;
		var datastr = "operation="+operation+"&color_code="+color_code+"&color="+color;
	}
	
	// alert(datastr);
	//$('#cover-spin').show(0);
    $.ajax({
        url:"module/ground_water_ex/lithology_color_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_litho_color_delete(color_code) {
  // alert(color_code);
  // var row_id = row_id;
  // var well_id = $("#well_id").val();
  // var id = $("#id_" + row_id).val();
  var operation = "delete";
  var datastr = "color_code=" + color_code + "&operation=" + operation;
  // alert(datastr);
  if (confirm("Are you sure you want to delete the selected row?")) {
    $.ajax({
      url: "module/ground_water_ex/lithology_color_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////