
// Set UserInfo Variables from getInfo() function
// Response definitions available here: http://wiki.developers.facebook.com/index.php/Users.getInfo#Response
var fb_first_name = document.getElementById('fb_first_name');
var fb_last_name = document.getElementById('fb_last_name');
var fb_name = document.getElementById('fb_name');
var fb_about_me = document.getElementById('fb_about_me');
var fb_activities = document.getElementById('fb_activities');
var fb_affiliations_nid = document.getElementById('fb_affiliations_nid');
var fb_affiliations_name = document.getElementById('fb_affiliations_name');
var fb_affiliations_status = document.getElementById('fb_affiliations_status');
var fb_affiliations_region = document.getElementById('fb_affiliations_region');
var fb_affiliations_year = document.getElementById('fb_affiliations_year');
var fb_birthday = document.getElementById('fb_birthday');
var fb_birthday_year = document.getElementById('fb_birthday_year');
var fb_books = document.getElementById('fb_books');
var fb_current_city = document.getElementById('fb_current_city');
var fb_current_state = document.getElementById('fb_current_state');
var fb_current_country = document.getElementById('fb_current_country');
var fb_current_zip = document.getElementById('fb_current_zip');
var fb_email_hashes = document.getElementById('fb_email_hashes');
var fb_hometown_city = document.getElementById('fb_hometown_city');
var fb_hometown_state = document.getElementById('fb_hometown_state');
var fb_hometown_country = document.getElementById('fb_hometown_country');
var fb_education_year = document.getElementById('fb_education_year');
var fb_education_name = document.getElementById('fb_education_name');
var fb_education_concentrations = document.getElementById('fb_education_concentrations');
var fb_education_degree = document.getElementById('fb_education_degree');
var fb_has_added_app = document.getElementById('fb_has_added_app');
var fb_hs1_name = document.getElementById('fb_hs1_name');
var fb_hs2_name = document.getElementById('fb_hs2_name');
var fb_hs_grad_year = document.getElementById('fb_hs_grad_year');
var fb_hs1_id = document.getElementById('fb_hs1_id');
var fb_hs2_id = document.getElementById('fb_hs2_id');
var fb_interests = document.getElementById('fb_interests');
var fb_is_app_user = document.getElementById('fb_is_app_user');
var fb_locale = document.getElementById('fb_locale');
var fb_meeting_for = document.getElementById('fb_meeting_for');
var fb_meeting_sex = document.getElementById('fb_meeting_sex');
var fb_movies = document.getElementById('fb_movies');
var fb_music = document.getElementById('fb_music');
var fb_notes_count = document.getElementById('fb_notes_count');
var fb_pic = document.getElementById('fb_pic');
var fb_pic_with_logo = document.getElementById('fb_pic_with_logo');
var fb_pic_big = document.getElementById('fb_pic_big');
var fb_pic_big_with_logo = document.getElementById('fb_pic_big_with_logo');
var fb_pic_small = document.getElementById('fb_pic_small');
var fb_pic_small_with_logo = document.getElementById('fb_pic_small_with_logo');
var fb_pic_square = document.getElementById('fb_pic_square');
var fb_pic_square_with_logo = document.getElementById('fb_pic_square_with_logo');
var fb_political = document.getElementById('fb_political');
var fb_profile_update_time = document.getElementById('fb_profile_update_time');
var fb_profile_url = document.getElementById('fb_profile_url');
var fb_proxied_email = document.getElementById('fb_proxied_email');
var fb_quotes = document.getElementById('fb_quotes');
var fb_relationship_status = document.getElementById('fb_relationship_status');
var fb_religion = document.getElementById('fb_religion');
var fb_sex_type = '';
var fb_sex = document.getElementById('fb_sex');
var fb_sex_type_male = document.getElementById('fb_sex_type_male');
var fb_sex_type_female = document.getElementById('fb_sex_type_female');
var fb_significant_other_id = document.getElementById('fb_significant_other_id');
var fb_status = document.getElementById('fb_status');
var fb_timezone = document.getElementById('fb_timezone');
var fb_tv = document.getElementById('fb_tv');
var fb_wall_count = document.getElementById('fb_wall_count');
var fb_work_location = document.getElementById('fb_work_location');
var fb_work_company_name = document.getElementById('fb_work_company_name');
var fb_work_description = document.getElementById('fb_work_description');
var fb_work_position = document.getElementById('fb_work_position');
var fb_work_location = document.getElementById('fb_work_location');
var fb_work_start_date = document.getElementById('fb_work_start_date');
var fb_work_end_date = document.getElementById('fb_work_end_date');

// check/set for null variables (hidden fields)
var return_page = document.getElementById('return_page');
var send_page = document.getElementById('send_page');
var site_www = document.getElementById('site_www');
var site_beta = document.getElementById('site_beta');
var current_dateTime = document.getElementById('current_dateTime');
var APIKey = document.getElementById('APIKey');
var loggedIn = document.getElementById('loggedIn');

	if(null != return_page){
		var return_page = document.getElementById("return_page").value;
		document.getElementById("send_page").value = return_page;
		}
	if(null != send_page){
		var send_page = document.getElementById("send_page").value;
		document.getElementById("send_page").value = send_page;
		}
	//if(null != send_page){var document.getElementById("send_page").value = return_page;}
	if(null != current_dateTime){var current_dateTime = document.getElementById("current_dateTime").value;}
	if(null != APIKey){var APIKey = document.getElementById("APIKey").value;}
	if(null != loggedIn){var loggedIn = document.getElementById("loggedIn").value;}
	if(null != site_www){var site_www = document.getElementById("site_www").value;}
	if(null != site_beta){var site_beta = document.getElementById("site_beta").value;}

//alert(APIKey)
if((site_www == '1')||(site_beta == '1')){
	FB.init(APIKey, "/content/content/xd_receiver.cfm", 
		{"ifUserConnected":onConnected}); 
}else{
	FB.init("1412ec76562ccb7bb183cd4d70876f9b", "/content/content/xd_receiver.cfm", 
		{"ifUserConnected":onConnected}); 
}

	function onConnected(){

	    FB_RequireFeatures(["XFBML"],
	
	    function(){
	      	// Get Facebook session info
	      	FB.Facebook.get_sessionState().waitUntilReady(function(){
	        // Get Current User FBUID
	        var uid = FB.Facebook.apiClient.get_session().uid;
			var fbuid = document.getElementById('fbuid');
			// Insert Current User FBUID in hidden field
			if(null != fbuid){
	        	document.getElementById("fbuid").value = uid;
			}
			// Get functions here after connected
			var api = FB.Facebook.apiClient; 
				// Result of Friends FBUIDs
				function getResults(result,exception) {
					//alert(api.get_session().uid,'you');
					//alert(result);
				}

				// Result of Current User Info
				function getInfo(result,exception) {
					if(null != fb_first_name){document.getElementById("fb_first_name").value = result[0].first_name;}
					if(null != fb_last_name){document.getElementById("fb_last_name").value = result[0].last_name;}
					if(null != fb_name){document.getElementById("fb_name").value = result[0].name;}
					if(null != fb_about_me){document.getElementById("fb_about_me").value = result[0].about_me;}
					if(null != fb_activities){document.getElementById("fb_activities").value = result[0].activities;}
					if(null != fb_affiliations_nid){document.getElementById("fb_affiliations_nid").value = result[0].affiliations.nid;}
					if(null != fb_affiliations_name){document.getElementById("fb_affiliations_name").value = result[0].affiliations.name;}
					if(null != fb_affiliations_status){document.getElementById("fb_affiliations_statusd").value = result[0].affiliations.status;}
					if(null != fb_affiliations_region){document.getElementById("fb_affiliations_region").value = result[0].affiliations.region;}
					if(null != fb_affiliations_year){document.getElementById("fb_affiliations_year").value = result[0].affiliations.year;}
					if(null != fb_birthday){document.getElementById("fb_birthday").value = result[0].birthday;}
					/*if(null != fb_birthday){
						var fb_birthday_year = result[0].birthday;
							document.getElementById("fb_birthday").value = result[0].birthday;
						var bd_year = fb_birthday_year.getFullYear();
							document.getElementById("fb_birthday_year").value = fb_birthday_year.getFullYear();
						}*/
					if(null != fb_books){document.getElementById("fb_books").value = result[0].books;}
					if(null != fb_current_city){document.getElementById("fb_current_city").value = result[0].current_location.city;}
					if(null != fb_current_state){document.getElementById("fb_current_state").value = result[0].current_location.state;}
					if(null != fb_current_country){document.getElementById("fb_current_country").value = result[0].current_location.country;}
					if(null != fb_current_zip){document.getElementById("fb_current_zip").value = result[0].current_location.zip;}
					if(null != fb_email_hashes){document.getElementById("fb_email_hashes").value = result[0].email_hashes;}
					if(null != fb_hometown_city){document.getElementById("fb_hometown_city").value = result[0].hometown_location.city;}
					if(null != fb_hometown_state){document.getElementById("fb_hometown_state").value = result[0].hometown_location.state;}
					if(null != fb_hometown_country){document.getElementById("fb_hometown_country").value = result[0].hometown_location.country;}
					if(null != fb_education_year){document.getElementById("fb_education_year").value = result[0].education_history.year;}
					if(null != fb_education_name){document.getElementById("fb_education_name").value = result[0].education_history.name;}
					if(null != fb_education_concentrations){document.getElementById("fb_education_concentrations").value = result[0].education_history.concentrations;}
					if(null != fb_education_degree){document.getElementById("fb_education_degree").value = result[0].education_history.degree;}
					if(null != fb_has_added_app){document.getElementById("fb_has_added_app").value = result[0].has_added_app;}
					if(null != fb_hs1_name){document.getElementById("fb_hs1_name").value = result[0].hs_info.hs1_name;}
					if(null != fb_hs2_name){document.getElementById("fb_hs2_name").value = result[0].hs_info.hs2_name;}
					if(null != fb_hs_grad_year){document.getElementById("fb_hs_grad_year").value = result[0].hs_info.grad_year;}
					if(null != fb_hs1_id){document.getElementById("fb_hs1_id").value = result[0].hs_info.hs1_id;}
					if(null != fb_hs2_id){document.getElementById("fb_hs2_id").value = result[0].hs_info.hs2_id;}
					if(null != fb_interests){document.getElementById("fb_interests").value = result[0].interests;}
					if(null != fb_is_app_user){document.getElementById("fb_is_app_user").value = result[0].is_app_user;}
					if(null != fb_locale){document.getElementById("fb_locale").value = result[0].locale;}
					if(null != fb_meeting_for){document.getElementById("fb_meeting_for").value = result[0].meeting_for;}
					if(null != fb_meeting_sex){document.getElementById("fb_meeting_sex").value = result[0].meeting_sex;}
					if(null != fb_movies){document.getElementById("fb_movies").value = result[0].movies;}
					if(null != fb_music){document.getElementById("fb_music").value = result[0].music;}
					if(null != fb_notes_count){document.getElementById("fb_notes_count").value = result[0].notes_count;}
					if(null != fb_pic){document.getElementById("fb_pic").value = result[0].pic;}
					if(null != fb_pic_with_logo){document.getElementById("fb_pic_with_logo").value = result[0].pic_with_logo;}
					if(null != fb_pic_big){document.getElementById("fb_pic_big").value = result[0].pic_big;}
					if(null != fb_pic_big_with_logo){document.getElementById("fb_pic_big_with_logo").value = result[0].pic_big_with_logo;}
					if(null != fb_pic_small){document.getElementById("fb_pic_small").value = result[0].pic_small;}
					if(null != fb_pic_small_with_logo){document.getElementById("fb_pic_small_with_logo").value = result[0].pic_small_with_logo;}
					if(null != fb_pic_square){document.getElementById("fb_pic_square").value = result[0].pic_square;}
					if(null != fb_pic_square_with_logo){document.getElementById("fb_pic_square_with_logo").value = result[0].pic_square_with_logo;}
					if(null != fb_political){document.getElementById("fb_political").value = result[0].political;}
					if(null != fb_profile_update_time){document.getElementById("fb_profile_update_time").value = result[0].profile_update_time;}
					if(null != fb_profile_url){document.getElementById("fb_profile_url").value = result[0].profile_url;}
					if(null != fb_proxied_email){document.getElementById("fb_proxied_email").value = result[0].proxied_email;}
					if(null != fb_quotes){document.getElementById("fb_quotes").value = result[0].quotes;}
					if(null != fb_relationship_status){document.getElementById("fb_relationship_status").value = result[0].relationship_status;}
					if(null != fb_religion){document.getElementById("fb_religion").value = result[0].religion;}
					if(null != fb_sex){document.getElementById("fb_sex").value = result[0].sex;}
					if(undefined !== fb_sex){
							fb_sex_type = result[0].sex;
							if(fb_sex_type == 'male'){
								if(null != fb_sex_type_male){document.getElementById("fb_sex_type_male").value = 'male';}
							}else{
								if(null != fb_sex_type_female){document.getElementById("fb_sex_type_female").value = 'female';}
							}
						}
					if(null != fb_significant_other_id){document.getElementById("fb_significant_other_id").value = result[0].significant_other_id;}
					if(null != fb_status){document.getElementById("fb_status").value = result[0].status;}
					if(null != fb_timezone){document.getElementById("fb_timezone").value = result[0].timezone;}
					if(null != fb_tv){document.getElementById("fb_tv").value = result[0].tv;}
					if(null != fb_wall_count){document.getElementById("fb_wall_count").value = result[0].wall_count;}
					if(null != fb_work_location){document.getElementById("fb_work_location").value = result[0].work_history.location;}
					if(null != fb_work_company_name){document.getElementById("fb_work_company_name").value = result[0].work_history.company_name;}
					if(null != fb_work_description){document.getElementById("fb_work_description").value = result[0].work_history.description;}
					if(null != fb_work_position){document.getElementById("fb_work_position").value = result[0].work_history.position;}
					if(null != fb_work_location){document.getElementById("fb_work_location").value = result[0].work_history.location;}
					if(null != fb_work_start_date){document.getElementById("fb_work_start_date").value = result[0].work_history.start_date;}
					if(null != fb_work_end_date){document.getElementById("fb_work_end_date").value = result[0].work_history.end_date;}

					/*alert("first_name "+result[0].first_name + ' ' + result[0].last_name);
					alert("about_me "+result[0].about_me);
					alert("activities "+result[0].activities);
					alert("nid "+result[0].affiliations.nid);
					alert("name "+result[0].affiliations.name);
					alert("status "+result[0].affiliations.status);
					alert("region "+result[0].affiliations.region)
					alert("year "+result[0].affiliations.year);
					alert("birthday "+result[0].birthday);
					alert("books "+result[0].books);
					alert("current city "+result[0].current_location.city);
					alert("current state "+result[0].current_location.state);
					alert("current country "+result[0].current_location.country);
					alert("current zip "+result[0].current_location.zip);
					alert("education_history "+result[0].education_history);
					alert("email_hashes "+result[0].email_hashes);
					alert("home city "+result[0].hometown_location.city);
					alert("home state "+result[0].hometown_location.state);
					alert("home country "+result[0].hometown_location.country);
					alert("edu year "+result[0].education_history.year);
					alert("edu name "+result[0].education_history.name);
					alert("edu concentrations "+result[0].education_history.concentrations);
					alert("edu degree "+result[0].education_history.degree);
					alert("has_added_app "+result[0].has_added_app);
					alert("HS hs1_name "+result[0].hs_info.hs1_name);
					alert("HS hs2_name "+result[0].hs_info.hs2_name);
					alert("HS grad_year "+result[0].hs_info.grad_year);
					alert("HS hs1_id "+result[0].hs_info.hs1_id);
					alert("HS hs2_id "+result[0].hs_info.hs2_id);
					alert("interests "+result[0].interests);
					alert("is_app_user "+result[0].is_app_user);
					alert("locale "+result[0].locale);
					alert("meeting_for "+result[0].meeting_for);
					alert("meeting_sex "+result[0].meeting_sex);
					alert("movies "+result[0].movies);
					alert("music "+result[0].music);
					alert("name "+result[0].name);
					alert("notes_count "+result[0].notes_count);
					alert("pic "+result[0].pic);
					alert("pic_with_logo "+result[0].pic_with_logo);
					alert("pic_big "+result[0].pic_big);
					alert("pic_big_with_logo "+result[0].pic_big_with_logo);
					alert("pic_small "+result[0].pic_small);
					alert("pic_small_with_logo "+result[0].pic_small_with_logo);
					alert("pic_square "+result[0].pic_square);
					alert("pic_square_with_logo "+result[0].pic_square_with_logo);
					alert("political "+result[0].political);
					alert("profile_update_time "+result[0].profile_update_time);
					alert("profile_url "+result[0].profile_url);
					alert("proxied_email "+result[0].proxied_email);
					alert("quotes "+result[0].quotes);
					alert("relationship_status "+result[0].relationship_status);
					alert("religion "+result[0].religion);
					alert("sex "+result[0].sex);
					alert("significant_other_id "+result[0].significant_other_id);
					alert("status "+result[0].status);
					alert("timezone "+result[0].timezone);
					alert("tv "+result[0].tv);
					alert("wall_count "+result[0].wall_count);
					alert("work location "+result[0].work_history.location);
					alert("work company_name "+result[0].work_history.company_name);
					alert("work description "+result[0].work_history.description);
					alert("work position "+result[0].work_history.position);
					alert("work start_date "+result[0].work_history.start_date);
					alert("work end_date "+result[0].work_history.end_date);*/
				}
				
					// Require user to login and Get Info
					api.requireLogin(function(exception) {
					// Get Friends FBUID
					api.friends_get(null,getResults);
					// Get Current User Info
					var myinfo='last_name,first_name,about_me,activities,affiliations,birthday,books,current_location,education_history,email_hashes,hometown_location,education_history,has_added_app,hometown_location,hs_info,interests,is_app_user,locale,meeting_for,meeting_sex,movies,music,name,notes_count,pic,pic_with_logo,pic_big,pic_big_with_logo,pic_small,pic_small_with_logo,pic_square,pic_square_with_logo,political,profile_update_time,profile_url,proxied_email,quotes,relationship_status,religion,sex,significant_other_id,status,timezone,tv,wall_count,work_history';
					api.users_getInfo(uid,myinfo,getInfo);
				});
			
			// Display Logged Facebook personal info here
			var fbloggedin = document.getElementById('fbloggedin');
			if(null != fbloggedin){
		       	document.getElementById("fbloggedin").innerHTML =
				"<span>"
				+ "<fb:profile-pic uid='loggedinuser' facebook-logo='true' linked='true'></fb:profile-pic><br />"
				+ "Welcome, <fb:name uid='loggedinuser' useyou='false'></fb:name>.<br />"
				+ "You are signed in with your facebook account.<br />"
				+ "If this is not you, please " + logoutFacebookBuild + " and re-login to Facebook."
				+ "</span>";
			}
					
			// If user is logged into Facebook, check to see if account has been verified with their CMS account
			var current_page = document.getElementById('current_page');
			//alert(current_page);
			if(current_page != null){
    			var current_page = document.getElementById("current_page").value;
				//alert(current_page);
				// other pages may be added to this when facebook logins are placed e.g. chat comments or article comments
				if((current_page == 'event/account:login')||(current_page == 'event=account:login')){
					// Send user to see if their facebook account is connected to their cms account
					//alert("facebookConnect");
					document.facebookConnect.submit();
				}
			}

			if(loggedIn == 'true'){
	    	//alert("1 CMS/IN - FB/IN")
				// Display what cms login links need to be displayed based on Facebook login here
			       document.getElementById("login").innerHTML =
				accountBuild + " | " + logoutFacebookBuild + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+current_dateTime;
			}else{
	    	//alert("2 CMS/OUT - FB/IN")
				// Display what cms login links need to be displayed based on Facebook login here
			       document.getElementById("login").innerHTML =
				logoutFacebookBuild + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+current_dateTime;
			}

			// This pulls Facebook personal info for FBML tags
			FB.XFBML.Host.parseDomTree();
	      	});  	
	    });
	}
	/*
	function onNotConnected(){
	    FB_RequireFeatures(["XFBML"],
	
	    function(){
			if(loggedIn == 'true'){
	    	//alert("3 CMS/IN - FB/OUT")
				// Display what cms login links need to be displayed based on Facebook login here
		       	document.getElementById("login").innerHTML =
				accountBuild + " | " + logoutBuild + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+current_dateTime;
			}else{
	    	//alert("4 CMS/OUT - FB/OUT")
				// Display what cms login links need to be displayed based on Facebook login here
		       	document.getElementById("login").innerHTML =
				loginBuild + " | " + registerBuild + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+current_dateTime;
			}
	    });
	}*/
