function showErr(err){
	var txt = "There was an error on this page.\n\n";
	txt += "Error description: " + err.description + "\n\n";
	txt += "Click OK to continue.\n\n";
	alert(txt);
}

swfobject.registerObject("flash-body", "9.0.0", "flash/expressInstall.swf");

var strPageURL = window.location.href;

function setURL(strNewURL){
	strPageURL = strNewURL;
}

var bolBodyLoaded = false;
var FBUserID = null;

var flash;
window.onload = function(){
	flash = swfobject.getObjectById("flash-body");

	bolBodyLoaded = true;
	try{
		if (FBUserID != null) flash.fblogin(FBUserID, false);
	}catch(err){
		showErr(err);
	}
}



var FACEBOOK = 2;
var DATABASE = 3;
var NOTLOGGEDIN = 1;
var logintype = NOTLOGGEDIN;

function logout(){
	if (bolBodyLoaded == true && flash) flash.logout();
	switch(logintype) {
  		case FACEBOOK:
			document.getElementById('facebookname').style.display = "none";
			document.getElementById("facebooklogin").style.height = "38px";
			document.getElementById("facebooklogin").style.paddingTop = "12px";
			break;
		case DATABASE:
			document.getElementById('dbname').style.display = "none";
			document.getElementById("facebooklogin").style.display = "block";
	}
	logintype = NOTLOGGEDIN;
	changeNotLoggedIn("block");
	document.getElementById('btnRegister').innerHTML = "Register";
}


function loggedin(name){
	logintype = DATABASE;
	document.getElementById("dbname").innerHTML = "Welcome " + name + ", <a href='#' onclick='logout()'>Click here to log out</a>";
	document.getElementById("dbname").style.display = "block";
	document.getElementById("facebookname").style.display = "none";
	document.getElementById("facebooklogin").style.display = "none";
	changeNotLoggedIn("none");
	document.getElementById('btnRegister').innerHTML = "Update";
}

function checkFBConnection(){
//	//alert(FBUserID);
	return FBUserID;
}

function onFBConnected(user_id) {
	
	if(logintype != DATABASE){
		FBUserID = user_id;
		
		logintype = FACEBOOK;	
		document.getElementById("facebooklogin").style.height = "50px";
		document.getElementById("facebooklogin").style.paddingTop = "0px";
		document.getElementById("facebookname").style.display = "block";
		document.getElementById('btnRegister').innerHTML = "Update";
			
		changeNotLoggedIn("none");
		
		if (!flash) flash = swfobject.getObjectById("flash-body");
		try{
			if (bolBodyLoaded == true && flash) flash.fblogin(FBUserID, true);  
		}catch(err){
			showErr(err);
		}
	}
}

function onNotFBConnected() {
	if(logintype != DATABASE){
		try{
			if (bolBodyLoaded == true && flash && FBUserID) flash.logout();
		}catch(err){
			showErr(err);
		}	
		FBUserID = null;
		logout();
	}
}

function changeNotLoggedIn(displayrule) {
	document.getElementById("dblogin").style.display = displayrule;
}

 // Browser-agnostic factory function
function _createXMLHttpRequest() {
   if (window.XMLHttpRequest) {
     return new XMLHttpRequest();
   } else if (window.ActiveXObject) {
     return new ActiveXObject('Microsoft.XMLHTTP');
   } else {
     flash.FBXMLerror("Could not create XMLHttpRequest on this browser");
     return null;
   }
 }
  
 function showDialog(){
	var strURL = strPageURL + "?m=f&r=" + flash.getRegistrationID();
	FB.Connect.showShareDialog(strURL, onStreamPublished);
 }
 
 function catchLogin(user_id){
	//alert("onStreamPublished called");
	showDialog();
 }
 
 function publishOnFacebook(){
	
	if (FB.Connect.get_loggedInUser() == null){
		FB.Connect.requireSession(catchLogin);
	}else{
		showDialog();
	}
 }
 
 function onStreamPublished(post_id, exception) {
	 if (exception == null){
	 	//if (post_id != null && post_id != "null"){
			var objHttp = _createXMLHttpRequest();
			objHttp.open("POST", "feeds/save-fb-post.asp", false);
			objHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			
			objHttp.send("RegistrationID=" + flash.getRegistrationID());
			
			flash.FBXMLsuccess(objHttp.responseText);
			
		//}	
	 }else{
		 flash.FBXMLerror(exception);
	 }
	 if(logintype == DATABASE){
		 FB.Connect.logout(null);
	 }
 }
 
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}