$(document).ready(function(){
	$("#invoice_details_CN").hide();
	
	 $('#CN_selectinvoice').select2({
      ajax: {
        url: url_invoice_for_s2,
        type: "get",
        dataType: 'json',
        delay: 100,
        data: function (params) {
           return {
              searchTerm: params.term // search term
           };
        },
        processResults: function (response) {
           return {
              results: response
                   
           };		   
		 },
        cache: true
      }
 });
 
  $('#CN_selectinvoice').on('select2:select', function (e) {
   var invoice_id = e.params.data.id;
   var option="";
    
   $.ajax(url_getinvoicedetail,{
      type : 'post',
      data :{'invoice_id' : invoice_id} ,
      success : function(data){

         $("#invoicetablebody").empty();
         $("#invoicetablefoot").empty();

         piaddresses(data);
         pidetails(data);
         bankdetailsforinvoice(data.bank);
         $(document).find('#invoice_picompanycode').val(data.company.company_code);
         $(document).find('#invoice_picompanyname').val(data.company.name);
         $(document).find('#invoice_pigstno').val(data.company.gstin);
         
         $('#invoice_details_CN').show();
         
      },
      error: function (jqXhr, textStatus, errorMessage) {
         console.log('Error' + errorMessage);
      }
   });
});
	
	$('#credit_note_create_dialog').css("width","800px");

	$('.credit_note_createbtn').click(function(){
		//generateCreditNote_NO();
	  $("#credit_note_create_modal").modal('show');
	});
	credit_note_modal();
}); 


	function credit_note_modal()
  	{
		$("#credit_note_create_modal").each(function() {
  
			var element = this;
	
	  		var pages = $(this).find('.modal-split');
  
  console.log(pages);
			if (pages.length != 0)
			
			{
		  		pages.hide();
		  		pages.eq(0).show();
   
		  		var b_button = document.createElement("button");
				b_button.setAttribute("type","button");
				b_button.setAttribute("class","btn btn-primary pull-left");
				b_button.setAttribute("style","display: none;");
				b_button.innerHTML = "Back";
  
		  		var n_button = document.createElement("button");
				n_button.setAttribute("type","button");
				n_button.setAttribute("class","btn btn-primary pull-right testbutton");
				n_button.innerHTML = "Next";
  
		  		$(this).find('.modal-footer').append(b_button).append(n_button);
  
  
		  		var page_track = 0;
  
		  		$(n_button).click(function() {
		  
		  			this.blur();

			  		if(page_track == 0)
			  		{
				  		$(b_button).show();
			  		}

  
			  		if(page_track == pages.length-2)
			  		{								
							$('#quotationselectblock').addClass("hide"); 
							n_button.setAttribute("type","button");  
							n_button.setAttribute("class","btn btn-success pull-right");
							$(n_button).text("Submit");
							
					} 

			  		if(page_track == pages.length-1)
			  		{
						saveinvoice();
			
			  		}
			  
					if(page_track < pages.length-1)
			  		{
				  		page_track++;
						pages.hide();
				  		pages.eq(page_track).show();
			  		}
				
				});
  
		  		$(b_button).click(function() {
  
			  		if(page_track == 1)
			  		{
				  		$(b_button).hide();
			  		}
  
			  		if(page_track == pages.length-3)
			  		{
			
			  		}
			  
					if(page_track == pages.length-1)
			  		{
				
						n_button.removeAttribute("style","display:none;");
						n_button.removeAttribute("type","submit");  
						n_button.removeAttribute("class","btn btn-success");
						n_button.setAttribute("type","button");  
						n_button.setAttribute("class","btn btn-primary testbutton pull-right");
				  		$(n_button).text("Next");
			  		}
  
			  		if(page_track > 0)
			  		{
				  
						page_track--;
						pages.hide();
				  		pages.eq(page_track).show();
			  		}
				
				});
			
			}
  
		});
  	}