/**
 * Site : http:www.smarttutorials.net
 * @author muni
 */
	      
//adds extra table rows
var i=$('table tr').length;
$(".addmore").on('click',function(){
	html = '<tr>';
	html += '<td><input class="case" type="checkbox"/></td>';
	html += '<td><input type="text" data-type="name" name="perticulars[]" class="form-control" id="perticulars_'+i+'" class="form-control"></td>';
	html += '<td><input type="text" data-type="name" name="description[]" class="form-control" id="description_'+i+'" class="form-control"></td>';
	html += '<td><input type="text" name="amount[]" id="amount_'+i+'" class="form-control totalLinePrice"></td>';
	html += '</tr>';
	$('table').append(html);
	i++;
	calculateTotal();
});

//to check all checkboxes
$(document).on('change','#check_all',function(){
	$('input[class=case]:checkbox').prop("checked", $(this).is(':checked'));
});

//deletes the selected table rows
$(".delete").on('click', function() {
	$('.case:checkbox:checked').parents("tr").remove();
	$('#check_all').prop("checked", false); 
	calculateTotal();
});

//deletes the selected table rows
$(".subTotal").on('click', function() {
	
	calculateTotal();
});





//total price calculation 
function calculateTotal(){
	subTotal = 0 ; total = 0; 
	$('.totalLinePrice').each(function(){
		if($(this).val() != '' )subTotal += parseFloat( $(this).val() );
	});
	$('#subTotal').val( Math.round(subTotal.toFixed(2)) );
	$('.subTotal').val( subTotal );
	$('.amountDue').val( subTotal );
	}




//It restrict the non-numbers
var specialKeys = new Array();
specialKeys.push(8,46); //Backspace
function IsNumeric(e) {
    var keyCode = e.which ? e.which : e.keyCode;
    console.log( keyCode );
    var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
    return ret;
}

//datepicker
$(function () {
    $('#invoiceDate').datepicker({});
});