// 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){
	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')
			}
		});
	}
}


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);
			}
		}
	
	});

}

function check_region_duplicate(region, location)
{
	if(region.length > 0 && location.length > 0){
		$.post('/ajax/check_region_duplicate/'+ region + '/' + location +'/', function(data){
			if(data=="True")
			{
				alert(region + ", "+ location +" already exists. Please choose it from the list or add a new region.");
				$('#id_region_sel_txt').attr('value', ''); 
			}
		});
	}
	else
	{
		if(region.length < 0)
		{
			alert('Please fill in a region');
		}
		else 
		{
			alert('Please fill in a location');
			$('#id_region_sel_txt').attr('value', ''); 
		}
	}
}

// Get associated regions to provinces
function ajax_regions(loc, sel){
    if(loc != ''){
		$("#id_region_sel").html('<option value="">---------</option>');
		$.getJSON('/ajax/get_regions/'+ loc +'/', 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_sel").append(new_html);
				}
			}
		});
	}
	else {
		$('#id_region_sel').html('<option value = "">---------</option')
	}
}

// Get associated regions to provinces
function ajax_suburbs(reg, sel){
	if(reg != ''){
		$("#id_suburb").html('<option value="">---------</option>');
		$.getJSON('/ajax/get_suburbs/'+ reg +'/', 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_provinces_regions(sel){
		var new_html = '<option value="">Province, Region</option>';
        $.post('/ajax/get_provinces_regions/' + sel + '/', function(data){
			if(data){
            $('#id_search_provinces').html('');	
            $('#id_search_provinces').html(data);
			}
		});
}


function ajax_load_items(id, category) {
		$.post('/ajax/load_items/' + category + '/', function(data){
		if (data){
			html = data
			$(id).html(html);
		}		
		});
}


// Get associated suburbs to regions
function ajax_regions_suburbs(sel, sel_option){
        var new_html = '<option value="all-suburbs">Suburb</option>';
		if(sel != ''){
        $.post('/ajax/get_regions_suburbs/'+ sel + '/' + sel_option + '/', function(data){
			if(data){
            $('#id_search_regions_suburbs').html('');	
            $('#id_search_regions_suburbs').html(data);		
			}
		});
        } else{
            $('#id_search_regions_suburbs').append(new_html); 
        }
}

function ajax_property_types(sel, prov, sel_option){
    prov = prov.split('|')
    reg = prov[1]
    var new_html = '<option value="all-property-types">Property Type</option>';
    if(sel != ''){
        $.post('/ajax/get_property_types/' + sel + '/' + reg + '/' + sel_option + '/', function(data){
            if(data){
                $('#id_search_property_types').html('');	
                $('#id_search_property_types').html(data);
            }
        });
        }
        else{
            $(ele).append(new_html); 
        }
}

function add_new_region(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 ajax_load_province_regions() {
	
		$.post('/ajax/load_province_regions/', function(data){
		if (data){
			html = data
			$('#id_province_regions').html(html);
		}
			
		});
}

function ajax_update_order(quantity, order_id) {
		
		$.post('/ajax/update_item/' + order_id + '/' + quantity + '/', function(data){
		if (data=="True")
		{
			window.location('/mytoprocks/cart_item_edit/' + order_id + '/');
		}
		else
		{
			alert("You have requested more items then currently in stock.");				
		}
			
		});
}

