$(document).ready(function(){
	//loadBrokers();

	//SHOW LIST OF BROKER
	$("#broker_list").click(function(){
			var broker = $('#current_broker').val();  
			$('.broker_list_modal').modal({
			  keyboard: false,
			  backdrop: 'static'
			}); 
			var main_type = "broker_list";
			var customBar = "";
			var rightalign = "";
			var statusrow = "";
			var extra = "";
			var allow_order = "";
			var datealign = "";
			loadScrollList('#'+main_type+'_table','./list/'+main_type+'.php',customBar,rightalign,allow_order,no_allow_order);
	});

	//SHOW LIST OF AGENT
	$("#agent_list").click(function(){
			var agent = $('#current_agent').val(); 
			$('.agent_list_modal').modal({
			  keyboard: false,
			  backdrop: 'static'
			}); 
	});
	/*
	loadReference('#buyer_profile','buyer_profile');
	loadReference('#position','position');
	//loadReference('#employee_type','employee_type');

	loadReference('#reservation_position_co1','position');
	loadReference('#reservation_employee_type_co1','employee_type');

	loadReference('#reservation_position_co2','position');
	loadReference('#reservation_employee_type_co2','employee_type');

	loadReference('#reservation_position_co3','position');
	loadReference('#reservation_employee_type_co3','employee_type');*/
});

	

	function loadBrokers(){
			$.ajax({ url: "./ajax/load_broker.php",
				 async: true,
				 cache: true,
				 success:
				 function(msg) { 
					var broker = jQuery.parseJSON(msg);
					for(var i = 0 ; i < broker.length ; i++){
						$('#current_broker').append('<option value="'+broker[i].Broker+'">'+broker[i].Broker+'</option>');
					}
					loadAgents();
				 }
			});
	}

	function loadAgents(){
			$("#current_agent").select2("val", "");
			var broker = $("#current_broker").val(); 
			if(broker!=""){
				$("#current_agent").prop("disabled", false);
			}
			$.ajax({ url: "./ajax/load_agent.php",
				 data: {broker_name: broker},
				 type: 'get',
				 async: true,
				 cache: true,
				 success:
				 function(msg){
						$('#current_agent').find('option').remove().end();
						var agent = jQuery.parseJSON(msg);
						if(agent.length!=0){
							for(var i = 0 ; i < agent.length ; i++){
								$('#current_agent').append('<option value="'+agent[i].Agent+'">'+agent[i].Agent+'</option>'); 
							}
						} 
				 }
			});
	}

	function loadReference(refId,refValue){
			var refload = refValue;
			$(refId).select2("val", "");
			$.ajax({ url: "./ajax/load_references.php",
				 data: {reference: refload},
				 type: 'get',
				 async: true,
				 success:
				 function(msg){
						$(refId).find('option').remove().end();
						var model = jQuery.parseJSON(msg);
						for(var i = 0 ; i < model.length ; i++){ 
							$(refId).append('<option value="'+model[i].Reference+'">'+model[i].Reference+'</option>');
						}
				 }
			});
	}
	
	function loadBlock(){
			var project	 		= $('#current_project').val();
			var model	 		= $('#current_model').val();
			var phase	 		= $('#reservation_phase').val();
			var reserve_date	= $('#reservation_reserve_date').val();
			
			var dataString 		= "phase="+encodeURIComponent(phase)+"&reserved_date="+encodeURIComponent(reserve_date)+"&project="+encodeURIComponent(project)+"&model="+encodeURIComponent(model); 
				$('#reservation_block').empty();
				$('#reservation_lot').empty();
				$('#reservation_block').select2("val","");
				$('#reservation_lot').select2("val","");
			$.post('./ajax/load_block.php', dataString, function(msg){
				var obj	= $.parseJSON(msg); 
						var details	= $.parseJSON(obj.data[0].block_details);
						if(details.details.length!=0 || details.details.length!=null){ 
								for(var a=0; a<details.details.length; a++){
									if(phase==details.details[a].phase){
										var block				=	details.details[a].block;
										var lot					=	details.details[a].lot;
										var blockExist = $("#reservation_block option[value='"+block+"']").length > 0;
										if(blockExist==false){
											$('#reservation_block').append('<option value="'+block+'">'+block+'</option>'); 
										} 
										
										var lotExist = $("#reservation_lot option[value='"+lot+"']").length > 0;
										if(lotExist==false){
											$('#reservation_lot').append('<option value="'+lot+'">'+lot+'</option>'); 
										}
									}
									
								}
						}
			});
	}
	
	
function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key] == val) {
            objects.push(obj);
        }
    }
    return objects;
} 


	function getBlockDetails(){ 
			var project	 		= $('#current_project').val();
			var model	 		= $('#current_model').val();
			var rblock	 		= $('#reservation_block').val();
			var rlot	 		= $('#reservation_lot').val();
			var phase	 		= $('#reservation_phase').val();
			var reserve_date	= $('#reservation_reserve_date').val(); 
			var dataString 		= "phase="+encodeURIComponent(phase)+"&reserved_date="+encodeURIComponent(reserve_date)+"&project="+encodeURIComponent(project)+"&model="+encodeURIComponent(model); 
				$.post('./ajax/load_block.php', dataString, function(msg){
					var obj	= $.parseJSON(msg);
							//current selected	
							var fblock	 		= $('#reservation_block').val();
							var flot	 		= $('#reservation_lot').val();
							var fphase	 		= $('#reservation_phase').val();

							var details2	= $.parseJSON(obj.data[0].block_details);
							if(details2.details.length!=0 || details2.details.length!=null){ 
									for(var a=0; a<details2.details.length; a++){ 
										if((fphase==details2.details[a].phase) && (fblock==details2.details[a].block) && (flot==details2.details[a].lot)){
											 
											var block				=	details2.details[a].block;
											var lot					=	details2.details[a].lot; 
											var pricelist_status	=	details2.details[a].pricelist_status;
											var lot_size			=	details2.details[a].lot_size;
											var lot_type			=	details2.details[a].lot_type;
											var floor_area			=	details2.details[a].floor_area;
											var contract_price		=	details2.details[a].contract_price;
											var loanable_amount		=	details2.details[a].loanable_amount;
											var pf_net				=	details2.details[a].pf_net;
											var pf_payable			=	details2.details[a].pf_payable;
											var required_gross		=	details2.details[a].required_gross;
											var amortization		=	details2.details[a].amortization;
											var phase				=	details2.details[a].phase;  
											 
											$('#reserve_predef_pricelist_status').val(pricelist_status);	
											$('#reserve_predef_lot_size').val(lot_size);
											$('#reserve_predef_lot_type').val(lot_type);
											$('#reserve_predef_floor_area').val(floor_area);
											$('#reserve_predef_contract_price').val(contract_price);
											$('#reserve_predef_loanable_amount').val(loanable_amount);
											$('#reserve_predef_pf_net').val(pf_net);
											$('#reserve_predef_pf_payable').val(pf_payable);
											$('#reserve_predef_required_gross').val(required_gross);
											$('#reserve_predef_amortization').val(amortization);
										}
									}
							}
				});
	}