function showHideIcon(){
						var icontitle = '';
						var financing		 		= $('#financing').val();
						var complete_equity		 	= $('#complete_equity').val();
						var take_out_date 			= $('#take_out_date').val();
						var twenty_four_checks 		= $('#twenty_four_checks').val();
						var signed_loan_documents 	= $('#signed_loan_documents').val();
						var attended_homeowners_orientation_and_signed_deed_of_restriction = $('#attended_homeowners_orientation_and_signed_deed_of_restriction').val();
							 
						if(financing=="no" && attended_homeowners_orientation_and_signed_deed_of_restriction=="yes" && signed_loan_documents=="yes" && twenty_four_checks=="yes" && take_out_date=="yes" && complete_equity=="yes"){
							 
							$(".showicon").hide();
						}else if(financing=="no" && (attended_homeowners_orientation_and_signed_deed_of_restriction=="no" || signed_loan_documents=="no" ||  twenty_four_checks=="no" || take_out_date=="no" || complete_equity=="no")){
							 
							$(".showicon").show();
						} 

						if(complete_equity=="no"){
							icontitle += '■ Equity not updated from Accounting1.\n'; 
						}

						if(take_out_date=="no"){
							icontitle += '■ No Take-out Date from Documentation2.\n'; 
						}

						if(twenty_four_checks=="no"){
							icontitle += '■ No 24 PDC for Pag-ibig from Documentation1 Checklist of Requirements.\n'; 
						}

						if(signed_loan_documents=="no"){
							icontitle += '■ No signed documents from Documentation1.\n'; 
						}

						if(attended_homeowners_orientation_and_signed_deed_of_restriction=="no"){
							icontitle += '■ No Homeowner Orientation and Signed Doc from Documentation1.\n'; 
						}

						$(".showicon").attr("title", icontitle);
 						 

}


function checkCashFinancing(reservationId){
  	//check if financing is cash
	var dataString				=	"reservation_id="+encodeURIComponent(reservationId); 
	$.post('./ajax/check_financing.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									//Financing cash
									$('#financing').val('yes');
									$('.showicon').hide();
								}else{
									//Financing not cash
									$('#financing').val('no'); 
								}
						}); 
}

function checkCompleteEquity(reservationId){
  	//check if Equity not updated in (accounting1)
	var dataString				=	"reservation_id="+encodeURIComponent(reservationId); 
	$.post('./ajax/check_complete_equity.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									//EQUITY ALL COMPLETE
									$('#complete_equity').val('yes'); 
								}else{
									//EQUITY INCOMPLETE
									$('#complete_equity').val('no'); 
								}
						}); 
}

function checkTakeOutDate(reservationId){
  	//check if No take-out (documentation2)
	var dataString				=	"reservation_id="+encodeURIComponent(reservationId); 
	$.post('./ajax/check_take_out_date.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									//take_out_date
									$('#take_out_date').val('yes'); 
								}else{
									//no take_out_date
									$('#take_out_date').val('no'); 
								}
						}); 
}

function checkTwentyFourChecks(reservationId){
  	//check if No 24 PDC for Pag-ibig (documentation1) checklist of requirements
	var dataString				=	"reservation_id="+encodeURIComponent(reservationId); 
	$.post('./ajax/check_twenty_four_checks.php', dataString, function(msg){
		 
								var obj	= $.parseJSON(msg);
								if(obj.output==true){ 
									//complete twenty_four_checks
									$('#twenty_four_checks').val('yes'); 
								}else{ 
									//not complete twenty_four_checks
									$('#twenty_four_checks').val('no'); 
								}
						}); 
}

function checkSignedLoanDocuments(reservationId){
  //check if No signed documents (documentation1)
	var dataString				=	"reservation_id="+encodeURIComponent(reservationId); 
	$.post('./ajax/check_signed_loan_documents.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){ 
									//complete signed_loan_documents
									$('#signed_loan_documents').val('yes'); 
								}else{ 
									//not complete signed_loan_documents
									$('#signed_loan_documents').val('no'); 
								}
						}); 
}

function checkAttendedHomeOrientation(reservationId){
  //check if No homeowner’s orientation and signed doc (documentation1)
	var dataString				=	"reservation_id="+encodeURIComponent(reservationId); 
	$.post('./ajax/check_attended_homeowners_orientation_and_signed_deed_of_restriction.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){ 
									//attended
									$('#attended_homeowners_orientation_and_signed_deed_of_restriction').val('yes'); 
								}else{ 
									//not attended
									$('#attended_homeowners_orientation_and_signed_deed_of_restriction').val('no'); 
								}
						}); 
}
 



function save_releases_to_broker_for_wht(){
	//alert('saving...'); 
	var mods 					=	$('#main_page').val();
	var types 					=	$('#main_type').val(); 
	var action 					=	$('#main_action').val();
	var mainid					=	$('#main_id').val(); 
	var releases_to_broker		=	$('#releases_to_broker_for_wht_form').serialize(); 
  
	 
	var full_release_checkbox	= $('#full_release_checkbox').prop("checked");
  	
	if(full_release_checkbox==true){
		$('#full_release_checkbox').attr("checked",true);
  		var dataString				=	releases_to_broker+"&fulltick=yes"+"&action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid)+"&types="+encodeURIComponent(types);  
	}else{
		var dataString				=	releases_to_broker+"&fulltick=no"+"&action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid)+"&types="+encodeURIComponent(types);  
	}
 
	$.post('./ajax/releases_to_broker_for_wht.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; }, 1000);
			}else{
				bootbox.alert("Failed in saving.");
			}
	}); 
}


load_releases_to_broker_for_wht();


$('#bpm_date_rf_deposited').change(function(e){
	e.preventDefault();
	computeBalaceReservationLessCa();
	computeMiscExpense();
	computeBasisCommission();
	computeCommission();
	releaseStatus();
	checkBalanceDiscrepancy();
});
$('.showicon').hide();
function load_releases_to_broker_for_wht(){
				var mods 			=	$('#main_page').val();
				var types 			=	$('#main_type').val(); 
				var action 			=	$('#main_action').val();
				var mainid			=	$('#main_id').val();
				var dataString		=	"action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid); 

				
				checkCashFinancing(mainid);
				checkCompleteEquity(mainid);
				checkTakeOutDate(mainid);
				checkTwentyFourChecks(mainid);
				checkSignedLoanDocuments(mainid);
				checkAttendedHomeOrientation(mainid);


				if(action=='edit' || action=='view' || action=='create'){
						$.post('./ajax/load_releases_to_broker_for_wht.php', dataString, function(msg){ 
								var obj	= $.parseJSON(msg); 
								var date_rf_deposited	=	obj.data[0].date_rf_deposited;
								var model				=	obj.data[0].model;
						 		var block				=	obj.data[0].block;
								var lot					=	obj.data[0].lot; 
						 		var full_name			=	obj.data[0].full_name; 
								var tcp					=	parseFloat(obj.data[0].tcp);
								var discount			=	parseFloat(obj.data[0].less_discount); 
								var broker				=	obj.data[0].broker; 
								var agent				=	obj.data[0].agent;
								var realty				=	obj.data[0].realty; 
								var reservation_id		=	obj.data[0].id; 


								var broker_account		=	obj.data[0].broker_account; 
								var agent_account		=	obj.data[0].agent_account; 

 
								var reservation_less_ca		=	obj.data[0].reservation_less_ca; 
								var reservation				=	obj.data[0].reservation; 
								var less_ca					=	obj.data[0].less_ca; 

								var misc_expense_rate 				= obj.data[0].misc_expense_rate;
								var rate 							= obj.data[0].rate; 
								var wht 							= obj.data[0].wht; 
								var first_release_checkbox 			= obj.data[0].first_release_checkbox;  
								var first_release_type 				= obj.data[0].first_release_type;
								var first_release_broker_amount 	= (obj.data[0].first_release_broker_amount=='NaN' || obj.data[0].first_release_broker_amount=='0.00' || obj.data[0].first_release_broker_amount=='' || obj.data[0].first_release_broker_amount==null) ? 0.00 : obj.data[0].first_release_broker_amount; 
								var first_release_agent_amount 		= (obj.data[0].first_release_agent_amount=='NaN' || obj.data[0].first_release_agent_amount=='0.00' || obj.data[0].first_release_agent_amount=='' || obj.data[0].first_release_agent_amount==null) ? 0.00 : obj.data[0].first_release_agent_amount; 
								 
								var first_release_date 				= obj.data[0].first_release_date; 
								var first_release_broker_check 		= obj.data[0].first_release_broker_check; 
								var first_release_agent_check 		= obj.data[0].first_release_agent_check; 

								var second_release_checkbox 	 	= obj.data[0].second_release_checkbox;
								var second_release_type				= obj.data[0].second_release_type; 
								var second_release_broker_amount 	= (obj.data[0].second_release_broker_amount=='NaN' || obj.data[0].second_release_broker_amount=='0.00' || obj.data[0].second_release_broker_amount=='' || obj.data[0].second_release_broker_amount==null) ? 0.00 : obj.data[0].second_release_broker_amount; 
								var second_release_agent_amount 	= (obj.data[0].second_release_agent_amount=='NaN' || obj.data[0].second_release_agent_amount=='0.00' || obj.data[0].second_release_agent_amount=='' || obj.data[0].second_release_agent_amount==null) ? 0.00 : obj.data[0].second_release_agent_amount; 
								 
								var second_release_date				= obj.data[0].second_release_date; 
								var second_release_broker_check	 	= obj.data[0].second_release_broker_check; 
								var second_release_agent_check	 	= obj.data[0].second_release_agent_check; 

								var third_release_checkbox			= obj.data[0].third_release_checkbox;
								var third_release_type				= obj.data[0].third_release_type;  
								var third_release_broker_amount		= (obj.data[0].third_release_broker_amount=='NaN' || obj.data[0].third_release_broker_amount=='0.00' || obj.data[0].third_release_broker_amount=='' || obj.data[0].third_release_broker_amount==null) ? 0.00 : obj.data[0].third_release_broker_amount; 
								var third_release_agent_amount		= (obj.data[0].third_release_agent_amount=='NaN' || obj.data[0].third_release_agent_amount=='0.00' || obj.data[0].third_release_agent_amount=='' || obj.data[0].third_release_agent_amount==null) ? 0.00 : obj.data[0].third_release_agent_amount; 
								 
								var third_release_date				= obj.data[0].third_release_date;
								var third_release_broker_check		= obj.data[0].third_release_broker_check;  
								var third_release_agent_check		= obj.data[0].third_release_agent_check; 
 
								var fourth_release_checkbox			= obj.data[0].fourth_release_checkbox;
								var fourth_release_type				= obj.data[0].fourth_release_type;   
								var fourth_release_broker_amount	= (obj.data[0].fourth_release_broker_amount=='NaN' || obj.data[0].fourth_release_broker_amount=='0.00' || obj.data[0].fourth_release_broker_amount=='' || obj.data[0].fourth_release_broker_amount==null) ? 0.00 : obj.data[0].fourth_release_broker_amount; 
								var fourth_release_agent_amount		= (obj.data[0].fourth_release_agent_amount=='NaN' || obj.data[0].fourth_release_agent_amount=='0.00' || obj.data[0].fourth_release_agent_amount=='' || obj.data[0].fourth_release_agent_amount==null) ? 0.00 : obj.data[0].fourth_release_agent_amount; 
								 
								var fourth_release_date				= obj.data[0].fourth_release_date; 
								var fourth_release_broker_check		= obj.data[0].fourth_release_broker_check;   
								var fourth_release_agent_check		= obj.data[0].fourth_release_agent_check; 

								var fifth_release_checkbox			= obj.data[0].fifth_release_checkbox;
								var fifth_release_type				= obj.data[0].fifth_release_type;    
								var fifth_release_broker_amount		= (obj.data[0].fifth_release_broker_amount=='NaN' || obj.data[0].fifth_release_broker_amount=='0.00' || obj.data[0].fifth_release_broker_amount=='' || obj.data[0].fifth_release_broker_amount==null) ? 0.00 : obj.data[0].fifth_release_broker_amount; 
								var fifth_release_agent_amount		= (obj.data[0].fifth_release_agent_amount=='NaN' || obj.data[0].fifth_release_agent_amount=='0.00' || obj.data[0].fifth_release_agent_amount=='' || obj.data[0].fifth_release_agent_amount==null) ? 0.00 : obj.data[0].fifth_release_agent_amount; 
								 
								var fifth_release_date				= obj.data[0].fifth_release_date; 
								var fifth_release_broker_check		= obj.data[0].fifth_release_broker_check; 
								var fifth_release_agent_check		= obj.data[0].fifth_release_agent_check; 

								var six_release_checkbox			= obj.data[0].six_release_checkbox; 
								var six_release_type				= obj.data[0].six_release_type;    
								var six_release_broker_amount		= (obj.data[0].six_release_broker_amount=='NaN' || obj.data[0].six_release_broker_amount=='0.00' || obj.data[0].six_release_broker_amount=='' || obj.data[0].six_release_broker_amount==null) ? 0.00 : obj.data[0].six_release_broker_amount; 
								var six_release_agent_amount		= (obj.data[0].six_release_agent_amount=='NaN' || obj.data[0].six_release_agent_amount=='0.00' || obj.data[0].six_release_agent_amount=='' || obj.data[0].six_release_agent_amount==null) ? 0.00 : obj.data[0].six_release_agent_amount; 
								 
								var six_release_date				= obj.data[0].six_release_date; 
								var six_release_broker_check		= obj.data[0].six_release_broker_check; 
								var six_release_agent_check			= obj.data[0].six_release_agent_check; 

								var seven_release_checkbox			= obj.data[0].seven_release_checkbox;
								var seven_release_type				= obj.data[0].seven_release_type;     
								var seven_release_broker_amount		= (obj.data[0].seven_release_broker_amount=='NaN' || obj.data[0].seven_release_broker_amount=='0.00' || obj.data[0].seven_release_broker_amount=='' || obj.data[0].seven_release_broker_amount==null) ? 0.00 : obj.data[0].seven_release_broker_amount; 
								var seven_release_agent_amount		= (obj.data[0].seven_release_agent_amount=='NaN' || obj.data[0].seven_release_agent_amount=='0.00' || obj.data[0].seven_release_agent_amount=='' || obj.data[0].seven_release_agent_amount==null) ? 0.00 : obj.data[0].seven_release_agent_amount; 
								 
								var seven_release_date				= obj.data[0].seven_release_date; 
								var seven_release_broker_check		= obj.data[0].seven_release_broker_check;  
								var seven_release_agent_check		= obj.data[0].seven_release_agent_check; 

								var eight_release_checkbox			= obj.data[0].eight_release_checkbox; 
								var eight_release_type				= obj.data[0].eight_release_type;     
								var eight_release_broker_amount		= (obj.data[0].eight_release_broker_amount=='NaN' || obj.data[0].eight_release_broker_amount=='0.00' || obj.data[0].eight_release_broker_amount=='' || obj.data[0].eight_release_broker_amount==null) ? 0.00 : obj.data[0].eight_release_broker_amount; 
								var eight_release_agent_amount		= (obj.data[0].eight_release_agent_amount=='NaN'  || obj.data[0].eight_release_agent_amount=='0.00'  || obj.data[0].eight_release_agent_amount=='' || obj.data[0].eight_release_agent_amount==null) ? 0.00 : obj.data[0].eight_release_agent_amount; 
								 
								var eight_release_date				= obj.data[0].eight_release_date; 
								var eight_release_broker_check		= obj.data[0].eight_release_broker_check;  
								var eight_release_agent_check		= obj.data[0].eight_release_agent_check; 

								var nine_release_checkbox			= obj.data[0].nine_release_checkbox; 
								var nine_release_type				= obj.data[0].nine_release_type;     
								var nine_release_broker_amount		= (obj.data[0].nine_release_broker_amount=='NaN' || obj.data[0].nine_release_broker_amount=='0.00' || obj.data[0].nine_release_broker_amount=='' || obj.data[0].nine_release_broker_amount==null) ? 0.00 : obj.data[0].nine_release_broker_amount; 
								var nine_release_agent_amount		= (obj.data[0].nine_release_agent_amount=='NaN'  || obj.data[0].nine_release_agent_amount=='0.00'  || obj.data[0].nine_release_agent_amount==''  || obj.data[0].nine_release_agent_amount==null) ? 0.00 : obj.data[0].nine_release_agent_amount; 
								 
								var nine_release_date				= obj.data[0].nine_release_date; 
								var nine_release_broker_check		= obj.data[0].nine_release_broker_check;   
								var nine_release_agent_check		= obj.data[0].nine_release_agent_check; 

								var ten_release_checkbox			= obj.data[0].ten_release_checkbox; 
								var ten_release_type				= obj.data[0].ten_release_type;     
								var ten_release_broker_amount		= (obj.data[0].ten_release_broker_amount=='NaN' || obj.data[0].ten_release_broker_amount=='0.00' || obj.data[0].ten_release_broker_amount=='' || obj.data[0].ten_release_broker_amount==null) ? 0.00 : obj.data[0].ten_release_broker_amount; 
								var ten_release_agent_amount		= (obj.data[0].ten_release_agent_amount=='NaN'  || obj.data[0].ten_release_agent_amount=='0.00'  || obj.data[0].ten_release_agent_amount==''  || obj.data[0].ten_release_agent_amount==null) ? 0.00 : obj.data[0].ten_release_agent_amount; 
								 
								var ten_release_date				= obj.data[0].ten_release_date; 
								var ten_release_broker_check		= obj.data[0].ten_release_broker_check;   
								var ten_release_agent_check			= obj.data[0].ten_release_agent_check;  

								var eleven_release_checkbox			= obj.data[0].eleven_release_checkbox; 
								var eleven_release_type				= obj.data[0].eleven_release_type;     
								var eleven_release_broker_amount	= (obj.data[0].eleven_release_broker_amount=='NaN' || obj.data[0].eleven_release_broker_amount=='0.00' || obj.data[0].eleven_release_broker_amount=='' || obj.data[0].eleven_release_broker_amount==null) ? 0.00 : obj.data[0].eleven_release_broker_amount; 
								var eleven_release_agent_amount		= (obj.data[0].eleven_release_agent_amount=='NaN'  || obj.data[0].eleven_release_agent_amount=='0.00'  || obj.data[0].eleven_release_agent_amount==''  || obj.data[0].eleven_release_agent_amount==null) ? 0.00 : obj.data[0].eleven_release_agent_amount; 
								 
								var eleven_release_date				= obj.data[0].eleven_release_date; 
								var eleven_release_broker_check		= obj.data[0].eleven_release_broker_check;   
								var eleven_release_agent_check		= obj.data[0].eleven_release_agent_check;  

								var twelve_release_checkbox			= obj.data[0].twelve_release_checkbox; 
								var twelve_release_type				= obj.data[0].twelve_release_type;     
								var twelve_release_broker_amount	= (obj.data[0].twelve_release_broker_amount=='NaN' || obj.data[0].twelve_release_broker_amount=='0.00' || obj.data[0].twelve_release_broker_amount=='' || obj.data[0].twelve_release_broker_amount==null) ? 0.00 : obj.data[0].twelve_release_broker_amount; 
								var twelve_release_agent_amount		= (obj.data[0].twelve_release_agent_amount=='NaN' || obj.data[0].twelve_release_agent_amount=='0.00' || obj.data[0].twelve_release_agent_amount=='' || obj.data[0].twelve_release_agent_amount==null) ? 0.00 : obj.data[0].twelve_release_agent_amount; 
								 
								var twelve_release_date				= obj.data[0].twelve_release_date; 
								var twelve_release_broker_check		= obj.data[0].twelve_release_broker_check;   
								var twelve_release_agent_check		= obj.data[0].twelve_release_agent_check;  



								 
								var full_date					= obj.data[0].full_date;  

								var subtotal_amount				= obj.data[0].subtotal_amount; 
							 

								var full_release_checkbox		= obj.data[0].full_release_checkbox; 
								var full_amount					= (obj.data[0].full_amount!='NaN' || obj.data[0].full_amount!='' || obj.data[0].full_amount!=null) ? obj.data[0].full_amount : '0.00'; 	
								
								var full_net					= (obj.data[0].full_net!='NaN' || obj.data[0].full_net!='' || obj.data[0].full_net!=null) ? obj.data[0].full_net : '0.00'; 	
								var full_check					= (obj.data[0].full_check!='') ? obj.data[0].full_check : broker_account; 	
 
								var total_release_amount		= (obj.data[0].total_release_amount!='NaN') ? obj.data[0].total_release_amount : '0.00'; 
								var total_release_wht			= (obj.data[0].total_release_wht!='NaN') ? obj.data[0].total_release_wht : '0.00'; 
								 
								var release_status				= obj.data[0].release_status; 
								var release_remarks				= obj.data[0].release_remarks; 

								var take_out_date				= obj.data[0].take_out_date; 


								var broker_account				= obj.data[0].broker_account; 
								var agent_account				= obj.data[0].agent_account; 
								var add_cash_advance			= obj.data[0].add_cash_advance; 
								var total_release_amount		= obj.data[0].total_release_amount; 







								/****************/  
								
					 

								if(first_release_date=='01/01/1970'){
									first_release_date = '';
								}

								if(second_release_date=='01/01/1970'){
									second_release_date = '';
								}

								if(third_release_date=='01/01/1970'){
									third_release_date = '';
								}

								if(fourth_release_date=='01/01/1970'){
									fourth_release_date = '';
								}

								if(fifth_release_date=='01/01/1970'){
									fifth_release_date = '';
								}

								if(six_release_date=='01/01/1970'){
									six_release_date = '';
								}

								if(seven_release_date=='01/01/1970'){
									seven_release_date = '';
								}

								if(eight_release_date=='01/01/1970'){
									eight_release_date = '';
								}

								if(nine_release_date=='01/01/1970'){
									nine_release_date = '';
								}

								if(ten_release_date=='01/01/1970'){
									ten_release_date = '';
								}

								if(eleven_release_date=='01/01/1970'){
									eleven_release_date = '';
								}

								if(twelve_release_date=='01/01/1970'){
									twelve_release_date = '';
								}

								 

								if(full_date=='01/01/1970'){
									full_date = '';
								}

								 
 
								if(first_release_checkbox=='on'){
									$('#first_release_checkbox').attr('checked',true);
									$('#first_release_checkbox').attr('disabled',false);
									$('#first_release_broker_amount').val(first_release_broker_amount);
									$('#first_release_agent_amount').val(first_release_agent_amount);									
								}
	 							 
								if(second_release_checkbox=='on'){
									$('#second_release_checkbox').attr('checked',true);
									$('#second_release_checkbox').attr('disabled',false);
									$('#second_release_broker_amount').val(second_release_broker_amount);
									$('#second_release_agent_amount').val(second_release_agent_amount);
								}
								  
								if(third_release_checkbox=='on'){
									$('#third_release_checkbox').attr('checked',true);
									$('#third_release_checkbox').attr('disabled',false);
									$('#third_release_agent_amount').val(third_release_agent_amount);
									$('#third_release_broker_amount').val(third_release_broker_amount);
								}
 
								if(fourth_release_checkbox=='on'){
									$('#fourth_release_checkbox').attr('checked',true);
									$('#fourth_release_checkbox').attr('disabled',false);
									$('#fourth_release_agent_amount').val(fourth_release_agent_amount);
									$('#fourth_release_broker_amount').val(fourth_release_broker_amount);
								}

								if(fifth_release_checkbox=='on'){
									$('#fifth_release_checkbox').attr('checked',true);
									$('#fifth_release_checkbox').attr('disabled',false);
									$('#fifth_release_broker_amount').val(fifth_release_broker_amount);
									$('#fifth_release_agent_amount').val(fifth_release_agent_amount);
								}

								if(six_release_checkbox=='on'){
									$('#six_release_checkbox').attr('checked',true);
									$('#six_release_checkbox').attr('disabled',false);
									$('#six_release_broker_amount').val(six_release_broker_amount);
									$('#six_release_agent_amount').val(six_release_agent_amount);
								}

								if(seven_release_checkbox=='on'){
									$('#seven_release_checkbox').attr('checked',true);
									$('#seven_release_checkbox').attr('disabled',false);
									$('#seven_release_broker_amount').val(seven_release_broker_amount);
									$('#seven_release_agent_amount').val(seven_release_agent_amount);
								}

								if(eight_release_checkbox=='on'){
									$('#eight_release_checkbox').attr('checked',true);
									$('#eight_release_checkbox').attr('disabled',false);
									$('#eight_release_broker_amount').val(eight_release_broker_amount);
									$('#eight_release_agent_amount').val(eight_release_agent_amount);
								}

								if(nine_release_checkbox=='on'){
									$('#nine_release_checkbox').attr('checked',true);
									$('#nine_release_checkbox').attr('disabled',false);
									$('#nine_release_agent_amount').val(nine_release_agent_amount);
									$('#nine_release_broker_amount').val(nine_release_broker_amount);
								}

								if(ten_release_checkbox=='on'){
									$('#ten_release_checkbox').attr('checked',true);
									$('#ten_release_checkbox').attr('disabled',false);
									$('#ten_release_broker_amount').val(ten_release_broker_amount); 
									$('#ten_release_agent_amount').val(ten_release_agent_amount);
								}

								if(eleven_release_checkbox=='on'){
									$('#eleven_release_checkbox').attr('checked',true);
									$('#eleven_release_checkbox').attr('disabled',false);
									$('#eleven_release_broker_amount').val(eleven_release_broker_amount); 
									$('#eleven_release_agent_amount').val(eleven_release_agent_amount);
								}

								if(twelve_release_checkbox=='on'){
									$('#twelve_release_checkbox').attr('checked',true);
									$('#twelve_release_checkbox').attr('disabled',false);
									$('#twelve_release_broker_amount').val(twelve_release_broker_amount); 
									$('#twelve_release_agent_amount').val(twelve_release_agent_amount);
								}

								if(full_release_checkbox == 'yes'){
									$('#full_release_checkbox').attr('checked',true); 
								}
 
								$('#first_release_type').val(first_release_type);
								$('#second_release_type').val(second_release_type);
								$('#third_release_type').val(third_release_type);
								$('#fourth_release_type').val(fourth_release_type);
								$('#fifth_release_type').val(fifth_release_type);
								$('#six_release_type').val(six_release_type);
								$('#seven_release_type').val(seven_release_type);
								$('#eight_release_type').val(eight_release_type);
								$('#nine_release_type').val(nine_release_type);
								$('#ten_release_type').val(ten_release_type);
								$('#eleven_release_type').val(eleven_release_type);
								$('#twelve_release_type').val(twelve_release_type);


								$('#first_release_broker_check').val(first_release_broker_check);
								$('#first_release_agent_check').val(first_release_agent_check);
								
								$('#second_release_broker_check').val(second_release_broker_check);
								$('#second_release_agent_check').val(second_release_agent_check);
								
								$('#third_release_broker_check').val(third_release_broker_check);
								$('#third_release_agent_check').val(third_release_agent_check);
								
								$('#fourth_release_broker_check').val(fourth_release_broker_check);
								$('#fourth_release_agent_check').val(fourth_release_agent_check);
								
								$('#fifth_release_broker_check').val(fifth_release_broker_check);
								$('#fifth_release_agent_check').val(fifth_release_agent_check);
								
								$('#six_release_broker_check').val(six_release_broker_check);
								$('#six_release_agent_check').val(six_release_agent_check);
								
								$('#seven_release_broker_check').val(seven_release_broker_check);
								$('#seven_release_agent_check').val(seven_release_agent_check);

								$('#eight_release_broker_check').val(eight_release_broker_check);
								$('#eight_release_agent_check').val(eight_release_agent_check);
								
								$('#nine_release_broker_check').val(nine_release_broker_check);
								$('#nine_release_agent_check').val(nine_release_agent_check);
								
								$('#ten_release_broker_check').val(ten_release_broker_check);
								$('#ten_release_agent_check').val(ten_release_agent_check);

								$('#eleven_release_broker_check').val(eleven_release_broker_check);
								$('#eleven_release_agent_check').val(eleven_release_agent_check);

								$('#twelve_release_broker_check').val(twelve_release_broker_check);
								$('#twelve_release_agent_check').val(twelve_release_agent_check);
								

								if(take_out_date == '01/01/1970'){
									take_out_date = '';
								} 								
								$('#bpm_take_out_date').val(take_out_date);
								 
								tcp    		= Comma(parseFloat(tcp).toFixed(2));
								 
								discount	= Comma(parseFloat(discount).toFixed(2));
						
								$('#bpm_tcp').val(tcp); 
 
								$('#first_release_date').val(first_release_date);
								$('#second_release_date').val(second_release_date); 
								$('#third_release_date').val(third_release_date);
								$('#fourth_release_date').val(fourth_release_date);
								$('#fifth_release_date').val(fifth_release_date);
								$('#six_release_date').val(six_release_date);
								$('#seven_release_date').val(seven_release_date);
								$('#eight_release_date').val(eight_release_date);
								$('#nine_release_date').val(nine_release_date);
								$('#ten_release_date').val(ten_release_date);
								$('#eleven_release_date').val(eleven_release_date);
								$('#twelve_release_date').val(twelve_release_date);

								 
								$('#full_date').val(full_date);
								 
 
								$('#subtotal_release_amount').val(subtotal_amount);
								 
					 
								$('#full_release_checkbox').val(full_release_checkbox); 

								$('#full_amount').val(full_amount);
								 
								 
								$('#full_net').val(full_net);
								$('#full_check').val(full_check);

								$('#total_amount').val(total_release_amount);
								$('#total_wht').val(total_release_wht);
 
								 
 
 								$('#subtotal_release_broker_amount').val(broker_account);
 								$('#subtotal_release_agent_amount').val(agent_account);
 								$('#add_cash_advance').val(add_cash_advance);
 								$('#total_releases_amount').val(total_release_amount);



								$('#releases_status').val(release_status);
								$('#releases_remarks').val(release_remarks);
 
								$('#withholding_tax').val(wht);
								$('#bpm_less_discount').val(discount);					
								$('#bpm_rate').val(rate);
								$('#bpm_misc_expense_rate').val(misc_expense_rate);

								$('#bpm_buyer').val(full_name);

								if(date_rf_deposited=='01/01/1970'){
									date_rf_deposited = '';
								}

								$('#bpm_date_rf_deposited').val(date_rf_deposited);
								$('#bpm_block').val(block);
								$('#bpm_broker').val(broker);
								$('#bpm_agent').val(agent);
								$('#bpm_realty').val(realty);
								$('#bpm_lot').val(lot);
								$('#reservation_id').val(reservation_id);
								 
								reservation	= Comma(parseFloat(reservation).toFixed(2)); 
								if(reservation=='' || reservation=='NaN' || reservation==undefined || reservation==null){
									$('#bpm_reservation').val(parseFloat(0.00).toFixed(2));	
								}else{
									$('#bpm_reservation').val(reservation);
								}

							 
								less_ca	= Comma(parseFloat(less_ca).toFixed(2)); 
								if(less_ca=='' || less_ca=='NaN' || less_ca==undefined || less_ca==null){
									$('#bpm_less_ca').val(parseFloat(0.00).toFixed(2));	 
									$('#add_cash_advance').val(parseFloat(0.00).toFixed(2));  
								}else{
									$('#bpm_less_ca').val(less_ca); 
									$('#add_cash_advance').val(less_ca);
								}
							 


								var subtotal_amount 	=	$('#subtotal_release_amount').val().replace(/,/g, "");
								var cash_advance 		=	$('#bpm_less_ca').val().replace(/,/g, "");
								 
								var total_release = parseFloat(subtotal_amount) + parseFloat(cash_advance);
								$('#total_releases_amount').val(Comma(total_release.toFixed(2)));
								
								
								bpm_reservation_less_ca	= Comma(parseFloat(bpm_reservation_less_ca).toFixed(2)); 
								if(bpm_reservation_less_ca=='' || bpm_reservation_less_ca=='NaN' || bpm_reservation_less_ca==undefined || bpm_reservation_less_ca==null){
									$('#bpm_reservation_less_ca').val(parseFloat(0.00).toFixed(2));	
								}else{
									$('#bpm_reservation_less_ca').val(reservation_less_ca);
								}
								
 
								//if no values will set default values of 
								var default_reservation = $('#bpm_reservation').val().replace(/,/g, "");
								  
								if(default_reservation=='' || default_reservation=='0' || default_reservation=='0.00' || default_reservation=='NaN' || default_reservation=='0'){
 								 	$('#first_release_cash').val('0.00');
									 
									$('#bpm_reservation').val('0.00');
									$('#bpm_less_discount').val('0.00');
								}

								 
 
								var default_less_discount = $('#bpm_less_discount').val().replace(/,/g, "");
								if(default_less_discount=='NaN' || default_less_discount=='' || default_less_discount=='0.00' || default_less_discount=='0'){
									$('#bpm_less_discount').val('10,000.00');
								}

								var default_rate = $('#bpm_rate').val().replace(/,/g, "");
								if(default_rate=='NaN' || default_rate=='' || default_rate=='0.00' || default_rate=='0'){
									$('#bpm_rate').val(7);
								}
								
								var default_misc_expense_rate = $('#bpm_misc_expense_rate').val().replace(/,/g, "");
								if(default_misc_expense_rate=='NaN' || default_misc_expense_rate=='' || default_misc_expense_rate=='0.00' || default_misc_expense_rate=='0'){
									$('#bpm_misc_expense_rate').val(14);
								}
								
								var default_less_ca = $('#bpm_less_ca').val().replace(/,/g, "");
								if(default_less_ca=='NaN' || default_less_ca=='' || default_less_ca=='0.00' || default_less_ca=='0'){
									//$('#bpm_less_ca').val('5,000.00'); 
								}

								var default_witholding = $('#withholding_tax').val();
								if(default_witholding=='NaN' || default_witholding=='' || default_witholding=='0.00' || default_witholding=='0'){
									$('#withholding_tax').val(10);
								}

 
									 

 
								computeBalaceReservationLessCa();
								computeMiscExpense();
								computeBasisCommission();
								computeCommission(); 
								computeWHT();
								releaseStatus();
								computeBalance();

								showHideIcon();
								checkBalanceDiscrepancy();

						});					

			}
}






var rfdep = $('#bpm_date_rf_deposited').val();
//check if rf deposited have values otherwise reservation and CA should be empty, it will be filled up once rf deposited had been selected

if(rfdep==''){
	$('#bpm_reservation').val('0.00');
	$('#bpm_less_ca').val('0.00');	
}



$('#bpm_date_rf_deposited').change(function(e){
	e.preventDefault();  
	//if the date reservation deposited changed/selected check condition in release to broker database table if there's values already in reservation and less ca incase admin has made some modifications.  
	//If there's no existing values will used the fixed values P10,000 for reservation and P5,000 for less CA

	$('#bpm_reservation').val('10,000.00');
	$('#bpm_less_ca').val('5,000.00');

	$('#add_cash_advance').val('5,000.00'); 
	
});
  
function computeBalaceReservationLessCa(){
	var reservation = $('#bpm_reservation').val().replace(/,/g, "");
	var less_ca = $('#bpm_less_ca').val().replace(/,/g, "");
	var total = reservation - less_ca;
	total = Comma(total.toFixed(2));
	$('#bpm_reservation_less_ca').val(total);
}


function computeMiscExpense(){ 
	var misc_expense_rate 	=	$('#bpm_misc_expense_rate').val().replace(/,/g, "");
	var tcp 				=	$('#bpm_tcp').val().replace(/,/g, "");
	var discount			=	$('#bpm_less_discount').val().replace(/,/g, "");
 
	var total 				=	(tcp - discount) * (misc_expense_rate/100);
	total 					=	Comma(total.toFixed(2));
	$('#bpm_misc_expenses').val(total);	
}
 

function computeWHT(){  
	
	var withhold = 10; 
	var total_amount 	=	$('#total_amount').val().replace(/,/g, "");
		total_amount 		=	total_amount * (withhold/100);
		total_amount		=	Comma(total_amount.toFixed(2));
		$('#total_wht').val(total_amount); 

	computeNetCom();
  
}

function computeBasisCommission(){ 
	var tcp 					=	$('#bpm_tcp').val().replace(/,/g, "");
	var bpm_less_discount 		=	$('#bpm_less_discount').val().replace(/,/g, "");
	var bpm_misc_expenses		=	$('#bpm_misc_expenses').val().replace(/,/g, "");
	var commission				=	tcp - bpm_less_discount - bpm_misc_expenses;
	commission 					=	Comma(commission.toFixed(2)); 
	$('#bpm_basis_commission').val(commission);	
}

function computeBalance(){
	var bpm_reservation		=	$('#bpm_reservation').val().replace(/,/g, "");
	var bpm_less_ca			=	$('#bpm_less_ca').val().replace(/,/g, "");
 	bpm_less_ca				=	Comma(parseFloat(bpm_less_ca).toFixed(2)); 
	$('#add_cash_advance').val(bpm_less_ca);
	

 	var balance 			=	bpm_reservation - bpm_less_ca.replace(/,/g, "");

 	balance					=	Comma(balance.toFixed(2)); 
	$('#bpm_reservation_less_ca').val(balance); 


	var subtotal_amount 	=	$('#subtotal_release_amount').val().replace(/,/g, "");
	var cash_advance 		=	$('#bpm_less_ca').val().replace(/,/g, "");

	var total_release = parseFloat(subtotal_amount) + parseFloat(cash_advance);
	$('#total_releases_amount').val(Comma(total_release.toFixed(2)));


	//BALANCE PAYABLE 28aug23
	var total_amount 			= $('#total_amount').val().replace(/,/g, ""); 
	var total_releases_amount 	= $('#total_releases_amount').val().replace(/,/g, ""); 
	var full_amount 			= total_amount - total_releases_amount; //new formula as of 28aug23
	full_amount					=	(full_amount!='NaN' || full_amount!='' || full_amount!=null) ? Comma(full_amount.toFixed(2)) : '0.00';
	$('#full_amount').val(full_amount);								
	computeNetCom();
}

function computeCommission(){	
  	var bpm_basis_commission	=	$('#bpm_basis_commission').val().replace(/,/g, "");
	var bpm_rate				=	$('#bpm_rate').val().replace(/,/g, "");
 	var total 					=	bpm_basis_commission * (bpm_rate/100);
 	total 						=	Comma(total.toFixed(2));
 	 
	$('#bpm_commission').val(total);
 
	var total_amount 			= $('#total_amount').val().replace(/,/g, ""); 
	var total_releases_amount 	= $('#total_releases_amount').val().replace(/,/g, ""); 
	var full_amount 			= total_amount - total_releases_amount; //new formula as of 28aug23
 
	full_amount		=	(full_amount!='NaN' || full_amount!='' || full_amount!=null) ? Comma(full_amount.toFixed(2)) : '0.00';
	$('#full_amount').val(full_amount);
	$('#total_amount').val(total);
 
 	computeAmountBalance();
}



function computeAmountBalance(){
	//GET BROKER AMOUNT
	var first_release_broker_amount		= $('#first_release_broker_amount').val().replace(/,/g, "");
	var second_release_broker_amount	= $('#second_release_broker_amount').val().replace(/,/g, "");
	var third_release_broker_amount		= $('#third_release_broker_amount').val().replace(/,/g, "");
	var fourth_release_broker_amount	= $('#fourth_release_broker_amount').val().replace(/,/g, "");
  	var fifth_release_broker_amount		= $('#fifth_release_broker_amount').val().replace(/,/g, "");
	var six_release_broker_amount		= $('#six_release_broker_amount').val().replace(/,/g, "");
	var seven_release_broker_amount		= $('#seven_release_broker_amount').val().replace(/,/g, "");
	var eight_release_broker_amount		= $('#eight_release_broker_amount').val().replace(/,/g, "");
	var nine_release_broker_amount		= $('#nine_release_broker_amount').val().replace(/,/g, "");
	var ten_release_broker_amount		= $('#ten_release_broker_amount').val().replace(/,/g, "");
	var eleven_release_broker_amount	= $('#eleven_release_broker_amount').val().replace(/,/g, "");
	var twelve_release_broker_amount	= $('#twelve_release_broker_amount').val().replace(/,/g, "");


	//GET AGENT AMOUNT
	var first_release_agent_amount 		= $('#first_release_agent_amount').val().replace(/,/g, "");
	var second_release_agent_amount		= $('#second_release_agent_amount').val().replace(/,/g, "");
	var third_release_agent_amount		= $('#third_release_agent_amount').val().replace(/,/g, "");
	var fourth_release_agent_amount		= $('#fourth_release_agent_amount').val().replace(/,/g, "");
  	var fifth_release_agent_amount		= $('#fifth_release_agent_amount').val().replace(/,/g, "");
	var six_release_agent_amount		= $('#six_release_agent_amount').val().replace(/,/g, "");
	var seven_release_agent_amount		= $('#seven_release_agent_amount').val().replace(/,/g, "");
	var eight_release_agent_amount		= $('#eight_release_agent_amount').val().replace(/,/g, "");
	var nine_release_agent_amount		= $('#nine_release_agent_amount').val().replace(/,/g, "");
	var ten_release_agent_amount		= $('#ten_release_agent_amount').val().replace(/,/g, "");
	var eleven_release_agent_amount		= $('#eleven_release_agent_amount').val().replace(/,/g, "");
	var twelve_release_agent_amount		= $('#twelve_release_agent_amount').val().replace(/,/g, "");

	var subtotal_broker 		= parseFloat(first_release_broker_amount) + parseFloat(second_release_broker_amount)  + parseFloat(third_release_broker_amount) + parseFloat(fourth_release_broker_amount) + parseFloat(fifth_release_broker_amount) + parseFloat(six_release_broker_amount) + parseFloat(seven_release_broker_amount) + parseFloat(eight_release_broker_amount) + parseFloat(nine_release_broker_amount) + parseFloat(ten_release_broker_amount) + parseFloat(eleven_release_broker_amount) + parseFloat(twelve_release_broker_amount);

	var csubtotal_broker		= Comma(subtotal_broker.toFixed(2));
	$('#subtotal_release_broker_amount').val(csubtotal_broker);

	var subtotal_agent 		= parseFloat(first_release_agent_amount) + parseFloat(second_release_agent_amount)  + parseFloat(third_release_agent_amount) + parseFloat(fourth_release_agent_amount) + parseFloat(fifth_release_agent_amount) + parseFloat(six_release_agent_amount) + parseFloat(seven_release_agent_amount) + parseFloat(eight_release_agent_amount) + parseFloat(nine_release_agent_amount) + parseFloat(ten_release_agent_amount) + parseFloat(eleven_release_agent_amount) + parseFloat(twelve_release_agent_amount);
	var csubtotal_agent		= Comma(subtotal_agent.toFixed(2));
	$('#subtotal_release_agent_amount').val(csubtotal_agent);

	var subtotal_broker_agent 		= subtotal_broker + subtotal_agent;
	var csubtotal_broker_agent		= Comma(subtotal_broker_agent.toFixed(2));
	$('#subtotal_release_amount').val(csubtotal_broker_agent);
  
	var subtotal_amount 	=	$('#subtotal_release_amount').val().replace(/,/g, "");
	var cash_advance 		=	$('#bpm_less_ca').val().replace(/,/g, "");
	$('#add_cash_advance').val(cash_advance);

	var total_release = parseFloat(subtotal_amount) + parseFloat(cash_advance);
	$('#total_releases_amount').val(Comma(total_release.toFixed(2)));

	//GET WHT
	var withhold 		=	$('#withholding_tax').val();
 

	computeNetCom();
	computeWHT();

	 
    
}


$('#bpm_misc_expense_rate').change(function(e){
	e.preventDefault();
	computeMiscExpense();	
	computeBasisCommission();
	computeCommission();
	releaseStatus();
	checkBalanceDiscrepancy();
});


$('#bpm_less_discount').change(function(e){
	e.preventDefault();
	computeBasisCommission();
	computeCommission();
});

$('#bpm_rate').change(function(e){
	e.preventDefault();
	computeCommission();	
});

 



function computeNetCom(){ 
	
	 var full_amount 	=	$('#full_amount').val().replace(/,/g, "");
	 var total_wht 		=	$('#total_wht').val().replace(/,/g, "");
 	 var full_net 		= parseFloat(full_amount) - parseFloat(total_wht);
 	 $('#full_net').val(Comma(full_net.toFixed(2)));

}

function checkBalanceDiscrepancy(){
 
	//with balance formula = total_releases_amount + full_net + total_wht = total_amount

	var total_releases_amount 	=	$('#total_releases_amount').val().replace(/,/g, "");
	var net_amount 				=	$('#full_net').val().replace(/,/g, "");
	var ewt 					=	$('#total_wht').val().replace(/,/g, "");

	var total_commission		=	$('#total_amount').val().replace(/,/g, "");

	var withbal = parseFloat(total_releases_amount) + parseFloat(net_amount) + parseFloat(ewt);
	 
	if(withbal!=total_commission){ 
		//discrepancy not balance send notification to admin 
				var mods 					=	$('#main_page').val();
				var types 					=	$('#main_type').val(); 
				var action 					=	$('#main_action').val();
				var mainid					=	$('#main_id').val(); 			  
				   

				var dataString				=   "total_releases_amount="+encodeURIComponent(total_releases_amount)+"&net_amount="+encodeURIComponent(net_amount)+"&ewt="+encodeURIComponent(ewt)+"&total_commission="+encodeURIComponent(total_commission)+"&action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid)+"&types="+encodeURIComponent(types);  

				$.post('./ajax/balance_discrepancies.php', dataString, function(msg){
					var obj	= $.parseJSON(msg);
						if(obj.output==true){ 
						} 
				}); 

	} 
}	  


function releaseStatus(){

	let full_release_checkbox 	= $('#full_release_checkbox').prop("checked");

	//with balance formula = total_releases_amount + full_net + total_wht = total_amount

	var total_releases_amount 	=	$('#total_releases_amount').val().replace(/,/g, "");
	var net_amount 				=	$('#full_net').val().replace(/,/g, "");
	var ewt 					=	$('#total_wht').val().replace(/,/g, "");

	var total_commission		=	$('#total_amount').val().replace(/,/g, "");

	var withbal = parseFloat(total_releases_amount) + parseFloat(net_amount) + parseFloat(ewt);
	 
	if(withbal==total_commission){
		$('#releases_status').val('With Balance');   
	} 

	if(full_release_checkbox==true){
		$(":input[name=full_check]").focus();
	}	

	 
	 /*
			 let balance_check 	= $('#balancereleasecheckbox').prop("checked");
			 let full_check 	= $('#full_release_checkbox').prop("checked");
			   
			if(balance_check==true && full_check==true){
				$('#releases_status').val('Fully Paid');
			}else{
				$('#releases_status').val('With Balance');
			} 

			if(balance_check==true && full_check==true){
				$(":input[name=total_check]").focus();	
			}else if(balance_check==true){
				$(":input[name=partial_check]").focus();	
			}else if(full_check==true){
				$(":input[name=full_check]").focus();	
			}
	*/

	//With Balance – Total Releases + Net Amount + EWT should equal to Total Commission
	//Note: To send alert to admin if discrepancy
}

 

$('#full_release_checkbox').on('change',function(){
	releaseStatus(); 
	checkBalanceDiscrepancy();
}); 
 



function checkCompleteRequirements(reservationId){
  
	var dataString				=	"reservation_id="+encodeURIComponent(reservationId); 
	$.post('./ajax/check_complete_requirements.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									//REQUIREMENTS ALL COMPLETE
									$('#complete_requirement').val('yes');
								}else{
									//REQUIREMENTS INCOMPLETE
									$('#complete_requirement').val('no');
								}
						}); 
}

function checkCompleteEquity(reservationId){
  
	var dataString				=	"reservation_id="+encodeURIComponent(reservationId); 
	$.post('./ajax/check_complete_equity.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									//EQUITY ALL COMPLETE
									$('#complete_equity').val('yes');
								}else{
									//EQUITY INCOMPLETE
									$('#complete_equity').val('no');
								}
						}); 
}
