//var addrow;
$aw_i = 2;
$tabindexnum = 8;
/*$("#new1").disable('click');
$("#new1").enable();
////////////////
var myFunc = function(event){
     event.stopPropagation();
     // execute a bunch of action to preform
}

$('#web').on('click', notify); //bind myFunc
$('#web').off('click'); // click is succesfully removed
$('#web').on('click', notify); //rebind again
function notify()
{
	alert('clicked');
}

*/
//$('#new1').unbind('click');
//$('#new1').bind('click',new1func);
//$("#new1").on('click',new1func);
// JavaScript Document
$("#clientId").change(function(e) {
    $selectedId = document.getElementById('clientId').value;
		$.ajax({
				  url: "MaintennanceSettlementControllerAjax.php?clientId="+$selectedId,
				  type: "get",
				  success: function(data){
					 //alert(data);
					 if(data != null)
					 {
						$("#loadMaintenanceReceipts").html(data);
						$('select').select2();
					 }
				  },
				  error:function(){
				  alert("لقد حدث خطأ أثناء إجراء العملية");
		  }   
		});
		
		$(".pay").remove(); //.parent().remove(); 
		addrow($aw_i,$tabindexnum);
		
		
		// incrementaion of row number and tab index		
		$aw_i++;
		$tabindexnum++ ;
		
		// fill the iltration filed with common iltration
		document.getElementById("hidden_itr").value = $aw_i ;
		$hidden_ArrayOfIds=document.getElementById('hidden_ArrayOfIds').value;
		
		
		$.ajax({
				  url: "MaintennanceSettlementControllerAjax.php?do=getPayments&clientId="+$selectedId+"&itr="+$aw_i+"&Ids="+$hidden_ArrayOfIds,
				  type: "get",
				  success: function(data){
					 //alert(data);
					 if(data != null)
					 {
						$("#clientPayments"+$aw_i).html(data);
						$('select').select2();
						document.getElementById('selectedReciptTotalCostLbl1').innerHTML='الاجمالى : 0';
						document.getElementById('selectedReciptTotalCostLbl1_hidden').value=0;
					 }
				  },
				  error:function(){
				  alert("لقد حدث خطأ أثناء إجراء العملية");
		  }   
		}); 	
		
});


  	//add new row in bill
    /**************************/
    addrow = function($aw_if, $tabindexnumf)
    {
	
		$xx = '<tr id="row'+$aw_i+'" class="pay">';
			
			$xx += '<td><a class="newLink btn btn-primary btn-xs addpayment"  id="new'+$aw_i+'"  name="'+$aw_i+'">+</a></td>';
			$xx +='<td>'
			$xx += '<div id="clientPayments'+$aw_i+'">'
			
			+'    <select name="payment'+$aw_i+'" id="payment'+$aw_i+'" data-placeholder="اختر" class="select2_custom aw_m1 "  tabindex="' + $tabindexnum + '">'
					+ '<option value="-1">-- اختر العميل اولا --</option>'
			+'    </select>'
			+'    </div>'
			$xx +=' <input type="hidden" id="payed'+$aw_i+'" name="payed'+$aw_i+'" value ="0" />'
			+'    </td>'
			
		
			$xx += '<td>	<button type="button" id="remove'+$aw_i+'" class="removeit close flt-none" aria-hidden="true"   onclick="removeproduct('+$aw_i+');" >&times;</button>'
	+'				</td>'
					+ '</tr>';
					
			
					
					
			
			//add the new row with itration and with all propperties	
			$("#containerpros").append($xx);
			
			
		//	//load elements
		$("#clientPayments"+$aw_i).load(function(e) {
			
			$myvar=$aw_i;
						$clientId=document.getElementById('clientId').value;
						$hidden_ArrayOfIds=document.getElementById('hidden_ArrayOfIds').value;

							$.ajax({
									  url: "MaintennanceSettlementControllerAjax.php?do=getPayments&clientId="+$clientId+"&itr="+$aw_i+"&Ids="+$hidden_ArrayOfIds,
									  type: "get",
									  success: function(data){
										//alert(data);
										 if(data != null)
										 {
											$("#clientPayments"+$myvar).html(data);
											$('select').select2();
										 }
									  },
									  error:function(){
									  alert("لقد حدث خطأ أثناء إجراء العملية");
							  }   
							}); 	
						
						
				
                
            });
			
		$("#clientPayments"+$aw_i).trigger('load');
			
			//remain value
			
			$('select').select2();

		
       
        //$(".numberinput").forceNumeric();
		
		
		$("#new"+$aw_i).click(function() {
			if($( ".addpayment" ).hasClass( "enabel" ))
			{
					$noOfRows=Number(document.getElementById('hidden_itr').value);
					
					for($i=2;$i<$noOfRows;$i++)
					{
						if(document.getElementById("payment"+$i+""))
						{
							if(document.getElementById("payment"+$i+"").value!=-1)
							{
								//alert('ok');
							}
							else
							{
								alert(" من فضلك اختر كل الدفعات اولا.");
								return false;
							}
						}
					
					}
					
						addrow($aw_i,$tabindexnum);
						
						// incrementaion of row number and tab index		
						$aw_i++;
						$tabindexnum++ ;
						
						
						// fill the iltration filed with common iltration
						document.getElementById("hidden_itr").value = $aw_i ;
						
						//reload the dropdownlist and jquery files	
						//$('select2').select2();
						return false;
			}
			else
			{
				alert(' الدفعات المدخلة كافية');
			}
		
		});
		
		
		//$("#allProducts'+ $aw_if + '").html($("#allProducts").html());
	

    }
    /**************************/

/*$("#new1").click(function () {
	
		addrow($aw_i,$tabindexnum);
		
		// incrementaion of row number and tab index		
		$aw_i++;
		$tabindexnum++ ;
		
		// fill the iltration filed with common iltration
		document.getElementById("hidden_itr").value = $aw_i ;
			
		//reload the dropdownlist and jquery files	
		//$('select2').select2();
		return false;
	});*/
	

// the delete action that delete row of products
    /************************/
    $(document).on('click', '.removeit', function() {

        // delete by partents
        $(this).parent().parent().remove();

        // get the itration from itration colum
        $remitr = document.getElementById("hidden_itr").value;
		
		calculateRemain();

      
        return false;
    });


//submit
$('#save').click(function(e) {
    //alert('ssssssssss');
	
	//check if client is choosen
	$clientId=document.getElementById('clientId').value;
	//alert($clientId.length>0&&$clientId!=-1);
	if($clientId.length>0&&$clientId!=-1)
	{
		//true contine
	}
	else
	{
		//event.preventDefault();
		alert("من فضلك اختر العميل اولا.");
		return false;
	}
	
	
	//check if mMaintenanceReceiptId is choosen
	$mMaintenanceReceiptId=document.getElementById('mMaintenanceReceiptId').value;
	//alert($clientId.length>0&&$clientId!=-1);
	if($mMaintenanceReceiptId.length>0&&$mMaintenanceReceiptId!=-1)
	{
		//true contine
	}
	else
	{
		//event.preventDefault();
		alert("من فضلك اختر الفاتورة اولا.");
		return false;
	}
	
	
	
	$rows= Number(document.getElementById("hidden_itr").value);
	//check if payments are choosen
	for($i=2;$i<$rows;$i++)
	{
		if(document.getElementById("payment"+$i+""))
		{
			if(document.getElementById("payment"+$i+"").value!=-1)
			{
				//true contine
			}
			else
			{	
				//event.preventDefault();
				alert(" من فضلك اختر كل الدفعات اولا.");//will prevent default form submission
				return false;	
			}
		}
	
	}
	
	//check if remain <0
	$remain_hidden=Number(document.getElementById('remain_hidden').value);
	//alert($remain_hidden);
	if($remain_hidden<=0)
	{
		//true contine
	}
	else
	{
		//event.preventDefault();
		alert(" الدفعات المدخلة غير كافية.");
		return false;
	}
});

/////////////////////////////////////////////////////////////////////////////////////////////
//                                  srtop many submits                                     //
/////////////////////////////////////////////////////////////////////////////////////////////

$("#mSettelment").submit(function(ev) {
	
	
	
// submit more than once return false
$(this).submit(function() {

return false;
});
// submit once return true


return true;
});