function training_details_save_data(){
    text_box_validation_training_details();
    const formdata = new FormData();
    formdata.append("training_name", $("#training_name").val());
    formdata.append("total_days", $("#total_days").val());
    formdata.append("total_modules", $("#total_modules").val());
    formdata.append("add_image", $("#add_image").val());
    formdata.append("total_marks", $("#total_marks").val());
    formdata.append("about_training", $("#about_training").val());
    formdata.append("segment_id", $('#segment_list option:selected').attr("nameid"));
    formdata.append("segment_name", $('#segment_list option:selected').attr("value"));
    $.ajax({
       type:'POST',
      // enctype: 'multipart/form-data',
       url:window.location.origin+"/trainingportal/Adddetails/save_training_details_data",
       data: formdata,
       contentType: false,
       processData: false,
       success:function(data){
        //success_alert
        if(data==1){
          //success_alert
         // $("#success_alert").val("Data save successfully");
          document.getElementById("success_alert").style.display = "block";
        }else{
        //  $("#error_alert").val("Data not save successfully");
          document.getElementById("error_alert").style.display = "block";
        }
       }
    });

}



function text_box_validation_training_details(){

    var isValid = true;
    $('#training_name,#total_days,#total_modules,#add_image,#total_marks,#about_training').each(function () {
        if ($.trim($(this).val()) == '') {
            isValid = false;
            $(this).css({
                "border": "1px solid red",
                "background": "#FFCECE"
            });
        }
        else {
            $(this).css({
                "border": "",
                "background": ""
            });
        }
    });
    if (isValid == false){
          return;
    }
        
}



function module_save_data(){
   var isvalidation= text_box_validation_module_save_data();
   if(!isvalidation){
       return;
   }
    var brands = $('#module_target option:selected');
    var selected = [];
    $(brands).each(function(index, brand){
        var index=index+1;
        selected.push(index+". "+[$(this).val()+" <br>"]);
    });
    var user_list = $('#module_user_list option:selected');
    var selected_trainers = [];
    var trainer_id=[];
    $(user_list).each(function(index, user_list){
        var index=index+1;
        selected_trainers.push(index+". "+[$(this).text()+" <br>"]);
        trainer_id.push($(this).val().trim());
    });

    var trainee_list = $('#module_trainees_list option:selected');
    var selected_trainee_target = [];
    var selected_trainee_target_ids=[];
    $(trainee_list).each(function(index, trainee_list){
        var index=index+1;
        selected_trainee_target.push(index+". "+[$(this).text()+" <br>"]);
        selected_trainee_target_ids.push($(this).val().trim());
    });

    var file_data = $("#module_image_path").prop("files")[0]; 
    const formdata = new FormData();
    formdata.append("module_name", $("#module_name").val());
    formdata.append("module_sort_index", $("#module_sort_index").val());
    formdata.append("about_module", $("#about_module").val());
    formdata.append("module_target",selected.toString().replace(/\,/g,''));
    formdata.append("module_trainer",selected_trainers.toString().replace(/\,/g,''));
    formdata.append("module_sort_index", $("#module_sort_index").val());
    formdata.append("training_id", $('#training_list option:selected').attr("nameid"));
    formdata.append("training_name", $('#training_list option:selected').attr("value"));
    formdata.append("segment_id", $('#module_segment_list option:selected').attr("nameid"));
    formdata.append("segment_name", $('#module_segment_list option:selected').attr("value"));
    formdata.append("trainer_id", trainer_id.toString());
    formdata.append("image_data", file_data);
    formdata.append("trainee_target_id", selected_trainee_target_ids.toString());
    formdata.append("trainee_target_name",selected_trainee_target.toString().replace(/\,/g,''));
    $.ajax({
       type:'POST',
      // enctype: 'multipart/form-data',
       url:window.location.origin+"/trainingportal/Adddetails/save_module_data",
       data: formdata,
       contentType: false,
       processData: false,
       success:function(data){
        if(data==1){
      
         document.getElementById("module_success_alert").style.display = "block";
        }else{
      
         document.getElementById("module_error_alert").style.display = "block";
        }
       }
    });

}

  
function text_box_validation_module_save_data(){

    var isValid = true;
    $('#module_name,#module_sort_index,#about_module').each(function () {
        if ($.trim($(this).val()) == '') {
            isValid = false;
            $(this).css({
                "border": "1px solid red",
                "background": "#FFCECE"
            });
        }
        else {
            $(this).css({
                "border": "",
                "background": ""
            });
        }
    });
    if (isValid == false){
          return ;
    }
return isValid;
        
}


function create_target_save_data(){
    var isvalidation= text_box_validation_create_target_save_data();
    if(!isvalidation){
        return;
    }
     const formdata = new FormData();
     formdata.append("target_name", $("#target_name").val());
     formdata.append("target_sort_index", $("#target_sort_index").val());
    //  formdata.append("module_id", $('#target_model_list option:selected').attr("nameid"));
    //  formdata.append("module_name", $('#target_model_list option:selected').attr("value"));
     formdata.append("segment_id", $('#target_segment_list option:selected').attr("nameid"));
     formdata.append("segment_name", $('#target_segment_list option:selected').attr("value"));
     formdata.append("training_id", $('#target_training_list option:selected').attr("nameid"));
     formdata.append("training_name", $('#target_training_list option:selected').attr("value"));
     $.ajax({
        type:'POST',
       // enctype: 'multipart/form-data',
        url:window.location.origin+"/trainingportal/Adddetails/save_target_data",
        data: formdata,
        contentType: false,
        processData: false,
        success:function(data){
          if(data==1){
           // $("#target_success_alert").val("Data save successfully");
            document.getElementById("target_success_alert").style.display = "block";
          }else{
          //  $("#target_error_alert").val("Data not save successfully");
            document.getElementById("target_error_alert").style.display = "block";
            
          }
          
        }
     });
 
 }

 function text_box_validation_create_target_save_data(){

    var isValid = true;
    $('#target_sort_index,#target_name').each(function () {
        if ($.trim($(this).val()) == '') {
            isValid = false;
            $(this).css({
                "border": "1px solid red",
                "background": "#FFCECE"
            });
        }
        else {
            $(this).css({
                "border": "",
                "background": ""
            });
        }
    });
    if (isValid == false){
          return ;
    }
return isValid;
        
}


function trainees_save_data(){
    var isvalidation= text_box_validation_trainees_save_data();
    if(!isvalidation){
        return;
    }
     const formdata = new FormData();
     formdata.append("trainees_name", $("#trainees_name").val());
     formdata.append("trainees_sort_index", $("#trainees_sort_index").val());
     formdata.append("segment_id", $('#trainees_segment_list option:selected').attr("nameid"));
     formdata.append("segment_name", $('#trainees_segment_list option:selected').attr("value"));
     $.ajax({
        type:'POST',
       // enctype: 'multipart/form-data',
        url:window.location.origin+"/trainingportal/Adddetails/save_trainees_data",
        data: formdata,
        contentType: false,
        processData: false,
        success:function(data){

        if(data==1){

            document.getElementById("trainees_success_alert").style.display = "block";
          }else{
            document.getElementById("trainees_error_alert").style.display = "block";
            
          }
        }
     });
 
 }

 function text_box_validation_trainees_save_data(){

    var isValid = true;
    $('#trainees_name,#trainees_sort_index').each(function () {
        if ($.trim($(this).val()) == '') {
            isValid = false;
            $(this).css({
                "border": "1px solid red",
                "background": "#FFCECE"
            });
        }
        else {
            $(this).css({
                "border": "",
                "background": ""
            });
        }
    });
    if (isValid == false){
          return ;
    }
return isValid;
        
}


//create_training_content
function save_training_content(){
    var isvalidation= text_box_validation_save_training_content_data();
    if(!isvalidation){
        return;
    }
    
     const formdata = new FormData();
     formdata.append("content_name", $("#content_name").val());
     formdata.append("content_sort_index", $("#content_sort_index").val());
     formdata.append("content_discription", $("#content_discription").val());

     formdata.append("training_id", $('#content_training_list option:selected').val());
     formdata.append("training_name", $('#content_training_list option:selected').text());
     formdata.append("model_id", $('#content_model_list option:selected').val());
     formdata.append("model_name", $('#content_model_list option:selected').text());
     formdata.append("segment_id", $('#content_segment_list option:selected').val());
     formdata.append("segment_name", $('#content_segment_list option:selected').text());
     $.ajax({
        type:'POST',
       // enctype: 'multipart/form-data',
        url:window.location.origin+"/trainingportal/Adddetails/save_training_content_data",
        data: formdata,
        contentType: false,
        processData: false,
        beforeSend: function() {
          document.getElementById("content_error_alert").style.display = "none";
          document.getElementById("content_success_alert").style.display ="none";
      },
        success:function(data){
          if(data==1){

            document.getElementById("content_success_alert").style.display = "block";
          }else{
            document.getElementById("content_error_alert").style.display = "block";
            
          }
        }
     });
 
 }

 function text_box_validation_save_training_content_data(){

    var isValid = true;
    $('#content_name,#content_sort_index,#content_discription').each(function () {
        if ($.trim($(this).val()) == '') {
            isValid = false;
            $(this).css({
                "border": "1px solid red",
                "background": "#FFCECE"
            });
        }
        else {
            $(this).css({
                "border": "",
                "background": ""
            });
        }
    });
    if (isValid == false){
          return ;
    }
return isValid;
        
}

function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("module_user_list");
    a = div.getElementsByTagName("option");
    for (i = 0; i < a.length; i++) {
      txtValue = a[i].textContent || a[i].innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        a[i].style.display = "";
      } else {
        a[i].style.display = "none";
      }
    }
  }

  function filterFunction_target() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput_target");
    filter = input.value.toUpperCase();
    div = document.getElementById("module_target");
    a = div.getElementsByTagName("option");
    for (i = 0; i < a.length; i++) {
      txtValue = a[i].textContent || a[i].innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        a[i].style.display = "";
      } else {
        a[i].style.display = "none";
      }
    }
  }

  
  function filterFunction_training() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput_training");
    filter = input.value.toUpperCase();
    div = document.getElementById("training_list");
    a = div.getElementsByTagName("option");
    for (i = 0; i < a.length; i++) {
      txtValue = a[i].textContent || a[i].innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        a[i].style.display = "";
      } else {
        a[i].style.display = "none";
      }
    }
  }


  

  function filterFunction_segment() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput_segment");
    filter = input.value.toUpperCase();
    div = document.getElementById("module_segment_list");
    a = div.getElementsByTagName("option");
    for (i = 0; i < a.length; i++) {
      txtValue = a[i].textContent || a[i].innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        a[i].style.display = "";
      } else {
        a[i].style.display = "none";
      }
    }
  }

  function filterFunctionTraineeCat() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInputTrainee");
    filter = input.value.toUpperCase();
    div = document.getElementById("module_trainees_list");
    a = div.getElementsByTagName("option");
    for (i = 0; i < a.length; i++) {
      txtValue = a[i].textContent || a[i].innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        a[i].style.display = "";
      } else {
        a[i].style.display = "none";
      }
    }
  }
  

  // function getselectedSegmentVal(segment_id){
    
  //   const formdata = new FormData();
  //   formdata.append("selected_segment_dropdown", segment_id);
  //   $.ajax({
  //      type:'POST',
  //     // enctype: 'multipart/form-data',
  //      url:window.location.origin+"/trainingportal/Adddetails/set_segmentid_for_dropdown",
  //      data: formdata,
  //      contentType: false,
  //      processData: false,
  //      success:function(data){
  //        console.log(data);
  //      }
  //   });
  // }