// Front end ajax functions

// Call the python function get alert string and del the session
function AjaxDeleteSession(id){
	$.post('/ajax/delsess/'+id+'/', function(data){
		if(data != 'none'){
			alert(data);
		}
	});
}

// 
function ajaxDelImg(id, img, num, mode,listing_type){
	if(confirm('Are you sure you want to delete this image?')){
        if (listing_type == "None") {
            url = '/ajax/delimg/' + mode +'/' + id + '/' + img + '/';
            
        } else {
            url = '/ajax/delimg/' + mode +'/' + id + '/' + img + '/' + listing_type + '/';
        }
		$.post(url, function(data){
			$(".imgdel_hide"+num).hide();
			$(".imgdel_show"+num).show();
			if(mode == 'logo'){
			   toggle_logo('cancel')
			}
		});
	}
}



function ajaxDelUpload(file,num){
    
	if(confirm('Are you sure you want to delete this image?')){
		$.post('/ajax/delete-temp-file/' + file +'/' , function(data){
		
			$('#id_src_' + num).hide();
            $('#id_href_' + num).hide();
            set_image_uploader_single(num);
    

		});
	}
}

function AjaxDelDoc(current_num, id){
	if(confirm("Are you sure you want to delete this file?")){
		$.post('/ajax/deldoc/'+id+'/', function(data){
			if(data == 'done'){
				var prev_num = current_num - 1
				var app_html = '<label>&nbsp;</label><a href="#1" onclick="add_new_document(%s)">Add Another Document</a> &nbsp; | &nbsp; <a href="#1" onclick="remove_document(%s)">Remove This Document</a>'
				$("#id_document"+current_num).remove()
				$("#id_document_num").val(count_forms())
				$("#id_doc_control"+prev_num).html(app_html.replace(/%s/g, prev_num))
				if(current_num == 1){
					$("#id_document_num").val('1')
					add_new_document(1)
				}
				//window.location.reload(true)
			}
		});
	}
}

function AjaxDelFile(id, obj_id){
	if(confirm("Are you sure you want to delete this file?")){
		$.post('/ajax/delfile/'+id+'/', function(data){
			if(data == 'done'){
				
			}
		});
	}
}


function ajax_listing_count(sel, ele) {
	$(ele).html('Listing Status');
	$.getJSON('/ajax/get_listing_count/', function(data){
		if(data){
			for(i=0; i<data.length; i++) {
				new_html = '<option value="'+data[i][0]+'">'+data[i][0]+ ' (' +data[i][1] +')</option>';
				$(ele).append(new_html);
			}
		}
	
	});

}


// Get associated regions to provinces
function ajax_regions(prov, sel){
	if(prov != ''){
		$("#id_region").html('<option value="">---------</option>');
		$.getJSON('/ajax/get_regions/'+ prov +'/', function(data){
			if(data){
				for(i=0; i<data.length; i++) {
					var new_html = '';
					if(data[i] == sel){
						new_html = '<option value="'+data[i]+'" selected>'+data[i]+'</option>';
					}
					else{
						new_html = '<option value="'+data[i]+'">'+data[i]+'</option>';
					}
					$("#id_region").append(new_html);
				}
			}
		});
	}
	else {
		$('#id_region').html('<option value = "">---------</option')
	}
}

// Get associated regions to provinces
function ajax_suburbs(reg, sel){
	if(reg != ''){
		$("#id_suburb").html('<option value="">---------</option>');
		var random_number = Math.floor(Math.random()*100000001)
		$.getJSON('/ajax/get_suburbs/'+ reg +'/?random_number=' + random_number, function(data){
			if(data){
				for(i=0; i<data.length; i++) {
					var new_html = '';
					if(data[i] == sel){
						new_html = '<option value="'+data[i]+'" selected>'+data[i]+'</option>';
					}
					else{
						new_html = '<option value="'+data[i]+'">'+data[i]+'</option>';
					}
					$("#id_suburb").append(new_html);
				}
			}
		});
	}
	else {
		$('#id_suburb').html('<option value = "">---------</option')
	}
}

// Get associated suburbs to regions
function ajax_regions_suburbs(sel,ele,sel_option){
		
		var new_html = '<option value="">Region, Suburb</option>';
		if(sel != ''){
		$.post('/ajax/get_regions_suburbs/'+ sel + '/' + sel_option + '/' , function(data){
			if(data){
				
            $(ele).html('');	
            $(ele).html(data);		
			}
		});
		}
        else {
            $(ele).append(new_html);
           }
}

function ajax_switch_property_status(status, ele_rent, ele_sale) {
	if(status == 'For Sale|Commercial' || status == 'For Sale|Industrial'
	   || status == 'For Sale|Retail' || status == 'For Sale|Vacant land' || status == 'For Sale'){
		$(ele_rent).hide();
		$(ele_sale).show();
	}
	else{
		$(ele_rent).show();
		$(ele_sale).hide();
	}
}

function ajax_listings_type(sel){
	$('#id_search_listing_status').html('<option value="">Listing Type</option>');
	var new_html = '';
	$.getJSON('/ajax/get_listings_type/', function(data){
		if(data){
			new_html += '<optgroup label="For Sale">'
			$('#id_search_listing_status').append(new_html);
			for(i=0; i<data.length; i++) {
				if(data[i][0] == 'For Sale'){
					region_data = data[i][0] + '|' + data[i][1] 
					if(region_data == sel){
							new_html = '<option value="'+data[i][0]+'|'+ data[i][1] +'" selected>'+data[i][1]+'</option>';
					}
					else{
						new_html = '<option value="'+data[i][0]+'|'+ data[i][1] +'">'+data[i][1]+'</option>';
					}
				$('#id_search_listing_status').append(new_html);
				}
			}
			new_html += '<optgroup label="To Let">'
			$('#id_search_listing_status').append(new_html);
			for(i=0; i<data.length; i++) {
				if(data[i][0] == 'To Let'){
					region_data = data[i][0] + '|' + data[i][1]
					if(region_data == sel){
						new_html = '<option value="'+data[i][0]+'|'+ data[i][1] +'" selected>'+data[i][1]+'</option>';
					}
					else{
						new_html = '<option value="'+data[i][0]+'|'+ data[i][1] +'">'+data[i][1]+'</option>';
					}
				$('#id_search_listing_status').append(new_html);
				}
			}		
		}
	});
	
}

function ajax_property_type(sel, ele1, ele2,sel_option){
	
	var suburb = sel.split('|');
	var list_type = ele1.split('|');
	
	$(ele2).html('<option value="all">Property Type</option>');
	new_html = '<option value="all">All Property Types</option>';
	$(ele2).append(new_html);
	$.getJSON('/ajax/get_property_type/' + suburb[2] +'/' + list_type[0] +'/' + list_type[1] +'/', function(data){
		if(data){

			for(i=0; i<data.length; i++) {
				if(data[i][0] == sel_option){
					new_html = '<option value="'+data[i][0]+'" selected=selected>'+data[i][0] + ' (' + data[i][1]  +')</option>';
				}
				else{
					new_html = '<option value="'+data[i][0]+'">'+data[i][0] + ' (' + data[i][1]  +')</option>';
				}
				$(ele2).append(new_html);
			}
			}
	});
	
}

function add_new_suburb(box) {
	select_box = '#id_' + box ;
	input_box = select_box + '_txt';
	link = '#id_' + box + '_link';
	
	if ($(select_box).is(':visible')) {
		$(select_box).hide();
		$(input_box).val('').show();
		$(link).html('Cancel');
	} else {
		$(select_box).val('').show();
		$(input_box).hide();
		$(link).html('Add New');
	}
}

function ajaxShowImage(file, order, file_name, elem) {
        new_html = "";
		if(order != ''){
		$.post('/ajax/show-image/' + file + '/' + order +'/' + file_name + '/' + elem +'/' , function(data){
			if(data){
				
            $(ele).html('');	
            $(ele).html(data);		
			}
		});
		}
        else {
            $(ele).append(new_html);
           }
}

