var timeLeft = 30;

var myInput = document.getElementById("newpassword");
var letter = document.getElementById("password_letter");
var capital = document.getElementById("password_capital");
var number = document.getElementById("password_number");
var password_length = document.getElementById("password_length");
var special_character=document.getElementById("password_special_character");

letter.classList.add("invalid");
capital.classList.add("invalid");
number.classList.add("invalid");
password_length.classList.add("invalid");
special_character.classList.add("invalid");

document.getElementById("message").style.display = "none";

// When the user clicks on the password field, show the message box
myInput.onfocus = function() {
  document.getElementById("message").style.display = "block";
}

// When the user clicks outside of the password field, hide the message box
myInput.onblur = function() {
  document.getElementById("message").style.display = "none";
}

// When the user starts to type something inside the password field
myInput.onkeyup = function() {
   $("#password_message").show();	
	
  // Validate lowercase letters
  var lowerCaseLetters = /[a-z]/g;
  if(myInput.value.match(lowerCaseLetters)) {  
    letter.classList.remove("invalid");
    letter.classList.add("correct");
  } else {
    letter.classList.remove("correct");
    letter.classList.add("invalid");
  }
  
  // Validate capital letters
  var upperCaseLetters = /[A-Z]/g;
  if(myInput.value.match(upperCaseLetters)) {  
    capital.classList.remove("invalid");
    capital.classList.add("correct");
  } else {
    capital.classList.remove("correct");
    capital.classList.add("invalid");
  }

  // Validate numbers
  var numbers = /[0-9]/g;
  if(myInput.value.match(numbers)) {  
    number.classList.remove("invalid");
    number.classList.add("correct");
  } else {
    number.classList.remove("correct");
    number.classList.add("invalid");
  }
  
  // Validate length
  if(myInput.value.length >= 6) {
    password_length.classList.remove("invalid");
    password_length.classList.add("correct");
  } else {
    password_length.classList.remove("correct");
    password_length.classList.add("invalid");
  }
  
  // Validate special character
   var Spaecialcharacters = /[!@#\$%\^\&*]/g;
  if(myInput.value.match(Spaecialcharacters)) {  
    special_character.classList.remove("invalid");
    special_character.classList.add("correct");
  } else {
    special_character.classList.remove("correct");
    special_character.classList.add("invalid");
  }
  
  if((myInput.value.match(lowerCaseLetters)) && (myInput.value.match(upperCaseLetters)) && (myInput.value.match(numbers)) && (myInput.value.password_length >= 6) && (myInput.value.match(Spaecialcharacters)))
  {
	  $("#password_message").hide();
  }
  else $("#password_message").show();
   
}

$(document).ready(function(){       
	
	$("#password_message").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');		 					
				}else
				{  	 
					source_list = '<ul>';  
					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(); 					
				}				
			}
		}); 
	});     
	 
	 
	 
	
	$("#Add_More_Mobile").click(function(e) 
	{   
		$("#Add_More_Mobile_PopUp").modal({
			show: 'true',
			backdrop: 'static',
			keyboard:true
			}); 
	});
	
	$("#Add_More_Email").click(function(e) 
	{    
		$("#Add_More_Email_Popup").modal({
			show: 'true',
			backdrop: 'static',
			keyboard:true
		}); 
	});
	
	$("#change_password").click(function(e) 
	{   
		$("#Change_Password_PopUp").modal({
			show: 'true',
			backdrop: 'static',
			keyboard:true
			});   
	});
		
	
    $("#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"				  
				}             			   
            } 			  
		});

        $("#changepwd_frm").validate({ 
			
			errorElement:'div',
			rules:{ 
			oldpassword:{
				required: true							
				},		 
				
		    newpassword:{
				required: true							
				},

            confirmpassword:{
				required: true							
				}				
			},
			messages:{	
     			oldpassword:{
				 required:"Please enter old password"			 						
				},		
			   
			   newpassword:{
				required:"Please enter new password"				 				
			   },
			   
			   confirmpassword:{
				required:"Please enter confirm password"				 				
			   }			                			   
            } 				
      });  

	  
	   $("#add_email_form").validate({ 
			
			errorElement:'div',
			rules:{ 
			   add_more_email:{
				required: true,
				email: true  
				}		
			},
			messages:{	
     			add_more_email:{
				 required:"Please Enter  Email",
				 email: "Please enter valid email"
				}	                			   
            } 				
        }); 
		
		$("#add_email_form" ).submit(function(event) 
		{
			if ($('#add_email_form').valid()) 
			{    
					event.preventDefault();    
					$.ajax({	
					type: "POST",
					url: URL+"/MyDetails/value_check.php",			
					data: {add_more_email:$("#add_more_email").val()},
					beforeSend: function(){ 
					},
					success: function(data)
					{  	 
						 if(data=="true")
							location.href = URL+"/MyDetails/default.php?addEmail="+$("#add_more_email").val();  
						 else
						 {
							 alert("Email is already exist");
							 $("#add_more_email").val('');
						 }
					}
					});        
					 
			}  
		});

        $("#edit_email_form").validate({ 
			
			errorElement:'div',
			rules:{ 
			   edit_more_email:{
				required: true,
				email: true  
				}		
			},
			messages:{	
     			edit_more_email:{
				 required:"Please Enter  Email",
				 email: "Please enter valid email"
				}	                			   
            } 				
        });		
		
		$("#edit_email_form" ).submit(function(event) 
		{
			if ($('#edit_email_form').valid()) 
			{    
					event.preventDefault();    
					$.ajax({	
					type: "POST",
					url: URL+"/MyDetails/value_check.php",			
					data: {edit_more_email:+$("#edit_more_email").val(),more_emailid_hidden:+$("#more_emailid_hidden").val()},
					beforeSend: function(){ 
					},
					success: function(data)
					{  	 
						 if(data=="true")
					     {
							location.href = URL+"/MyDetails/default.php?edit_more_email="+$("#edit_more_email").val() +'&'+ "edit_more_emailid="+$("#edit_more_emailid").val()+'&'+ "more_emailid_hidden="+$("#more_emailid_hidden").val();  
						 }
						 else
						 {
							 alert("Email is already exist");
							 $("#edit_more_email").val('');
						 }
					}
					});        
					 
			}  
		});	
		
		
       
        $("#add_mob_form").validate({ 
			
			errorElement:'div',
			rules:{ 
			   add_more_mob:{
				required: true,
				number: true,
				maxlength:10,
				minlength:10  
                //remote: 
				}		
			},
			messages:{	
     			add_more_mob:{
				 required:"Please Enter Mobile No",
				 maxlength: "You can enter maximum 10 digits",
				 minlength:  "Please enter minimum 10 digits"
                 //remote: "This no. is already exist!"				 
				}	                			   
            } 				
        }); 
		
		$("#profile_resend_otp").click(function(e)
		{
			$("#profile_resend_otp").hide();
			$.ajax({	
			type: "POST",
			url: URL+"/all-ajax/MyDetails.php", 
			data: {check_resend_otp:1},
			beforeSend: function(){ 
			},
			success: function(data)
			{   
			    alert("OTP sent!"); 
				var timeLeft = 30;
				var elem = document.getElementById('profile_some_div');
				var timerId = setInterval(countdown1, 1000);
				function countdown1() {
				  $("#profile_some_div").show();
				  if (timeLeft == 0) {
				   clearTimeout(timerId);
				   $("#profile_resend_otp").show();
				   $("#profile_some_div").hide();
				  }
				  else {				
					elem.innerHTML = timeLeft + ' seconds remaining';
					timeLeft--;	                				
				  }			  
				}		
			}
		   }); 	
		});
		
		$("#profile_resend_otp").hide();
		$("#add_mob_form" ).submit(function(event) 
		{
			if ($('#add_mob_form').valid()) 
			{    
					event.preventDefault();    
					$.ajax({	
					type: "POST",
					url: URL+"/MyDetails/value_check.php",			
					data: {add_more_mob:+$("#add_more_mob").val()},
					beforeSend: function(){ 
					},
					success: function(data)
					{  	 
						 if(data=="true")
						 {
							$("#Add_More_Mobile_PopUp").modal('hide'); 
							$.ajax({	
							type: "POST",
							url: URL+"/all-ajax/MyDetails.php", 
							data: {verified_mobile_for_otp:1,ph:$("#add_more_mob").val()},
							beforeSend: function(){ 
							},
							success: function(data)
							{  	 
							    $("#entered_opt_for_mobile").attr("placeholder", "Enter OTP which is sent to "+$("#add_more_mob").val());
								
								$("#verify-mobile-model").modal({
								show: 'true',
								backdrop: 'static',
								keyboard:true
								}); 
								
								var timeLeft = 30;
								var elem = document.getElementById('profile_some_div');
								var timerId = setInterval(countdown2, 1000);
								function countdown2() {
								  $("#profile_some_div").show();
								  if (timeLeft == 0) {
								   clearTimeout(timerId);
								   $("#profile_resend_otp").show();
								   $("#profile_some_div").hide();
								  }
								  else {				
									elem.innerHTML = timeLeft + ' seconds remaining';
									timeLeft--;	                				
								  }			  
								}								
							}
							});         
							 
						 }else
						 {
							 alert("No is already exist");
							 $("#add_more_mob").val('');
						 }
					}
					});        
					 
			}  
		});	 

        $("#edit_mob_form").validate({ 
			
			errorElement:'div',
			rules:{ 
			   edit_more_mob:{
				required: true,
				number: true,
				maxlength:10,
				minlength:10  
                //remote: 
				}		
			},
			messages:{	
     			edit_more_mob:{
				 required:"Please Enter Mobile No",
				 maxlength: "You can enter maximum 10 digits",
				 minlength:  "Please enter minimum 10 digits"
                 //remote: "This no. is already exist!"				 
				}	                			   
            } 				
        });		
		
		$("#profile_mobile_otp").hide();
		$("#edit_mob_form" ).submit(function(event) 
		{
			if ($('#edit_mob_form').valid()) 
			{    
					event.preventDefault();    
					$.ajax({	
					type: "POST",
					url: URL+"/MyDetails/value_check.php",			
					data: {edit_more_mob:+$("#edit_more_mob").val(),edit_more_mob_id:+$("#edit_more_mob_id").val(),more_mobid_hidden:+$("#more_mobid_hidden").val()},
					beforeSend: function(){ 
					},
					success: function(data)
					{  	
                        if(data=="true")
						{
							$("#Edit_More_Mobile_PopUp").modal('hide'); 
							$.ajax({	
							type: "POST",
							url: URL+"/all-ajax/MyDetails.php", 
							data: {verified_mobile_for_OTP:1,ph:$("#edit_more_mob").val(),id:$("#more_mobid_hidden").val(),val:$("#edit_more_mob_id").val()},
							beforeSend: function(){ 
							},
							success: function(data)
							{  	 
								$("#verify-mobile-models").modal({
								show: 'true',
								backdrop: 'static',
								keyboard:true
								}); 
								
								var timeLeft = 30;
								var elem = document.getElementById('profile_mobile_div');
								var timerId = setInterval(countdown2, 1000);
								function countdown2() {
								  $("#profile_mobile_div").show();
								  if (timeLeft == 0) {
								   clearTimeout(timerId);
								   $("#profile_mobile_otp").show();
								   $("#profile_mobile_div").hide();
								  }
								  else {				
									elem.innerHTML = timeLeft + ' seconds remaining';
									timeLeft--;	                				
								  }			  
								}
								
							}
							});     							 
						}
						else
						{
							alert("No is already exist");
							$("#edit_more_mob").val('');
						}
					}
					});        
					 
			}  
		});
		
		
		$("#submit_mob_otp").click(function()
		{   
			$.ajax({	
				type: "POST",
				url: URL+"/all-ajax/MyDetails.php", 
				data: {varify_mobile:1,entered_opt_for_mobile:$("#entered_opt_for_mobile").val()},
				beforeSend: function(){ 
				},
				success: function(data)
				{   
					if(data==1)
					{
						alert("Mobile no successfully verified!");
						location.reload();
					}
					else
						alert("Wrong OTP!!! Please enter correct OTP");
				}
			  }); 		
		});
		
		$("#submit_mob_OTP").click(function()
		{   
			$.ajax({	
				type: "POST",
				url: URL+"/all-ajax/MyDetails.php", 
				data: {varify_mobiles:1,entered_OTP_for_mobile:$("#entered_OTP_for_mobile").val()},
				beforeSend: function(){ 
				},
				success: function(data)
				{   
					if(data==1)
					{
						alert("Mobile no successfully verified!");
						location.reload();
					}
					else
						alert("Wrong OTP!!! Please enter correct OTP");
				}
			  }); 		
		});	
		
	
		$("#changepwd_frm").submit(function( event )  // on click on this id...we will do folowwings
		{ 		   
			if($('#changepwd_frm').valid())  
			{ 		        
				clear_profile_error_msg();
				var formData = $('#changepwd_frm').serialize();  // i have taken all form in serialized format here...
				$.ajax({	
				type: "POST",
				url: URL+"/all-ajax/MyDetails.php", 
				data: {user_changepwd_formData:1,formData:formData},	
						beforeSend: function(){
							//$("#search-box").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
						},
					  success: function(val)
					  {  		
                        if(val==0)
                        {
                            $("#password_message").hide();	
                            $("#oldpassword").val('');
                            $("#newpassword").val('');
                            $("#confirmpassword").val('');
							$("#change_pwd_success").html("Password Updated Successfully!!!");
                        }
						else if(val==1)
						{
						    $("#password_message").show();	
							$("#change_pwd_error").html("Password pattern not match"); 
						}
						else if(val==2)
						{
						    $("#password_message").hide();	
							$("#change_pwd_error").html("Password not match"); 
						}
						else if(val==3)
						{
						    $("#password_message").hide();	
							$("#change_pwd_error").html("Incorrect Old Password");	
						}
					 }  
				});     
			}
			event.preventDefault();  // this is help to prevent from submit
		});
		
		$("#profilefrm").submit(function( event )  // on click on this id...we will do folowwings
		{ 		   
			if($('#profilefrm').valid())  
			{
				var formData = $('#profilefrm').serialize();  // i have taken all form in serialized format here...
				$.ajax({	
				type: "POST",
				url: URL+"/all-ajax/MyDetails.php", 
				data: {update_profile_formData:1,formData:formData},	
						beforeSend: function(){
							//$("#search-box").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
						},
					  success: function(val)
					  {  		
					    if(val==1)
						{
							alert("Profile updated successfully");
							//$("#update_profile_msg").html("Profile updated successfully");
							location.reload();
						}
						else if(val==0)
						{
							$("#update_profile_error").html("Profile not updated");
						}
					 }  
				});     
			}
			event.preventDefault();  // this is help to prevent from submit
		});
	
}); 	     



function del_addition_data(val)
{ 
	var r = confirm("Do you really want to delete phone no !");    
	if (r == true) 
	{    
			$.ajax({
			type: 'POST',
			url: URL+"/all-ajax/MyDetails.php",
			//async: false,			 
			data: {del_addition_details:1,val:val},	  
			success: function(ajaxresult){	     
					location.reload();	 	
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) { 
			alert('Error');
			}
			});  
	}	 
}  

function del_addition_email(val)
{ 
	var r = confirm("Do you really want to delete email !");    
	if (r == true) 
	{    
			$.ajax({
			type: 'POST',
			url: URL+"/all-ajax/MyDetails.php",
			//async: false,			 
			data: {del_addition_details:2,val:val},	  
			success: function(ajaxresult){	     
					location.reload();	 	
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) { 
			alert('Error');
			}
			});  
	}	 
}

function verify_mainemail(id,val,val1)
{ 
	  
	$.ajax({
	type: 'POST',
	url: URL+"/all-ajax/MyDetails.php",
	//async: false,			 
	data: {verify_mainemail_details:1,id:id,val:val,val1:val1},	  
	success: function(data){	     
	    //location.reload();
		alert("Please check the email box!!!");
	 }	
  });  		 
}  

function verify_email(id,val,val1)
{ 
	  
	$.ajax({
	type: 'POST',
	url: URL+"/all-ajax/MyDetails.php",
	//async: false,			 
	data: {verify_email_details:2,id:id,val:val,val1:val1},	  
	success: function(data){	     
	    //location.reload();
		alert("Please check the email box!!!");
	 }	
	});  		 
} 

function get_mainno(val,id)
{
	$("#edit_more_mob").val(val);	
	$("#more_mobid_hidden").val(id);
	$("#Edit_More_Mobile_PopUp").modal({
	show: 'true',
	backdrop: 'static',
	keyboard:true
	}); 
}

function get_phno1(val,val1,id)
{
	$("#edit_more_mob").val(val);
    $("#edit_more_mob_id").val(val1);
	$("#more_mobid_hidden").val(id);	
	$("#Edit_More_Mobile_PopUp").modal({
	show: 'true',
	backdrop: 'static',
	keyboard:true
	}); 
}

function get_email(val,id,val1)
{
	var val=$("#edit_more_emailid").val(val);
	var id=$("#edit_more_email").val(id); 	
	var val1=$("#more_emailid_hidden").val(val1);
	console.log(val);
	console.log(id);
	console.log(val1);
	$("#Edit_More_Email_Popup").modal({
	show: 'true',
	backdrop: 'static',
	keyboard:true
	}); 
}

function get_emailid(val,id,val1)
{
	var val=$("#edit_more_emailid").val(val);
	var id=$("#edit_more_email").val(id); 	
	var val1=$("#more_emailid_hidden").val(val1);	
	console.log(val);
	console.log(id);
	console.log(val1);
	$("#Edit_More_Email_Popup").modal({
	show: 'true',
	backdrop: 'static',
	keyboard:true
	}); 
}


function  select_city(name)
{
	new_name =   name.replace(new RegExp("%20", "g"), ' ');     
	new_name =   new_name.replace(new RegExp("%2F", "g"), ' ');     
	$("#profile_city_search").val(new_name);
    $("#profile_city_search").next().slideUp();	
    $("#city-ajax").hide(400);	
} 

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