var jQueryDM = jQuery;
jQueryDM('window').ready(function(){
  
    parent_select     =   '.signin_forgot_password_div';
    parent_select_obj  = jQueryDM(parent_select);
   //var parent_id_select  = jQueryDM(parent_select).attr('id');
   //var parent_id_select_obj  = jQueryDM('#'+parent_id_select);
       
   
   dap_signin_forgot_password();
   dap_reset_password();
   var submit_url = "/dap/inc/reset_password.php";
   dap_enter_submit_login_form();
   dap_enter_submit_forgot_form();
   dap_enter_submit_reset_form();
});

function dap_enter_submit_login_form(){
	
	
	jQueryDM('form#dap_login input.dap_login_email,form#dap_login input.dap_login_pass').keypress(function (e) {
		if (e.which == 13) {
			var parent_id = jQueryDM(this).parents('.signin_forgot_password_div').attr('id');
			var parent_obj = jQueryDM('#'+parent_id);
			parent_obj.find('form#dap_login .signin_btn').trigger('click');
			return false; 
		}
	});
}

function dap_enter_submit_forgot_form(){
		jQuery(document).on('submit',"form#dap_forgot",function(){
			event.preventDefault();
			var parent_id = jQueryDM(this).parents('.signin_forgot_password_div').attr('id');
			var parent_obj = jQueryDM('#'+parent_id);
			parent_obj.find('form#dap_forgot .forgot_btn').trigger('click');
			return false; 
		});
				
		jQuery('form#dap_forgot input.dap_forgot_email').keypress(function (e) {
			if (e.which == 13) {
				return false; 
			}
		});
}


function dap_enter_submit_reset_form(){
	
		jQuery(document).on('submit',"form#dap_reset",function(){
			event.preventDefault();
			var parent_id = jQueryDM(this).parents('.signin_forgot_password_div').attr('id');
			var parent_obj = jQueryDM('#'+parent_id);
			parent_obj.find('form#dap_reset .forgot_btn').trigger('click');
			return false; 
		});
		
		jQuery('form#dap_reset input.dap_reset_pass,form#dap_reset input.dap_reset_re_pass').keypress(function (e) {
			if (e.which == 13) {
				var parent_id = jQueryDM(this).parents('.signin_forgot_password_div').attr('id');
				var parent_obj = jQueryDM('#'+parent_id);
				parent_obj.find('form#dap_reset .reset_pass_btn').trigger('click');
				return false; 
			}
		});
}

function dap_signin_forgot_password(){
	
	if(jQueryDM('#dap_member_login_frontend_shortcode .user_login').hasClass('signin_forgot_password_div')){
	       parent_select_obj.find('.forgot_password').on('click',function(e){
		   e.preventDefault();
		   window.dap_global_login_parent_id = jQueryDM(this).parents('.signin_forgot_password_div').attr('id');
		   window.dap_global_login_event_trigger = "forgot_show";
		  
		  
		   dap_member_set_error_message('');
		   dap_member_login_forgot_pass();
		   
	   });
	   
	   parent_select_obj.find('.signin_btn').on('click',function(e){
		   e.preventDefault();
		   window.dap_global_login_event_trigger = "signup";
		   window.dap_global_login_parent_id = jQueryDM(this).parents('.signin_forgot_password_div').attr('id');
		   var dap_global_login_parent_obj =  jQueryDM('#'+window.dap_global_login_parent_id);
		  // console.log("dap_login_btn_html start ");
		   //console.log(jQueryDM(this).html());
		   //console.log("dap_login_btn_html end ");
		   dap_global_login_parent_obj.find('.dap_login_btn_html').html(jQueryDM(this).html());
		   var dap_loader_html = dap_global_login_parent_obj.find('.dap_loader_html').html(); 
		   jQueryDM(this).html(dap_loader_html);
		   dap_member_signin_action(); 
		   
		   
		   
		   
	   });
	   
		jQueryDM(document).on('click',parent_select+' .forgot_btn',function(e){
			e.preventDefault();
			window.dap_global_login_event_trigger = "forgot";
			window.dap_global_login_parent_id = jQueryDM(this).parents('.signin_forgot_password_div').attr('id');
			var dap_global_login_parent_obj =  jQueryDM('#'+window.dap_global_login_parent_id);
			dap_global_login_parent_obj.find('.dap_login_btn_html').html(jQueryDM(this).html());
			var dap_loader_html = dap_global_login_parent_obj.find('.dap_loader_html').html(); 
			jQueryDM(this).html(dap_loader_html);
		   dap_member_forgot_action(); 
		  
		   
	   });
	   
	   
	   jQueryDM(parent_select).find('.dap_login_pass').attr('type','password');
	   error_message_div = jQueryDM(parent_select).find('.error_message_div').clone().wrap('<p>').parent().html();
	   jQueryDM(parent_select).find('.forgot_outer_sub_heading').prepend(error_message_div);
	   
	   
	 
	}
   
}

function dap_reset_password(){
	
   jQueryDM('#dap_member_login_frontend_shortcode .user_login').each(function(index, value){
      
	   if(jQueryDM(this).hasClass('reset_password_div')){
	   
		
		
		   
		jQueryDM(this).find('.dap_reset_pass').attr('type','password');
		jQueryDM(this).find('.dap_reset_re_pass').attr('type','password');
		
		error_message_div = jQueryDM(this).find('.error_message_div').clone().wrap('<p>').parent().html();
		jQueryDM(this).find('form#dap_reset').prepend('<div class="form-group">'+error_message_div+"</div>");
		
		
		var forgot_hrml = jQueryDM(this).find('.dap_reset_template_static_div').html();
		jQueryDM(this).find('.dap_reset_template_static_div').remove();
		jQueryDM(this).find('.login_outer').html(forgot_hrml);
		
		jQueryDM(this).show();
		
		var parent_id = jQueryDM(this).attr('id');
		window.dap_global_login_parent_id = parent_id;
		dap_member_check_reset_password_key_expire();
		
		jQueryDM(document).on('click','#'+parent_id+' .reset_pass_btn',function(e){
			e.preventDefault();
			window.dap_global_login_event_trigger = "reset_pass";
			window.dap_global_login_parent_id = parent_id;
			dap_member_check_reset_password_action();
			 
		   });
		}
	});
   
   
}

function dap_member_check_reset_password_action(){
	
	var dap_global_login_parent_obj = jQueryDM('#'+window.dap_global_login_parent_id);
	//console.log("dap_global_login_parent_id"+window.dap_global_login_parent_id)
	key = dap_global_login_parent_obj.find('input[name="dm_reset_pass_key"]').val();
	var submit_url = "/dap/inc/reset_password.php";
	var current_pageurl = window.location.href;
	var get_pass = dap_global_login_parent_obj.find('.dap_reset_pass').val();
	var re_enter_pass = dap_global_login_parent_obj.find('.dap_reset_re_pass').val();
	if(get_pass ==""){ 
		error_msg = dap_global_login_parent_obj.parents('#dap_member_login_frontend_shortcode').find('input[name="dap_password_empty"]').val()+"<br>";
		dap_member_set_error_message(error_msg);
		return false;
	}else if(get_pass != re_enter_pass){
		error_msg = dap_global_login_parent_obj.parents('#dap_member_login_frontend_shortcode').find('input[name="dap_password_not_match"]').val()+"<br>";
		dap_member_set_error_message(error_msg);
		return false;
	}else if(get_pass == re_enter_pass){
		//dap_member_show_loader();
		jQuery.ajax({
			url: submit_url,
			type: "POST",
			data: {"get_pass":get_pass ,"key":key, "current_pageurl":current_pageurl,"reset": "Y"},
			cache: false,
			success: function (returnval) {
				msg =	dap_global_login_parent_obj.parents('#dap_member_login_frontend_shortcode').find('input[name="dap_reset_password_success"]').val();
				dap_member_set_error_message(msg);
				//jQueryDM('.error_message_div').hide();
				//swal.fire(msg);
				dap_global_login_parent_obj.parents('#dap_member_login_frontend_shortcode').find('.signin_forgot_password_div').show();
				dap_global_login_parent_obj.remove();
			}
		}); 
		dap_member_hide_loader();
		return;
	}
	return false;
}

function dap_member_check_reset_password_key_expire(){
	var dap_global_login_parent_obj = jQueryDM('#'+window.dap_global_login_parent_id);
	//console.log("++");
	//console.log(window.dap_global_login_parent_id);
	
	dm_reset_pass_key = dap_global_login_parent_obj.find('input[name="dm_reset_pass_key"]').val();
	var submit_url = "/dap/inc/reset_password.php";
	jQuery.ajax({
			url: submit_url,
			type: "POST",
			data: {"key":dm_reset_pass_key, "Keyexistcheck": "Y"},
			cache: false,
			success: function (returnval) {
				//console.log("returnval"+returnval);
				if(returnval == "" ||  returnval == undefined || returnval == null ){
					
					error_msg = dap_global_login_parent_obj.parents("#dap_member_login_frontend_shortcode").find('input[name="dap_reset_key_expired"]').val();
					window.dap_global_login_parent_id  = 'login_outer';
					
					dap_member_set_error_message(error_msg);
					dap_global_login_parent_obj.find('.reset_pass_btn').removeClass('reset_pass_btn');
				}
			}
	}); 
	
}

function dap_member_forgot_action(){
	//console.log("dap_member_forgot_action  click call ");
	var dap_global_login_parent_obj = jQueryDM('#'+window.dap_global_login_parent_id);
	//console.log("dap_global_login_parent_id  click call "+window.dap_global_login_parent_id);
	
	var email = '';
	var get_email = email = dap_global_login_parent_obj.find('.dap_forgot_email').val();
	validation_status = true;
	if(email == ""){
		validation_status = false;
		email_msg = dap_global_login_parent_obj.find('input[name="dap_email_empty"]').val()+"<br>";
		
	}else if(!dapMemberValidateEmail(email)){
		
		email_msg = dap_global_login_parent_obj.find('input[name="dap_email_valid"]').val()+"<br>";
		validation_status = false;
	}
	
	if(validation_status){
		var submit_url="/dap/inc/reset_password.php";
		var current_pageurl = window.location.href;

		if (window.location.href.indexOf('?') > 0) {
			var current_pageurl_new = current_pageurl.indexOf("?");
			var current_pageurl_new1 =  current_pageurl.substring(current_pageurl_new);
			var current_pageurl_new2 = current_pageurl.replace(current_pageurl_new1,"");
			current_pageurl = current_pageurl_new2;
		}
		
		
		//dap_member_show_loader();
		jQuery.ajax({
			url: submit_url,
			type: "POST",
			data: {"get_email":get_email , "current_pageurl":current_pageurl, "forgot": "Y"},
			cache: false,
			success: function (response) {		
				dap_member_hide_loader();
				
				/* var responsejson = jQuery.parseJSON(response); */
				var msg_status = response;
				if(msg_status =="N"){
					email_msg =	dap_global_login_parent_obj.find('input[name="dap_email_not_found"]').val();
					dap_member_set_error_message(email_msg);  
				}else{
					email_msg =	dap_global_login_parent_obj.find('input[name="dap_reset_link"]').val();
					dap_member_set_error_message(email_msg);  
				 
					//jQueryDM('.error_message_div').hide();
					//swal.fire(email_msg);
				}
				              
			
			}
		}); 
		return false;
		
		
	}else{
		dap_member_set_error_message(email_msg);
		
	}
	return false;
}


function dap_member_login_forgot_pass(){
	
	var dap_global_login_parent_obj =   jQueryDM('#'+window.dap_global_login_parent_id);
	forgot_html = dap_global_login_parent_obj.find('.dap_forgot_template_static_div #login_template_wrapper').html();	
	dap_global_login_parent_obj.find('#login_template_wrapper .login_outer').html(forgot_html); 
	dap_global_login_parent_obj.find('#login_template_wrapper .error_message_div').hide(); 
	return false;
}


function dap_member_signin_action(){

	var dap_global_login_parent_obj =   jQueryDM('#'+window.dap_global_login_parent_id);
	var email = dap_global_login_parent_obj.find('.dap_login_email').val();
	var pass = dap_global_login_parent_obj.find('.dap_login_pass').val();
	var recaptcharesponse = dap_global_login_parent_obj.find('#g-recaptcha-response').val();
	var dm_login_temp_captcha = dap_global_login_parent_obj.find('input[name="dm_login_temp_captcha"]').val();
	var email_msg = '';
	var pass_msg = '';
	var validation_status = true;
	if(email == ""){
		validation_status = false;
		email_msg = dap_global_login_parent_obj.find('input[name="dap_email_empty"]').val()+"<br>";
	}else if(!dapMemberValidateEmail(email)){
		email_msg = dap_global_login_parent_obj.find('input[name="dap_email_valid"]').val()+"<br>";
		validation_status = false;
	}
    
    if(pass == ''){
		validation_status = false;
		pass_msg = dap_global_login_parent_obj.find('input[name="dap_password_empty"]').val()+"<br>";
	} 
	captcha_msg = '';
	if(dm_login_temp_captcha == 'yes' && recaptcharesponse == ''){
		validation_status = false;
		captcha_msg = dap_global_login_parent_obj.find('input[name="dap_captcha_not_select"]').val()+"<br>";
	}
	
	dap_global_login_parent_obj.find('.error_message_div').hide();
	if(validation_status){
		var action_id = dap_global_login_parent_obj.find('input[name="action_id"]').val();		
		
		//dap_member_show_loader();
		
		var dap_fblogin_email = '';
		if(dap_global_login_parent_obj.find('#login_template_wrapper .signin_btn').hasClass('fbloginaliasactive')){
			dap_fblogin_email = dap_global_login_parent_obj.find('input[name="dap_fblogin_email"]').val();
		}
		jQueryDM.post(dap_login_ajax_url, {
			action: 'dap_member_login_authenticate',
			email: email,
			pass: pass,
			dap_action_id: action_id, 
			recaptcharesponse: recaptcharesponse,
			dm_login_temp_captcha: dm_login_temp_captcha,
			dap_fblogin_email: dap_fblogin_email,
						
		}, function(response) {
			dap_member_hide_loader();
			response = JSON.parse(response);		
			//console.log(response);
			if(response.error){
				message = response.error;
				
				dap_member_set_error_message(message);
				return ;
			}
			
			message = response.success;
			if(message != ''){
				//dap_member_fontent_sweet_alert_message(message);
			}
			if(jQuery("input[name='dap_request']").val() == 1){
				window.location.href =  jQuery("input[name='dap_login_stie_url']").val();
				return false;
			}
			
			//window.location.href = response.post_login_url;
			dap_global_login_parent_obj.find('#login_request_url').val(response.post_login_url);
			//jQueryDM('form#dap_login .signin_btn').html(jQueryDM('form#dap_login .signin_btn').html());
			
			dap_global_login_parent_obj.find('form#dap_login').attr('action', response.form_action).attr('method', 'post').submit();
			return ;
			
			
	
		});
		
		
	}else{
		msg = email_msg+pass_msg+captcha_msg;
		dap_member_set_error_message(msg);
		
	}
}



function dap_member_set_error_message(msg = ''){
	    
	    if(window.dap_global_login_parent_id == 'login_outer'){
			var dap_global_login_parent_obj =   jQueryDM('.'+window.dap_global_login_parent_id);
		}else{
			var dap_global_login_parent_obj =   jQueryDM('#'+window.dap_global_login_parent_id);
		}
	    
	    //console.log("dap_global_login_parent_id error "+window.dap_global_login_parent_id);
	    dap_global_login_parent_obj.find('#login_template_wrapper .error_message_div').show();
		dap_global_login_parent_obj.find('#login_template_wrapper span.additional_info_msg').remove();
		
		var child_selector = '#login_template_wrapper .additional_info';
		msg = "<span class='additional_info_msg'>"+msg+"</span>";
		if(dap_global_login_parent_obj.find('#login_template_wrapper .additional_info').children().length > 0 ) {
			
			//jQueryDM('#login_template_wrapper .additional_info').children().last().html(msg);
			//jQueryDM('#login_template_wrapper .additional_info').find('*:first').html(msg);
			dap_global_login_parent_obj.find('#login_template_wrapper .additional_info').find(':only-child:last').html(msg);
		}else{
			
			dap_global_login_parent_obj.find('#login_template_wrapper .additional_info').html(msg); 
		}
		
		//console.log("dap_login_parent_id start ");
		//console.log(dap_global_login_parent_obj.find('.dap_login_btn_html').html());
		//console.log("dap_login_parent_id end ");
		if(window.dap_global_login_event_trigger == 'signup'){
			
			dap_global_login_parent_obj.find('#login_template_wrapper .signin_btn').html(dap_global_login_parent_obj.find('.dap_login_btn_html').html());
		}else if(window.dap_global_login_event_trigger == 'forgot'){
			
			dap_global_login_parent_obj.find('#login_template_wrapper .forgot_btn').html(dap_global_login_parent_obj.find('.dap_login_btn_html').html());
		}else {
			//console.log("login reset");
		}     
		//console.log(window.dap_global_login_event_trigger);
		
}

 function dapMemberValidateEmail($email) {
  //var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return emailReg.test( $email );
}

