var mods 			=	$('#main_page').val();
var types 			=	$('#main_type').val(); 
var action 			=	$('#main_action').val();
if(mods=="reservation" && types=="monitoring_folders" && (action=="edit" || action=="view")){
	load_monitoring_folder_details(); 
}

function save_monitoring_folders_batch_update(){
	var batch_details		= 	$('#batch_update_form').serialize(); 
	var mods 					=	$('#main_page').val();
	var types 					=	$('#main_type').val(); 
	var action 					=	$('#main_action').val();
	var dataString				=	batch_details+"&action="+encodeURIComponent(action); 
	$.post('./ajax/monitoring_batch_updates.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=reservation'; }, 1000);
		}else{
				bootbox.alert("Failed in saving.");
		}
	});
}

function save_monitoring_folders(){
				var monitoring_details		= 	$('#monitoring_folders_form').serialize(); 
				var mods 					=	$('#main_page').val();
				var types 					=	$('#main_type').val(); 
				var action 					=	$('#main_action').val();
				var mainid					=	$('#main_id').val(); 
				var dataString				=	monitoring_details+"&action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid); 
						$.post('./ajax/monitoring.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=reservation'; }, 1000);
								}else{
									bootbox.alert("Failed in saving.");
								}
						});
}

function load_construction_and_utilities_icons(){
	var action	= $('#main_action').val();	 
	var block 	= $('#monitoring_block').val();
	var lot		= $('#monitoring_lot').val();

	var dataString		=	"action="+encodeURIComponent(action)+"&block="+encodeURIComponent(block)+"&lot="+encodeURIComponent(lot);
						$.post('./ajax/load_construction_and_utilities_icons.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								
								var road					=	obj.data[0].road;
								var side_walk		 		=	obj.data[0].side_walk;
								var meralco					=	obj.data[0].meralco;
								var water					=	obj.data[0].water;
								var house_construction	 	=	obj.data[0].house_construction;
								var house_construction_percentage =	obj.data[0].house_construction_percentage; 
							 
								if(road=='yes'){
									$('#road').attr('checked',true);
								} 

								if(side_walk=='yes'){
									$('#side_walk').attr('checked',true);
								}

								if(meralco=='yes'){
									$('#meralco').attr('checked',true);
								}

								if(water=='yes'){
									$('#water').attr('checked',true);
								}

								if(house_construction=='yes'){
									$('#house_construction').attr('checked',true);
								}

								$('#house_construction_percentage').val(house_construction_percentage);

								if(house_construction_percentage=='100.00'){
									$('#house_construction').attr('checked',true);
								}



							});
}

function load_permits_and_licences_icons(){
	var action	=	$('#main_action').val();	 
	var block 	= $('#monitoring_block').val();
	var lot		= $('#monitoring_lot').val();

	var dataString		=	"action="+encodeURIComponent(action)+"&block="+encodeURIComponent(block)+"&lot="+encodeURIComponent(lot);
						$.post('./ajax/load_permits_and_licences_icons.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								
								var block					=	obj.data[0].block;
								var tax_dec_land_processed	=	obj.data[0].TaxDecLandProcessed;
								var tax_dec_land_released	=	obj.data[0].TaxDecLandReleased;

								var tax_dec_building_processed	=	obj.data[0].TaxDecBuildingProcessed;
								var tax_dec_building_released	=	obj.data[0].TaxDecBuildingReleased;

								var tax_receipt_land_processed	=	obj.data[0].TaxReceiptLandProcessed;
								var tax_receipt_land_released	=	obj.data[0].TaxReceiptLandReleased;

								var tax_receipt_building_processed	=	obj.data[0].TaxReceiptBuildingProcessed;
								var tax_receipt_building_released	=	obj.data[0].TaxReceiptBuildingReleased;
 
								var building_permit_processed	=	obj.data[0].BuildingPermitProcessed;
								var building_permit_released	=	obj.data[0].BuildingPermitReleased;

								var electrical_permit_processed	=	obj.data[0].ElectricalPermitProcessed;
								var electrical_permit_released	=	obj.data[0].ElectricalPermitReleased;

								var sanitary_permit_processed	=	obj.data[0].SanitaryPermitProcessed;
								var sanitary_permit_released	=	obj.data[0].SanitaryPermitReleased;

								var occupancy_permit_processed	=	obj.data[0].OccupancyPermitProcessed;
								var occupancy_permit_released	=	obj.data[0].OccupancyPermitReleased;
								 
						//tax_dec_land_processed	&&	tax_dec_land_released START	
								if((tax_dec_land_processed=='NULL' && tax_dec_land_processed=="" && tax_dec_land_processed=='1970-01-01') && (tax_dec_land_released=='NULL' && tax_dec_land_released=="" && tax_dec_land_released=='1970-01-01')){
									$('#tax_dec_land_process').html('');
								} 
								if(tax_dec_land_processed!='NULL' && tax_dec_land_processed!="" && tax_dec_land_processed!='1970-01-01'){
									$('#tax_dec_land_process').html('<span class="glyphicon glyphicon-repeat"></span>');
								}

								if(tax_dec_land_released!='NULL' && tax_dec_land_released!="" && tax_dec_land_released!='1970-01-01'){  
									$('#tax_dec_land_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								} 
								if((tax_dec_land_processed!='NULL' && tax_dec_land_processed!="" && tax_dec_land_processed!='1970-01-01') && (tax_dec_land_released!='NULL' && tax_dec_land_released!="" && tax_dec_land_released!='1970-01-01')){  
									$('#tax_dec_land_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								}
						//tax_dec_land_processed	&&	tax_dec_land_released END		

						//tax_dec_building_processed	&&	tax_dec_building_released START	
								if((tax_dec_building_processed=='NULL' && tax_dec_building_processed=="" && tax_dec_building_processed=='1970-01-01') && (tax_dec_building_released=='NULL' && tax_dec_building_released=="" && tax_dec_building_released=='1970-01-01')){
									$('#tax_dec_building_process').html('');
								} 
								if(tax_dec_building_processed!='NULL' && tax_dec_building_processed!="" && tax_dec_building_processed!='1970-01-01'){
									$('#tax_dec_building_process').html('<span class="glyphicon glyphicon-repeat"></span>');
								}

								if(tax_dec_building_released!='NULL' && tax_dec_building_released!="" && tax_dec_building_released!='1970-01-01'){  
									$('#tax_dec_building_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								} 
								if((tax_dec_building_processed!='NULL' && tax_dec_building_processed!="" && tax_dec_building_processed!='1970-01-01') && (tax_dec_building_released!='NULL' && tax_dec_building_released!="" && tax_dec_building_released!='1970-01-01')){  
									$('#tax_dec_building_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								}
						//tax_dec_building_processed	&&	tax_dec_building_released END		
 
 						//tax_receipt_land_processed	&&	tax_receipt_land_released START	
								if((tax_receipt_land_processed=='NULL' && tax_receipt_land_processed=="" && tax_receipt_land_processed=='1970-01-01') && (tax_receipt_land_released=='NULL' && tax_receipt_land_released=="" && tax_receipt_land_released=='1970-01-01')){
									$('#tax_receipt_land_process').html('');
								} 
								if(tax_receipt_land_processed!='NULL' && tax_receipt_land_processed!="" && tax_receipt_land_processed!='1970-01-01'){
									$('#tax_receipt_land_process').html('<span class="glyphicon glyphicon-repeat"></span>');
								}

								if(tax_receipt_land_released!='NULL' && tax_receipt_land_released!="" && tax_receipt_land_released!='1970-01-01'){  
									$('#tax_receipt_land_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								} 
								if((tax_receipt_land_processed!='NULL' && tax_receipt_land_processed!="" && tax_receipt_land_processed!='1970-01-01') && (tax_receipt_land_released!='NULL' && tax_receipt_land_released!="" && tax_receipt_land_released!='1970-01-01')){  
									$('#tax_receipt_land_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								}
						//tax_receipt_land_processed	&&	tax_receipt_land_released END		

						//tax_receipt_building_processed	&&	tax_receipt_land_released START	
								if((tax_receipt_building_processed=='NULL' && tax_receipt_building_processed=="" && tax_receipt_building_processed=='1970-01-01') && (tax_receipt_building_released=='NULL' && tax_receipt_building_released=="" && tax_receipt_building_released=='1970-01-01')){
									$('#tax_receipt_building_process').html('');
								} 
								if(tax_receipt_building_processed!='NULL' && tax_receipt_building_processed!="" && tax_receipt_building_processed!='1970-01-01'){
									$('#tax_receipt_building_process').html('<span class="glyphicon glyphicon-repeat"></span>');
								}

								if(tax_receipt_building_released!='NULL' && tax_receipt_building_released!="" && tax_receipt_building_released!='1970-01-01'){  
									$('#tax_receipt_building_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								} 
								if((tax_receipt_building_processed!='NULL' && tax_receipt_building_processed!="" && tax_receipt_building_processed!='1970-01-01') && (tax_receipt_building_released!='NULL' && tax_receipt_building_released!="" && tax_receipt_building_released!='1970-01-01')){  
									$('#tax_receipt_building_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								}
						//tax_receipt_building_processed	&&	tax_receipt_building_released END		

						//building_permit_processed	&&	building_permit_released START	
								if((building_permit_processed=='NULL' && building_permit_processed=="" && building_permit_processed=='1970-01-01') && (building_permit_released=='NULL' && building_permit_released=="" && building_permit_released=='1970-01-01')){
									$('#building_permit_process').html('');
								} 
								if(building_permit_processed!='NULL' && building_permit_processed!="" && building_permit_processed!='1970-01-01'){
									$('#building_permit_process').html('<span class="glyphicon glyphicon-repeat"></span>');
								}

								if(building_permit_released!='NULL' && building_permit_released!="" && building_permit_released!='1970-01-01'){  
									$('#building_permit_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								} 
								if((building_permit_processed!='NULL' && building_permit_processed!="" && building_permit_processed!='1970-01-01') && (building_permit_released!='NULL' && building_permit_released!="" && building_permit_released!='1970-01-01')){  
									$('#building_permit_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								}
						//building_permit_processed	&&	building_permit_released END		
								    
						//electrical_permit_processed	&&	electrical_permit_released START	
								if((electrical_permit_processed=='NULL' && electrical_permit_processed=="" && electrical_permit_processed=='1970-01-01') && (electrical_permit_released=='NULL' && electrical_permit_released=="" && electrical_permit_released=='1970-01-01')){
									$('#electrical_permit_process').html('');
								} 
								if(electrical_permit_processed!='NULL' && electrical_permit_processed!="" && electrical_permit_processed!='1970-01-01'){
									$('#electrical_permit_process').html('<span class="glyphicon glyphicon-repeat"></span>');
								}

								if(electrical_permit_released!='NULL' && electrical_permit_released!="" && electrical_permit_released!='1970-01-01'){  
									$('#electrical_permit_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								} 
								if((electrical_permit_processed!='NULL' && electrical_permit_processed!="" && electrical_permit_processed!='1970-01-01') && (electrical_permit_released!='NULL' && electrical_permit_released!="" && electrical_permit_released!='1970-01-01')){  
									$('#electrical_permit_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								}
						//electrical_permit_processed	&&	electrical_permit_released END		
								    
						//sanitary_permit_processed	&&	sanitary_permit_released START	
								if((sanitary_permit_processed=='NULL' && sanitary_permit_processed=="" && sanitary_permit_processed=='1970-01-01') && (sanitary_permit_released=='NULL' && sanitary_permit_released=="" && sanitary_permit_released=='1970-01-01')){
									$('#sanitary_permit_process').html('');
								} 
								if(sanitary_permit_processed!='NULL' && sanitary_permit_processed!="" && sanitary_permit_processed!='1970-01-01'){
									$('#sanitary_permit_process').html('<span class="glyphicon glyphicon-repeat"></span>');
								}

								if(sanitary_permit_released!='NULL' && sanitary_permit_released!="" && sanitary_permit_released!='1970-01-01'){  
									$('#sanitary_permit_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								} 
								if((sanitary_permit_processed!='NULL' && sanitary_permit_processed!="" && sanitary_permit_processed!='1970-01-01') && (sanitary_permit_released!='NULL' && sanitary_permit_released!="" && sanitary_permit_released!='1970-01-01')){  
									$('#sanitary_permit_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								}
						//sanitary_permit_processed	&&	sanitary_permit_released END		
								     
						//occupancy_permit_processed	&&	occupancy_permit_released START	
								if((occupancy_permit_processed=='NULL' && occupancy_permit_processed=="" && occupancy_permit_processed=='1970-01-01') && (occupancy_permit_released=='NULL' && occupancy_permit_released=="" && occupancy_permit_released=='1970-01-01')){
									$('#occupancy_permit_process').html('');
								} 
								if(occupancy_permit_processed!='NULL' && occupancy_permit_processed!="" && occupancy_permit_processed!='1970-01-01'){
									$('#occupancy_permit_process').html('<span class="glyphicon glyphicon-repeat"></span>');
								}

								if(occupancy_permit_released!='NULL' && occupancy_permit_released!="" && occupancy_permit_released!='1970-01-01'){  
									$('#occupancy_permit_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								} 
								if((occupancy_permit_processed!='NULL' && occupancy_permit_processed!="" && occupancy_permit_processed!='1970-01-01') && (occupancy_permit_released!='NULL' && occupancy_permit_released!="" && occupancy_permit_released!='1970-01-01')){  
									$('#occupancy_permit_process').html('<span class="glyphicon glyphicon-ok"></span>'); 
								}
						//occupancy_permit_processed	&&	occupancy_permit_released END		
								       
 								 
						});
}

//LOADING MONITORING OF FOLDERS DETAILS
function load_monitoring_folder_details(){
				$('.supervisor').hide();
				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);
						$.post('./ajax/load_monitoring.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								var full_name			=	obj.data[0].full_name;
								var block				=	obj.data[0].block;
								var lot					=	obj.data[0].lot; 
								var monitoring_folders	=	obj.data[0].monitoring_folders; 
								var user_type			=	obj.data[0].user_type; 
								var borrower_validation	=	obj.data[0].borrower_validation; 
								var floor_area			=	obj.data[0].floor_area; 
								var lot_size			=	obj.data[0].lot_size; 
								var pricelist_status	=	obj.data[0].pricelist_status; 
								 if(borrower_validation=="01/01/1970"){ 	borrower_validation = null; }
								if(user_type=="documentation_supervisor"){
									$('.supervisor').show();
								}
								 
								$('#monitoring_pricelist_status').select2().select2('val',pricelist_status);
								$('#monitoring_fullname').val(full_name);
								$('#monitoring_block').val(block);
								$('#monitoring_lot').val(lot);  
								$('#borrowers_validation').val(borrower_validation);  
								$('#monitoring_floor_area').val(floor_area);  
								$('#monitoring_lot_size').val(lot_size);  

								var obj2	=	$.parseJSON(monitoring_folders);
								
								if(obj2!=null){
									for(var c=0; c<obj2.length; c++){
										 $("input[name='monitoring_folders[]']").each( function () {
												if(obj2[c]==$(this).val()){
													$(this).prop('checked', true);
												}
										   });
									}
								}else{
									obj2 = "";
								}


								load_permits_and_licences_icons();
								load_construction_and_utilities_icons();
						});
}