window.fbAsyncInit = function() 
{
	window.FB.init({
	  appId      : '1118539591872290',
	  cookie     : true,
	  xfbml      : true,
	  version    : 'v7.0'
	});
	window.FB.AppEvents.logPageView();  
};

(function(d, s, id){
 var js, fjs = d.getElementsByTagName(s)[0];
 if (d.getElementById(id)) {return;}
 js = d.createElement(s); js.id = id;
 js.src = "https://connect.facebook.net/en_US/sdk.js";
 fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function checkLoginState()
{
	window.FB.getLoginStatus(
		function(response)
		{
			if (response.status === 'connected') 
			{
				// The user is logged in and has authenticated your
				// app, and response.authResponse supplies
				// the user's ID, a valid access token, a signed
				// request, and the time the access token 
				// and signed request each expire.
				var data = {};
				data.uid = response.authResponse.userID;
				data.accessToken = response.authResponse.accessToken;
				console.log(data);
				callModule("lib/registerFacebookUser.php", data, populateForm);
			}
			else
			{ 
				if (response.status === 'not_authorized')
				{
				// The user hasn't authorized your application.  They
				// must click the Login button, or you must call FB.login
				// in response to a user gesture, to launch a login dialog.
				}
				else
				{
				// The user isn't logged in to Facebook. You can launch a
				// login dialog with a user gesture, but the user may have
				// to log in to Facebook before authorizing your application.
				}
			}
		});
}

function populateForm()
{
	//name,birthday,first_name,gender,last_name,email
	var data = JSON.parse(document.getElementById('facebookUserData').value);
	document.getElementById('edtName').value = data.first_name;
	document.getElementById('edtSurname').value = data.last_name;
	document.getElementById('edtEmail').value = data.email;
	document.getElementById('edtBirthDate').value = data.birthday;
	joinUser();
}