var url = $('meta[name="base-url"]').attr('content');
 $.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

function BureauChange(newBureau, selectedTab)
{
    $('#Search_Bureau').val(newBureau);  //update hidden search object for ajax cals
    $(".bureauTabs.nav.nav-bar li a").removeClass('active');   //set the selected div
    selectedTab.className += " active";

    //change text of dipuste button
    if (newBureau == '1') {
        document.getElementById('btnDispute').innerHTML = "Create Transunion Dispute";
    }
    else if (newBureau == '2') {
        document.getElementById('btnDispute').innerHTML = "Create Equifax Dispute";
    } 
    else if (newBureau == '3') {
        document.getElementById('btnDispute').innerHTML = "Create Experian Dispute";
    }

    $.ajax({
    	type:'get',
    	url:url+'/setDisputeSession/'+ newBureau,
    	success:function()
    	{}
    });
}

function refreshAccounts(openAccount) 
{
	var check = [];
	var dispute = [];
  	$.each($(".AccountStatus:checked"), function(){
        check.push($(this).val());
    });

    $.each($(".DisputeStatus:checked"), function(){
    	dispute.push($(this).val());
    });
  
	$.ajax({
		type:'post',
		url:url+'/getAccountSummary' ,
		data:{'id':$('#Search_Bureau').val(),'status':check,'dispute':dispute},
		dataType:'json',
		success:function(res)
		{
			console.log(res);
			var acc = '';
			$('.tranCount').text(res.length);
			if(res.length>0)
			{
				for(var i=0;i<res.length;i++)
				{
				 	var acc_status = '';
				 	var color = '';
				 	var open_date = '';
				 	var table = '';

				 	if(res[i].dateOpened!=null)
				 	{
				 		open_date = new Date(res[i].dateOpened);
				 		open_date = open_date.getDate()  + '/'+ (open_date.getMonth()+1) + '/' + open_date.getFullYear(); 
				 	}
				 	var account = '';
				 	if(res[i].accountNumber!=null)
				 	{
					 	account = res[i].accountNumber.toString();
					 	account = account.replace(/\d{4}$/, '****');				 		
				 	} 

				 	var tr = '';
				 	if(res[i].disputeData!=null && res[i].disputeData!='')
		 			{
		 				var data = res[i].disputeData;
		 				for(var j=0;j<data.length;j++)
		 				{
			 				var dl_date = new Date(data[j].created);
			 				dl_date = (dl_date.getMonth()+1)+ '/' + dl_date.getDate() + '/' + dl_date.getFullYear();
			 				if(data[j].status==1){var dl = 'Pending';var content='Disputes letters created but not marked as sent yet.';}else if(data[j].status==2){var dl = 'Sent';var content='Dispute letters sent to credit bureaus but still awaiting results.';}else if(data[j].status==3){var dl = 'Completed';var content='Dispute letters sent to credit bureaus and results have been received.';}
			 				tr += '<tr>'+
					                	'<td data-label="Dispute" style="width:10%;"> Round '+data[j].round+'</td>'+
					                	'<td data-label="'+dl+'" style="width:20%;">'+
						                    '<div class="d-flex align-items-center">'+
						                        '<span>'+dl_date+'</span>'+
						                        '<a class="btn-border btn btn-icon btn-xs ml-2" data-class="dynamic-class" data-content="'+content+'" data-placement="top" data-toggle="popover" data-trigger="hover" href="javascript:;" title="" rel="popover" data-original-title="'+dl+'"><i class="fa fa-info"></i></a>'+
						                    '</div>'+
						                '</td>'+
					                	'<td data-label="Reason" style="width:20%;"><span>'+data[j].subject+'</span></td>'+
					                	'<td class="font-weight-normal"> '+
					                		'<span>'+data[j].reason+'</span>'+
					                	'</td>'+
					            	'</tr>';
		 				}
		 			}

				 	if(res[i].AccountCondition_abbreviation=='Open' && res[i].AccountCondition_symbol=='O')
				 	{
				 		acc_status = 'Positive';
				 		color = 'success';
				 		table = '<div class="row table-data">'+
							    	'<div class="col-md-6">'+
							        	'<table class="table-sm table-striped table-data__content">'+
							            	'<tbody>'+
								            	'<tr>'+
								                	'<th>Account #</th>'+
								                	'<td>'+account+'</td>'+
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>Status</th>'+
								                	'<td>'+res[i].OpenClosed_description+'</td>'+
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>Credit Limit</th>'+
								                	'<td> $'+res[i].GrantedTrade_CreditLimit+'  </td>'+
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>High Balance</th>'+
								                	'<td>$'+res[i].highBalance+'  </td>'+
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>Payment Amount</th>'+
								                	'<td> $'+res[i].GrantedTrade_monthlyPayment+'</td>'+ 
								            	'</tr>'+
								            	'<tr>'+
								                	'<th>Last Payment</th>'+
								                	'<td>6/26/2020  </td>'+
								            	'</tr>'+
								              	'<tr>'+
								                	'<th>Term</th>'+
								                	'<td>'+res[i].GrantedTrade_termMonths+'</td>'+
								            	'</tr>'+
								            	'<tr>'+
						                			'<th>Description</th>'+
				                					'<td>'+res[i].AccountDesignator_description+'</td>'+
				            					'</tr>'+  
				        					'</tbody>'+
				        				'</table>'+
				    				'</div>'+
								    '<div class="col-md-6 table-data__table">'+
								        '<table class="table-sm table-striped table-data__content">'+
								           '<tbody>'+
									           	'<tr>'+
									                '<th> Open Date</th>'+
									                '<td>'+open_date+'</td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th> Closed Date</th>'+
									                '<td></td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Last Reported</th>'+
									                '<td>7/2/2020 </td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Days Late</th>'+
									                '<td> 30: <span class=""> '+res[i].GrantedTrade_late90Count+' </span>'+
									                	'| 60: <span class=" "> '+res[i].GrantedTrade_late60Count+' </span>'+
									                	'| 90: <span class=" "> '+res[i].GrantedTrade_late30Count+' </span>'+ 
									                '</td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Payment Status</th>'+
									                '<td class=" ">'+res[i].PayStatus_abbreviation+'</td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Past Due</th>'+
									                '<td class=" "> $'+res[i].GrantedTrade_amountPastDue+'</td>'+
									            '</tr>'+
									          	'<tr>'+
									                '<th>Worst Rating</th>'+
									                '<td class=""> </td>'+
									            '</tr>'+
									            '<tr>'+
									                '<th>Remarks</th>'+
									                '<td class="">'+res[i].remark_description+'</td>'+
									           	'</tr>'+           
			        						'</tbody>'+
			        					'</table>'+
			    					'</div>'+
								'</div>';
				 	}
				 	else if(res[i].AccountCondition_abbreviation=='Derog' && res[i].AccountCondition_symbol=='F')
				 	{
				 		acc_status = 'Negative';
				 		color = 'danger';
				 		table = '<div class="row table-data">'+
								    '<div class="col-md-6">'+
								        '<table class="table-sm table-striped table-data__content">'+
								            '<tbody>'+
								            	'<tr> '+
								            		'<th>Account #</th>'+
								            		'<td>'+account+'</td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Status</th>'+
								            		'<td>'+res[i].OpenClosed_description+'</td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Original Creditor</th>'+
								            		'<td>CONWAY HOSPITAL</td> '+
								            	'</tr>'+
								            	'<tr>'+
								            		'<th>Creditor Type</th>'+
								            		'<td>Collection Services</td> '+
								            	'</tr>'+
								        	'</tbody>'+
								    	'</table>'+
								    '</div>'+
								    '<div class="col-md-6 table-data__table">'+
								        '<table class="table-sm table-striped table-data__content">'+
								            '<tbody>'+
								            	'<tr> '+
								            		'<th>High Balance</th>'+
								            		'<td>$'+res[i].highBalance+'</td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Open Date</th>'+
								            		'<td>'+open_date+'</td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Last Reported</th>'+
								            		'<td>6/24/2018  </td> '+
								            	'</tr>'+
								            	'<tr> '+
								            		'<th>Remarks</th>'+
								            		'<td>'+res[i].remark_description+'</td> '+
								            	'</tr>'+
									        '</tbody>'+
									    '</table>'+
								    '</div>'+
								'</div>'+
								'<table class="table-accordion__footer" width="100%">'+
						            '<tbody id="disTr">'+tr+'</tbody>'+
						        '</table>';
				 	}

				 var id = $('#disputeId').val();
				 var checked = '';
				 var dis_status = '';
				 var round = 'N/A';
				 if(id==res[i].trans_id){var checked = 'checked';}
				 if(res[i].dispute_letter!=null && res[i].dispute_letter!='')
			 	 {
			 	 	round = 'Round '+res[i].dispute_letter.round;
			 		if(res[i].dispute_letter.status==1){dis_status='Pending';}else if(res[i].dispute_letter.status==2){dis_status='Sent';}else if(res[i].dispute_letter.status==3){dis_status='Completed';}
			 	 }

				  acc+='<tr class="table-accordion__header tr--'+color+' disAcc'+i+'">'+
				  			'<td class="td-checkbox">'+
                                '<div class="custom-control custom-checkbox Dispute-Checkbox">'+
                                    '<input class="custom-control-input" type="checkbox" value="'+res[i].trans_id+'" name="transDispute[]" '+checked+'>'+
                                    '<label class="custom-control-label"></label>'+
                                '</div>'+
                            '</td>'+
							'<td data-label="Credit Card " class="text-uppercase label--">'+res[i].creditorName+'</td>'+
							'<td data-label="BALANCE">$'+res[i].currentBalance+'</td>'+
							'<td data-label="ACCOUNT STATUS" class="text-uppercase">'+acc_status+'</td>'+
		                	'<td class="d-none  btnDisputeReason">'+
		                        '<button type="button" class="btn btn-link p-0 btn-font--wider" onclick="openDisputeReasonPopUp(\''+res[i].trans_id+'\',\''+res[i].bureau+'\',"", 0,edit); ">'+
		                            'Add Dispute Reason'+
		                        '</button>'+
		                	'</td>'+
		                    '<td class="d-none  btnRemoveFromDispute">'+
		                        '<button class="btn btn-link btn-gray btn-icon" onclick="RemoveAccountFromDispute(\''+i+'\', \''+res[i].trans_id+'\')" type="button"><i class="fa fa-trash" aria-hidden="true"></i></button>'+
		                    '</td>'+
		                    '<td data-label="DISPUTE STATUS" class="">'+round+' '+ dis_status+ 
		                    '</td>'+
		                    '<td>'+
		                        '<div class="table-accordion__icon collapsed" data-toggle="collapse" href="#collapse-Accounts-'+i+'" aria-controls="#collapse-Accounts-'+i+'">'+
		                            '<i class="fa fa-plus"></i>'+
		                            '<i class="fa fa-minus"></i>'+
		                        '</div>'+
		                	'</td>'+
		                '</tr>'+
		                '<tr class="table-accordion__content disAcc'+i+'">'+
		                    '<td colspan="7">'+
		                        '<label class="checkbox-on" for="check-Accounts-'+i+'"> </label>'+
		                        '<div name="DIV_'+res[i].trans_id+'" class="collapse table-accordion__card table-accordion__card--'+color+'" id="collapse-Accounts-'+i+'">'+
									table +
		                        '</div>'+
		                    '</td>'+
		                '</tr>';
				}
			}
			$('#actb').html(acc);
			setTimeout(function(){
				$('a[data-toggle="popover"]').popover();				
			},3000);
		}
	});
}

function mobileAllSelected(group) 
{
    if (document.getElementById("AllPill-" + group).checked) {
        document.getElementById("AllPill-label-" + group).innerHTML = ""
        document.getElementById("checkbox-label-" + group + "-All").checked = true;
        FilterUpdateSelected(group, "All")
    } else {
        document.getElementById("AllPill-label-" + group).innerHTML = "Clear Filters";
        document.getElementById("checkbox-label-" + group + "-All").checked = false;
        FilterUpdateSelected(group, "All")
    }

}

function FilterUpdateSelected(group, labelValue) {
            var itemsInGroup = $("." + group);
            var allItem;
            itemsInGroup.each(function () {
                var item = this;
                $.each(this.attributes, function () {
                    if (this.name == 'labelvalue') {
                        if (this.value == "All") {
                            //allItem.checked = false;
                            allItem = item;
                        }
                    }
                });
            });

            //when all is selected unselect everything in that group
            if (labelValue == "All") {
                itemsInGroup.prop("checked", false)
                allItem.checked = false; //recheck the all
                //update the text of label for mobile to say All Selected
                 $(".check-all #AllPill-label-" + group ).text('') //for mobile update 
               
            } else {
                //when anything but all is selected unselect all
                allItem.checked = false;
                 $(".check-all #AllPill-label-" + group ).text('Clear Filters') //for mobile update 
                
            }

            //if nothing is checked in the group recheck all
            if ($('.' + group + ':checked').length == 0) {
                allItem.checked = true;
                $(".check-all #AllPill-label-" + group ).text('') //for mobile update 
            }
        }

$('.Disputenext').click(function(){
	var type = $('#dispute_type').val();
	var step=$(this).attr('data-step');
	if(step=='first'){
		var trans=[];
		$('input[name="transDispute[]"]:checked').each(function(){trans.push($(this).val())});
		// console.log(trans);
		if(trans.length==0){
			alert('Please Select transaction for dispute.');
			return false;
		}
		$.ajax({
			url:url+'/getDispute',
			data:{'trans':trans,'type':type},
			type:'post',
			async:false,
			success:function(res)
			{
				console.log(res);
				var acc_disp = '';
				$('#acc_count').text(res.length);
				if(res.length>0)
				{
					for(var i=0;i<res.length;i++)
					{
					 	var acc_status = '';
					 	var color = '';
					 	var open_date = '';
					 	var table = '';
					 	var tr = '';
	
					 	if(res[i].dateOpened!=null)
					 	{
					 		open_date = new Date(res[i].dateOpened);
					 		open_date = open_date.getDate()  + '/'+ (open_date.getMonth()+1) + '/' + open_date.getFullYear(); 
					 	}
					 	var account = '';
					 	if(res[i].accountNumber!=null)
					 	{
						 	account = res[i].accountNumber.toString();
						 	account = account.replace(/\d{4}$/, '****');				 		
					 	} 

			 			if(res[i].disputLetter!=null && res[i].disputLetter!='')
			 			{
			 				var data = res[i].disputLetter;
			 				for(var j=0;j<data.length;j++)
			 				{
				 				var dl_date = new Date(data[j].created);
				 				dl_date = (dl_date.getMonth()+1)+ '/' + dl_date.getDate() + '/' + dl_date.getFullYear();
				 				if(data[j].status==1){var dl = 'Pending';var content='Disputes letters created but not marked as sent yet.';}else if(data[j].status==2){var dl = 'Sent';var content='Dispute letters sent to credit bureaus but still awaiting results.';}else if(data[j].status==3){var dl = 'Completed';var content='Dispute letters sent to credit bureaus and results have been received.';}
				 				tr += '<tr>'+
						                	'<td data-label="Dispute" style="width:10%;"> Round '+data[j].round+'</td>'+
						                	'<td data-label="'+dl+'" style="width:20%;">'+
							                    '<div class="d-flex align-items-center">'+
							                        '<span>'+dl_date+'</span>'+         
							                        '<a class="btn-border btn btn-icon btn-xs ml-2" data-class="dynamic-class" data-content="'+content+'" data-placement="top" data-toggle="popover" data-trigger="hover" href="javascript:;" title="" rel="popover" data-original-title="'+dl+'"><i class="fa fa-info"></i></a>'+
							                    '</div>'+
							                '</td>'+
						                	'<td data-label="Reason" style="width:20%;"><span>'+data[j].subject+'</span></td>'+
						                	'<td class="font-weight-normal"> '+
						                		'<span>'+data[j].reason+'</span>'+
						                	'</td>'+
						            	'</tr>';
			 				}
			 			}
	
					 	if(res[i].AccountCondition_abbreviation=='Open' && res[i].AccountCondition_symbol=='O')
					 	{
					 		acc_status = 'Positive';
					 		color = 'success';
					 		table = '<div class="row table-data">'+
								    	'<div class="col-md-6">'+
								        	'<table class="table-sm table-striped table-data__content">'+
								            	'<tbody>'+
									            	'<tr>'+
									                	'<th>Account #</th>'+
									                	'<td>'+account+'</td>'+
									            	'</tr>'+
									            	'<tr>'+
									                	'<th>Status</th>'+
									                	'<td>'+res[i].OpenClosed_description+'</td>'+
									            	'</tr>'+
									            	'<tr>'+
									                	'<th>Credit Limit</th>'+
									                	'<td> $'+res[i].GrantedTrade_CreditLimit+'  </td>'+
									            	'</tr>'+
									            	'<tr>'+
									                	'<th>High Balance</th>'+
									                	'<td>$'+res[i].highBalance+'  </td>'+
									            	'</tr>'+
									            	'<tr>'+
									                	'<th>Payment Amount</th>'+
									                	'<td> $'+res[i].GrantedTrade_monthlyPayment+'</td>'+ 
									            	'</tr>'+
									            	'<tr>'+
									                	'<th>Last Payment</th>'+
									                	'<td>6/26/2020  </td>'+
									            	'</tr>'+
									              	'<tr>'+
									                	'<th>Term</th>'+
									                	'<td>'+res[i].GrantedTrade_termMonths+'</td>'+
									            	'</tr>'+
									            	'<tr>'+
							                			'<th>Description</th>'+
					                					'<td>'+res[i].AccountDesignator_description+'</td>'+
					            					'</tr>'+  
					        					'</tbody>'+
					        				'</table>'+
					    				'</div>'+
									    '<div class="col-md-6 table-data__table">'+
									        '<table class="table-sm table-striped table-data__content">'+
									           '<tbody>'+
										           	'<tr>'+
										                '<th> Open Date</th>'+
										                '<td>'+open_date+'</td>'+
										            '</tr>'+
										            '<tr>'+
										                '<th> Closed Date</th>'+
										                '<td>   </td>'+
										            '</tr>'+
										            '<tr>'+
										                '<th>Last Reported</th>'+
										                '<td>7/2/2020   </td>'+
										            '</tr>'+
										            '<tr>'+
										                '<th>Days Late</th>'+
										                '<td> 30: <span class=""> '+res[i].GrantedTrade_late90Count+' </span>'+
										                	'| 60: <span class=" "> '+res[i].GrantedTrade_late60Count+' </span>'+
										                	'| 90: <span class=" "> '+res[i].GrantedTrade_late30Count+' </span>'+ 
										                '</td>'+
										            '</tr>'+
										            '<tr>'+
										                '<th>Payment Status</th>'+
										                '<td class="">'+res[i].PayStatus_abbreviation+'</td>'+
										            '</tr>'+
										            '<tr>'+
										                '<th>Past Due</th>'+
										                '<td class=""> $'+res[i].GrantedTrade_amountPastDue+'</td>'+
										            '</tr>'+
										          	'<tr>'+
										                '<th>Worst Rating</th>'+
										                '<td class=""> </td>'+
										            '</tr>'+
										            '<tr>'+
										                '<th>Remarks</th>'+
										                '<td class="">'+res[i].remark_description+'</td>'+
										           	'</tr>'+           
				        						'</tbody>'+
				        					'</table>'+
				    					'</div>'+
									'</div>';
					 	}
					 	else if(res[i].AccountCondition_abbreviation=='Derog' && res[i].AccountCondition_symbol=='F')
					 	{
					 		acc_status = 'Negative';
					 		color = 'danger';
					 		table = '<div class="row table-data">'+
									    '<div class="col-md-6">'+
									        '<table class="table-sm table-striped table-data__content">'+
									            '<tbody>'+
									            	'<tr> '+
									            		'<th>Account #</th>'+
									            		'<td>'+account+'</td> '+
									            	'</tr>'+
									            	'<tr> '+
									            		'<th>Status</th>'+
									            		'<td>'+res[i].OpenClosed_description+'</td> '+
									            	'</tr>'+
									            	'<tr> '+
									            		'<th>Original Creditor</th>'+
									            		'<td>CONWAY HOSPITAL</td> '+
									            	'</tr>'+
									            	'<tr>'+
									            		'<th>Creditor Type</th>'+
									            		'<td>Collection Services</td> '+
									            	'</tr>'+
									        	'</tbody>'+
									    	'</table>'+
									    '</div>'+
									    '<div class="col-md-6 table-data__table">'+
									        '<table class="table-sm table-striped table-data__content">'+
									            '<tbody>'+
									            	'<tr> '+
									            		'<th>High Balance</th>'+
									            		'<td>$'+res[i].highBalance+'</td> '+
									            	'</tr>'+
									            	'<tr> '+
									            		'<th>Open Date</th>'+
									            		'<td>'+open_date+'</td> '+
									            	'</tr>'+
									            	'<tr> '+
									            		'<th>Last Reported</th>'+
									            		'<td>6/24/2018  </td> '+
									            	'</tr>'+
									            	'<tr> '+
									            		'<th>Remarks</th>'+
									            		'<td>'+res[i].remark_description+'</td> '+
									            	'</tr>'+
										        '</tbody>'+
										    '</table>'+
									    '</div>'+
									'</div>'+
									'<table class="table-accordion__footer" width="100%">'+
							            '<tbody>'+tr+'</tbody>'+
							        '</table>';
					 	}

					 	var addDRbtn='Add Dispute Reason <i class="btn-warning fa fa-exclamation-circle" aria-hidden="true"></i>'; var dispId=0;var round=0;$('.action-button.Disputenext').attr('disabled','disabled');$('.action-button.Disputenext').attr('style','background: #0573da59;'); 
					 	if(res[i].desp!=null && res[i].desp!='')
					 	{
					 		addDRbtn='View/Edit Dispute Reason <i class="fa fa-check"></i>';dispId=res[i].desp.id;round=res[i].desp.round;$('.action-button.Disputenext').prop('disabled',false);$('.action-button.Disputenext').attr('style','background: #0573da;');
							if(res[i].bureau=='1'){var label = 'Transunion';}else if(res[i].bureau=='2'){var label = 'Experian';}else{var label = 'Equifax';}
							$('#fintext').html(label+' Round '+round);		 		
					 	}
					 	
					  acc_disp+='<tr class="table-accordion__header tr--'+color+'">'+
									'<td data-label="Credit Card " class="text-uppercase label--">'+res[i].creditorName+'</td>'+
									'<td data-label="BALANCE">$'+res[i].currentBalance+'</td>'+
									'<td data-label="ACCOUNT STATUS" class="text-uppercase">'+acc_status+'</td>'+
				                	'<td class="btnDisputeReason">'+
				                        '<button type="button" class="btn btn-link p-0 btn-font--wider" id="disbtn" onclick="openDisputeReasonPopUp(\''+res[i].trans_id+'\',\''+res[i].creditorName+'\',\''+res[i].currentBalance+'\',\''+acc_status+'\','+dispId+','+res[i].bureau+','+round+'); ">'+
				                            addDRbtn+
				                        '</button>'+
				                	'</td>'+
				                    '<td class="btnRemoveFromDispute">'+
				                        '<button class="btn btn-link btn-gray btn-icon" type="button"><i class="fa fa-trash" aria-hidden="true"></i></button>'+
				                    '</td>'+
				                    '<td data-label="DISPUTE STATUS" class="d-none">'+
				                        'N/A'+
				                    '</td>'+
				                    '<td>'+
				                        '<div class="table-accordion__icon collapsed" data-toggle="collapse" href="#collapse-Accounts-'+i+'" aria-controls="#collapse-Accounts-'+i+'">'+
				                            '<i class="fa fa-plus"></i>'+
				                            '<i class="fa fa-minus"></i>'+
				                        '</div>'+
				                	'</td>'+
				                '</tr>'+
				                '<tr class="table-accordion__content">'+
				                    '<td colspan="7">'+
				                        '<label class="checkbox-on" for="check-Accounts-'+i+'"> </label>'+
				                        '<div name="DIV_'+res[i].trans_id+'" class="collapse table-accordion__card table-accordion__card--'+color+'" id="collapse-Accounts-'+i+'">'+
											table +
				                        '</div>'+
				                    '</td>'+
				                '</tr>';
					}
				}
				$('#credis').html(acc_disp);
				setTimeout(function(){
					$('a[data-toggle="popover"]').popover();				
				},3000);
			}
		});
	}


  if(animating) return false;
  animating = true;
  
  current_fs = $(this).parent().parent();
  next_fs = $(this).parent().parent().next();
  // console.log(current_fs,next_fs);
  //activate next step on progressbar using the index of next_fs
  $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");
  
  //show the next fieldset
  next_fs.show(); 
  //hide the current fieldset with style
  current_fs.animate({opacity: 0}, {
    step: function(now, mx) {
      //as the opacity of current_fs reduces to 0 - stored in "now"
      //1. scale current_fs down to 80%
      scale = 1 - (1 - now) * 0.2;
      //2. bring next_fs from the right(50%)
      left = (now * 50)+"%";
      //3. increase opacity of next_fs to 1 as it moves in
      opacity = 1 - now;
      current_fs.css({
        // 'transform': 'scale('+scale+')',
        // 'position': 'absolute'
      });
      next_fs.css({'left': left, 'opacity': opacity});
    }, 
    duration: 800, 
    complete: function(){
      current_fs.hide();
      animating = false;
    }, 
    //this comes from the custom easing plugin
    easing: 'easeInOutBack'
  });
});

$(".previous").click(function() {
  if (animating) return false;
  animating = true;

  current_fs = $(this).parent().parent();
  previous_fs = $(this).parent().parent().prev();

  //de-activate current step on progressbar
  $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

  //show the previous fieldset
  previous_fs.show();
  //hide the current fieldset with style
  current_fs.animate({
    opacity: 0
  }, {
    step: function(now, mx) {
      //as the opacity of current_fs reduces to 0 - stored in "now"
      //1. scale previous_fs from 80% to 100%
      scale = 0.8 + (1 - now) * 0.2;
      //2. take current_fs to the right(50%) - from 0%
      left = ((1 - now) * 50) + "%";
      //3. increase opacity of previous_fs to 1 as it moves in
      opacity = 1 - now;
      current_fs.css({
        'left': left
      });
      previous_fs.css({
        'opacity': opacity
      });
    },
    duration: 800,
    complete: function() {
      current_fs.hide();
      animating = false;
    },
    //this comes from the custom easing plugin
    easing: 'easeInOutBack'
  });
});

function openDisputeReasonPopUp(trans_id,name,balance,status,dispId,bureau,round='')
{
	$('#trans_id').val(trans_id);
	$('#bureau').val(bureau);
	$('#disId').val(dispId);
	$('#col_name').text(name);
	$('#col_bal').html('$'+balance);
	$('#col_status').text(status);

	$.ajax({
		type:'get',
		url:url+'/getDisputeReasons/'+dispId,
		dataType:'json',
		success:function(res)
		{
			// console.log(res);
			var reason = '';
			if(res.dr.length>0)
			{
				res1=res.dr;
				reason += '<option value="" data-reason="">Suggested Reason</option>';
				var select='';
				for(var i=0;i<res1.length;i++)
				{
					reason += '<option value='+res1[i].id+' data-reason="'+res1[i].reason+'">'+res1[i].subject+'</option>';
				}

				if(res.reason.length>0)
				{
					res2 = res.reason;
					for(var i=0;i<res2.length;i++)
					{
						var date = new Date(res2[i].created);
						date = (date.getMonth()+1) + '/' + date.getDate()  + '/'+ date.getFullYear();
						if(res2[i].bureau=='1'){var label = 'TRU';}else if(res2[i].bureau=='2'){var label = 'EXP';}else{var label = 'EQU';}
						reason += '<option value='+res2[i].dispute_reason_id+' data-reason="'+res2[i].custom+'">'+label+' Dispute Reason ('+date+')</option>';
					}
				}
			}
			$('select[name="selectedValue"]').html(reason);
			if(res.disp!=''){
				var disp=res.disp;
				$('select[name="selectedValue"]').val(disp.dispute_reason_id).trigger('change');
			}else{
				$('select[name="selectedValue"]').val('').trigger('change');
			}
			if(bureau=='1'){var label = 'Transunion';}else if(bureau=='2'){var label = 'Experian';}else{var label = 'Equifax';}
			$('#fintext').html(label+' Round '+round);
		}
	});

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

function openDisputePopUp(dispId)
{
	$.ajax({
		type:'get',
		url:url+'/getDisputeReasons/'+dispId,
		dataType:'json',
		success:function(res)
		{
			var reason = '';
			if(res.dr.length>0)
			{
				res1=res.dr;
				reason += '<option value="" data-reason="">Suggested Reason</option>';
				var select='';
				for(var i=0;i<res1.length;i++)
				{
					reason += '<option value='+res1[i].id+' data-reason="'+res1[i].reason+'">'+res1[i].subject+'</option>';
				}
			}
			$('select[name="selectedValue"]').html(reason);
			if(res.disp!=''){
				var disp=res.disp;
				$('select[name="selectedValue"]').val(disp.dispute_reason_id).trigger('change');
			}else{
				$('select[name="selectedValue"]').val('').trigger('change');
			}
		}
	});
	$('#myModal1').modal('show');	
}

function showReason(id)
{
	var text = $('select[name="selectedValue"] option:selected').attr('data-reason');
	$('#txtDisputeText').text(text);
}

function saveReason()
{
	if($('select[name="selectedValue"]').val()=='')
	{
		errMessage('errreason','Please select reason.');
        return false;	
	}

	if($('#txtDisputeText').val()=='')
	{
		errMessage('errtext','Please enter text.');
        return false;	
	}

	$.ajax({
		type:'post',
		url:url+'/saveDisputeReason',
		data:$('#DisputeReasonForm').serialize(),
		dataType:'json',
		success:function(res)
		{
			// console.log(res);
			$('#disId').val(res.id);
			$('#myModal1').modal('hide');
			$('.action-button.Disputenext').attr('style','background: #0573da;');
			$('.action-button.Disputenext').prop('disabled',false);
			if(res.bureau=='1'){var label = 'Transunion';}else if(res.bureau=='2'){var label = 'Experian';}else{var label = 'Equifax';}
			$('#fintext').html(label+' Round '+res.round);
			$('.btnDisputeReason').html('<button type="button" class="btn btn-link p-0 btn-font--wider" id="disbtn" onclick="openDisputePopUp(\''+res.id+'\'); ">View/Edit Dispute Reason<i class="btn-warning fa fa-exclamation-circle" aria-hidden="true"></i></button>');
		}
	});
}

$(document).on('click','table .fa-trash',function(){
	$(this).parent().parent().parent().remove();
	$('#acc_count').text($('#credis .table-accordion__header').length);
	if($('#credis .table-accordion__header').length==0)
	{
		window.location.href = url+'/account_summary';
	}
});

function editLetter(trans_id)
{
	$('#dispute_type').val('');	
	localStorage.setItem('dispute_letter','edit');
	window.location = url+'/create_dispute?disputeId='+trans_id;
}

function closePopup()
{
	$('#dispute_type').val('new');
	$('#myModal').modal('hide');
}

$(document).ajaxStart(function() {
  $("#loading").fadeIn('200');
}).ajaxStop(function() {
  $("#loading").fadeOut('200');
});