<script>
$(document).ready(function(){
        $('#vw_length').keyup(calculate);
        $('#vw_breadth').keyup(calculate);
        $('#vw_height').keyup(calculate);
        $('#quantity').keyup(calculate);
        $('#a_weight').keyup(type);
        
        $('#vw_length').change(calculate);
        $('#quantity').change(calculate);
        $('#vw_breadth').change(calculate);
        $('#vw_height').change(calculate);
        $('#a_weight').change(type);
   
    function get_availability()
    {
            var pincode_mode=$('#pincode_mode').val();
             var pincode_type=$('input[name="pincode_type"]:checked').val();
             var pincode=$('#consignee_pincode').val();
            var tot_length= $("#consignee_pincode").val().replace(/ /g,'').length;
           // alert(pincode);
             if(tot_length>5){
              $.ajax({
                  type:'POST',
                  url:'get_oda_pincode.php',
                  data:{pincode_mode:pincode_mode,pincode_type:pincode_type,pincode:pincode},
                  success:function(response){
                      alert(response);
                      if(response==1)
                      {    
                        //   $('#form_submit').prop('disabled', false);
                          $('#availability').css('color', 'green');
                          $('#availability').html('Service Available !');
                      }
                      else if(response==0)
                      {
                        //   $('#form_submit').prop('disabled', true);
                          $('#availability').css('color', 'red');
                          $('#availability').html('Service Not Available For Your Pincode !');
                  }
                  }
                });
              }
            }   

   
    function calculate()
    {
        var qty=$('#quantity').val();
        var v_weight=parseFloat(($('#vw_length').val() * $('#vw_breadth').val() * $('#vw_height').val())/(5000*qty));
        v_weight=v_weight.toFixed(3);
        $('#v_weight').val(v_weight);
        type();
    }
    
    function type(){
        var act_weight=$('#a_weight').val();
        var vol_weight=$('#v_weight').val();
        if(Number(act_weight)>Number(vol_weight))
        {
            $('#c_weight').val(act_weight);
        }
        else
        {
           $('#c_weight').val(vol_weight); 
        }
    }
    
   
                function cal_gst()
                {   
                     var taxable=$('#taxable_value').val();
                     var gst_type=$('#gst_rate').val();
                     var igst=$('#igst').val();
                     var cgst=$('#cgst').val();
                     var sgst=$('#sgst').val();
                     if(gst_type=='gr_igst')
                     {
                        var gst_amount=(taxable*igst)/100;
                     }
                     else if(gst_type=='gr_cgst')
                     {
                         var gst=parseFloat(cgst)+parseFloat(sgst);
                         var gst_amount=(taxable*gst)/100;
                     }
                     $('#gst_amount').val(gst_amount);
                     var invoice=0;
                     invoice=parseFloat(taxable)+parseFloat(gst_amount);
                     $('#invoice_value').val(invoice);
                }
});    
$(document).ready(function(){
    $("#txt_hsncode").keyup(function(){
        var search = $(this).val();
        if(search != ""){
            $.ajax({
                url: 'getsearch_hsncode.php',
                type: 'post',
                data: {search:search},
                dataType: 'json',
                success:function(response){
                    var len = response.length;
                    $("#searchResult").empty();
                    for( var i = 0; i<len; i++){
                        var id = response[i]['id'];
                        var hsncode = response[i]['hsncode'];
                        $("#searchResult").append("<li value='"+hsncode+"'>"+hsncode+"</li>");
                         }                    
                    // binding click event to li
                    $("#searchResult li").bind("click",function(){
                        setText(this);
                    });
                }
            });
        }
    });

function setText(element){
    var value = $(element).text();
    var userid = $(element).val();
    $("#txt_hsncode").val(value);
    $("#hsn_code").val(userid);
    $("#searchResult").empty(); 
      if(userid != ""){
            $.ajax({
                url: 'getsearch_hsncode.php',
                type: 'post',
                data: {search:userid},
                dataType: 'json',
                success:function(response){
                    var len = response.length;
                    for( var i = 0; i<len; i++){
                        var id = response[i]['id'];
                        var hsncode = response[i]['hsncode'];
                        var igst = response[i]['igst'];
                        var cgst = response[i]['cgst'];
                        var sgst = response[i]['sgst'];
                        
                         $("#igst").val(igst);
                         $("#cgst").val(cgst);
                         $("#sgst").val(sgst);
                    }                    
                }
            });
         }
      }
     
  /***********    For fetching customer address and phone number ***********/
         $('#order_type').on('change',function(){
             var type=$(this).val();
             if(type=='prepaid')
             {
                 $('#collectable_amount').val('0');
                 $('#collectable').hide();
             }
             else
             {
                 $('#collectable_amount').val('');
                 $('#collectable').show();
             }
         });
        
        
         /***********    For fetching gst number ***********/
         $('#client_id').on('change',function(){
           var client_id=$(this).val();
        //   alert(client_id);
           if(client_id!=''){
                $.ajax({
                  type:'POST',
                  url:'get_client_data.php',
                  data:{id:client_id},
                   dataType: 'json',
                  success:function(response){
                      var len = response.length;
                      $('#shipper_name').val(response[0]['full_name']);
                      $('#rto_name').val(response[0]['full_name']);
                      
                      $('#shipper_address').val(response[0]['address']);
                      $('#rto_address').val(response[0]['address']);
                      
                      $('#shipper_city').val(response[0]['city']);
                      $('#rto_city').val(response[0]['city']);
                      
                      $('#shipper_state').val(response[0]['state']);
                      $('#rto_state').val(response[0]['state']);
                      
                      $('#shipper_phone').val(response[0]['phone']);
                      $('#rto_phone').val(response[0]['phone']);
                      
                      $('#shipper_pincode').val(response[0]['pincode']);
                      $('#rto_pincode').val(response[0]['pincode']);
                      
                  }
               });
           }
       });
       /***********    For checking Unique 3PLAWB number ***********/
          $("#3pl_awb").keyup(function(){
              var awb=$(this).val();
              var courier_id=$('#courier_id').val();
              if(awb!=''){
              $.ajax({
                  type:'POST',
                  url:'get_unique_awb.php',
                  data:{awb:awb,type:'pl3_awb',courier_id:courier_id},
                   dataType: 'json',
                  success:function(response){
                      if(response[0]['error']=='0')
                      {
                          $("#ncheck1").css('color','green'); 
                         $("#ncheck1").text(response[0]['txt']); 
                      }
                      else
                      {
                          $("#ncheck1").css('color','red'); 
                         $("#ncheck1").text(response[0]['txt']); 
                      }
                  }
                });
              }
            });
              /***********    For checking Unique 3PLAWB number ***********/
          $("#courier_id").change(function(){
              var awb=$('#3pl_awb').val();
              var courier_id=$('#courier_id').val();
              if(awb!=''){
              $.ajax({
                  type:'POST',
                  url:'get_unique_awb.php',
                  data:{awb:awb,type:'pl3_awb',courier_id:courier_id},
                   dataType: 'json',
                  success:function(response){
                      if(response[0]['error']=='0')
                      {
                          $("#ncheck1").css('color','green'); 
                         $("#ncheck1").text(response[0]['txt']); 
                      }
                      else
                      {
                          $("#ncheck1").css('color','red'); 
                         $("#ncheck1").text(response[0]['txt']); 
                      }
                  }
                });
              }
            });
             /***********    For checking Unique CSCS AWB number ***********/
          $("#cscs_awb").keyup(function(){
              var awb=$(this).val();
              $.ajax({
                  type:'POST',
                  url:'get_unique_awb.php',
                  data:{awb:awb,type:'cscs_awb'},
                   dataType: 'json',
                  success:function(response){
                      if(response[0]['error']=='0')
                      {
                          $("#ncheck").css('color','green'); 
                         $("#ncheck").text(response[0]['txt']); 
                      }
                      else
                      {
                          $("#ncheck").css('color','red'); 
                         $("#ncheck").text(response[0]['txt']); 
                      }
                  }
                });
            });
            /***********    For checking Unique REF Number ***********/
          $("#ref_no").keyup(function(){
              var awb=$(this).val();
              $.ajax({
                  type:'POST',
                  url:'get_unique_awb.php',
                  data:{awb:awb,type:'ref_no'},
                   dataType: 'json',
                  success:function(response){
                      if(response[0]['error']=='0')
                      {
                          $("#ncheck2").css('color','green'); 
                         $("#ncheck2").text(response[0]['txt']); 
                      }
                      else
                      {
                          $("#ncheck2").css('color','red'); 
                         $("#ncheck2").text(response[0]['txt']); 
                      }
                  }
                });
            });
        
     $('#form_submit').on('click',function(e){
                    e.preventDefault();
                    var valid=1;
                     if( $('#client_id').val()=='none' ) {
                         valid=0;
                          $('#client_id_txt').text('Please select the Client');
                          $('#client_id_txt').css('color','red');
                     }
                     else{  $('#client_id_txt').text('');  }
                     if( $('#ref_no').val()=='' ) {
                         valid=0;
                          $('#ncheck2').text('Please enter the reference number');
                          $('#ncheck2').css('color','red');
                     }
                     else{  $('#ncheck2').text('');  }
                     if( $('#shipper_name').val()=='' ) {
                         valid=0;
                          $('#shipper_name_txt').text('Please enter all the shipper details');
                          $('#shipper_name_txt').css('color','red');
                     }
                     else{  $('#shipper_name_txt').text('');  }
                     if( $('#shipper_address').val()=='' ) {
                         valid=0;
                          $('#shipper_address_txt').text('Please select the shipper address');
                          $('#shipper_address_txt').css('color','red');
                     }
                     else{  $('#shipper_address_txt').text('');  }
                     if( $('#shipper_city').val()=='' ) {
                         valid=0;
                          $('#shipper_city_txt').text('Please select the shipper city');
                          $('#shipper_city_txt').css('color','red');
                     }else{  $('#shipper_city_txt').text('');  }
                     if( $('#shipper_state').val()=='' ) {
                         valid=0;
                          $('#shipper_state_txt').text('Please select the shipper state');
                          $('#shipper_state_txt').css('color','red');
                     }
                     else{  $('#shipper_state_txt').text('');  }
                     if( $('#shipper_pincode').val()=='' ) {
                         valid=0;
                          $('#shipper_pincode_txt').text('Please select the shipper pincode');
                          $('#shipper_pincode_txt').css('color','red');
                     }
                     else{  $('#shipper_pincode_txt').text('');  }
                     if( $('#shipper_phone').val()=='' ) {
                         valid=0;
                          $('#shipper_phone_txt').text('Please select the shipper phone');
                          $('#shipper_phone_txt').css('color','red');
                     }
                     else{  $('#shipper_phone_txt').text('');  }
                     if( $('#consignee_name').val()=='' ) {
                         valid=0;
                          $('#consignee_name_txt').text('Please enter the consignee name');
                          $('#consignee_name_txt').css('color','red');
                     }
                     else{  $('#consignee_name_txt').text('');  }
                     if( $('#consignee_address').val()=='' ) {
                         valid=0;
                          $('#consignee_address_txt').text('Please select the consignee address');
                          $('#consignee_address_txt').css('color','red');
                     }
                     else{  $('#consignee_address_txt').text('');  }
                     if( $('#consignee_city').val()=='' ) {
                         valid=0;
                          $('#consignee_city_txt').text('Please select the consignee city');
                          $('#consignee_city_txt').css('color','red');
                     }
                     else{  $('#consignee_city_txt').text('');  }
                     if( $('#consignee_state').val()=='' ) {
                         valid=0;
                          $('#consignee_state_txt').text('Please select the consignee state');
                          $('#consignee_state_txt').css('color','red');
                     }
                     else{  $('#consignee_state_txt').text('');  }
                     if( $('#consignee_pincode').val()=='' ) {
                         valid=0;
                          $('#availability').text('Please select the consignee pincode');
                          $('#availability').css('color','red');
                     }
                     else{  $('#availability').text('');  }
                     if( $('#consignee_phone').val()=='' ) {
                         valid=0;
                          $('#consignee_phone_txt').text('Please select the consignee phone');
                          $('#consignee_phone_txt').css('color','red');
                     }
                     else{  $('#consignee_phone_txt').text('');  }
                     if( $('#boxes').val()=='' ) {
                         valid=0;
                          $('#boxes_txt').text('Please enter the no.of boxes');
                          $('#boxes_txt').css('color','red');
                     }
                     else{  $('#boxes_txt').text('');  }
                     if( $('#c_weight').val()=='' || ('#a_weight').val()=='' || ('#v_weight').val()=='') {
                         valid=0;
                          $('#weight_txt').text('Please enter any one weight');
                          $('#weight_txt').css('color','red');
                     }
                     else{  $('#weight_txt').text('');  }
                     if( $('#taxable_value').val()=='' ) {
                         valid=0;
                          $('#taxable_value_txt').text('Please enter the taxable value');
                          $('#taxable_value_txt').css('color','red');
                     }
                     else{  $('#taxable_value_txt').text('');  }
                     if( $('#hsn_code').val()=='' ) {
                         valid=0;
                          $('#hsn_code_txt').text('Please enter the HSN Code');
                          $('#hsn_code_txt').css('color','red');
                     }
                     else{  $('#hsn_code_txt').text('');  }
                     if( $('#gst_amount').val()=='' ) {
                         valid=0;
                          $('#gst_amount_txt').text('Please enter the GST Amount');
                          $('#gst_amount_txt').css('color','red');
                     }
                     else{  $('#gst_amount_txt').text('');  }
                     if( $('#gst_rate').val()=='none' ) {
                         valid=0;
                          $('#gst_rate_txt').text('Please select the GST type');
                          $('#gst_rate_txt').css('color','red');
                     }
                     else{  $('#gst_rate_txt').text('');  }
                      if( $('#invoice_value').val()=='' ) {
                         valid=0;
                          $('#invoice_value_txt').text('Please select the GST type');
                          $('#invoice_value_txt').css('color','red');
                     }
                     else{  $('#invoice_value_txt').text('');  }
                     
                     if(valid)
                     {
                          $('#form_submit').removeClass('btn-danger');
                         $('#form_submit').addClass('btn-success');
                         $('#submit_txt').text('Wait');
                         
                       $.ajax({
        					type: 'POST',
        					url: 'pk_add_ajax.php',
        					data: $("form").serialize(),
        					 beforeSend: function() {
                                // setting a timeout
                                $('#submit_spin').addClass('fa fa-spinner fa-spin');
                            },
        					dataType: 'json',
        					success: function(response) {
        					    var res_text = response[0]['txt'];
        						if(response[0]['error'] == '0') {
        						    $('#form_submit').removeClass('btn-danger');
                                    $('#form_submit').addClass('btn-success');
        							$('#submit_txt').val(res_text);
        							$("#submit_spin").removeClass("fa fa-spinner fa-spin");
        						} else {
        						     $('#form_submit').removeClass('btn-success');
                                    $('#form_submit').addClass('btn-danger');
        						    $('#submit_txt').val(res_text);
        							$("#submit_spin").removeClass("fa fa-spinner fa-spin");
        						}
        					}
        				});
                     }
                     else
                     {
                         window.scrollTo(0,0);
                         $('#form_submit').removeClass('btn-success');
                         $('#form_submit').addClass('btn-danger');
                         $('#submit_txt').text('Please Fill All The Inputs');
                     }
                });        
            });
 </script>