// Ajax Functions

function updateElement(ele, data, sel_opt){
  
	$("#"+ele).html('');
	options = '<option value="">---------</option>'
    var opts = data.split("~");
    for (i=0; i<opts.length-1; i++) {
		val_text = opts[i].split('|')
		if(sel_opt == val_text[0]){
			options += '<option value="' + val_text[0] +'" selected="selected">' + val_text[1] + '</option>'
		}
		else{
			options += '<option value="' + val_text[0] +'">' + val_text[1] + '</option>'
		}
	}
	$("#"+ele).html(options);
}

function AjaxFetchDataSelect_Single(url_prefix, value, sel_opt, ele){
	if(value != ''){
		$.ajax({
			url:     '/ajax/'+url_prefix+'/'+value+'/',
			async:   false,
			success: function(data){
				updateElement(ele, data, sel_opt)
			}
		});
	}
}

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){
            if (img == "floor_plan") { 
    			$(".imgdel_hide"+num).hide();
	    		$(".imgdel_show"+num).show();
            } else {
        		$("#imgdel_hide"+num).hide();
	    		$("#imgdel_show"+num).show();
                if(mode == 'logo'){
                   toggle_logo('cancel')
                }
            }
		});
	}
}

function ajaxDelImgFla(id, img, num){
	var color = $('#id_li_img' + num).hasClass('alt')
	if(confirm('Are you sure you want to delete this image?')){
		$.post('/ajax/delimg/image/' + id + '/' + img + '/', function(data){
			$("#imgdel_hide"+num).hide();
			$("#imgdel_hide"+num).after(embedObject(num, color));
		});
	}
}

function AjaxIsAgency(value){
	if(value != ''){
		$.post('/ajax/mem/isagency/'+value+'/', function(data){
			if(data == 'false'){
				$('.agent_form').hide()
			}
			else{
				$('.agent_form').show()
			}
		});
	}
}

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)
			}
		});
	}
}

// Get associated regions to provinces
function ajax_regions(prov, sel, frm){
	if(prov != ''){
		$("#id_region").html('<option value="">Loading...</option>').attr('disabled', true)
		$.getJSON('/ajax/get-regions/'+ prov +'/', function(data){
			if(data){
				$('#id_region').removeAttr('disabled')
				if(frm == 'services'){
					$("#id_region").html('<option value="">Any Region</option>')
				}
				else{
					$("#id_region").html('')
				}
				for(i=0; i<data.length; i++) {
					var new_html = '';
					new_html = '<option value="'+data[i][0]+'">'+data[i][1]+'</option>';
                    $("#id_region").append(new_html)
				}
				$("#id_region").val(sel)
			}
		});
	}
}

// Get associated suburbs to regions and provinces
function ajax_suburbs(prov, reg, sel){
	if(reg != ''){
		$("#id_suburb").html('<option value="">Loading...</option>').attr('disabled', true)
		$.getJSON('/ajax/get-suburbs/'+ prov +'/'+ reg +'/', function(data){
			if(data){
				$('#id_suburb').removeAttr('disabled')
				$("#id_suburb").html('<option value="">Any Suburb</option>')
				for(i=0; i<data.length; i++){
					var new_html = '';
					new_html = '<option value="'+data[i][0]+'">'+data[i][1]+'</option>';
                    $("#id_suburb").append(new_html)
				}
				$("#id_suburb").val(sel)
			}
		});
	}
}

