var loc_val = new Array();
var loc_id = new Array(); 
var last;  
$(document).ready(function(){ 

	$("#city").click(function(){
			$(this).next().slideDown();    
	});  job-category-sugeetion-ajax

	
	$("#search_roles_").click(function(){ 
			$(this).next().slideDown();    
	}); 
	
	$("#select-sub-locations").click(function(){ 
			$(this).next().slideDown();
			 
	});	

	$("body").click(function(e) 
	{  
		if ( e.target.id == "city" || $(e.target).parents("#city-ajax").size() ) 
		{ 
			 //alert("Inside div");   
		} else 
		{    
			$("#city").next().slideUp();
			$("#city-ajax").hide(400); 
			$("#job-loction-sugeetion-ajax").hide(400); 
		} 	
		
		if ( e.target.id == "search_roles_" || $(e.target).parents("#suggesstion_search_roles_").size() ) 
		{ 
             //alert("Inside div");   
        } else 
		{     
			 
			$("#search_roles_").next().slideUp();     
			$("#suggesstion_search_roles_").hide(); 
			 
        }   	
	});		
	
	$("#select-sub-locations").keyup(function() 
	{     
		$.ajax({	
			type: "POST",
			url: URL+"/all-ajax/job.php", 
			data: {select_city_sub_location:1,keyword:$(this).val(),city_id:$("#search_city_id_hidden").val()},
			beforeSend: function(){ 
			},
			success: function(data)
			{   
				var obj = jQuery.parseJSON(data);  
				if(obj==null || obj=='null')
				{   
					$("#job-loction-sugeetion-ajax").hide(); 
				}else
				{  	 
					source_list = '<ul class="menu-box-3">';  
					for(i=0;i<obj.length;i++)			  
					source_list +='<a href="javascript:void(0)"  ><li onclick=select_location_post_job("'+encodeURIComponent(obj[i].location_name)+'","'+obj[i].id+'") >'+obj[i].location_name+'</li></a>' 
					
					source_list +='</ul>';  
					$("#job-loction-sugeetion-ajax").show();
					$("#job-loction-sugeetion-ajax").html(source_list); 
				}   
				
			}
		}); 
	});
	
	
	$("#city").keyup(function() 
	{     
		$.ajax({	
			type: "POST",
			url: URL+"/all-ajax/job.php", 
			data: {select_city:1,keyword:$(this).val()},
			beforeSend: function(){ 
			},
			success: function(data)
			{   
				var obj = jQuery.parseJSON(data);  
				if(obj==null || obj=='null')
				{   
					$("#city-ajax").hide(); 
				}else
				{  	 
					source_list = '<ul class="menu-box-3">';  
					for(i=0;i<obj.length;i++)			  
					source_list +='<a href="javascript:void(0)"  ><li onclick=select_sub_locns("'+encodeURIComponent(obj[i].name)+'","'+obj[i].id+'") >'+obj[i].name+'</li></a>' 
					
					source_list +='</ul>';  
					$("#city-ajax").show();
					$("#city-ajax").html(source_list); 
				}   
				
			}
		}); 
	});
	
	 
	
	$("#search_roles_").keyup(function() 
	{  
		 
		//$("#job-role-ul").hide();
		$.ajax({	
			type: "POST",
			url: URL+"/all-ajax/job.php", 
			data: {select_job_category:1,keyword:$(this).val()},
			beforeSend: function(){ 
			},
			success: function(data)
			{  
				
				var obj = jQuery.parseJSON(data);  
				if(obj==null || obj=='null')
				{  
					 
					$("#suggesstion_search_roles_").hide();
					//$("#job-role-ul").show();
					
					
				}else
				{  	
					source_first_name = obj[0].name; 
					source_list = '<ul class="menu-box-3">'; 
					
					for(i=0;i<obj.length;i++)			  
					source_list +='<li><a href="javascript:void(0)" onclick=search_by_category("'+i+'","'+encodeURIComponent(obj[i].name)+'","'+obj[i].id+'")>'+obj[i].name+'</a></li>'
					
					
					source_list +='</ul>';   
					$("#suggesstion_search_roles_").show();
					$("#suggesstion_search_roles_").html(source_list); 
				}   
				
			}
		}); 
	});  
	
}); 	  
	
 
 
function select_location_post_job(name,val) 
{
	 
	var v='';
	new_name =   name.replace(new RegExp("%20", "g"), ' ');     
	new_name =   new_name.replace(new RegExp("%2F", "g"), ' ');     
	$("#select-sub-locations").val(new_name);
	$("#job-loction-sugeetion-ajax").hide(400);    
	$("#select-sub-locations").next().slideUp();  
	$("#locality_placed").html(v);
	 
	
	loc_val.push(new_name); 
	loc_id.push(val);  
	var res = loc_id.join(",");  
	$("#locations_val").val(res);  
	for (var i = 0, len = loc_val.length; i < len; i++) 
		v += '<a  href="javascript:void(0)"><li  onclick=select_loc_id("'+i+'")  >'+loc_val[i]+'</li></a> <br>';
	 
	$("#locality_placed").html(v);
	 
	  
}
 
function select_loc_id(val) 
{      
	var v='';
	loc_val.splice(val, 1); 
	loc_id.splice(val, 1);   
	var res = loc_id.join(",");  
	$("#locations_val").val(res);  
		
	for (var i = 0, len = loc_val.length; i < len; i++) 
		v += '<a  href="javascript:void(0)"><li  onclick=select_loc_id("'+i+'") >'+loc_val[i]+'</li></a> <br>'; 
	
	
	$("#locality_placed").html(v); 
}

function select_sub_locns_alt(name,val)
{ 
	
	$("#search_city_id_hidden").val(val);  
	new_name =   name.replace(new RegExp("%20", "g"), ' ');     
	new_name =   new_name.replace(new RegExp("%2F", "g"), ' ');     
	$("#city").val(new_name);    
	$("#city").next().slideUp();
	$("#select-sub-locations").val('');
	$("#city-ajax").hide(400);
	$("#city_val").val(val);
	$("#locality_placed").html(""); 
	 
	
	var source_list ='';
	
	$.ajax({	
		type: "POST",
		url: URL+"/all-ajax/job.php", 
		data: {select_city_sub_location:1,ajax_set:3,city_id:$("#search_city_id_hidden").val()},
		beforeSend: function(){ 
		},
		success: function(data)
		{   
			 
			var obj = jQuery.parseJSON(data);  
			if(obj==null || obj=='null')
			{   
				$("#select-sub-locations").val('Entire City');  
			}else
			{  	 
				//source_list = '<ul>';  
				for(i=0;i<obj.length;i++)			  
				source_list +='<a href="javascript:void(0)"  ><li onclick=select_location_post_job("'+encodeURIComponent(obj[i].location_name)+'","'+obj[i].id+'") >'+obj[i].location_name+'</li></a>'   
				//source_list +='</ul>';    
				$("#ul-location-ajax").html(source_list); 
			
			}   
			
		}
	}); 
		
} 


function search_by_category(val,name,id)
{   
	new_name =   name.replace(new RegExp("%20", "g"), ' ');     
	new_name =   new_name.replace(new RegExp("%2F", "g"), ' ');     
	$("#search_roles_").val(new_name); 
	$("#search_roles_").next().slideUp();     
	$("#suggesstion_search_roles_").hide();	
	$("#search_by_category").val(id);	  
} 	       