		var wl_dataContext;
		var wl_auth;
		var wl_direct_login;

		// Callback for when the Application successfully loads.
		function wl_appLoaded(appLoadedEventArgs)
		{
			wl_auth = Microsoft.Live.App.get_auth();
		}

		// Callback for when Sign in completes. Check whether it was successful.
		function wl_signInCompleted()
		{
			if (wl_auth.get_state() === Microsoft.Live.AuthState.failed)
			{
				Sys.Debug.trace("Authentication failed.");
				return;
			}
			else if (wl_auth.get_state() === Microsoft.Live.AuthState.authenticated)
			{
				Sys.Debug.trace("Authentication succeeded.");

				wl_dataContext = Microsoft.Live.App.get_dataContext();

				// --- get Profildata and call wl_profilesLoaded()
				wl_dataContext.loadAll(Microsoft.Live.DataType.profile, wl_profilesLoaded);

			}
		}

		function wl_signOutCompleted() {
			// Perform actions upon signing out.
			Sys.Debug.trace("Good-bye.");
		}

		// collect profildata
        function wl_profilesLoaded(dataLoadCompletedEventArgs) {
            if (dataLoadCompletedEventArgs.get_resultCode() !== Microsoft.Live.AsyncResultCode.success)
			{
                /*alert("Profiles failed to load: " + Microsoft.Live.AsyncResultCode.toString(dataLoadCompletedEventArgs.get_resultCode()));*/
                return;
            }

            var profileCollection = dataLoadCompletedEventArgs.get_data();
            var profile = profileCollection.get_item(0);

			// if there is profildata then
            if (profile)
			{
				// create loginform
				var frmlogin = '<form name="frmlogin" action="/portal.html" method="POST">';
				frmlogin = frmlogin + '<input type="hidden" name="i" value="' + wl_auth.get_cid() + '">';
				frmlogin = frmlogin + '<input type="hidden" name="e" value="' + isDefined(profile.get_emails()[0].get_address()) + '">';
				frmlogin = frmlogin + '<input type="hidden" name="u" value="' + isDefined(profile.get_title()) + '">';
				frmlogin = frmlogin + '<input type="hidden" name="fn" value="' + isDefined(profile.get_firstName()) + '">';
				frmlogin = frmlogin + '<input type="hidden" name="sn" value="' + isDefined(profile.get_lastName()) + '">';
				frmlogin = frmlogin + '<input type="hidden" name="g" value="' + Microsoft.Live.Services.GenderType.toString(profile.get_gender()) + '">';
				frmlogin = frmlogin + '<input type="hidden" name="windows_live" value="true">';
				frmlogin = frmlogin + '<div style="visibility: visible;" onclick="document.frmlogin.submit();">';
				frmlogin = frmlogin + '<a class="wlsignInLayout wlOrange-wlMD wlOrange wlMD ltr" id="container_0"><span class="wlLeft"></span><span class="wlCenter"><span id="text_0" class="wlText">'+wl_caption_login+'</span></span><span class="wlRight"></span></a></div>';
				frmlogin = frmlogin + '</form>';
				document.getElementById("site_wl").innerHTML = frmlogin;

				// if this is a actual click on the login button and not a autologin: send the form
				if (wl_direct_login == true)
					document.forms.frmlogin.submit();

            }
        }
