$('#add_new_postdate_checks_equity').on('click',function(e){
	e.preventDefault(); 
	 
	$('#pdc_date').val('');
	$('#pdc_bank').val('');
	$('#pdc_ck').val('');
	$('#pdc_amount').val('');
	$('#pdc_remarks').val('');	  
	$('.pdc_mode').prop('checked', false);
	  
	$("#pdc_transcode_select").select2('val',"");
	$('#pdc_ck').attr('readonly',false);
	$('#pdc_bank').attr('readonly',false);
	$('#pdc_no_cheque').attr('readonly',false);

	$('.postdated_cheque_modal').modal('show');
});

$('#pdc_cancel').click(function(e){
	e.preventDefault(); 
	var save_message 	= "Are you sure you want to cancel this record?";
	bootbox.dialog({
	  message: save_message, 
	  buttons: {
		success: {
		  label: "Yes",
		  className: "btn-success",
		  callback: function() {
			$('#pdc_date').val('');
			$('#pdc_bank').val('');
			$('#pdc_ck').val('');
			$('#pdc_amount').val('');
			$('#pdc_remarks').val('');
			$('#pdc_transcode_select').val('');
			$('.pdc_mode').val('');  
			$('.postdated_cheque_modal').modal('hide');
		  }
		},
		danger: {
		  label: "No",
		  className: "btn-danger" 
		}
	}
	});
});

var mods 					=	$('#main_page').val();
var types 					=	$('#main_type').val(); 
var mainrid					=	$('#main_rid').val();

if(mods=="payment" && types=="postdated_checks_received"){
	loadTotalCheque(mainrid);
}

function loadTotalCheque(mainrid){
				var dataString				=	"mainrid="+encodeURIComponent(mainrid);
				$.post('./ajax/compute_check_total.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								var total = obj.output; 
								if(total=="" || total==0 || total==null){
								$('#cheque_total').val(0.00);
								}else{
								$('#cheque_total').val(Comma(total.toFixed(2)));
								}
						});
}
 
//SAVING PDC
function save_pdc(){ 
				var mods 					=	$('#main_page').val();
				var types 					=	$('#main_type').val(); 
				var action 					=	$('#pdc_action').val();
				var mainrid					=	$('#main_rid').val();
				var mainid					=	$('#pdc_id').val();

				var pdc_transcode 			=   $('#pdc_transcode_select').val();

				var pdc_details				=	$('#postdated_checks_received_form').serialize();
				var dataString				=	pdc_details+"&action="+encodeURIComponent(action)+"&mainrid="+encodeURIComponent(mainrid)+"&mainid="+encodeURIComponent(mainid);

				
				if(pdc_transcode==""){
					bootbox.alert("Transcode is required.");
				}else{
					$.post('./ajax/postdated_checks_received.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									bootbox.alert("Successfully Saved ...");  
									window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types+'&rid='+mainrid; }, 1000);		
								}else{
									bootbox.alert("Failed in saving.");
								}
					});
				}
						
}


$('.pdc_mode').change(function(){
	var pdc_mode = $("input[name='pdc_mode']:checked").val(); 
	if(pdc_mode=="cash"){
		$('#pdc_ck').attr('readonly',true);
		$('#pdc_bank').attr('readonly',true);
		$('#pdc_no_cheque').attr('readonly',true);
	}else{
		$('#pdc_ck').attr('readonly',false);
		$('#pdc_bank').attr('readonly',false);
		$('#pdc_no_cheque').attr('readonly',false);
	}  
});



$('.replace_cash').hide();
		$('.replace_cheque').hide();
$('#pdc_replace_option').change(function(){
	var pdc_mode = $("#pdc_replace_option").val(); 
		 
	if(pdc_mode=="cash"){
		$('.replace_cash').show();
		$('.replace_cheque').hide();
	}else if(pdc_mode=="cheque"){
		$('.replace_cash').hide();
		$('.replace_cheque').show();
	}else{
		$('.replace_cash').hide();
		$('.replace_cheque').hide();
	}  
});
$('.hidecheckno').show();
function updatePDC(reserveId,Id){
	$('#main_rid').val(reserveId);
	$('#pdc_id').val(Id);
	$('#pdc_action').val('edit'); 
	$('.hidecheckno').hide();

	var dataString		=	"mainid="+encodeURIComponent(Id)+"&mainrid="+encodeURIComponent(reserveId); 
	callModal('.postdated_cheque_modal');
						$.post('./ajax/load_pdc.php', dataString, function(msg){ 
								var obj	= $.parseJSON(msg); 
								var cheque_date			=	obj[0].ChequeDate;
								var cheque_no			=	obj[0].ChequeNo;
								var bank				=	obj[0].Bank;
								var amount				=	Comma(obj[0].Amount);
								var remarks				=	obj[0].Remarks;
								var modepayment			=	obj[0].ModePayment;
								var transcode			=	obj[0].Transcode;
								
								
								$('#pdc_date').val(cheque_date);
								$('#pdc_ck').val(cheque_no);
								$('#pdc_bank').val(bank);
								$('#pdc_amount').val(amount);
								$('#pdc_remarks').val(remarks); 
								$('#pdc_transcode_select').val(transcode); 
								$('#pdc_transcode_select').select2('val',transcode);

								if(modepayment=="cash"){
									$("#pdc_mode_cash").prop("checked", true);
									$('#pdc_ck').attr('readonly',true);
									$('#pdc_bank').attr('readonly',true);
								}else if(modepayment=="cheque"){
									$("#pdc_mode_cheque").prop("checked", true);
									$('#pdc_bank').attr('readonly',false);
									$('#pdc_no_cheque').attr('readonly',false);
								} 

						});
}

function deletePDC(id){ 
		var transaction		=	"buyer_cheque_monitoring";
		var mods 			=	$('#main_page').val();
		var types 			=	$('#main_type').val(); 
		var mainrid			=	$('#main_rid').val(); 
		var save_message 	= "Are you sure you want to delete Postdated Check Received for Equity?";
				bootbox.dialog({
				  message: save_message, 
				  buttons: {
					success: {
						  label: "Yes",
						  className: "btn-success",
						  callback: function() {
								var dataString		=	"mainid="+encodeURIComponent(id)+"&transaction="+encodeURIComponent(transaction);	 
								$.post('./ajax/delete_transaction.php', dataString, function(msg){
								var obj	= $.parseJSON(msg); 
								if(obj.output==true){
									bootbox.alert("Successfully Deleted ...");   
									window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types+'&rid='+mainrid; }, 1000);		
								} 	
						});
						  }
					},
					danger: {
					  label: "No",
					  className: "btn-danger" 
					}
				  }
				});
}

$('.show_return').hide();
$('#pdc_status_select').change(function(e){
	e.preventDefault();
	var status = $('#pdc_status_select').val();
	var rp_option = $('#pdc_replace_option').val();
	 
	if(rp_option=="cash"){
		$('.replace_cash').show();
	}
	if(status=="Cleared" || status=="Hold" || status=="No Check" || status=="Due Date" || status=="No Payment"){
		$('.replace_cash').hide();
		$('.show_return').hide();
	}/*else if(status=="Returned" || status=="Replacement"){
		$('.show_return').show();
	}else{
		$('.show_return').hide();
	} */
});

$('#pdc_status_cancel').click(function(e){
	e.preventDefault(); 
	var save_message 	= "Are you sure you want to cancel this record?";
	bootbox.dialog({
	  message: save_message, 
	  buttons: {
		success: {
		  label: "Yes",
		  className: "btn-success",
		  callback: function() {
			$('#pdc_status_date').val('');
			$('#pdc_status_select').select2('val','');
			$('#pdc_status_remarks').select2('val',''); 
			$('.postdated_cheque_status_modal').modal('hide');
		  }
		},
		danger: {
		  label: "No",
		  className: "btn-danger" 
		}
	}
	});
});

$('.replace_cash').hide();
$('.replace_cheque').hide();
function updatePDCStatus(reserveId,Id){  
	$('#main_rid').val(reserveId);
	$('#pdc_id').val(Id); 
	var dataString		=	"mainid="+encodeURIComponent(Id)+"&mainrid="+encodeURIComponent(reserveId); 
	callModal('.postdated_cheque_status_modal');
						$.post('./ajax/load_pdc.php', dataString, function(msg){ 
								var obj	= $.parseJSON(msg); 
								var cheque_status		=	obj[0].ChequeStatus;
								var cheque_process_date	=	obj[0].ChequeProcessDate;
								var return_remarks		=	obj[0].ReturnRemarks;
								var or_num				=	obj[0].OrNum;
								var replace_bank		=	obj[0].ReplaceBank;
								var replace_cheque_no	=	obj[0].ReplaceChequeNo;
								var replace_option		=	obj[0].ReplaceOption;
								var chekdate			=	obj[0].ChequeDate;
								var replace_date		=	obj[0].ReplaceDate;
								var cheque_no			=	obj[0].ChequeNo;
							//	var replace_bank		=	obj[0].Bank; 
								var replace_amount		=	Comma(obj[0].ReplaceAmount); 
								//var replace_amount		=	obj[0].Amount; 
								var replace_cash_amount		=	obj[0].ReplaceCashAmount; 
								 
								$('#pdc_or_num').val(or_num);
								$('#pdc_status_date').val(cheque_process_date);
								
								$('#pdc_status_select').select2('val',cheque_status);
								$('#pdc_status_remarks').select2('val',return_remarks);
								$('#pdc_replace_option').select2('val',replace_option);
									$('.replace_cash').hide();
									$('.replace_cheque').hide();
								if((replace_option=="cash" && cheque_status=="Returned") || (replace_option=="cash" && cheque_status=="Replacement")){
									$('.replace_cash').show();
									$('.replace_cheque').hide();
									$('#pdc_date_replace_cash').val(replace_date);
									$('#pdc_amount_replace_cash').val(replace_cash_amount);
								}else if((replace_option=="cheque" && cheque_status=="Returned") || (replace_option=="cheque" && cheque_status=="Replacement")){
									$('.replace_cash').hide();
									$('.replace_cheque').show();
									$('#pdc_bank_replace_cheque').val(replace_bank);
									$('#pdc_date_replace_cheque').val(replace_date);
									$('#pdc_ck_replace_cheque').val(replace_cheque_no);
									$('#pdc_amount_replace_cheque').val(replace_amount);
								}else{
									$('.replace_cash').hide();
									$('.replace_cheque').hide();
								}
								var status = $('#pdc_status_select').val();
								if(status=="Returned" || status=="Replacement"){
									//$('.show_return').show();
								}else{
									//$('.show_return').hide();
								}
						});
} 


//SAVING PDC STATUS
function save_pdc_status(){ 
				var mods 					=	$('#main_page').val();
				var types 					=	$('#main_type').val(); 
				var action 					=	$('#pdc_status_action').val();
				var mainrid					=	$('#main_rid').val();
				var mainid					=	$('#pdc_id').val();

				

				var pdc_details				=	$('#postdated_checks_received_status_form').serialize();
				var dataString				=	pdc_details+"&action="+encodeURIComponent(action)+"&mainrid="+encodeURIComponent(mainrid)+"&mainid="+encodeURIComponent(mainid);
						

						$.post('./ajax/postdated_checks_received_status.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									bootbox.alert("Successfully Saved ..."); 
									$('.postdated_cheque_status_modal').modal('hide'); 
									var status_select	=	$('#pdc_status_select').val();
									var status_remarks	=	$('#pdc_status_remarks').val();
									//if return and close account show what bank and id
									if(status_select=="Returned" && status_remarks=="Closed Account"){
											var dataString2		=	"mainid="+encodeURIComponent(mainid)+"&mainrid="+encodeURIComponent(mainrid);
											$.post('./ajax/load_pdc.php', dataString2, function(msg){
													var obj	= $.parseJSON(msg);
													var bank				=	obj[0].Bank;
													var ckno				=	obj[0].ChequeNo; 
													var fname				=	obj[0].FullName; 
													var block				=	obj[0].Block; 
													var lot					=	obj[0].Lot; 
													//call popup show list of returning closed account checks 
													$('#pdc_return_rid').val(mainrid);
													$('#pdc_return_id').val(mainid);
													$('#pdc_return_bank').val(bank);
													$('#pdc_return_ckno').val(ckno); 
													$('#return_pdc_bank').html(bank); 
													$('#return_pdc_borrower').html(fname); 
													$('#return_pdc_block').html(block); 
													$('#return_pdc_lot').html(lot); 
													loadReturnPDC(mainrid,mainid,bank,ckno);
											});
									}else{
										window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types+'&rid='+mainrid; }, 1000);
									}
								}else{
									bootbox.alert("Failed in saving.");
								}
						});

				 

}


//SAVING PDC STATUS RETURN
function save_pdc_return_status(){  
				var mods 					=	$('#main_page').val();
				var types 					=	$('#main_type').val(); 
				var action 					=	$('#pdc_return_status_action').val();
				var mainrid					=	$('#pdc_return_rid').val();
				var mainid					=	$('#pdc_return_id').val();
				var pdc_details				=	$('#return_checks_received_status_form').serialize();
						
				//CONFIRMATION
					var save_message 	= "Are you sure you want to save this record?";
					bootbox.dialog({
					  message: save_message, 
					  buttons: {
						success: {
						  label: "Yes",
						  className: "btn-success",
						  callback: function() {
								var dataString				=	pdc_details+"&action="+encodeURIComponent(action)+"&mainrid="+encodeURIComponent(mainrid)+"&mainid="+encodeURIComponent(mainid);
								$.post('./ajax/postdated_checks_return_group.php', dataString, function(msg){
										var obj	= $.parseJSON(msg);  
										if(obj.output==true){
											bootbox.alert("Successfully Saved ...");
											$('.return_cheque_status_modal').modal('hide');
											window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types+'&rid='+mainrid; }, 1000); 
										}else{
											bootbox.alert("Failed in saving.");
										}
								}); 
						  }
						},
						danger: {
						  label: "No",
						  className: "btn-danger" 
						}
					}
					});
}

//FUNCTION GET SUCCEEDING RETURN PDC AFTER CURRENT CHECK RETURN SELECTED
	function loadReturnPDC(mainrid,mainid,bank,ckno){
				var dataString2	=	"&mainrid="+encodeURIComponent(mainrid)+"&mainid="+encodeURIComponent(mainid)+"&bank="+encodeURIComponent(bank)+"&ckno="+encodeURIComponent(ckno);
				$.post('./ajax/load_return_pdc.php', dataString2, function(msg){
						var obj	= $.parseJSON(msg); 
						var cq = "";
						//cq += '<select multiple style="height:100px; width:20px" id="return_select_pdc" name="return_select_pdc">';
						 
						if(obj.length!=0){
							callModal('.return_cheque_status_modal');
						}else{
							window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types+'&rid='+mainrid; }, 1000);
						}
						 cq += '<table class="table table-striped" style="height:250px; overflow-y:scroll; display: block; width:100%">';
						 cq	+=	'<tr><td width="5px">&nbsp;</td><td width="200px"><small><b>CHEQUE #</b></small></td><td  width="200px"><small><b>CHEQUE DATE</b></small></td><td  width="200px"><small><b>AMOUNT</b></small></td></tr>'; 
						for(a=0; a<obj.length; a++){ 
							cq	+=	'<tr><td><input type="checkbox" class="myCheckbox" name="return_select_pdc[]" value="'+obj[a].ChequeNo+'"></td><td>'+obj[a].ChequeNo+'</td><td>'+obj[a].ChequeDate+'</td><td>'+Comma(obj[a].Amount)+'</td></tr>'; 
						}
						cq += '</table>';
						$('#return_pdc_list').html(cq);
						$('.myCheckbox').prop('checked', true);   
				});
	}


	

$('#pdc_return_status_cancel').click(function(e){
	e.preventDefault(); 
	var save_message 	= "Are you sure you want to cancel this record?";
	bootbox.dialog({
	  message: save_message, 
	  buttons: {
		success: {
		  label: "Yes",
		  className: "btn-success",
		  callback: function() { 
			$('.return_cheque_status_modal').modal('hide');
		  }
		},
		danger: {
		  label: "No",
		  className: "btn-danger" 
		}
	}
	});
});