function checkFBGDUserStatus() {
	if(userSessionId == null || userSessionId == "null" || userSessionId == "")
	{
		FB.getLoginStatus(function(response) {
	       	  if (response.session) {
				  
				 
			         FB.api('/me', function(user) {
			             if(user != null) {
			            	 
			                 var email = user.email;
							 
							   var dataString = 'email=' +email;  
             
							   //ajax call in order to see if this email is a registered user in our db
							   // --> if it is return the userId and login him 
							   // --> if it is not redirect him to signup page 
								url = "/deals/chk_fbLogin/";
				
				
			                 
			                 //ajax call in order to see if this email is a registered user in our db
			                 // --> if it is return the userId and login him 
			                 // --> if it is not return null and do not show the fbLoginbutton as he is already logged in facebook
							 $.ajax({
								 url: url,
								  data: dataString,
								 success: function(html) {
								  if(html!=null && html != "") {
						              window.location.reload();
								  }
						      }
						    });
			               }
			           });
			  }
	        });		
	}
}


function onLogIn() {
	var html=null;
		 FB.api('/me', function(user) {
	         if(user != null) {
	          var name = user.name;
               var email = user.email;
			   var userid = user.id;
			   
			   var birthday = user.birthday;
               var gender   = user.gender;
              



			   dataString = '';
			   
			 
var dataString = 'email=' +email+"&fb_id="+userid+"&name="+name+"&birthday="+birthday+"&gender="+gender;  


               //ajax call in order to see if this email is a registered user in our db
               // --> if it is return the userId and login him 
               // --> if it is not redirect him to signup page 
           		url = base_url+"deals/chk_fbLogin/";
				//url = "/check_user.php?email=" + email;
				
				
  				  $.ajax({
					 type: "POST",
  					 url: url,
					 data: dataString,
  					 success: function(html) {
						
						
						if(html=="1")
					 	{
							window.location = base_url;
						 }
  						 else if(html=="3") {
  							window.location = base_url+'login/user_login/';
  						 } 
						 else if(html=="0")
						 {
							 window.location = userRegisterUrl;
						 }
  						
  			      }
  			    });
	           }
	       });     
	}

function onLogout() {
	FB.getLoginStatus(function(response) {
     	  if (response.session) {
		         FB.api('/me', function(user) {
		             if(user != null) {
		            	FB.logout(function(response) {window.location = userLogoutUrl;});
		             }
			         
		           });
		  }
     	  else
     	  {
     		  window.location =  userLogoutUrl;
     	  }
      });		
}

