// Front end ajax functions

// Call the python function get alert string and del the session
function AjaxDeleteSession(id){
	$.post('/ajax/session/delete/'+id+'/', function(data){
		if(data != 'None'){
			alert(data);
		}
	});
}

// Delete images for the management system via an ajax call
function ajaxDelImg(id, img, num, mode){
	if(confirm('Are you sure you want to delete this image?')){
		$.post('/ajax/delimg/' + mode +'/' + id + '/' + img + '/', function(data){
			$(".imgdel_hide"+num).hide();
			$(".imgdel_show"+num).show();
			if(mode == 'logo'){
			   toggle_logo('cancel')
			}
		});
	}
}

// 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][0] == sel){
						new_html = '<option value="'+data[i][0]+'" selected>'+data[i][1]+'</option>';
					}
					else{
						new_html = '<option value="'+data[i][0]+'">'+data[i][1]+'</option>';
					}
					$("#id_region").append(new_html);
				}
			}
		});
	}
	else {
		$('#id_region').html('<option value = "">---------</option')
	}
}


// Get associated packages for developments
function ajax_packages(dev, sel){
	if(dev != ''){
		$("#id_package").html('<option value="">---------</option>');
		$.getJSON('/ajax/get-packages/'+ dev +'/', function(data){
			if(data){
				for(i=0; i<data.length; i++) {
					var new_html = '';
					if(data[i][0] == sel){
						new_html = '<option value="'+data[i][0]+'" selected>'+data[i][1]+'</option>';
					}
					else{
						new_html = '<option value="'+data[i][0]+'">'+data[i][1]+'</option>';
					}
					$("#id_package").append(new_html);
				}
			}
		});
	}
}

function AjaxDelFile(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 AjaxDelPlan(current_num, id){
	if(confirm("Are you sure you want to delete this file?")){
		$.post('/ajax/delplan/'+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 ajax_unique_dev(dev){
	if(dev !=''){
		$.post('/ajax/check-dev/'+dev+'/', function(data){
		if (data == 'True'){
			alert('This Development Already Exists')
			$("#id_development_name").val('')
		}
		});
	}
}

function ajax_load_estates() {
	
		
		$.post('/ajax/get-estates/', function(data){
		if (data){
			html = data
			$('#id_estates').html(html);
		}
			
		});
	//<a href="/home-building-packages/executive/">The Executive</a>
}