var level_designation;
$( function() {
    $( ".datepicker" ).datepicker();
  } );

$(document).ready(function () {
    
    //Initialize tooltips
    $('.nav-tabs > li a[title]').tooltip();
    
    //Wizard
    $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {

        var $target = $(e.target);
    
        if ($target.parent().hasClass('disabled')) {
            return false;
        }
    });

    $(".next-step").click(function (e) {

      var form = $("#employee_form");
      form.validate({
        errorElement: 'span',
        errorClass: 'help-block',
        highlight: function(element, errorClass, validClass) {
          $(element).closest('.form-group').addClass("has-error");
        },
        unhighlight: function(element, errorClass, validClass) {
          $(element).closest('.form-group').removeClass("has-error");
        },
        rules: {
      
          name: {
            required: true,
            minlength: 5,
          },
          father_name: {
            required: true,
            minlength: 5,
          },
          mother_name: {
            required: true,
            minlength: 5,
          },
          gender: {
            required: true,
          },
          dob: {
            required: true,
          },
          home_district: {
            required: true,
          },
          home_block: {
            required: false,                
          },
          home_town: {
            required: true,
          },
		 home_gp: {
            required: false,
          },
          per_address: {
            required: true,
          },
          com_district: {
            required: true,
          },
          add_proof: {
            required: true,
          },
          email: {
            required: true,
            minlength: 5,
            email: true,
          },
          mobile: {
            required: true,
          },
          
          pan: {
            required: true,
          },
         
          adhar: {
            required: false,
          },
         
         
          emp_code: {
            required: true,                
          },
          doj: {
            required: true,
          },
          designation: {
            required: true,
          },
          salary: {
            required: true,
          },
          level: {
            required: true,
          },
          district: {
            required: true,
          },
          block: {
            required: true,
          },
         // panchyat: {
           // required: true,
          //},
          program: {
            required: true,                
          },
          contract_date: {
            required: true,
          },
        
          exp_one: {
            required: true,
          },
          // exp_two: {
          //   required: true,
          // },
          // exp_three: {
          //   required: true,
          // },
          // exp_four: {
          //   required: true,
          // },
          bank_name: {
            required: true,                
          },
          ifsc: {
            required: true,
          },
          account_no: {
            required: true,
          },
          
          hslc_board: {
            required: false,
          },
          hslc_pass_year: {
            required: false,
          },
          hslc_class: {
            required: false,
          },
          hslc_per: {
            required: false,
          },
          hslc_attch: {
            required: false,
          },
          hsslc_board: {
            required: false,                
          },
		   hsslc_stream: {
            required: false,
          },
          hsslc_pass_year: {
            required: false,
          },
          hsslc_class: {
            required: false,
          },
          hsslc_per: {
            required: false,
          },
          hsslc_attch: {
            required: false,
          },
          ug_course: {
            required: false,
          },
          ug_board: {
            required: false,                
          },
          ug_pass_year: {
            required: false,
          },
          ug_class: {
            required: false,
          },
          ug_per: {
            required: false,
          },
          ug_attch: {
            required: false,
          },
			
		  ot_course: {
            required: false,
          },
          ot_board: {
            required: false,                
          },
          ot_pass_year: {
            required: false,
          },
          ot_class: {
            required: false,
          },
          ot_per: {
            required: false,
          },
         
			
          pg_course: {
            required: false,
          },
          pg_board: {
            required: false,                
          },
          pg_pass_year: {
            required: false,
          },
          pg_class: {
            required: false,
          },
          pg_per: {
            required: false,
          },
          pg_attch: {
            required: false,
          },
          oth_course: {
            required: false,
          },
          oth_duration: {
            required: false,                
          },
          oth_inst_name: {
            required: false,
          },
          oth_pass_year: {
            required: false,
          },
          oth_class: {
            required: false,
          },
          oth_attch: {
            required: false,
          }
       },
        messages: {
         
          name: {
            required: "Name required",
          },
          email: {
            required: "Email required",

          },
        }
      });
      if (form.valid() === true){

        var $active = $('.wizard .nav-tabs li.active');
        $active.next().removeClass('disabled');
        nextTab($active);
      }

    });
    $(".prev-step").click(function (e) {

        var $active = $('.wizard .nav-tabs li.active');
        prevTab($active);

    });
});

function nextTab(elem) {
    $(elem).next().find('a[data-toggle="tab"]').click();
}
function prevTab(elem) {
    $(elem).prev().find('a[data-toggle="tab"]').click();
}

    $(document).on('change','#district',function(){
       $.get("get_blocks",{
            district:$(this).val()
        },function(j){
           var option = "<option>---Select Block---</option>";
           for(var i = 1; i < j.length; i++){
               option += "<option value="+j[i].block_id+">"+j[i].block_name+"</option>";
           }
           $('#block').html(option);
        },'JSON'); 
    });

    $(document).on('change','#home_district',function(){
       $.get("employee/get_blocks",{
            district:$(this).val()
        },function(j){
           var option = "<option>---Select Block---</option>";
           for(var i = 1; i < j.length; i++){
               option += "<option value="+j[i].block_id+">"+j[i].block_name+"</option>";
           }
           $('#home_block').html(option);
        },'JSON'); 
		 $.get("employee/get_grampanchyat2",{
            district:$(this).val()
        },function(j){
           var option = "<option value=''>---Select Gram Panchyat---</option>";
           for(var i = 1; i < j.length; i++){
               option += "<option value="+j[i].gram_panchyat_id+">"+j[i].gram_panchyat_name+"</option>";
           }
           $('#home_gp').html(option);
        },'JSON');
    });

   

   $(document).on('change','#block',function(){
       $.get("get_grampanchyat",{
            block:$(this).val()
        },function(j){
           var option = "<option value=''>---Select Grampanchyat---</option>";
           for(var i = 1; i < j.length; i++){
               option += "<option value="+j[i].gram_panchyat_id+">"+j[i].gram_panchyat_name+"</option>";
           }
           $('#panchyat').html(option);
        },'JSON'); 
    });



  $(document).on('change','#level',function(){
       $.get("employee/get_designations",{
            program:$(this).val()
        },function(j){
           var option = "<option value=''>---Select Designations---</option>";
           for(var i = 1; i < j.length; i++){
               option += "<option value="+j[i].designation_id+">"+j[i].designation_name+"</option>";
           }
           $('#designation').html(option);
        },'JSON'); 
    });

  $(document).on('change','#designation',function(){
      $.get("employee/get_salaries",{
             designation:$(this).val()
          },function(j){
           var sal = 0;
        //    // var option = "<option value=''>---Select Designations---</option>";
           for(var i = 0; i < j.length; i++){
               sal=j[i].salary;
           }
            
            $('#salary').val(sal);
        },'JSON'); 
      
     });

$(function(){
    $('#same').click(function(){
      $('#com_address').val($('#per_address').val());
    });
});

$(function(){
    $('#level').click(function(){
        if($("#level").val()==1)
        {
            $('#district').prop('disabled', true);
            $('#block').prop('disabled', true);
            $('#panchyat').prop('disabled', true);
        }
        else if($("#level").val()==2)
        {
            $('#district').prop('disabled', false);
            $('#block').prop('disabled', true);
            $('#panchyat').prop('disabled', true);
        }
        else if($("#level").val()==3)
        {
            level_designation=3;
            $('#district').prop('disabled', false);
            $('#block').prop('disabled', false);
            $('#panchyat').prop('disabled', false);
        }
        else
        {
            $('#district').prop('disabled', false);
            $('#block').prop('disabled', false);
            $('#panchyat').prop('disabled', false);
        }
    });
});

$(function(){
    $('#designation').click(function(){
        if($("#designation").val()==35 && $("#level").val()==3)
        {
            $('#district').prop('disabled', false);
            $('#block').prop('disabled', false);
            $('#panchyat').prop('disabled', true);
        }else if($("#level").val()==3){
			$('#panchyat').prop('disabled', false);
		}
		
    });
});