<script type="text/javascript">

	function addRowOnetime(event,id)
	{
		event.preventDefault();

    var set_percentage = 100;

    if(((set_percentage-getPercentage($('.plan_percentage')))) > 1)
    {
      var auto_generate = 'amount_'+(countPlanList('#onetime_table tr') + 1);

       //INTRODUCED THIS QUOTE VARIABLE BECAUSE WE HAD TO CONCATENATE IT WITH THE CLASS BECAUSE OF CONFLICT OF SINGLE AND DOUBLE QUOTES
      var special_char = "'";


      var description ='<input class=" cls_alpha_numeric cls-one-time-description form-control form-control-sm" placeholder="Description" type="text" name="description[]"/>';


      var percentage = '<input type="text"  class="cls-one-time-percentage cls_decimal_digit plan_percentage form-control form-control-sm" onchange = "calculatePercentageAmount(event,this,'+special_char+auto_generate+special_char+')" value="0" name="percentage[]"/>';

      

      var amount = '<input class=" cls_decimal_digit form-control form-control-sm cls-one-time-amount '+auto_generate+'"  placeholder="Amount" type="text" readonly="true" name="amount[]"/>';


      var type_of_payment = <?php echo json_encode(EF::getPlanType(), 15, 512) ?>;

      var dropdown_payment = '<select name="payment_type[]" class = "form-control form-control-sm cls_selection"><option value = "0" selected>Select Type</option>';

      var due_date = '<input type="date" name="date[]" class="cls-one-time-duedate form-control form-control-sm cls_date"/>';

      $.each(type_of_payment, function( index, value ) 
      {
            if(value!= "One Time")
            {
              dropdown_payment+='<option value="'+value+'">'+value+'</option>';
            }
      

      });

      dropdown_payment+='</select>';

      var buttons = "'#onetime_table'";
      


      var action = '<button class="btn-danger btn-sm" onclick="removeRowOnetime(event,this)"><i class="fa fa-times"></i></button>';

      var view = '<tr><th scope = "row">'+(countPlanList('#onetime_table tr') + 1)+'</th><td><div>'+description+'<small style = "float:none;" class="form-errors  req" value="*"><?php echo e($errors->first("description[]",":message")); ?></small></div></td><td><div>'+dropdown_payment+'<small style = "float:none;" class="form-errors  req" value="*"><?php echo e($errors->first("payment_type[]",":message")); ?></small></div></td><td><div>'+percentage+'<small style = "float:none;" class="form-errors  req" value="*"><?php echo e($errors->first("percentage[]",":message")); ?></small></div></td><td>'+amount+'</td><td><div>'+due_date+'<small style = "float:none;" class="form-errors  req" value="*"><?php echo e($errors->first("date[]",":message")); ?></small></div></td><td>'+action+'</td></tr>'; 

      $(id).append(view); 
    }
    else
    {
         messageToaster('error','INVALID PERCENTAGE INPUT','INVALID INPUT');

    }

		   
	}

  // TABLE ID = "tab"  $('#tab tr')
  function countPlanList(table_object)
  {
  	 var count = 0;

  	 table_object = $(table_object);

  	 table_object.each(function(){

  	 	count++;

  	 });

  	 return count;
  }
  function rearrangeTableOrder(table_object)
  {
  	 var count = 0;

  	 table_object = $(table_object);

  	 table_object.each(function(){

  	 	$(this).find('th').html(++count);
  	 

  	 });

  	 return count;
  }

  function chooseOneTime(event,obj)
  {
  	

  	obj = $(obj).val();
  		if(obj == 0)
  		{
 $('#onetimediv').show();
  $('#installmentdiv').hide();
  		}
  		else if(obj == 1)
  		{
 $('#onetimediv').hide();
  $('#installmentdiv').show();
  		}
  		else
  		{
 $('#onetimediv').hide();
  $('#installmentdiv').hide();
  		}
  }

  function removeRowOnetime(event,obj)
  {
  
  	event.preventDefault();

    // SET ACTUAL ONE TIE PERCENTAGE
    var set_percentage = 100;

    // VIA OBJECT FIND CLOSEST TR THEN REMOVE NOW
  	$(obj).closest("tr").remove(); 

    // REARANGE THE SERIAL NO 1.2.3.....
  	rearrangeTableOrder('#onetime_table tr'); 

    // GET NEW PERCENTAGE FOR ONE TIME AND SET AGAIN 
    setOnetimePercentage();
  }

  function calculatePercentageAmount(event,obj,amount)
  {

  	event.preventDefault();
    
    // SET ACTUAL ONE TIE PERCENTAGE
    set_percentage = 100;

    //GETTING FIRST ROW PERCENTAGE AND AMOUNT
    var agreed_amount = "<?php echo e($remaining_plan_amount); ?>";
     

    //GETTING SUM OF PERCENTAGE
    var total_percentage = getPercentage($('.plan_percentage'));


     //GETTING CURRENT ENTERED PERCENTAGE VALUE
     var current_percentage = $(obj).val();


      //CHECK ENSURES CURRENT PERCENTAGE IS LESS THAN SUM OF PERCENTAGE
     if(current_percentage >= 1 &&  total_percentage < 100)
     {

       //DIVIDING PERCENTAGE BY ZERO
       var percentage_division = isDefaulter(current_percentage/100);

       var agreedamount = "<?php echo e($remaining_plan_amount); ?>";

       //CALCULATING CURRENT AMOUNT FROM AGREED AMLUNT
       var current_amount = Math.round(percentage_division * agreedamount);


        $('.'+amount).val(current_amount);

        setOnetimePercentage();
 
     }
     else
     {
     	   messageToaster('error','INVALID PERCENTAGE INPUT','INVALID INPUT');
         $('.'+amount).val('');
         $(obj).val('');

         setOnetimePercentage();



     }

    
   


  }

  function getPercentage(element)
  {
  	var percentage = 0;

  	element.each(function(event){

  		  percentage+= isDefaulter($(this).val());
  	});

  	return percentage;
  }

  function setOnetimePercentage()
  {
      var agreed_amount = "<?php echo e($remaining_plan_amount); ?>";

      var total_percentage = (100 - getPercentage($('.plan_percentage')));

      var percentage = isDefaulter(total_percentage/100);


      $('#amount').val(percentage * agreed_amount);

      $('#percentage').val(total_percentage);
  }
 


</script><?php /**PATH /var/www/skylite/resources/views/scripts/onetime.blade.php ENDPATH**/ ?>