function save_payment_status_to_broker(){ 
	var mods 							=	$('#main_page').val();
	var types 							=	$('#main_type').val(); 
	var action 							=	$('#main_action').val();
	var mainid							=	$('#main_id').val(); 
	var payment_status_tobroker			=	$('#payment_status_to_broker_form').serialize();  
	var dataString						=	payment_status_tobroker+"&action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid)+"&types="+encodeURIComponent(types);
	$.post('./ajax/payment_status_to_broker.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_payment_status_to_broker();


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

function load_payment_status_to_broker(){
				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); 


				if(action=='edit' || action=='view' || action=='create'){
						$.post('./ajax/load_payment_status_to_broker.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 reservation_id		=	obj.data[0].id; 


								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 ca_checkbox 				= obj.data[0].ca_checkbox; 
								var first_release_amount 		= obj.data[0].first_release_amount; 
								var first_release_wht  			= obj.data[0].first_release_wht; 
								var first_release_cash 			= obj.data[0].first_release_cash; 
								var first_release_net 			= obj.data[0].first_release_net; 
								var first_release_date 			= obj.data[0].first_release_date; 
								var first_release_check  		= obj.data[0].first_release_check; 
								var second_release_checkbox  	= obj.data[0].second_release_checkbox; 
								var second_release_amount  		= obj.data[0].second_release_amount; 
								var second_release_wht 			= obj.data[0].second_release_wht; 
								var second_release_cash 		= obj.data[0].second_release_cash; 
								var second_release_net 			= obj.data[0].second_release_net; 
								var second_release_date			= obj.data[0].second_release_date; 
								var second_release_check		= obj.data[0].second_release_check; 
								var third_release_checkbox		= obj.data[0].third_release_checkbox; 
								var third_release_amount		= obj.data[0].third_release_amount; 
								var third_release_wht			= obj.data[0].third_release_wht; 
								var third_release_cash			= obj.data[0].third_release_cash; 
								var third_release_net			= obj.data[0].third_release_net; 
								var third_release_date			= obj.data[0].third_release_date; 
								var third_release_check			= obj.data[0].third_release_check; 
								var total_release_amount		= obj.data[0].total_release_amount; 
								var total_release_wht			= obj.data[0].total_release_wht; 
								var total_release_cash			= obj.data[0].total_release_cash; 
								var total_release_net			= obj.data[0].total_release_net; 
								var release_status				= obj.data[0].release_status; 
								var release_remarks				= obj.data[0].release_remarks; 

								/****************/  
								
								if(first_release_net=='NaN' || first_release_net=='0.00' || first_release_net=='' || first_release_net==null){
									first_release_net = '0.00';
								}

								if(first_release_cash=='NaN' || first_release_cash=='0.00' || first_release_cash=='' || first_release_cash==null){
									first_release_cash = '0.00';
								}

								if(second_release_cash=='NaN' || second_release_cash=='0.00' || second_release_cash==''){
									second_release_cash = '0.00';
								}

								if(third_release_cash=='NaN' || third_release_cash=='0.00' || third_release_cash==''){
									third_release_cash = '0.00';
								}
								
								if(total_release_cash=='NaN' || total_release_cash=='0.00' || total_release_cash==''){
									total_release_cash = '0.00';
								}

								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(first_release_checkbox=='on'){
									$('#first_release_checkbox').attr('checked',true);
								}
								 
								if(ca_checkbox=='on'){
									$('#ca_checkbox').attr('checked',true);
								}
								 
								if(second_release_checkbox=='on'){
									$('#second_release_checkbox').attr('checked',true);
								}
								 
								if(third_release_checkbox=='on'){
									$('#third_release_checkbox').attr('checked',true);
								}
 								
								 
								tcp    		= Comma(parseFloat(tcp).toFixed(2));
								 
								discount	= Comma(parseFloat(discount).toFixed(2));
						
								$('#bpm_tcp').val(tcp); 



								$('#first_release_cash').val(first_release_cash);
								second_release_cash = Comma(parseFloat(second_release_cash).toFixed(2));
								$('#second_release_cash').val(second_release_cash);
								third_release_cash = Comma(parseFloat(third_release_cash).toFixed(2));
								$('#third_release_cash').val(third_release_cash);

								$('#first_release_check').val(first_release_check);
								$('#second_release_check').val(second_release_check);
								$('#third_release_check').val(third_release_check);

								$('#first_release_date').val(first_release_date);
								$('#second_release_date').val(second_release_date);
								$('#third_release_date').val(third_release_date);
								$('#total_release_amount').val(total_release_amount);
								$('#total_release_wht').val(total_release_wht);
								$('#total_release_cash').val(total_release_cash);
								$('#total_release_net').val(total_release_net);
								$('#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_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));	
								}else{
									$('#bpm_less_ca').val(less_ca);
								}
								
								
								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();
								  
								if(default_reservation=='' || default_reservation=='0' || default_reservation=='0.00' || default_reservation=='NaN' || default_reservation=='0'){
 								 	$('#first_release_cash').val('0.00');
									$('#first_release_net').val('0.00');
									$('#total_release_cash').val('0.00');
									$('#total_release_net').val('0.00');
									$('#bpm_reservation').val('0.00');
									$('#bpm_less_discount').val('0.00');
								}

								 
 
								var default_less_discount = $('#bpm_less_discount').val();
								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();
								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();
								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();
								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();
								releaseStatus();

								var frc = $('#first_release_cash').val().replace(/,/g, "");
								var src = $('#second_release_cash').val().replace(/,/g, "");
								var trc = $('#third_release_cash').val().replace(/,/g, "");

								if(src=='' && trc==''){
									frc = Comma(parseFloat(frc).toFixed(2));
									$('#total_release_cash').val(frc);
									$('#first_release_cash').val('0.00');
									$('#second_release_cash').val('0.00');
									$('#third_release_cash').val('0.00');
								} 
						});
			}
}


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');
});



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 total 				=	tcp * (misc_expense_rate/100);
	total 					=	Comma(total.toFixed(2));
	$('#bpm_misc_expenses').val(total);	
}
 

function computeWHT(){ 

	var withhold 			=	$('#withholding_tax').val().replace(/,/g, "");
  	var first_amount		=	$('#first_release_amount').val().replace(/,/g, "");
	first_amount 			=	first_amount * (withhold/100);
	first_amount 			=	Comma(first_amount.toFixed(2)); 
	$('#first_release_wht').val(first_amount);
 
 	var second_amount		=	$('#second_release_amount').val().replace(/,/g, "");
	second_amount 			=	second_amount * (withhold/100);
	second_amount 			=	Comma(second_amount.toFixed(2)); 
	$('#second_release_wht').val(second_amount);

 	var third_amount		=	$('#third_release_amount').val().replace(/,/g, "");
	third_amount 			=	third_amount * (withhold/100);
	third_amount 			=	Comma(third_amount.toFixed(2)); 
	$('#third_release_wht').val(third_amount);

	computeNetCom();
	computeTotalRelease();
 
}

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 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);

	computeFirstRelease();
	computeSecondRelease();
	computeThirdRelease();
	computeTotalRelease();
}

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


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

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



function computeBalance(){
	var bpm_reservation		=	$('#bpm_reservation').val().replace(/,/g, "");
	var bpm_less_ca			=	$('#bpm_less_ca').val().replace(/,/g, "");
 	var balance 			=	bpm_reservation - bpm_less_ca; 
 	balance					=	Comma(balance.toFixed(2)); 
	$('#bpm_reservation_less_ca').val(balance); 
}

function computeNetCom(){
	var first_amount	= parseFloat($('#first_release_amount').val().replace(/,/g, ""));
	var first_wht 		= parseFloat($('#first_release_wht').val().replace(/,/g, ""));
	var first_cash 		= parseFloat($('#first_release_cash').val().replace(/,/g, ""));
	var first_net 		= Comma((first_amount - first_wht - first_cash).toFixed(2));
	$('#first_release_net').val(first_net);  

	var second_amount	= parseFloat($('#second_release_amount').val().replace(/,/g, ""));
	var second_wht 		= parseFloat($('#second_release_wht').val().replace(/,/g, ""));
	var second_cash 	= parseFloat($('#second_release_cash').val().replace(/,/g, ""));
	var second_net 		= Comma((second_amount - second_wht - second_cash).toFixed(2));
	$('#second_release_net').val(second_net);  

	var third_amount	= parseFloat($('#third_release_amount').val().replace(/,/g, ""));
	var third_wht 		= parseFloat($('#third_release_wht').val().replace(/,/g, ""));
	var third_cash 		= parseFloat($('#third_release_cash').val().replace(/,/g, ""));
	var third_net 		= Comma((third_amount - third_wht - third_cash).toFixed(2));
	$('#third_release_net').val(third_net); 

	computeTotalRelease();
}

function computeFirstRelease(){
	var commission	=	$('#bpm_commission').val().replace(/,/g, "");
	var first_release_amount = (commission * 0.2).toFixed(2); 
	var total_first_release_amount	=	Comma(first_release_amount);
	$('#first_release_amount').val(total_first_release_amount);
	  
	var withholding_tax = $('#withholding_tax').val().replace(/,/g, "");
	var first_release_wht = (first_release_amount * (withholding_tax/100)).toFixed(2); 
	var total_first_release_wht	=	Comma(first_release_wht);
	$('#first_release_wht').val(total_first_release_wht); 
	  
	 var cash_advance = $('#bpm_less_ca').val();
	 $('#first_release_cash').val(cash_advance); 

	 var com_amount = $('#first_release_amount').val().replace(/,/g, "");
	 var com_wht	= $('#first_release_wht').val().replace(/,/g, "");
	 var com_ca		= $('#first_release_cash').val().replace(/,/g, "");
	 var com_net 	= (com_amount - com_wht - com_ca).toFixed(2); 
	 var total_net 	= Comma(com_net);
	 $('#first_release_net').val(total_net); 
}

function computeSecondRelease(){
	var commission	=	$('#bpm_commission').val().replace(/,/g, "");
	var second_release_amount = (commission * 0.2).toFixed(2); 
	var total_second_release_amount	=	Comma(second_release_amount);
	$('#second_release_amount').val(total_second_release_amount);
	  
	var withholding_tax = $('#withholding_tax').val().replace(/,/g, "");
	var second_release_wht = (second_release_amount * (withholding_tax/100)).toFixed(2); 
	var total_second_release_wht	=	Comma(second_release_wht);
	$('#second_release_wht').val(total_second_release_wht); 
	   

	 var com_amount = $('#second_release_amount').val().replace(/,/g, "");
	 var com_wht	= $('#second_release_wht').val().replace(/,/g, "");
	 var com_ca		= $('#second_release_cash').val().replace(/,/g, "");
	 var com_net 	= (com_amount - com_wht - com_ca).toFixed(2); 
	 var total_net 	= Comma(com_net);
	 $('#second_release_net').val(total_net); 
}


function computeThirdRelease(){
	var commission					=	$('#bpm_commission').val().replace(/,/g, "");
	var third_release_amount 		=	(commission * 0.6).toFixed(2); 
	var total_third_release_amount	=	Comma(third_release_amount);
	$('#third_release_amount').val(total_third_release_amount);
	  
	var withholding_tax 		=	$('#withholding_tax').val().replace(/,/g, "");
	var third_release_wht 		=	(third_release_amount * (withholding_tax/100)).toFixed(2); 
	var total_third_release_wht	=	Comma(third_release_wht);
	$('#third_release_wht').val(total_third_release_wht); 
	   

	 var com_amount = $('#third_release_amount').val().replace(/,/g, "");
	 var com_wht	= $('#third_release_wht').val().replace(/,/g, "");
	 var com_ca		= $('#third_release_cash').val().replace(/,/g, "");
	 var com_net 	= (com_amount - com_wht - com_ca).toFixed(2); 
	 var total_net 	= Comma(com_net);
	 $('#third_release_net').val(total_net); 
}

function computeTotalRelease(){

	 var first_amount 	= parseFloat($('#first_release_amount').val().replace(/,/g, ""));
	 var second_amount 	= parseFloat($('#second_release_amount').val().replace(/,/g, ""));
	 var third_amount 	= parseFloat($('#third_release_amount').val().replace(/,/g, ""));
	 
	 var first_wht	  	= parseFloat($('#first_release_wht').val().replace(/,/g, ""));
	 var second_wht	  	= parseFloat($('#second_release_wht').val().replace(/,/g, ""));
	 var third_wht	  	= parseFloat($('#third_release_wht').val().replace(/,/g, ""));
	 
	 var first_ca		= parseFloat($('#first_release_cash').val().replace(/,/g, ""));
	 var second_ca		= parseFloat($('#second_release_cash').val().replace(/,/g, ""));
	 var third_ca		= parseFloat($('#third_release_cash').val().replace(/,/g, ""));
	 
	 var first_net 		= parseFloat($('#first_release_net').val().replace(/,/g, ""));
 	 var second_net 	= parseFloat($('#second_release_net').val().replace(/,/g, ""));
 	 var third_net 		= parseFloat($('#third_release_net').val().replace(/,/g, ""));

 	 var total_amount 	=	first_amount + second_amount + third_amount;
 	 var total_wht 		=	first_wht + second_wht + third_wht;
 	 var total_ca 		=	first_ca + second_ca + third_ca;
 	 var total_net 		= 	first_net + second_net + third_net;
 
	total_amount		= Comma(total_amount.toFixed(2)); 
	total_wht  			= Comma(total_wht.toFixed(2)); 
	total_ca 			= Comma(total_ca.toFixed(2)); 
	total_net 			= Comma(total_net.toFixed(2)); 

 	 $('#total_release_amount').val(total_amount);
 	 $('#total_release_wht').val(total_wht);
 	 $('#total_release_cash').val(total_ca);
 	 $('#total_release_net').val(total_net); 
}
 

function releaseStatus(){
	 var first_check 	= $('#first_release_checkbox'). prop("checked");
	 var second_check 	= $('#second_release_checkbox'). prop("checked");
	 var third_check	= $('#third_release_checkbox'). prop("checked");

	if(first_check==true && second_check==true && third_check==true){
		$('#releases_status').val('Fully Paid');
	}else{
		$('#releases_status').val('With Balance');
	} 
}

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

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


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


$('#withholding_tax').change(function(e){
	e.preventDefault();
	
	var reservation = $('#bpm_reservation').val();
	if(reservation==0.00 || reservation=='NaN'){
		$('#first_release_cash').val('0.00');
		$('#first_release_net').val('0.00');
	}
});

									