<script type="text/javascript">


    //This function read input for total cost
    function readTotalCost(){
        sum_value=0;
$('.bulkCost').each(function(){ 
    if($(this).val()!=''){
    sum_value =parseInt($(this).val())+sum_value;
    //console.log(sum_value);
    $('#price,#prod_costed').val(sum_value);

}
  })
    }
   
    $("#country").on('change', function () { //alert('Hello');
  var getId =  $(this).find('option:selected').attr('id');  getValue = $(this).val(); 
  
$.ajax({url: 'inline_specs', type: 'POST',data: {stateIdN: getId},success: function (result) {
    $('#state').html(result);
     // alert(result);
        }  });

})

//show Stock Count
    $('#showInput').hide();
$('#showStkBal').change(function(e){$.ajax({data: {get_sel_info:$(this).val()},type:"post",dataType:'html',url: "processor",success: function(data){  $('.show_my_stk_bal').html(data); $('.show_my_stk_bal').val(data);
if(data>0){ $('#showInput').show();}else{ $('#showInput').hide();}
},error: function(jqXHR, textStatus, errorThrown) { alert('Error');}
    });
})




    setwhere =1;

$('#getInfo').change(function(e){$.ajax({data: {getProductInfo:$(this).val()},type:"post",dataType:'html',url: "processor",success: function(data){  
// alert(data);

  $('#showProdInfo').html(data);
},error: function(jqXHR, textStatus, errorThrown) { alert('Error');}
    });
})

// if(remove_disabled_status!=1){$('[id^=orderCount]').prop('disabled','true');}



  var myArray = [];




$('[id^=orderProd]').change(function(e){ 
id = $(this).prop('id');
lcard=id.slice(9);
getValue =$('#'+id).val();




var value = $(this).val();


if(value==''){ myArray.splice(lcard,1);}
 if(!isNaN(getValue)){$('#orderCount'+lcard).val(1); 
 $('#orderCount'+lcard).removeAttr('disabled');
}



$.ajax({url:'inline_specs',type:'POST',data:{storedID:getValue,myprice:setwhere},success:function(retr){ 
/*****Handling the Non Duplicate Selection***/
if(myArray.includes(value,0)&& value!=''){ alert('Previously Selected'); }
 $('#wrapCost1').val(parseInt(retr)); $('#orderCost'+lcard).val(parseInt(retr));
myArray[lcard] = value;


/*********************************END OF DUPLICATE SELECTION************/
// console.log(myArray);
/*******************END**************************/


},error: function(jqXHR, textStatus, errorThrown) { 
$(function(){ setTimeout(function() { 
$.toast({title: 'An Error Occured',backgroundColor: 'gray',textColor: '#fff'});
}, 5000);})} });



       })














//     var myArray = [];
// // $('#orderCost1').val(20);

// $('[id^=orderProd]').change(function(e){ 
// id = $(this).prop('id');lcard=id.slice(9);getValue =$('#'+id).val();




// // console.log(lcard);
// var value = $(this).val();


// if(value==''){ myArray.splice(lcard,1);}





    
//  if(!isNaN(getValue)){$('#orderCount'+lcard).val(1); 
//  $('#orderCount'+lcard).removeAttr('disabled');
// }



// $.ajax({url:'inline_specs',type:'POST',data:{storedID:getValue,myprice:setwhere},success:function(retr){ 

// // alert(retr);
//     // alert('#orderCost'+lcard);

// /*****Handling the Non Duplicate Selection***/
// if(myArray.includes(value,0)&& value!=''){alert('Previously Selected'); 
// $('#orderProd'+lcard).val(null).trigger('change.select2');

// // alert('Okay');

// $('#orderCount'+lcard).val('');
// $('#orderCost'+lcard).val('');$('#wrapCost'+lcard).val('');
// }else if(value==''){ //alert('Empty Selected');
// $('#orderProd'+lcard).val(null).trigger('change.select2');; $('#orderCount'+lcard).val('');
// $('#orderCost'+lcard).val('');$('#wrapCost'+lcard).val('');
// }else{ //alert(retr); 

// $('#wrapCost'+lcard).val(parseInt(retr)); 
// $('#orderCost'+lcard).val(parseInt(retr));


// //When a Product is selected show the total Amount => 04/02/24
// readTotalCost();


// myArray[lcard] = value;
// }

// // console.log(myArray);
// /*******************END**************************/


// },error: function(jqXHR, textStatus, errorThrown) { 
// $(function(){ setTimeout(function() { 
// $.toast({title: 'An Error Occured',backgroundColor: 'gray',textColor: '#fff'});
// }, 5000);})} });

//        })














//keyup change click
$('[id^=orderCount]').bind('keyup change', function (e) {
id = $(this).prop('id'); lcard=id.slice(10);
$calcPrice = $('#wrapCost'+lcard).val();  getCount =$('#'+id).val();
if(getCount<1){ $(this).val(1); }else{
  $newCalc =$calcPrice*getCount;
$('#orderCost'+lcard).val($newCalc);

readTotalCost(); //04/02/24

}

  });





//if amount is changed
$('[id^=wrapCost]').bind('keyup change', function (e) {
id = $(this).prop('id'); lcard=id.slice(8); getVal=$(this).val();
$('#orderCost'+lcard).val(getVal); $('#orderCount'+lcard).val(1);

/***04/02/24 *****/
readTotalCost(); //04/02/24
sum_value=0;
$('.bulkCost').each(function(){ 
if($(this).val()!=''){sum_value =parseInt($(this).val())+sum_value;}
});
$('#price').val(sum_value);
/********************END UPDATE*************/

  });


// 

$('#btnShowR').click(function(){ $('.showForm').toggle();})



$('#btnShowR').click(function(){
  if ( $('.showForm').css('visibility') == 'hidden' )
    $('.showForm').css('visibility','visible');
  else
    $('.showForm').css('visibility','hidden');
});

/******************SHOW CUSTOMERS***************/
$('#btnShow').click(function(){ 
$.ajax({data: {loadCustomers:'trigger'},type: "post",url: "inline_specs",success: function(data){
 $('.showUsers').html(data);
},error: function(jqXHR, textStatus, errorThrown) { 
$(function(){ setTimeout(function() { 
$.toast({title: 'An Error Occured',backgroundColor: 'gray',textColor: '#fff'});
}, 5000);})}
        });
    })
/**************************************************/





$('[id^=blrBtn]').click(function(e){e.preventDefault();
  lead=$(this).prop('id').slice(6);
     $('#orderProd'+lead+' option').prop('selected', function() {
      $('#wrapCost'+lead).val(''); $('#orderCost'+lead).val('');
        return this.defaultSelected;
    });
    $('#orderProd'+lead).val(null).trigger('change.select2');; 
    $('#orderCount'+lead).val('');
    //PevCode - $('#orderCount'+lead).prop('disabled','true');
    myArray.splice(lead,1);
    console.log(myArray);
});




 $(".calcItem").click( function(e){e.preventDefault(); 
  $("#showTotal,#prod_costed,#price").val($.map($('input[id^="orderCost"]'), function (elem, i) {
        return parseInt(elem.value, 10) || 0;
    }).reduce(function (a, b) {
        return a + b;
    }, 0));
  $('#barOrder').prop('disabled',false);
 })


 //Send Request
$('#form4').submit(function(e){e.preventDefault(); 

$("#price").val($.map($('input[id^="orderCost"]'), function (elem, i) {
        return parseInt(elem.value, 10) || 0;
    }).reduce(function (a, b) {
        return a + b;
    }, 0));

var data = $(this).serialize();
$.ajax({data: data,type: "post",url: "reqOrder_1",success: function(data){ 
// alert(data);

if(data=='nil'){
swal({text:'Amount Exceed Bill',type:'warning',title:'Excess Payment'},function(){ 
    });     
}else{
swal({text:'Payment was successful',type:'success',title:'Approved'},function(){ 
    window.location = 'receeipts?no='+data;}); 
}
},error: function(jqXHR, textStatus, errorThrown) { 
$(function(){ setTimeout(function() { 
swal({text:'An Error Occurred',type:'warning',title:'Error'},function(){ window.location = 'payFirst';}); 
    alert('An error Occured');
}, 5000);})}
}); })








// if name is loaded
$('#name').change(function(e){ 
if(!isNaN($(this).val())){
$.ajax({url: 'inline_specs', type: 'POST',data: {sel_customer: $(this).val()},success: function (result) {
  manStr = result.split("-");
$('#eqSex option[value='+manStr[2]+']').attr('selected','selected');
$('#prog_type option[value='+manStr[4]+']').attr('selected','selected');
$('#usemails').val(manStr[3]);
$('#usphones').val(manStr[1]);
$('#myAddr').html(manStr[5]);
        }  });
}
})


$('[id^=paying]').bind('keyup change', function (e) { 
$num = $(this).attr('id').slice(6); rAmount = parseInt($('#debt'+$num).val());
$paying =parseInt($(this).val());
$('#bal'+$num).val(rAmount-$paying);
})




$('#username').keypress(function( e ) {
    if(e.which === 32) 
        return false;
});


//partners Sales 
$('#form5').submit(function(e){e.preventDefault(); 

$("#price").val($.map($('input[id^="orderCost"]'), function (elem, i) {
        return parseInt(elem.value, 10) || 0;
    }).reduce(function (a, b) {
        return a + b;
    }, 0));

var data = $(this).serialize();
$.ajax({data: data,type: "post",url: "out_partners_process",success: function(data){ 
// alert(data);

if(data=='nil'){
swal({text:'Amount Exceed Bill',type:'warning',title:'Excess Payment'},function(){ 
    });     
}else{
swal({text:'Payment was successful',type:'success',title:'Approved'},function(){ 
    window.location = 'out_sales_partners';}); 
}
},error: function(jqXHR, textStatus, errorThrown) { 
$(function(){ setTimeout(function() { 
swal({text:'An Error Occurred',type:'warning',title:'Error'},function(){ window.location = 'payFirst';}); 
    alert('An error Occured');
}, 5000);})}
}); })
</script>