var OTP='';

var timeLeft = 30;
	

$(document).ready(function(){   
	
	$("#socialform").show();		
    $("#login_here").show();
	$("#forgot_password_heading").hide();
	$("#reg_some_div").hide();
	$("#message").hide();
	$("#show_pwd").hide();  
	$("#some_div").hide();  
	$("#password").hide(); 
	$("#submit_login").hide();
    $("#forgot_pass").hide();
    //$("#submit_pwd").hide();
    $("#submit_reset").hide();	
	$("#submit_login_otp").hide(); 
	$("#entered_opt").hide(); 
	$("#submit_otp").hide();
    $("#entered_OTP").hide(); 
	$("#submit_OTP").hide();
    $("#resend_otp").hide();
	$("#login_terms").hide();
	$("#login_terms_condn").hide();
	$("#resend_reg_otp").hide();

    $("#resend_otp").click(function(e)
	{
	   $("#resend_otp").hide();	   
	   
	   $.ajax({	
		type: "POST",
		url: URL+"/all-ajax/general-ajax.php", 
		data: {check_resend_otp:1},
		beforeSend: function(){ 
		},
		success: function(data)
		{   
		    var timeLeft = 30;
            var elem = document.getElementById('some_div');
			var timerId = setInterval(countdown1, 1000);
		    function countdown1() {
			  $("#some_div").show();
			  if (timeLeft == 0) {
			   clearTimeout(timerId);
			   $("#resend_otp").show();
			   $("#some_div").hide();
			  }
			  else {				
				elem.innerHTML = timeLeft + ' seconds remaining';
				timeLeft--;	                				
			  }			  
			}		
		}
	  }); 	
    });	
	
	$("#resend_reg_otp").click(function(e)
	{
	   $("#resend_reg_otp").hide();	
	   
	   $.ajax({	
		type: "POST",
		url: URL+"/all-ajax/general-ajax.php", 
		data: {check_reg_otp:1},
		beforeSend: function(){ 
		},
		success: function(data)
		{   
		    var timeLeft = 30;
            var elem = document.getElementById('reg_some_div');
			var timerId = setInterval(countdown1, 1000);
		    function countdown1() {
			  $("#reg_some_div").show();
			  if (timeLeft == 0) {
			   clearTimeout(timerId);
			   $("#resend_reg_otp").show();
			   $("#reg_some_div").hide();
			  }
			  else {				
				elem.innerHTML = timeLeft + ' seconds remaining';
				timeLeft--;	                				
			  }			  
			}			
		}
	  }); 	
    });
     
	$("#submit_login_otp").click(function(e) 
	{
		clear_msg();
		
       var isChecked = $("#login_terms").is(":checked");
       if (isChecked)
  	  { 
		$("#login_id").hide(); 
		$("#show_pwd").hide();
		$("#password").hide(); 
		$("#submit_login").hide();
        $("#forgot_pass").hide();
        $("#submit_pwd").hide();
        $("#submit_reset").hide();			
		$("#submit_login_otp").hide(); 
		$("#entered_opt").show();     
		$("#submit_otp").show(); 
        //$("#resend_otp").show();
		$("#some_div").show();
		$("#resend_otp").hide();
        $("#login_terms").hide();
	    $("#login_terms_condn").hide();		
		
		var mobileno=$("#login_id").val();
		$("#entered_opt").attr("placeholder", "Enter OTP which is sent to "+mobileno);
		
		$.ajax({	
		type: "POST",
		url: URL+"/all-ajax/general-ajax.php", 
		data: {check_login_otp:1,login_id:$("#login_id").val()},
		beforeSend: function(){ 
		},
		success: function(data)
		{  	
		    var timeLeft = 30;
            var elem = document.getElementById('some_div');
			var timerId = setInterval(countdown, 1000);
		    function countdown() {
			  if (timeLeft == 0) {
			   clearTimeout(timerId);
			   $("#resend_otp").show();
			   $("#some_div").hide();
			  }
			  else {
				elem.innerHTML = timeLeft + ' seconds remaining';
				timeLeft--;
			  }
			}
		 }
		}); 
      }
      else $("#login_msg").html("Please select terms and condition");//alert("Please select terms and condition"); 	  
	});
	
	$("#submit_otp").click(function(e) 
	{
		var otpReg = /^\d{4}$/; 
		if(!otpReg.test($("#entered_opt").val()))  
			alert("Enter 4 digit number!!!"); 
		else 
		{ 
			$.ajax({	
			type: "POST",
			url: URL+"/login_logout.php", 
			data: {checked_otp:1,entered_opt:$("#entered_opt").val()},
			beforeSend: function(){ 
			},
			success: function(data)
			{ 
			   if(data==1)
			   {
				   //////alert("Login Done Successfully!!");
				   $('#login').modal('hide');
				   window.location.href = URL;
				   //location.reload();
			   }
			  else if(data==0)
			  {
				alert("Wrong OTP!!! Please enter correct OTP"); 				
			  }             		  
			}			
		  });  
		}
	});
	
	$("#submit_OTP").click(function(e) 
	{
		var otpReg = /^\d{4}$/; 
		if(!otpReg.test($("#entered_OTP").val()))  
			alert("Enter 4 digit number!!!"); 
		else 
		{ 
			$.ajax({	
			type: "POST",
			url: URL+"/all-ajax/default.php", 
			data: {submit_OTP:1,entered_OTP:$("#entered_OTP").val()},
			beforeSend: function(){ 
			},
			success: function(data)
			{               			
			   if(data==1)
			   {
				   $("#registration").modal('hide');
				   $('#login').modal('show');
			   }
			  else
			  {
				alert("Wrong OTP!!! Please enter correct OTP"); 				
			  }
             		  
			}			
		  });  
		}
	}); 
	

    $("#submit_pwd").click(function(e) 
	{
		clear_msg();
		var email_ph=$("#login_id").val();
		if(email_ph)
			$("#forgot_pass").val(email_ph);
		$("#login_id").hide(); 
		$("#show_pwd").hide();
		$("#password").hide();   
		$("#submit_login").hide();        		  
		$("#submit_pwd").hide(); 
        $("#login_terms").hide();        		  
		$("#login_terms_condn").hide();
        $("#submit_login_otp").hide();
		$("#social_heading").hide();
		$("#socialform").hide();		
        $("#login_here").hide(); 		
		$("#forgot_pass").show();
        $("#submit_reset").show();
        $("#forgot_password_heading").show();		
	});	
	
	$("#submit_reset").click(function(e) 
	{ 
	   if($("#forgot_pass").val()!='')
	   {
		var phoneno = /^\d{10}$/;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; 

		   if( (($("#forgot_pass").val().match(phoneno)) || ($("#forgot_pass").val().match(emailReg))))
		   {
				$.ajax({	
				type: "POST",
				url: URL+"/all-ajax/general-ajax.php", 
				data: {submit_reset:1,forgot_pass:$("#forgot_pass").val()},
				beforeSend: function(){
				 
				},
				success: function(data)
				{   
				   if(data==1)
				   {
				   //alert("Your Password has been reset Successfully!!! Kindly check your registered email !!");
				   alert("kindly check your mail to reset password");
				   $('#login').modal('hide');
				   $("#forgot_pass").val('');
				   //$('#myModal').modal('toggle');			  
					$("#forgot_pass").hide();
					$("#submit_reset").hide(); 
					$("#login_id").show(); 
					$("#password").show();
					$("#show_pwd").show();				
					$("#submit_login").show();        		  
					$("#submit_pwd").show();   				
				   }
				  else
				  {
					alert("Mobile No or Email id does not exits !!"); 
					$("#forgot_pass").val(''); 
				  }
				}		
			  });
		   }
		   else	 
		   {			   
	           $("#forgot_pass").val(''); 
			   alert("Enter valid email id or mobile no");
		   }
	    }
		else		   
		  alert("Enter Mobile No or Email Address");
	   
	});	
	
	$("#master_logout").click(function(e) 
	{
		$.ajax({	
		type: "POST",
		url: URL+"/all-ajax/general-ajax.php", 
		data: {master_logout:1},
		beforeSend: function(){
		//location.reload(); 
		//window.location.href = window.location.href;
		},
		success: function(data)
		{   
			window.location.href = window.location.href;  
		}
		}); 
	});
	 
	
	
	$("#login_id").keyup(function (e) 
	{    
	    $(".error").removeClass("error");
		set = 0;
		phone = 0;
		details = $(this).val();
		var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);  
		if(pattern.test(details)==true)
			set = 1;
		else  if (/^[0-9]{10}$/.test(+details)) // OR if (/^[0-9]{1,10}$/.test(+tempVal) && tempVal.length<=10) 
		{ 
			set = 1; 
			phone = 1;
		}
		 
		if(set==1)
		{  	
	        $("#submit_pwd").show();
			
			$.ajax({	
			type: "POST",
			url: URL+"/all-ajax/general-ajax.php", 
			data: {check_user_existance:1,login_id:details},
			beforeSend: function(){
			//location.reload(); 
			//window.location.href = window.location.href;
			},
			success: function(data)
			{
				if(data==0 && phone==1) 
				{    
			        $("#show_pwd").hide();
					$("#password").hide(); 
					$("#submit_login").hide(); 
					$("#forgot_pass").hide();
					//$("#submit_pwd").hide();
					$("#submit_reset").hide();
					$("#submit_login_otp").show();
                    $("#login_terms").show(); 
                    $("#login_terms_condn").show(); 					
					clear_msg();
				}
				else if(data==0 && phone==0) 
				{
					$("label.error").hide();
                    $(".error").removeClass("error");
                    
					$("#login_terms").hide(); 
                    $("#login_terms_condn").hide(); 	
                    
					$("#show_pwd").hide();
					$("#password").hide(); 
					$("#submit_login").hide(); 
					$("#forgot_pass").hide();
					//$("#submit_pwd").hide();
					$("#submit_reset").hide();
					$("#submit_login_otp").hide(); 
					$("#login_msg").html("Email id does not exits");
				}else if(data==1 && phone==1) 
				{    
			        $("#show_pwd").show();
				    $("#password").show(); 
				    $("#submit_login").show(); 
					$("#submit_login_otp").show(); 
					$("#login_terms").show(); 
                    $("#login_terms_condn").show(); 					
				    clear_msg();
				}
				else if(data==2 && phone==1) 
				{    
				    $("#login_msg").html("Some error occured. please try again");
				}
				else if(data==1 && phone==0) 
				{    
			        $("#show_pwd").show();
				    $("#password").show(); 
				    $("#submit_login").show();
                    $("#login_terms").show(); 
                    $("#login_terms_condn").show(); 					
				    clear_msg();
				}
				else{
					clear_msg();
				}			
			}
		  });  
		}else
		{
			clear_msg();
			$("#show_pwd").hide();
			$("#password").hide(); 
			$("#submit_login").hide(); 
			$("#forgot_pass").hide();
			//$("#submit_pwd").hide();
			$("#submit_reset").hide();
			$("#submit_login_otp").hide();
            $("#login_terms").hide(); 
            $("#login_terms_condn").hide(); 				
		}		
	});   
}); 


function clear_msg()
{
    $("label.error").hide();
    $(".error").removeClass("error");
    $("#login_msg").html("");
}

$(document).ready(function() 
{
	$("#loginform").validate({ 
			
			errorElement:'div',
			rules:{ 
		       login_id:{
					required: true,
				    remote: URL+"/all-ajax/value_check_login.php"
				}, 
			password: {
                 required:true 				 				
                },		
			
			forgot_pass: {
                 required:true 				 				
                },				
					
			entered_opt: {
                 required:true 				 				
                },
				
			login_terms: {
                 required:true 				 				
                }	
			},
       
	   messages:{				 
				
			  login_id:{
					//required:"Please enter an Email Id or Mobile No",	
					remote:"Enter valid email id or mobile no"											
				},
				
			  password:{
					required:"Enter your password"								 
				},
				
			  forgot_pass:{
					required:"Enter your password"								 	 
			  },
			  
			  entered_opt:{
					required:"Please enter an OTP"								 
			  },

              login_terms:{					
                  required:"Please select terms and condition"				  		
				}			  
			}
		});
		
	 $("#registrationform").validate({ 
			
			errorElement:'div',
			rules:{ 
		     registered_name:{
				 required:true,
				 minlength:3,
				 maxlength:35
				},	

			registered_email:{
				required: true,
				email: true,
				remote: URL+"/all-ajax/value_check.php"
				},

		     registered_mob:{
                    required:true,
				    number:true,
				    minlength:10,
				    maxlength:10,					
                    remote: URL+"/all-ajax/value_check.php"					 
                },
				
			registered_password: {
                 required:true 
				 //minlength:5,
				 //maxlength:25 					
                },
				
			reg_terms:{
				 required:true				
				},

            entered_OTP:{
                    required:true,
				    number:true				    			 
               }				
			},
			messages:{	

				registered_name:{					
                  required:"Please enter name",
				  minlength : "Please enter minimum 3 characters name",
				  maxlength : "Max 35 characters are allowed"  					
				},   
				
				registered_email:{
				 required:"Please enter email id",	
				 remote: "This email is already registered"							
				}, 
				
				registered_mob:{
				required:"Please enter mobile no",	
				minlength : "Please enter minimum 10 digits",
				maxlength : "Max 10 digits are allowed",
				remote: "This number is already registered"				
			   },
			   
			   registered_password:{
				required:"Please enter Password"					
				//minlength : "Please enter minimum 5 characters password",
				//maxlength : "Max 25 characters are allowed"  				
			   },
			   
			   reg_terms:{					
                  required:"Please select terms and condition"				  		
				},

               entered_OTP:{
				 required:"Please enter an OTP"					
			   }			   
            }			  
		});	
		
		
		$( "#registrationform" ).submit(function( event )  // on click on this id...we will do folowwings
		{ 		   
			if($('#registrationform').valid())  
			{ 
				var formData = $('#registrationform').serialize();  // i have taken all form in serialized format here...
				$.ajax({	
				type: "POST",
				url: URL+"/all-ajax/general-ajax.php", 
				data: {check_reg:1,formData:formData},	
						beforeSend: function(){
							//$("#search-box").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
						},
					   success: function(val)
					  {  				
						//var obj = jQuery.parseJSON(val); 
						//console.log(obj.formData.name);	
						if(val==1)
						{
							clear_msg();
							$("#message").hide();
							$("#registered_name").hide(); 
							$("#registered_mob").hide(); 
							$("#registered_email").hide();
							$("#registered_password").hide();
							$("#show_regipwd").hide();
							$("#reg_terms").hide();
							$("#log").hide();
							$("#iam").hide();
							$("#regi").hide();
							$("#submit_reg").hide();
							$("#entered_OTP").show();     
							$("#submit_OTP").show();
							
							var timeLeft = 30;
							var elem = document.getElementById('reg_some_div');
							var timerId = setInterval(countdown2, 1000);
							function countdown2() {
							  $("#reg_some_div").show();
							  if (timeLeft == 0) {
							   clearTimeout(timerId);
							   $("#resend_reg_otp").show();
							   $("#reg_some_div").hide();
							  }
							  else {				
								elem.innerHTML = timeLeft + ' seconds remaining';
								timeLeft--;	                				
							  }			  
							}						
						}
						else //if(val==0)
						{
							/*$("#message").show();
							event.preventDefault();*/
							if(val==2)
								alert("Mobile No and Email id already registered!!!");
							else if(val==3)
								alert("Mobile No already registered!!!");
							else if(val==4)
								alert("Email id already registered!!!");
							else if(val==0)
								$("#message").show();                        
								
							event.preventDefault(); 							
						}				
					 }  
				});     
			}
			event.preventDefault();  // this is help to prevent from submit
		});	
		
		
		$( "#loginform" ).submit(function( event )  // on click on this id...we will do folowwings
		{
		   if ($('#loginform').valid())  
		  {
		    var formData = $('#loginform').serialize();  // i have taken all form in serialized format here...
			$.ajax({	
			type: "POST",
			//url: URL+"/all-ajax/general-ajax.php", 
			url: URL+"/login_logout.php", 
			data: {check_user_login:1,formData:formData},	
					beforeSend: function(){
					},
					success: function(data){
                    if(data==1)
                    {
                        ////////////alert("Login Done Successfully!!");
                        window.location.href = URL;
                    }
                    else if(data==2)
                    {
                        $("#login_msg").html("Incorrect Password!!!");
                    }
                    else if(data==3)
                    {
                        $("#login_msg").html("Invalid Credientials!!!");
                    }
			    }  
			});     
			}
			event.preventDefault();  // this is help to prevent from submit
		});	  
		
		
	$("#show_pwd").click(function() {
		if ($("#password").attr("type") == "password") {
		  $("#password").attr("type", "text");

		} else {
		  $("#password").attr("type", "password");
		}
    });	
		
	$("#show_regipwd").click(function() {
		if ($("#registered_password").attr("type") == "password") {
		  $("#registered_password").attr("type", "text");

		} else {
		  $("#registered_password").attr("type", "password");
		}
    });	
     		
});



