$(document).ready(function()
{
    $("#Receiver_referral_code").hide();
    $("#submit_referral_code").hide();
    $("#share_currentAdv").hide();
    $("#Resend_referral_code").hide();
    $("#Dailyshrare").hide();
    
    $("#main_facebook_url").hide();
    $("#facebook_url").hide();
    $("#facebook_likeimg").hide();
    $("#facebook_likeimg_span").hide();
    $("#whatsapp_shareImg").hide();
    $("#whatsapp_shareImg_span").hide();
    $("#facebook_likeCnt").hide();
    $("#whatsapp_shareCnt").hide();
    
    $("#facebook_likeimg_span2").hide();
    $("#facebook_url2").hide();
    
    $("#refer_login_modal").click( function(e)
	{ 					
		user=1;
		setCookie(user,30);
		url = URL+"/login/login-modals-refral.php";
		$("#LoginModalLoad").load(url); 
		$("#login").modal({
			show: 'true',
			backdrop: 'static',
			keyboard:false
		});   		
		removeCookie(); 
		e.preventDefault();
	});
	
	
    $("#FrontLineLogin").click( function(e)
	{ 					
		user=1;
		$("#AppInstl").modal('hide');
		setCookie(user,30);
		url = URL+"/login/login-modals-refral.php";
		$("#LoginModalLoad").load(url); 
		$("#login").modal({
			show: 'true',
			backdrop: 'static',
			keyboard:false
		});   		
		removeCookie(); 
		e.preventDefault();
	});
	
	
	 
	
	$("#refer_login_job").click( function(e)
	{ 					
		user=1;
		setCookie(user,30);
		url = URL+"/login/login-modals-refral.php";
		$("#LoginModalLoad").load(url); 
		$("#login").modal({
			show: 'true',
			backdrop: 'static',
			keyboard:false
		});   		
		removeCookie(); 
		e.preventDefault();
	});
    
    
	$("#role_preference").hide();
	$("#receiver_profile_frm").validate({ 
			
		errorElement:'div',
		rules:{
        
		profile_city_search:{
			 required:true			 
			},
			
		location:{
			 required:true			 
			},
		user_type:{
			 required:true			 
			}	
		},
		messages:{       
		  
		   profile_city_search:{					
              required:"What is your city"//"Please select city"			  
			},
			
			location:{					
              required:"What is your location"//"Please select location"			  
			},
			
		   user_type:{					
              required:"Please select user type"			  
			}
        } 			  
	});
	
	$("#receiver_profile_frm").validate({ 
			
		errorElement:'div',
		rules:{
        
		profile_city_search:{
			 required:true			 
			}	
		},
		messages:{       
		  
		   profile_city_search:{					
              required:"What is your city"//"Please select city"			  
			}
        } 			  
	});
	
	$("#profile_city_search").change(function(event)
	{
	    var city_id=$("#profile_city_search").val();
        event.preventDefault();
        $.ajax({	
    		type: "POST",
    		url:  URL+'/all-ajax/refralProgram.php', 
    		data: {getprofile_cityloc:1,city_id:city_id},	
    		beforeSend: function(){
    		     
    		},
    		success: function(data)
    		{
    		    var obj = jQuery.parseJSON(data);
    			source_list = '<option value="">Please Select Location</option>'; 
    			
    			for(i=0;i<obj.length;i++)			
    				source_list +='<option value='+obj[i].id+'>'+obj[i].name+'</option>'; 
    			
    			source_list +='</ul>';
    			$("#profile_location").html(source_list);
    		}   
        });
	});  
	
	$("#user_type").change(function(event)
	{
	    var user_type=$("#user_type").val();
	    if(user_type==2)
	    {
	       $("#role_preference").show();
	    }
	    else 
	    {
	       $("#role_preference").hide();
	       $(".prefredRoles_val").val("");
	    }
	    event.preventDefault();
	});    
	
	$("#receiver_profile_frm").submit(function(e)  // on click on this id...we will do folowwings
	{ 		   
		if($('#receiver_profile_frm').valid())  
		{ 
		    var formData = $('#receiver_profile_frm').serialize();  // i have taken all form in serialized format here...
		    if($("#user_type").val()==1)
		    {
		       $.ajax({	
				type: "POST",
				url: URL+"/all-ajax/refralProgram.php", 
				data: {update_receiverprofile_formData:1,formData:formData},	
    					beforeSend: function(){
    					    $('#submit_receiver_profile').addClass('disableloginbtn');
    					},
    				  success: function(val)
    				  {  		
    				    if(val==1)
    				    {
    				       $("#receiver_profile_PopUp").modal('hide');     
    				       location.reload();
    				    }
    					else if(val==0)
    					{
    						alert("Profile not updated");
    						$('#submit_receiver_profile').removeClass('disableloginbtn'); 
    					}
    					 e.preventDefault();
    				 }  
				});
		    }
		    else if($("#user_type").val()==2)
		    {
			    var count_checked = $("[name='prefredRoles[]']:checked").length; // count the checked rows
                if (count_checked == 0)
                {
                    alert("At least one job preference required");
                    return false;
                    $('#submit_receiver_profile').removeClass('disableloginbtn'); 
                    e.preventDefault();
                }
                else
                {
    				$.ajax({	
    				type: "POST",
    				url: URL+"/all-ajax/refralProgram.php", 
    				data: {update_receiverprofile_formData:1,formData:formData},	
        					beforeSend: function(){
        					    $('#submit_receiver_profile').addClass('disableloginbtn');
        					},
        				  success: function(val)
        				  {  		
        				    if(val==1)
        				    {
        				        $("#receiver_profile_PopUp").modal('hide'); 
        				        location.reload();
        				    }
        					else if(val==0)
        					{
        						alert("Profile not updated");
        						$('#submit_receiver_profile').removeClass('disableloginbtn'); 
        					}
        					 e.preventDefault();
        				 }  
    				});    
			    }
		    }
		}
		e.preventDefault();
	});
    
});    


    //function check_userReferral_status(user_status,refralurl,rec)
    function check_userReferral_status(user_status,refralurl,rec)
    {    
       
        txt = 1; 
        if(user_status==2)
        {    
             
            var r = confirm("Do you  really interested to contact advertiser ?");
            if (r == true) 
                txt = 1;
            else
                 txt = 0;
        } 
     
      
        if(txt==1)
        {    
        $("#Receiver_referral_code").hide();
        $("#submit_referral_code").hide();
        $("#Resend_referral_code").hide(); 
        //alert("Enter referral code which will appear as notifications on your mobile"); 
        $.ajax({	
    		type: "POST",
    		url:  URL+'/all-ajax/refralProgram.php', 
    		data: {check_userReferral_status:1,user_status:user_status,refralurl:refralurl},	
    		beforeSend: function(){
    		     $('#ad_seen').addClass('disableloginbtn');
    		     $('#ad_like').addClass('disableloginbtn');
    		},
    		success: function(val)
    		{
    		    if(val==1)
    		    {    
        		    $('#ad_seen').removeClass('disableloginbtn');
        		    $('#ad_like').removeClass('disableloginbtn');  
                    $("#ad_seen").hide();
                    $("#share_currentAdv").hide();
                    $("#ad_like").hide();
                    $("#PleaseSubmitResponse").hide();    
        		   /* 
            		   $("#Receiver_referral_codeId").show();
            		    $("#SubmitResend").show();   
                        $("#Receiver_referral_code").show();
                        $("#submit_referral_code").show();
                        $("#Resend_referral_code").show();  
                    */  
                    
                    
                    /// 
                            $.ajax({	
                    		type: "POST",
                    		//url:  URL+'/all-ajax/refralProgram.php', 
                    		url:  URL+'/custom_function/EmailConfigure.php', 
                    		data: {submit_userReferral_status:1,rec:rec,user_status:user_status},
                    		beforeSend: function(){
                    		    $('#submit_referral_code').addClass('disableloginbtn');
                    		},
                    		success: function(recval)
                    		{
                    		    if(recval==1)
                    		    {
                    		        $.ajax({	
                                		type: "POST",
                                		url:  URL+'/all-ajax/refralProgram.php', 
                                		data: {get_fbwhatsapp_status:1},
                                		beforeSend: function(){
                                		},
                                		success: function(data)
                                		{
                                		    var obj = jQuery.parseJSON(data); 
                                			if(obj!=null || obj!='null')
                                			{ 
                                			    $("#fb_cnt").html(obj['fb_cnt']);
                                			    $("#whatsapp_cnt").html(obj['whatsapp_cnt']);
                                			    
                                    		    $("#ad_seen").hide();
                                    		    $("#ad_like").hide();
                                    		    $("#share_currentAdv").show();
                                    		    $("#Dailyshrare").show(); 
                                    		    
                                    		    $("#facebook_url").show();
                                    		    $("#facebook_likeimg").show();
                                    		    $("#whatsapp_shareImg").show();
                                    		    $("#facebook_likeimg_span").show();
                                    		    $("#whatsapp_shareImg_span").show();
                                    		    
                                    		    $("#facebook_likeCnt").show();
                                                $("#whatsapp_shareCnt").show();
                                			}
                                		}
                    		        });
                    		    }
                    		}   
                        });        
                                
                    //
                    
    		    }
    		    else if((val==0) || (val==2))
    		    {
    		        alert("Sorry This Link Has Broken!!");
    		        window.location.href = URL;
    		    }
    		    
    		}   
        });  
      }
    }

    function submit_userReferral_status(val,rec)
    {
        var referral_code=$("#Receiver_referral_code").val();
        var otpReg = /^\d{4}$/;
        
        if((referral_code!='') && (referral_code.match(otpReg)))
        {
            $.ajax({	
        		type: "POST",
        		//url:  URL+'/all-ajax/refralProgram.php', 
        		url:  URL+'/custom_function/EmailConfigure.php', 
        		data: {submit_userReferral_status:1,val:val,rec:rec,referral_code:referral_code},
        		beforeSend: function(){
        		    $('#submit_referral_code').addClass('disableloginbtn');
        		},
        		success: function(val)
        		{
        		    $('#submit_referral_code').removeClass('disableloginbtn');
        		    if(val==0)
        		    {
        		        alert("Incorrect Referral Code!!!");
        		        $("#Receiver_referral_code").val("");
        		        $("#ad_seen").hide();
            		    $("#ad_like").hide();
            		    $("#Receiver_referral_code").show();
        	            $("#submit_referral_code").show();
            		    $("#share_currentAdv").hide();
        		    }
        		    else if(val==1)
        		    {
            		    $("#ad_seen").hide();
            		    $("#ad_like").hide();
            		    $("#Receiver_referral_code").hide();
    		            $("#submit_referral_code").hide();
            		    $("#share_currentAdv").show();
            		    $("#Dailyshrare").show(); 
            		    $("#Resend_referral_code").hide();
        		    }
        		}   
            });
        }
        else 
        {
            $("#Receiver_referral_code").show();
            $("#submit_referral_code").show();
            alert("Please enter correct 4 digit referral code");
            $("#Receiver_referral_code").val("");
        }
    }
            
    function Resend_referral_code(val)
    {
        $("#Resend_referral_code").hide();
        
        $.ajax({	
		type: "POST",
		url:  URL+'/all-ajax/refralProgram.php', 
		data: {Resend_referral_code:1},
		beforeSend: function(){
		    $('#Resend_referral_code').addClass('disableloginbtn');
		},
		success: function(val)
		{
		    $('#Resend_referral_code').removeClass('disableloginbtn');
		    alert("Referral code has been sent on your registered mobile number");
		    $("#Resend_referral_code").show();
		} 
       });	
    }














