 (function ($) {
	"use strict";
 var base_url;

$(document).ready(function(){
	base_url = $('#base_url').val();
	
	$(document).on('click' , '#MfwLoginBtn' ,  function(){
		var email = $('#MfwLoginEmail').val(); 
		var pass = $('#MfwLoginPass').val(); 
		var remember_id = $('#remember').val(); 
		var re = /^(([^<>()\[\]\\.,;:\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,}))$/;
		if(email.trim() != ''){ 
			if(pass != ''){
				if(re.test(String(email).toLowerCase())){
					$('#MfwLoader').fadeIn();
					 $.ajax({ 
						method : 'post',
						url : base_url+'login/check_login',
						data : {'user_email' : email , 'password' : pass,'remember_id' :remember_id},
						success : function(resp){
							var respData = JSON.parse(resp);
							console.log(respData);
							//return false;
							 if(respData['status'] == 1){
							     
								toastr.success(respData['msg']);
								setTimeout(function () {
									window.location.href = base_url + respData['redirect'];
								}, 800);
							}else if(respData['status'] == 0){
								$('#MfwLoader').fadeOut(500);
								toastr.error(respData['error']);
							}else{
								$('#MfwLoader').fadeOut(500);
								toastr.error('Something went wrong, please try.');
							}
						},  
						error : function(){
							$('#MfwLoader').fadeOut(500);
							toastr.error('Something went wrong, please try.');
						} 
					}); 
				}else{
					$('#password').focus();
					toastr.error('Email should be valid.');
				}
			}else{
				$('#password').focus();
				toastr.error('Please enter password.');
			}
		}else{
			$('#user_email').focus();
			toastr.error('Please enter email.');
		} 
	});
	
	
	/* login code start */
	//fargot password
	$(document).on('click' , '#MfwLoginfargot' ,  function(){
		var email = $('#MfwLoginEmail').val();
		var email_otp =1;
		var re = /^(([^<>()\[\]\\.,;:\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,}))$/;
		if(email.trim() != ''){ 
				if(re.test(String(email).toLowerCase())){
					$('#MfwLoader').fadeIn();
					 $.ajax({ 
						method : 'post',
						url : base_url+'login/forgot_password',
						data : {'user_email' : email , 'email_otp' : email_otp},
						success : function(resp){
						
							var respData = JSON.parse(resp);
							
							 if(respData['status'] == 1){ 
								toastr.success(respData['msg']);
								setTimeout(function () {
									window.location.href = base_url + respData['redirect'];
								}, 800);
							}else if(respData['status'] == 0){
								$('#MfwLoader').fadeOut(500);
								toastr.error(respData['error']);
							}else{
								$('#MfwLoader').fadeOut(500);
								toastr.error('Something went wrong, please try.');
							}
						},  
						error : function(){
							$('#MfwLoader').fadeOut(500);
							toastr.error('Something went wrong, please try.');
						} 
					}); 
				}else{
					$('#password').focus();
					toastr.error('Email should be valid.');
				}
		}else{
			$('#user_email').focus();
			toastr.error('Please enter email.');
		} 
	});
	
	//fargot otp check
	$(document).on('click' , '#MfwLoginotp' ,  function(){
		var email = $('#MfwLoginEmail').val();
		var otp = $('#MfwLoginotp').val();
		var npwd = $('#MfwLoginNPass').val();
		var cpwd = $('#MfwLoginCPass').val();
		
		
		var re = /^(([^<>()\[\]\\.,;:\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,}))$/;
		if(email.trim() != ''){ 
		    if(otp.length>=5 || otp.length<=3){
		            toastr.error('please enter a valid otp.');
		            return false;
		  }
		        
		
		if(npwd!=cpwd){
            toastr.error('password and confirm password does not match.');
		            return false;
		        }
		if(npwd.length<5 || cpwd.length<5){
		            toastr.error('Minimum password length is 6 characters.');
		            return false;
		        }
				if(re.test(String(email).toLowerCase())){
					$('#MfwLoader').fadeIn();
					 $.ajax({ 
						method : 'post',
						url : base_url+'login/check_otp',
						data : {'user_email' : email , 'email_otp' : otp,'npwd': npwd},
						success : function(resp){
						   
							var respData = JSON.parse(resp);
							
							 if(respData['status'] == 1){ 
								toastr.success(respData['msg']);
								location.href = base_url+respData['redirect'];
							}else if(respData['status'] == 0){
								$('#MfwLoader').fadeOut(500);
								toastr.error(respData['error']);
							}else{
								$('#MfwLoader').fadeOut(500);
								toastr.error('Something went wrong, please try.');
							}
						},  
						error : function(){
							$('#MfwLoader').fadeOut(500);
							toastr.error('Something went wrong, please try.');
						} 
					}); 
				}else{
					$('#password').focus();
					toastr.error('Email should be valid.');
				}
		}else{
			$('#user_email').focus();
			toastr.error('Please enter email.');
		} 
	});
	


});

	
	/*-----------------------------------------------------
		Fix PreLoader
	-----------------------------------------------------*/
// 	$(window).on('load', function() {
// 		$(".status").fadeOut();
// 		$(".preloader").delay(350).fadeOut("slow");
// 	});
	
})(jQuery);	