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


$(document).ready(function(){       
	  		
	$("#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();
			 
		}		
	});	
		
	
	$("#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
				}
			},
			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"
				}
            } 			  
		});
		
	
}); 

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("");
}*/