$(document).ready(function(){
	
	 setTimeout(function(){
			 if($('#user_idd_send_money').val() !='')
			 {
				 user_id_check();
			 }
		 },2000); 
		 
		 
	/*********  Code for login   *******/
	$("#login_email").click(function(){
		document.getElementById("login_id_error").innerHTML="";
	});	
	$("#login_pwd").click(function(){
		document.getElementById("login_pwd_error").innerHTML="";
	});	
	//   email blank validation check through keyup function
	$("#login_email").blur(function(){
		var email=document.getElementById("login_email").value;
		if(email==='')
		{
			document.getElementById("login_id_error").innerHTML="Please enter the email";
			return false;
		}
		else
		{
			document.getElementById("login_id_error").innerHTML="";
			return true;
		}
	});
	// password blank validation check through keyup function
	$("#login_pwd").blur(function(){
		var email=document.getElementById("login_pwd").value;
		if(email==='')
		{
			document.getElementById("login_pwd_error").innerHTML="Please enter the password";
			return false;
		}
		else
		{
			document.getElementById("login_pwd_error").innerHTML="";
			return true;
		}
	});
	
	$("#login_check").click(function(e){
		e.preventDefault();
		var email=document.getElementById("login_email").value;
		var pwd=document.getElementById("login_pwd").value;
		if(email==""){
			document.getElementById("login_id_error").innerHTML="Please enter the email";
			return false;
		}
		else if(pwd==""){
			document.getElementById("login_pwd_error").innerHTML="Please enter the password";
			return false;
		}
		
		
		$.ajax({
		type:"POST",
		async:false,
		data:{email:email,pwd:pwd},
		url:"login_code.php",
		success:function(response){
		   // alert(response);
			//document.getElementById("err_login_pwd").innerHTML=(response);
			if(jQuery.trim(response)=="user" || jQuery.trim(response)=='admin' || jQuery.trim(response)=='mng')
			{
				swal({
				title: "Successfully",
				text: "Your login has been successfully.",
				type: "success",
				timer: 1000
			    },
				function () {
                 window.location.href="dashboard.php";
                });
		    }
			else if(jQuery.trim(response)=="err")
			{
			    	document.getElementById("login_pwd_error").innerHTML=("Invalid username or password");
			}
		}
	
	});
	});
	
	/************** signup   ***********/
	//  disable root user name
	$("#root_name").prop('disabled', true);
	$("#email_take").css('display','none');
	$("#mobile_take").css('display','none');
	$("#msg_root_id").css('display','none');
	$("#msg_sub_ref").css('display','none');
	
	
	$("#signup_button").click(function(){
		var Obj = $(this);
		var i=root_id_check();
		var a=name_check();
		var j=gender_check();
		var k=dob_check();
		var b=address_check();
		var c=signup_email_check();
		var d=number_check();
		var e=pwd_check();
		var f=re_pwd_check();
		var g=terms_check();
		
		var ee=$("#email_take").text();
		var mobile_check=$("#mobile_take").text();
		console.log('mobile '+mobile_check);
		
		var msg_root_id=$("#msg_root_id").text();
		var sub_ref=$("#msg_sub_ref").text();
		if(ee=='a' || mobile_check =='a' || msg_root_id =='b' || sub_ref=='b' || i=='' || a=='' || b=='' || c=='' || d=='' || e=='' || f=='' || g=='' ||  j=='' || k==''){
			//alert("false");
		}
		else if(ee=='b' && mobile_check == 'b'){
			
			var name=$("#username").val();
			var email=$("#emailid").val();
			var address=$("#address").val();
			var mobile=$("#mobile").val();
			var pwd=$("#password1_signup").val();
			var root_ref_id=$("#root_id").val();
			var gender=$("#user_gender").val();
			var dob=$("#dob").val();
			var position=$(".check_box:checked").val();
			
			
			$.ajax({
				type:"POST",
				async:false,
				data:{name:name,email:email,address:address,mobile:mobile,pwd:pwd,root_ref:root_ref_id,gender:gender,dob:dob,position:position},
				url:"signup_code.php",
				beforeSend:function(){
					$("#signup_button").attr('disabled', true);
					$("#signup_button").html('Registering <i class="fa fa-spinner fa-spin"></i>');
				},
				success:function(return_val){
					$("#signup_button").attr('disabled', false);
					$("#signup_button").html('Register');
					if(jQuery.trim(return_val)!='err')
					{
						window.location.href="dashboard.php";
					}
					else
					{
						$("#check_error1").text("Please check signup error");
					}
				  // console.log(return_val);
				}
			});
		}
	});
	
	/*$("#name").on('keyup blur', function () {
		name_check();
	});*/
	$("#root_id").blur(function(){
		root_id_check();
	});
	$("#username").keyup(function(){
		name_check();
	});
	$("#user_gender").change(function(){
		gender_check();
	});
	$("#address").keyup(function(){
		address_check();
	});
	$("#emailid").keyup(function(){
		signup_email_check();
	});
	$("#mobile").keyup(function(){
		number_check();
	});
	$("#password1_signup").keyup(function(){
		pwd_check();
	});
	$("#password2_signup").keyup(function(){
		re_pwd_check();
	});
	
	
	function root_id_check(){
		var root_id=document.getElementById("root_id").value;
		if(root_id=="")
		{
			document.getElementById("root_id_error1").innerHTML="Please enter root user refferance";
			$("#root_name").val("");
			return false;
		}
		else{
			$.ajax({
		type:"POST",
		async:false,
		data:{root_id:root_id},
		url:"ref_id_check.php",
		success:function(response){
		    //alert(response);
			if(jQuery.trim(response)!=2){
				$("#root_id_error1").text("");
				$("#root_name").val(response);
				$("#root_name").prop('disabled', false);
				$("#msg_root_id").text('a');
			}
			else{
				document.getElementById("root_id_error1").innerHTML="<span style='color: red'> Refferance id not found </span>";
				$("#msg_root_id").text('b');
				$("#root_name").val("");
			}
		}
	});
		}
		
	}
	
	function name_check(){   //   name check fuction
		
		var name=document.getElementById("username").value;
		var filter= /^[A-z ]+$/;
		if(name=="")
		{
			$("#username_error1").text("Please enter the name");
		}
		else if(!filter.test(name))
		{
			$("#username_error1").text("Please enter valid name");
		}
		else if(name.length<6)
		{
			$("#username_error1").text("name should be more than 5 words");
		}
		else
		{
			$("#username_error1").text("");
		}
		
	}
	function gender_check(){
		var gender=$("#user_gender").val();
		if(gender=='0')
		{
			$("#gender_error1").text('Please select your gender');
			return false;
		}
		else
		{
			$("#gender_error1").text('');
			return true;
		}
	}
	function dob_check(){
		var dob=$("#dob").val();
		if(dob=='')
		{
			$("#dob_error1").text("Select your DOB");
			return false;
		}
		else
		{
			$("#dob_error1").text("");
			return true;
		}
	}
	function address_check(){     //   address check function
		var address=document.getElementById("address").value;
		if(address=="")
		{
			$("#address_error1").text("Please enter the address");
		}
		else
		{
			$("#address_error1").text("");
		}
		
	}
	function signup_email_check(){  //   email
		var email=document.getElementById("emailid").value;
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(typeof(email) == 'undefined' || email=='')
		{
		    $('#email_error1').text("Please enter your email address");
			$("#email_take").text('a');
		}
		else if(!filter.test(email)){
    			document.getElementById("email_error1").innerHTML="Invalid email syntex";
    			$("#email_take").text('a');
    			return false;
		}
		else
		{
			
		// check email has already registered or not 
			var email=document.getElementById("emailid").value;
			$.ajax({
			type:"POST",
			async:false,
			data:{email:email},
			url:"mail_check.php",
			success:function(response){
				//document.getElementById("email_err").innerHTML=(response);
				if(response==1){
					
					document.getElementById("email_error1").innerHTML="Email has already registered with us";
					$("#email_take").text('a');
				}
				else{
					document.getElementById("email_error1").innerHTML="<span style='color: green'>  </span>";
					$("#email_take").text('b');
				}
			}
			});
		}
	}
	function number_check(){   //  number
		var number=document.getElementById("mobile").value;
		if(number=="")
		{
			document.getElementById("mobile_error1").innerHTML="Please fill the box";
			return false;
		}
		else if(isNaN(number)){
			document.getElementById("mobile_error1").innerHTML="Invalid phone number";
			return false;
		}
		else if(number.length>10 || number.length<10){
			document.getElementById("mobile_error1").innerHTML="Number should be 10 digit";
			return false;
		}
		else
		{
			
		// check mobile has already registered or not 
			var resource = 'mobile_exists_check';
			$.ajax({
			type:"POST",
			async:false,
			data:{resource:resource, number:number},
			url:"mail_check.php",
			success:function(response){
				//document.getElementById("email_err").innerHTML=(response);
				if(response==1){
					
					document.getElementById("mobile_error1").innerHTML="Number has already registered with us";
					$("#mobile_take").text('a');
				}
				else{
					document.getElementById("mobile_error1").innerHTML="<span style='color: green'>  </span>";
					$("#mobile_take").text('b');
				}
			}
			});
		}
	}
	function pwd_check(){    //   password
		var pwd=document.getElementById("password1_signup").value;
		if(pwd=="")
		{
			document.getElementById("pwd_error1").innerHTML="Please fill the box";
			return false;
		}
		else if(pwd.length<5){
			document.getElementById("pwd_error1").innerHTML="Password must be grater than or equalto 5";
			return false;
		}
		else{
			document.getElementById("pwd_error1").innerHTML="";
			return true;
		}	
	}
	function re_pwd_check(){
		var pwd=document.getElementById("password1_signup").value;
		var re_pwd=document.getElementById("password2_signup").value;
		if(pwd!=re_pwd)
		{
			$("#pwd_error2").text("Password does not matched");
		}
		else
		{
			$("#pwd_error2").text("");
		}
	}
	function terms_check(){
		
		if(!(document.getElementById('checkbox').checked))
		{
			document.getElementById("check_error1").innerHTML="Please check the terms & condition fields";
			return false;
		}
		else{
			document.getElementById("check_error1").innerHTML="";
			return true;
		}
		
	}

	
	//    mail check ajax call
	
	$("#verify_btn").click(function(){
	
		var val=$("#verify_box").val();
		var email=$("#user_email").val();
		if(val=='')
		{
			$("#verify_error").text("Please enter the 6 digit code");
			return false;
		}
		else
		{
			$("#verify_error").text("");
			
		$.ajax({
		type:"POST",
		url:"email_code_verify.php",
		data:{val:val,email:email},
		success:function(response){
		    var email=jQuery.trim(response);
			if(jQuery.trim(response)!=2)
			{
			    	$.redirect("bank_details.php",
			      [{
			        email: email,
			      }],
			      "POST",null,null,true);
			}
			else
			{
				$("#verify_error").text("Invalid verification code");
			}
		}
		});
		}
	});
	//  bank details
	$("#bank_details_btn").click(function(){
	    var f=ac_check();
	    var a=adhar_check();
	    var b=pan_check();
	    var c=holder_name_check();
	    var d=ifsc_check();
	    var e=nominee_check();
	    
	    if(a!='' && b!='' && c!='' && d!='' && e!='' && f!='')
	    {
	    var ac=$("#ac").val();
	    var user_id=$("#user_id").val();
	    var adhar=$("#adhar").val();
	    var pan=$("#pan").val();
	    var holder_name=$("#holder_name").val();
	    var ifsc=$("#ifsc").val();
	    var nominee=$("#nominee").val();
	   
	    
	    $.ajax({
	        type:"POST",
	        url:"bank_details_code.php",
	        data:{user_id:user_id,ac:ac,adhar:adhar,pan:pan,holder_name:holder_name,ifsc:ifsc,nominee:nominee},
	        success:function(response){
	            alert(response);
	            window.location.href="dashboard.php";
	        }
	    });
	
	    }   //  if close braces
	    });

	function ac_check(){
	   var ac=$("#ac").val();
	    if(ac=='')
	    {
	        $("#ac_error").text("Please enter account number");
	        return false;
	    }
	}
	function adhar_check(){
	     var adhar=$("#adhar").val();
	     if(adhar=='')
	    {
	        $("#adhar_error").text("please enter adhar/id number hh");
	        return false;
	    }
	    else
	    {
	         $("#adhar_error").text("");
	        return true;
	    }
	}
	function pan_check(){
	    var pan=$("#pan").val();
	     if(pan=='')
	    {
	        $("#pan_error").text("please enter pan number");
	        return false;
	    }
	    else
	    {
	         $("#pan_error").text("");
	        return true;
	    }
	    
	}
	function holder_name_check(){
	    var holder_name=$("#holder_name").val();
	     if(holder_name=='')
	    {
	        $("#holder_name_error").text("please enter account holder name");
	        return false;
	    }
	    else
	    {
	        $("#holder_name_error").text("");
	        return true;
	    }
	}
	function ifsc_check(){
	    var ifsc=$("#ifsc").val();
	     if(ifsc=='')
	    {
	        $("#ifsc_error").text("please enter ifsc code");
	        return false;
	    }
	    else
	    {
	        $("#ifsc_error").text("");
	        return true;
	    }
	}
	function nominee_check(){
	     var nominee=$("#nominee").val();
	        if(nominee=='')
	    {
	        $("#nominee_error").text("please enter nominee name");
	        return false;
	    }
	    else
	    {
	         $("#nominee_error").text("");
	        return true;
	    }
	}
	
	
    
    /*******************    send money    *********************/
    $("#fetch_name").val();
    $("#fetch_name").prop('disabled', true);
    
	$("#send_balance").click(function(){
	    var aa= user_id_check();
	    var bb=amount_check_send_money();
	    var cc= trans_pwd();
	    var err1=$("#msg_check").text();
	    var err2=$("#msg_check2").text();
	    var err3=$("#err_pwd_check").text();
	
	    if(err1=='b' || err2=='b' || err3=='b' || err1=='' || err2=='' ||  cc=='')
	    {
	        return false;
	    }
	    else
	    {
	        return true;
	        //  now it will call the send_money_code.php page
	    } 
	});
	
	$("#user_idd_send_money").blur(function(){
	    user_id_check();
	});
	$("#amount_send_money").blur(function(){
	    amount_check_send_money();
	});
	$("#treansaction_pwd2").blur(function(){
	    trans_pwd();
	});
	
	function user_id_check(){
        var id= $('#user_idd_send_money').val();
		var login_user_id=$("#take_user_id").val();
		if(id=="")
		{
			$("#err_user_id").text("Please enter user id");
			return false;
		}
		else if(id==login_user_id)
		{
		    $("#err_user_id").text("You can't send money to this user");
			return false;
		}
		else
		{
    		$.ajax({
    		type:"POST",
    		async:false,
    		data:{root_id:id},
    		url:"ref_id_check.php",
    		success:function(response){
    		    //alert(response);
    			if(jQuery.trim(response)!=2){
    				$("#err_user_id").text("");
    				$("#fetch_name").val(response);
    				$("#fetch_name").prop('disabled', true);
    				$("#msg_check").text('a');
    			}
    			else{
    				$("#err_user_id").text("Refferance id not found");
    				$("#msg_check").text('b');
    				$("#fetch_name").val("");
    			}
    		 }
    	  });
		}
	}
	function amount_check_send_money(){
	    
	   var amount=$("#amount_send_money").val();
	   var id=$("#take_user_id").val();
        var filter=/^[0-9]*$/;
	   if(amount=='')
	   {
	       $("#err_amount").text("Please enter the amount");
	       return false;
	   }
	   else if(!filter.test(amount))
	   {
	       $("#err_amount").text("Invalid amount");
	       return false;
	   }
	   else if(parseInt(amount)<1)
	   {
	       $("#err_amount").text("Minimum transaction limit Rs. 1");
	       return false;
	   }
	   else
	   {
        	    $.ajax({
        		type:"POST",
        		async:false,
        		data:{id:id,amount:amount},
        		url:"amount_check.php",
        		success:function(response){
        		    //alert(response);
        			if(jQuery.trim(response)!=1){
        				$("#err_amount").text("");
        				$("#msg_check2").text('a');
        			}
        			else{
        				$("#err_amount").text("Insufficient balance");
        				$("#msg_check2").text('b');
        			}
        		}
        	});
	   }
	}
	function trans_pwd(){
	    var pwd=$("#treansaction_pwd2").val();
	    if(pwd=="")
	    {
	        $("#err_trns_pwd").text("Please enter the transaction password");
	        return false;
	    }
	    else
	    {
	        var main_user=$("#take_user_id").val();
	        var pwd=$("#treansaction_pwd2").val();
	        
	         $.ajax({
        		type:"POST",
        		async:false,
        		data:{main_user:main_user,pwd:pwd},
        		url:"amount_pwd_check.php",
        		success:function(response){
        			if(jQuery.trim(response)!=2){
        				$("#err_trns_pwd").text("");
        				$("#err_pwd_check").text('a');
        			}
        			else{
        				$("#err_trns_pwd").text("Wrong password");
        				$("#err_pwd_check").text('b');
        			}
        		}
        	});
	    }
	}
	
	/************   Deduct money from user  *************/
	$("#fetch_name").val();
    $("#fetch_name").prop('disabled', true);
     $("#fetch_balance").prop('disabled', true);
    
	$("#deduct_money").click(function(){
	    var aa= user_id_check2();
	    var bb=amount_check2();
	    var cc= trans_pwd2();
	    var dd=remarks2();
	    var err1=$("#msg_check").text();
	    var err2=$("#msg_check2").text();
	    var err3=$("#err_pwd_check").text();
	
	    if(err1=='b' || err2=='b' || err3=='b' || err1=='' || err2=='' ||  cc=='' || dd=='')
	    {
	        return false;
	    }
	    else
	    {
	        return true;
	        //  now it will call the send_money_code.php page
	    } 
	});
	
	$("#user_idd").blur(function(){
	    user_id_check2();
	});
	$("#amount").blur(function(){
	    amount_check2();
	});
	$("#treansaction_pwd").blur(function(){
	    trans_pwd2();
	});
	$("#remarks").blur(function(){
	    remarks2();
	});
	function remarks2(){
	    var remarks=$("#remarks").val();
	    if(remarks=='')
	    {
	        $("#err_remarks").text("Please enter remarks");
	        return false;
	    }
	    else
	    {
	        $("#err_remarks").text("");
	        return true;
	    }
	    
	}
	function user_id_check2(){
		var id=document.getElementById("user_idd").value;
		var login_user_id=$("#take_user_id").val();
		if(id=="")
		{
			$("#err_user_id").text("Please enter user id");
			return false;
		}
		else if(id==login_user_id)
		{
		    $("#err_user_id").text("You can't deduct money to this user");
		    $("#fetch_name").val('');
		    $("#fetch_balance").val('');
			return false;
		}
		else
		{
    		$.ajax({
    		type:"POST",
    		async:false,
    		data:{root_id:id},
    		url:"deduct_ref_id_check.php",
    		success:function(response){
    		    //alert(response);
    			if(jQuery.trim(response)!=2){
    				$("#err_user_id").text("");
    				var xyz=response.split('-');
    				$("#fetch_name").val(xyz['0']);
    				$("#fetch_balance").val(xyz['1']);
    				$("#fetch_name").prop('disabled', true);
    				$("#msg_check").text('a');
    			}
    			else{
    				$("#err_user_id").text("User id not found");
    				$("#msg_check").text('b');
    				$("#fetch_name").val("");
    				$("#fetch_balance").val("");
    			}
    		 }
    	  });
		}
		
	}
	function amount_check2(){
	    
	   var amount=$("#amount").val();
	   var id=$("#user_idd").val();
        var filter=/^[0-9]*$/;
	   if(amount=='')
	   {
	       $("#err_amount").text("Please enter the amount");
	       return false;
	   }
	   else if(!filter.test(amount))
	   {
	       $("#err_amount").text("Invalid amount");
	       return false;
	   }
	   else if(parseInt(amount)<1)
	   {
	       $("#err_amount").text("Minimum transaction limit Rs. 1");
	       return false;
	   }
	   else
	   {
        	    $.ajax({
        		type:"POST",
        		async:false,
        		data:{id:id,amount:amount},
        		url:"amount_check.php",
        		success:function(response){
        		    //alert(response);
        			if(jQuery.trim(response)!=1){
        				$("#err_amount").text("");
        				$("#msg_check2").text('a');
        			}
        			else{
        				$("#err_amount").text("Insufficient balance");
        				$("#msg_check2").text('b');
        			}
        		}
        	});
	   }
	}
	function trans_pwd2(){
	    var pwd=$("#treansaction_pwd").val();
	    if(pwd=="")
	    {
	        $("#err_trns_pwd").text("Please enter the transaction password");
	        return false;
	    }
	    else
	    {
	        var main_user=$("#user_idd").val();
	        var pwd=$("#treansaction_pwd").val();
	        
	         $.ajax({
        		type:"POST",
        		async:false,
        		data:{main_user:main_user,pwd:pwd},
        		url:"amount_pwd_check.php",
        		success:function(response){
        			if(jQuery.trim(response)!=2){
        				$("#err_trns_pwd").text("");
        				$("#err_pwd_check").text('a');
        			}
        			else{
        				$("#err_trns_pwd").text("Wrong password");
        				$("#err_pwd_check").text('b');
        			}
        		}
        	});
	    }
	}
	
	/**************     buy products     *******************/

//	if($('#others').prop('checked'))
    $("#hide_rw2").css('display','none');
    $("#hide_rw3").css('display','none');
    $("#self").click(function(){
        $("#hide_rw").css('display','block');
        $("#hide_rw2").css('display','none');
        $("#hide_rw3").css('display','none');
        
        $("#err_buy_user_id").text("");
        $("#fetch_name_buy").val("");
        $("#buy_user_idd").val("");
    }); 
    $("#others").click(function(){
        $("#hide_rw").css('display','none');
        $("#hide_rw2").css('display','block');
        $("#hide_rw3").css('display','block');
        
        $("#treansaction_pwd_buy").val("");
    });
    //  onchange price fetch
    $("#product").change(function(){
        var price=this.value;
        if(price=='0')
        {
            $("#err_product").text("Please select any product");
            $("#print_tax_msg").text('');
            $("#fetch_amount").val("");
            return false;
        }
        else
        {
         var xyz=price.split('+');
        $("#fetch_amount").val(xyz['1']);
        $("#err_product").text("");
        //   check wallet balance is available or not 
    	    var fetch_id=$("#fetch_id").val();
    	   // alert("dd");
    	    $.ajax({
        		type:"POST",
        		async:false,
        		data:{id:fetch_id,amount:xyz['1']},
        		url:"amount_check.php",
        		success:function(response){
        		    //alert(response);
        			if(jQuery.trim(response)!=1){
        				$("#err_amount_not").text("");
        				tax_pers=$("#take_tax_amount").val();
        				tax=(tax_pers*xyz['1'])/100;
        				tax_with_amount=tax+parseInt(xyz['1']);
        				$("#print_tax_msg").empty();
        				$("#print_tax_msg").append("<span style='color: red; font-size: 14px; '> including " +tax_pers+ "% tax. " +tax_with_amount+ " amount will be debited </span>");
        		        //  print tax amount into hidden fields
        		        $("#print_tax_amount").val(tax);
        			}
        			else{
        				$("#err_amount_not").text("Insufficient wallet balance");
        				 $("#print_tax_msg").text('');
        			}
        		}
        	});
        }
    });
	
	//    after buy now  button click
	//$("#buy_product_btn").click(function(e){
		//e.preventDefault();
	   //$("#buy_product_btn").submit();
	   
	    /*var b=product_name_check();
	    var c=$("#err_pwd_check_buy").text();
	    trans_pwd_buy();
	    //var d=$("#err_amount_not").text();
	    if(c=='' || c=='b')
	    {
	        return false;
	    }
	    else
	    {
			console.log("true");
			$("#buy_product_btn").submit();
	    }*/
	    
	//});
	$("#buy_user_idd").blur(function(){
	    //alert("function");
	    user_id_check_buy();
	     trans_pwd_buy();
	});
	
	$("#treansaction_pwd_buy").blur(function(){
	    trans_pwd_buy();
	});
	function user_id_check_buy(){
		var id=document.getElementById("buy_user_idd").value;
		if(id=="")
		{
			$("#err_buy_user_id").text("Please enter user id");
			$("#fetch_name_buy").val("");
			return false;
		}
		else{
			$.ajax({
		type:"POST",
		async:false,
		data:{root_id:id},
		url:"ref_id_check.php",
		success:function(response){
		    //alert(response);
			if(jQuery.trim(response)!=2){
				$("#err_buy_user_id").text("");
				$("#fetch_name_buy").val(response);
				$("#fetch_name_buy").prop('disabled', true);
				$("#msg_check_buy").text('a');
			}
			else{
				$("#err_buy_user_id").text("Refferance id not found");
				$("#msg_check_buy").text('b');
				$("#fetch_name_buy").val("");
			}
		}
	});
		}
		
	}
	function product_name_check(){
	   if($("#fetch_amount").val()==''){
	       $("#err_product").text("Please select any product");
	       return false;
	   }
	}
	function trans_pwd_buy(){
	    var pwd=$("#treansaction_pwd_buy").val();
	    if(pwd=="")
	    {
	        $("#err_trns_pwd_buy").text("Please enter the transaction password");
	        return false;
	    }
	    else
	    {
	       /* if (document.getElementById('self').checked)
    	    {
    	        var main_user=$("#fetch_id").val();
    	    }
    	    else if (document.getElementById('others').checked)
    	    {
    	        var main_user=$("#buy_user_idd").val();
    	    }
    	    */
    	    
	        var main_user=$("#fetch_id").val();
	        var pwd=$("#treansaction_pwd_buy").val();
	        
	         $.ajax({
        		type:"POST",
        		async:false,
        		data:{main_user:main_user,pwd:pwd},
        		url:"amount_pwd_check.php",
        		success:function(response){
        			if(jQuery.trim(response)!=2){
        				$("#err_trns_pwd_buy").text("");
        				$("#err_pwd_check_buy").text('a');
        			}
        			else{
        				$("#err_trns_pwd_buy").text("Wrong password");
        				$("#err_pwd_check_buy").text('b');
        			}
        		}
        	});
	    }
	   
	}
	
	/**************     Add money to wallet     *******************/

	    //  after add money button click
	    $("#add_money_btn").click(function(){
	         var a=amount_check_wallet();
             var b=trans_pwd_wallet();
             var e=$("#err_pwd_check_wallet").text();
            if( a=='' || b=='' || e=='b')
            {
                return false;
            }
            else
            {
                return true;
                /*
                if (document.getElementById('self1').checked)
    	         {
    	             var sub_user=$("#take_user_id_wallet").val();
    	         }
        	    else if (document.getElementById('others1').checked)
        	    {
        	        var sub_user=$("#wallet_user_idd").val();
        	    }
        	    var amount=$("#take_amount").val();
        	    var main_user=$("#take_user_id_wallet").val();
        	    $.ajax({
        	        type:"POST",
        	        url:"add_money_code.php",
        	        data:{user:sub_user,amount:amount,main_user:main_user},
        	        success:function(response){
        	            alert(response);
        	           location.reload();
        	            
        	        }
        	    });
        	    */
        	    
            }
	    });
	
	    $("#take_amount").blur(function(){
	        amount_check_wallet();
	    });
	    $("#treansaction_pwd_wallet").blur(function(){
	        trans_pwd_wallet();
	    });
	
	function amount_check_wallet(){
	   var amount=$("#take_amount").val();
	   var id=$("#take_user_id").val();
        var filter=/^[0-9]*$/;
	   if(amount=='')
	   {
	       $("#err_wallet_amount").text("Please enter the amount");
	       return false;
	   }
	   else if(!filter.test(amount))
	   {
	       $("#err_wallet_amount").text("Invalid amount");
	       return false;
	   }
	   else if(parseInt(amount)<1)
	   {
	       $("#err_wallet_amount").text("Minimum added value is  Rs. 1");
	       return false;
	   }
	   else
	   {
           $("#err_wallet_amount").text("");
	       return true;
	   }
	}
	
		function trans_pwd_wallet(){
	    var pwd=$("#treansaction_pwd_wallet").val();
	    if(pwd=="")
	    {
	        $("#err_wallet_pwd").text("Please enter the transaction password");
	        return false;
	    }
	    else
	    {
	        var main_user=$("#fetch_id_wallet").val();
	        var pwd=$("#treansaction_pwd_wallet").val();
	        
	         $.ajax({
        		type:"POST",
        		async:false,
        		data:{main_user:main_user,pwd:pwd},
        		url:"amount_pwd_check.php",
        		success:function(response){
        			if(jQuery.trim(response)!=2){
        				$("#err_wallet_pwd").text("");
        				$("#err_pwd_check_wallet").text('a');
        			}
        			else{
        				$("#err_wallet_pwd").text("Wrong password");
        				$("#err_pwd_check_wallet").text('b');
        			}
        		}
        	});
	    }
	   
	}
	
    //    request money
    
    $("#btn_request_payout").click(function(){
        amount_check_request_money();
        if($("#err_payout_money").text()=='')
        {
            return true;
        }
        else
        {
            return false;
        }
    });
    $("#request_payout_money").blur(function(){
        amount_check_request_money();
    });
    function amount_check_request_money(){
	   var amount=$("#request_payout_money").val();
	   var id=$("#take_user_id").val();
        var filter=/^[0-9]*$/;
	   if(amount=='')
	   {
	       $("#err_payout_money").text("Please enter the amount");
	       $("#print_msg").text("");
	       return false;
	   }
	   else if(!filter.test(amount))
	   {
	       $("#err_payout_money").text("Invalid amount");
	        $("#print_msg").text("");
	       return false;
	   }
	   else if(parseInt(amount)<1)
	   {
	       $("#err_payout_money").text("Minimum transaction limit Rs. 1");
	        $("#print_msg").text("");
	       return false;
	   }
	   else
	   {
        	    $.ajax({
        		type:"POST",
        		async:false,
        		data:{id:id,amount:amount},
        		url:"amount_check.php",
        		success:function(response){
        		    //alert(response);
        			if(jQuery.trim(response)!=1){
        				$("#err_payout_money").text("");
        				var aa=$("#get_tax_amount").val();   //  get tax amount
        				fee=((aa*amount)/100);
        				total_amount=fee+parseInt(amount);
        				$("#print_msg").empty();
        				$("#print_msg").append("<span style='color: red; font-size: 14px; '> including " +aa+ "% transaction fees. " +total_amount+ " amount will be debited </span>");
        		        // print into hidden field
        		        $("#aft_cal_amount").val(fee);
        		        
        			}
        			else{
        				$("#err_payout_money").text("Insufficient balance");
        				 $("#print_msg").text("");
        			}
        		}
        	});
	   }
	}
	
	
	/////     coupons generate
	$("#generate").click(function(){
	    a=coupons_user_id_check();
	    b=category_check();
	    c=coupon_amount_check();
	    d=coupon_check();
	    err=$("#err_id_check").text();
	    if(a=='' || err=='err' || b=='' || c=='' || d=='')
	    {
	        return false;
	    }
	    else
	    {
	        return true;
	    }
	    
	});
	$("#user_id").blur(function(){
	    coupons_user_id_check();
	});
	$("#coupon_category").change(function(){
	    category_check();
	});
		$("#coupon_amount").blur(function(){
	    coupon_amount_check();
	});
		$("#coupon_code").blur(function(){
	    coupon_check();
	});
	
	function coupons_user_id_check(){
		var id=document.getElementById("user_id").value;
		if(id=="")
		{
			$("#err_user_id").text("Please enter user id");
			 $("#user_name").val("");
			return false;
		}
		else
		{
    		$.ajax({
    		type:"POST",
    		async:false,
    		data:{user_id:id,coupons_user_id:'check'},
    		url:"action.php",
    		success:function(response){
    		    if(jQuery.trim(response)=='err')
    		    {
    		        $("#err_user_id").text("User id not found");
    		        $("#err_id_check").text("err'");
    		    }
    		    else
    		    {
    		         $("#err_user_id").text("");
    		        $("#user_name").val(response);
    		        $("#err_id_check").text("correct");
    		    }
    		 }
    	  });
		}
		
	  }
	  function category_check(){
	      cat=$("#coupon_category").val();
	      if(cat=='0' || cat==null)
	      {
	          $("#err_coupon_category").text("Please select any category");
	          return false;
	      }
	      else
	      {
	           $("#err_coupon_category").text("");
	          return true;
	      }
	  }
	  function coupon_amount_check(){
	      amount=$("#coupon_amount").val();
	      var filter=/^[0-9]*$/;

	      if(amount=='')
	      {
	          $("#err_coupon_amount").text("Please enter the amount");
	          return false;
	      }
	      else if(!filter.test(amount))
	      {
	          $("#err_coupon_amount").text("Invalid input");
	          return false;
	      }
	      else 
	      {
	           $("#err_coupon_amount").text("");
	          return true;
	      }
	  }
	function coupon_check(){
	      coupon_code=$("#coupon_code").val();
	      if(coupon_code=='')
	      {
	          $("#err_coupon_code").text("Please enter coupon code");
	          return false;
	      }
	      else 
	      {
	           $("#err_coupon_code").text("");
	          return true;
	      }
	  }
	
	
	
	
	
	
});

//////  #################     active deactive user
 function change_status(id)
    	 {
    	     $.ajax({
    	         type:"POST",
    	         url:"change_status.php",
    	         data:{id:id},
    	         success:function(response){
    	             if(jQuery.trim(response)=='Active')
    	             {
    	                 $("#"+id).val(response);
    	                  $("#"+id).attr('disabled', false);
    	             }
    	             else
    	             {
    	                  $("#"+id).val(response);
    	                  $("#"+id).attr('disabled', true);
    	             }
    	         }
    	     });
    	 }
////////////#######     member joining graph for admin
        $(document).ready(function () {
            showGraph('2019');
            $("#slt_year").change(function(){
                 showGraph(this.value);
            });
        });


        function showGraph(year)
        {
            {
                $.post("data_admin.php?year="+year,
                function (data)
                {
                    var xyz= ['January','February','March','April','May','June','July','August','Septepber','October','November','December'];
                    var aa=JSON.parse(data);
                    //alert(data);
                    var chartdata = {
                        labels: xyz,
                        datasets: [
                            {
                                label: 'User joined',
                                backgroundColor: '#49e2ff',
                                borderColor: '#46d5f1',
                                hoverBackgroundColor: '#CCCCCC',
                                hoverBorderColor: '#ffffff',
                                data: aa
                            }
                        ]
                    };
                    var graphTarget = $("#graphCanvas");
                    var barGraph = new Chart(graphTarget, {
                        type: 'line',
                        data: chartdata
                    });
                });
            }
        }

//////////////      user credential send over email
$(document).ready(function(){
    // $("#example tbody").on('click','tr td.email_send', function(){
     $('#example tbody').on( 'click', '.email_send', function () {
	         id=this.id;
	         $.ajax({
	             type:"POST",
	             url:"email_send.php",
	             data:{id:this.id},
	             beforeSend:function(){
	                $("#"+id).val("Sending..");
	                $("#"+id).prop('disabled',true);
	             },
	             success:function(response){
	                 //alert(response);
	             },
	             complete: function() {
                      //$("#"+id).prop('disabled',false);
                       $("#"+id).val("Success");
                       //$("#"+id).append('<i class="fa fa-dashboard"></i>')
                }
	         });
	});
});
        
