

//autocomplete script
$(document).on('focus','.autocomplete_txt',function(){
	type = $(this).data('type');
	
	if(type =='productcode' )autoTypeNo=0;
	if(type =='productname' )autoTypeNo=1; 	
	
	$(this).autocomplete({
		source: function( request, response ) {
			$.ajax({
				url : 'ajax.php',
				dataType: "json",
				method: 'post',
				data: {
				   name_startsWith: request.term,
				   type: type
				},
				 success: function( data ) {
					 response( $.map( data, function( item ) {
					 	var code = item.split("|");
						return {
							label: code[autoTypeNo],
							value: code[autoTypeNo],
							data : item
						}
					}));
				}
			});
		},
		autoFocus: true,	      	
		minLength: 0,
		select: function( event, ui ) {
			var names = ui.item.data.split("|");						
			id_arr = $(this).attr('id');
	  		id = id_arr.split("_");
			$('#itemNo_'+id[1]).val(names[0]);
			$('#itemName_'+id[1]).val(names[1]);
            $('#type_'+id[1]).val(names[3]);
            $('#pprice_'+id[1]).val(names[4]);
			$('#quantity_'+id[1]).val(1);
			$('#price_'+id[1]).val(0);
			$('#total_'+id[1]).val(0);
			calculateTotal();
		}		      	
	});
});

//price change
$(document).on('change keyup blur','.changesNo',function(){
	id_arr = $(this).attr('id');
	id = id_arr.split("_");
	quantity = $('#quantity_'+id[1]).val();
	price = $('#price_'+id[1]).val();
    edty = $('#eduty_'+id[1]).val();
    discount = $('#discount_'+id[1]).val();
    tax = $('#tax_'+id[1]).val();
    caltot=parseFloat(price)*parseFloat(quantity)+parseFloat(edty);
    calc=caltot-(caltot*parseFloat(discount)/100);
	if( quantity!='' && price !='' ) $('#total_'+id[1]).val( (parseFloat(calc)+(parseFloat(calc)*parseFloat(tax)/100)).toFixed(2) );	
	calculateTotal();
});