<script type="text/javascript">
	// EDIT VOUCHER SCRIPT FORM AM'S
  function voucherFilters(event)
  {
  	 event.preventDefault();

  	 showLoader();

      var route = "{{route('voucherfilters')}}";

      var from  = $('#from').val();

      var to  = $('#to').val();


      var project  = $('#filter_project').val();

      var formdata = {'_token':token,'from':from,'to':to,'project':project};

      $.post(route,formdata, function(result){
				
      		$('#show_vouchers').html(result);

  	 		removeLoader();
      });
  }

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

		changePaymentMode();
  	  	var voucher_type = $(obj).val();

  	  	var route = "{{url('subheadtype')}}/"+voucher_type;


  	     showLoader();

		$.get(route, function(data, status)
		{
			var subhead_list = '';

			if(data.voucher_type === "CRV" || data.voucher_type === "BRV")
			{
				$('.debit_subhead_list').each(function(){

					$(this).html(data.filter_subhead_list);

					setFstDropdown();

	                document.querySelector('.'+$(this).attr('content')).fstdropdown.rebind();

				});

				$('.credit_subhead_list').each(function(){

					$(this).html(data.subhead_list);

					setFstDropdown();

	                document.querySelector('.'+$(this).attr('content')).fstdropdown.rebind();

				});
			}
			else if(data.voucher_type === "CPV" || data.voucher_type === "BPV")
			{
				$('.credit_subhead_list').each(function(){

					$(this).html(data.filter_subhead_list);

					setFstDropdown();

	                document.querySelector('.'+$(this).attr('content')).fstdropdown.rebind();

				});

				$('.debit_subhead_list').each(function(){

					$(this).html(data.subhead_list);

					setFstDropdown();

	                document.querySelector('.'+$(this).attr('content')).fstdropdown.rebind();

				});
			}
			else
			{
				$('.debit_subhead_list').each(function(){

					$(this).html(data.subhead_list);

					setFstDropdown();

	                document.querySelector('.'+$(this).attr('content')).fstdropdown.rebind();

				});

				$('.credit_subhead_list').each(function(){

					$(this).html(data.subhead_list);

					setFstDropdown();

	                document.querySelector('.'+$(this).attr('content')).fstdropdown.rebind();

				});
			}










	        removeLoader();

	  	});
  }
   // AJAX REQUEST TO STORE VOUCHER
  function storeVoucher(event,obj)
  {
      event.preventDefault();

      var obj = $('#form_add_voucher');
      var route = obj.attr('action');
      var formdata = obj.serialize();

      showLoader();

      $.post(route, formdata, function(result)
      {
          if(result)
          {
              // Reload the current page without the browser cache
              messageToaster('success','Voucher has been created.','Success');

              // location.reload(true);

			$('#view_data').html('');

			obj.trigger("reset");



          }
      removeLoader();


      },'json')
      .fail(function(data) {

          messageToaster('error','Required form validation.','Failed');

          ajax_show_error(data,obj,$('#md_add_voucher'));


          removeLoader();

        });
  }
function showVoucher(event,obj)
{
	event.preventDefault();

	var route = $(obj).attr('href');

        showLoader();

	$.get(route, function(data, status)
	{
		$('#show_voucher').html(data);
		$('#md_update_voucher').modal('show');

        removeLoader();

  	});
}

function getPaymentList(element,key_element,encrypt_element)
{
	resetBookingSection(key_element,encrypt_element);

	var booking = $(element).val();

	getGenericPaymentList(key_element,booking);
}

function getGenericPaymentList(key_element,args)
{

      showLoader();

      var route = "{{url('genericpaymentlist')}}";

      var type  = $('#generic_type_'+key_element).val();

      var formdata = {'_token':token,'generic_type':type,'args':args};

      $.post(route,formdata, function(result)
      {
      	  $('#payment_type_'+key_element).html(result.view);

      	  if(result.show_section === "Booking")
      	  {
      		 $('#booking_section_'+key_element).show();
      	  }
      	  else
      	  {
      	  	$('#booking_section_'+key_element).hide();
      	  	$('#booking_'+key_element).prop("selectedIndex", 0);
      	  	$('#amount_'+key_element).removeAttr('readonly');
      	  	$('#amount_'+key_element).val('');

      	  }

      	  removeLoader();

      });
}

function getBookingListOnCompound(event,key_element){

    var option = $('#branch_'+key_element).val();

	var booking_route ="{{url('/bookingbranch')}}/"+option;

    showLoader();

	$.get(booking_route,function(data,status){

		$('select[name^=booking').html(data.booking_view);

		$('select[select^=payment').html(data.booking_view);

		$('select[name^=booking').html(data.booking_view);

		// $('#booking_'+key_element).html();

        removeLoader();

	});
}

function getBookingList(element,key_element,encrypt_element)
{
	resetBranchSection(key_element,encrypt_element);

	var option = $(element).val();

	var booking_route ="{{url('/bookingbranch')}}/"+option;

    showLoader();

	$.get(booking_route,function(data,status){

		$('select[name^=booking').html(data.booking_view);

		$('select[select^=payment').html(data.booking_view);

		$('select[name^=booking').html(data.booking_view);

		// $('#booking_'+key_element).html();

        removeLoader();

	});
}


function resetBranchSection(key_element,encrypt_element)
{
	// $('#booking_'+key_element).html('<option value="'+encrypt_element+'" >Select Booking</option>');

	// $('#payment_type_'+key_element).html('<option value="'+encrypt_element+'" >Select Payment</option>');

	// $('#amount_'+key_element).val('');


	// $('#user_'+key_element).html('<option value="'+encrypt_element+'" >Select User</option>');

	// setDropDown($('#usertype_'+key_element+' option'),'Select User Type');

 //    $('#amount_'+key_element).removeAttr('readonly',true);

    $('select[name^=booking]').html('<option value="'+encrypt_element+'" >Select Booking</option>');

    $('select[name^=payment_type]').html('<option value="'+encrypt_element+'" >Select Payment</option>');

    $('input[name^=amount]').val('');

    $('input[name^=amount]').removeAttr('readonly',true);

	// $('select[name^=user]').html('<option value="'+encrypt_element+'" >Select User</option>');

	$('select[name^=payment_mode]').val('Cash');

	$('select[name^=payment_mode]').each(function()
	{
		$(this).find('option').each(function()
		{
			$(this).show();
		});

	});



    $('select[name^=payment_mode]').removeAttr('readonly');
    $('input[name^=instrument]').removeAttr('readonly');
    $('input[name^=drawn]').removeAttr('readonly');
    $('input[name^=instrument_date]').removeAttr('readonly');



	// setDropDown($('select[name^=user_type] option'),'Select User Type');

    // $('#amount_'+key_element).removeAttr('readonly',true);
}


function resetBookingSection(key_element,encrypt_element)
{
	$('#payment_mode_'+key_element+' option').each(function(){

     	$(this).show();
    });

	$('#payment_type_'+key_element).html('<option value="'+encrypt_element+'" >Select Payment</option>');

	$('#user_'+key_element).html('<option value="'+encrypt_element+'" >Select User</option>');

	setDropDown($('#usertype_'+key_element+' option'),'Select User Type');

	$('#amount_'+key_element).val('');

	$('#amount_'+key_element).removeAttr('readonly');

	$('#payment_mode_'+key_element).val('Cash');

	$('#instrument_'+key_element).val('');

	$('#drawn_'+key_element).val('');

	$('#instrument_date_'+key_element).val('readonly');

	$('#payment_mode_'+key_element).removeAttr('readonly');

	$('#instrument_'+key_element).removeAttr('readonly');

	$('#drawn_'+key_element).removeAttr('readonly');

	$('#instrument_date_'+key_element).removeAttr('readonly');

	$('.bank_section_'+key_element).hide();



}
function getPayment(element,key_element)
{
	// RESET PAYMENT SECTION
	var option = $(element).val();

	var payment_route ="{{url('/getpayment')}}/"+option;

    showLoader();

	$.get(payment_route,function(data,status){


		$('#amount_'+key_element).val(data.amount);
		$('#payment_mode_'+key_element).val(data.mode);
		$('#instrument_'+key_element).val(data.instrument);
		$('#instrument_date_'+key_element).val(data.instrument_date);
		$('#drawn_'+key_element).val(data.drawn_on);
		$('#deposit_no_'+key_element).val(data.deposit_no);
		$('#deposit_date_'+key_element).val(data.deposit_date);


		if(data.mode == "Cash")
		{
			$('.bank_section_'+key_element).hide();
		}
		else
		{
			$('.bank_section_'+key_element).show();
		}

		if(data.amount > 0)
		{
			$('#user_'+key_element).html(data.user_view);

			$('#amount_'+key_element).attr('readonly',true);

			$('#payment_mode_'+key_element).attr('readonly',true);
		    $('#instrument_'+key_element).attr('readonly',true);
		    $('#drawn_'+key_element).attr('readonly',true);
		    $('#instrument_date_'+key_element).attr('readonly',true);

		    $('#deposit_no_'+key_element).attr('readonly',true);
		    $('#deposit_date_'+key_element).attr('readonly',true);

		}
		else
		{
			$('#user_'+key_element).html(data.user_view);

			$('#amount_'+key_element).removeAttr('readonly');

			$('#payment_mode_'+key_element).removeAttr('readonly');
		    $('#instrument_'+key_element).removeAttr('readonly');
		    $('#drawn_'+key_element).removeAttr('readonly');
		    $('#instrument_date_'+key_element).removeAttr('readonly');

		    $('#deposit_no_'+key_element).removeAttr('readonly');
		    $('#deposit_no_date_'+key_element).removeAttr('readonly');

		    $('#payment_mode_'+key_element+' option').each(function(){
     			$(this).show();
     		});
		}


		setDropDown($('#usertype_'+key_element+' option'),data.type);
        removeLoader();

	});
}

function getUserByDealer(element,key_element)
{
	var option = $(element).val();

	var user_route ="{{url('/userlistwithroles')}}/"+option;
        showLoader();


	$.get(user_route,function(data,status){

		$('#user_'+key_element).html(data.view);
        removeLoader();

	});
}
// function changeBooking(key_element,child_element)
// {
// 		resetBookingSection(key_element,child_element);

// }


function getSubheadByHead(element,key_element,convention)
{
	var option = $(element).val();

	var subhead_route = "{{url('vouchersubheadlist')}}/"+option;

    showLoader();

	$.get(subhead_route,function(data,status){

		$('#'+convention+'_subhead_'+key_element).html(data);
        removeLoader();


	});
}

function showPaymentDropDown(element,key_element)
{
	var payment_route = null;

	var booking_id = $('#booking_'+key_element).val();

	var first_commission_child = $('#commission_'+key_element+' option:first-child').val();

	var first_installment_child = $('#installment_'+key_element+' option:first-child').val();

	element = $(element).val();

	if(element.trim() == "Installment")
	{
		resetPaymentDropDown('commission',first_commission_child,key_element);

		payment_route = "{{url('voucherinstallments')}}/"+booking_id;
        showLoader();

		$.get(payment_route,function(data,status)
		{
			$('#installment_'+key_element).html(data);
        removeLoader();

		});

		$('#installment_dropdown_'+key_element).show();
	}
	else if(element.trim() == "Commission")
	{
		resetPaymentDropDown('installment',first_installment_child,key_element);

		payment_route = "{{url('vouchercommission')}}/"+booking_id;
        showLoader();


		$.get(payment_route,function(data,status)
		{
			$('#commission_'+key_element).html(data);
        removeLoader();

		});

		$('#commission_dropdown_'+key_element).show();
	}
	else
	{
		resetPaymentDropDown('installment',first_installment_child,key_element);
		resetPaymentDropDown('commission',first_commission_child,key_element);
	}
}

function resetPaymentDropDown(conventions,child_element,key_element)
{
    $('#'+conventions+'_dropdown_'+key_element).hide();

	$('#'+conventions+'_'+key_element).html('<option value="'+child_element+'" >Select '+conventions+'</option>');

	$('#amount_'+key_element).val(0);
}


function setDropDown(element,key_match)
{
	element.each(function(){

		if($(this).html().trim() == key_match.trim())
		{
			$(this).prop('selected','selected');

		}
		else
		{
			$(this).removeAttr('selected');
		}





	});
}

function getAmountBySelection(element,key_element,conventions)
{
	var amount_route = null;

	id = $(element).val();

	if(conventions == "Installment")
	{
		amount_route ="{{url('voucherinstallmentamount')}}/"+id;
        showLoader();

		$.get(amount_route,function(data,status){

			$('#amount_'+key_element).val(data.amount);
			$('#user_'+key_element).html(data.user_view);

			setDropDown($('#usertype_'+key_element+' option'),data.type);
        removeLoader();

		});
	}
	else if(conventions == "Commission")
	{
		amount_route ="{{url('vouchercommissionamount')}}/"+id;
        showLoader();


		$.get(amount_route,function(data,status){


			$('#amount_'+key_element).val(data.amount);
			$('#user_'+key_element).html(data.user_view);
			setDropDown($('#usertype_'+key_element+' option'),data.type);
          removeLoader();

		});
	}
	else
	{
		$('#amount_'+key_element).val(0);
	}
}



function payPayment(event,fetch_data,form_element,modal_element,key_element)
  {


     event.preventDefault();

     data = JSON.parse(fetch_data);

     if(data.payment_type == "Installment" || data.payment_type == "Transfer" || data.payment_type == "Deduction")
     {


       voucher_list = '<option value="BRV">Bank Receipt Voucher</option><option value="CRV">Cash Receipt Voucher</option><option value="JV">Journal Voucher</option>';


        $('#type').val('Installment');



     }
     else
     {
         voucher_list = '<option value="BPV">Bank Payment Voucher</option><option value="CPV">Cash Payment Voucher</option><option value="JV">Journal Voucher</option>';


        $('#type').val(data.payment_type);



         // END COMMISSION SECTION
     }

     $('#'+form_element+' #payment_type').val(data.id);
     $('#'+form_element+' .branch_id').val(data.branch);

     $('#'+form_element+' #pay_user').val(data.user);
     $('#'+form_element+' #booking').val(data.booking);



     $('#'+form_element+' .amount').val(data.amount);
     $('#'+form_element+' .date').val(data.date);
     $('#'+form_element+' .branch').val(data.branch_name);
     $('#'+form_element+' input[name=user_name]').val(data.name);
     $('#'+form_element+' #pay_usertype').val(data.user_type);
     $('#'+form_element+' input[name=booking_name]').val(data.booking_name);


     $('#instrument_'+key_element).val(data.instrument);
     $('#drawn_'+key_element).val(data.drawn_on);
     $('#instrument_date_'+key_element).val(data.instrument_date);
     $('#deposit_no_'+key_element).val(data.deposit_no);
     $('#deposit_date_'+key_element).val(data.deposit_date);


     $('#payment_mode_'+key_element).val(data.mode);

     $('#payment_mode_'+key_element+' option').each(function(){


     	if(data.mode != $(this).val())
     	{
     		$(this).hide();
     	}


     });

     $('#payment_mode_'+key_element).attr('readonly',true);
     $('#instrument_'+key_element).attr('readonly',true);
     $('#drawn_'+key_element).attr('readonly',true);
     $('#instrument_date_'+key_element).attr('readonly',true);
     $('#deposit_no_'+key_element).attr('readonly',true);
     $('#deposit_date_'+key_element).attr('readonly',true);


     $('#voucher_branch').attr('readonly',true);




	if(data.mode == "Cash")
	{
		$('.bank_section_'+key_element).hide();
	}
	else
	{
		$('.bank_section_'+key_element).show();
	}







     $('select[name=voucher_list]').html(voucher_list);



     $('#md_pay_payment').modal('show');
  }

// function payPayment(event,data)
//   {
//   	 $('#form_pay_payment')[0].reset();

//      event.preventDefault();

//      data = JSON.parse(data);

//      if(data.installment_type == "Installment")
//      {
//          // COMMISSION SECTION
//          $('#pay_commission_dropdown').hide();

//          $('#pay_commission').val(default_encrypt);

//          $('input[name=commission_name]').val('');



//          // END COMMISSION SECTION

//          // INSTALLMENT SECTION
//          $('#pay_installment_dropdown').show();

//          $('#pay_installment').val(data.installment);

//          $('input[name=installment_name]').val(data.description);

//          $('.voucher_title').html('Pay Installment');

//          voucher_list = '<option value="BRV">Bank Receipt Voucher</option><option value="CRV">Cash Receipt Voucher</option><option value="JV">Journal Voucher</option>';

//        $('#form_pay_payment').attr("action", "{{url('payinstallment')}}");

//          // END INSTALLMENT SECTION

//      $('#payment_type').val('Installment');


//      }
//      else if(data.installment_type == "Commission")
//      {
//         // INSTALLMENT SECTION
//          $('#pay_installment_dropdown').hide();


//          $('#pay_installment').val(default_encrypt);

//          $('input[name=installment_name]').val('');

//          // END INSTALLMENT SECTION

//          // COMMISSION SECTION
//          $('#pay_commission_dropdown').show();

//          $('#pay_commission').val(data.installment);

//          $('input[name=commission_name]').val(data.description);

//          $('.voucher_title').html(data.payment_type+' Voucher');

//          $('#pay_btn_description').html('Submit');

//          voucher_list = '<option value="BPV">Bank Payment Voucher</option><option value="CPV">Cash Payment Voucher</option><option value="JV">Journal Voucher</option>';

//          $('#form_pay_payment').attr("action", "{{url('paycommission')}}");

//      	$('#payment_type').val(data.payment_type);



//          // END COMMISSION SECTION
//      }


//      $('#pay_amount').val(data.amount);
//      $('#payment_type').val(data.payment_type);


//      // $('#payment_type').val(data.installment_type);

//      $('#pay_usertype').val(data.user_type);

//      $('#pay_user').val(data.user);

//      $('input[name="user_name"]').val(data.name);

//      $('select[name=voucher_list]').html(voucher_list);



//      $('#md_pay_payment').modal('show');
//   }

  function compoundType(event,element,key_element)
  {
  	var option=$(element).val();

  	var voucher_type = $('.voucher_type_'+key_element).val();

	var route = null;

		if(option == "General")
		{
			route="{{url('vouchergeneral')}}/"+voucher_type;
            showLoader();

			$.get(route,function(data,status){
			$('#view_data').html(data);
            removeLoader();
			changePaymentMode()
            getBookingListOnCompound(event,key_element);


			});

			$('.account_section').html('');
		}
		else if(option == "Multiple Debit")
		{
			route="{{url('voucherdebit')}}/"+voucher_type;
            showLoader();

			$.get(route,function(data,status){
			$('#view_data').html(data.view);


			$('.account_section').html(data.account_section);
         removeLoader();
		 changePaymentMode()
         getBookingListOnCompound(event,key_element);

		});
		}
		else if(option == "Multiple Credit")
		{
			route="{{url('vouchercredit')}}/"+voucher_type;

            showLoader();

			$.get(route,function(data,status){
			$('#view_data').html(data.view);


			$('.account_section').html(data.account_section);
            removeLoader();
			changePaymentMode()
            getBookingListOnCompound(event,key_element);
		});
		}
		else
		{
			$('#view_data').html('');
			$('.account_section').html('');


		}

  }

  function removeVoucherSection(event,obj)
  {
  	  obj = $(obj).closest('.row').remove();
  }


  function changePaymentMode()
  {
	var voucher_type = $('.voucher_list').val();

	var mode = '';

	if(voucher_type == "CRV" || voucher_type == "CPV")
	{
		mode = 'Cash';
	}

	else if(voucher_type ==  "BRV" || voucher_type == "BPV")
	{
		mode = 'Online';
	}
	else
	{
		mode = 'Cash';
	}
	
	$('.paymentmode_list option').each(function(){
		
		if($(this).html().trim() == mode)
		{
			var uniqid = $(this).closest('select').attr('id').split('_')[2];

			if(mode != "Cash")
			{
				$('.bank_section_'+uniqid).show();
			}
			else{
				$('.bank_section_'+uniqid).hide();

			}
			$(this).prop('selected','selected');
		}
		else
		{
			 $(this).removeAttr('selected');
		}
	});
  }

</script>
