 $(document).ready(function(){
     $(document).on('click', '#checkAll', function() {
		$(".itemRow").prop("checked", this.checked);
	});	
	$(document).on('click', '.itemRow', function() {
		if ($('.itemRow:checked').length == $('.itemRow').length) {
			$('#checkAll').prop('checked', true);
		} else {
			$('#checkAll').prop('checked', false);
		}
	});

	var count = $(".itemRow").length;

	$(document).on('click', '#addRows1', function() {
     //$('#addRows1').on('click', function () {
        alert('ready');
		/*count++;
		var htmlRows = '';
		htmlRows += '<tr>';
		htmlRows += '<td><input class="itemRow" type="checkbox"></td>';          
		htmlRows += '<td><div class="input-group date datetimepicker1"><input type="text" name="rcvd_date[]" id="rcvd_date_'+count+'" class="form-control" autocomplete="off">' +
        '<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span></div></td>';
		htmlRows += '<td><input type="text" name="paymode[]" id="paymode_'+count+'" class="form-control" autocomplete="off"></td>';
		htmlRows += '<td><input type="number" name="amount[]" id="amount_'+count+'" class="form-control amount" autocomplete="off"></td>';
		//htmlRows += '<td><input type="number" name="price[]" id="price_'+count+'" class="form-control price" autocomplete="off"></td>';
		//htmlRows += '<td><input type="number" name="total[]" id="total_'+count+'" class="form-control total" autocomplete="off"></td>';
		htmlRows += '</tr>';
		$('#invoiceItem').append(htmlRows);*/
	});

	$(document).on('click', '#removeRows', function(){
        //alert('ready');
		$(".itemRow:checked").each(function() {
			$(this).closest('tr').remove();
		});
		$('#checkAll').prop('checked', false);
		calculateTotal();
	});		

	var count = $(".itemRow").length;
	$(document).on('click', '#addItemRows', function() {
        alert('ready');
		/*count++;
		var htmlRows = '';
		htmlRows += '<tr>';
		htmlRows += '<td><input class="itemRow" type="checkbox"></td>';                    
		htmlRows += '<td><input type="text" name="productName[]" id="productName_'+count+'" class="form-control" autocomplete="off"></td>';
		htmlRows += '<td><input type="number" name="price[]" id="price_'+count+'" class="form-control price" autocomplete="off"></td>';
		htmlRows += '</tr>';
		$('#insertItem').append(htmlRows);*/
	}); 
	
	/*$(document).on('blur', "[id^=quantity_]", function(){
		calculateTotal();
	});	
	$(document).on('blur', "[id^=price_]", function(){
		calculateTotal();
	});	
	$(document).on('blur', "[id^=productCode_]", function(){
		fillup();
	});
	$(document).on('blur', "#taxRate", function(){		
		calculateTotal();
	});	
	$(document).on('blur', "#amountPaid", function(){
		var amountPaid = $(this).val();
		var totalAftertax = $('#totalAftertax').val();	
		if(amountPaid && totalAftertax) {
			totalAftertax = totalAftertax-amountPaid;			
			$('#amountDue').val(totalAftertax);
		} else {
			$('#amountDue').val(totalAftertax);
		}	
	});*/

	/*$(document).on('click', '.deleteCustomer', function(){
		var id = $(this).attr("id");
		if(confirm("Are you sure you want to remove this?")){
			$.ajax({
                url:"{{ route('')}}",
				method:"POST",
				dataType: "json",
				data:{id:id, action:'delete_customer'},				
				success:function(response) {
					if(response.status == 1) {
						$('#'+id).closest("tr").remove();
					}
				}
			});
		} else {
			return false;
		}
	});*/

	/*$(document).on('click', '.deleteInvoice', function(){
		var id = $(this).attr("id");
		if(confirm("Are you sure you want to remove this?")){
			$.ajax({
				url:"{{ route('')}}",
				method:"POST",
				dataType: "json",
				data:{id:id, action:'delete_invoice'},				
				success:function(response) {
					if(response.status == 1) {
						$('#'+id).closest("tr").remove();
					}
				}
			});
		} else {
			return false;
		}
	});*/
});	

function calculateTotal(){
	var totalAmount = 0; 
	$("[id^='price_']").each(function() {
		var id = $(this).attr('id');
		id = id.replace("price_",'');
		var price = $('#price_'+id).val();
		var quantity  = $('#quantity_'+id).val();
		if(!quantity) {
			quantity = 1;
		}
		var total = price*quantity;
		$('#total_'+id).val(parseFloat(total));
		totalAmount += total;			
	});
	$('#subTotal').val(parseFloat(totalAmount));	
	var taxRate = $("#taxRate").val();
	var subTotal = $('#subTotal').val();	
	if(subTotal) {
		var taxAmount = subTotal*taxRate/100;
		$('#taxAmount').val(taxAmount);
		subTotal = parseFloat(subTotal)+parseFloat(taxAmount);
		$('#totalAftertax').val(subTotal);		
		var amountPaid = $('#amountPaid').val();
		var totalAftertax = $('#totalAftertax').val();	
		if(amountPaid && totalAftertax) {
			totalAftertax = totalAftertax-amountPaid;			
			$('#amountDue').val(totalAftertax);
		} else {		
			$('#amountDue').val(subTotal);
		}
	}
}

function fillup() {
	/*$("[id^='rcvd_date_']").each(function() {
		var id = $(this).attr('id');
		id = id.replace("rcvd_date_",'');
		var code = $('#rcvd_date_'+id).val();
		//alert(code);
		$.ajax({		
			url:"action.php",
			method:"POST",
			dataType: "json",
			data:{id:code, action:'fill_fields'},				
			success:function(response) {
					$('#productName_'+id).val(response.name);
					$('#price_'+id).val(response.price);
			}
		});	
	});*/
}



 