var fb_register=0;
function facebook_onload(already_logged_into_facebook) {
  FB.ensureInit(function() {
      FB.Facebook.get_sessionState().waitUntilReady(function(session) {
          var is_now_logged_into_facebook = session ? true : false;
		  if (is_now_logged_into_facebook == already_logged_into_facebook) {
            return;
          }
		   if(fb_register==0) refresh_page();
        });
    });
}

function facebook_onlogin_ready() {
   refresh_page();
}
function facebook_onlogin_ready_register(){
	FB.Connect.requireSession(); 
	fb_register=1;
	FB.ensureInit(function() {
		FB.Facebook.get_sessionState().waitUntilReady(function(){
			var fbuid=FB.Facebook.apiClient.get_session().uid;
			document.getElementById('sppass1').innerHTML='fb_' + fbuid;
			document.getElementById('sppass2').innerHTML='fb_' + fbuid;
			document.getElementById('fbpass').value='fb_' + fbuid;
			placeIt('pop');
    	});
	});	
}

function refresh_page() {
  window.location = '/FConnect.php';
}

function logout(){
	new ajax("/index.php","mode=fb",onLogout);
}
function onLogout(val){
	refresh_page();
}

function invitefriends(){
	var friend_ids=""; var total=0;
	var elem=document.getElementsByTagName('input');
	for(i=0;i<elem.length;i++){
		if(elem[i].type=='checkbox' && elem[i].checked){
			total++;
            if (total > 1) {
                friend_ids += ",";
            }
			friend_ids += elem[i].value;
		}
	}
	if(total>0){
		new ajax("invite.php","friends="+friend_ids,onInvite);	
	}
}
function onInvite(val){
	alert(val);	return false;
}
/*
 * Prompts the user to grant a permission to the application.
 */
function facebook_prompt_permission(permission) {
  FB.ensureInit(function() {
    FB.Connect.showPermissionDialog(permission);
  });
}

/*
 * Show the feed form. This would be typically called in response to the
 * onclick handler of a "Publish" button, or in the onload event after
 * the user submits a form with info that should be published.
 *
 */
function facebook_publish_feed_story(form_bundle_id, template_data) {
  // Load the feed form
  FB.ensureInit(function() {
     FB.Connect.showFeedDialog(form_bundle_id, template_data);
     //FB.Connect.showFeedDialog(form_bundle_id, template_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);

      // hide the "Loading feed story ..." div
      ge('feed_loading').style.visibility = "hidden";
  });
}

/*
 * If a user is not connected, then the checkbox that says "Publish To Facebook"
 * is hidden in the "add run" form.
 *
 * This function detects whether the user is logged into facebook but just
 * not connected, and shows the checkbox if that's true.
 */
function facebook_show_feed_checkbox() {
  FB.ensureInit(function() {
      FB.Connect.get_status().waitUntilReady(function(status) {
          if (status != FB.ConnectState.userNotLoggedIn) {
            // If the user is currently logged into Facebook, but has not
            // authorized the app, then go ahead and show them the feed dialog + upsell
            checkbox = ge('publish_fb_checkbox');
            if (checkbox) {
              checkbox.style.visibility = "visible";
            }
          }
        });
    });
}

function ajax(url, vars, callbackFunction) {
	document.getElementById('friendsbox').style.display='none';
	document.getElementById('ajax_circle').style.display='block';
  if(window.XMLHttpRequest){
	request=new XMLHttpRequest();
  }else{
	  request=new ActiveXObject("Microsoft.XMLHTTP");
  }
  request.open("POST", url, true);
  request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
      if (request.responseText) {
        //callbackFunction(request.responseText);
		document.getElementById('ajax_circle').style.display='none';
		document.getElementById('friendsbox').style.display='none';
		document.getElementById('noti_send').style.display='block';
      }
    }
  };
  request.send(vars);
}
