function fbConnect() {
	FB.login(function(response) {
		if (response.authResponse) {
			getAppFriends(response.authResponse)
		} else {}
	}, {scope:'email,user_about_me,user_hometown,user_religion_politics,user_notes,user_activities,user_birthday,user_interests,user_photos,user_videos,user_photo_video_tags'});
}

function getAppFriends(session) {
	$.post('./ajax/fbconnect.php', {"fb_sid":session}, function(reply) {
		if (reply.result) {
			switch (reply.next) {
				case 'redirect':
					var r = reply.data;
					location.href = reply.link;
					break;
				case 'register':
					$('.fb_connect_button:first').attr('href', './?action=fbreg&fb_sid[access_token]=' + reply.data.access_token
						+ '&fb_sid[expires]=' + reply.data.expires
						+ '&fb_sid[secret]=' + reply.data.secret
						+ '&fb_sid[session_key]=' + reply.data.session_key
						+ '&fb_sid[sig]=' + reply.data.sig
						+ '&fb_sid[uid]=' + reply.data.uid).click(function(){
							return hs.htmlExpand(this, {objectType: 'ajax'});
						}).trigger('click');
					break;
			}
		} else {
			alert(reply.message);
		}
	}, "json");
}

function nextStep(elm){
	$(elm).attr("disabled", true);
	var suggestions = new Array();
	
	for (var i = 1; i < 4; i++) {
		if ($('#suggestion_' + i).val()) {
			if ($('#suggestion_' + i).attr('checked') == true)
				suggestions.push($('#suggestion_' + i).val());
		}
	}
	
	$.post('./ajax/fbreg.php', {"username":$('#fbreg_username').val(), "email":$('#email').val(), "suggestions":suggestions}, function(reply) {
		if (reply.result) {
			$('#additional_info_1').html('<label for="email">Account wurde angelegt</label>');
			$('#additional_info_2').html('').append($('#step_2_data'));
			$('#step_2_data').show();
			$('#layer').addClass('step2');
		} else {
			if (reply.message.x_username) {
				alert(reply.message.x_username);
			}
			if (reply.message.general) {
				alert(reply.message.general);
			}
			if (reply.message.x_email) {
				alert(reply.message.x_email);
			}
			$(elm).attr("disabled", false);
		}
	}, "json");
    return false;
}

function lastStep(elm){
	$(elm).attr("disabled", true);
	$.post('./ajax/fbregcomplite.php', {
			"countryCode":$('#choose_city').val(),
			"dateOfBirth_day":$('#birthday_day').val(),
			"dateOfBirth_month":$('#birthday_month').val(),
			"dateOfBirth_year":$('#birthday_year').val()
		},
		function(reply) {
			if (reply.result) {
				location.href = reply.link;
			} else {
				if (reply.message.x_dateOfBirth) {
					alert(reply.message.x_dateOfBirth);
				}
				$(elm).attr("disabled", false);
			}
		},
		"json");
}
