$(document).ready(function(e) {
    $("#supplierid").change(function(e) {
        $.ajax({
		  url: "supplierPayedDeptControllerAjax.php?id="+$(this).val(),
		  type: "get",
		  success: function(data){
				 var response = $.parseJSON(data);
				 $("#debtBefore").val(response.suppliercurrentDebt);
				 $("#lastDate").val(response.supplierdate);
		  },
		  error:function(){
			  alert("لقد حدث خطأ أثناء إجراء العملية");
		  }   
		}); 
    });
	
});


//function will allow negative numbers in debtAfter
function populateDebtAfter()
{
	
	//alert(2222);
	var payedDept = Number($("#payedDept").val());
	var debtBefore = Number($("#debtBefore").val());
	var suplliernegative = Number($("#suplliernegative").val());
	//alert(suplliernegative);
	
	if(suplliernegative == 0)
	{
			//alert('in if');
		if(debtBefore)
		{  
		    if( payedDept <=  debtBefore)
			{
				var debtAfter = debtBefore - payedDept;
				$("#debtAfter").val(debtAfter);
			}
			else
			{
				$("#debtAfter").val(0);
				$("#payedDept").val(debtBefore);
				
			}
		}
		else//if user didn't choose any client yet
		{
			$("#payedDept").val(0);
			//alert("يرجى اختيار مورد");	
		}
	}
	else
	{
		//alert('in else');
		
		if(debtBefore >= 0 || debtBefore <= 0)
		{
			var debtAfter = debtBefore - payedDept;
			$("#debtAfter").val(debtAfter);
		}
		else//if user didn't choose any client yet
		{
			$("#payedDept").val(0);
			//alert("يرجى اختيار مورد");	
		}
		
	}
}


 $(document).ready(function() {

		/*$("#txtcheckNum").val("yes");
		$("#ddlBank").val("1");*/
});
 
 
function hideDel(){
  if(document.getElementById("showDel").checked){
       
       $('.hideTrow').hide();
 }else{
            $('.hideTrow').show();

 }
}
 
function  getdata()
	{
		 
		
		if(document.getElementById("paytype1").checked)
		{
			document.getElementById("checkdata").style.display="none";
			
				
				$("#txtcheckNum").val("yes");
				$("#ddlBank").val("1");
		         submitform();
		}
		
		else
		{
			
			document.getElementById("checkdata").style.display="block";
                        $sum=(parseInt (document.getElementById("payedDept").value )- parseInt ( document.getElementById("accountbeginingbalanceDis").value))*-1;
			if($sum==""){
                            $sum=0;
                        }
                        document.getElementById("txtAmount").value=$sum ;
			$("#txtcheckNum").val("");
				$("#ddlBank").val("");
				   submitform();
				
			
		}
	
	}
	
	
	
	function load1()
	{
		$('#sss').html('<img src="images/loader.gif/>');
		$('#sss').load('getallaccountajax.php?do=bank&bankid='+document.getElementById('ddlBank').value); 
    }
	
	
	
	
	
	function submitform()
	{
		
		
	   var txtAmount=Number(document.getElementById("txtAmount").value);	
	   var assetprice=Number(document.getElementById("payedDept").value);
		if(document.getElementById("paytype1").checked)
		{
			
		
			
		}
		
	
		else if(document.getElementById("paytype2").checked == true )
		{
			
			document.getElementById("txtAmount").value=(parseInt(document.getElementById("payedDept").value) - parseInt( document.getElementById("accountbeginingbalanceDis").value))*-1;
			//alert('dddd');
				if (document.getElementById("accountbeginingbalanceDis").value  > 0)
				{
					 var accountbeginingbalanceDis =document.getElementById("accountbeginingbalanceDis").value;
					if(accountbeginingbalanceDis  < assetprice)
					{
						alert('عفوا القيمه المبلغ الموجود بالحساب  لا يكفى');
						document.getElementById("txtAmount").value=accountbeginingbalanceDis;
						document.getElementById("payedDept").value=accountbeginingbalanceDis;
					}
					
				}
				else
				{
					
					//alert('من فضلك اختر البنك والحساب لكى يتم  تحديد المبلغ المتاح');
				}
			
			
		}
	}
	
	function load2(){
	//alert('lkjklj');
	$('#ddd').html('<img src="images/loader.gif/>');
	$('#ddd').load('getallaccountajax.php?do=account&accountid='+document.getElementById('accountid').value);
}

