$(document).ready(function()
{
    $("#Receiver_referral_code").hide();
    $("#submit_referral_code").hide();
    $("#share_currentAdv").hide();
    $("#Resend_referral_code").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();
	});
    
    
	$("#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:"Please select city"			  
			},
			
			location:{					
              required:"Please select location"			  
			},
			
		   user_type:{					
              required:"Please select user type"			  
			}
        } 			  
	});
	
	$("#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)
    {
        $.ajax({	
    		type: "POST",
    		url:  URL+'/all-ajax/refralProgram.php', 
    		data: {submit_userReferral_status:1,user_status:user_status,refralurl:refralurl,rec:rec},
    		beforeSend: function(){
    		    if(user_status==1){
    		        $('#ad_seen').addClass('disableloginbtn');    
    		        $('#ad_like').hide();    
    		    }
    		    else if(user_status==2){
    		        $('#ad_like').addClass('disableloginbtn'); 
    		        $('#ad_seen').hide();    
    		    }
    		},
    		success: function(val)
    		{
    		    if(val==1)
    		    {
        		    $("#ad_seen").hide();
        		    $("#ad_like").hide();
        		    $("#share_currentAdv").show();
    		    }
    		    else if((val==0) || (val==2))
    		    {
    		        $('#ad_seen').show();
    		        $('#ad_like').show();
    		        $('#ad_seen').removeClass('disableloginbtn');
                    $('#ad_like').removeClass('disableloginbtn');
    		         
    		        alert("Link Broken");
    		    }
    		}   
       });    
        /*$("#Receiver_referral_code").hide();
        $("#submit_referral_code").hide();
        $("#Resend_referral_code").hide();
        
        $.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();
                    $("#ad_like").hide();
                    $("#Receiver_referral_code").show();
                    $("#submit_referral_code").show();
                    $("#Resend_referral_code").show();
                    $("#share_currentAdv").hide();
    		    }
    		    else if((val==0) || (val==2))
    		    {
    		        alert("Link Broken");
    		    }
    		    
    		}   
        });*/
    }

    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', 
        		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();
            		    $("#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();
		} 
       });	
    }














