

	jQuery(document).ready(function() {
		
		FormValidator.init();
		
		//hide error box
		$('.alert-danger').hide();
	});
	
	jQuery(document).ready(function() {
		
		
		
		
	//conirm
	$( "#conirm" ).click(function() {
		var sel_id=$('#sel_id').val();
		var popup_type=$('#popup_type').val();
		var page=$('#page').val();
		var row_id=sel_id;

if(page=='sale_add'){
	if(popup_type=='delete'){
		var tmp='#row_'+row_id;
		$(tmp).remove();
		displayNotice('page','Product item has been deleted successfully!');
		calculateTotal(); 
		
	}
} //end page check
	
	});
	

});		

function deleteSalesItem(row_id){
	
	$("#myModal4").modal();
	$('#sel_id').val(row_id); 
	$('#popup_type').val('delete');
	$('#page').val('sale_add');
	$("#label").text("Are you sure you want to delete this product item?");
}

function clearForm(){
	
	$('#qtyTotal').text(convertToAmount(0));
	//('#sale_datetime').text('');
	$('#sale_datetime').css("borderColor","#d5d5d5");
	$('#warehouse_id').css("borderColor","#d5d5d5");
	$('#customer_id').css("borderColor","#d5d5d5");
	
	$('#balance_dis').text(convertToAmount(0));
	$('#Subtotal').text(convertToAmount(0));
	//set ref no
	getNextRefNo();
}




function addProductToListByID(rowcount,sale_id,sale_reference_no,sale_total,sale_pymnt_amount)
{

	/*var cash_collector_id=$('#cash_collector_id').val();
	error=false;

	if(cash_collector_id==''){
		error=true;	
	}
	if(error){
		 bootbox.alert('Please select Cash Collector before adding any product', function () {
            $('#add_item').focus();
         });
	}else {*/
	
	var rowCount=$('#rowCount').val();
	var nxtCount=parseInt(rowCount)+1;
	$('#rowCount').val(nxtCount);
	//alert(product_price);
	var rwcount=rowcount;
	var sales_id=sale_id;
	var sale_reference_no=String(sale_reference_no);
	var sale_total=sale_total;
	
	
	//window.alert(sale_reference_no);
	
$('#soTable tbody').prepend('<tr class="child" id="row_'+nxtCount+'"><td class="center">'+rwcount+'</td><td>'+sale_reference_no+'<input type="hidden" class="form-control text-center rquantity" name="row['+nxtCount+'][sale_id][]" value="'+sale_id+'" id="sales_id_'+nxtCount+'"></td><td class="text-right"><input type="hidden" name="row['+nxtCount+'][sale_total][]" id="sale_total_'+nxtCount+'" value="'+sale_total+'"><span>'+sale_total+'</span></td> <td class="text-right"><input type="hidden" name="row['+nxtCount+'][payment_total][]" id="payment_total'+nxtCount+'" value="'+sale_pymnt_amount+'"><span>'+sale_pymnt_amount+'</span></td><td class="text-right"><span class="text-right ssubtotal" id="subtotal_'+nxtCount+'">'+(sale_total-sale_pymnt_amount)+'</span>&nbsp;</td><td><a onclick="deleteSalesItem('+nxtCount+')"><i class="fa fa-times tip podel" id="1446800197032" title="Remove" style="cursor:pointer;"></i></a></td></tr>');
	
	
	calculateTotal();
//} //end error check
}

function changeQtyByProductID(qty,nxtCount)
{
	calculateTotal();
}

function changeDiscountByProductID(discount,nxtCount,product_id){
	calculateTotal();
}

function changePaidValue(paid){
	if(isNaN(paid)) {
		displayNotice('page','Invalid Paid Amount');
		
		
		var oldVal=$('#tmpVal').val();
		$('#paid').val(oldVal); //set last val
	}else{
		
		calculateTotal();
		$('#paid').val(convertToAmount(paid));
		
	}	
}



function setTmpVal(val){
	$('#tmpVal').val(val); 
}

function convertToAmount(val)
{
	var disval=val; //+'.00'; //.toFixed(val);
	return accounting.formatMoney(disval, "", 2, ",", "."); // €4.999,99 
}

function changeMainDiscount(val){
	
	calculateTotal();
}


	
	


/* end check is added produtct */

function calculateTotal(){
	
	
	//get row
	var rowCount=parseInt($('#rowCount').val());
	var subtotal=0;
	var subtotal_item=0;
	var balance=0;
	var paid=0;
	var total_amount=0;
	
	
	

	for(i=1; i<=rowCount; i++)
	{	
		var invoice_total_val=0;
	
		var invoice_total_fls='#sale_total_'+i;
		
		
		 invoice_total_val=parseFloat($(invoice_total_fls).val());		
		
		if(isNaN(invoice_total_val))
		{
			
		}
		
		else
		{
			total_amount=total_amount+invoice_total_val;
		}
	}

	//display val

	$('#Subtotal').text(convertToAmount(total_amount));
	
	
		
	//footer amount bar
	$('#titems').text(0);
	$('#gtotal').text(convertToAmount(total_amount));
	$('#tds').text(convertToAmount(0));
	$('#f_total').text(convertToAmount(total_amount));
	
	
	$('#sale_total').val((total_amount));
	
	//set cursser to update qty
	
	var tmp=$("#rowCount").val();
	var tmpFld="#quantity_"+tmp;
	$(tmpFld).focus();
	$(tmpFld).select();
	
}

for (var key in localStorage) {		
  //console.log(key + ':' + localStorage[key]);
}


$('#comment').keypress(function(event){

    if (event.keyCode == 10 || event.keyCode == 13) 
        event.preventDefault();

  });
  
  
//************** reset function *******

    $('#reset').click(function(e) {
        bootbox.confirm("Are you sure?", function(result) {
            if (result) {

               $('body').modalmanager('loading');
                location.reload();
            }
        });
    });


//************** end reset function ***
  