// check password
$(document).ready(function(){
	$(".check-otp-form").on("submit",function(e){
		e.preventDefault();
		
		$.ajax({
			type : "POST",
			url : "php/check_otp.php",
			data : {
				otp : $("#otp").val()
			},
			beforeSend : function()
			{
				$(".check-otp-btn").html("Checking...");
				$(".check-otp-btn").attr("disabled","disabled");
			},
			success : function(response)
			{
				$(".check-otp-btn").html("Check");
				$(".check-otp-btn").removeAttr("disabled");


				var result = JSON.parse(response);
            	if(result.status.trim() == "ok")
            	{
            		$(".check-otp-btn").attr("disabled","disabled");
            		$("#otp").attr("disabled","disabled");
            		$("#resend-otp").attr("disabled","disabled");
            		$("#new_pwd").removeAttr("disabled");
            		$("#confirm_pwd").removeAttr("disabled");

            	}
            	else if(result.status.trim() == "error")
            	{
            		document.getElementById("alert-otp").className = "";
					$("#alert-otp").addClass("alert alert-danger mt-2");
					$("#alert-otp").html("Wrong OTP");

					setTimeout(function(){
						document.getElementById("alert-otp").className = "";
						$("#alert-otp").html("");
					},5000);

            	}

			}
		});
	});
});

// change password
$(document).ready(function(){
	$(".pwd-change-form").on("submit",function(e){
		e.preventDefault();
		$.ajax({
			type : "POST",
			url : "php/change_pwd.php",
			data : {
				new_pwd : $("#new_pwd").val(),
				confirm_pwd : $("#confirm_pwd").val(),
				otp : $("#otp").val()
			},
			beforeSend : function()
			{

			},
			success : function(response)
			{
				document.write(response);
			}
		});
		
	});

	$("#new_pwd").on("input",function(){
			document.getElementById("new_pwd_icon").className = "";
			$("#new_pwd_icon").addClass("fa fa-spinner fa-spin");
			$("#new_pwd_icon").css("color","black");
		});
	$("#confirm_pwd").on("input",function(){
			document.getElementById("confirm_pwd_icon").className = "";
			$("#confirm_pwd_icon").addClass("fa fa-spinner fa-spin");
			$("#confirm_pwd_icon").css("color","black");
	});

		$("#new_pwd").on("blur",function(){
			var new_pwd = $("#new_pwd").val();
			var new_pwd_filed = $("#new_pwd");
			var confirm_pwd = $("#confirm_pwd").val();
			var capital = /[A-Z]/g;
			var small = /[a-z]/g;
			var num = /[0-9]/g;
			var spacial = "[{!@#$%^&*}]";
			if(new_pwd.match(capital) && new_pwd.match(small) && new_pwd.match(num) && new_pwd.match(spacial) && new_pwd.length >= 8 && new_pwd.length <= 16)
			{
				document.getElementById("new_pwd_icon").className = "";
				$("#new_pwd_icon").addClass("fa fa-check");
				$("#new_pwd_icon").css("color","green");

				document.getElementById("alert").className = "";
				$("#alert").html("");
				$("#confirm_pwd").removeAttr("disabled");
			}
			else{
				document.getElementById("new_pwd_icon").className = "";
				$("#new_pwd_icon").addClass("fa fa-times");
				$("#new_pwd_icon").css("color","red");

				document.getElementById("alert").className = "";
				$("#alert").addClass("alert alert-danger");
				$("#alert").html("These Charactors Must Be use A-Z, a-z, 0-9, Spacial Charactors and Password must be between 8-16 digits.");
				$("#confirm_pwd").attr("disabled","disabled");
				$(".change-pwd-btn").attr("disabled","disabled");
			}
		});



		$("#confirm_pwd").on("blur",function(){
			var new_pwd = $("#new_pwd").val();
			var new_pwd_filed = $("#new_pwd");
			var confirm_pwd = $("#confirm_pwd").val();
			var capital = /[A-Z]/g;
			var small = /[a-z]/g;
			var num = /[0-9]/g;
			var spacial = "[{!@#$%^&*}]";

			if(new_pwd != "" && new_pwd == confirm_pwd)
			{
				$(".change-pwd-btn").removeAttr("disabled");
				document.getElementById("confirm_pwd_icon").className = "";
				$("#confirm_pwd_icon").addClass("fa fa-check");
				$("#confirm_pwd_icon").css("color","green");

				document.getElementById("alert").className = "";
				$("#alert").removeClass("alert alert-danger");
				$("#alert").html("");

			}
			else{
				document.getElementById("confirm_pwd_icon").className = "";
				$("#confirm_pwd_icon").addClass("fa fa-times");
				$("#confirm_pwd_icon").css("color","red");

				document.getElementById("alert").className = "";
				$("#alert").addClass("alert alert-danger");
				$("#alert").html("Password Not Matched.");
				$(".change-pwd-btn").attr("disabled","disabled");
				
			}
		});
});

// resend pwd
$(document).ready(function(){
	$("#resend-otp").click(function(e){
		e.preventDefault();
 
	        $.ajax({
	          type : "POST",
	          url : "php/forgot.php",
	          beforeSend : function()
	          {
	            $("#resend-otp").html("Please Wait...");
	            $("#resend-otp").attr("disabled","disabled");
	          },
	          success : function(response)
	          {
	          	$("#resend-otp").html("Resend OTP");
	            $("#resend-otp").removeAttr("disabled");
	            var result = JSON.parse(response);
	            if(result.status.trim() == "ok")
	            {
	              document.getElementById("alert-otp").className = "";
					$("#alert-otp").addClass("alert alert-success mt-2");
					$("#alert-otp").html("OTP sent successfully. Please check your Email");

					setTimeout(function(){
						document.getElementById("alert-otp").className = "";
						$("#alert-otp").html("");
					},5000);
	            }
	            else{
	              document.getElementById("alert-otp").className = "";
					$("#alert-otp").addClass("alert alert-danger mt-2");
					$("#alert-otp").html("OOPS!!! something went wrong, Please try again later.");

					setTimeout(function(){
						document.getElementById("alert-otp").className = "";
						$("#alert-otp").html("");
					},5000);
	            }
	            
	          }
	        });

	});
});