var TestingEvents;
TestingEvents = false


if (window.location.search.substring(1).indexOf('testevent')>0)
{
MyGetElementById('dFaceBookEvents').style.display='';
MyGetElementById('lblShowEvents').style.display='';
TestingEvents = true;
}

var FBLoginsmade =0;

//	FB.init({appId: '116574758394319', status: true, cookie: true,xfbml: true});

    function CheckFBLoggedIn()
    {
FB.getLoginStatus(function(response) {
  if (response.session) {
    // logged in and connected user, someone you know
	CheckUserPermissionsAndLogin();
  } else {
    // no user session available, someone you dont know
	LogUserIn();

  }
});

            return false;
    }

function LogUserIn(){

FBLoginsmade =FBLoginsmade+1;

FB.login(function(response) {
  if (response.session) {
    if (response.perms) {
      // user is logged in and granted some permissions.
      // perms is a comma separated list of granted permissions
	if (response.perms.indexOf("user_events")==-1)
		FBShowEventsUnCheck();

    } else {
      // user is logged in, but did not grant any permissions
	FBShowEventsUnCheck();
    }
  } else {
    // user is not logged in
	FBShowEventsUnCheck();

  }
  SubmitSearchForm();
}, {perms:'user_events'});
}

function CheckUserPermissionsAndLogin()
{
	    getUserPermissions("retUserPermissions");
}

function getUserPermissions(returnValueFunc) {

FB.api(
        {
            method : 'fql.query',
            query : 'SELECT user_events FROM permissions WHERE uid=' + FB.getSession().uid
        },
        function(response) {
            var permission = response[0];
		if (permission.user_events)
			{ 
			SubmitSearchForm();
			} else {
			LogUserIn();
			}
        });

}

function FBShowEventsUnCheck()
{
MyGetElementById('chkShowEventsInCalendar').checked=false;
alert("Unable to connect to FaceBook. Events cannot be displayed in the calendar.");
}

function SubmitSearchForm()
{
if (TestingEvents==true) 
	alert("Permissions OK. Submit form");
//__doPostBack('ctl00$ContentPlaceHolder1$aSubmit','');
MySubmitForm(); return true;
}
