function initializeAuthorizationBox(cookieNameAuthorized, cookieNameArticleList, freeArticles, remainingArticlesText, authorizedText, serviceFunctionLogin, serviceFunctionRegister, publicationUrl) {
	var authorizationToken = $.cookie(cookieNameAuthorized);
	$('div.polarisPaywall #pwLogoutBox').hide();

	if($.cookie(cookieNameAuthorized) != null) {
		$('div.polarisPaywall #pwLoginBox').hide();
		$('div.polarisPaywall #pwLogoutBox').show();

		/* Get username from cookie */
		var userIdPos = authorizationToken.indexOf('I');
		var username = authorizationToken.substring(userIdPos+1);
				
		/* Set authorization information for the user that has logged in */
		usernameHtml = $('div.polarisPaywall #pwLogoutBox .loginInformation').html();
		authorizedTextSplitted = authorizedText.split('#');
		if (authorizedTextSplitted.length<2) {
			authorizedTextSplitted.push('');
		}
		$('div.polarisPaywall #pwLogoutBox .loginInformation').html(authorizedTextSplitted[0] + usernameHtml + authorizedTextSplitted[1] );
		$('div.polarisPaywall #pwLogoutBox .loginInformation .username').text(username);
	}

	var articleList = $.cookie(cookieNameArticleList);
	var remainingArticles = 0;
	if (articleList!=null) {
		articleListArray = articleList.split(';');
		remainingArticles = parseInt(freeArticles) - articleListArray.length;
		if (remainingArticles < 0) remainingArticles = 0;
	}
	else {
		remainingArticles = parseInt(freeArticles);
	}
	itemsLeftHtml = $('div.polarisPaywall #pwLoginBox .counter').html();
	remainingArticlesTextSplitted = remainingArticlesText.split('#'); 

	if (remainingArticlesTextSplitted.length<2) {
		remainingArticlesTextSplitted.push('');
	}
	$('div.polarisPaywall #pwLoginBox .counter').html(remainingArticlesTextSplitted[0] + itemsLeftHtml + remainingArticlesTextSplitted[1] );
	
	setItemsLeftElement(remainingArticles);

	$('div.polarisPaywall #pwLoginBox a.login').click(function(){
		$("#frmLogininfo [name=func]").val(serviceFunctionLogin);
		$('#frmLogininfo').submit();
	});
	$('div.polarisPaywall #pwLoginBox a.register').click(function(){
		$("#frmLogininfo [name=func]").val(serviceFunctionRegister);
		$('#frmLogininfo').submit();
	});
	$('div.polarisPaywall #pwLogoutBox a.logout').click(function(){
		$.cookie(cookieNameAuthorized, null, { path: '/'});
		window.location.replace(publicationUrl);
	});
}

function setToken(cookieNameAuthorized, authorizationToken) {
	if (authorizationToken.length > 2) {
		var userIdPos = authorizationToken.indexOf('I');
		if (userIdPos > 1) {
			var exp = parseInt(authorizationToken.substring(1, userIdPos));
			if (exp >= 0 && authorizationToken.length > userIdPos) {
				if (exp == 0) {
					exp = 1; /* Default to one day */
				}
				$.cookie(cookieNameAuthorized, authorizationToken, {expires: exp, path: '/'});
			}
		}
	}
}

function blockVisitor(cookieNameAuthorized, cookieNameArticleList, articleId, freeArticles, blockRedirectUrl) {

	if((!$.cookie(cookieNameAuthorized)) && (blockRedirectUrl != window.location.href.split('?')[0])) {
		var remainingArticles = 0; 
		if(!$.cookie(cookieNameArticleList)) {
			remainingArticles = freeArticles - 1; 
			$.cookie(cookieNameArticleList, articleId, { path: '/', expires: 365});
		}

		/* User not authorized.
		/* Checking free articles and/or block the user from content, redirecting to registration/login facilities */
		var articleList = $.cookie(cookieNameArticleList);
		var articleArray = articleList.split(';');

		if (articleArray.length > 0){
			/* Check if article Id has been viewed previously */
            remainingArticles = freeArticles - articleArray.length;

			if($.inArray(articleId, articleArray) < 0) {
				/* article is not among already viewed, check counter */
                if (remainingArticles > 0) {
					articleArray.push(articleId);
	                $.cookie(cookieNameArticleList, articleArray.join(';'), { path:'/', expires: 365});
                }
                else {
    				/* free articles are spent, block user from viewing by redirecting to payment information page */
    				window.location.replace(blockRedirectUrl + '?serviceReturnToUrl=' + window.location.href);
                }
			}
		}
        setItemsLeftElement(remainingArticles);
	}
}	

function setItemsLeftElement(remainingArticles) {
	$('div.polarisPaywall #pwLoginBox .counter .itemsLeft').text(remainingArticles);
}

/*$(".polarisPaywall #blockDialog").dialog({
modal: true,
buttons: {
	"Ok": function() {
		$(".polarisPaywall #redirectForm").submit();
		//window.location.replace(blockRedirectUrl + '');
	},
	"Avbryt": function() {
		window.back();
	}
}
});
*/

