var city_val = new Array();
var city_id = new Array();
var city_ct = 0;


/// job prefernces start

var loc_val = new Array();
var loc_id = new Array();


var Olyloc_id = new Array();
var loc_ct = 0;
var sub_catId= Array();
var job_cnt=0;

    function select_loc_id(val) 
    {      
    	var v='';  
    	loc_val.splice(val, 1); 
    	loc_id.splice(val, 1);  
    	 
    	if(loc_val.length > 0)
    	{
    	   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) onclick=select_loc_id("'+i+'") >'+loc_val[i]+' X </a>';   
    		
    		$("#locality_placed").html(v);   
    	    loc_ct--;
    	}
    	else alert("At least one job preference required");
    }
    
    function Addlocation(name,val)  
    { 
        new_name =   name.replace(new RegExp("%20", "g"), ' ');     
        new_name =   new_name.replace(new RegExp("%2F", "g"), ' ');  
        
        loc_val.push(new_name); 
    	if(Olyloc_id.includes(val)==false)
    	{
    	  
    	  Olyloc_id.push(val.toString());
    	}
    	//if(val!='')
    	if(loc_id.includes(val)==false)
    	{
    	   loc_id.push(val.toString());
    	}
        loc_ct++;
    }
    
    function SubAddlocation(name,val)  
    {
    	if(val!='')
    	{
    	  sub_catId.push(val);
    	}
    }
    
    function checklocValue(value, arr) 
    {
        var status = 0;///var status = 'Not exist';
    
        for (var i = 0; i < arr.length; i++) 
        {
            //console.log("arr val:=>"+arr[i]+" & value function:=>"+value);
            var name = arr[i];
            if (name == value) 
            {
                status = 1;//status = 'Exist';
                break;
            }
        }
        return status;
    }
    
    function select_role_post_job(name,val,originId) 
    {   
    	/*******
    	new_name =   name.replace(new RegExp("%20", "g"), ' ');     
    	new_name =   new_name.replace(new RegExp("%2F", "g"), ' ');     
    	$("#job-category").val(new_name);
    	$("#job-category-sugeetion-ajax").hide(400);    
    	$("#job-category").next().slideUp();
    	$("#job_cat_val").val(val);**********/
        
        var tempVal=0;
        
    	if(loc_id.length <= 9)
    	{
    	    tempVal=checklocValue(val, loc_id);
    	    
            //if(loc_id.indexOf(val) < 0)
            if(tempVal==0)
            {
                $("#job_cat_val").val(val);
             	var v='';
            	new_name =   name.replace(new RegExp("%20", "g"), ' ');     
            	new_name =   new_name.replace(new RegExp("%2F", "g"), ' ');     
            	$("#job-category").val(new_name);
            	$("#job-category-sugeetion-ajax").hide(400);    
            	$("#job-category").next().slideUp();  
            	 
            	
            	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) onclick=select_loc_id("'+i+'") >'+loc_val[i]+' </a>';   
                	$.ajax({	
            			type: "POST",
            			url: URL+"/all-ajax/job.php", 
            			//////data: {select_sub_job_category:1,val:val},
            			data: {select_completeProfile_subjob_category:1,val:val},
            			beforeSend: function(){ 
            			},
            			success: function(SubCatdata)
            			{  	
            				var obje = jQuery.parseJSON(SubCatdata);  
            				if(obje==null || obje=='null')
            				{  
            				    if(Olyloc_id.includes(val)==false)
            				    {
            			           Olyloc_id.push(val);  
            				    }
            			       
    						    select_sub_selectall(2);
            				}
            				else
            				{
            				    if(Olyloc_id.includes(val)==false)
            				    {
            			           Olyloc_id.push(val);  
            				    }
            				    
        					    Subsource_list = '<a href="javascript:void(0)" id="sub_selectall" onclick=select_sub_selectall(1)>Select</a><ul>';  
            					for(t=0;t<obje.length;t++)			  
            					{
            					    if(t==0)
            					    {
            					      Subsource_list +='<li><input type="radio" checked="checked" />&nbsp;<span><b>'+obje[t].cat_name+'</b></span></li>';
            					    }
            					    
            					    if(obje[t].id==originId) 
            					        Subsource_list += '<li><input type=checkbox  checked name="sub-roles[]" class="sub-roles" value="'+obje[t].id+'" />'+obje[t].name+'</li>';
            					   else
            					        Subsource_list += '<li><input type=checkbox  name="sub-roles[]" class="sub-roles" value="'+obje[t].id+'" />'+obje[t].name+'</li>';// & '+obje[t].id+'
            					}
            				
            					Subsource_list +='</ul>';     
            					$("#job-sub-category").show(); 
            					$("#job-sub-category").html(Subsource_list);
                			}
            			}
            	   });
            	}
            	
            	$("#job-category").next().slideUp();
            	$("#job-category").val('');  
            	$("#job_cat_val").val(''); 
            	loc_ct++;  
            }
            else
        	{
        	    $("#job-category").next().slideUp();
        	    $("#job-category-sugeetion-ajax").hide(); 
        	    $("#job-category").val('');
        		alert("This role is already added, if you want to add any subrole for this role then please refresh page and add again!");
        	}
        }
        else
    	{
    	    $("#job-category").next().slideUp();
    	    $("#job-category-sugeetion-ajax").hide(); 
    	    $("#job-category").val('');
    	    //$("#job_cat_val").val('');
    		alert("You can add job preferences upto 10");
    	}
    }
    
    function select_sub_selectall(val)
    {  
		var v1='';  
		if(val==1)
		{
			$(".sub-roles").each(function()
			{
				if(this.checked) 
					sub_catId.push($(this).val());  
			});
		}
		
	  	var loc_idStr = Olyloc_id.join(",");       
    	var Subres = sub_catId.join(",");   
    	//if(Subres.length > 0)	
    	{
    		$.ajax({	
                type: "POST",
                url: URL+"/all-ajax/job.php", 
                data: {select_completeProfile_subjob_cat:1,Subval:Subres,loc_idStr:loc_idStr},
                beforeSend: function(){ 
                },
                success: function(SubCatdata)
                {  	
                	var obj = jQuery.parseJSON(SubCatdata);  
                	if(obj==null || obj=='null')
                	{
                	    $("#job-sub-category").hide();
                	}
                	else
                	{
                	    for(t=0;t<obj.length;t++)			  
					    {
					       v1 += '<a  href=javascript:void(0) onclick=select_sub_catId("'+obj[t].id+'") >'+obj[t].name+' </a>';
    				       $("#locality_placed").html(v1);
					    }
                	} 
                }
            });
    	}    
        $("#job-category").val('');  
        $("#SubRole_val").val(Subres); 
        $("#job-sub-category").hide();
    	
    }

/// Job preferences end



$(document).ready(function()
{       
    /// job prefernces start
    $("#job-category-sugeetion").hide();
	$("#job-category").click(function()
	{
		$(this).next().slideDown();
			//job_cat_set = 0;
	});
	
	$("#job-category").keyup(function() 
	{
		var job_loc_val;//="";
		var job_str="";
		job_loc_val = $("#locations_val").val();
		job_str = job_loc_val.split(",");
		   
		
	    if(job_str.length < 10)
	    {
    		$("#job-sub-category").hide();
    		$("#job-sub-category").html('');
    		$("#job_cat_val").val('');
    		$.ajax({	
    			type: "POST",
    			url: URL+"/all-ajax/job.php", 
    			data: {select_job_categoryProfile:1,keyword:$(this).val()},
    			beforeSend: function(){ 
    			},
    			success: function(data)
    			{  
    				var obj = jQuery.parseJSON(data);  
    				if(obj==null || obj=='null')
    				{  
    					$("#job-category-sugeetion").hide(); 
    					$("#job-category-sugeetion-ajax").hide();
    				}
    				else
    				{  	
    					source_first_name = obj[0].name; 
    					source_list = '<ul class="menu-box">'; 
    					
    					for(i=0;i<obj.length;i++)			  
    					source_list +='<a href="javascript:void(0)"><li onclick=select_role_post_job("'+encodeURIComponent(obj[i].catName)+'","'+obj[i].id+'","'+obj[i].originId+'") >'+obj[i].name+'</li></a>' 
    					
    					source_list +='</ul>';   
    					 
    					$("#job-category-sugeetion-ajax").show();
    					$("#job-category-sugeetion-ajax").html(source_list);
                        $("#job-category-sugeetion").hide();					
    				}   
    				
    			}
    		}); 
	    }
	    else
	    {
	        alert("You can add job preferences upto 10");
	        $("#job-sub-category").hide();
    		$("#job-sub-category").html('');
    		$("#job_cat_val").val('');
    		
    		$("#job-category").val('');
	    }
	});
    
    /// Job preferences end    
    
	  		
	$("#city_next").hide();		
	$("#profile_city_search").click(function(){ 
			$(this).next().slideDown();    
	});

	/*$("#profile_city_search").click(function(){
		
			$(this).next().slideDown();    
	});  */
	
	 
	$("body").click(function(e) 
	{  	    
		if ( e.target.id == "profile_city_search" || $(e.target).parents("#city-ajax").size() ) 
		{ 
			 //alert("Inside div");   
		} else 
		{    
			$("#profile_city_search").next().slideUp();
			$("#city-ajax").hide(400); 
		}

		if ( e.target.id == "city_next" || $(e.target).parents("#profile_city_search").size() ) 
		{ 
			 //alert("Inside div");   
		} else 
		{    
			
			//$("#select-sub-locations").next().slideUp();
			 
		}		
		
		/// job prefernces start
		
		if ( e.target.id == "job-category" || $(e.target).parents("#job-category-sugeetion-ajax").size() ) 
		{ 
			 //alert("Inside div");   
		} else 
		{    
			$("#job-category").next().slideUp();
			$("#job-category-sugeetion-ajax").hide(400);
		} 
		
		if ( e.target.id == "job-category-sugeetion" || $(e.target).parents("#job-category").size() ) 
		{ 
			 //alert("Inside div");   
		} 
		else 
		{    
			//$("#select-sub-locations").next().slideUp();
		}
		
		/// Job preferences end
		
		
	});	
		
	
	$("#profile_city_search").keyup(function() 
	{     
		$.ajax({	
			type: "POST",
			url: URL+"/all-ajax/MyDetails.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();
					$("#city_next").hide();
			        //$("#profile_city_search").val('No record found');		
			        $("#city_next").remove();
				}else
				{  	 
					source_list = '<ul class="menu-box">';  
					for(i=0;i<obj.length;i++)			  
					source_list +='<a href="javascript:void(0)"  ><li onclick=select_city("'+encodeURIComponent(obj[i].name)+'","'+obj[i].id+'") >'+obj[i].name+'</li></a>' 
					
					source_list +='</ul>';  
					$("#city-ajax").show();
					$("#city-ajax").html(source_list);
                    $("#city_next").hide(); 					
				}				
			}
		}); 
	});     
	
		
	
    /*$("#profilefrm").validate({ 
			
			errorElement:'div',
			rules:{ 
			email:{
				required: true,
				email: true,
                remote: URL+"/MyDetails/value_check.php"				
				},  
		    main_ph:{
                    required:true,
				    number:true,
				    minlength:10,
				    maxlength:10,
                    remote: URL+"/MyDetails/value_check.php"						
                },  
            age:{
				required: true,
				number:true				
				},
				
		  profile_city_search:{
				required: true							
				},
				
			gender:{
				required: true							
				},
				
			address:{
				 required:true,
				 minlength:10,
                 maxlength:2000				 
				}						
			},
			messages:{	
     			email:{
				 required:"Please enter valid Email Id",
                 remote: "This email is already registered"				 
				},   
			   main_ph:{
				required:"Please enter an Mobile No",	
				minlength : "Please enter minimum 10 digits",
				maxlength : "Max 10 digits are allowed",
                remote: "This number is already registered"				
			   },              
			  age:{
				required:"Please enter an age"				 				
			   },
			   
			   profile_city_search:{
				required:"Please select city"				 				
			   },
			   
			   gender:{
				required:"Please select gender"				 				
			   },
			   
			   address:{					
                  required:"Please Enter an address",
				  minlength : "Please Enter minimum 10 characters address",
                  maxlength : "Max 2000 characters are allowed"				  
				}             			   
            } 			  
		});

        */
        
        $("#complete_profile").validate({ 
			
			errorElement:'div',
			rules:{
            salary:{
				//required: true,
				number:true,
				minlength:4,
				maxlength:6
				},
				
		  designation:{
				//required: true
				maxlength:35
				},
				
			workedfor:{
				//required: true	
				maxlength:35
				},
				
			special:{
				 //required:true
				 maxlength:35
				},	
			/*yearsofworking:{
				required: true							
				},*/
				
			idproof:{
				 required:true			 
				},
				
			language:{
				 required:true			 
				},
				
			education:{
				 required:true,
				 maxlength:35
				},
			user_type:{
				required: true							
				}
			},
			messages:{       
			  salary:{
				//required:"Please enter salary",
				minlength:"Please enter Min 4 Digit Salary",	 
				maxlength:"Max 6 Digit Salary Is Allowed"
			   },
			   
			   designation:{
				//required:"Please enter designation"
				maxlength : "Max 35 characters are allowed"
			   },
			   
			   workedfor:{
				//required:"Please enter worked for"
				maxlength : "Max 35 characters are allowed"
			   },
			   
			   special:{					
                  //required:"Please enter specialization"	
                  maxlength : "Max 35 characters are allowed"
				},   
				
			/* yearsofworking:{
				required:"Please select working years"				 				
			   },*/
			   
			   idproof:{					
                  required:"Please select id proof"			  
				},
				
				language:{					
                  required:"Please select languages"			  
				},
				
			   education:{					
                  required:"Please enter education",
                  maxlength : "Max 35 characters are allowed"
				},
				
				user_type:{
				required:"Please select user type"				 				
			   }
            } 			  
		});
		
	
}); 

function AddCity(name,val)  
{ 
    new_name =   name.replace(new RegExp("%20", "g"), ' ');     
    new_name =   new_name.replace(new RegExp("%2F", "g"), ' ');  
    
    city_val.push(new_name); 
	city_id.push(val.toString()); 
    city_ct++;
}

function select_city(name,val)
{
    val = val.toString();
    
    if(city_ct < 5)
	{
	   if(city_id.indexOf(val) < 0)
	   {
    		var v='';
    		citynew_name =   name.replace(new RegExp("%20", "g"), ' ');     
    		citynew_name =   citynew_name.replace(new RegExp("%2F", "g"), ' ');     
    		//$("#profile_city_search").val(new_name);
    		$("#profile_city_search").val(citynew_name);
    		$("#city-ajax").hide(400);    
    		$("#profile_city_search").next().slideUp();  
    		 
    		
    		city_val.push(citynew_name); 
    		city_id.push(val);  
    		var res = city_id.join(",");  
    		$("#all_city_val").val(res);  
    		
    		for (var i = 0, len = city_val.length; i < len; i++) 
    			v += '<a  href=javascript:void(0) onclick=select_city_id("'+i+'")  >'+city_val[i]+' X </a>';
    		 
    		
    		$("#all_city_placed").html(v);
    		$("#profile_city_search").next().slideUp();
    		$("#profile_city_search").val('');
    		 
    		city_ct++;
	    }
		else
		{
            $("#city-ajax").hide(400);    
            $("#profile_city_search").next().slideUp(); 
            $("#profile_city_search").val('');
            alert("City already added!");
		}
	}else
	{
	    $("#city-ajax").hide(400);    
		$("#profile_city_search").next().slideUp();  
		$("#profile_city_search").val('');
		alert("You can't add more than 5 cities!");
	}
} 	  

function select_city_id(val) 
{ 
	var v='';  
	city_val.splice(val, 1); 
	city_id.splice(val, 1);  
	 
	
	if(city_val.length > 0)
	{
	   var res = city_id.join(",");  
	   $("#all_city_val").val(res);  
	
	  for (var i = 0, len = city_val.length; i < len; i++) 
		v += '<a  href=javascript:void(0) onclick=select_city_id("'+i+'") >'+city_val[i]+' X </a>'; 
		
		
		$("#all_city_placed").html(v); 
	    city_ct--;
	}
	else alert("Atleast one city required");
}

/*
function clear_profile_error_msg()
{
    $("label.error").hide();
    $(".error").removeClass("error");
    $("#change_pwd_error").html("");
	$("#change_pwd_success").html("");
}*/