

	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){
	//alert(row_id);
	//$("#myModal4").modal();
	$('#sel_id').val(row_id); 
	$('#popup_type').val('delete');
	$('#page').val('sale_add');
	var tmp='#row_'+row_id;
		$(tmp).remove();
		calculateTotal(); 
//	$("#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");
	//$('#tax_rate_id').css("borderColor","#d5d5d5");
	//$('#warehouse_id').val("");
	//$('#customer_id').val("");

	$('#balance_dis').text(convertToAmount(0));
	$('#Subtotal').text(convertToAmount(0));
	//set ref no
	//getNextRefNo();
}




function addProductToListByID(product_id,product_name,product_code,product_price,product_part_no,product_oem_part_number,item_cost){
	//alert(product_id);
	var customer_id=$('#customer_id').val();
	error=false;
	
	if(customer_id==''){
		error=true;	
	}
	if(error){
		 bootbox.alert('Please select Customer 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 product_price=product_price;
	var product_price_dis=convertToAmount(product_price);
	var sub_total_item=convertToAmount(product_price*1); //when add qty=1
	var discount_val=0;
	var discount_val_tot=0;
	var product_part_no_txt='';
	var product_oem_part_number_txt='';
	if(product_part_no) product_part_no_txt=', SAP Code.:'+product_part_no;
	//if(product_oem_part_number) product_oem_part_number_txt=', OEM Part No.:'+product_oem_part_number;
	//alert(rowCount);
$('#soTable tbody').prepend('<tr class="child" id="row_'+nxtCount+'"><td>'+product_name+' ('+product_code+')'+product_part_no_txt+product_oem_part_number_txt+'<input type="hidden" class="form-control text-center rquantity" name="row['+nxtCount+'][product_id][]" value="'+product_id+'" id="product_id_'+nxtCount+'"></td><td class="text-right"><input type="hidden" name="row['+nxtCount+'][unit_price][]" id="product_price_'+nxtCount+'" value="'+product_price+'"><input type="hidden" name="row['+nxtCount+'][item_cost][]" id="item_cost_'+nxtCount+'" value="'+item_cost+'"><input type="text" style="width:50px;" name="row['+nxtCount+'][item_price_p][]" id="item_price_p_'+nxtCount+'" value="'+0+'"  onchange="addextraprice();"> + '+product_price_dis+'</td><td><input type="text" class="noEnterSubmit form-control text-center rquantity" name="row['+nxtCount+'][qty][]" value="1" id="quantity_'+nxtCount+'" onclick="this.select(); setTmpVal(this.value);" onchange="changeQtyByProductID(this.value,'+nxtCount+');"></td><td class="text-right"><span class="text-right sdiscount text-danger" id="sdiscount_1446800197032"><input type="text" name="row['+nxtCount+'][discount][]" value="0" id="discount_'+nxtCount+'" onclick="this.select(); setTmpVal(this.value);" onchange="changeDiscountByProductID(this.value,'+nxtCount+');" style="width:50px;"> </span><input type="hidden" name="row['+nxtCount+'][discount_val][]" value="'+discount_val+'" id="discount_val_'+nxtCount+'"><input type="hidden" name="row['+nxtCount+'][discount_val_tot][]" value="'+discount_val_tot+'" id="discount_val_tot_'+nxtCount+'"></td><td class="text-right"><span class="text-right ssubtotal" id="subtotal_'+nxtCount+'">'+sub_total_item+'</span><input type="hidden" name="row['+nxtCount+'][gross_total][]" value="0" id="gross_total_'+nxtCount+'">&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>');
	
	//$.post( "<?php echo base_url();?>sales/add_sales_item_to_list", product_id:product_id)
	//	  var obj = jQuery.parseJSON(data);

	//  });		
	calculateTotal();
} //end error check
}

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

function changePaidValue(paid){
	if(isNaN(paid)) {
		displayNotice('page','Invalid Paid Amount');
		
		//alert(quantity_fld);
		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 convertToAmount2Des(val)
{
	var disval=val; //+'.00'; //.toFixed(val);
	return accounting.formatMoney(disval, "", 2, "", "."); // €4.999,99 
}

function changeMainDiscount(val){
	//alert(val);
	calculateTotal();
}

/* check is added produtct */
function isAddedProduct(pid){
	
	//alert(pid);
	var rowCount=parseInt($('#rowCount').val());
	for(i=1; i<=rowCount; i++){
		var product_fld='#product_id_'+i; 
		//alert(product_fld);
		var product_id=parseInt($(product_fld).val());
		if(product_id==pid){
			//change qty
			var quantity_fld='#quantity_'+i;
			var quantity_val=parseFloat($(quantity_fld).val());
			$(quantity_fld).val(quantity_val+1)
			
			//alert('updated qty');
			return 1;
		}else{
			//alert('no');	
			//return 0;
		}
		
	}
}


/* end check is added produtct */


function check_credit_limit_avalable_by_cus_id_and_tot_amount(cus_id,tot_amount){
	//alert(customer_id+' '+tot_amount);
	var base_url=$("#base_url").val();
	$.get(base_url+"sales/get_old_credit_amount?cus_id="+cus_id)
	  .done(function( data ) {
		  var obj = jQuery.parseJSON(data);
		 
	var old_credit_amount=obj.old_credit_amount;
	//alert(old_credit_amount);
	return old_credit_amount;	
	}); 	
}

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 ***
  