$(document).ready(function(){ 
	var mods 			=	$('#main_page').val();
	var types 			=	$('#main_type').val(); 
	var action 			=	$('#main_action').val(); 
	if(mods=="reservation" && types=="reservation" && (action=="edit" || action=="view" || action=="transfer")){
			load_reservation(); 
	}

	if(action=="create"){
		$("#current_agent").prop("disabled", true);
	}
 	
	$('.container_coborrower1').hide();
	$('.container_coborrower2').hide();
	$('.container_coborrower3').hide();

	$('#reservation_with_coborrower').click(function(){
		var result = $('#reservation_with_coborrower').prop('checked');
		if(result==false){
			$('.container_coborrower1').hide();
			$('.container_coborrower2').hide();
			$('.container_coborrower3').hide();
			$('#coborrower_num').val(0);
		}else{
			$('#coborrower_num').val(1);
			showCoBorrower();
		}
	});

	$('#coborrower_num').change(function(){
		showCoBorrower();
	});
	
	//CHECK CUSTOM PRICELIST DATE 
	$('input[name="check_pricelist_date"]').click(function(){
            /*if($(this).prop("checked") == true){ 
            }else if($(this).prop("checked") == false){ 
            }*/
			var r = $('#reservation_date').val();  
			loadSelect1('pricelist','#div_pricelist','select.select2',r);  
	});
	
	//IS CASH PAYMENT 
	$('input[name="reservation_cash_payment"]').click(function(){
            if($(this).prop("checked") == true){
              
            }else if($(this).prop("checked") == false){
               
            }
	});
});

function showCoBorrower(){
	var num	=	$('#coborrower_num').val();
	if(num=="1" || num=="2" || num=="3"){
		$('#reservation_with_coborrower').prop('checked', true);
	}else{
		$('#reservation_with_coborrower').prop('checked', false);
	}
	
	if(num=="1"){
		$('.container_coborrower1').show();
		$('.container_coborrower2').hide();
		$('.container_coborrower3').hide();
	}else if(num=="2"){
		$('.container_coborrower1').show();
		$('.container_coborrower2').show();
		$('.container_coborrower3').hide();
	}else if(num=="3"){
		$('.container_coborrower1').show();
		$('.container_coborrower2').show();
		$('.container_coborrower3').show();
	}else{
		$('.container_coborrower1').hide();
		$('.container_coborrower2').hide();
		$('.container_coborrower3').hide();
	}
}


//SAVING RESERVATION
function save_reservation(){ 
				var mods 					=	$('#main_page').val();
				var types 					=	$('#main_type').val(); 
				var action 					=	$('#main_action').val();
				var mainid					=	$('#main_id').val();
				var rtransfer				=	$('#rtransfer').val(); 
				var myform = $('#reservation_form');
				myform.find('select:disabled').removeAttr('disabled');
				myform.find(':input:disabled').removeAttr('disabled');
				var reservation_details		=	$('#reservation_form').serialize();
				
				var p = $('#current_phase').val();
				var b = $('#current_block').val();
				var l = $('#current_lot').val();
				var pd = $('#pricelist_date').val();
				 
				var rd = $('#reservation_date').val();
				var et = $('#employee_type').val();
				var cm = $('#current_model').val();
				var cp = $('#current_project').val();
				var cb = $('#current_broker').val();
				var ca = $('#current_agent').val();
				
				if(pd=='' || pd=='1970-01-01'){
					bootbox.alert("Required Fields"); 
				}else if(rd=='' || rd=='1970-01-01'){
					bootbox.alert("Required Fields"); 
				}else if(p=='' || b=='' || l==''){
					bootbox.alert("Required Fields"); 
				}else if(cm=='' || cp==''){
					bootbox.alert("Required Fields"); 
				}else if(cb=='' || ca=='' || et=='' || et=='-'){
					bootbox.alert("Required Fields"); 
				}else{
						var dataString				=	reservation_details+"&action="+encodeURIComponent(action)+"&mainid="+encodeURIComponent(mainid)+"&rtransfer="+encodeURIComponent(rtransfer);  
						$.post('./ajax/reservation.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.");
								}
						});
				}
				
				
}
 
function reloadBroker(){ 
	loadSelectBroker('broker','#div_broker','select.select2');
}

function reloadAgent(){   
	var b = $('#current_broker').val().replace(/_/g, ' ');  
	loadSelectAgent('agent','#div_agent','select.select2',b);
}

function reloadEmployeeType(){ 
	loadSelect('employee_type','#div_employee_type','select.select2'); 
}

function reloadBuyerProfile(){ 
	loadSelect('buyer_profile','#div_buyer_profile','select.select2'); 
}	

function reloadPosition(){
	loadSelect('employee_position','#div_employee_position','select.select2'); 
}

function reloadPosition1(){
	loadSelect('position_co1','#div_position_co1','select.select2');
}

function reloadPosition2(){
	loadSelect('position_co2','#div_position_co2','select.select2'); 
}

function reloadPosition3(){
	loadSelect('position_co3','#div_position_co3','select.select2'); 
}
 
function reloadEmployeeType1(){
		loadSelect('employee_type_co1','#div_employee_type_co1','select.select2'); 
}

function reloadEmployeeType2(){
		loadSelect('employee_type_co2','#div_employee_type_co2','select.select2'); 
}

function reloadEmployeeType3(){
		loadSelect('employee_type_co3','#div_employee_type_co3','select.select2'); 
}

function callPricelist(){
	var action 				=	$('#main_action').val();
	var pricelist_date		=	$('#pricelist_date').val();
	var project 			=	$('#current_project').val();
	var model	 			=	$('#current_model').val();  
	var reservation_date 	=	$('#reservation_date').val(); 
	var dataString			=	"action="+encodeURIComponent(action)+"&default="+encodeURIComponent(pricelist_date)+"&project="+encodeURIComponent(project)+"&model="+encodeURIComponent(model)+"&reservation_date="+encodeURIComponent(reservation_date);
	$.post('./ajax/load_pricelist_box.php', dataString, function(msg){ 
		//$('#div_pricelist').html(msg); 
				var obj	= $.parseJSON(msg);
				var disc = obj.output.pricelist; 
			$('#div_pricelist').html(disc); 
	});
	 
}
//callPricelist();
//LOADING RESERVATION DETAILS
function load_reservation(){
				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_reservation.php', dataString, function(msg){ 
								var obj	= $.parseJSON(msg);
								var project				=	obj.data[0].project;
								var model				=	obj.data[0].model;
								var reservationDate		=	obj.data[0].reservation_date;
								var pricelistDate		=	obj.data[0].pricelist_date;  
								var custom_price		=	obj.data[0].custom_price; 
							 	 
								if(custom_price=="yes"){
									$('#check_pricelist_date').prop('checked', true); 
								}
								
								var phase				=	obj.data[0].phase;
								var block				=	obj.data[0].block;
								var lot					=	obj.data[0].lot;
 
							 $('#reservedate').val(reservationDate);   
								
								
								var last_name			=	obj.data[0].last_name;
								var first_name			=	obj.data[0].first_name;
								var middle_name			=	obj.data[0].middle_name;
								var broker				=	obj.data[0].broker;
								var agent				=	obj.data[0].agent;
								var spouse				=	obj.data[0].spouse;
								var home_tel			=	obj.data[0].home_tel;
								var cell_no				=	obj.data[0].cell_no;
								var spouse_contact		=	obj.data[0].spouse_contact;
								var address1			=	obj.data[0].address1;
								var address2			=	obj.data[0].address2;
								var employer_name		=	obj.data[0].employer_name;
								var employee_type		=	obj.data[0].employee_type;
								var gross_income		=	Comma(obj.data[0].gross_income);
								 
								var position			=	obj.data[0].position;
								var buyer_profile		=	obj.data[0].buyer_profile;
								var spouse_income		=	Comma(obj.data[0].spouse_income);
								var tinno				=	obj.data[0].tinno;
								var office_tel			=	obj.data[0].office_tel;
								var aggregate_income	=	Comma(obj.data[0].aggregate_income);
								var num_borrower		=	obj.data[0].num_borrower;
								var details				=	obj.data[0].details; 
								
								var spa					=	obj.data[0].spa;
								var spa_contactno		=	obj.data[0].spa_contactno;
								var spa_email			=	obj.data[0].spa_email;
								var spouse_email		= 	obj.data[0].spouse_email; //26MAR23
								var construction_status =   obj.data[0].construction_status; //28MAR23
								var tcp					=	obj.data[0].tcp;
								var loanable_amount		=	obj.data[0].loanable_amount;
								tcp =  Comma(tcp);
								loanable_amount =  Comma(loanable_amount);
								$('#reservation_loanable_amount').val(loanable_amount); 
								$('#reservation_tcp').val(tcp); 
								
								$('#reservepricedate').val(pricelistDate); 
								$('#pricelist_date').val(pricelistDate); 

								var r = $('#reservation_date').val();   
								//loadSelect1('pricelist','#div_pricelist','select.select2',reservationDate,pricelistDate);  
								loadPhaseBlockLot(project,model,pricelistDate,phase,block,lot);  

								//$('#pricelist_date').select2().select2('val',pricelistDate);

								$('#reservation_date').val(reservationDate);
								$('#reservation_last_name').val(last_name);
								$('#reservation_first_name').val(first_name);
								$('#reservation_middle_name').val(middle_name);
								$('#reservation_home_telno').val(home_tel);
								 
								$('#reservation_spouse').val(spouse);
								$('#reservation_spouse_contactno').val(spouse_contact);
								$('#reservation_address1').val(address1);
								$('#reservation_address2').val(address2);
								$('#reservation_employer_name').val(employer_name);
								
								$('#reservation_gross_income').val(gross_income); 
								$('#reservation_cellphone_no').val(cell_no); 
								$('#reservation_income_of_spouse').val(spouse_income); 
								$('#coborrower_num').val(num_borrower);
								$('#reservation_tinno').val(tinno);
								$('#reservation_office_telno').val(office_tel);
								$('#reservation_aggregate_income').val(aggregate_income);
								
								$('#reservation_spa').val(spa);								
								$('#reservation_spa_contactno').val(spa_contactno);
								$('#reservation_spa_email').val(spa_email);
								$('#reservation_spouse_email').val(spouse_email); //26MAR23
								$('#reservation_construction_status').val(construction_status); //28MAR23
			
								if(num_borrower>0){
									$('#reservation_with_coborrower').prop('checked', true);
									$('.container_coborrower1').hide();
									$('.container_coborrower2').hide();
									$('.container_coborrower3').hide();
									
										var obj2	= $.parseJSON(details); 
										var b=1;
										if(num_borrower==1){
												$('.container_coborrower1').show();
										}else if(num_borrower==2){
												$('.container_coborrower1').show();
												$('.container_coborrower2').show();
										}else if(num_borrower==3){
												$('.container_coborrower1').show();
												$('.container_coborrower2').show();
												$('.container_coborrower3').show();
										}
										for(var a=0; a<obj2.details.length; a++){   
												$('#reservation_coborrower'+b).val(obj2.details[a].coborrower);
												$('#reservation_tinno_co'+b).val(obj2.details[a].tinno);
												$('#reservation_office_telno_co'+b).val(obj2.details[a].office_telno);
												$('#reservation_cellphoneno_co'+b).val(obj2.details[a].cellphoneno);
												$('#reservation_email_co'+b).val(obj2.details[a].email);
												$('#reservation_employers_name_co'+b).val(obj2.details[a].employer); 
												$('#reservation_grossincome_co'+b).val(Comma(obj2.details[a].gross_income)); 
												$('#reservation_relation_to_principal_co'+b).val(obj2.details[a].principal_relation);
												b++;
										}
								}
  								 
								if(action=="view"){
									$('input').attr("disabled", true);
									$('textarea').attr("disabled", true);
									$('select').attr("disabled", true);
									$('#current_project').select2('disable');
									$('#current_model').select2('disable'); 
									$('.glyphicon-trash').hide();
									$('.glyphicon-plus').hide();  
									$('.refresh').hide();  
								}
					 
					//	$('#pricelist_date').select2().select2('val',pricelistDate);		
						
								//for transfer save old
								$('#transfer_pricelist_date').val(pricelistDate);   
								$('#transfer_reservation_date').val(reservationDate);
								$('#transfer_reservation_last_name').val(last_name);
								$('#transfer_reservation_first_name').val(first_name);
								$('#transfer_reservation_middle_name').val(middle_name);
								$('#transfer_reservation_home_telno').val(home_tel);
					 
								$('#transfer_reservation_spouse').val(spouse);
								$('#transfer_reservation_spouse_contactno').val(spouse_contact);
								$('#transfer_reservation_address1').val(address1);
								$('#transfer_reservation_address2').val(address2);
								$('#transfer_reservation_employer_name').val(employer_name); 
								$('#transfer_reservation_gross_income').val(gross_income); 
								$('#transfer_reservation_cellphone_no').val(cell_no); 
								$('#transfer_reservation_income_of_spouse').val(spouse_income); 
								$('#transfer_coborrower_num').val(num_borrower);
								$('#transfer_reservation_tinno').val(tinno);
								$('#transfer_reservation_office_telno').val(office_tel);
								$('#transfer_reservation_aggregate_income').val(aggregate_income);  
								$('#transfer_broker').val(broker);  
								$('#transfer_agent').val(agent);  
								$('#transfer_spa').val(spa);  
								$('#transfer_spa_contactno').val(spa_contactno);  
								$('#transfer_spa_email').val(spa_email);  
								$('#transfer_spouse_email').val(spouse_email);  //26MAR23
								$('#transfer_construction_status').val(construction_status); //28MAR23
						});
}
  
//DELETE RESERVATION
function delete_reservation(id){
		var transaction		=	"reservation";
		var mods 			=	$('#main_page').val();
		var types 			=	$('#main_type').val(); 
		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; }, 1000);
								}else{
									bootbox.alert("Failed in Deleting ..."); 
									window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types; }, 1000);
								}	
						});
}

//UPDATE RESERVATION STATUS
function cancel_reservation_status(id){
		var mods 			=	$('#main_page').val();
		var types 			=	$('#main_type').val(); 
		var dataString		=	"mainid="+encodeURIComponent(id); 
		$.post('./ajax/cancel_reservation.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types; }, 1000);
								}	
		});
}


//UPDATE UNDO CANCELLED RESERVATION STATUS
function undo_cancel_reservation_status(id){
		var mods 			=	$('#main_page').val();
		var types 			=	$('#main_type').val(); 
		var dataString		=	"mainid="+encodeURIComponent(id); 
		$.post('./ajax/undo_cancel_reservation.php', dataString, function(msg){
								var obj	= $.parseJSON(msg);
								if(obj.output==true){
									window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types; }, 1000);
								}	
		});
}


//CANCEL RESERVATION
function cancelReservation(id){ 
				var mods 	= $('#main_page').val();
				var types 	= $('#main_type').val(); 
				var action 	= $('#main_action').val(); 
				var save_message 	= "Do you want reservation to be transferred to new buyer?";
				var save_message2 	= "Do you really want  to cancel this account?";
				var save_message3 	= "Do you really want  to transfer this account?";
				bootbox.dialog({
				  message: save_message, 
				  buttons: {
					success: {
						  label: "Yes",
						  className: "btn-success",
						  callback: function() {
							//window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types+'&action=edit&rtransfer=yes&id='+id; }, 1000);
							
							
							bootbox.dialog({
									  message: save_message3, 
									  buttons: {
										success: {
											  label: "Yes",
											  className: "btn-success",
											  callback: function() {
											  		bootbox.dialog({
													message: "Username &nbsp;&nbsp;<input type='text' required class='form-control' id='confirm_user'/><br/>Password &nbsp;&nbsp;<input type='password' required class='form-control' id='confirm_pass'/>",
													title: "Authorize Password",
													buttons: {
														main: {
															label: "Confirm",
															className: "btn-primary",
															callback: function() {
																var f = $('#confirm_pass').val(); 
																var uname = $('#confirm_user').val(); 
																
																if(f!=''){
																	$.ajax({
																		url: './ajax/check_pass_admin.php',
																		type: 'POST',
																		data: {text: f,user:uname},
																		success: function(data){  
											
																			    if(data==true){
																					//cancel_reservation_status(id);
																					window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types+'&action=edit&rtransfer=yes&id='+id; }, 1000);
 																				}else{
																					bootbox.alert("Invalid Password");
																				}
																		} 
																	});
																}else{
																	bootbox.alert('Invalid Password');
																}
															}
														}
													}
												});
											  }
										},
										danger: {
										  label: "No",
										  className: "btn-danger",
										  callback: function() { 
												window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types; }, 1000);
											}
										}
									  }
									});
							
						  }
					},
					danger: {
					  label: "No",
					  className: "btn-danger",
					  callback: function() { 
							 //cancel_reservation_status(id);
								bootbox.dialog({
									  message: save_message2, 
									  buttons: {
										success: {
											  label: "Yes",
											  className: "btn-success",
											  callback: function() {
												//cancel_reservation_status(id);
												
													/*bootbox.dialog({
														message: "Password &nbsp;&nbsp;<input type='password' required class='form-control' id='confirm_pass'/>",
														title: "Authorize Password",
														buttons: {
															main: {
																label: "Confirm",
																className: "btn-primary",
																callback: function() {
																	var f = $('#confirm_pass').val();
																	if(f!=''){
																		$.ajax({
																			url: './ajax/check_pass.php',
																			type: "POST",
																			data: {text: f},
																			success: function(data){
																				if(data==true){
																					cancel_reservation_status(id);
 																				}else{
																					bootbox.alert("Invalid Password");
																				}
																			} 
																		});
																	}else{
																		bootbox.alert("Invalid Password");
																	}
																}
															}
														}
													});*/
													
													bootbox.dialog({
													message: "Username &nbsp;&nbsp;<input type='text' required class='form-control' id='confirm_user'/><br/>Password &nbsp;&nbsp;<input type='password' required class='form-control' id='confirm_pass'/>",
													title: "Authorize Password",
													buttons: {
														main: {
															label: "Confirm",
															className: "btn-primary",
															callback: function() {
																var f = $('#confirm_pass').val(); 
																var uname = $('#confirm_user').val(); 
																
																if(f!=''){
																	$.ajax({
																		url: './ajax/check_pass_admin.php',
																		type: 'POST',
																		data: {text: f,user:uname},
																		success: function(data){  
											
										 									    if(data==true){
																					cancel_reservation_status(id);
 																				}else{
																					bootbox.alert("Invalid Password");
																				}
																		} 
																	});
																}else{
																	bootbox.alert('Invalid Password');
																}
															}
														}
													}
												});
												
											  }
										},
										danger: {
										  label: "No",
										  className: "btn-danger",
										  callback: function() { 
												window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types; }, 1000);
											}
										}
									  }
									});
							 
						}
					}
				  }
				});
}


//UNDO CANCEL RESERVATION
function undoCancelReservation(id){ 
				var mods 	= $('#main_page').val();
				var types 	= $('#main_type').val(); 
				var action 	= $('#main_action').val();  
				var save_message2 	= "Do you really want to activate this cancelled account?";
				bootbox.dialog({
									  message: save_message2, 
									  buttons: {
										success: {
											  label: "Yes",
											  className: "btn-success",
											  callback: function() {  
													bootbox.dialog({
														//message: "Password &nbsp;&nbsp;<input type='password' required class='form-control' id='confirm_pass'/>",
														message: "Username &nbsp;&nbsp;<input type='text' required class='form-control' id='confirm_user'/><br/>Password &nbsp;&nbsp;<input type='password' required class='form-control' id='confirm_pass'/>", 
														title: "Authorize Password",
														buttons: {
															main: {
																label: "Confirm",
																className: "btn-primary",
																callback: function() {
																	/*
																	var f = $('#confirm_pass').val();
																	if(f!=''){
																		$.ajax({
																			url: './ajax/check_pass.php',
																			type: "POST",
																			data: {text: f},
																			success: function(data){
																				if(data==true){
																					undo_cancel_reservation_status(id);
 																				}else{
																					bootbox.alert("Invalid Password");
																				}
																			} 
																		});
																	}else{
																		bootbox.alert("Invalid Password");
																	}*/
																	var f = $('#confirm_pass').val(); 
																	var uname = $('#confirm_user').val(); 
																	var usertype = $('#usertype').val(); 																	
																	if(f!=''){
																		$.ajax({
																			url: './ajax/check_pass_admin.php',
																			type: 'POST',
																			data: {text: f,user:uname},
																			success: function(data){ 
																				//alert(usertype);
																				if(data==true){ 
																					if(usertype=='admin'){
																						undo_cancel_reservation_status(id);
																					}else{
																						bootbox.alert('No access');
																					} 
																				}else{
																					bootbox.alert('Invalid Password');
																				}
																			} 
																		});
																	}else{
																		bootbox.alert('Invalid Password');
																	}
																}
															}
														}
													});
												
												
											  }
										},
										danger: {
										  label: "No",
										  className: "btn-danger",
										  callback: function() { 
												window.setTimeout(function() { window.location.href = 'index.php?pg='+mods+'&type='+types; }, 1000);
											}
										}
									  }
									});
}


		$('#reservation_date').on('change', function(){
			var a = $('#current_project').val().replace(/_/g, ' ');
			var c = $('#current_model').val().replace(/_/g, ' ');
			var r = $('#reservation_date').val();
			loadSelect1('pricelist','#div_pricelist','select.select2',r);
		});
	
		
