$(function(){
	
	$(".search_result input[type='checkbox']").change(function(){
		
		var unit_id = $(this).val();
		
		if($(this).is(':checked')) {
			api('lettings', '_model', 'add_compare', [unit_id], function(response){});
		}
		else {
			api('lettings', '_model', 'remove_compare', [unit_id], function(response){});
		}

	});
	
	$("#basic_search select[name='date_start']").change(function(){
		
		var sel_date = $(this).val();
		
		api(lettings_module_variant, '_model', 'get_durations_for_date', [sel_date], function(response){
		
			$("#basic_search select[name='duration'] option").each(function(){
				
				var val = $(this).val();
				$(this).attr("disabled","true");
				$(this).removeAttr("selected");
				
				for(var i=0; i < response.length; i++) {
					if(val == response[i]) {
						$(this).removeAttr("disabled");
					}
				}

			});
			
			$("#basic_search select[name='duration'] option:first").removeAttr("disabled");
			$("#basic_search select[name='duration']").val("");
		});
		
	});
	
	$("#search_refiner_form select[name='date_start']").change(function(){
		
		var sel_date = $(this).val();
		
		api(lettings_module_variant, '_model', 'get_durations_for_date', [sel_date], function(response){
		
			$("#search_refiner_form select[name='duration'] option").each(function(){
				
				var val = $(this).val();
				$(this).attr("disabled","true");
				$(this).removeAttr("selected");
				
				for(var i=0; i < response.length; i++) {
					if(val == response[i]) {
						$(this).removeAttr("disabled");
					}
				}

			});
			
			$("#search_refiner_form select[name='duration'] option:first").removeAttr("disabled");
			$("#search_refiner_form select[name='duration']").val("");
		});
		
	});
	
	$("#bookingsSearchSubmit").click(function(){
		
		var ref = $("#bookingsSearchForm input[name='ref']").val();
		var unit_id = $("#bookingsSearchForm select[name='unit_id']").val();
		var last_name = $("#bookingsSearchForm input[name='last_name']").val();
		
		api('lettings', '_view', 'admin_bookings_search_result', [ref, unit_id, last_name], function(html){
				
			$("#bookingsSearchResults").html(html);
				
		});
		
	});
	
	$("#checkVoucherCodeButton").click(function(){

		var code = $("input[name='voucher_code']").val();
		
		api('lettings', '_model', 'check_voucher_code', [code], function(response){
			
			if(response == 'invalid') {
				
			}
			else {
				
				var discount = response;
				
				console.log(discount);

			}
			
		});

	});
});
